mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-04 05:17:24 +02:00
Exclude "&" from the XML entity names
Scanning to the end of the string for every "&" made the entity resolution quadratic. Stopping at the next "&" also fixes a bare ampersand swallowing the reference which follows it.
This commit is contained in:
parent
d0779c411e
commit
f7f30dd844
@ -49,7 +49,8 @@ function isWhitespaceString(s) {
|
|||||||
|
|
||||||
class XMLParserBase {
|
class XMLParserBase {
|
||||||
static get _entityRegex() {
|
static get _entityRegex() {
|
||||||
return shadow(this, "_entityRegex", /&(?:#x([^;]+)|#([^;]+)|([^;]+));/g);
|
// Entity references cannot contain "&", keeping the scan linear.
|
||||||
|
return shadow(this, "_entityRegex", /&(?:#x([^;&]+)|#([^;&]+)|([^;&]+));/g);
|
||||||
}
|
}
|
||||||
|
|
||||||
_resolveEntities(s) {
|
_resolveEntities(s) {
|
||||||
|
|||||||
@ -153,4 +153,33 @@ describe("XML", function () {
|
|||||||
["foo", ""],
|
["foo", ""],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("entities", function () {
|
||||||
|
const parseText = xml =>
|
||||||
|
new SimpleXMLParser({}).parseFromString(xml).documentElement.textContent;
|
||||||
|
|
||||||
|
it("should resolve the entities", function () {
|
||||||
|
expect(
|
||||||
|
parseText("<a><b> & "c" 'd'</a>")
|
||||||
|
).toEqual(`<b> & "c" 'd'`);
|
||||||
|
expect(parseText("<a>AB</a>")).toEqual("AB");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should keep the unresolved entities as-is", function () {
|
||||||
|
expect(parseText("<a>&unknown; a&b;c</a>")).toEqual("&unknown; a&b;c");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should resolve an entity preceded by a bare ampersand", function () {
|
||||||
|
expect(parseText("<a>AT&T & Co</a>")).toEqual("AT&T & Co");
|
||||||
|
expect(parseText("<a>&&</a>")).toEqual("&&");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle a long run of ampersands efficiently", function () {
|
||||||
|
const text = "&".repeat(100000);
|
||||||
|
|
||||||
|
const startTime = performance.now();
|
||||||
|
expect(parseText(`<a>${text}</a>`)).toEqual(text);
|
||||||
|
expect(performance.now() - startTime).toBeLessThan(1000);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user