|
| 1 | +--TEST-- |
| 2 | +Dom\Element::insertAdjacentHTML() with HTML nodes |
| 3 | +--EXTENSIONS-- |
| 4 | +dom |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | + |
| 8 | +const POSITIONS = [ |
| 9 | + Dom\AdjacentPosition::BeforeBegin, |
| 10 | + Dom\AdjacentPosition::AfterBegin, |
| 11 | + Dom\AdjacentPosition::BeforeEnd, |
| 12 | + Dom\AdjacentPosition::AfterEnd, |
| 13 | +]; |
| 14 | + |
| 15 | +function test(string $html) { |
| 16 | + echo "=== HTML ($html) ===\n"; |
| 17 | + |
| 18 | + foreach (POSITIONS as $position) { |
| 19 | + echo "--- Position ", $position->name, " ---\n"; |
| 20 | + |
| 21 | + $dom = Dom\HTMLDocument::createFromString("<div></div>", LIBXML_NOERROR); |
| 22 | + $div = $dom->body->firstChild; |
| 23 | + $div->append("Sample text"); |
| 24 | + |
| 25 | + $div->insertAdjacentHTML($position, $html); |
| 26 | + |
| 27 | + echo $dom->saveXML(), "\n"; |
| 28 | + echo $dom->saveHTML(), "\n"; |
| 29 | + var_dump($div->childNodes->length); |
| 30 | + var_dump($dom->body->childNodes->length); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +test("<p>foo</p><p>bar</p>"); |
| 35 | +test("text"); |
| 36 | +test(""); |
| 37 | + |
| 38 | +?> |
| 39 | +--EXPECT-- |
| 40 | +=== HTML (<p>foo</p><p>bar</p>) === |
| 41 | +--- Position BeforeBegin --- |
| 42 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 43 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><p>foo</p><p>bar</p><div>Sample text</div></body></html> |
| 44 | +<html><head></head><body><p>foo</p><p>bar</p><div>Sample text</div></body></html> |
| 45 | +int(1) |
| 46 | +int(3) |
| 47 | +--- Position AfterBegin --- |
| 48 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 49 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div><p>foo</p><p>bar</p>Sample text</div></body></html> |
| 50 | +<html><head></head><body><div><p>foo</p><p>bar</p>Sample text</div></body></html> |
| 51 | +int(3) |
| 52 | +int(1) |
| 53 | +--- Position BeforeEnd --- |
| 54 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 55 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text<p>foo</p><p>bar</p></div></body></html> |
| 56 | +<html><head></head><body><div>Sample text<p>foo</p><p>bar</p></div></body></html> |
| 57 | +int(3) |
| 58 | +int(1) |
| 59 | +--- Position AfterEnd --- |
| 60 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 61 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div><p>foo</p><p>bar</p></body></html> |
| 62 | +<html><head></head><body><div>Sample text</div><p>foo</p><p>bar</p></body></html> |
| 63 | +int(1) |
| 64 | +int(3) |
| 65 | +=== HTML (text) === |
| 66 | +--- Position BeforeBegin --- |
| 67 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 68 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>text<div>Sample text</div></body></html> |
| 69 | +<html><head></head><body>text<div>Sample text</div></body></html> |
| 70 | +int(1) |
| 71 | +int(2) |
| 72 | +--- Position AfterBegin --- |
| 73 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 74 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>textSample text</div></body></html> |
| 75 | +<html><head></head><body><div>textSample text</div></body></html> |
| 76 | +int(2) |
| 77 | +int(1) |
| 78 | +--- Position BeforeEnd --- |
| 79 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 80 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample texttext</div></body></html> |
| 81 | +<html><head></head><body><div>Sample texttext</div></body></html> |
| 82 | +int(2) |
| 83 | +int(1) |
| 84 | +--- Position AfterEnd --- |
| 85 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 86 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div>text</body></html> |
| 87 | +<html><head></head><body><div>Sample text</div>text</body></html> |
| 88 | +int(1) |
| 89 | +int(2) |
| 90 | +=== HTML () === |
| 91 | +--- Position BeforeBegin --- |
| 92 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 93 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> |
| 94 | +<html><head></head><body><div>Sample text</div></body></html> |
| 95 | +int(1) |
| 96 | +int(1) |
| 97 | +--- Position AfterBegin --- |
| 98 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 99 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> |
| 100 | +<html><head></head><body><div>Sample text</div></body></html> |
| 101 | +int(1) |
| 102 | +int(1) |
| 103 | +--- Position BeforeEnd --- |
| 104 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 105 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> |
| 106 | +<html><head></head><body><div>Sample text</div></body></html> |
| 107 | +int(1) |
| 108 | +int(1) |
| 109 | +--- Position AfterEnd --- |
| 110 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 111 | +<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> |
| 112 | +<html><head></head><body><div>Sample text</div></body></html> |
| 113 | +int(1) |
| 114 | +int(1) |
0 commit comments