版博士V2.0程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ChildNodeUtility.js 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 DOMException_1 = __importDefault(require("../../exception/DOMException"));
  7. const XMLParser_1 = __importDefault(require("../../xml-parser/XMLParser"));
  8. /**
  9. * Child node utility.
  10. */
  11. class ChildNodeUtility {
  12. /**
  13. * Removes the node from its parent.
  14. *
  15. * @param childNode Child node.
  16. */
  17. static remove(childNode) {
  18. if (childNode.parentNode) {
  19. childNode.parentNode.removeChild(childNode);
  20. }
  21. }
  22. /**
  23. * The Node.replaceWith() method replaces this Node in the children list of its parent with a set of Node or DOMString objects.
  24. *
  25. * @param childNode Child node.
  26. * @param nodes List of Node or DOMString.
  27. */
  28. static replaceWith(childNode, ...nodes) {
  29. const parent = childNode.parentNode;
  30. if (!parent) {
  31. throw new DOMException_1.default('This element has no parent node.');
  32. }
  33. for (const node of nodes) {
  34. if (typeof node === 'string') {
  35. const newChildNodes = XMLParser_1.default.parse(childNode.ownerDocument, node).childNodes.slice();
  36. for (const newChildNode of newChildNodes) {
  37. parent.insertBefore(newChildNode, childNode);
  38. }
  39. }
  40. else {
  41. parent.insertBefore(node, childNode);
  42. }
  43. }
  44. parent.removeChild(childNode);
  45. }
  46. /**
  47. * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just before this ChildNode. DOMString objects are inserted as equivalent Text nodes.
  48. *
  49. * @param childNode Child node.
  50. * @param nodes List of Node or DOMString.
  51. */
  52. static before(childNode, ...nodes) {
  53. const parent = childNode.parentNode;
  54. if (!parent) {
  55. return;
  56. }
  57. for (const node of nodes) {
  58. if (typeof node === 'string') {
  59. const newChildNodes = XMLParser_1.default.parse(childNode.ownerDocument, node).childNodes.slice();
  60. for (const newChildNode of newChildNodes) {
  61. parent.insertBefore(newChildNode, childNode);
  62. }
  63. }
  64. else {
  65. parent.insertBefore(node, childNode);
  66. }
  67. }
  68. }
  69. /**
  70. * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just after this ChildNode. DOMString objects are inserted as equivalent Text nodes.
  71. *
  72. * @param childNode Child node.
  73. * @param nodes List of Node or DOMString.
  74. */
  75. static after(childNode, ...nodes) {
  76. const parent = childNode.parentNode;
  77. if (!parent) {
  78. return;
  79. }
  80. const nextSibling = childNode.nextSibling;
  81. for (const node of nodes) {
  82. if (typeof node === 'string') {
  83. const newChildNodes = XMLParser_1.default.parse(childNode.ownerDocument, node).childNodes.slice();
  84. for (const newChildNode of newChildNodes) {
  85. if (!nextSibling) {
  86. parent.appendChild(newChildNode);
  87. }
  88. else {
  89. parent.insertBefore(newChildNode, nextSibling);
  90. }
  91. }
  92. }
  93. else if (!nextSibling) {
  94. parent.appendChild(node);
  95. }
  96. else {
  97. parent.insertBefore(node, nextSibling);
  98. }
  99. }
  100. }
  101. }
  102. exports.default = ChildNodeUtility;
  103. //# sourceMappingURL=ChildNodeUtility.js.map