-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Merged
+364
−4
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fca2bbb
Implement GH-18550: Implement getElementsByClassName()
nielsdos 00e2303
fixes
nielsdos 85af612
Tests
nielsdos 8e7f320
Can use HASH_MAP
nielsdos fd749fb
Make test a bit more logical
nielsdos 210397f
NEWS and UPGRADING
nielsdos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
ext/dom/tests/modern/common/Element_getElementsByClassName_empty.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
55 changes: 55 additions & 0 deletions
55
ext/dom/tests/modern/common/Element_getElementsByClassName_non_quirks.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.