1--TEST-- 2Dom\Element::insertAdjacentHTML() with HTML nodes 3--EXTENSIONS-- 4dom 5--FILE-- 6<?php 7 8const POSITIONS = [ 9 Dom\AdjacentPosition::BeforeBegin, 10 Dom\AdjacentPosition::AfterBegin, 11 Dom\AdjacentPosition::BeforeEnd, 12 Dom\AdjacentPosition::AfterEnd, 13]; 14 15function 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 34test("<p>foo</p><p>bar</p>"); 35test("text"); 36test(""); 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> 45int(1) 46int(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> 51int(3) 52int(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> 57int(3) 58int(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> 63int(1) 64int(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> 70int(1) 71int(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> 76int(2) 77int(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> 82int(2) 83int(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> 88int(1) 89int(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> 95int(1) 96int(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> 101int(1) 102int(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> 107int(1) 108int(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> 113int(1) 114int(1) 115