Skip to content

Commit 8e8fa71

Browse files
SethFalcoisaacs
authored andcommitted
fix: don't throw on comment in dtd
PR-URL: #267 Credit: @SethFalco Close: #267 Reviewed-by: @isaacs
1 parent 93a63dd commit 8e8fa71

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/sax.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,15 +1090,23 @@
10901090
continue
10911091

10921092
case S.SGML_DECL:
1093+
if (parser.sgmlDecl + c === '--') {
1094+
parser.state = S.COMMENT
1095+
parser.comment = ''
1096+
parser.sgmlDecl = ''
1097+
continue;
1098+
}
1099+
1100+
if (parser.doctype && parser.doctype !== true) {
1101+
parser.sgmlDecl += c
1102+
continue;
1103+
}
1104+
10931105
if ((parser.sgmlDecl + c).toUpperCase() === CDATA) {
10941106
emitNode(parser, 'onopencdata')
10951107
parser.state = S.CDATA
10961108
parser.sgmlDecl = ''
10971109
parser.cdata = ''
1098-
} else if (parser.sgmlDecl + c === '--') {
1099-
parser.state = S.COMMENT
1100-
parser.comment = ''
1101-
parser.sgmlDecl = ''
11021110
} else if ((parser.sgmlDecl + c).toUpperCase() === DOCTYPE) {
11031111
parser.state = S.DOCTYPE
11041112
if (parser.doctype || parser.sawRoot) {
@@ -1152,10 +1160,14 @@
11521160
continue
11531161

11541162
case S.DOCTYPE_DTD:
1155-
parser.doctype += c
11561163
if (c === ']') {
1164+
parser.doctype += c
11571165
parser.state = S.DOCTYPE
1166+
} else if (c === '<') {
1167+
parser.state = S.OPEN_WAKA
1168+
parser.startTagPosition = parser.position
11581169
} else if (isQuote(c)) {
1170+
parser.doctype += c
11591171
parser.state = S.DOCTYPE_DTD_QUOTED
11601172
parser.q = c
11611173
}
@@ -1198,6 +1210,8 @@
11981210
// which is a comment of " blah -- bloo "
11991211
parser.comment += '--' + c
12001212
parser.state = S.COMMENT
1213+
} else if (parser.doctype && parser.doctype !== true) {
1214+
parser.state = S.DOCTYPE_DTD
12011215
} else {
12021216
parser.state = S.TEXT
12031217
}

test/doctype-with-comment.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require(__dirname).test({
2+
xml: '<!DOCTYPE svg [<!--comment with \' and ] symbols-->]><svg></svg>',
3+
expect: [
4+
[ 'comment', 'comment with \' and ] symbols' ],
5+
[ 'doctype', ' svg []' ],
6+
[ 'opentagstart', { name: 'SVG', attributes: {} } ],
7+
[ 'opentag', { name: 'SVG', attributes: {}, isSelfClosing: false } ],
8+
[ 'closetag', 'SVG' ],
9+
]
10+
})

0 commit comments

Comments
 (0)