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

fix: don't throw on comment in dtd #267

Merged
merged 1 commit into from
May 27, 2024
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
24 changes: 19 additions & 5 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,15 +1090,23 @@
continue

case S.SGML_DECL:
if (parser.sgmlDecl + c === '--') {
parser.state = S.COMMENT
parser.comment = ''
parser.sgmlDecl = ''
continue;
}

if (parser.doctype && parser.doctype !== true) {
parser.sgmlDecl += c
continue;
}

if ((parser.sgmlDecl + c).toUpperCase() === CDATA) {
emitNode(parser, 'onopencdata')
parser.state = S.CDATA
parser.sgmlDecl = ''
parser.cdata = ''
} else if (parser.sgmlDecl + c === '--') {
parser.state = S.COMMENT
parser.comment = ''
parser.sgmlDecl = ''
} else if ((parser.sgmlDecl + c).toUpperCase() === DOCTYPE) {
parser.state = S.DOCTYPE
if (parser.doctype || parser.sawRoot) {
Expand Down Expand Up @@ -1152,10 +1160,14 @@
continue

case S.DOCTYPE_DTD:
parser.doctype += c
if (c === ']') {
parser.doctype += c
parser.state = S.DOCTYPE
} else if (c === '<') {
parser.state = S.OPEN_WAKA
parser.startTagPosition = parser.position
} else if (isQuote(c)) {
parser.doctype += c
parser.state = S.DOCTYPE_DTD_QUOTED
parser.q = c
}
Expand Down Expand Up @@ -1198,6 +1210,8 @@
// which is a comment of " blah -- bloo "
parser.comment += '--' + c
parser.state = S.COMMENT
} else if (parser.doctype && parser.doctype !== true) {
parser.state = S.DOCTYPE_DTD
} else {
parser.state = S.TEXT
}
Expand Down
10 changes: 10 additions & 0 deletions test/doctype-with-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require(__dirname).test({
xml: '<!DOCTYPE svg [<!--comment with \' and ] symbols-->]><svg></svg>',
expect: [
[ 'comment', 'comment with \' and ] symbols' ],
[ 'doctype', ' svg []' ],
[ 'opentagstart', { name: 'SVG', attributes: {} } ],
[ 'opentag', { name: 'SVG', attributes: {}, isSelfClosing: false } ],
[ 'closetag', 'SVG' ],
]
})
Loading