版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

107 строки
4.0 KiB

  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const XMLParser_1 = __importDefault(require("../xml-parser/XMLParser"));
  7. const Node_1 = __importDefault(require("../nodes/node/Node"));
  8. const DOMException_1 = __importDefault(require("../exception/DOMException"));
  9. const HTMLDocument_1 = __importDefault(require("../nodes/html-document/HTMLDocument"));
  10. const XMLDocument_1 = __importDefault(require("../nodes/xml-document/XMLDocument"));
  11. const SVGDocument_1 = __importDefault(require("../nodes/svg-document/SVGDocument"));
  12. /**
  13. * DOM parser.
  14. *
  15. * Reference:
  16. * https://developer.mozilla.org/en-US/docs/Web/API/DOMParser.
  17. */
  18. class DOMParser {
  19. /**
  20. * Constructor.
  21. */
  22. constructor() {
  23. this._ownerDocument = null;
  24. this._ownerDocument = this.constructor._ownerDocument;
  25. }
  26. /**
  27. * Parses HTML and returns a root element.
  28. *
  29. * @param string HTML data.
  30. * @param mimeType Mime type.
  31. * @returns Root element.
  32. */
  33. parseFromString(string, mimeType) {
  34. if (!mimeType) {
  35. throw new DOMException_1.default('Second parameter "mimeType" is mandatory.');
  36. }
  37. const ownerDocument = this._ownerDocument;
  38. const newDocument = this._createDocument(mimeType);
  39. newDocument.defaultView = ownerDocument.defaultView;
  40. newDocument.childNodes.length = 0;
  41. newDocument.children.length = 0;
  42. const root = XMLParser_1.default.parse(newDocument, string, true);
  43. let documentElement = null;
  44. let documentTypeNode = null;
  45. for (const node of root.childNodes) {
  46. if (node['tagName'] === 'HTML') {
  47. documentElement = node;
  48. }
  49. else if (node.nodeType === Node_1.default.DOCUMENT_TYPE_NODE) {
  50. documentTypeNode = node;
  51. }
  52. if (documentElement && documentTypeNode) {
  53. break;
  54. }
  55. }
  56. if (documentElement) {
  57. if (documentTypeNode) {
  58. newDocument.appendChild(documentTypeNode);
  59. }
  60. newDocument.appendChild(documentElement);
  61. const body = newDocument.querySelector('body');
  62. if (body) {
  63. for (const child of root.childNodes.slice()) {
  64. body.appendChild(child);
  65. }
  66. }
  67. }
  68. else {
  69. const documentElement = newDocument.createElement('html');
  70. const bodyElement = newDocument.createElement('body');
  71. const headElement = newDocument.createElement('head');
  72. documentElement.appendChild(headElement);
  73. documentElement.appendChild(bodyElement);
  74. newDocument.appendChild(documentElement);
  75. for (const node of root.childNodes.slice()) {
  76. bodyElement.appendChild(node);
  77. }
  78. }
  79. return newDocument;
  80. }
  81. /**
  82. *
  83. * @param mimeType Mime type.
  84. * @returns IDocument.
  85. */
  86. _createDocument(mimeType) {
  87. switch (mimeType) {
  88. case 'text/html':
  89. HTMLDocument_1.default._defaultView = this._ownerDocument.defaultView;
  90. return new HTMLDocument_1.default();
  91. case 'image/svg+xml':
  92. SVGDocument_1.default._defaultView = this._ownerDocument.defaultView;
  93. return new SVGDocument_1.default();
  94. case 'text/xml':
  95. case 'application/xml':
  96. case 'application/xhtml+xml':
  97. XMLDocument_1.default._defaultView = this._ownerDocument.defaultView;
  98. return new XMLDocument_1.default();
  99. default:
  100. throw new DOMException_1.default(`Unknown mime type "${mimeType}".`);
  101. }
  102. }
  103. }
  104. exports.default = DOMParser;
  105. // Owner document is set by a sub-class in the Window constructor
  106. DOMParser._ownerDocument = null;
  107. //# sourceMappingURL=DOMParser.js.map