TreeWalker.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const Node = require("./Node.js");
  5. const implSymbol = utils.implSymbol;
  6. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  7. const interfaceName = "TreeWalker";
  8. exports.is = value => {
  9. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  10. };
  11. exports.isImpl = value => {
  12. return utils.isObject(value) && value instanceof Impl.implementation;
  13. };
  14. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  15. if (exports.is(value)) {
  16. return utils.implForWrapper(value);
  17. }
  18. throw new globalObject.TypeError(`${context} is not of type 'TreeWalker'.`);
  19. };
  20. function makeWrapper(globalObject, newTarget) {
  21. let proto;
  22. if (newTarget !== undefined) {
  23. proto = newTarget.prototype;
  24. }
  25. if (!utils.isObject(proto)) {
  26. proto = globalObject[ctorRegistrySymbol]["TreeWalker"].prototype;
  27. }
  28. return Object.create(proto);
  29. }
  30. exports.create = (globalObject, constructorArgs, privateData) => {
  31. const wrapper = makeWrapper(globalObject);
  32. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  33. };
  34. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  35. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  36. return utils.implForWrapper(wrapper);
  37. };
  38. exports._internalSetup = (wrapper, globalObject) => {};
  39. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  40. privateData.wrapper = wrapper;
  41. exports._internalSetup(wrapper, globalObject);
  42. Object.defineProperty(wrapper, implSymbol, {
  43. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  44. configurable: true
  45. });
  46. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  47. if (Impl.init) {
  48. Impl.init(wrapper[implSymbol]);
  49. }
  50. return wrapper;
  51. };
  52. exports.new = (globalObject, newTarget) => {
  53. const wrapper = makeWrapper(globalObject, newTarget);
  54. exports._internalSetup(wrapper, globalObject);
  55. Object.defineProperty(wrapper, implSymbol, {
  56. value: Object.create(Impl.implementation.prototype),
  57. configurable: true
  58. });
  59. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  60. if (Impl.init) {
  61. Impl.init(wrapper[implSymbol]);
  62. }
  63. return wrapper[implSymbol];
  64. };
  65. const exposed = new Set(["Window"]);
  66. exports.install = (globalObject, globalNames) => {
  67. if (!globalNames.some(globalName => exposed.has(globalName))) {
  68. return;
  69. }
  70. const ctorRegistry = utils.initCtorRegistry(globalObject);
  71. class TreeWalker {
  72. constructor() {
  73. throw new globalObject.TypeError("Illegal constructor");
  74. }
  75. parentNode() {
  76. const esValue = this !== null && this !== undefined ? this : globalObject;
  77. if (!exports.is(esValue)) {
  78. throw new globalObject.TypeError(
  79. "'parentNode' called on an object that is not a valid instance of TreeWalker."
  80. );
  81. }
  82. return utils.tryWrapperForImpl(esValue[implSymbol].parentNode());
  83. }
  84. firstChild() {
  85. const esValue = this !== null && this !== undefined ? this : globalObject;
  86. if (!exports.is(esValue)) {
  87. throw new globalObject.TypeError(
  88. "'firstChild' called on an object that is not a valid instance of TreeWalker."
  89. );
  90. }
  91. return utils.tryWrapperForImpl(esValue[implSymbol].firstChild());
  92. }
  93. lastChild() {
  94. const esValue = this !== null && this !== undefined ? this : globalObject;
  95. if (!exports.is(esValue)) {
  96. throw new globalObject.TypeError("'lastChild' called on an object that is not a valid instance of TreeWalker.");
  97. }
  98. return utils.tryWrapperForImpl(esValue[implSymbol].lastChild());
  99. }
  100. previousSibling() {
  101. const esValue = this !== null && this !== undefined ? this : globalObject;
  102. if (!exports.is(esValue)) {
  103. throw new globalObject.TypeError(
  104. "'previousSibling' called on an object that is not a valid instance of TreeWalker."
  105. );
  106. }
  107. return utils.tryWrapperForImpl(esValue[implSymbol].previousSibling());
  108. }
  109. nextSibling() {
  110. const esValue = this !== null && this !== undefined ? this : globalObject;
  111. if (!exports.is(esValue)) {
  112. throw new globalObject.TypeError(
  113. "'nextSibling' called on an object that is not a valid instance of TreeWalker."
  114. );
  115. }
  116. return utils.tryWrapperForImpl(esValue[implSymbol].nextSibling());
  117. }
  118. previousNode() {
  119. const esValue = this !== null && this !== undefined ? this : globalObject;
  120. if (!exports.is(esValue)) {
  121. throw new globalObject.TypeError(
  122. "'previousNode' called on an object that is not a valid instance of TreeWalker."
  123. );
  124. }
  125. return utils.tryWrapperForImpl(esValue[implSymbol].previousNode());
  126. }
  127. nextNode() {
  128. const esValue = this !== null && this !== undefined ? this : globalObject;
  129. if (!exports.is(esValue)) {
  130. throw new globalObject.TypeError("'nextNode' called on an object that is not a valid instance of TreeWalker.");
  131. }
  132. return utils.tryWrapperForImpl(esValue[implSymbol].nextNode());
  133. }
  134. get root() {
  135. const esValue = this !== null && this !== undefined ? this : globalObject;
  136. if (!exports.is(esValue)) {
  137. throw new globalObject.TypeError("'get root' called on an object that is not a valid instance of TreeWalker.");
  138. }
  139. return utils.getSameObject(this, "root", () => {
  140. return utils.tryWrapperForImpl(esValue[implSymbol]["root"]);
  141. });
  142. }
  143. get whatToShow() {
  144. const esValue = this !== null && this !== undefined ? this : globalObject;
  145. if (!exports.is(esValue)) {
  146. throw new globalObject.TypeError(
  147. "'get whatToShow' called on an object that is not a valid instance of TreeWalker."
  148. );
  149. }
  150. return esValue[implSymbol]["whatToShow"];
  151. }
  152. get filter() {
  153. const esValue = this !== null && this !== undefined ? this : globalObject;
  154. if (!exports.is(esValue)) {
  155. throw new globalObject.TypeError(
  156. "'get filter' called on an object that is not a valid instance of TreeWalker."
  157. );
  158. }
  159. return utils.tryWrapperForImpl(esValue[implSymbol]["filter"]);
  160. }
  161. get currentNode() {
  162. const esValue = this !== null && this !== undefined ? this : globalObject;
  163. if (!exports.is(esValue)) {
  164. throw new globalObject.TypeError(
  165. "'get currentNode' called on an object that is not a valid instance of TreeWalker."
  166. );
  167. }
  168. return utils.tryWrapperForImpl(esValue[implSymbol]["currentNode"]);
  169. }
  170. set currentNode(V) {
  171. const esValue = this !== null && this !== undefined ? this : globalObject;
  172. if (!exports.is(esValue)) {
  173. throw new globalObject.TypeError(
  174. "'set currentNode' called on an object that is not a valid instance of TreeWalker."
  175. );
  176. }
  177. V = Node.convert(globalObject, V, {
  178. context: "Failed to set the 'currentNode' property on 'TreeWalker': The provided value"
  179. });
  180. esValue[implSymbol]["currentNode"] = V;
  181. }
  182. }
  183. Object.defineProperties(TreeWalker.prototype, {
  184. parentNode: { enumerable: true },
  185. firstChild: { enumerable: true },
  186. lastChild: { enumerable: true },
  187. previousSibling: { enumerable: true },
  188. nextSibling: { enumerable: true },
  189. previousNode: { enumerable: true },
  190. nextNode: { enumerable: true },
  191. root: { enumerable: true },
  192. whatToShow: { enumerable: true },
  193. filter: { enumerable: true },
  194. currentNode: { enumerable: true },
  195. [Symbol.toStringTag]: { value: "TreeWalker", configurable: true }
  196. });
  197. ctorRegistry[interfaceName] = TreeWalker;
  198. Object.defineProperty(globalObject, interfaceName, {
  199. configurable: true,
  200. writable: true,
  201. value: TreeWalker
  202. });
  203. };
  204. const Impl = require("../../jsdom/living/traversal/TreeWalker-impl.js");