metadata.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. function fixMetadata(meta) {
  20. return meta.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
  21. var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
  22. return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
  23. });
  24. var chars = '';
  25. for (var i = 0; i < bytes.length; i += 2) {
  26. var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
  27. chars += code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38 ? String.fromCharCode(code) : '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
  28. }
  29. return '>' + chars;
  30. });
  31. }
  32. function Metadata(meta) {
  33. if (typeof meta === 'string') {
  34. meta = fixMetadata(meta);
  35. var parser = new DOMParser();
  36. meta = parser.parseFromString(meta, 'application/xml');
  37. } else if (!(meta instanceof Document)) {
  38. throw new Error('Metadata: Invalid metadata object');
  39. }
  40. this.metaDocument = meta;
  41. this.metadata = Object.create(null);
  42. this.parse();
  43. }
  44. Metadata.prototype = {
  45. parse: function Metadata_parse() {
  46. var doc = this.metaDocument;
  47. var rdf = doc.documentElement;
  48. if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  49. rdf = rdf.firstChild;
  50. while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  51. rdf = rdf.nextSibling;
  52. }
  53. }
  54. var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
  55. if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
  56. return;
  57. }
  58. var children = rdf.childNodes,
  59. desc,
  60. entry,
  61. name,
  62. i,
  63. ii,
  64. length,
  65. iLength;
  66. for (i = 0, length = children.length; i < length; i++) {
  67. desc = children[i];
  68. if (desc.nodeName.toLowerCase() !== 'rdf:description') {
  69. continue;
  70. }
  71. for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) {
  72. if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') {
  73. entry = desc.childNodes[ii];
  74. name = entry.nodeName.toLowerCase();
  75. this.metadata[name] = entry.textContent.trim();
  76. }
  77. }
  78. }
  79. },
  80. get: function Metadata_get(name) {
  81. return this.metadata[name] || null;
  82. },
  83. has: function Metadata_has(name) {
  84. return typeof this.metadata[name] !== 'undefined';
  85. }
  86. };
  87. exports.Metadata = Metadata;