xref: /php-src/ext/tidy/tests/sibling_nodes.phpt (revision ad452086)
1--TEST--
2getPreviousSibling() and getNextSibling()
3--EXTENSIONS--
4tidy
5--FILE--
6<?php
7
8$tidy = tidy_parse_string(<<<HTML
9<!DOCTYPE html>
10<html>
11    <body>
12        <div>first</div>
13        <!-- second -->
14        <div>third</div>
15    </body>
16</html>
17HTML);
18$body = $tidy->body();
19
20function format($str) {
21    if (is_null($str)) return $str;
22    return trim($str);
23}
24
25foreach ($body->child as $i => $child) {
26    echo "=== From the perspective of child $i ===\n";
27    echo "Previous: ";
28    var_dump(format($child->getPreviousSibling()?->value));
29    echo "Next: ";
30    var_dump(format($child->getNextSibling()?->value));
31}
32
33echo "=== html element has only the doctype as sibling ===\n";
34echo "Previous: ";
35var_dump(format($tidy->html()->getPreviousSibling()?->value));
36echo "Next: ";
37var_dump(format($tidy->html()->getNextSibling()?->value));
38
39?>
40--EXPECT--
41=== From the perspective of child 0 ===
42Previous: NULL
43Next: string(15) "<!-- second -->"
44=== From the perspective of child 1 ===
45Previous: string(16) "<div>first</div>"
46Next: string(16) "<div>third</div>"
47=== From the perspective of child 2 ===
48Previous: string(15) "<!-- second -->"
49Next: NULL
50=== html element has only the doctype as sibling ===
51Previous: string(15) "<!DOCTYPE html>"
52Next: NULL
53