Skip to content

Commit

Permalink
Parse result of custom entity replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored and TrySound committed Feb 23, 2021
1 parent 7311a50 commit 719ecb1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,15 @@
}

if (c === ';') {
parser[buffer] += parseEntity(parser)
var parsedEntity = parseEntity(parser)

// Custom entities can contain tags, so we potentially need to parse the result
if (parser.state === S.TEXT_ENTITY && !sax.ENTITIES[parser.entity] && parsedEntity !== '&' + parser.entity + ';') {
chunk = chunk.slice(0, i) + parsedEntity + chunk.slice(i)
} else {
parser[buffer] += parsedEntity
}

parser.entity = ''
parser.state = returnState
} else if (isMatch(parser.entity.length ? entityBody : entityStart, c)) {
Expand Down
15 changes: 15 additions & 0 deletions test/entity-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require(__dirname).test({
xml: 'testing &custom; hi &unknown;',
entities: {
custom: '<foo bar="baz">hi</foo>'
},
expect: [
['text', 'testing '],
['opentagstart', {'name': 'FOO', attributes: {}}],
['attribute', {name: 'BAR', value: 'baz'}],
['opentag', {'name': 'FOO', attributes: {BAR: 'baz'}, isSelfClosing: false}],
['text', 'hi'],
['closetag', 'FOO'],
['text', ' hi &unknown;']
]
})
5 changes: 5 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ exports.sax = sax
exports.test = function test (options) {
var xml = options.xml
var parser = sax.parser(options.strict, options.opt)
if (options.entities) {
for (var key in options.entities) {
parser.ENTITIES[key] = options.entities[key]
}
}
var expect = options.expect
var e = 0
sax.EVENTS.forEach(function (ev) {
Expand Down

0 comments on commit 719ecb1

Please sign in to comment.