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

208 рядки
6.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 Node_1 = __importDefault(require("../node/Node"));
  7. const QuerySelector_1 = __importDefault(require("../../query-selector/QuerySelector"));
  8. const ParentNodeUtility_1 = __importDefault(require("../parent-node/ParentNodeUtility"));
  9. const HTMLCollectionFactory_1 = __importDefault(require("../element/HTMLCollectionFactory"));
  10. /**
  11. * DocumentFragment.
  12. */
  13. class DocumentFragment extends Node_1.default {
  14. constructor() {
  15. super(...arguments);
  16. this.nodeType = Node_1.default.DOCUMENT_FRAGMENT_NODE;
  17. this.children = HTMLCollectionFactory_1.default.create();
  18. this._rootNode = this;
  19. }
  20. /**
  21. * Last element child.
  22. *
  23. * @returns Element.
  24. */
  25. get childElementCount() {
  26. return this.children.length;
  27. }
  28. /**
  29. * First element child.
  30. *
  31. * @returns Element.
  32. */
  33. get firstElementChild() {
  34. return this.children ? this.children[0] || null : null;
  35. }
  36. /**
  37. * Last element child.
  38. *
  39. * @returns Element.
  40. */
  41. get lastElementChild() {
  42. return this.children ? this.children[this.children.length - 1] || null : null;
  43. }
  44. /**
  45. * Get text value of children.
  46. *
  47. * @returns Text content.
  48. */
  49. get textContent() {
  50. let result = '';
  51. for (const childNode of this.childNodes) {
  52. if (childNode.nodeType === Node_1.default.ELEMENT_NODE || childNode.nodeType === Node_1.default.TEXT_NODE) {
  53. result += childNode.textContent;
  54. }
  55. }
  56. return result;
  57. }
  58. /**
  59. * Sets text content.
  60. *
  61. * @param textContent Text content.
  62. */
  63. set textContent(textContent) {
  64. for (const child of this.childNodes.slice()) {
  65. this.removeChild(child);
  66. }
  67. if (textContent) {
  68. this.appendChild(this.ownerDocument.createTextNode(textContent));
  69. }
  70. }
  71. /**
  72. * Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
  73. *
  74. * @param nodes List of Node or DOMString.
  75. */
  76. append(...nodes) {
  77. ParentNodeUtility_1.default.append(this, ...nodes);
  78. }
  79. /**
  80. * Inserts a set of Node objects or DOMString objects before the first child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
  81. *
  82. * @param nodes List of Node or DOMString.
  83. */
  84. prepend(...nodes) {
  85. ParentNodeUtility_1.default.prepend(this, ...nodes);
  86. }
  87. /**
  88. * Replaces the existing children of a node with a specified new set of children.
  89. *
  90. * @param nodes List of Node or DOMString.
  91. */
  92. replaceChildren(...nodes) {
  93. ParentNodeUtility_1.default.replaceChildren(this, ...nodes);
  94. }
  95. /**
  96. * Query CSS selector to find matching nodes.
  97. *
  98. * @param selector CSS selector.
  99. * @returns Matching elements.
  100. */
  101. querySelectorAll(selector) {
  102. return QuerySelector_1.default.querySelectorAll(this, selector);
  103. }
  104. /**
  105. * Query CSS Selector to find matching node.
  106. *
  107. * @param selector CSS selector.
  108. * @returns Matching element.
  109. */
  110. querySelector(selector) {
  111. return QuerySelector_1.default.querySelector(this, selector);
  112. }
  113. /**
  114. * Returns an element by ID.
  115. *
  116. * @param id ID.
  117. * @returns Matching element.
  118. */
  119. getElementById(id) {
  120. return ParentNodeUtility_1.default.getElementById(this, id);
  121. }
  122. /**
  123. * Clones a node.
  124. *
  125. * @override
  126. * @param [deep=false] "true" to clone deep.
  127. * @returns Cloned node.
  128. */
  129. cloneNode(deep = false) {
  130. const clone = super.cloneNode(deep);
  131. if (deep) {
  132. for (const node of clone.childNodes) {
  133. if (node.nodeType === Node_1.default.ELEMENT_NODE) {
  134. clone.children.push(node);
  135. }
  136. }
  137. }
  138. return clone;
  139. }
  140. /**
  141. * Append a child node to childNodes.
  142. *
  143. * @override
  144. * @param node Node to append.
  145. * @returns Appended node.
  146. */
  147. appendChild(node) {
  148. // If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
  149. // See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
  150. if (node.nodeType !== Node_1.default.DOCUMENT_FRAGMENT_NODE) {
  151. if (node.parentNode && node.parentNode['children']) {
  152. const index = node.parentNode['children'].indexOf(node);
  153. if (index !== -1) {
  154. node.parentNode['children'].splice(index, 1);
  155. }
  156. }
  157. if (node !== this && node.nodeType === Node_1.default.ELEMENT_NODE) {
  158. this.children.push(node);
  159. }
  160. }
  161. return super.appendChild(node);
  162. }
  163. /**
  164. * Remove Child element from childNodes array.
  165. *
  166. * @override
  167. * @param node Node to remove.
  168. */
  169. removeChild(node) {
  170. if (node.nodeType === Node_1.default.ELEMENT_NODE) {
  171. const index = this.children.indexOf(node);
  172. if (index !== -1) {
  173. this.children.splice(index, 1);
  174. }
  175. }
  176. return super.removeChild(node);
  177. }
  178. /**
  179. * Inserts a node before another.
  180. *
  181. * @override
  182. * @param newNode Node to insert.
  183. * @param [referenceNode] Node to insert before.
  184. * @returns Inserted node.
  185. */
  186. insertBefore(newNode, referenceNode) {
  187. const returnValue = super.insertBefore(newNode, referenceNode);
  188. // If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
  189. // See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
  190. if (newNode.nodeType !== Node_1.default.DOCUMENT_FRAGMENT_NODE) {
  191. if (newNode.parentNode && newNode.parentNode['children']) {
  192. const index = newNode.parentNode['children'].indexOf(newNode);
  193. if (index !== -1) {
  194. newNode.parentNode['children'].splice(index, 1);
  195. }
  196. }
  197. this.children.length = 0;
  198. for (const node of this.childNodes) {
  199. if (node.nodeType === Node_1.default.ELEMENT_NODE) {
  200. this.children.push(node);
  201. }
  202. }
  203. }
  204. return returnValue;
  205. }
  206. }
  207. exports.default = DocumentFragment;
  208. //# sourceMappingURL=DocumentFragment.js.map