From ee5e9298e7993ae516fb6f480599912329d3d7fd Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 31 Mar 2021 14:14:41 -0400 Subject: [PATCH] Tests and fixes for DOCTYPE serialization --- lib/DOM/traits/Serialize.php | 5 ++++- tests/cases/TestSerializer.php | 20 ++++++++++++-------- tests/cases/serializer/menbeam02.dat | 28 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 tests/cases/serializer/menbeam02.dat diff --git a/lib/DOM/traits/Serialize.php b/lib/DOM/traits/Serialize.php index 3ee4685..e0ed9ff 100644 --- a/lib/DOM/traits/Serialize.php +++ b/lib/DOM/traits/Serialize.php @@ -57,7 +57,10 @@ trait Serialize { # LETTER E), followed by a space (U+0020 SPACE), followed by the value of # current node's name IDL attribute, followed by the literal string ">" (U+003E # GREATER-THAN SIGN). - $s .= "childNodes->item(0)->name}>"; + // DEVIATION: The name is trimmed because PHP's DOM does not + // accept the empty string as a DOCTYPE name + $name = trim($node->childNodes->item(0)->name, " "); + $s .= ""; $start = 1; } diff --git a/tests/cases/TestSerializer.php b/tests/cases/TestSerializer.php index b807f01..7072ab6 100644 --- a/tests/cases/TestSerializer.php +++ b/tests/cases/TestSerializer.php @@ -61,10 +61,10 @@ class TestSerializer extends \PHPUnit\Framework\TestCase { $exp = []; assert($lines[$l] === "#output", new \Exception("Test $file #$index follows input with something other than output at line ".($l + 1))); for (++$l; $l < sizeof($lines); $l++) { - if ($lines[$l] === "" && in_array(($lines[$l + 1] ?? ""), ["#ddocument", "#fragment"])) { + if ($lines[$l] === "" && in_array(($lines[$l + 1] ?? ""), ["#document", "#fragment"])) { break; } - assert(preg_match('/^[^#]/', $lines[$l]) === 1, new \Exception("Test $file #$index contains unrecognized data after document at line ".($l + 1))); + assert(preg_match('/^[^#]/', $lines[$l]) === 1, new \Exception("Test $file #$index contains unrecognized data after output at line ".($l + 1))); $exp[] = $lines[$l]; } $exp = implode("\n", $exp); @@ -80,8 +80,12 @@ class TestSerializer extends \PHPUnit\Framework\TestCase { protected function buildTree(array $data, bool $fragment): \DOMNode { $document = new Document; - $document->appendChild($document->createElement("html")); - $out = $fragment ? $document->createDocumentFragment() : $document; + if ($fragment) { + $document->appendChild($document->createElement("html")); + $out = $document->createDocumentFragment(); + } else { + $out = $document; + } $cur = $out; $pad = 2; // process each line in turn @@ -99,11 +103,11 @@ class TestSerializer extends \PHPUnit\Framework\TestCase { if (preg_match('/^$/', $d, $m)) { // comment $cur->appendChild($document->createComment($m[1])); - } elseif (preg_match('/^]*)(?: "([^"]*)" "([^"]*)")?>$/', $d, $m)) { + } elseif (preg_match('/^]*)(?: "([^"]*)" "([^"]*)")?)?>$/', $d, $m)) { // doctype - $name = strlen((string) $m[1]) ? $m[1] : null; - $public = strlen((string) $m[2]) ? $m[2] : null; - $system = strlen((string) $m[3]) ? $m[3] : null; + $name = strlen((string) ($m[1] ?? "")) ? $m[1] : " "; + $public = strlen((string) ($m[2] ?? "")) ? $m[2] : ""; + $system = strlen((string) ($m[3] ?? "")) ? $m[3] : ""; $cur->appendChild($document->implementation->createDocumentType($name, $public, $system)); } elseif (preg_match('/^<(?:([^ ]+) )?([^>]+)>$/', $d, $m)) { // element diff --git a/tests/cases/serializer/menbeam02.dat b/tests/cases/serializer/menbeam02.dat new file mode 100644 index 0000000..4dffa11 --- /dev/null +++ b/tests/cases/serializer/menbeam02.dat @@ -0,0 +1,28 @@ +#document +| +#output + + +#document +| +| +#output + + +#document +| +| +#output + + +#document +| +| +#output + + +#document +| +| +#output +