Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore unparsed entity logic for predefined xml entities #266

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']
]
})
Loading