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

84 строки
3.5 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 Node_1 = __importDefault(require("../nodes/node/Node"));
  7. const VoidElements_1 = __importDefault(require("../config/VoidElements"));
  8. const he_1 = require("he");
  9. /**
  10. * Utility for converting an element to string.
  11. */
  12. class XMLSerializer {
  13. /**
  14. * Renders an element as HTML.
  15. *
  16. * @param root Root element.
  17. * @param [options] Options.
  18. * @param [options.includeShadowRoots] Set to "true" to include shadow roots.
  19. * @returns Result.
  20. */
  21. serializeToString(root, options) {
  22. switch (root.nodeType) {
  23. case Node_1.default.ELEMENT_NODE:
  24. const element = root;
  25. const tagName = element.tagName.toLowerCase();
  26. if (VoidElements_1.default.includes(tagName)) {
  27. return `<${tagName}${this._getAttributes(element)}>`;
  28. }
  29. const childNodes = element.tagName === 'TEMPLATE'
  30. ? root.content.childNodes
  31. : root.childNodes;
  32. let innerHTML = '';
  33. for (const node of childNodes) {
  34. innerHTML += this.serializeToString(node, options);
  35. }
  36. if (options?.includeShadowRoots && element.shadowRoot) {
  37. innerHTML += `<template shadowroot="${element.shadowRoot.mode}">`;
  38. for (const node of element.shadowRoot.childNodes) {
  39. innerHTML += this.serializeToString(node, options);
  40. }
  41. innerHTML += '</template>';
  42. }
  43. return `<${tagName}${this._getAttributes(element)}>${innerHTML}</${tagName}>`;
  44. case Node_1.default.DOCUMENT_FRAGMENT_NODE:
  45. case Node_1.default.DOCUMENT_NODE:
  46. let html = '';
  47. for (const node of root.childNodes) {
  48. html += this.serializeToString(node, options);
  49. }
  50. return html;
  51. case Node_1.default.COMMENT_NODE:
  52. return `<!--${root.textContent}-->`;
  53. case Node_1.default.TEXT_NODE:
  54. return root['textContent'];
  55. case Node_1.default.DOCUMENT_TYPE_NODE:
  56. const doctype = root;
  57. const identifier = doctype.publicId ? ' PUBLIC' : doctype.systemId ? ' SYSTEM' : '';
  58. const publicId = doctype.publicId ? ` "${doctype.publicId}"` : '';
  59. const systemId = doctype.systemId ? ` "${doctype.systemId}"` : '';
  60. return `<!DOCTYPE ${doctype.name}${identifier}${publicId}${systemId}>`;
  61. }
  62. return '';
  63. }
  64. /**
  65. * Returns attributes as a string.
  66. *
  67. * @param element Element.
  68. * @returns Attributes.
  69. */
  70. _getAttributes(element) {
  71. let attributeString = '';
  72. if (!element._attributes.is && element._isValue) {
  73. attributeString += ' is="' + (0, he_1.escape)(element._isValue) + '"';
  74. }
  75. for (const attribute of Object.values(element._attributes)) {
  76. if (attribute.value !== null) {
  77. attributeString += ' ' + attribute.name + '="' + (0, he_1.escape)(attribute.value) + '"';
  78. }
  79. }
  80. return attributeString;
  81. }
  82. }
  83. exports.default = XMLSerializer;
  84. //# sourceMappingURL=XMLSerializer.js.map