UNPKG

4.82 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2019 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.Metadata = void 0;
28
29var _util = require("../shared/util");
30
31var _xml_parser = require("./xml_parser");
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
36
37function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
38
39var Metadata =
40/*#__PURE__*/
41function () {
42 function Metadata(data) {
43 _classCallCheck(this, Metadata);
44
45 (0, _util.assert)(typeof data === 'string', 'Metadata: input is not a string');
46 data = this._repair(data);
47 var parser = new _xml_parser.SimpleXMLParser();
48 var xmlDocument = parser.parseFromString(data);
49 this._metadata = Object.create(null);
50
51 if (xmlDocument) {
52 this._parse(xmlDocument);
53 }
54 }
55
56 _createClass(Metadata, [{
57 key: "_repair",
58 value: function _repair(data) {
59 return data.replace(/^([^<]+)/, '').replace(/>\\376\\377([^<]+)/g, function (all, codes) {
60 var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
61 return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
62 }).replace(/&(amp|apos|gt|lt|quot);/g, function (str, name) {
63 switch (name) {
64 case 'amp':
65 return '&';
66
67 case 'apos':
68 return '\'';
69
70 case 'gt':
71 return '>';
72
73 case 'lt':
74 return '<';
75
76 case 'quot':
77 return '\"';
78 }
79
80 throw new Error("_repair: ".concat(name, " isn't defined."));
81 });
82 var chars = '';
83
84 for (var i = 0, ii = bytes.length; i < ii; i += 2) {
85 var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
86
87 if (code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38) {
88 chars += String.fromCharCode(code);
89 } else {
90 chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
91 }
92 }
93
94 return '>' + chars;
95 });
96 }
97 }, {
98 key: "_parse",
99 value: function _parse(xmlDocument) {
100 var rdf = xmlDocument.documentElement;
101
102 if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
103 rdf = rdf.firstChild;
104
105 while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
106 rdf = rdf.nextSibling;
107 }
108 }
109
110 var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
111
112 if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
113 return;
114 }
115
116 var children = rdf.childNodes;
117
118 for (var i = 0, ii = children.length; i < ii; i++) {
119 var desc = children[i];
120
121 if (desc.nodeName.toLowerCase() !== 'rdf:description') {
122 continue;
123 }
124
125 for (var j = 0, jj = desc.childNodes.length; j < jj; j++) {
126 if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
127 var entry = desc.childNodes[j];
128 var name = entry.nodeName.toLowerCase();
129 this._metadata[name] = entry.textContent.trim();
130 }
131 }
132 }
133 }
134 }, {
135 key: "get",
136 value: function get(name) {
137 var data = this._metadata[name];
138 return typeof data !== 'undefined' ? data : null;
139 }
140 }, {
141 key: "getAll",
142 value: function getAll() {
143 return this._metadata;
144 }
145 }, {
146 key: "has",
147 value: function has(name) {
148 return typeof this._metadata[name] !== 'undefined';
149 }
150 }]);
151
152 return Metadata;
153}();
154
155exports.Metadata = Metadata;
\No newline at end of file