mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Move the encodeToXmlString
helper function to src/core/core_utils.js
With the previous patch this function is now *only* accessed on the worker-thread, hence it's no longer necessary to include it in the *built* `pdf.js` file.
This commit is contained in:
parent
b66f294f64
commit
d366bbdf51
5 changed files with 62 additions and 64 deletions
|
@ -316,8 +316,54 @@ function collectActions(xref, dict, eventType) {
|
|||
return objectSize(actions) > 0 ? actions : null;
|
||||
}
|
||||
|
||||
const XMLEntities = {
|
||||
/* < */ 0x3c: "<",
|
||||
/* > */ 0x3e: ">",
|
||||
/* & */ 0x26: "&",
|
||||
/* " */ 0x22: """,
|
||||
/* ' */ 0x27: "'",
|
||||
};
|
||||
|
||||
function encodeToXmlString(str) {
|
||||
const buffer = [];
|
||||
let start = 0;
|
||||
for (let i = 0, ii = str.length; i < ii; i++) {
|
||||
const char = str.codePointAt(i);
|
||||
if (0x20 <= char && char <= 0x7e) {
|
||||
// ascii
|
||||
const entity = XMLEntities[char];
|
||||
if (entity) {
|
||||
if (start < i) {
|
||||
buffer.push(str.substring(start, i));
|
||||
}
|
||||
buffer.push(entity);
|
||||
start = i + 1;
|
||||
}
|
||||
} else {
|
||||
if (start < i) {
|
||||
buffer.push(str.substring(start, i));
|
||||
}
|
||||
buffer.push(`&#x${char.toString(16).toUpperCase()};`);
|
||||
if (char > 0xd7ff && (char < 0xe000 || char > 0xfffd)) {
|
||||
// char is represented by two u16
|
||||
i++;
|
||||
}
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer.length === 0) {
|
||||
return str;
|
||||
}
|
||||
if (start < str.length) {
|
||||
buffer.push(str.substring(start, str.length));
|
||||
}
|
||||
return buffer.join("");
|
||||
}
|
||||
|
||||
export {
|
||||
collectActions,
|
||||
encodeToXmlString,
|
||||
escapePDFName,
|
||||
getArrayLookupTableFactory,
|
||||
getInheritableProperty,
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
// The code for XMLParserBase copied from
|
||||
// https://github.com/mozilla/shumway/blob/16451d8836fa85f4b16eeda8b4bda2fa9e2b22b0/src/avm2/natives/xml.ts
|
||||
|
||||
import { encodeToXmlString } from "../shared/util.js";
|
||||
import { encodeToXmlString } from "./core_utils.js";
|
||||
|
||||
const XMLParserErrorCode = {
|
||||
NoError: 0,
|
||||
|
|
|
@ -967,53 +967,6 @@ const createObjectURL = (function createObjectURLClosure() {
|
|||
};
|
||||
})();
|
||||
|
||||
const XMLEntities = {
|
||||
/* < */ 0x3c: "<",
|
||||
/* > */ 0x3e: ">",
|
||||
/* & */ 0x26: "&",
|
||||
/* " */ 0x22: """,
|
||||
/* ' */ 0x27: "'",
|
||||
};
|
||||
|
||||
function encodeToXmlString(str) {
|
||||
const buffer = [];
|
||||
let start = 0;
|
||||
for (let i = 0, ii = str.length; i < ii; i++) {
|
||||
const char = str.codePointAt(i);
|
||||
if (0x20 <= char && char <= 0x7e) {
|
||||
// ascii
|
||||
const entity = XMLEntities[char];
|
||||
if (entity) {
|
||||
if (start < i) {
|
||||
buffer.push(str.substring(start, i));
|
||||
}
|
||||
buffer.push(entity);
|
||||
start = i + 1;
|
||||
}
|
||||
} else {
|
||||
if (start < i) {
|
||||
buffer.push(str.substring(start, i));
|
||||
}
|
||||
buffer.push(`&#x${char.toString(16).toUpperCase()};`);
|
||||
if (char > 0xd7ff && (char < 0xe000 || char > 0xfffd)) {
|
||||
// char is represented by two u16
|
||||
i++;
|
||||
}
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer.length === 0) {
|
||||
return str;
|
||||
}
|
||||
|
||||
if (start < str.length) {
|
||||
buffer.push(str.substring(start, str.length));
|
||||
}
|
||||
|
||||
return buffer.join("");
|
||||
}
|
||||
|
||||
export {
|
||||
AbortException,
|
||||
AnnotationActionEventType,
|
||||
|
@ -1035,7 +988,6 @@ export {
|
|||
createPromiseCapability,
|
||||
createValidAbsoluteUrl,
|
||||
DocumentActionEventType,
|
||||
encodeToXmlString,
|
||||
escapeString,
|
||||
FONT_IDENTITY_MATRIX,
|
||||
FontType,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue