Browse Source

Tests and fixes for DOCTYPE serialization

split-manual
J. King 3 years ago
parent
commit
ee5e9298e7
  1. 5
      lib/DOM/traits/Serialize.php
  2. 20
      tests/cases/TestSerializer.php
  3. 28
      tests/cases/serializer/menbeam02.dat

5
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 # 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 # current node's name IDL attribute, followed by the literal string ">" (U+003E
# GREATER-THAN SIGN). # GREATER-THAN SIGN).
$s .= "<!DOCTYPE {$node->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 .= "<!DOCTYPE $name>";
$start = 1; $start = 1;
} }

20
tests/cases/TestSerializer.php

@ -61,10 +61,10 @@ class TestSerializer extends \PHPUnit\Framework\TestCase {
$exp = []; $exp = [];
assert($lines[$l] === "#output", new \Exception("Test $file #$index follows input with something other than output at line ".($l + 1))); 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++) { 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; 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[] = $lines[$l];
} }
$exp = implode("\n", $exp); $exp = implode("\n", $exp);
@ -80,8 +80,12 @@ class TestSerializer extends \PHPUnit\Framework\TestCase {
protected function buildTree(array $data, bool $fragment): \DOMNode { protected function buildTree(array $data, bool $fragment): \DOMNode {
$document = new Document; $document = new Document;
$document->appendChild($document->createElement("html")); if ($fragment) {
$out = $fragment ? $document->createDocumentFragment() : $document; $document->appendChild($document->createElement("html"));
$out = $document->createDocumentFragment();
} else {
$out = $document;
}
$cur = $out; $cur = $out;
$pad = 2; $pad = 2;
// process each line in turn // process each line in turn
@ -99,11 +103,11 @@ class TestSerializer extends \PHPUnit\Framework\TestCase {
if (preg_match('/^<!-- (.*?) -->$/', $d, $m)) { if (preg_match('/^<!-- (.*?) -->$/', $d, $m)) {
// comment // comment
$cur->appendChild($document->createComment($m[1])); $cur->appendChild($document->createComment($m[1]));
} elseif (preg_match('/^<!DOCTYPE ([^ >]*)(?: "([^"]*)" "([^"]*)")?>$/', $d, $m)) { } elseif (preg_match('/^<!DOCTYPE(?: ([^ >]*)(?: "([^"]*)" "([^"]*)")?)?>$/', $d, $m)) {
// doctype // doctype
$name = strlen((string) $m[1]) ? $m[1] : null; $name = strlen((string) ($m[1] ?? "")) ? $m[1] : " ";
$public = strlen((string) $m[2]) ? $m[2] : null; $public = strlen((string) ($m[2] ?? "")) ? $m[2] : "";
$system = strlen((string) $m[3]) ? $m[3] : null; $system = strlen((string) ($m[3] ?? "")) ? $m[3] : "";
$cur->appendChild($document->implementation->createDocumentType($name, $public, $system)); $cur->appendChild($document->implementation->createDocumentType($name, $public, $system));
} elseif (preg_match('/^<(?:([^ ]+) )?([^>]+)>$/', $d, $m)) { } elseif (preg_match('/^<(?:([^ ]+) )?([^>]+)>$/', $d, $m)) {
// element // element

28
tests/cases/serializer/menbeam02.dat

@ -0,0 +1,28 @@
#document
| <html>
#output
<html></html>
#document
| <!DOCTYPE html>
| <html>
#output
<!DOCTYPE html><html></html>
#document
| <!DOCTYPE html "public" "system">
| <html>
#output
<!DOCTYPE html><html></html>
#document
| <!DOCTYPE test>
| <html>
#output
<!DOCTYPE test><html></html>
#document
| <!DOCTYPE>
| <html>
#output
<!DOCTYPE ><html></html>
Loading…
Cancel
Save