Skip to content

Commit

Permalink
Merge pull request #186 from IMSoP/issue-185-closing-br
Browse files Browse the repository at this point in the history
Add special case for end tag </br>. Fixes #185
  • Loading branch information
goetas committed Jun 24, 2020
2 parents a3edfe5 + 8696866 commit d7961a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
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.');

$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);
}
}

0 comments on commit d7961a8

Please sign in to comment.