metadata.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.Metadata = undefined;
  27. 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; }; }();
  28. var _util = require('../shared/util');
  29. var _dom_utils = require('./dom_utils');
  30. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  31. var Metadata = function () {
  32. function Metadata(data) {
  33. _classCallCheck(this, Metadata);
  34. (0, _util.assert)(typeof data === 'string', 'Metadata: input is not a string');
  35. data = this._repair(data);
  36. var parser = new _dom_utils.SimpleXMLParser();
  37. data = parser.parseFromString(data);
  38. this._metadata = Object.create(null);
  39. this._parse(data);
  40. }
  41. _createClass(Metadata, [{
  42. key: '_repair',
  43. value: function _repair(data) {
  44. return data.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
  45. var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
  46. return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
  47. }).replace(/&(amp|apos|gt|lt|quot);/g, function (str, name) {
  48. switch (name) {
  49. case 'amp':
  50. return '&';
  51. case 'apos':
  52. return '\'';
  53. case 'gt':
  54. return '>';
  55. case 'lt':
  56. return '<';
  57. case 'quot':
  58. return '\"';
  59. }
  60. throw new Error('_repair: ' + name + ' isn\'t defined.');
  61. });
  62. var chars = '';
  63. for (var i = 0, ii = bytes.length; i < ii; i += 2) {
  64. var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
  65. if (code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38) {
  66. chars += String.fromCharCode(code);
  67. } else {
  68. chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
  69. }
  70. }
  71. return '>' + chars;
  72. });
  73. }
  74. }, {
  75. key: '_parse',
  76. value: function _parse(domDocument) {
  77. var rdf = domDocument.documentElement;
  78. if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  79. rdf = rdf.firstChild;
  80. while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  81. rdf = rdf.nextSibling;
  82. }
  83. }
  84. var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
  85. if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
  86. return;
  87. }
  88. var children = rdf.childNodes;
  89. for (var i = 0, ii = children.length; i < ii; i++) {
  90. var desc = children[i];
  91. if (desc.nodeName.toLowerCase() !== 'rdf:description') {
  92. continue;
  93. }
  94. for (var j = 0, jj = desc.childNodes.length; j < jj; j++) {
  95. if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
  96. var entry = desc.childNodes[j];
  97. var name = entry.nodeName.toLowerCase();
  98. this._metadata[name] = entry.textContent.trim();
  99. }
  100. }
  101. }
  102. }
  103. }, {
  104. key: 'get',
  105. value: function get(name) {
  106. return this._metadata[name] || null;
  107. }
  108. }, {
  109. key: 'getAll',
  110. value: function getAll() {
  111. return this._metadata;
  112. }
  113. }, {
  114. key: 'has',
  115. value: function has(name) {
  116. return typeof this._metadata[name] !== 'undefined';
  117. }
  118. }]);
  119. return Metadata;
  120. }();
  121. exports.Metadata = Metadata;