Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <body> if it's missing #183

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/HTML5/Parser/DOMTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ public function startTag($name, $attributes = array(), $selfClosing = false)
break;
}

// Case when no <body> exists, note section on 'Anything else' below.
// https://html.spec.whatwg.org/multipage/parsing.html#the-after-head-insertion-mode
if ($this->insertMode === static::IM_AFTER_HEAD && 'head' !== $name && 'body' !== $name) {
$this->startTag('body');
}

// Special case handling for SVG.
if ($this->insertMode === static::IM_IN_SVG) {
$lname = Elements::normalizeSvgElement($lname);
Expand Down Expand Up @@ -556,21 +562,20 @@ public function comment($cdata)

public function text($data)
{
// XXX: Hmmm.... should we really be this strict?
// https://html.spec.whatwg.org/multipage/parsing.html#the-before-head-insertion-mode
if ($this->insertMode < static::IM_IN_HEAD) {
// Per '8.2.5.4.3 The "before head" insertion mode' the characters
// " \t\n\r\f" should be ignored but no mention of a parse error. This is
// practical as most documents contain these characters. Other text is not
// expected here so recording a parse error is necessary.
// " \t\n\r\f" should be ignored .
$dataTmp = trim($data, " \t\n\r\f");
if (!empty($dataTmp)) {
// fprintf(STDOUT, "Unexpected insert mode: %d", $this->insertMode);
$this->parseError('Unexpected text. Ignoring: ' . $dataTmp);
$this->startTag('head');
$this->endTag('head');
$this->startTag('body');
} else {
return;
}

return;
}
// fprintf(STDOUT, "Appending text %s.", $data);

$node = $this->doc->createTextNode($data);
$this->current->appendChild($node);
}
Expand Down
46 changes: 46 additions & 0 deletions test/HTML5/Html5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,50 @@ public function testAnchorTargetQueryParam()
$res
);
}

/**
* Test for issue #166.
goetas marked this conversation as resolved.
Show resolved Hide resolved
*
* @param $input
* @param $expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such @param without type is totally useless. either add the types or remove those phpdoc

*
* @dataProvider tagOmissionProvider
*/
public function testTagOmission($input, $expected)
{
$doc = $this->html5->loadHTML($input);

$out = $this->html5->saveHTML($doc);

$this->assertRegExp('|' . preg_quote($expected, '|') . '|', $out);
}

/**
* Tag omission test cases.
*
* @return \string[][]
*/
public function tagOmissionProvider()
{
return $provider = array(
goetas marked this conversation as resolved.
Show resolved Hide resolved
array(
'<html>Hello, This is a test.<br />Does it work this time?</html>',
'<html><head></head><body>Hello, This is a test.<br>Does it work this time?</body></html>',
),
// test whitespace (\n)
array(
'<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<br>
</body>
</html>',
'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<br>
</body>',
),
);
}
}
9 changes: 6 additions & 3 deletions test/HTML5/Parser/DOMTreeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,17 @@ public function testText()
$data = $wrapper->childNodes->item(0);
$this->assertEquals(XML_TEXT_NODE, $data->nodeType);
$this->assertEquals('test', $data->data);
}

public function testTextBeforeHeadNotAllowed()
{
// The DomTreeBuilder has special handling for text when in before head mode.
$html = '<!DOCTYPE html><html>
Foo<head></head><body></body></html>';
$html = '<!DOCTYPE html><html>Foo<head></head><body></body></html>';
$doc = $this->parse($html);
$this->assertEquals('Line 0, Col 0: Unexpected text. Ignoring: Foo', $this->errors[0]);
$this->assertEquals('Line 0, Col 0: Unexpected head tag outside of head context.', $this->errors[0]);
$headElement = $doc->documentElement->firstChild;
$this->assertEquals('head', $headElement->tagName);
$this->assertXmlStringEqualsXmlString($doc, '<html xmlns="http://www.w3.org/1999/xhtml"><head/><body>Foo<head/><body/></body></html>');
goetas marked this conversation as resolved.
Show resolved Hide resolved
}

public function testParseErrors()
Expand Down