From db2d4a341f0c0ff1ec64fcc76d8bc63810241ac6 Mon Sep 17 00:00:00 2001 From: Yan Chen Date: Wed, 2 Dec 2026 10:47:12 +0800 Subject: [PATCH] xml: handle single-quoted literals, comments, and PIs in DOCTYPE Per W3C XML 1.0 (Fifth Edition) Sections 2.3, 2.5, 2.6, and 2.8, DOCTYPE SystemLiteral, PubidLiteral, and EntityValue strings may be delimited by either single or double quotes, and internal subsets may contain comments and processing instructions. Previously shiftDOCTYPEText only tracked double quotes, causing single-quoted literals containing '>', '[', or '"' and internal subset comments or PIs containing ']' to either terminate the DOCTYPE token prematurely or swallow the remainder of the document. Signed-off-by: Yan Chen --- xml/lex.go | 52 +++++++++++++++++++++++++++++++++++++++++-------- xml/lex_test.go | 7 +++++++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/xml/lex.go b/xml/lex.go index b7cc8f6..3311c44 100644 --- a/xml/lex.go +++ b/xml/lex.go @@ -181,20 +181,56 @@ func (l *Lexer) Next() (TokenType, []byte) { // The following functions follow the specifications at http://www.w3.org/html/wg/drafts/html/master/syntax.html func (l *Lexer) shiftDOCTYPEText() []byte { - inString := false + var quote byte inBrackets := false for { c := l.r.Peek(0) - if c == '"' { - inString = !inString - } else if (c == '[' || c == ']') && !inString { - inBrackets = (c == '[') - } else if c == '>' && !inString && !inBrackets { + if c == 0 { l.text = l.r.Lexeme()[9:] - l.r.Move(1) return l.r.Shift() - } else if c == 0 { + } else if quote != 0 { + if c == quote { + quote = 0 + } + } else if c == '"' || c == '\'' { + quote = c + } else if inBrackets && c == '<' { + if l.r.Peek(1) == '!' && l.r.Peek(2) == '-' && l.r.Peek(3) == '-' { + l.r.Move(4) + for { + c = l.r.Peek(0) + if c == 0 { + l.text = l.r.Lexeme()[9:] + return l.r.Shift() + } else if c == '-' && l.r.Peek(1) == '-' && l.r.Peek(2) == '>' { + l.r.Move(3) + break + } + l.r.Move(1) + } + continue + } else if l.r.Peek(1) == '?' { + l.r.Move(2) + for { + c = l.r.Peek(0) + if c == 0 { + l.text = l.r.Lexeme()[9:] + return l.r.Shift() + } else if c == '?' && l.r.Peek(1) == '>' { + l.r.Move(2) + break + } + l.r.Move(1) + } + continue + } + } else if c == '[' { + inBrackets = true + } else if c == ']' { + inBrackets = false + } else if c == '>' && !inBrackets { l.text = l.r.Lexeme()[9:] + l.r.Move(1) return l.r.Shift() } l.r.Move(1) diff --git a/xml/lex_test.go b/xml/lex_test.go index de77144..fd6b371 100644 --- a/xml/lex_test.go +++ b/xml/lex_test.go @@ -37,6 +37,13 @@ func TestTokens(t *testing.T) { {"", TTs{CDATAToken}}, {"", TTs{DOCTYPEToken}}, {"", TTs{DOCTYPEToken}}, + {"b.dtd'>", TTs{DOCTYPEToken, StartTagToken, StartTagCloseVoidToken}}, + {"", TTs{DOCTYPEToken, StartTagToken, StartTagCloseVoidToken}}, + {"", TTs{DOCTYPEToken, StartTagToken, StartTagCloseVoidToken}}, + {`]>`, TTs{DOCTYPEToken, StartTagToken, StartTagCloseVoidToken}}, + {`]>`, TTs{DOCTYPEToken, StartTagToken, StartTagCloseVoidToken}}, + {`]>`, TTs{DOCTYPEToken, StartTagToken, StartTagCloseVoidToken}}, + {`]>`, TTs{DOCTYPEToken, StartTagToken, StartTagCloseVoidToken}}, {` W3Schools.">]>`, TTs{DOCTYPEToken}}, {"", TTs{StartTagToken, StartTagCloseToken}},