Browse Source

Fix fragment parser and regression in innerHTML

split-manual
J. King 3 years ago
parent
commit
66093df6b7
  1. 2
      lib/DOM/Element.php
  2. 7
      lib/Parser.php

2
lib/DOM/Element.php

@ -84,7 +84,7 @@ class Element extends \DOMElement {
# 3. If the context object is a template element, then let context object be the
# template's template contents (a DocumentFragment).
if ($this->nodeName === 'template') {
$this->content = $frag;
$this->content = $fragment;
}
# 4. Replace all with fragment within the context object.
else {

7
lib/Parser.php

@ -51,13 +51,14 @@ 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, $document, $encodingOrContentType, $fragmentContext, $file);
self::parse($data, $tempDocument, $encodingOrContentType, $fragmentContext, $file);
// extract the nodes from the temp document into a fragment
$fragment = $document->createDocumentFragment();
foreach ($document->documentElement->childNodes as $node) {
$document->importNode($node, true);
foreach ($tempDocument->documentElement->childNodes as $node) {
$node = $document->importNode($node, true);
$fragment->appendChild($node);
}
return $fragment;

Loading…
Cancel
Save