Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set DOMAttr::$value without expanding entities
The manual refers to the DOM Level 3 Core spec which says: "On setting, this creates a Text node with the unparsed contents of the string. I.e. any characters that an XML processor would recognize as markup are instead treated as literal text." PHP is expanding entities when DOMAttr::value is set, which is non-compliant and is a difference in behaviour compared to browser DOM implementations. So, when value is set, remove all children of the attribute node. Then create a single text node and insert that as the only child of the attribute. Add tests.
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,30 @@ | ||
--TEST-- | ||
DOMAttr entity expansion | ||
--FILE-- | ||
<?php | ||
$doc = new DOMDocument; | ||
$elt = $doc->createElement('elt'); | ||
$doc->appendChild($elt); | ||
$elt->setAttribute('a','&'); | ||
print $doc->saveXML($elt) . "\n"; | ||
|
||
$attr = $elt->getAttributeNode('a'); | ||
$attr->value = '&'; | ||
print $doc->saveXML($elt) . "\n"; | ||
|
||
$attr->removeChild($attr->firstChild); | ||
print $doc->saveXML($elt) . "\n"; | ||
|
||
$elt->setAttributeNS('http://www.w3.org/2000/svg', 'svg:id','&'); | ||
print $doc->saveXML($elt) . "\n"; | ||
|
||
$attr = $elt->getAttributeNodeNS('http://www.w3.org/2000/svg', 'id'); | ||
$attr->value = '<&'; | ||
print $doc->saveXML($elt) . "\n"; | ||
|
||
--EXPECT-- | ||
<elt a="&"/> | ||
<elt a="&amp;"/> | ||
<elt a=""/> | ||
<elt xmlns:svg="http://www.w3.org/2000/svg" a="" svg:id="&amp;"/> | ||
<elt xmlns:svg="http://www.w3.org/2000/svg" a="" svg:id="&lt;&amp;"/> |