metadata.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. exports.Metadata = undefined;
  20. var _util = require('../shared/util');
  21. function fixMetadata(meta) {
  22. return meta.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
  23. var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
  24. return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
  25. });
  26. var chars = '';
  27. for (var i = 0; i < bytes.length; i += 2) {
  28. var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
  29. chars += code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38 ? String.fromCharCode(code) : '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
  30. }
  31. return '>' + chars;
  32. });
  33. }
  34. function Metadata(meta) {
  35. if (typeof meta === 'string') {
  36. meta = fixMetadata(meta);
  37. var parser = new DOMParser();
  38. meta = parser.parseFromString(meta, 'application/xml');
  39. } else if (!(meta instanceof Document)) {
  40. (0, _util.error)('Metadata: Invalid metadata object');
  41. }
  42. this.metaDocument = meta;
  43. this.metadata = Object.create(null);
  44. this.parse();
  45. }
  46. Metadata.prototype = {
  47. parse: function Metadata_parse() {
  48. var doc = this.metaDocument;
  49. var rdf = doc.documentElement;
  50. if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  51. rdf = rdf.firstChild;
  52. while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  53. rdf = rdf.nextSibling;
  54. }
  55. }
  56. var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
  57. if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
  58. return;
  59. }
  60. var children = rdf.childNodes,
  61. desc,
  62. entry,
  63. name,
  64. i,
  65. ii,
  66. length,
  67. iLength;
  68. for (i = 0, length = children.length; i < length; i++) {
  69. desc = children[i];
  70. if (desc.nodeName.toLowerCase() !== 'rdf:description') {
  71. continue;
  72. }
  73. for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) {
  74. if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') {
  75. entry = desc.childNodes[ii];
  76. name = entry.nodeName.toLowerCase();
  77. this.metadata[name] = entry.textContent.trim();
  78. }
  79. }
  80. }
  81. },
  82. get: function Metadata_get(name) {
  83. return this.metadata[name] || null;
  84. },
  85. has: function Metadata_has(name) {
  86. return typeof this.metadata[name] !== 'undefined';
  87. }
  88. };
  89. exports.Metadata = Metadata;