Fix stack overflow when whitespace appears after attribute name and before tag-closing >

This commit is contained in:
Shadowfacts 2024-02-06 18:46:56 -05:00
parent 7e624c0a4a
commit aed61d84d3
2 changed files with 9 additions and 2 deletions

View File

@ -731,7 +731,7 @@ private extension Tokenizer {
switch c { switch c {
case "\t", "\n", "\u{000C}", " ": case "\t", "\n", "\u{000C}", " ":
// ignore the character // ignore the character
return next() return tokenizeBeforeAttributeName()
case "/", ">", nil: case "/", ">", nil:
reconsume(c) reconsume(c)
state = .afterAttributeName state = .afterAttributeName
@ -794,13 +794,16 @@ private extension Tokenizer {
switch nextChar() { switch nextChar() {
case "\t", "\n", "\u{000C}", " ": case "\t", "\n", "\u{000C}", " ":
// ignore the character // ignore the character
return tokenizeAttributeName() return tokenizeAfterAttributeName()
case "/": case "/":
state = .selfClosingStartTag state = .selfClosingStartTag
return tokenizeSelfClosingStartTag() return tokenizeSelfClosingStartTag()
case "=": case "=":
state = .beforeAttributeValue state = .beforeAttributeValue
return tokenizeBeforeAttributeValue() return tokenizeBeforeAttributeValue()
case ">":
state = .data
return takeCurrentToken()
case nil: case nil:
// parse error: eof-in-tag // parse error: eof-in-tag
state = .endOfFile state = .endOfFile

View File

@ -74,6 +74,10 @@ final class TokenizerTests: XCTestCase {
XCTAssertEqual(tokenize("🇺🇸"), [.characterSequence("\u{1F1FA}\u{1F1F8}")]) XCTAssertEqual(tokenize("🇺🇸"), [.characterSequence("\u{1F1FA}\u{1F1F8}")])
} }
func testWhitespaceAfterAttributeName() {
XCTAssertEqual(tokenize("<a foo >"), [.startTag("a", selfClosing: false, attributes: [.init(name: "foo", value: "")])])
}
} }
private struct PrintIterator<Inner: IteratorProtocol>: IteratorProtocol { private struct PrintIterator<Inner: IteratorProtocol>: IteratorProtocol {