Browse Source

Optimize Element::appendChild

split-manual
Dustin Wilson 3 years ago
parent
commit
a8a585269e
  1. 30
      lib/DOM/Element.php

30
lib/DOM/Element.php

@ -12,22 +12,28 @@ class Element extends \DOMElement {
protected $_classList; protected $_classList;
public function appendChild($node) { public function appendChild($node) {
// If appending a class attribute node, and classList has been invoked set $fixID = false;
// the class using classList instead of appending the attribute node. Will if ($node instanceof \DOMAttr && $node->namespaceURI === null) {
// return the created node instead. TokenList appends an attribute node if ($node->name === 'id') {
// internally to set the class attribute, so to prevent an infinite call loop $fixID = true;
// from occurring, a check between the normalized value and classList's }
// serialized value is performed. The spec is vague on how this is supposed to // If appending a class attribute node, and classList has been invoked set
// be handled. // the class using classList instead of appending the attribute node. Will
if ($node instanceof \DOMAttr && $this->_classList !== null && $node->namespaceURI === null && $node->name === 'class' && preg_replace(Data::WHITESPACE_REGEX, ' ', $node->value) !== $this->_classList->value) { // return the created node instead. TokenList appends an attribute node
$this->_classList->value = $node->value; // internally to set the class attribute, so to prevent an infinite call loop
return $this->getAttributeNode('class'); // from occurring, a check between the normalized value and classList's
// serialized value is performed. The spec is vague on how this is supposed to
// be handled.
elseif ($this->_classList !== null && $node->name === 'class' && preg_replace(Data::WHITESPACE_REGEX, ' ', $node->value) !== $this->_classList->value) {
$this->_classList->value = $node->value;
return $this->getAttributeNode('class');
}
} }
$node = parent::appendChild($node); $node = parent::appendChild($node);
// Fix id attributes when appending attribute nodes. // Fix id attributes when appending id attribute nodes.
if ($node instanceof \DOMAttr && $node->namespaceURI === null && $node->name === 'id') { if ($fixID) {
$this->setIdAttribute('id', true); $this->setIdAttribute('id', true);
} }

Loading…
Cancel
Save