Skip to content

Commit 8696866

Browse files
committed
Add special case for end tag </br>. Fixes #185
Normally, an end tag for a void element would simply be discarded, but the spec includes a special rule as follows: > An end tag whose tag name is "br" > Parse error. Drop the attributes from the token, and act as > described in the next entry; i.e. act as if this was a "br" > start tag token with no attributes, rather than the end tag > token that it actually is.
1 parent a3edfe5 commit 8696866

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/HTML5/Parser/DOMTreeBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,14 @@ public function endTag($name)
474474
{
475475
$lname = $this->normalizeTagName($name);
476476

477-
// Ignore closing tags for unary elements.
478-
if (Elements::isA($name, Elements::VOID_TAG)) {
477+
// Special case within 12.2.6.4.7: An end tag whose tag name is "br" should be treated as an opening tag
478+
if ($name === 'br') {
479+
$this->parseError('Closing tag encountered for void element br.');
480+
481+
$this->startTag('br');
482+
}
483+
// Ignore closing tags for other unary elements.
484+
elseif (Elements::isA($name, Elements::VOID_TAG)) {
479485
return;
480486
}
481487

test/HTML5/Parser/DOMTreeBuilderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,29 @@ public function testIAudioInParagraph()
715715
$this->assertSame('p', $audio->parentNode->nodeName);
716716
$this->assertSame(3, $audio->childNodes->length);
717717
}
718+
719+
public function testClosingBr()
720+
{
721+
$html = <<<EOM
722+
<!DOCTYPE html>
723+
<html>
724+
<head>
725+
<title>testClosingBr</title>
726+
</head>
727+
<body>
728+
<p>
729+
This line ends with a normal line break <br class="attribute-should-be-retained">
730+
This line ends with a line break marked up as a closing tag </br class="attribute-should-be-discarded">
731+
</p>
732+
</body>
733+
</html>>
734+
</html>
735+
EOM;
736+
737+
$dom = $this->parse($html);
738+
739+
$this->assertSame(2, $dom->getElementsByTagName('br')->length);
740+
$this->assertSame(1, $dom->getElementsByTagName('br')->item(0)->attributes->length);
741+
$this->assertSame(0, $dom->getElementsByTagName('br')->item(1)->attributes->length);
742+
}
718743
}

0 commit comments

Comments
 (0)