From 93a63ddee88a541b0246ccb733a26dab3440a4fc Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Tue, 26 Dec 2023 22:20:46 +0000 Subject: [PATCH] fix: ignore unparsed entity logic for std xml entities PR-URL: https://github.com/isaacs/sax-js/pull/266 Credit: @SethFalco Close: #266 Reviewed-by: @HeikoTheissen --- lib/sax.js | 6 ++-- test/unparsed-entities-amp.js | 17 ++++++++++ test/unparsed-entities-predefind-in-custom.js | 32 +++++++++++++++++++ test/unparsed-entities-quote-in-attr.js | 11 +++++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/unparsed-entities-amp.js create mode 100644 test/unparsed-entities-predefind-in-custom.js create mode 100644 test/unparsed-entities-quote-in-attr.js diff --git a/lib/sax.js b/lib/sax.js index ffd441a..51e8d1a 100644 --- a/lib/sax.js +++ b/lib/sax.js @@ -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 } diff --git a/test/unparsed-entities-amp.js b/test/unparsed-entities-amp.js new file mode 100644 index 0000000..6e06e24 --- /dev/null +++ b/test/unparsed-entities-amp.js @@ -0,0 +1,17 @@ +require(__dirname).test({ + opt: {unparsedEntities: true}, + xml: '' + + '' + + '&' + + '' + + '', + 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'] + ] +}) diff --git a/test/unparsed-entities-predefind-in-custom.js b/test/unparsed-entities-predefind-in-custom.js new file mode 100644 index 0000000..6f9fd27 --- /dev/null +++ b/test/unparsed-entities-predefind-in-custom.js @@ -0,0 +1,32 @@ +var sax = require('../lib/sax'); +sax.ENTITIES.entity_reference = "entity reference"; +sax.ENTITIES.escaped_entity_reference = "<text>escaped entity reference</text>"; +require(__dirname).test({ + opt: {unparsedEntities: true}, + xml: '' + + '' + + '<text>escaped literal</text>' + + '' + + '&entity_reference;' + + '' + + '&escaped_entity_reference;' + + '' + + '', + expect: [ + ['opentagstart', {'name': 'SVG', attributes: {}}], + ['opentag', {'name': 'SVG', attributes: {}, isSelfClosing: false}], + ['opentagstart', {'name': 'TEXT', attributes: {}}], + ['opentag', {'name': 'TEXT', attributes: {}, isSelfClosing: false}], + ['text', 'escaped literal'], + ['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', 'escaped entity reference'], + ['closetag', 'TEXT'], + ['closetag', 'SVG'] + ] +}) diff --git a/test/unparsed-entities-quote-in-attr.js b/test/unparsed-entities-quote-in-attr.js new file mode 100644 index 0000000..714155b --- /dev/null +++ b/test/unparsed-entities-quote-in-attr.js @@ -0,0 +1,11 @@ +require(__dirname).test({ + opt: {unparsedEntities: true}, + xml: '' + + '', + expect: [ + ['opentagstart', {'name': 'DOC', attributes: {}}], + ['attribute', { name: 'A', value: '"'} ], + ['opentag', {'name': 'DOC', attributes: {A: '"'}, isSelfClosing: false}], + ['closetag', 'DOC'] + ] +})