CSSStyleDeclaration.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 interfaceName = "CSSStyleDeclaration";
  11. exports.is = value => {
  12. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  13. };
  14. exports.isImpl = value => {
  15. return utils.isObject(value) && value instanceof Impl.implementation;
  16. };
  17. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  18. if (exports.is(value)) {
  19. return utils.implForWrapper(value);
  20. }
  21. throw new globalObject.TypeError(`${context} is not of type 'CSSStyleDeclaration'.`);
  22. };
  23. function makeWrapper(globalObject, newTarget) {
  24. let proto;
  25. if (newTarget !== undefined) {
  26. proto = newTarget.prototype;
  27. }
  28. if (!utils.isObject(proto)) {
  29. proto = globalObject[ctorRegistrySymbol]["CSSStyleDeclaration"].prototype;
  30. }
  31. return Object.create(proto);
  32. }
  33. function makeProxy(wrapper, globalObject) {
  34. let proxyHandler = proxyHandlerCache.get(globalObject);
  35. if (proxyHandler === undefined) {
  36. proxyHandler = new ProxyHandler(globalObject);
  37. proxyHandlerCache.set(globalObject, proxyHandler);
  38. }
  39. return new Proxy(wrapper, proxyHandler);
  40. }
  41. exports.create = (globalObject, constructorArgs, privateData) => {
  42. const wrapper = makeWrapper(globalObject);
  43. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  44. };
  45. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  46. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  47. return utils.implForWrapper(wrapper);
  48. };
  49. exports._internalSetup = (wrapper, globalObject) => {};
  50. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  51. privateData.wrapper = wrapper;
  52. exports._internalSetup(wrapper, globalObject);
  53. Object.defineProperty(wrapper, implSymbol, {
  54. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  55. configurable: true
  56. });
  57. wrapper = makeProxy(wrapper, globalObject);
  58. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  59. if (Impl.init) {
  60. Impl.init(wrapper[implSymbol]);
  61. }
  62. return wrapper;
  63. };
  64. exports.new = (globalObject, newTarget) => {
  65. let wrapper = makeWrapper(globalObject, newTarget);
  66. exports._internalSetup(wrapper, globalObject);
  67. Object.defineProperty(wrapper, implSymbol, {
  68. value: Object.create(Impl.implementation.prototype),
  69. configurable: true
  70. });
  71. wrapper = makeProxy(wrapper, globalObject);
  72. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  73. if (Impl.init) {
  74. Impl.init(wrapper[implSymbol]);
  75. }
  76. return wrapper[implSymbol];
  77. };
  78. const exposed = new Set(["Window"]);
  79. exports.install = (globalObject, globalNames) => {
  80. if (!globalNames.some(globalName => exposed.has(globalName))) {
  81. return;
  82. }
  83. const ctorRegistry = utils.initCtorRegistry(globalObject);
  84. class CSSStyleDeclaration {
  85. constructor() {
  86. throw new globalObject.TypeError("Illegal constructor");
  87. }
  88. item(index) {
  89. const esValue = this !== null && this !== undefined ? this : globalObject;
  90. if (!exports.is(esValue)) {
  91. throw new globalObject.TypeError(
  92. "'item' called on an object that is not a valid instance of CSSStyleDeclaration."
  93. );
  94. }
  95. if (arguments.length < 1) {
  96. throw new globalObject.TypeError(
  97. `Failed to execute 'item' on 'CSSStyleDeclaration': 1 argument required, but only ${arguments.length} present.`
  98. );
  99. }
  100. const args = [];
  101. {
  102. let curArg = arguments[0];
  103. curArg = conversions["unsigned long"](curArg, {
  104. context: "Failed to execute 'item' on 'CSSStyleDeclaration': parameter 1",
  105. globals: globalObject
  106. });
  107. args.push(curArg);
  108. }
  109. return utils.tryWrapperForImpl(esValue[implSymbol].item(...args));
  110. }
  111. getPropertyValue(property) {
  112. const esValue = this !== null && this !== undefined ? this : globalObject;
  113. if (!exports.is(esValue)) {
  114. throw new globalObject.TypeError(
  115. "'getPropertyValue' called on an object that is not a valid instance of CSSStyleDeclaration."
  116. );
  117. }
  118. if (arguments.length < 1) {
  119. throw new globalObject.TypeError(
  120. `Failed to execute 'getPropertyValue' on 'CSSStyleDeclaration': 1 argument required, but only ${arguments.length} present.`
  121. );
  122. }
  123. const args = [];
  124. {
  125. let curArg = arguments[0];
  126. curArg = conversions["DOMString"](curArg, {
  127. context: "Failed to execute 'getPropertyValue' on 'CSSStyleDeclaration': parameter 1",
  128. globals: globalObject
  129. });
  130. args.push(curArg);
  131. }
  132. return utils.tryWrapperForImpl(esValue[implSymbol].getPropertyValue(...args));
  133. }
  134. getPropertyPriority(property) {
  135. const esValue = this !== null && this !== undefined ? this : globalObject;
  136. if (!exports.is(esValue)) {
  137. throw new globalObject.TypeError(
  138. "'getPropertyPriority' called on an object that is not a valid instance of CSSStyleDeclaration."
  139. );
  140. }
  141. if (arguments.length < 1) {
  142. throw new globalObject.TypeError(
  143. `Failed to execute 'getPropertyPriority' on 'CSSStyleDeclaration': 1 argument required, but only ${arguments.length} present.`
  144. );
  145. }
  146. const args = [];
  147. {
  148. let curArg = arguments[0];
  149. curArg = conversions["DOMString"](curArg, {
  150. context: "Failed to execute 'getPropertyPriority' on 'CSSStyleDeclaration': parameter 1",
  151. globals: globalObject
  152. });
  153. args.push(curArg);
  154. }
  155. return utils.tryWrapperForImpl(esValue[implSymbol].getPropertyPriority(...args));
  156. }
  157. setProperty(property, value) {
  158. const esValue = this !== null && this !== undefined ? this : globalObject;
  159. if (!exports.is(esValue)) {
  160. throw new globalObject.TypeError(
  161. "'setProperty' called on an object that is not a valid instance of CSSStyleDeclaration."
  162. );
  163. }
  164. if (arguments.length < 2) {
  165. throw new globalObject.TypeError(
  166. `Failed to execute 'setProperty' on 'CSSStyleDeclaration': 2 arguments required, but only ${arguments.length} present.`
  167. );
  168. }
  169. const args = [];
  170. {
  171. let curArg = arguments[0];
  172. curArg = conversions["DOMString"](curArg, {
  173. context: "Failed to execute 'setProperty' on 'CSSStyleDeclaration': parameter 1",
  174. globals: globalObject
  175. });
  176. args.push(curArg);
  177. }
  178. {
  179. let curArg = arguments[1];
  180. curArg = conversions["DOMString"](curArg, {
  181. context: "Failed to execute 'setProperty' on 'CSSStyleDeclaration': parameter 2",
  182. globals: globalObject,
  183. treatNullAsEmptyString: true
  184. });
  185. args.push(curArg);
  186. }
  187. {
  188. let curArg = arguments[2];
  189. if (curArg !== undefined) {
  190. curArg = conversions["DOMString"](curArg, {
  191. context: "Failed to execute 'setProperty' on 'CSSStyleDeclaration': parameter 3",
  192. globals: globalObject,
  193. treatNullAsEmptyString: true
  194. });
  195. } else {
  196. curArg = "";
  197. }
  198. args.push(curArg);
  199. }
  200. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  201. try {
  202. return esValue[implSymbol].setProperty(...args);
  203. } finally {
  204. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  205. }
  206. }
  207. removeProperty(property) {
  208. const esValue = this !== null && this !== undefined ? this : globalObject;
  209. if (!exports.is(esValue)) {
  210. throw new globalObject.TypeError(
  211. "'removeProperty' called on an object that is not a valid instance of CSSStyleDeclaration."
  212. );
  213. }
  214. if (arguments.length < 1) {
  215. throw new globalObject.TypeError(
  216. `Failed to execute 'removeProperty' on 'CSSStyleDeclaration': 1 argument required, but only ${arguments.length} present.`
  217. );
  218. }
  219. const args = [];
  220. {
  221. let curArg = arguments[0];
  222. curArg = conversions["DOMString"](curArg, {
  223. context: "Failed to execute 'removeProperty' on 'CSSStyleDeclaration': parameter 1",
  224. globals: globalObject
  225. });
  226. args.push(curArg);
  227. }
  228. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  229. try {
  230. return utils.tryWrapperForImpl(esValue[implSymbol].removeProperty(...args));
  231. } finally {
  232. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  233. }
  234. }
  235. get cssText() {
  236. const esValue = this !== null && this !== undefined ? this : globalObject;
  237. if (!exports.is(esValue)) {
  238. throw new globalObject.TypeError(
  239. "'get cssText' called on an object that is not a valid instance of CSSStyleDeclaration."
  240. );
  241. }
  242. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  243. try {
  244. return utils.tryWrapperForImpl(esValue[implSymbol]["cssText"]);
  245. } finally {
  246. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  247. }
  248. }
  249. set cssText(V) {
  250. const esValue = this !== null && this !== undefined ? this : globalObject;
  251. if (!exports.is(esValue)) {
  252. throw new globalObject.TypeError(
  253. "'set cssText' called on an object that is not a valid instance of CSSStyleDeclaration."
  254. );
  255. }
  256. V = conversions["DOMString"](V, {
  257. context: "Failed to set the 'cssText' property on 'CSSStyleDeclaration': The provided value",
  258. globals: globalObject
  259. });
  260. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  261. try {
  262. esValue[implSymbol]["cssText"] = V;
  263. } finally {
  264. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  265. }
  266. }
  267. get length() {
  268. const esValue = this !== null && this !== undefined ? this : globalObject;
  269. if (!exports.is(esValue)) {
  270. throw new globalObject.TypeError(
  271. "'get length' called on an object that is not a valid instance of CSSStyleDeclaration."
  272. );
  273. }
  274. return esValue[implSymbol]["length"];
  275. }
  276. get parentRule() {
  277. const esValue = this !== null && this !== undefined ? this : globalObject;
  278. if (!exports.is(esValue)) {
  279. throw new globalObject.TypeError(
  280. "'get parentRule' called on an object that is not a valid instance of CSSStyleDeclaration."
  281. );
  282. }
  283. return utils.tryWrapperForImpl(esValue[implSymbol]["parentRule"]);
  284. }
  285. }
  286. Object.defineProperties(CSSStyleDeclaration.prototype, {
  287. item: { enumerable: true },
  288. getPropertyValue: { enumerable: true },
  289. getPropertyPriority: { enumerable: true },
  290. setProperty: { enumerable: true },
  291. removeProperty: { enumerable: true },
  292. cssText: { enumerable: true },
  293. length: { enumerable: true },
  294. parentRule: { enumerable: true },
  295. [Symbol.toStringTag]: { value: "CSSStyleDeclaration", configurable: true },
  296. [Symbol.iterator]: { value: globalObject.Array.prototype[Symbol.iterator], configurable: true, writable: true }
  297. });
  298. ctorRegistry[interfaceName] = CSSStyleDeclaration;
  299. Object.defineProperty(globalObject, interfaceName, {
  300. configurable: true,
  301. writable: true,
  302. value: CSSStyleDeclaration
  303. });
  304. };
  305. const proxyHandlerCache = new WeakMap();
  306. class ProxyHandler {
  307. constructor(globalObject) {
  308. this._globalObject = globalObject;
  309. }
  310. get(target, P, receiver) {
  311. if (typeof P === "symbol") {
  312. return Reflect.get(target, P, receiver);
  313. }
  314. const desc = this.getOwnPropertyDescriptor(target, P);
  315. if (desc === undefined) {
  316. const parent = Object.getPrototypeOf(target);
  317. if (parent === null) {
  318. return undefined;
  319. }
  320. return Reflect.get(target, P, receiver);
  321. }
  322. if (!desc.get && !desc.set) {
  323. return desc.value;
  324. }
  325. const getter = desc.get;
  326. if (getter === undefined) {
  327. return undefined;
  328. }
  329. return Reflect.apply(getter, receiver, []);
  330. }
  331. has(target, P) {
  332. if (typeof P === "symbol") {
  333. return Reflect.has(target, P);
  334. }
  335. const desc = this.getOwnPropertyDescriptor(target, P);
  336. if (desc !== undefined) {
  337. return true;
  338. }
  339. const parent = Object.getPrototypeOf(target);
  340. if (parent !== null) {
  341. return Reflect.has(parent, P);
  342. }
  343. return false;
  344. }
  345. ownKeys(target) {
  346. const keys = new Set();
  347. for (const key of target[implSymbol][utils.supportedPropertyIndices]) {
  348. keys.add(`${key}`);
  349. }
  350. for (const key of Reflect.ownKeys(target)) {
  351. keys.add(key);
  352. }
  353. return [...keys];
  354. }
  355. getOwnPropertyDescriptor(target, P) {
  356. if (typeof P === "symbol") {
  357. return Reflect.getOwnPropertyDescriptor(target, P);
  358. }
  359. let ignoreNamedProps = false;
  360. if (utils.isArrayIndexPropName(P)) {
  361. const index = P >>> 0;
  362. if (target[implSymbol][utils.supportsPropertyIndex](index)) {
  363. const indexedValue = target[implSymbol].item(index);
  364. return {
  365. writable: false,
  366. enumerable: true,
  367. configurable: true,
  368. value: utils.tryWrapperForImpl(indexedValue)
  369. };
  370. }
  371. ignoreNamedProps = true;
  372. }
  373. return Reflect.getOwnPropertyDescriptor(target, P);
  374. }
  375. set(target, P, V, receiver) {
  376. if (typeof P === "symbol") {
  377. return Reflect.set(target, P, V, receiver);
  378. }
  379. // The `receiver` argument refers to the Proxy exotic object or an object
  380. // that inherits from it, whereas `target` refers to the Proxy target:
  381. if (target[implSymbol][utils.wrapperSymbol] === receiver) {
  382. const globalObject = this._globalObject;
  383. }
  384. let ownDesc;
  385. if (utils.isArrayIndexPropName(P)) {
  386. const index = P >>> 0;
  387. if (target[implSymbol][utils.supportsPropertyIndex](index)) {
  388. const indexedValue = target[implSymbol].item(index);
  389. ownDesc = {
  390. writable: false,
  391. enumerable: true,
  392. configurable: true,
  393. value: utils.tryWrapperForImpl(indexedValue)
  394. };
  395. }
  396. }
  397. if (ownDesc === undefined) {
  398. ownDesc = Reflect.getOwnPropertyDescriptor(target, P);
  399. }
  400. return utils.ordinarySetWithOwnDescriptor(target, P, V, receiver, ownDesc);
  401. }
  402. defineProperty(target, P, desc) {
  403. if (typeof P === "symbol") {
  404. return Reflect.defineProperty(target, P, desc);
  405. }
  406. const globalObject = this._globalObject;
  407. if (utils.isArrayIndexPropName(P)) {
  408. return false;
  409. }
  410. return Reflect.defineProperty(target, P, desc);
  411. }
  412. deleteProperty(target, P) {
  413. if (typeof P === "symbol") {
  414. return Reflect.deleteProperty(target, P);
  415. }
  416. const globalObject = this._globalObject;
  417. if (utils.isArrayIndexPropName(P)) {
  418. const index = P >>> 0;
  419. return !target[implSymbol][utils.supportsPropertyIndex](index);
  420. }
  421. return Reflect.deleteProperty(target, P);
  422. }
  423. preventExtensions() {
  424. return false;
  425. }
  426. }
  427. const Impl = require("../../jsdom/living/css/CSSStyleDeclaration-impl.js");