CSSKeyframesRule.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const implSymbol = utils.implSymbol;
  5. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  6. const CSSRule = require("./CSSRule.js");
  7. const interfaceName = "CSSKeyframesRule";
  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 'CSSKeyframesRule'.`);
  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]["CSSKeyframesRule"].prototype;
  27. }
  28. return Object.create(proto);
  29. }
  30. function makeProxy(wrapper, globalObject) {
  31. let proxyHandler = proxyHandlerCache.get(globalObject);
  32. if (proxyHandler === undefined) {
  33. proxyHandler = new ProxyHandler(globalObject);
  34. proxyHandlerCache.set(globalObject, proxyHandler);
  35. }
  36. return new Proxy(wrapper, proxyHandler);
  37. }
  38. exports.create = (globalObject, constructorArgs, privateData) => {
  39. const wrapper = makeWrapper(globalObject);
  40. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  41. };
  42. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  43. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  44. return utils.implForWrapper(wrapper);
  45. };
  46. exports._internalSetup = (wrapper, globalObject) => {
  47. CSSRule._internalSetup(wrapper, globalObject);
  48. };
  49. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  50. privateData.wrapper = wrapper;
  51. exports._internalSetup(wrapper, globalObject);
  52. Object.defineProperty(wrapper, implSymbol, {
  53. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  54. configurable: true
  55. });
  56. wrapper = makeProxy(wrapper, globalObject);
  57. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  58. if (Impl.init) {
  59. Impl.init(wrapper[implSymbol]);
  60. }
  61. return wrapper;
  62. };
  63. exports.new = (globalObject, newTarget) => {
  64. let wrapper = makeWrapper(globalObject, newTarget);
  65. exports._internalSetup(wrapper, globalObject);
  66. Object.defineProperty(wrapper, implSymbol, {
  67. value: Object.create(Impl.implementation.prototype),
  68. configurable: true
  69. });
  70. wrapper = makeProxy(wrapper, globalObject);
  71. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  72. if (Impl.init) {
  73. Impl.init(wrapper[implSymbol]);
  74. }
  75. return wrapper[implSymbol];
  76. };
  77. const exposed = new Set(["Window"]);
  78. exports.install = (globalObject, globalNames) => {
  79. if (!globalNames.some(globalName => exposed.has(globalName))) {
  80. return;
  81. }
  82. const ctorRegistry = utils.initCtorRegistry(globalObject);
  83. class CSSKeyframesRule extends globalObject.CSSRule {
  84. constructor() {
  85. throw new globalObject.TypeError("Illegal constructor");
  86. }
  87. appendRule(rule) {
  88. const esValue = this !== null && this !== undefined ? this : globalObject;
  89. if (!exports.is(esValue)) {
  90. throw new globalObject.TypeError(
  91. "'appendRule' called on an object that is not a valid instance of CSSKeyframesRule."
  92. );
  93. }
  94. if (arguments.length < 1) {
  95. throw new globalObject.TypeError(
  96. `Failed to execute 'appendRule' on 'CSSKeyframesRule': 1 argument required, but only ${arguments.length} present.`
  97. );
  98. }
  99. const args = [];
  100. {
  101. let curArg = arguments[0];
  102. curArg = conversions["DOMString"](curArg, {
  103. context: "Failed to execute 'appendRule' on 'CSSKeyframesRule': parameter 1",
  104. globals: globalObject
  105. });
  106. args.push(curArg);
  107. }
  108. return esValue[implSymbol].appendRule(...args);
  109. }
  110. deleteRule(select) {
  111. const esValue = this !== null && this !== undefined ? this : globalObject;
  112. if (!exports.is(esValue)) {
  113. throw new globalObject.TypeError(
  114. "'deleteRule' called on an object that is not a valid instance of CSSKeyframesRule."
  115. );
  116. }
  117. if (arguments.length < 1) {
  118. throw new globalObject.TypeError(
  119. `Failed to execute 'deleteRule' on 'CSSKeyframesRule': 1 argument required, but only ${arguments.length} present.`
  120. );
  121. }
  122. const args = [];
  123. {
  124. let curArg = arguments[0];
  125. curArg = conversions["DOMString"](curArg, {
  126. context: "Failed to execute 'deleteRule' on 'CSSKeyframesRule': parameter 1",
  127. globals: globalObject
  128. });
  129. args.push(curArg);
  130. }
  131. return esValue[implSymbol].deleteRule(...args);
  132. }
  133. findRule(select) {
  134. const esValue = this !== null && this !== undefined ? this : globalObject;
  135. if (!exports.is(esValue)) {
  136. throw new globalObject.TypeError(
  137. "'findRule' called on an object that is not a valid instance of CSSKeyframesRule."
  138. );
  139. }
  140. if (arguments.length < 1) {
  141. throw new globalObject.TypeError(
  142. `Failed to execute 'findRule' on 'CSSKeyframesRule': 1 argument required, but only ${arguments.length} present.`
  143. );
  144. }
  145. const args = [];
  146. {
  147. let curArg = arguments[0];
  148. curArg = conversions["DOMString"](curArg, {
  149. context: "Failed to execute 'findRule' on 'CSSKeyframesRule': parameter 1",
  150. globals: globalObject
  151. });
  152. args.push(curArg);
  153. }
  154. return utils.tryWrapperForImpl(esValue[implSymbol].findRule(...args));
  155. }
  156. get name() {
  157. const esValue = this !== null && this !== undefined ? this : globalObject;
  158. if (!exports.is(esValue)) {
  159. throw new globalObject.TypeError(
  160. "'get name' called on an object that is not a valid instance of CSSKeyframesRule."
  161. );
  162. }
  163. return utils.tryWrapperForImpl(esValue[implSymbol]["name"]);
  164. }
  165. set name(V) {
  166. const esValue = this !== null && this !== undefined ? this : globalObject;
  167. if (!exports.is(esValue)) {
  168. throw new globalObject.TypeError(
  169. "'set name' called on an object that is not a valid instance of CSSKeyframesRule."
  170. );
  171. }
  172. V = conversions["DOMString"](V, {
  173. context: "Failed to set the 'name' property on 'CSSKeyframesRule': The provided value",
  174. globals: globalObject
  175. });
  176. esValue[implSymbol]["name"] = V;
  177. }
  178. get cssRules() {
  179. const esValue = this !== null && this !== undefined ? this : globalObject;
  180. if (!exports.is(esValue)) {
  181. throw new globalObject.TypeError(
  182. "'get cssRules' called on an object that is not a valid instance of CSSKeyframesRule."
  183. );
  184. }
  185. return utils.getSameObject(this, "cssRules", () => {
  186. return utils.tryWrapperForImpl(esValue[implSymbol]["cssRules"]);
  187. });
  188. }
  189. get length() {
  190. const esValue = this !== null && this !== undefined ? this : globalObject;
  191. if (!exports.is(esValue)) {
  192. throw new globalObject.TypeError(
  193. "'get length' called on an object that is not a valid instance of CSSKeyframesRule."
  194. );
  195. }
  196. return esValue[implSymbol]["length"];
  197. }
  198. }
  199. Object.defineProperties(CSSKeyframesRule.prototype, {
  200. appendRule: { enumerable: true },
  201. deleteRule: { enumerable: true },
  202. findRule: { enumerable: true },
  203. name: { enumerable: true },
  204. cssRules: { enumerable: true },
  205. length: { enumerable: true },
  206. [Symbol.toStringTag]: { value: "CSSKeyframesRule", configurable: true },
  207. [Symbol.iterator]: { value: globalObject.Array.prototype[Symbol.iterator], configurable: true, writable: true }
  208. });
  209. ctorRegistry[interfaceName] = CSSKeyframesRule;
  210. Object.defineProperty(globalObject, interfaceName, {
  211. configurable: true,
  212. writable: true,
  213. value: CSSKeyframesRule
  214. });
  215. };
  216. const proxyHandlerCache = new WeakMap();
  217. class ProxyHandler {
  218. constructor(globalObject) {
  219. this._globalObject = globalObject;
  220. }
  221. get(target, P, receiver) {
  222. if (typeof P === "symbol") {
  223. return Reflect.get(target, P, receiver);
  224. }
  225. const desc = this.getOwnPropertyDescriptor(target, P);
  226. if (desc === undefined) {
  227. const parent = Object.getPrototypeOf(target);
  228. if (parent === null) {
  229. return undefined;
  230. }
  231. return Reflect.get(target, P, receiver);
  232. }
  233. if (!desc.get && !desc.set) {
  234. return desc.value;
  235. }
  236. const getter = desc.get;
  237. if (getter === undefined) {
  238. return undefined;
  239. }
  240. return Reflect.apply(getter, receiver, []);
  241. }
  242. has(target, P) {
  243. if (typeof P === "symbol") {
  244. return Reflect.has(target, P);
  245. }
  246. const desc = this.getOwnPropertyDescriptor(target, P);
  247. if (desc !== undefined) {
  248. return true;
  249. }
  250. const parent = Object.getPrototypeOf(target);
  251. if (parent !== null) {
  252. return Reflect.has(parent, P);
  253. }
  254. return false;
  255. }
  256. ownKeys(target) {
  257. const keys = new Set();
  258. for (const key of target[implSymbol][utils.supportedPropertyIndices]) {
  259. keys.add(`${key}`);
  260. }
  261. for (const key of Reflect.ownKeys(target)) {
  262. keys.add(key);
  263. }
  264. return [...keys];
  265. }
  266. getOwnPropertyDescriptor(target, P) {
  267. if (typeof P === "symbol") {
  268. return Reflect.getOwnPropertyDescriptor(target, P);
  269. }
  270. let ignoreNamedProps = false;
  271. if (utils.isArrayIndexPropName(P)) {
  272. const index = P >>> 0;
  273. if (target[implSymbol][utils.supportsPropertyIndex](index)) {
  274. const indexedValue = target[implSymbol][utils.indexedGet](index);
  275. return {
  276. writable: false,
  277. enumerable: true,
  278. configurable: true,
  279. value: utils.tryWrapperForImpl(indexedValue)
  280. };
  281. }
  282. ignoreNamedProps = true;
  283. }
  284. return Reflect.getOwnPropertyDescriptor(target, P);
  285. }
  286. set(target, P, V, receiver) {
  287. if (typeof P === "symbol") {
  288. return Reflect.set(target, P, V, receiver);
  289. }
  290. // The `receiver` argument refers to the Proxy exotic object or an object
  291. // that inherits from it, whereas `target` refers to the Proxy target:
  292. if (target[implSymbol][utils.wrapperSymbol] === receiver) {
  293. const globalObject = this._globalObject;
  294. }
  295. let ownDesc;
  296. if (utils.isArrayIndexPropName(P)) {
  297. const index = P >>> 0;
  298. if (target[implSymbol][utils.supportsPropertyIndex](index)) {
  299. const indexedValue = target[implSymbol][utils.indexedGet](index);
  300. ownDesc = {
  301. writable: false,
  302. enumerable: true,
  303. configurable: true,
  304. value: utils.tryWrapperForImpl(indexedValue)
  305. };
  306. }
  307. }
  308. if (ownDesc === undefined) {
  309. ownDesc = Reflect.getOwnPropertyDescriptor(target, P);
  310. }
  311. return utils.ordinarySetWithOwnDescriptor(target, P, V, receiver, ownDesc);
  312. }
  313. defineProperty(target, P, desc) {
  314. if (typeof P === "symbol") {
  315. return Reflect.defineProperty(target, P, desc);
  316. }
  317. const globalObject = this._globalObject;
  318. if (utils.isArrayIndexPropName(P)) {
  319. return false;
  320. }
  321. return Reflect.defineProperty(target, P, desc);
  322. }
  323. deleteProperty(target, P) {
  324. if (typeof P === "symbol") {
  325. return Reflect.deleteProperty(target, P);
  326. }
  327. const globalObject = this._globalObject;
  328. if (utils.isArrayIndexPropName(P)) {
  329. const index = P >>> 0;
  330. return !target[implSymbol][utils.supportsPropertyIndex](index);
  331. }
  332. return Reflect.deleteProperty(target, P);
  333. }
  334. preventExtensions() {
  335. return false;
  336. }
  337. }
  338. const Impl = require("../../jsdom/living/css/CSSKeyframesRule-impl.js");