-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add Dom\Element::insertAdjacentHTML() #16614
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
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.
114 changes: 114 additions & 0 deletions
114
ext/dom/tests/modern/html/interactions/Dom_Element_insertAdjacentHTML.phpt
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,114 @@ | ||
--TEST-- | ||
Dom\Element::insertAdjacentHTML() with HTML nodes | ||
--EXTENSIONS-- | ||
dom | ||
--FILE-- | ||
<?php | ||
|
||
const POSITIONS = [ | ||
Dom\AdjacentPosition::BeforeBegin, | ||
Dom\AdjacentPosition::AfterBegin, | ||
Dom\AdjacentPosition::BeforeEnd, | ||
Dom\AdjacentPosition::AfterEnd, | ||
]; | ||
|
||
function test(string $html) { | ||
echo "=== HTML ($html) ===\n"; | ||
|
||
foreach (POSITIONS as $position) { | ||
echo "--- Position ", $position->name, " ---\n"; | ||
|
||
$dom = Dom\HTMLDocument::createFromString("<div></div>", LIBXML_NOERROR); | ||
$div = $dom->body->firstChild; | ||
$div->append("Sample text"); | ||
|
||
$div->insertAdjacentHTML($position, $html); | ||
|
||
echo $dom->saveXML(), "\n"; | ||
echo $dom->saveHTML(), "\n"; | ||
var_dump($div->childNodes->length); | ||
var_dump($dom->body->childNodes->length); | ||
} | ||
} | ||
|
||
test("<p>foo</p><p>bar</p>"); | ||
test("text"); | ||
test(""); | ||
|
||
?> | ||
--EXPECT-- | ||
=== HTML (<p>foo</p><p>bar</p>) === | ||
--- Position BeforeBegin --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><p>foo</p><p>bar</p><div>Sample text</div></body></html> | ||
<html><head></head><body><p>foo</p><p>bar</p><div>Sample text</div></body></html> | ||
int(1) | ||
int(3) | ||
--- Position AfterBegin --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div><p>foo</p><p>bar</p>Sample text</div></body></html> | ||
<html><head></head><body><div><p>foo</p><p>bar</p>Sample text</div></body></html> | ||
int(3) | ||
int(1) | ||
--- Position BeforeEnd --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text<p>foo</p><p>bar</p></div></body></html> | ||
<html><head></head><body><div>Sample text<p>foo</p><p>bar</p></div></body></html> | ||
int(3) | ||
int(1) | ||
--- Position AfterEnd --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div><p>foo</p><p>bar</p></body></html> | ||
<html><head></head><body><div>Sample text</div><p>foo</p><p>bar</p></body></html> | ||
int(1) | ||
int(3) | ||
=== HTML (text) === | ||
--- Position BeforeBegin --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>text<div>Sample text</div></body></html> | ||
<html><head></head><body>text<div>Sample text</div></body></html> | ||
int(1) | ||
int(2) | ||
--- Position AfterBegin --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>textSample text</div></body></html> | ||
<html><head></head><body><div>textSample text</div></body></html> | ||
int(2) | ||
int(1) | ||
--- Position BeforeEnd --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample texttext</div></body></html> | ||
<html><head></head><body><div>Sample texttext</div></body></html> | ||
int(2) | ||
int(1) | ||
--- Position AfterEnd --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div>text</body></html> | ||
<html><head></head><body><div>Sample text</div>text</body></html> | ||
int(1) | ||
int(2) | ||
=== HTML () === | ||
--- Position BeforeBegin --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> | ||
<html><head></head><body><div>Sample text</div></body></html> | ||
int(1) | ||
int(1) | ||
--- Position AfterBegin --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> | ||
<html><head></head><body><div>Sample text</div></body></html> | ||
int(1) | ||
int(1) | ||
--- Position BeforeEnd --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> | ||
<html><head></head><body><div>Sample text</div></body></html> | ||
int(1) | ||
int(1) | ||
--- Position AfterEnd --- | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>Sample text</div></body></html> | ||
<html><head></head><body><div>Sample text</div></body></html> | ||
int(1) | ||
int(1) |
24 changes: 24 additions & 0 deletions
24
ext/dom/tests/modern/html/interactions/Dom_Element_insertAdjacentHTML_edge_case.phpt
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,24 @@ | ||
--TEST-- | ||
Dom\Element::insertAdjacentHTML() with HTML nodes - edge case | ||
--EXTENSIONS-- | ||
dom | ||
--FILE-- | ||
<?php | ||
|
||
$dom = Dom\HTMLDocument::createFromString("", LIBXML_NOERROR); | ||
|
||
$fragment = $dom->createDocumentFragment(); | ||
$node = $fragment->appendChild($dom->createElement("node")); | ||
|
||
$node->insertAdjacentHTML(Dom\AdjacentPosition::BeforeBegin, "<p>foo</p>"); | ||
|
||
echo $dom->saveHtml($fragment), "\n"; | ||
|
||
$dom->firstChild->insertAdjacentHTML(Dom\AdjacentPosition::AfterBegin, $node->outerHTML); | ||
|
||
echo $dom->saveHtml(), "\n"; | ||
|
||
?> | ||
--EXPECT-- | ||
<p>foo</p><node></node> | ||
<html><node></node><head></head><body></body></html> |
54 changes: 54 additions & 0 deletions
54
ext/dom/tests/modern/html/interactions/Dom_Element_insertAdjacentHTML_errors.phpt
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,54 @@ | ||
--TEST-- | ||
Dom\Element::insertAdjacentHTML() with HTML nodes - error conditions | ||
--EXTENSIONS-- | ||
dom | ||
--FILE-- | ||
<?php | ||
|
||
$dom = Dom\HTMLDocument::createEmpty(); | ||
$element = $dom->createElement('root'); | ||
|
||
echo "--- BeforeBegin no parent ---\n"; | ||
|
||
try { | ||
$element->insertAdjacentHTML(Dom\AdjacentPosition::BeforeBegin, "test"); | ||
} catch (DOMException $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
echo "--- AfterEnd no parent ---\n"; | ||
|
||
try { | ||
$element->insertAdjacentHTML(Dom\AdjacentPosition::AfterEnd, "test"); | ||
} catch (DOMException $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
$dom->appendChild($element); | ||
|
||
echo "--- BeforeBegin document parent ---\n"; | ||
|
||
try { | ||
$element->insertAdjacentHTML(Dom\AdjacentPosition::BeforeBegin, "test"); | ||
} catch (DOMException $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
echo "--- AfterEnd document parent ---\n"; | ||
|
||
try { | ||
$element->insertAdjacentHTML(Dom\AdjacentPosition::AfterEnd, "test"); | ||
} catch (DOMException $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
--- BeforeBegin no parent --- | ||
No Modification Allowed Error | ||
--- AfterEnd no parent --- | ||
No Modification Allowed Error | ||
--- BeforeBegin document parent --- | ||
No Modification Allowed Error | ||
--- AfterEnd document parent --- | ||
No Modification Allowed Error |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
wtf
(edit: yes i see it, avoid strcmp for performance~)
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.
Similar trick is done in ext/random btw if you're curious
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.
We really need a mechanism to solve this in a generic fashion. The RoundingMode enum uses the same trick.
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.
Agreed. It would be useful to be able to compare enum elements, or at least their name, by address.
Maybe we can store the address of enum elements in some struct once they are instantiated. Storing them in the
zend_class_entry
would be ideal, but I'm not sure this is doable. Case classes may help with that. Alternatively we could store them in a separate struct generated in_arginfo.h
.