Skip to content

Commit

Permalink
Auto-insert attribute namespace if it's not already known
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Feb 11, 2025
1 parent c47e705 commit df7e50e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ public static function fromXML(DOMAttr $attr): static
*/
public function toXML(DOMElement $parent): DOMElement
{
if ($this->getNamespaceURI() !== null && !$parent->lookupPrefix($this->getNamespacePrefix())) {
$parent->setAttributeNS(
'http://www.w3.org/2000/xmlns/',
'xmlns:' . $this->getNamespacePrefix(),
$this->getNamespaceURI(),
);
}

$parent->setAttributeNS(
$this->getNamespaceURI(),
!in_array($this->getNamespacePrefix(), ['', null])
Expand Down
2 changes: 1 addition & 1 deletion src/DOMDocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class DOMDocumentFactory
* @var non-negative-int
* TODO: Add LIBXML_NO_XXE to the defaults when PHP 8.4.0 + libxml 2.13.0 become generally available
*/
public const DEFAULT_OPTIONS = LIBXML_COMPACT | LIBXML_NONET;
public const DEFAULT_OPTIONS = LIBXML_COMPACT | LIBXML_NONET | LIBXML_NSCLEAN;


/**
Expand Down

0 comments on commit df7e50e

Please sign in to comment.