mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Merge pull request #8912 from timvandermeij/xml-parser
[api-minor] Replace `DOMParser` with `SimpleXMLParser`
This commit is contained in:
commit
d7b37ae745
7 changed files with 226 additions and 171 deletions
|
@ -131,6 +131,132 @@ class DOMSVGFactory {
|
|||
}
|
||||
}
|
||||
|
||||
class SimpleDOMNode {
|
||||
constructor(nodeName, nodeValue) {
|
||||
this.nodeName = nodeName;
|
||||
this.nodeValue = nodeValue;
|
||||
|
||||
Object.defineProperty(this, 'parentNode', { value: null, writable: true, });
|
||||
}
|
||||
|
||||
get firstChild() {
|
||||
return this.childNodes[0];
|
||||
}
|
||||
|
||||
get nextSibling() {
|
||||
let index = this.parentNode.childNodes.indexOf(this);
|
||||
return this.parentNode.childNodes[index + 1];
|
||||
}
|
||||
|
||||
get textContent() {
|
||||
if (!this.childNodes) {
|
||||
return this.nodeValue || '';
|
||||
}
|
||||
return this.childNodes.map(function(child) {
|
||||
return child.textContent;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
hasChildNodes() {
|
||||
return this.childNodes && this.childNodes.length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleXMLParser {
|
||||
parseFromString(data) {
|
||||
let nodes = [];
|
||||
|
||||
// Remove all comments and processing instructions.
|
||||
data = data.replace(/<\?[\s\S]*?\?>|<!--[\s\S]*?-->/g, '').trim();
|
||||
data = data.replace(/<!DOCTYPE[^>\[]+(\[[^\]]+)?[^>]+>/g, '').trim();
|
||||
|
||||
// Extract all text nodes and replace them with a numeric index in
|
||||
// the nodes.
|
||||
data = data.replace(/>([^<][\s\S]*?)</g, (all, text) => {
|
||||
let length = nodes.length;
|
||||
let node = new SimpleDOMNode('#text', this._decodeXML(text));
|
||||
nodes.push(node);
|
||||
if (node.textContent.trim().length === 0) {
|
||||
return '><'; // Ignore whitespace.
|
||||
}
|
||||
return '>' + length + ',<';
|
||||
});
|
||||
|
||||
// Extract all CDATA nodes.
|
||||
data = data.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g,
|
||||
function(all, text) {
|
||||
let length = nodes.length;
|
||||
let node = new SimpleDOMNode('#text', text);
|
||||
nodes.push(node);
|
||||
return length + ',';
|
||||
});
|
||||
|
||||
// Until nodes without '<' and '>' content are present, replace them
|
||||
// with a numeric index in the nodes.
|
||||
let regex =
|
||||
/<([\w\:]+)((?:[\s\w:=]|'[^']*'|"[^"]*")*)(?:\/>|>([\d,]*)<\/[^>]+>)/g;
|
||||
let lastLength;
|
||||
do {
|
||||
lastLength = nodes.length;
|
||||
data = data.replace(regex, function(all, name, attrs, data) {
|
||||
let length = nodes.length;
|
||||
let node = new SimpleDOMNode(name);
|
||||
let children = [];
|
||||
if (data) {
|
||||
data = data.split(',');
|
||||
data.pop();
|
||||
data.forEach(function(child) {
|
||||
let childNode = nodes[+child];
|
||||
childNode.parentNode = node;
|
||||
children.push(childNode);
|
||||
});
|
||||
}
|
||||
|
||||
node.childNodes = children;
|
||||
nodes.push(node);
|
||||
return length + ',';
|
||||
});
|
||||
} while (lastLength < nodes.length);
|
||||
|
||||
// We should only have one root index left, which will be last in the nodes.
|
||||
return {
|
||||
documentElement: nodes.pop(),
|
||||
};
|
||||
}
|
||||
|
||||
_decodeXML(text) {
|
||||
if (text.indexOf('&') < 0) {
|
||||
return text;
|
||||
}
|
||||
|
||||
return text.replace(/&(#(x[0-9a-f]+|\d+)|\w+);/gi,
|
||||
function(all, entityName, number) {
|
||||
if (number) {
|
||||
if (number[0] === 'x') {
|
||||
number = parseInt(number.substring(1), 16);
|
||||
} else {
|
||||
number = +number;
|
||||
}
|
||||
return String.fromCharCode(number);
|
||||
}
|
||||
|
||||
switch (entityName) {
|
||||
case 'amp':
|
||||
return '&';
|
||||
case 'lt':
|
||||
return '<';
|
||||
case 'gt':
|
||||
return '>';
|
||||
case 'quot':
|
||||
return '\"';
|
||||
case 'apos':
|
||||
return '\'';
|
||||
}
|
||||
return '&' + entityName + ';';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimised CSS custom property getter/setter.
|
||||
* @class
|
||||
|
@ -353,4 +479,5 @@ export {
|
|||
DOMCanvasFactory,
|
||||
DOMCMapReaderFactory,
|
||||
DOMSVGFactory,
|
||||
SimpleXMLParser,
|
||||
};
|
||||
|
|
|
@ -13,43 +13,49 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
function fixMetadata(meta) {
|
||||
return meta.replace(/>\\376\\377([^<]+)/g, function(all, codes) {
|
||||
var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g,
|
||||
function(code, d1, d2, d3) {
|
||||
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
|
||||
});
|
||||
var chars = '';
|
||||
for (var i = 0; i < bytes.length; i += 2) {
|
||||
var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
|
||||
chars += (code >= 32 && code < 127 && code !== 60 && code !== 62 &&
|
||||
code !== 38) ? String.fromCharCode(code) :
|
||||
'&#x' + (0x10000 + code).toString(16).substring(1) + ';';
|
||||
}
|
||||
return '>' + chars;
|
||||
});
|
||||
}
|
||||
import { assert, deprecated } from '../shared/util';
|
||||
import { SimpleXMLParser } from './dom_utils';
|
||||
|
||||
function Metadata(meta) {
|
||||
if (typeof meta === 'string') {
|
||||
// Ghostscript produces invalid metadata
|
||||
meta = fixMetadata(meta);
|
||||
class Metadata {
|
||||
constructor(data) {
|
||||
assert(typeof data === 'string', 'Metadata: input is not a string');
|
||||
|
||||
var parser = new DOMParser();
|
||||
meta = parser.parseFromString(meta, 'application/xml');
|
||||
} else if (!(meta instanceof Document)) {
|
||||
throw new Error('Metadata: Invalid metadata object');
|
||||
// Ghostscript may produce invalid metadata, so try to repair that first.
|
||||
data = this._repair(data);
|
||||
|
||||
// Convert the string to a DOM `Document`.
|
||||
let parser = new SimpleXMLParser();
|
||||
data = parser.parseFromString(data);
|
||||
|
||||
this._metadata = Object.create(null);
|
||||
|
||||
this._parse(data);
|
||||
}
|
||||
|
||||
this.metaDocument = meta;
|
||||
this.metadata = Object.create(null);
|
||||
this.parse();
|
||||
}
|
||||
_repair(data) {
|
||||
return data.replace(/>\\376\\377([^<]+)/g, function(all, codes) {
|
||||
let bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g,
|
||||
function(code, d1, d2, d3) {
|
||||
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
|
||||
});
|
||||
|
||||
Metadata.prototype = {
|
||||
parse: function Metadata_parse() {
|
||||
var doc = this.metaDocument;
|
||||
var rdf = doc.documentElement;
|
||||
let chars = '';
|
||||
for (let i = 0, ii = bytes.length; i < ii; i += 2) {
|
||||
let code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
|
||||
if (code >= 32 && code < 127 && code !== 60 && code !== 62 &&
|
||||
code !== 38) {
|
||||
chars += String.fromCharCode(code);
|
||||
} else {
|
||||
chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
|
||||
}
|
||||
}
|
||||
|
||||
return '>' + chars;
|
||||
});
|
||||
}
|
||||
|
||||
_parse(domDocument) {
|
||||
let rdf = domDocument.documentElement;
|
||||
|
||||
if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') { // Wrapped in <xmpmeta>
|
||||
rdf = rdf.firstChild;
|
||||
|
@ -58,36 +64,46 @@ Metadata.prototype = {
|
|||
}
|
||||
}
|
||||
|
||||
var nodeName = (rdf) ? rdf.nodeName.toLowerCase() : null;
|
||||
let nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
|
||||
if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var children = rdf.childNodes, desc, entry, name, i, ii, length, iLength;
|
||||
for (i = 0, length = children.length; i < length; i++) {
|
||||
desc = children[i];
|
||||
let children = rdf.childNodes;
|
||||
for (let i = 0, ii = children.length; i < ii; i++) {
|
||||
let desc = children[i];
|
||||
if (desc.nodeName.toLowerCase() !== 'rdf:description') {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) {
|
||||
if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') {
|
||||
entry = desc.childNodes[ii];
|
||||
name = entry.nodeName.toLowerCase();
|
||||
this.metadata[name] = entry.textContent.trim();
|
||||
for (let j = 0, jj = desc.childNodes.length; j < jj; j++) {
|
||||
if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
|
||||
let entry = desc.childNodes[j];
|
||||
let name = entry.nodeName.toLowerCase();
|
||||
|
||||
this._metadata[name] = entry.textContent.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
get: function Metadata_get(name) {
|
||||
return this.metadata[name] || null;
|
||||
},
|
||||
get(name) {
|
||||
return this._metadata[name] || null;
|
||||
}
|
||||
|
||||
has: function Metadata_has(name) {
|
||||
return typeof this.metadata[name] !== 'undefined';
|
||||
},
|
||||
};
|
||||
getAll() {
|
||||
return this._metadata;
|
||||
}
|
||||
|
||||
has(name) {
|
||||
return typeof this._metadata[name] !== 'undefined';
|
||||
}
|
||||
|
||||
get metadata() {
|
||||
deprecated('`metadata` getter; use `getAll()` instead.');
|
||||
return this.getAll();
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
Metadata,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue