Skip to content

Commit

Permalink
fix: don't throw on comment in dtd
Browse files Browse the repository at this point in the history
PR-URL: #267
Credit: @SethFalco
Close: #267
Reviewed-by: @isaacs
  • Loading branch information
SethFalco authored and isaacs committed May 27, 2024
1 parent 93a63dd commit 8e8fa71
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
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' ],
]
})

0 comments on commit 8e8fa71

Please sign in to comment.