1--TEST--
2HTMLCollection::namedItem() and dimension handling for named accesses
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$xml = <<<XML
9<!DOCTYPE root [
10    <!ENTITY ent "test">
11]>
12<root>
13    <node name="foo">1</node>
14    <x id="foo">2</x>
15    <x id="foo&ent;">2 with entity</x>
16    <node test:id="foo" xmlns:test="http://example.com">3</node>
17    <node id="wrong">4</node>
18    <node id="foo">5</node>
19    <node name="bar">without html ns</node>
20    <node name="bar" xmlns="http://www.w3.org/1999/xhtml">with html ns</node>
21</root>
22XML;
23
24$dom = Dom\XMLDocument::createFromString($xml);
25
26function test($obj, $name) {
27    echo "--- Query \"$name\" ---\n";
28    var_dump($obj->namedItem($name)?->textContent);
29    var_dump($obj[$name]?->textContent);
30    var_dump(isset($obj[$name]));
31
32    // Search to check for dimension access consistency
33    $node = $obj[$name];
34    if ($node) {
35        $found = false;
36        for ($i = 0; $i < $obj->length && !$found; $i++) {
37            $found = $obj[$i] === $node;
38        }
39        if (!$found) {
40            throw new Error('inconsistency in dimension access');
41        }
42    }
43}
44
45test($dom->getElementsByTagName('node'), 'foo');
46test($dom->getElementsByTagName('node'), '');
47test($dom->getElementsByTagName('node'), 'does not exist');
48test($dom->getElementsByTagName('node'), 'wrong');
49test($dom->getElementsByTagName('node'), 'bar');
50test($dom->getElementsByTagName('x'), 'foo');
51test($dom->getElementsByTagName('x'), 'footest');
52
53?>
54--EXPECT--
55--- Query "foo" ---
56string(1) "5"
57string(1) "5"
58bool(true)
59--- Query "" ---
60NULL
61NULL
62bool(false)
63--- Query "does not exist" ---
64NULL
65NULL
66bool(false)
67--- Query "wrong" ---
68string(1) "4"
69string(1) "4"
70bool(true)
71--- Query "bar" ---
72string(12) "with html ns"
73string(12) "with html ns"
74bool(true)
75--- Query "foo" ---
76string(1) "2"
77string(1) "2"
78bool(true)
79--- Query "footest" ---
80string(13) "2 with entity"
81string(13) "2 with entity"
82bool(true)
83