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

Continue parsing as data after invalid opening tag #1

Merged
merged 1 commit into from
Jan 14, 2025
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
17 changes: 8 additions & 9 deletions src/HTML5/Parser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,25 @@ protected function consumeData()

// Parse tag
if ('<' === $tok) {
// Any buffered text data can go out now.
$this->flushBuffer();

$tok = $this->scanner->next();

if (false === $tok) {
// end of string
$this->parseError('Illegal tag opening');
} elseif ('!' === $tok) {
if ('!' === $tok) {
$this->flushBuffer();
$this->markupDeclaration();
} elseif ('/' === $tok) {
$this->flushBuffer();
$this->endTag();
} elseif ('?' === $tok) {
$this->flushBuffer();
$this->processingInstruction();
} elseif ($this->is_alpha($tok)) {
$this->flushBuffer();
$this->tagName();
} else {
$this->parseError('Illegal tag opening');
// TODO is this necessary ?
$this->characterData();
$this->text('<');
$this->scanner->unconsume();
return;
}

$tok = $this->scanner->current();
Expand Down
3 changes: 3 additions & 0 deletions test/HTML5/Html5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ public function testUnescaped()

$res = $this->cycleFragment('<style>div>div>div</style>');
$this->assertRegExp('|div>div>div|', $res);

$res = $this->cycleFragment('<p>There is a less-than character after this word < is it rendered?</p>');
$this->assertRegExp('|<p>There is a less-than character after this word &lt; is it rendered\\?</p>|', $res);
}

public function testEntities()
Expand Down
12 changes: 11 additions & 1 deletion test/HTML5/Parser/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public function testTagNotClosedAfterTagName()
$this->assertEventError($events->get(0));
$this->assertEventEquals('startTag', 'span', $events->get(1));
$this->assertEventError($events->get(2));
$this->assertEventEquals('text', '>02', $events->get(3));
$this->assertEventEquals('text', '<>02', $events->get(3));
$this->assertEventEquals('endTag', 'span', $events->get(4));
$this->assertEventEquals('eof', null, $events->get(5));

Expand Down Expand Up @@ -957,6 +957,16 @@ public function testText()
$events = $this->parse('a&sup2;b');
$this->assertEquals(2, $events->depth(), 'Events: ' . print_r($events, true));
$this->assertEventEquals('text', 'a²b', $events->get(0));

$events = $this->parse('a < b');
$this->assertEquals(3, $events->depth(), 'Events: ' . print_r($events, true));
$this->assertEventError($events->get(0));
$this->assertEventEquals('text', 'a < b', $events->get(1));

$events = $this->parse('a <');
$this->assertEquals(3, $events->depth(), 'Events: ' . print_r($events, true));
$this->assertEventError($events->get(0));
$this->assertEventEquals('text', 'a <', $events->get(1));
}

// ================================================================
Expand Down