Skip to content

Commit

Permalink
fix: ignore unparsed entity logic for std xml entities
Browse files Browse the repository at this point in the history
PR-URL: #266
Credit: @SethFalco
Close: #266
Reviewed-by: @HeikoTheissen
  • Loading branch information
SethFalco authored and isaacs committed May 27, 2024
1 parent b577600 commit 93a63dd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -1483,13 +1483,13 @@
}

if (c === ';') {
if (parser.opt.unparsedEntities) {
var parsedEntity = parseEntity(parser)
var parsedEntity = parseEntity(parser)
if (parser.opt.unparsedEntities && !Object.values(sax.XML_ENTITIES).includes(parsedEntity)) {
parser.entity = ''
parser.state = returnState
parser.write(parsedEntity)
} else {
parser[buffer] += parseEntity(parser)
parser[buffer] += parsedEntity
parser.entity = ''
parser.state = returnState
}
Expand Down
17 changes: 17 additions & 0 deletions test/unparsed-entities-amp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require(__dirname).test({
opt: {unparsedEntities: true},
xml: '<svg>' +
'<text>' +
'&amp;' +
'</text>' +
'</svg>',
expect: [
['opentagstart', {'name': 'SVG', attributes: {}}],
['opentag', {'name': 'SVG', attributes: {}, isSelfClosing: false}],
['opentagstart', {'name': 'TEXT', attributes: {}}],
['opentag', {'name': 'TEXT', attributes: {}, isSelfClosing: false}],
['text', '&'],
['closetag', 'TEXT'],
['closetag', 'SVG']
]
})
32 changes: 32 additions & 0 deletions test/unparsed-entities-predefind-in-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var sax = require('../lib/sax');
sax.ENTITIES.entity_reference = "<text>entity reference</text>";
sax.ENTITIES.escaped_entity_reference = "&lt;text&gt;escaped entity reference&lt;/text&gt;";
require(__dirname).test({
opt: {unparsedEntities: true},
xml: '<svg>' +
'<text>' +
'&lt;text&gt;escaped literal&lt;/text&gt;' +
'</text>' +
'&entity_reference;' +
'<text>' +
'&escaped_entity_reference;' +
'</text>' +
'</svg>',
expect: [
['opentagstart', {'name': 'SVG', attributes: {}}],
['opentag', {'name': 'SVG', attributes: {}, isSelfClosing: false}],
['opentagstart', {'name': 'TEXT', attributes: {}}],
['opentag', {'name': 'TEXT', attributes: {}, isSelfClosing: false}],
['text', '<text>escaped literal</text>'],
['closetag', 'TEXT'],
['opentagstart', {'name': 'TEXT', attributes: {}}],
['opentag', {'name': 'TEXT', attributes: {}, isSelfClosing: false}],
['text', 'entity reference'],
['closetag', 'TEXT'],
['opentagstart', {'name': 'TEXT', attributes: {}}],
['opentag', {'name': 'TEXT', attributes: {}, isSelfClosing: false}],
['text', '<text>escaped entity reference</text>'],
['closetag', 'TEXT'],
['closetag', 'SVG']
]
})
11 changes: 11 additions & 0 deletions test/unparsed-entities-quote-in-attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require(__dirname).test({
opt: {unparsedEntities: true},
xml: '<doc a="&#34;">' +
'</doc>',
expect: [
['opentagstart', {'name': 'DOC', attributes: {}}],
['attribute', { name: 'A', value: '"'} ],
['opentag', {'name': 'DOC', attributes: {A: '"'}, isSelfClosing: false}],
['closetag', 'DOC']
]
})

0 comments on commit 93a63dd

Please sign in to comment.