From 3915b41e4786dd333b31a5f5ea13d7e7e04c8151 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 17 Mar 2021 11:52:47 -0400 Subject: [PATCH] Set id attributes as IDs xml:id attributes also work on foreign content --- lib/DOM/Document.php | 30 ------------------------------ lib/DOM/Element.php | 6 ++++++ 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/lib/DOM/Document.php b/lib/DOM/Document.php index 945bf43..f7bee81 100644 --- a/lib/DOM/Document.php +++ b/lib/DOM/Document.php @@ -31,36 +31,6 @@ class Document extends \DOMDocument { $this->registerNodeClass('DOMText', '\dW\HTML5\Text'); } - public function fixIdAttributes() { - // TODO: Accept DOMDocumentFragment, append it to a document, fix shit, and - // then poop out a fragment so selecting id attributes works on fragments. - - // Fix id attributes so they may be selected by the DOM. Fix the PHP id attribute - // bug. Allows DOMDocument->getElementById() to work on id attributes. - $this->relaxNGValidateSource(' - - - - - - - - - - - - - - - - - - - -'); - $this->normalize(); - } - public function load($source, $options = null, ?string $encodingOrContentType = null): bool { $data = Parser::fetchFile($source, $encodingOrContentType); if (!$data) { diff --git a/lib/DOM/Element.php b/lib/DOM/Element.php index e8fca7d..f690111 100644 --- a/lib/DOM/Element.php +++ b/lib/DOM/Element.php @@ -21,6 +21,9 @@ class Element extends \DOMElement { $name = $this->coerceName($name); parent::setAttribute($name, $value); } + if ($name === "id") { + $this->setIdAttribute($name, true); + } } public function setAttributeNS($namespaceURI, $qualifiedName, $value) { @@ -34,6 +37,9 @@ class Element extends \DOMElement { $qualifiedName = $this->coerceName($qualifiedName); parent::setAttributeNS($namespaceURI, $qualifiedName, $value); } + if ($qualifiedName === "id" && $namespaceURI === null) { + $this->setIdAttribute($qualifiedName, true); + } } public function __get(string $prop) {