Skip to content

WIP getElementsByClassName #127

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,44 @@ PHP_METHOD(Dom_Element, getElementsByTagName)
}
/* }}} end dom_element_get_elements_by_tag_name */

PHP_METHOD(Dom_Element, getElementsByClassName)
{
dom_object *intern, *namednode;
zend_string *class_names;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &class_names) == FAILURE) {
RETURN_THROWS();
}

if (ZSTR_LEN(class_names) > INT_MAX) {
zend_argument_value_error(1, "is too long");
RETURN_THROWS();
}

DOM_GET_THIS_INTERN(intern);

object_init_ex(return_value, dom_html_collection_class_entry);
namednode = Z_DOMOBJ_P(return_value);

HashTable *token_set;
ALLOC_HASHTABLE(token_set);
zend_hash_init(token_set, 0, NULL, NULL, false);
dom_ordered_set_parser(token_set, ZSTR_VAL(class_names), intern->document->quirks_mode == PHP_LIBXML_QUIRKS);

if (zend_hash_num_elements(token_set) == 0) {
php_dom_create_obj_map(intern, namednode, NULL, NULL, NULL, &php_dom_obj_map_noop);

zend_hash_destroy(token_set);
FREE_HASHTABLE(token_set);
} else {
php_dom_create_obj_map(intern, namednode, NULL, NULL, NULL, &php_dom_obj_map_by_class_name);

dom_nnodemap_object *map = namednode->ptr;
map->array = token_set;
map->release_array = true;
}
}

/* should_free_result must be initialized to false */
static const xmlChar *dom_get_attribute_ns(dom_object *intern, xmlNodePtr elemp, const char *uri, size_t uri_len, const char *name, bool *should_free_result)
{
Expand Down
81 changes: 80 additions & 1 deletion ext/dom/obj_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 === *
**************************/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -292,6 +335,32 @@ 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) {
xmlNodePtr basep = nodep;
dom_node_idx_pair start_point = dom_obj_map_get_start_point(map, nodep, index);
if (start_point.node) {
itemnode = php_dom_next_in_tree_order(start_point.node, basep);
} 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, basep);
}
} 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);
Expand Down Expand Up @@ -478,6 +547,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 = NULL, // TODO ???
.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,
Expand Down Expand Up @@ -533,7 +612,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 = NULL, // TODO
.use_cache = false,
.nameless = true,
};
Expand Down
1 change: 1 addition & 0 deletions ext/dom/obj_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ zend_long php_dom_get_nodelist_length(dom_object *obj);

extern const php_dom_obj_map_handler php_dom_obj_map_attributes;
extern const php_dom_obj_map_handler php_dom_obj_map_by_tag_name;
extern const php_dom_obj_map_handler php_dom_obj_map_by_class_name;
extern const php_dom_obj_map_handler php_dom_obj_map_child_elements;
extern const php_dom_obj_map_handler php_dom_obj_map_child_nodes;
extern const php_dom_obj_map_handler php_dom_obj_map_nodeset;
Expand Down
3 changes: 3 additions & 0 deletions ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ public function removeAttributeNode(Attr $attr) : Attr {}

public function getElementsByTagName(string $qualifiedName): HTMLCollection {}
public function getElementsByTagNameNS(?string $namespace, string $localName): HTMLCollection {}
public function getElementsByClassName(string $classNames): HTMLCollection {}

public function insertAdjacentElement(AdjacentPosition $where, Element $element): ?Element {}
public function insertAdjacentText(AdjacentPosition $where, string $data): void {}
Expand Down Expand Up @@ -1986,6 +1987,8 @@ abstract class Document extends Node implements ParentNode
public function getElementsByTagName(string $qualifiedName): HTMLCollection {}
/** @implementation-alias Dom\Element::getElementsByTagNameNS */
public function getElementsByTagNameNS(?string $namespace, string $localName): HTMLCollection {}
/** @implementation-alias Dom\Element::getElementsByClassName */
public function getElementsByClassName(string $classNames): HTMLCollection {}

public function createElement(string $localName): Element {}
public function createElementNS(?string $namespace, string $qualifiedName): Element {}
Expand Down
11 changes: 10 additions & 1 deletion ext/dom/php_dom_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions ext/dom/tests/modern/common/Element_getElementsByClassName.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Dom\Element::getElementsByClassName()
--EXTENSIONS--
dom
--FILE--
<?php

$dom = Dom\HTMLDocument::createFromString(<<<HTML
<div class=" foo bar ">
<p class="bar">
<p class="bar"></p>
</p>
<b class="bars"></b>
</div>
HTML, LIBXML_NOERROR);
$collection = $dom->documentElement->getElementsByClassName("bar");

echo "There are {$dom->getElementsByClassName("foo \n bar")->count()} items in the document in total\n";

echo "There are {$dom->getElementsByClassName("")->count()} items that match set \"\" in the document in total\n";

echo "There are {$dom->getElementsByClassName(" ")->count()} items that match set \" \" in the document in total\n";

echo "There are {$collection->count()} items\n";

foreach ($collection as $key => $node) {
var_dump($key, $node->tagName);
var_dump($node === $collection->item($key));
}

?>
--EXPECT--
There are 3 items
There are 3 items
int(0)
string(3) "DIV"
bool(false)
int(1)
string(1) "P"
bool(false)
54 changes: 52 additions & 2 deletions ext/dom/token_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static zend_always_inline void dom_add_token(HashTable *ht, zend_string *token)

/* https://dom.spec.whatwg.org/#concept-ordered-set-parser
* and https://infra.spec.whatwg.org/#split-on-ascii-whitespace */
static void dom_ordered_set_parser(HashTable *token_set, const char *position)
void dom_ordered_set_parser(HashTable *token_set, const char *position, bool to_lowercase)
{
/* Adapted steps from "split on ASCII whitespace" such that that loop directly appends to the token set. */

Expand All @@ -72,6 +72,9 @@ static void dom_ordered_set_parser(HashTable *token_set, const char *position)

/* 4.2. Append token to tokens. */
zend_string *token = zend_string_init(start, length, false);
if (to_lowercase) {
zend_str_tolower(ZSTR_VAL(token), length);
}
dom_add_token(token_set, token);
zend_string_release_ex(token, false);

Expand All @@ -83,6 +86,53 @@ static void dom_ordered_set_parser(HashTable *token_set, const char *position)
* => That's the token set. */
}

/* This returns true if all tokens in "token_set" are found in "value". */
bool dom_ordered_set_all_contained(HashTable *token_set, const char *value, bool to_lowercase)
{
/* This code is conceptually close to dom_ordered_set_parser(),
* but without building a hash table.
* Since the storage of the token set maps a value on itself,
* we can reuse that storage as a "seen" flag by setting it to NULL. */
zval *zv;

uint32_t still_needed = zend_hash_num_elements(token_set);

value += strspn(value, ascii_whitespace);

while (*value != '\0' && still_needed > 0) {
const char *start = value;
value += strcspn(value, ascii_whitespace);
size_t length = value - start;

if (to_lowercase) {
ALLOCA_FLAG(use_heap)
char *lc_str = zend_str_tolower_copy(do_alloca(length + 1, use_heap), start, length);
zv = zend_hash_str_find(token_set, lc_str, length);
free_alloca(lc_str, use_heap);
} else {
zv = zend_hash_str_find(token_set, start, length);
}
if (zv) {
if (Z_STR_P(zv)) {
still_needed--;
Z_STR_P(zv) = NULL;
}
}

value += strspn(value, ascii_whitespace);
}

/* Restore "seen" flag. */
zend_string *k;
ZEND_HASH_FOREACH_STR_KEY_VAL(token_set, k, zv) {
if (!Z_STR_P(zv)) {
Z_STR_P(zv) = k;
}
} ZEND_HASH_FOREACH_END();

return still_needed == 0;
}

/* https://dom.spec.whatwg.org/#concept-ordered-set-serializer */
static char *dom_ordered_set_serializer(HashTable *token_set)
{
Expand Down Expand Up @@ -166,7 +216,7 @@ static void dom_token_list_update_set(dom_token_list_object *intern, HashTable *
xmlChar *value = dom_token_list_get_class_value(attr, &should_free);
if (value != NULL) {
/* 2. Otherwise, parse the token set. */
dom_ordered_set_parser(token_set, (const char *) value);
dom_ordered_set_parser(token_set, (const char *) value, false);
intern->cached_string = estrdup((const char *) value);
} else {
intern->cached_string = NULL;
Expand Down
2 changes: 2 additions & 0 deletions ext/dom/token_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static inline dom_token_list_object *php_dom_token_list_from_dom_obj(dom_object
return (dom_token_list_object *)((char *) obj - XtOffsetOf(dom_token_list_object, dom));
}

void dom_ordered_set_parser(HashTable *token_set, const char *position, bool to_lowercase);
bool dom_ordered_set_all_contained(HashTable *token_set, const char *value, bool to_lowercase);
void dom_token_list_ctor(dom_token_list_object *intern, dom_object *element_obj);
void dom_token_list_free_obj(zend_object *object);
zval *dom_token_list_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
Expand Down
Loading