--TEST--
DOMElement::insertAdjacentElement()
--EXTENSIONS--
dom
--FILE--
loadXML('foo
');
$container = $dom->documentElement;
$p = $container->firstElementChild;
echo "--- Edge cases ---\n";
var_dump($dom->createElement('free')->insertAdjacentElement("beforebegin", $dom->createElement('element')));
var_dump($dom->createElement('free')->insertAdjacentElement("afterend", $dom->createElement('element')));
try {
var_dump($dom->createElement('free')->insertAdjacentElement("bogus", $dom->createElement('element')));
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
echo "--- Hierarchy test ---\n";
$element = $dom->createElement('free');
$child = $element->appendChild($dom->createElement('child'));
foreach (['beforebegin', 'afterbegin', 'beforeend', 'afterend'] as $where) {
try {
var_dump($child->insertAdjacentElement($where, $element)->tagName);
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
}
function testNormalCases($dom, $uppercase) {
$container = $dom->documentElement;
$p = $container->firstElementChild;
$transform = fn ($s) => $uppercase ? strtoupper($s) : $s;
var_dump($p->insertAdjacentElement($transform("beforebegin"), $dom->createElement('A'))->tagName);
echo $dom->saveXML();
var_dump($p->insertAdjacentElement($transform("afterbegin"), $dom->createElement('B'))->tagName);
echo $dom->saveXML();
var_dump($p->insertAdjacentElement($transform("beforeend"), $dom->createElement('C'))->tagName);
echo $dom->saveXML();
var_dump($p->insertAdjacentElement($transform("afterend"), $dom->createElement('D'))->tagName);
echo $dom->saveXML();
}
echo "--- Normal cases uppercase ---\n";
testNormalCases(clone $dom, true);
echo "--- Normal cases lowercase ---\n";
testNormalCases($dom, false);
$empty = $dom->createElement('empty');
var_dump($empty->insertAdjacentElement("afterbegin", $dom->createElement('A'))->tagName);
echo $dom->saveXML($empty), "\n";
echo "--- Namespace test ---\n";
$dom->loadXML('');
$dom->documentElement->insertAdjacentElement("afterbegin", $dom->createElementNS("some:ns", "bar"));
echo $dom->saveXML();
echo "--- Two document test ---\n";
$dom1 = new DOMDocument();
$dom1->loadXML('');
$dom2 = new DOMDocument();
$dom2->loadXML('');
$dom1->documentElement->firstChild->insertAdjacentElement('afterbegin', $dom2->documentElement->firstChild);
echo $dom1->saveXML();
echo $dom2->saveXML();
?>
--EXPECT--
--- Edge cases ---
NULL
NULL
Syntax Error
--- Hierarchy test ---
Hierarchy Request Error
Hierarchy Request Error
Hierarchy Request Error
Hierarchy Request Error
--- Normal cases uppercase ---
string(1) "A"
foo
string(1) "B"
foo
string(1) "C"
foo
string(1) "D"
foo
--- Normal cases lowercase ---
string(1) "A"
foo
string(1) "B"
foo
string(1) "C"
foo
string(1) "D"
foo
string(1) "A"
--- Namespace test ---
--- Two document test ---