Skip to content

Commit

Permalink
fix: doctype with entities handling
Browse files Browse the repository at this point in the history
PR-URL: #272
Credit: @SethFalco
Close: #272
Reviewed-by: @isaacs
  • Loading branch information
SethFalco authored and isaacs committed May 27, 2024
1 parent 5d10b0a commit 968b53c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
13 changes: 7 additions & 6 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,12 +1103,11 @@
continue;
}

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

if ((parser.sgmlDecl + c).toUpperCase() === CDATA) {
if (parser.doctype && parser.doctype !== true && parser.sgmlDecl) {
parser.state = S.DOCTYPE_DTD
parser.doctype += '<!' + parser.sgmlDecl + c
parser.sgmlDecl = ''
} else if ((parser.sgmlDecl + c).toUpperCase() === CDATA) {
emitNode(parser, 'onopencdata')
parser.state = S.CDATA
parser.sgmlDecl = ''
Expand Down Expand Up @@ -1176,6 +1175,8 @@
parser.doctype += c
parser.state = S.DOCTYPE_DTD_QUOTED
parser.q = c
} else {
parser.doctype += c
}
continue

Expand Down
10 changes: 0 additions & 10 deletions test/doctype-with-comment.js

This file was deleted.

45 changes: 45 additions & 0 deletions test/doctypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// with entity
require(__dirname).test({
xml: '<!DOCTYPE svg [ <!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/"> ]><svg></svg>',
expect: [
[ 'doctype', ' svg [ <!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/"> ]' ],
[ 'opentagstart', { name: 'SVG', attributes: {} } ],
[ 'opentag', { name: 'SVG', attributes: {}, isSelfClosing: false } ],
[ 'closetag', 'SVG' ],
]
})

// with comment
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' ],
]
})

// with entity and comment
require(__dirname).test({
xml: '<!DOCTYPE svg [<!--comment with \' and ] symbols--> <!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/"> ]><svg></svg>',
expect: [
[ 'comment', 'comment with \' and ] symbols' ],
[ 'doctype', ' svg [ <!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/"> ]' ],
[ 'opentagstart', { name: 'SVG', attributes: {} } ],
[ 'opentag', { name: 'SVG', attributes: {}, isSelfClosing: false } ],
[ 'closetag', 'SVG' ],
]
})

// with empty doctype
require(__dirname).test({
xml: '<!DOCTYPE svg []><svg></svg>',
expect: [
[ 'doctype', ' svg []' ],
[ 'opentagstart', { name: 'SVG', attributes: {} } ],
[ 'opentag', { name: 'SVG', attributes: {}, isSelfClosing: false } ],
[ 'closetag', 'SVG' ],
]
})

0 comments on commit 968b53c

Please sign in to comment.