mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-03 04:47:21 +02:00
Don't throw on an invalid XML character reference
`String.fromCodePoint` throws on anything which isn't a code point, so e.g. "&#xZZ;" or "�" aborted the whole parsing. Such a reference is now kept as-is, like an unknown named entity.
This commit is contained in:
parent
7fc7072f9c
commit
cec8d2eed6
@ -53,12 +53,12 @@ class XMLParserBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_resolveEntities(s) {
|
_resolveEntities(s) {
|
||||||
return s.replaceAll(XMLParserBase._entityRegex, (_, hex, dec, entity) => {
|
return s.replaceAll(XMLParserBase._entityRegex, (all, hex, dec, entity) => {
|
||||||
if (hex) {
|
if (hex || dec) {
|
||||||
return String.fromCodePoint(parseInt(hex, 16));
|
const code = hex ? parseInt(hex, 16) : parseInt(dec, 10);
|
||||||
}
|
// An out-of-range or unparsable code point is kept as-is, since
|
||||||
if (dec) {
|
// `String.fromCodePoint` would throw on it.
|
||||||
return String.fromCodePoint(parseInt(dec, 10));
|
return code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : all;
|
||||||
}
|
}
|
||||||
switch (entity) {
|
switch (entity) {
|
||||||
case "lt":
|
case "lt":
|
||||||
|
|||||||
@ -109,6 +109,27 @@ describe("XML", function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("character references", function () {
|
||||||
|
const parseText = xml =>
|
||||||
|
new SimpleXMLParser({}).parseFromString(xml).documentElement.textContent;
|
||||||
|
|
||||||
|
it("should resolve the valid ones", function () {
|
||||||
|
expect(parseText("<a>AB😂�</a>")).toEqual(
|
||||||
|
"AB\u{1F602}\0\u{10FFFF}"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should keep the invalid ones as-is", function () {
|
||||||
|
// These must not throw: `String.fromCodePoint` rejects anything which
|
||||||
|
// isn't a code point.
|
||||||
|
expect(parseText("<a>&#xZZ;</a>")).toEqual("&#xZZ;");
|
||||||
|
expect(parseText("<a>&#zz;</a>")).toEqual("&#zz;");
|
||||||
|
expect(parseText("<a>�</a>")).toEqual("�");
|
||||||
|
expect(parseText("<a>�</a>")).toEqual("�");
|
||||||
|
expect(parseText("<a>&#-1;</a>")).toEqual("&#-1;");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("should parse processing instructions", function () {
|
it("should parse processing instructions", function () {
|
||||||
const xml = `
|
const xml = `
|
||||||
<a>
|
<a>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user