xref: /PHP-8.2/ext/dom/tests/bug67949.phpt (revision 9f7d8880)
1--TEST--
2Bug #67949: DOMNodeList elements should be accessible through array notation
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$html = <<<HTML
9<div>data</div>
10<a href="test">hello world</a>
11HTML;
12$doc = new DOMDocument;
13$doc->loadHTML($html);
14
15$nodes = $doc->getElementsByTagName('div');
16
17echo "testing has_dimension\n";
18var_dump(isset($nodes[0]));
19var_dump(isset($nodes[1]));
20var_dump(isset($nodes[-1]));
21
22echo "testing property access\n";
23var_dump($nodes[0]->textContent);
24var_dump($nodes[1]->textContent);
25
26echo "testing offset not a long\n";
27$offset = ['test'];
28var_dump($offset);
29var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
30var_dump($offset);
31
32$something = 'test';
33$offset = &$something;
34
35var_dump($offset);
36var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
37var_dump($offset);
38
39$offset = 'test';
40var_dump($offset);
41var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
42var_dump($offset);
43
44echo "testing read_dimension with null offset\n";
45try {
46    var_dump($nodes[][] = 1);
47} catch (Error $e) {
48    echo $e->getMessage(), "\n";
49}
50
51echo "testing attribute access\n";
52$anchor = $doc->getElementsByTagName('a')[0];
53var_dump($anchor->attributes[0]->name);
54
55echo "==DONE==\n";
56?>
57--EXPECTF--
58testing has_dimension
59bool(true)
60bool(false)
61bool(false)
62testing property access
63string(4) "data"
64
65Warning: Attempt to read property "textContent" on null in %s on line %d
66NULL
67testing offset not a long
68array(1) {
69  [0]=>
70  string(4) "test"
71}
72
73Warning: Attempt to read property "textContent" on null in %s on line %d
74bool(false)
75NULL
76array(1) {
77  [0]=>
78  string(4) "test"
79}
80string(4) "test"
81bool(true)
82string(4) "data"
83string(4) "test"
84string(4) "test"
85bool(true)
86string(4) "data"
87string(4) "test"
88testing read_dimension with null offset
89Cannot access DOMNodeList without offset
90testing attribute access
91string(4) "href"
92==DONE==
93