mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-03 04:47:21 +02:00
Merge pull request #21682 from calixteman/fix/xml-invalid-char-ref
Don't throw on an invalid XML character reference
This commit is contained in:
commit
5dc1d0c5d5
@ -53,12 +53,12 @@ class XMLParserBase {
|
||||
}
|
||||
|
||||
_resolveEntities(s) {
|
||||
return s.replaceAll(XMLParserBase._entityRegex, (_, hex, dec, entity) => {
|
||||
if (hex) {
|
||||
return String.fromCodePoint(parseInt(hex, 16));
|
||||
}
|
||||
if (dec) {
|
||||
return String.fromCodePoint(parseInt(dec, 10));
|
||||
return s.replaceAll(XMLParserBase._entityRegex, (all, hex, dec, entity) => {
|
||||
if (hex || dec) {
|
||||
const code = hex ? parseInt(hex, 16) : parseInt(dec, 10);
|
||||
// An out-of-range or unparsable code point is kept as-is, since
|
||||
// `String.fromCodePoint` would throw on it.
|
||||
return code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : all;
|
||||
}
|
||||
switch (entity) {
|
||||
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 () {
|
||||
const xml = `
|
||||
<a>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user