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

159 строки
5.6 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 QuerySelector_1 = __importDefault(require("../../query-selector/QuerySelector"));
  7. const XMLParser_1 = __importDefault(require("../../xml-parser/XMLParser"));
  8. const HTMLCollectionFactory_1 = __importDefault(require("../element/HTMLCollectionFactory"));
  9. /**
  10. * Parent node utility.
  11. */
  12. class ParentNodeUtility {
  13. /**
  14. * Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
  15. *
  16. * @param parentNode Parent node.
  17. * @param nodes List of Node or DOMString.
  18. */
  19. static append(parentNode, ...nodes) {
  20. for (const node of nodes) {
  21. if (typeof node === 'string') {
  22. const newChildNodes = XMLParser_1.default.parse(parentNode.ownerDocument, node).childNodes.slice();
  23. for (const newChildNode of newChildNodes) {
  24. parentNode.appendChild(newChildNode);
  25. }
  26. }
  27. else {
  28. parentNode.appendChild(node);
  29. }
  30. }
  31. }
  32. /**
  33. * Inserts a set of Node objects or DOMString objects before the first child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
  34. *
  35. * @param parentNode Parent node.
  36. * @param nodes List of Node or DOMString.
  37. */
  38. static prepend(parentNode, ...nodes) {
  39. const firstChild = parentNode.firstChild;
  40. for (const node of nodes) {
  41. if (typeof node === 'string') {
  42. const newChildNodes = XMLParser_1.default.parse(parentNode.ownerDocument, node).childNodes.slice();
  43. for (const newChildNode of newChildNodes) {
  44. parentNode.insertBefore(newChildNode, firstChild);
  45. }
  46. }
  47. else {
  48. parentNode.insertBefore(node, firstChild);
  49. }
  50. }
  51. }
  52. /**
  53. * Replaces the existing children of a ParentNode with a specified new set of children.
  54. *
  55. * @param parentNode Parent node.
  56. * @param nodes List of Node or DOMString.
  57. */
  58. static replaceChildren(parentNode, ...nodes) {
  59. for (const node of parentNode.childNodes.slice()) {
  60. parentNode.removeChild(node);
  61. }
  62. this.append(parentNode, ...nodes);
  63. }
  64. /**
  65. * Returns an elements by class name.
  66. *
  67. * @param parentNode Parent node.
  68. * @param className Tag name.
  69. * @returns Matching element.
  70. */
  71. static getElementsByClassName(parentNode, className) {
  72. return QuerySelector_1.default.querySelectorAll(parentNode, '.' + className.split(' ').join('.'));
  73. }
  74. /**
  75. * Returns an elements by tag name.
  76. *
  77. * @param parentNode Parent node.
  78. * @param tagName Tag name.
  79. * @returns Matching element.
  80. */
  81. static getElementsByTagName(parentNode, tagName) {
  82. const upperTagName = tagName.toUpperCase();
  83. const matches = HTMLCollectionFactory_1.default.create();
  84. const includeAll = tagName === '*';
  85. for (const child of parentNode.children) {
  86. if (includeAll || child.tagName === upperTagName) {
  87. matches.push(child);
  88. }
  89. for (const match of this.getElementsByTagName(child, tagName)) {
  90. matches.push(match);
  91. }
  92. }
  93. return matches;
  94. }
  95. /**
  96. * Returns an elements by tag name and namespace.
  97. *
  98. * @param parentNode Parent node.
  99. * @param namespaceURI Namespace URI.
  100. * @param tagName Tag name.
  101. * @returns Matching element.
  102. */
  103. static getElementsByTagNameNS(parentNode, namespaceURI, tagName) {
  104. const upperTagName = tagName.toUpperCase();
  105. const matches = HTMLCollectionFactory_1.default.create();
  106. const includeAll = tagName === '*';
  107. for (const child of parentNode.children) {
  108. if ((includeAll || child.tagName === upperTagName) && child.namespaceURI === namespaceURI) {
  109. matches.push(child);
  110. }
  111. for (const match of this.getElementsByTagNameNS(child, namespaceURI, tagName)) {
  112. matches.push(match);
  113. }
  114. }
  115. return matches;
  116. }
  117. /**
  118. * Returns the first element matching a tag name.
  119. * This is not part of the browser standard and is only used internally in the document.
  120. *
  121. * @param parentNode Parent node.
  122. * @param tagName Tag name.
  123. * @returns Matching element.
  124. */
  125. static getElementByTagName(parentNode, tagName) {
  126. const upperTagName = tagName.toUpperCase();
  127. for (const child of parentNode.children) {
  128. if (child.tagName === upperTagName) {
  129. return child;
  130. }
  131. const match = this.getElementByTagName(child, tagName);
  132. if (match) {
  133. return match;
  134. }
  135. }
  136. return null;
  137. }
  138. /**
  139. * Returns an element by ID.
  140. *
  141. * @param parentNode Parent node.
  142. * @param id ID.
  143. * @returns Matching element.
  144. */
  145. static getElementById(parentNode, id) {
  146. for (const child of parentNode.children) {
  147. if (child.id === id) {
  148. return child;
  149. }
  150. const match = this.getElementById(child, id);
  151. if (match) {
  152. return match;
  153. }
  154. }
  155. return null;
  156. }
  157. }
  158. exports.default = ParentNodeUtility;
  159. //# sourceMappingURL=ParentNodeUtility.js.map