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 #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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));
Copy link
Contributor

Choose a reason for hiding this comment

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

In that case, the parser would be in the 13.2.5.8 Tag name state - thus, form my POV the startTag should be span< and the text just 02.

\Masterminds\HTML5\Parser\Tokenizer::consumeData checks for is_alpha($tok) and thus skips < for the tag name. It seems the states are "optimized" in the current implementation.

In general I think that the current state of the test is still fine (having <>02 as text).
There might be a comment above the test that there is this divergence to the specs.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, the parser should be in tag name state, but tag name parsing does not quite follow the spec. It only consumes a restricted set of characters and then enters attribute state, where the spec indicates that the character should be part of the tag name. Attribute state parsing also does not quite follow the spec in that it exits as soon as it encounters the <, where the spec indicates it should be treated as part of an attribute name while in that state (thus <span <>02</span> also does not produce the correct output). At this point the parser re-enters the data state and the character combo <> does not match a known state and so is consumed as data. With the change to the data state handling the extra < is exposed whereas before it was just swallowed.

That was the meaning behind my comment in 250 that there are other issues to address. It took me a while to get the data state parsing right, so I didn't want to immediately dive into the parsing issues with the other states.

Copy link
Author

Choose a reason for hiding this comment

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

The original issue is referencing problems in the data state and that was what I was attempting to address. Instead of a comment here, the parsing issues in tag name and attribute name could be addressed now in this PR or a new issue outlining the problem in those states could be opened with information about the divergence. I was primarily waiting to see what feedback I got on the current change before deciding whether to address those issues here or open a new issue and defer the additional work.

$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
Loading