Browse Source

inner and outerHTML now use parseFragment, fixes to parseFragment

split-manual
Dustin Wilson 3 years ago
parent
commit
b174c6b49b
  1. 6
      lib/DOM/Element.php
  2. 7
      lib/Parser.php

6
lib/DOM/Element.php

@ -79,7 +79,7 @@ class Element extends \DOMElement {
# 2. Let fragment be the result of invoking the fragment parsing algorithm with
# the new value as markup, and with context element.
$frag = Parser::parse($value, $this->ownerDocument, $this->ownerDocument->documentEncoding, $this);
$fragment = Parser::parseFragment($value, $this->ownerDocument, 'UTF-8', $this);
# 3. If the context object is a template element, then let context object be the
# template's template contents (a DocumentFragment).
@ -123,7 +123,7 @@ class Element extends \DOMElement {
// DEVIATION: Normally the tree mutation record would do the actual replacement,
// but there is no scripting in this implementation. Going to simply append the
// fragment instead.
$this->appendChild($frag);
$this->appendChild($fragment);
}
break;
@ -157,7 +157,7 @@ class Element extends \DOMElement {
# 5. Let fragment be the result of invoking the fragment parsing algorithm with
# the new value as markup, and parent as the context element.
$fragment = Parser::parse($value, $this->ownerDocument, $this->ownerDocument->documentEncoding, $parent);
$fragment = Parser::parseFragment($value, $this->ownerDocument, 'UTF-8', $parent);
# 6. Replace the context object with fragment within the context object's
# parent.

7
lib/Parser.php

@ -29,7 +29,7 @@ class Parser {
$errorHandler = new ParseError;
} else {
$errorHandler = new ParseErrorDummy;
}
}
$decoder = new Data($data, $file ?? "STDIN", $errorHandler, $encodingOrContentType);
$document->documentEncoding = $decoder->encoding;
$stack = new OpenElementsStack($fragmentContext);
@ -51,13 +51,12 @@ class Parser {
public static function parseFragment(string $data, ?Document $document = null, ?string $encodingOrContentType = null, ?\DOMElement $fragmentContext = null, ?String $file = null): DocumentFragment {
// Create the requisite parsing context if none was supplied
$document = $document ?? new Document;
$tempDocument = new Document;
$fragmentContext = $fragmentContext ?? $document->createElement("div");
// parse the fragment into the temporary document
self::parse($data, $tempDocument, $encodingOrContentType, $fragmentContext, $file);
self::parse($data, $document, $encodingOrContentType, $fragmentContext, $file);
// extract the nodes from the temp document into a fragment
$fragment = $document->createDocumentFragment();
foreach ($tempDocument->documentElement->childNodes as $node) {
foreach ($document->documentElement->childNodes as $node) {
$document->importNode($node, true);
$fragment->appendChild($node);
}

Loading…
Cancel
Save