Browse Source

More bugfixes

wrapper-classes
Dustin Wilson 3 years ago
parent
commit
c03aebc52a
  1. 4
      composer.lock
  2. 4
      lib/Node.php
  3. 31
      lib/Serializer.php

4
composer.lock

@ -63,7 +63,7 @@
"source": {
"type": "git",
"url": "mensbeam-gitea:MensBeam/HTML-Parser.git",
"reference": "b145e1ebc9c94e3e0a8267345b21067c5b0dc110"
"reference": "8361ea0d88c4406213e39e2cb983bfcb1e9b9b37"
},
"require": {
"ext-dom": "*",
@ -129,7 +129,7 @@
"parsing",
"whatwg"
],
"time": "2021-11-16T22:47:08+00:00"
"time": "2021-11-17T05:59:36+00:00"
},
{
"name": "mensbeam/intl",

4
lib/Node.php

@ -1274,8 +1274,8 @@ abstract class Node {
if ($parentRoot instanceof \DOMDocumentFragment) {
$wrappedParentRoot = $parentRoot->ownerDocument->getWrapperNode($parentRoot);
$parentRootHost = $this->getInnerNode(Reflection::getProtectedProperty($wrappedParentRoot, 'host')->get());
if ($parentRootHost !== null && ($parentRootHost === $node || $this->containsInner($node, $parentRootHost))) {
$parentRootHost = Reflection::getProtectedProperty($wrappedParentRoot, 'host');
if ($parentRootHost !== null && ($parentRootHost === $node || $this->containsInner($node, $this->getInnerNode($parentRootHost->get())))) {
throw new DOMException(DOMException::HIERARCHY_REQUEST_ERROR);
}
}

31
lib/Serializer.php

@ -46,20 +46,13 @@ class Serializer extends ParserSerializer {
}
protected static function treatAsBlockWithTemplates(\DOMNode $node): bool {
$xpath = new \DOMXPath($node->ownerDocument);
$templates = $xpath->query('.//template[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"][not(ancestor::iframe[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::listing[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::noembed[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::noframes[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::noscript[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::plaintext[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::pre[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::style[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::script[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::textarea[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::title[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"] or ancestor::xmp[namespace-uri()="" or namespace-uri()="http://www.w3.org/1999/xhtml"])]', $node);
foreach ($templates as $t) {
$content = Reflection::getProtectedProperty($t->ownerDocument->getWrapperNode($t)->content, 'innerNode');
// This circumvents a PHP XPath bug where it will silently fail to query
// nodes within fragments.
$clone = $content->cloneNode(true);
$span = $content->ownerDocument->createElement('span');
$span->appendChild($clone);
if ($xpath->evaluate(self::BLOCK_QUERY, $span) > 0) {
return true;
}
if ($xpath->evaluate(self::BLOCK_QUERY, $content) > 0) {
return true;
}
}
@ -70,24 +63,28 @@ class Serializer extends ParserSerializer {
// NOTE: This method is used only when pretty printing. Implementors of userland
// PHP DOM solutions with template contents will need to extend this method to
// be able to moonwalk through document fragment hosts.
$n = $node;
while ($n = $n->parentNode) {
if ($n instanceof \DOMDocument || ($n instanceof \DOMElement && $n->parentNode === null)) {
return true;
} elseif ($n instanceof \DOMDocumentFragment) {
do {
if ($n instanceof \DOMDocumentFragment) {
$host = Reflection::getProtectedProperty($node->ownerDocument->getWrapperNode($n), 'host');
if ($host !== null) {
$n = Reflection::getProtectedProperty($host->get(), 'innerNode');
} else {
return true;
}
} elseif (($n->parentNode->namespaceURI ?? Parser::HTML_NAMESPACE) === Parser::HTML_NAMESPACE) {
} else {
if ($n->parentNode !== null && ($n->parentNode->namespaceURI ?? Parser::HTML_NAMESPACE) !== Parser::HTML_NAMESPACE) {
continue;
}
if (self::treatAsBlock($n->parentNode)) {
return true;
}
break;
}
}
break;
} while ($n = $n->parentNode);
return false;
}

Loading…
Cancel
Save