Skip to content

Commit 719ecb1

Browse files
devongovettTrySound
authored andcommitted
Parse result of custom entity replacement
1 parent 7311a50 commit 719ecb1

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/sax.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,15 @@
13761376
}
13771377

13781378
if (c === ';') {
1379-
parser[buffer] += parseEntity(parser)
1379+
var parsedEntity = parseEntity(parser)
1380+
1381+
// Custom entities can contain tags, so we potentially need to parse the result
1382+
if (parser.state === S.TEXT_ENTITY && !sax.ENTITIES[parser.entity] && parsedEntity !== '&' + parser.entity + ';') {
1383+
chunk = chunk.slice(0, i) + parsedEntity + chunk.slice(i)
1384+
} else {
1385+
parser[buffer] += parsedEntity
1386+
}
1387+
13801388
parser.entity = ''
13811389
parser.state = returnState
13821390
} else if (isMatch(parser.entity.length ? entityBody : entityStart, c)) {

test/entity-tags.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require(__dirname).test({
2+
xml: 'testing &custom; hi &unknown;',
3+
entities: {
4+
custom: '<foo bar="baz">hi</foo>'
5+
},
6+
expect: [
7+
['text', 'testing '],
8+
['opentagstart', {'name': 'FOO', attributes: {}}],
9+
['attribute', {name: 'BAR', value: 'baz'}],
10+
['opentag', {'name': 'FOO', attributes: {BAR: 'baz'}, isSelfClosing: false}],
11+
['text', 'hi'],
12+
['closetag', 'FOO'],
13+
['text', ' hi &unknown;']
14+
]
15+
})

test/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ exports.sax = sax
1010
exports.test = function test (options) {
1111
var xml = options.xml
1212
var parser = sax.parser(options.strict, options.opt)
13+
if (options.entities) {
14+
for (var key in options.entities) {
15+
parser.ENTITIES[key] = options.entities[key]
16+
}
17+
}
1318
var expect = options.expect
1419
var e = 0
1520
sax.EVENTS.forEach(function (ev) {

0 commit comments

Comments
 (0)