Browse Source

Fix DOCTYPE serialization

Also patch all top-level comments out
ns
J. King 3 years ago
parent
commit
ab972a838c
  1. 1
      lib/DOM/Document.php
  2. 16
      lib/DOM/DocumentType.php
  3. 26
      tests/cases/TestTreeConstructor.php

1
lib/DOM/Document.php

@ -10,7 +10,6 @@ class Document extends \DOMDocument {
$this->registerNodeClass('DOMComment', '\dW\HTML5\Comment');
$this->registerNodeClass('DOMDocumentFragment', '\dW\HTML5\DocumentFragment');
$this->registerNodeClass('DOMDocumentType', '\dW\HTML5\DocumentType');
$this->registerNodeClass('DOMElement', '\dW\HTML5\Element');
$this->registerNodeClass('DOMProcessingInstruction', '\dW\HTML5\ProcessingInstruction');
$this->registerNodeClass('DOMText', '\dW\HTML5\Text');

16
lib/DOM/DocumentType.php

@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
namespace dW\HTML5;
class DocumentType extends \DOMDocumentType {
function __toString(): string {
# Append the literal string "<!DOCTYPE" (U+003C LESS-THAN SIGN, U+0021
# EXCLAMATION MARK, U+0044 LATIN CAPITAL LETTER D, U+004F LATIN CAPITAL LETTER
# O, U+0043 LATIN CAPITAL LETTER C, U+0054 LATIN CAPITAL LETTER T, U+0059 LATIN
# CAPITAL LETTER Y, U+0050 LATIN CAPITAL LETTER P, U+0045 LATIN CAPITAL 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).
return "<!DOCTYPE {$this->name}>";
}
}

26
tests/cases/TestTreeConstructor.php

@ -33,7 +33,10 @@ class TestTreeConstructor extends \PHPUnit\Framework\TestCase {
$this->markTestIncomplete("Fragment tests still to be implemented");
}
// certain tests need to be patched to ignore unavoidable limitations of PHP's DOM
$exp = $this->patchTest($data, $fragment, $exp);
[$exp, $patched] = $this->patchTest($data, $fragment, $exp);
if ($patched) {
$this->markAsRisky();
}
// convert parse error constants into standard symbols in specification
$errorMap = array_map(function($str) {
return strtolower(str_replace("_", "-", $str));
@ -72,11 +75,15 @@ class TestTreeConstructor extends \PHPUnit\Framework\TestCase {
}
protected function patchTest(string $data, $fragment, array $exp): array {
if (strpos($exp[0], "| <!--") === 0) {
// comments before the DOCTYPE are silently dropped by the PHP DOM
array_shift($exp);
$patched = false;
// comments outside the root element are silently dropped by the PHP DOM
for ($a = 0; $a < sizeof($exp); $a++) {
if (strpos($exp[$a], "| <!--") === 0) {
array_splice($exp, $a--, 1);
$patched = true;
}
}
return $exp;
return [$exp, $patched];
}
protected function push(string $data): void {
@ -87,9 +94,12 @@ class TestTreeConstructor extends \PHPUnit\Framework\TestCase {
$this->out = [];
$this->depth = 0;
if ($d->doctype) {
$dt = "<!DOCTYPE ".$d->doctype->name;
$dt .= strlen($d->doctype->publicId) ? ' "'.$d->doctype->publicId.'"' : "";
$dt .= strlen($d->doctype->systemId) ? ' "'.$d->doctype->systemId.'"' : "";
$dt = "<!DOCTYPE ";
$dt .= ($d->doctype->name !== " ") ? $d->doctype->name : "";
if (strlen($d->doctype->publicId) || strlen($d->doctype->systemId)) {
$dt .= ' "'.$d->doctype->publicId.'"';
$dt .= ' "'.$d->doctype->systemId.'"';
}
$dt .= ">";
$this->push($dt);
}

Loading…
Cancel
Save