-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement DOMElement::insertAdjacent{Element,Text} #11700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
--TEST-- | ||
DOMElement::insertAdjacentElement() | ||
--EXTENSIONS-- | ||
dom | ||
--FILE-- | ||
<?php | ||
|
||
$dom = new DOMDocument(); | ||
$dom->loadXML('<?xml version="1.0"?><container><p>foo</p></container>'); | ||
$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('<?xml version="1.0"?><container xmlns:foo="some:ns"/>'); | ||
$dom->documentElement->insertAdjacentElement("afterbegin", $dom->createElementNS("some:ns", "bar")); | ||
echo $dom->saveXML(); | ||
|
||
echo "--- Two document test ---\n"; | ||
|
||
$dom1 = new DOMDocument(); | ||
$dom1->loadXML('<?xml version="1.0"?><container><div/></container>'); | ||
$dom2 = new DOMDocument(); | ||
$dom2->loadXML('<?xml version="1.0"?><container><p/></container>'); | ||
$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" | ||
<?xml version="1.0"?> | ||
<container><A/><p>foo</p></container> | ||
string(1) "B" | ||
<?xml version="1.0"?> | ||
<container><A/><p><B/>foo</p></container> | ||
string(1) "C" | ||
<?xml version="1.0"?> | ||
<container><A/><p><B/>foo<C/></p></container> | ||
string(1) "D" | ||
<?xml version="1.0"?> | ||
<container><A/><p><B/>foo<C/></p><D/></container> | ||
--- Normal cases lowercase --- | ||
string(1) "A" | ||
<?xml version="1.0"?> | ||
<container><A/><p>foo</p></container> | ||
string(1) "B" | ||
<?xml version="1.0"?> | ||
<container><A/><p><B/>foo</p></container> | ||
string(1) "C" | ||
<?xml version="1.0"?> | ||
<container><A/><p><B/>foo<C/></p></container> | ||
string(1) "D" | ||
<?xml version="1.0"?> | ||
<container><A/><p><B/>foo<C/></p><D/></container> | ||
string(1) "A" | ||
<empty><A/></empty> | ||
--- Namespace test --- | ||
<?xml version="1.0"?> | ||
<container xmlns:foo="some:ns"><foo:bar/></container> | ||
--- Two document test --- | ||
<?xml version="1.0"?> | ||
<container><div><p/></div></container> | ||
<?xml version="1.0"?> | ||
<container/> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec seem to indicate that a DOMException must be thrown:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but strangely PHP offers an "stricterror" option (default true) which allows you to get warnings instead of exceptions.
And that maybe doesn't even violate spec, because from https://webidl.spec.whatwg.org/#dfn-throw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Document3-strictErrorChecking for more info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah PHP extension used to have the trifecto of no warning/exception, warnings, exceptions. I think mainly because prior to the introduction of exception warnings were the only way.
We probably should get rid of those and only allow silent or exception behaviour (similarly to the SQLite3 RFC) but that's future scope