DOMRect.js 8.2 KB

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