From 8e8fa7167244ac155c8a976df2da6cf5abef7ac2 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 6 Jan 2024 16:06:42 +0000 Subject: [PATCH] fix: don't throw on comment in dtd PR-URL: https://github.com/isaacs/sax-js/pull/267 Credit: @SethFalco Close: #267 Reviewed-by: @isaacs --- lib/sax.js | 24 +++++++++++++++++++----- test/doctype-with-comment.js | 10 ++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 test/doctype-with-comment.js diff --git a/lib/sax.js b/lib/sax.js index 51e8d1a..c4288cc 100644 --- a/lib/sax.js +++ b/lib/sax.js @@ -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) { @@ -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 } @@ -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 } diff --git a/test/doctype-with-comment.js b/test/doctype-with-comment.js new file mode 100644 index 0000000..7ad2e53 --- /dev/null +++ b/test/doctype-with-comment.js @@ -0,0 +1,10 @@ +require(__dirname).test({ + xml: ']>', + expect: [ + [ 'comment', 'comment with \' and ] symbols' ], + [ 'doctype', ' svg []' ], + [ 'opentagstart', { name: 'SVG', attributes: {} } ], + [ 'opentag', { name: 'SVG', attributes: {}, isSelfClosing: false } ], + [ 'closetag', 'SVG' ], + ] +})