Skip to content

Commit df7e50e

Browse files
committed
Auto-insert attribute namespace if it's not already known
1 parent c47e705 commit df7e50e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/Attribute.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public static function fromXML(DOMAttr $attr): static
106106
*/
107107
public function toXML(DOMElement $parent): DOMElement
108108
{
109+
if ($this->getNamespaceURI() !== null && !$parent->lookupPrefix($this->getNamespacePrefix())) {
110+
$parent->setAttributeNS(
111+
'http://www.w3.org/2000/xmlns/',
112+
'xmlns:' . $this->getNamespacePrefix(),
113+
$this->getNamespaceURI(),
114+
);
115+
}
116+
109117
$parent->setAttributeNS(
110118
$this->getNamespaceURI(),
111119
!in_array($this->getNamespacePrefix(), ['', null])

src/DOMDocumentFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class DOMDocumentFactory
2424
* @var non-negative-int
2525
* TODO: Add LIBXML_NO_XXE to the defaults when PHP 8.4.0 + libxml 2.13.0 become generally available
2626
*/
27-
public const DEFAULT_OPTIONS = LIBXML_COMPACT | LIBXML_NONET;
27+
public const DEFAULT_OPTIONS = LIBXML_COMPACT | LIBXML_NONET | LIBXML_NSCLEAN;
2828

2929

3030
/**

0 commit comments

Comments
 (0)