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'] + ] +})