buildTree($data, $fragment);
$this->assertSame($exp, Serializer::serialize($node));
}
public function provideStandardTreeTests(): iterable {
$blacklist = [];
$files = new \AppendIterator();
$files->append(new \GlobIterator(\MensBeam\HTML\Parser\BASE."tests/cases/serializer/*.dat", \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME));
foreach ($files as $file) {
if (!in_array(basename($file), $blacklist)) {
yield from $this->parseTreeTestFile($file);
}
}
}
/** @dataProvider provideTemplateTests */
public function testSerializeADecoratedTemplate(?string $ns, string $exp): void {
$d = new \DOMDocument;
$t = $d->createElementNS($ns, "template");
$t->appendChild($d->createTextNode("EEK"));
$exp1 = $exp;
$exp2 = "$exp";
$this->assertSame($exp1, Serializer::serializeInner($t));
$this->assertSame($exp2, Serializer::serialize($t));
}
public function provideTemplateTests(): iterable {
return [
[null, "EEK"],
[Parser::HTML_NAMESPACE, "EEK"],
];
}
/** @dataProvider provideEmptyElementTests */
public function testInnerSerializeEmptyElement(string $tagName, ?string $ns, string $exp): void {
$d = new \DOMDocument;
$e = $d->createElementNS($ns, $tagName);
$e->appendChild($d->createTextNode("EEK"));
$this->assertSame($exp, Serializer::serializeInner($e));
}
public function provideEmptyElementTests(): iterable {
return [
["basefont", null, ""],
["bgsound", null, ""],
["frame", null, ""],
["keygen", null, ""],
["area", null, ""],
["base", null, ""],
["br", null, ""],
["col", null, ""],
["embed", null, ""],
["hr", null, ""],
["img", null, ""],
["input", null, ""],
["link", null, ""],
["meta", null, ""],
["param", null, ""],
["source", null, ""],
["track", null, ""],
["wbr", null, ""],
["basefont", Parser::HTML_NAMESPACE, ""],
["bgsound", Parser::HTML_NAMESPACE, ""],
["frame", Parser::HTML_NAMESPACE, ""],
["keygen", Parser::HTML_NAMESPACE, ""],
["area", Parser::HTML_NAMESPACE, ""],
["base", Parser::HTML_NAMESPACE, ""],
["br", Parser::HTML_NAMESPACE, ""],
["col", Parser::HTML_NAMESPACE, ""],
["embed", Parser::HTML_NAMESPACE, ""],
["hr", Parser::HTML_NAMESPACE, ""],
["img", Parser::HTML_NAMESPACE, ""],
["input", Parser::HTML_NAMESPACE, ""],
["link", Parser::HTML_NAMESPACE, ""],
["meta", Parser::HTML_NAMESPACE, ""],
["param", Parser::HTML_NAMESPACE, ""],
["source", Parser::HTML_NAMESPACE, ""],
["track", Parser::HTML_NAMESPACE, ""],
["wbr", Parser::HTML_NAMESPACE, ""],
["basefont", Parser::SVG_NAMESPACE, "EEK"],
["bgsound", Parser::SVG_NAMESPACE, "EEK"],
["frame", Parser::SVG_NAMESPACE, "EEK"],
["keygen", Parser::SVG_NAMESPACE, "EEK"],
["area", Parser::SVG_NAMESPACE, "EEK"],
["base", Parser::SVG_NAMESPACE, "EEK"],
["br", Parser::SVG_NAMESPACE, "EEK"],
["col", Parser::SVG_NAMESPACE, "EEK"],
["embed", Parser::SVG_NAMESPACE, "EEK"],
["hr", Parser::SVG_NAMESPACE, "EEK"],
["img", Parser::SVG_NAMESPACE, "EEK"],
["input", Parser::SVG_NAMESPACE, "EEK"],
["link", Parser::SVG_NAMESPACE, "EEK"],
["meta", Parser::SVG_NAMESPACE, "EEK"],
["param", Parser::SVG_NAMESPACE, "EEK"],
["source", Parser::SVG_NAMESPACE, "EEK"],
["track", Parser::SVG_NAMESPACE, "EEK"],
["wbr", Parser::SVG_NAMESPACE, "EEK"],
];
}
/** @dataProvider provideCustomSerializations */
public function testSerializeWithOptions(bool $fragment, ?string $fragmentContext, int $indentStep, bool $indentWithSpaces, bool $processingInstructions, bool $reformatWhitespace, bool $boolAttr, bool $foreignVoid, string $in, string $exp): void {
$parserConfig = new Config();
$parserConfig->processingInstructions = $processingInstructions;
$serializerConfig = [
'booleanAttributeValues' => $boolAttr,
'foreignVoidEndTags' => $foreignVoid,
'indentStep' => $indentStep,
'indentWithSpaces' => $indentWithSpaces,
'reformatWhitespace' => $reformatWhitespace
];
if (!$fragment) {
$d = Parser::parse($in, "UTF-8", $parserConfig)->document;
$act = Parser::serialize($d, $serializerConfig);
} else {
$d = new \DOMDocument();
$act = Parser::serialize(Parser::parseFragment($d->createElement($fragmentContext), 0, $in, 'UTF-8', $parserConfig), $serializerConfig);
}
$this->assertSame($exp, $act);
}
public function provideCustomSerializations(): iterable {
return [
// Boolean attribute values serialized
[false, null, 0, false, false, false, true, true,
<<
HTML,
<<
HTML
],
// Reformat whitespace, element grouping, foreign "block" content, & foreign
// void end tags not serialized
[false, null, 1, true, false, true, false, false,
<<
HTML,
<<
HTML
],
// Inline serialized comments and processing instructions, parsing of processing instructions off
[false, null, 1, true, false, true, false, false,
<<
HTML,
<<
HTML
],
// Block serialized comments and processing instructions, parsing of processing instructions on
[false, null, 1, true, true, true, false, false,
<<
HTML,
<<
HTML
],
// Reformat whitespace, whitespace collapsing, custom indentions
[false, null, 4, true, false, true, false, false,
<<
ook eek
This should be ignored also this
Ook Eek!
HTML, << ook eekThis should be ignored also this
Ook Eek!
HTML ], // Reformat whitespace, whitespace collapsing, custom indentions, tabs [false, null, 1, false, false, true, false, false, << ook eekThis should be ignored also this
Ook Eek!
HTML, << \t \took eek \t\tThis should be ignored also this\t\t \t\t
Ook Eek!
\t HTML ], // Fragment, html elements [true, 'div', 1, true, false, true, false, false, << Ook! HTML, <<Ook! HTML ], // Fragment, foreign elements [true, 'div', 1, true, false, true, false, false, <<Ook!
HTML; $d = Parser::parse($html, "UTF-8", $parserConfig)->document; return Parser::serialize($d->getElementsByTagName('p')->item(0), $serializerConfig); }, <<Ook! HTML ], // Solo html element without context [1, true, false, true, false, false, function (Config $parserConfig, array $serializerConfig): string { $html = <<Ook!
HTML; $d = Parser::parse($html, "UTF-8", $parserConfig)->document; $p = $d->getElementsByTagName('p')->item(0); $p->parentNode->removeChild($p); return Parser::serialize($p, $serializerConfig); }, <<Ook! HTML ], // Solo svg element serializing as inline with context [1, true, false, true, false, true, function (Config $parserConfig, array $serializerConfig): string { $html = << HTML; $d = Parser::parse($html, "UTF-8", $parserConfig)->document; $svg = $d->getElementsByTagName('svg')->item(0); return Parser::serialize($svg, $serializerConfig); }, <<