Browse Source

Convert `src/display/metadata.js` to ES6 syntax

Tim van der Meij 8 years ago
parent
commit
bc9afdf3c4
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
  1. 98
      src/display/metadata.js

98
src/display/metadata.js

@ -13,43 +13,48 @@
* limitations under the License. * limitations under the License.
*/ */
function fixMetadata(meta) { class Metadata {
return meta.replace(/>\\376\\377([^<]+)/g, function(all, codes) { constructor(data) {
var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, if (typeof data === 'string') {
function(code, d1, d2, d3) { // Ghostscript may produce invalid metadata, so try to repair that first.
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1); data = this._repair(data);
});
var chars = ''; // Convert the string to a DOM `Document`.
for (var i = 0; i < bytes.length; i += 2) { let parser = new DOMParser();
var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1); data = parser.parseFromString(data, 'application/xml');
chars += (code >= 32 && code < 127 && code !== 60 && code !== 62 && } else if (!(data instanceof Document)) {
code !== 38) ? String.fromCharCode(code) : throw new Error('Metadata: input is not a string or `Document`');
'&#x' + (0x10000 + code).toString(16).substring(1) + ';';
} }
return '>' + chars;
}); this._metadata = Object.create(null);
this._parse(data);
} }
function Metadata(meta) { _repair(data) {
if (typeof meta === 'string') { return data.replace(/>\\376\\377([^<]+)/g, function(all, codes) {
// Ghostscript produces invalid metadata let bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g,
meta = fixMetadata(meta); function(code, d1, d2, d3) {
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
});
var parser = new DOMParser(); let chars = '';
meta = parser.parseFromString(meta, 'application/xml'); for (let i = 0, ii = bytes.length; i < ii; i += 2) {
} else if (!(meta instanceof Document)) { let code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
throw new Error('Metadata: Invalid metadata object'); if (code >= 32 && code < 127 && code !== 60 && code !== 62 &&
code !== 38) {
chars += String.fromCharCode(code);
} else {
chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
}
} }
this.metaDocument = meta; return '>' + chars;
this.metadata = Object.create(null); });
this.parse();
} }
Metadata.prototype = { _parse(domDocument) {
parse: function Metadata_parse() { let rdf = domDocument.documentElement;
var doc = this.metaDocument;
var rdf = doc.documentElement;
if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') { // Wrapped in <xmpmeta> if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') { // Wrapped in <xmpmeta>
rdf = rdf.firstChild; rdf = rdf.firstChild;
@ -58,36 +63,37 @@ Metadata.prototype = {
} }
} }
var nodeName = (rdf) ? rdf.nodeName.toLowerCase() : null; let nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) { if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
return; return;
} }
var children = rdf.childNodes, desc, entry, name, i, ii, length, iLength; let children = rdf.childNodes;
for (i = 0, length = children.length; i < length; i++) { for (let i = 0, ii = children.length; i < ii; i++) {
desc = children[i]; let desc = children[i];
if (desc.nodeName.toLowerCase() !== 'rdf:description') { if (desc.nodeName.toLowerCase() !== 'rdf:description') {
continue; continue;
} }
for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) { for (let j = 0, jj = desc.childNodes.length; j < jj; j++) {
if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') { if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
entry = desc.childNodes[ii]; let entry = desc.childNodes[j];
name = entry.nodeName.toLowerCase(); let name = entry.nodeName.toLowerCase();
this.metadata[name] = entry.textContent.trim();
this._metadata[name] = entry.textContent.trim();
}
} }
} }
} }
},
get: function Metadata_get(name) { get(name) {
return this.metadata[name] || null; return this._metadata[name] || null;
}, }
has: function Metadata_has(name) { has(name) {
return typeof this.metadata[name] !== 'undefined'; return typeof this._metadata[name] !== 'undefined';
}, }
}; }
export { export {
Metadata, Metadata,

Loading…
Cancel
Save