Skip to content

Commit 9b0419a

Browse files
Valentin Kunzgoetas
authored andcommitted
don't close optgroup when followed by an option
Make sure options are children of optgroup, not the parent select. Otherwise we end up with a bunch of empty optgroups.
1 parent c30ea30 commit 9b0419a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/HTML5/Parser/TreeBuildingRules.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public function evaluate($new, $current)
7676
case 'option':
7777
return $this->closeIfCurrentMatches($new, $current, array(
7878
'option',
79-
'optgroup'
8079
));
8180
case 'tr':
8281
return $this->closeIfCurrentMatches($new, $current, array(

test/HTML5/Parser/DOMTreeBuilderTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,33 @@ public function testInstructionProcessor()
540540
$this->assertEquals('div', $div->tagName);
541541
$this->assertEquals('foo', $div->textContent);
542542
}
543+
544+
public function testSelectGroupedOptions()
545+
{
546+
$html = <<<EOM
547+
<!DOCTYPE html>
548+
<html>
549+
<head>
550+
<title>testSelectGroupedOptions</title>
551+
</head>
552+
<body>
553+
<select>
554+
<optgroup id="first" label="first">
555+
<option value="foo">foo</option>
556+
<option value="bar">bar</option>
557+
<option value="baz">baz</option>
558+
</optgroup>
559+
<optgroup id="second" label="first">
560+
<option value="lorem">lorem</option>
561+
<option value="ipsum">ipsum</option>
562+
</optgroup>
563+
</select>
564+
</body>
565+
</html>
566+
EOM;
567+
$dom = $this->parse($html);
568+
569+
$this->assertSame(3, $dom->getElementById('first')->getElementsByTagName('option')->length);
570+
$this->assertSame(2, $dom->getElementById('second')->getElementsByTagName('option')->length);
571+
}
543572
}

test/HTML5/Parser/TreeBuildingRulesTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ public function testHandleDT()
9090
$this->assertEquals('dd', $list->lastChild->tagName);
9191
}
9292

93+
public function testHandleOption()
94+
{
95+
$html = sprintf(self::HTML_STUB, '<optgroup id="foo" label="foo" ><option value="foo" >bar</option></optgroup>');
96+
$doc = $this->parse($html);
97+
98+
$list = $doc->getElementById('foo');
99+
100+
$this->assertEquals(1, $list->childNodes->length);
101+
foreach ($list->childNodes as $ele) {
102+
$this->assertEquals('option', $ele->tagName);
103+
}
104+
}
105+
93106
public function testTable()
94107
{
95108
$html = sprintf(self::HTML_STUB, '<table><thead id="a"><th>foo<td>bar<td>baz');

0 commit comments

Comments
 (0)