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

Entity declarations with XML elements or other entities #260

Merged
merged 6 commits into from
Sep 26, 2023
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
14 changes: 11 additions & 3 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
} catch (ex) {
Stream = function () {}
}
if (!Stream) Stream = function () {}

var streamWraps = sax.EVENTS.filter(function (ev) {
return ev !== 'error' && ev !== 'end'
Expand Down Expand Up @@ -1482,9 +1483,16 @@
}

if (c === ';') {
parser[buffer] += parseEntity(parser)
parser.entity = ''
parser.state = returnState
if (parser.opt.unparsedEntities) {
var parsedEntity = parseEntity(parser)
parser.entity = ''
parser.state = returnState
parser.write(parsedEntity)
} else {
parser[buffer] += parseEntity(parser)
parser.entity = ''
parser.state = returnState
}
} else if (isMatch(parser.entity.length ? entityBody : entityStart, c)) {
parser.entity += c
} else {
Expand Down
12 changes: 12 additions & 0 deletions test/entity-attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var sax = require('../');
sax.ENTITIES.elem = '<B/>';
require(__dirname).test({
opt: {unparsedEntities: true},
xml: `<A ATTR="&elem;"/>`,
expect: [
["opentagstart", {name: "A", attributes: {}}],
["attribute", {name: 'ATTR', value: "<B/>"}],
["opentag", {name: "A", attributes: {ATTR: "<B/>"}, isSelfClosing: true}],
["closetag", "A"]
]
});
19 changes: 19 additions & 0 deletions test/entity-elem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var sax = require('../');
sax.ENTITIES.attr = "1";
sax.ENTITIES.text = "2.&attr;";
sax.ENTITIES.elem = '<B ATTR="&attr;.3"/>';
require(__dirname).test({
opt: {unparsedEntities: true},
xml: `<A ATTR="&attr;.2">&text;&elem;</A>`,
expect: [
["opentagstart", {name: "A", attributes: {}}],
["attribute", {name: 'ATTR', value: "1.2"}],
["opentag", {name: "A", attributes: {ATTR: "1.2"}, isSelfClosing: false}],
["text", "2.1"],
["opentagstart", {name: "B", attributes: {}}],
["attribute", {name: 'ATTR', value: "1.3"}],
["opentag", {name: "B", attributes: {ATTR: "1.3"}, isSelfClosing: true}],
["closetag", "B"],
["closetag", "A"]
]
});
13 changes: 13 additions & 0 deletions test/entity-recursive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var sax = require('../');
sax.ENTITIES.attr = "1";
sax.ENTITIES.text = "2.&attr;";
require(__dirname).test({
opt: {unparsedEntities: true},
xml: `<A>&text;</A>`,
expect: [
["opentagstart", {name: "A", attributes: {}}],
["opentag", {name: "A", attributes: {}, isSelfClosing: false}],
["text", "2.1"],
["closetag", "A"]
]
});
Loading