MutationObserver.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const MutationCallback = require("./MutationCallback.js");
  5. const Node = require("./Node.js");
  6. const MutationObserverInit = require("./MutationObserverInit.js");
  7. const implSymbol = utils.implSymbol;
  8. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  9. const interfaceName = "MutationObserver";
  10. exports.is = value => {
  11. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  12. };
  13. exports.isImpl = value => {
  14. return utils.isObject(value) && value instanceof Impl.implementation;
  15. };
  16. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  17. if (exports.is(value)) {
  18. return utils.implForWrapper(value);
  19. }
  20. throw new globalObject.TypeError(`${context} is not of type 'MutationObserver'.`);
  21. };
  22. function makeWrapper(globalObject, newTarget) {
  23. let proto;
  24. if (newTarget !== undefined) {
  25. proto = newTarget.prototype;
  26. }
  27. if (!utils.isObject(proto)) {
  28. proto = globalObject[ctorRegistrySymbol]["MutationObserver"].prototype;
  29. }
  30. return Object.create(proto);
  31. }
  32. exports.create = (globalObject, constructorArgs, privateData) => {
  33. const wrapper = makeWrapper(globalObject);
  34. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  35. };
  36. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  37. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  38. return utils.implForWrapper(wrapper);
  39. };
  40. exports._internalSetup = (wrapper, globalObject) => {};
  41. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  42. privateData.wrapper = wrapper;
  43. exports._internalSetup(wrapper, globalObject);
  44. Object.defineProperty(wrapper, implSymbol, {
  45. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  46. configurable: true
  47. });
  48. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  49. if (Impl.init) {
  50. Impl.init(wrapper[implSymbol]);
  51. }
  52. return wrapper;
  53. };
  54. exports.new = (globalObject, newTarget) => {
  55. const wrapper = makeWrapper(globalObject, newTarget);
  56. exports._internalSetup(wrapper, globalObject);
  57. Object.defineProperty(wrapper, implSymbol, {
  58. value: Object.create(Impl.implementation.prototype),
  59. configurable: true
  60. });
  61. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  62. if (Impl.init) {
  63. Impl.init(wrapper[implSymbol]);
  64. }
  65. return wrapper[implSymbol];
  66. };
  67. const exposed = new Set(["Window"]);
  68. exports.install = (globalObject, globalNames) => {
  69. if (!globalNames.some(globalName => exposed.has(globalName))) {
  70. return;
  71. }
  72. const ctorRegistry = utils.initCtorRegistry(globalObject);
  73. class MutationObserver {
  74. constructor(callback) {
  75. if (arguments.length < 1) {
  76. throw new globalObject.TypeError(
  77. `Failed to construct 'MutationObserver': 1 argument required, but only ${arguments.length} present.`
  78. );
  79. }
  80. const args = [];
  81. {
  82. let curArg = arguments[0];
  83. curArg = MutationCallback.convert(globalObject, curArg, {
  84. context: "Failed to construct 'MutationObserver': parameter 1"
  85. });
  86. args.push(curArg);
  87. }
  88. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  89. }
  90. observe(target) {
  91. const esValue = this !== null && this !== undefined ? this : globalObject;
  92. if (!exports.is(esValue)) {
  93. throw new globalObject.TypeError(
  94. "'observe' called on an object that is not a valid instance of MutationObserver."
  95. );
  96. }
  97. if (arguments.length < 1) {
  98. throw new globalObject.TypeError(
  99. `Failed to execute 'observe' on 'MutationObserver': 1 argument required, but only ${arguments.length} present.`
  100. );
  101. }
  102. const args = [];
  103. {
  104. let curArg = arguments[0];
  105. curArg = Node.convert(globalObject, curArg, {
  106. context: "Failed to execute 'observe' on 'MutationObserver': parameter 1"
  107. });
  108. args.push(curArg);
  109. }
  110. {
  111. let curArg = arguments[1];
  112. curArg = MutationObserverInit.convert(globalObject, curArg, {
  113. context: "Failed to execute 'observe' on 'MutationObserver': parameter 2"
  114. });
  115. args.push(curArg);
  116. }
  117. return esValue[implSymbol].observe(...args);
  118. }
  119. disconnect() {
  120. const esValue = this !== null && this !== undefined ? this : globalObject;
  121. if (!exports.is(esValue)) {
  122. throw new globalObject.TypeError(
  123. "'disconnect' called on an object that is not a valid instance of MutationObserver."
  124. );
  125. }
  126. return esValue[implSymbol].disconnect();
  127. }
  128. takeRecords() {
  129. const esValue = this !== null && this !== undefined ? this : globalObject;
  130. if (!exports.is(esValue)) {
  131. throw new globalObject.TypeError(
  132. "'takeRecords' called on an object that is not a valid instance of MutationObserver."
  133. );
  134. }
  135. return utils.tryWrapperForImpl(esValue[implSymbol].takeRecords());
  136. }
  137. }
  138. Object.defineProperties(MutationObserver.prototype, {
  139. observe: { enumerable: true },
  140. disconnect: { enumerable: true },
  141. takeRecords: { enumerable: true },
  142. [Symbol.toStringTag]: { value: "MutationObserver", configurable: true }
  143. });
  144. ctorRegistry[interfaceName] = MutationObserver;
  145. Object.defineProperty(globalObject, interfaceName, {
  146. configurable: true,
  147. writable: true,
  148. value: MutationObserver
  149. });
  150. };
  151. const Impl = require("../../jsdom/living/mutation-observer/MutationObserver-impl.js");