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

Conversation

bsweeney
Copy link

@bsweeney bsweeney commented Aug 7, 2024

Per section 8.2.4.1 of spec version 20141028:

Parse error. Switch to the data state. Emit a U+003C LESS-THAN SIGN character token. Reconsume the current input character.

Note: the 20141028 version of the spec was used because the section numbering matches up with existing references in code. The relevant section for the living HTML5 spec is 13.2.5.1 and provides similar guidance.

fixes #250

Per the spec:

> Parse error. Switch to the data state. Emit a U+003C LESS-THAN SIGN character token. Reconsume the current input character.

https://www.w3.org/TR/2014/REC-html5-20141028/syntax.html#tag-open-state

fixes Masterminds#250
@benoit-waldmann
Copy link

up please 🙏

@ohader
Copy link
Contributor

ohader commented Sep 5, 2024

Note: the 20141028 version of the spec was used because the section numbering matches up with existing references in code. The relevant section for the living HTML5 spec is 13.2.5.1 and provides similar guidance.

I think it's 13.2.5.6 Tag open state, isn't it?

Anything else: This is an invalid-first-character-of-tag-name parse error. Emit a U+003C LESS-THAN SIGN character token. Reconsume in the data state.

The consequences are the same (adding <), which seems to be fine.

@@ -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.

@bsweeney
Copy link
Author

bsweeney commented Sep 5, 2024

Note: the 20141028 version of the spec was used because the section numbering matches up with existing references in code. The relevant section for the living HTML5 spec is 13.2.5.1 and provides similar guidance.

I think it's 13.2.5.6 Tag open state, isn't it?

Anything else: This is an invalid-first-character-of-tag-name parse error. Emit a U+003C LESS-THAN SIGN character token. Reconsume in the data state.

The consequences are the same (adding <), which seems to be fine.

I was attempting to address issues consuming the < while in data state. As noted in my other comment, there are indeed still issues to resolve with tag name and attribute name states.

@goetas goetas closed this Oct 31, 2024
@goetas goetas reopened this Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parser remove the single < (less than) character from given html string
4 participants