ShadowRoot.js 5.9 KB

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