Skip to content

Commit 9ae7179

Browse files
committed
Address review comments
1 parent 33e3d46 commit 9ae7179

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

ext/dom/php_dom.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,9 @@ PHP_MINIT_FUNCTION(dom)
750750
memcpy(&dom_token_list_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
751751
dom_token_list_object_handlers.offset = XtOffsetOf(dom_token_list_object, dom.std);
752752
dom_token_list_object_handlers.free_obj = dom_token_list_free_obj;
753-
/* The IDL has the [SameObject] constraint, which is incompatible with cloning because it imposes that there is only
754-
* one instance per parent object. */
753+
/* The Web IDL (Web Interface Description Language - https://webidl.spec.whatwg.org) has the [SameObject] constraint
754+
* for this object, which is incompatible with cloning because it imposes that there is only one instance
755+
* per parent object. */
755756
dom_token_list_object_handlers.clone_obj = NULL;
756757
dom_token_list_object_handlers.read_dimension = dom_token_list_read_dimension;
757758
dom_token_list_object_handlers.has_dimension = dom_token_list_has_dimension;

ext/dom/tests/modern/token_list/dimensions_error.phpt

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,30 @@ dom
88
$dom = DOM\XMLDocument::createFromString('<root class="A B C"/>');
99
$list = $dom->documentElement->classList;
1010

11-
try {
12-
$list[new stdClass];
13-
} catch (TypeError $e) {
14-
echo $e->getMessage(), "\n";
15-
}
11+
$testOffsets = [
12+
new stdClass,
13+
[],
14+
fopen("php://output", "w"),
15+
];
1616

17-
try {
18-
isset($list[new stdClass]);
19-
} catch (TypeError $e) {
20-
echo $e->getMessage(), "\n";
21-
}
17+
foreach ($testOffsets as $offset) {
18+
try {
19+
$list[$offset];
20+
} catch (TypeError $e) {
21+
echo $e->getMessage(), "\n";
22+
}
2223

23-
try {
24-
empty($list[new stdClass]);
25-
} catch (TypeError $e) {
26-
echo $e->getMessage(), "\n";
24+
try {
25+
isset($list[$offset]);
26+
} catch (TypeError $e) {
27+
echo $e->getMessage(), "\n";
28+
}
29+
30+
try {
31+
empty($list[$offset]);
32+
} catch (TypeError $e) {
33+
echo $e->getMessage(), "\n";
34+
}
2735
}
2836

2937
try {
@@ -33,8 +41,17 @@ try {
3341
}
3442

3543
?>
36-
--EXPECT--
44+
--EXPECTF--
3745
Cannot access offset of type stdClass on Dom\TokenList
3846
Cannot access offset of type stdClass in isset or empty
3947
Cannot access offset of type stdClass in isset or empty
48+
Cannot access offset of type array on Dom\TokenList
49+
Cannot access offset of type array in isset or empty
50+
Cannot access offset of type array in isset or empty
51+
52+
Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
53+
54+
Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
55+
56+
Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
4057
Cannot append to Dom\TokenList

ext/dom/tests/modern/token_list/toggle.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ var_dump($list->toggle("C"));
4545

4646
echo $dom->saveXML(), "\n";
4747

48+
echo "--- Toggle E ---\n";
49+
4850
$list->value = 'E';
4951
$list->toggle('E');
5052

@@ -76,5 +78,6 @@ bool(true)
7678
bool(false)
7779
<?xml version="1.0" encoding="UTF-8"?>
7880
<root class="D"/>
81+
--- Toggle E ---
7982
<?xml version="1.0" encoding="UTF-8"?>
8083
<root class=""/>

ext/dom/token_list.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,20 @@ static void dom_token_list_item_read(dom_token_list_object *token_list, zval *re
257257
ZVAL_STR_COPY(retval, str_index);
258258
} else {
259259
/* Not an out of bounds ValueError, but NULL, as according to spec.
260-
* This was a design choice to allow for constructs like `item(x) ?? ...` */
260+
* This design choice allows for constructs like `item(x) ?? ...`
261+
*
262+
* In particular:
263+
* https://dom.spec.whatwg.org/#interface-domtokenlist states DOMTokenList implements iterable<DOMString>.
264+
* From https://webidl.spec.whatwg.org/#idl-iterable:
265+
* If a single type parameter is given,
266+
* then the interface has a value iterator and provides values of the specified type.
267+
* This applies, and reading the definition of value iterator means we should support indexed properties.
268+
* From https://webidl.spec.whatwg.org/#dfn-support-indexed-properties:
269+
* An interface that defines an indexed property getter is said to support indexed properties.
270+
* And indexed property getter is defined here: https://webidl.spec.whatwg.org/#dfn-indexed-property-getter
271+
* Down below in their note they give an example of how an out-of-bounds access evaluates to undefined,
272+
* which would map to NULL for us.
273+
* This would also be consistent with how out-of-bounds array accesses in PHP result in NULL. */
261274
ZVAL_NULL(retval);
262275
}
263276
}

0 commit comments

Comments
 (0)