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 special case for end tag </br>. Fixes #185 #186

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions src/HTML5/Parser/DOMTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,14 @@ public function endTag($name)
{
$lname = $this->normalizeTagName($name);

// Ignore closing tags for unary elements.
if (Elements::isA($name, Elements::VOID_TAG)) {
// Special case within 12.2.6.4.7: An end tag whose tag name is "br" should be treated as an opening tag
if ($name === 'br') {
$this->parseError('Closing tag encountered for void element br.');
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure that triggering a parse error is needed. Will the returned DOM still be as expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was just going by the wording of the spec, which has a specific definition of "parse error" in terms of declaring a document non-conformant, even where the result is well-defined.

The DOM should look exactly as though this was a normal void <br> tag with no attributes, so I'm happy to remove this line if you want to limit parseError to more serious problems.

Copy link
Contributor

Choose a reason for hiding this comment

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

We already have parse errors for stuff like duplicate IDs, so ...

Copy link
Member

Choose a reason for hiding this comment

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

👍


$this->startTag('br');
}
// Ignore closing tags for other unary elements.
elseif (Elements::isA($name, Elements::VOID_TAG)) {
return;
}

Expand Down
25 changes: 25 additions & 0 deletions test/HTML5/Parser/DOMTreeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,4 +715,29 @@ public function testIAudioInParagraph()
$this->assertSame('p', $audio->parentNode->nodeName);
$this->assertSame(3, $audio->childNodes->length);
}

public function testClosingBr()
{
$html = <<<EOM
<!DOCTYPE html>
<html>
<head>
<title>testClosingBr</title>
</head>
<body>
<p>
This line ends with a normal line break <br class="attribute-should-be-retained">
This line ends with a line break marked up as a closing tag </br class="attribute-should-be-discarded">
</p>
</body>
</html>>
</html>
EOM;

$dom = $this->parse($html);

$this->assertSame(2, $dom->getElementsByTagName('br')->length);
$this->assertSame(1, $dom->getElementsByTagName('br')->item(0)->attributes->length);
$this->assertSame(0, $dom->getElementsByTagName('br')->item(1)->attributes->length);
}
}