-
Couldn't load subscription status.
- Fork 8k
Implement GH-18550: Implement getElementsByClassName() #19108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fca2bbb
00e2303
85af612
8e7f320
fd749fb
210397f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| #if defined(HAVE_LIBXML) && defined(HAVE_DOM) | ||
| #include "php_dom.h" | ||
| #include "obj_map.h" | ||
| #include "token_list.h" | ||
|
|
||
| static zend_always_inline void objmap_cache_release_cached_obj(dom_nnodemap_object *objmap) | ||
| { | ||
|
|
@@ -40,6 +41,30 @@ static zend_always_inline void reset_objmap_cache(dom_nnodemap_object *objmap) | |
| objmap->cached_length = -1; | ||
| } | ||
|
|
||
| static bool dom_matches_class_name(const dom_nnodemap_object *map, const xmlNode *nodep) | ||
| { | ||
| bool ret = false; | ||
|
|
||
| if (nodep->type == XML_ELEMENT_NODE) { | ||
| xmlAttrPtr classes = xmlHasNsProp(nodep, BAD_CAST "class", NULL); | ||
| if (classes != NULL) { | ||
| bool should_free; | ||
| xmlChar *value = php_libxml_attr_value(classes, &should_free); | ||
|
|
||
| bool quirks = map->baseobj->document->quirks_mode == PHP_LIBXML_QUIRKS; | ||
| if (dom_ordered_set_all_contained(map->array, (const char *) value, quirks)) { | ||
| ret = true; | ||
| } | ||
|
|
||
| if (should_free) { | ||
| xmlFree(value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| /************************** | ||
| * === Length methods === * | ||
| **************************/ | ||
|
|
@@ -106,6 +131,24 @@ static zend_long dom_map_get_by_tag_name_length(dom_nnodemap_object *map) | |
| return count; | ||
| } | ||
|
|
||
| static zend_long dom_map_get_by_class_name_length(dom_nnodemap_object *map) | ||
| { | ||
| xmlNodePtr nodep = dom_object_get_node(map->baseobj); | ||
| zend_long count = 0; | ||
| if (nodep) { | ||
| xmlNodePtr basep = nodep; | ||
| nodep = php_dom_first_child_of_container_node(basep); | ||
|
|
||
| while (nodep != NULL) { | ||
| if (dom_matches_class_name(map, nodep)) { | ||
| count++; | ||
| } | ||
| nodep = php_dom_next_in_tree_order(nodep, basep); | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| static zend_long dom_map_get_zero_length(dom_nnodemap_object *map) | ||
| { | ||
| return 0; | ||
|
|
@@ -276,6 +319,10 @@ static void dom_map_collection_named_item_elements_iter(dom_nnodemap_object *map | |
| } | ||
| } | ||
|
|
||
| static void dom_map_collection_named_item_null(dom_nnodemap_object *map, php_dom_obj_map_collection_iter *iter) | ||
| { | ||
| } | ||
|
|
||
| static void dom_map_get_by_tag_name_item(dom_nnodemap_object *map, zend_long index, zval *return_value) | ||
| { | ||
| xmlNodePtr nodep = dom_object_get_node(map->baseobj); | ||
|
|
@@ -292,12 +339,54 @@ static void dom_map_get_by_tag_name_item(dom_nnodemap_object *map, zend_long ind | |
| } | ||
| } | ||
|
|
||
| static void dom_map_get_by_class_name_item(dom_nnodemap_object *map, zend_long index, zval *return_value) | ||
| { | ||
| xmlNodePtr nodep = dom_object_get_node(map->baseobj); | ||
| xmlNodePtr itemnode = NULL; | ||
| if (nodep && index >= 0) { | ||
| dom_node_idx_pair start_point = dom_obj_map_get_start_point(map, nodep, index); | ||
| if (start_point.node) { | ||
| if (start_point.index > 0) { | ||
| /* Only start iteration at next point if we actually have an index to seek to. */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, I did a correction here because we don't want to immediately skip the current index if the current index is already the one we need. |
||
| itemnode = php_dom_next_in_tree_order(start_point.node, nodep); | ||
| } else { | ||
| itemnode = start_point.node; | ||
| } | ||
| } else { | ||
| itemnode = php_dom_first_child_of_container_node(nodep); | ||
| } | ||
|
|
||
| do { | ||
| --start_point.index; | ||
| while (itemnode != NULL && !dom_matches_class_name(map, itemnode)) { | ||
| itemnode = php_dom_next_in_tree_order(itemnode, nodep); | ||
| } | ||
| } while (start_point.index > 0 && itemnode); | ||
| } | ||
| dom_ret_node_to_zobj(map, itemnode, return_value); | ||
| if (itemnode) { | ||
| dom_map_cache_obj(map, itemnode, index, return_value); | ||
| } | ||
| } | ||
|
|
||
| static void dom_map_collection_named_item_by_tag_name_iter(dom_nnodemap_object *map, php_dom_obj_map_collection_iter *iter) | ||
| { | ||
| iter->candidate = dom_get_elements_by_tag_name_ns_raw(iter->basep, iter->candidate, map->ns, map->local, map->local_lower, &iter->cur, iter->next); | ||
| iter->next = iter->cur + 1; | ||
| } | ||
|
|
||
| static void dom_map_collection_named_item_by_class_name_iter(dom_nnodemap_object *map, php_dom_obj_map_collection_iter *iter) | ||
| { | ||
| xmlNodePtr basep = iter->basep; | ||
| xmlNodePtr nodep = iter->candidate ? php_dom_next_in_tree_order(iter->candidate, basep) : php_dom_first_child_of_container_node(basep); | ||
|
|
||
| while (nodep != NULL && !dom_matches_class_name(map, nodep)) { | ||
| nodep = php_dom_next_in_tree_order(nodep, basep); | ||
| } | ||
|
|
||
| iter->candidate = nodep; | ||
| } | ||
|
|
||
| static void dom_map_get_null_item(dom_nnodemap_object *map, zend_long index, zval *return_value) | ||
| { | ||
| RETURN_NULL(); | ||
|
|
@@ -478,6 +567,16 @@ const php_dom_obj_map_handler php_dom_obj_map_by_tag_name = { | |
| .nameless = true, | ||
| }; | ||
|
|
||
| const php_dom_obj_map_handler php_dom_obj_map_by_class_name = { | ||
| .length = dom_map_get_by_class_name_length, | ||
| .get_item = dom_map_get_by_class_name_item, | ||
| .get_ns_named_item = dom_map_get_ns_named_item_null, | ||
| .has_ns_named_item = dom_map_has_ns_named_item_null, | ||
| .collection_named_item_iter = dom_map_collection_named_item_by_class_name_iter, | ||
| .use_cache = true, | ||
| .nameless = true, | ||
| }; | ||
|
|
||
| const php_dom_obj_map_handler php_dom_obj_map_child_nodes = { | ||
| .length = dom_map_get_nodes_length, | ||
| .get_item = dom_map_get_nodes_item, | ||
|
|
@@ -533,7 +632,7 @@ const php_dom_obj_map_handler php_dom_obj_map_noop = { | |
| .get_item = dom_map_get_null_item, | ||
| .get_ns_named_item = dom_map_get_ns_named_item_null, | ||
| .has_ns_named_item = dom_map_has_ns_named_item_null, | ||
| .collection_named_item_iter = NULL, | ||
| .collection_named_item_iter = dom_map_collection_named_item_null, | ||
| .use_cache = false, | ||
| .nameless = true, | ||
| }; | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --TEST-- | ||
| Dom\Element::getElementsByClassName() empty class names | ||
| --EXTENSIONS-- | ||
| dom | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $dom = Dom\HTMLDocument::createFromString(<<<HTML | ||
| <div class=" foo bar "> | ||
| <p id="child"></p> | ||
| </div> | ||
| HTML, LIBXML_NOERROR); | ||
|
|
||
| $collection = $dom->documentElement->getElementsByClassName(""); | ||
| var_dump($collection->count()); | ||
|
|
||
| foreach ($collection as $node) { | ||
| throw new Error("unreachable"); | ||
| } | ||
|
|
||
| var_dump($dom->getElementsByClassName(" ")->count()); | ||
| var_dump($dom->getElementsByClassName("\t")->count()); | ||
| var_dump($dom->getElementsByClassName("\t\n\f\v")->count()); | ||
| var_dump($dom->getElementsByClassName("\t\n\f\v")->namedItem("child")); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| int(0) | ||
| int(0) | ||
| int(0) | ||
| int(0) | ||
| NULL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| --TEST-- | ||
| Dom\Element::getElementsByClassName() non quirks mode | ||
| --EXTENSIONS-- | ||
| dom | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $dom = Dom\HTMLDocument::createFromString(<<<HTML | ||
| <!DOCTYPE html> | ||
| <div id="container"> | ||
| <p class="Bar">1</p> | ||
| <p class="bar">2</p> | ||
| <p class="Bar Foo">3</p> | ||
| <p class="Bar foo">4</p> | ||
| <p class="foo bar">5</p> | ||
| <p class="foo bar" name="here">6</p> | ||
| </div> | ||
| <div> | ||
| <p class="Bar">7</p> | ||
| <p class="bar">8</p> | ||
| <p class="Bar Foo">9</p> | ||
| <p class="Bar foo">10</p> | ||
| <p class="foo bar">11</p> | ||
| </div> | ||
| HTML); | ||
|
|
||
| $collection = $dom->getElementsByClassName("foo \n bar"); | ||
|
|
||
| echo "There are {$collection->length} items in the document in total that have both \"foo\" and \"bar\"\n"; | ||
|
|
||
| $collection = $dom->getElementById('container')->getElementsByClassName("foo \n bar"); | ||
|
|
||
| echo "There are {$collection->length} items in #container in total that have both \"foo\" and \"bar\"\n"; | ||
|
|
||
| foreach ($collection as $key => $node) { | ||
| echo "--- Key $key ---\n"; | ||
| var_dump($node->tagName, $node->textContent); | ||
| var_dump($node === $collection->item($key)); | ||
| } | ||
|
|
||
| var_dump($collection->namedItem("here")->textContent); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| There are 3 items in the document in total that have both "foo" and "bar" | ||
| There are 2 items in #container in total that have both "foo" and "bar" | ||
| --- Key 0 --- | ||
| string(1) "P" | ||
| string(1) "5" | ||
| bool(true) | ||
| --- Key 1 --- | ||
| string(1) "P" | ||
| string(1) "6" | ||
| bool(true) | ||
| string(1) "6" |
Uh oh!
There was an error while loading. Please reload this page.