2
0

metadata.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 _createClass = function () { function 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _util = require('../shared/util');
  22. var _dom_utils = require('./dom_utils');
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. var Metadata = function () {
  25. function Metadata(data) {
  26. _classCallCheck(this, Metadata);
  27. (0, _util.assert)(typeof data === 'string', 'Metadata: input is not a string');
  28. data = this._repair(data);
  29. var parser = new _dom_utils.SimpleXMLParser();
  30. data = parser.parseFromString(data);
  31. this._metadata = Object.create(null);
  32. this._parse(data);
  33. }
  34. _createClass(Metadata, [{
  35. key: '_repair',
  36. value: function _repair(data) {
  37. return data.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
  38. var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
  39. return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
  40. });
  41. var chars = '';
  42. for (var i = 0, ii = bytes.length; i < ii; i += 2) {
  43. var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
  44. if (code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38) {
  45. chars += String.fromCharCode(code);
  46. } else {
  47. chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
  48. }
  49. }
  50. return '>' + chars;
  51. });
  52. }
  53. }, {
  54. key: '_parse',
  55. value: function _parse(domDocument) {
  56. var rdf = domDocument.documentElement;
  57. if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  58. rdf = rdf.firstChild;
  59. while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  60. rdf = rdf.nextSibling;
  61. }
  62. }
  63. var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
  64. if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
  65. return;
  66. }
  67. var children = rdf.childNodes;
  68. for (var i = 0, ii = children.length; i < ii; i++) {
  69. var desc = children[i];
  70. if (desc.nodeName.toLowerCase() !== 'rdf:description') {
  71. continue;
  72. }
  73. for (var j = 0, jj = desc.childNodes.length; j < jj; j++) {
  74. if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
  75. var entry = desc.childNodes[j];
  76. var name = entry.nodeName.toLowerCase();
  77. this._metadata[name] = entry.textContent.trim();
  78. }
  79. }
  80. }
  81. }
  82. }, {
  83. key: 'get',
  84. value: function get(name) {
  85. return this._metadata[name] || null;
  86. }
  87. }, {
  88. key: 'getAll',
  89. value: function getAll() {
  90. return this._metadata;
  91. }
  92. }, {
  93. key: 'has',
  94. value: function has(name) {
  95. return typeof this._metadata[name] !== 'undefined';
  96. }
  97. }]);
  98. return Metadata;
  99. }();
  100. exports.Metadata = Metadata;