CSSStyleSheet.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const CSSStyleSheetInit = require("./CSSStyleSheetInit.js");
  5. const implSymbol = utils.implSymbol;
  6. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  7. const StyleSheet = require("./StyleSheet.js");
  8. const interfaceName = "CSSStyleSheet";
  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 'CSSStyleSheet'.`);
  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]["CSSStyleSheet"].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. StyleSheet._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"]);
  69. exports.install = (globalObject, globalNames) => {
  70. if (!globalNames.some(globalName => exposed.has(globalName))) {
  71. return;
  72. }
  73. const ctorRegistry = utils.initCtorRegistry(globalObject);
  74. class CSSStyleSheet extends globalObject.StyleSheet {
  75. constructor() {
  76. const args = [];
  77. {
  78. let curArg = arguments[0];
  79. curArg = CSSStyleSheetInit.convert(globalObject, curArg, {
  80. context: "Failed to construct 'CSSStyleSheet': parameter 1"
  81. });
  82. args.push(curArg);
  83. }
  84. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  85. }
  86. insertRule(rule) {
  87. const esValue = this !== null && this !== undefined ? this : globalObject;
  88. if (!exports.is(esValue)) {
  89. throw new globalObject.TypeError(
  90. "'insertRule' called on an object that is not a valid instance of CSSStyleSheet."
  91. );
  92. }
  93. if (arguments.length < 1) {
  94. throw new globalObject.TypeError(
  95. `Failed to execute 'insertRule' on 'CSSStyleSheet': 1 argument required, but only ${arguments.length} present.`
  96. );
  97. }
  98. const args = [];
  99. {
  100. let curArg = arguments[0];
  101. curArg = conversions["DOMString"](curArg, {
  102. context: "Failed to execute 'insertRule' on 'CSSStyleSheet': parameter 1",
  103. globals: globalObject
  104. });
  105. args.push(curArg);
  106. }
  107. {
  108. let curArg = arguments[1];
  109. if (curArg !== undefined) {
  110. curArg = conversions["unsigned long"](curArg, {
  111. context: "Failed to execute 'insertRule' on 'CSSStyleSheet': parameter 2",
  112. globals: globalObject
  113. });
  114. } else {
  115. curArg = 0;
  116. }
  117. args.push(curArg);
  118. }
  119. return esValue[implSymbol].insertRule(...args);
  120. }
  121. deleteRule(index) {
  122. const esValue = this !== null && this !== undefined ? this : globalObject;
  123. if (!exports.is(esValue)) {
  124. throw new globalObject.TypeError(
  125. "'deleteRule' called on an object that is not a valid instance of CSSStyleSheet."
  126. );
  127. }
  128. if (arguments.length < 1) {
  129. throw new globalObject.TypeError(
  130. `Failed to execute 'deleteRule' on 'CSSStyleSheet': 1 argument required, but only ${arguments.length} present.`
  131. );
  132. }
  133. const args = [];
  134. {
  135. let curArg = arguments[0];
  136. curArg = conversions["unsigned long"](curArg, {
  137. context: "Failed to execute 'deleteRule' on 'CSSStyleSheet': parameter 1",
  138. globals: globalObject
  139. });
  140. args.push(curArg);
  141. }
  142. return esValue[implSymbol].deleteRule(...args);
  143. }
  144. replace(text) {
  145. try {
  146. const esValue = this !== null && this !== undefined ? this : globalObject;
  147. if (!exports.is(esValue)) {
  148. throw new globalObject.TypeError(
  149. "'replace' called on an object that is not a valid instance of CSSStyleSheet."
  150. );
  151. }
  152. if (arguments.length < 1) {
  153. throw new globalObject.TypeError(
  154. `Failed to execute 'replace' on 'CSSStyleSheet': 1 argument required, but only ${arguments.length} present.`
  155. );
  156. }
  157. const args = [];
  158. {
  159. let curArg = arguments[0];
  160. curArg = conversions["USVString"](curArg, {
  161. context: "Failed to execute 'replace' on 'CSSStyleSheet': parameter 1",
  162. globals: globalObject
  163. });
  164. args.push(curArg);
  165. }
  166. return utils.tryWrapperForImpl(esValue[implSymbol].replace(...args));
  167. } catch (e) {
  168. return globalObject.Promise.reject(e);
  169. }
  170. }
  171. replaceSync(text) {
  172. const esValue = this !== null && this !== undefined ? this : globalObject;
  173. if (!exports.is(esValue)) {
  174. throw new globalObject.TypeError(
  175. "'replaceSync' called on an object that is not a valid instance of CSSStyleSheet."
  176. );
  177. }
  178. if (arguments.length < 1) {
  179. throw new globalObject.TypeError(
  180. `Failed to execute 'replaceSync' on 'CSSStyleSheet': 1 argument required, but only ${arguments.length} present.`
  181. );
  182. }
  183. const args = [];
  184. {
  185. let curArg = arguments[0];
  186. curArg = conversions["USVString"](curArg, {
  187. context: "Failed to execute 'replaceSync' on 'CSSStyleSheet': parameter 1",
  188. globals: globalObject
  189. });
  190. args.push(curArg);
  191. }
  192. return esValue[implSymbol].replaceSync(...args);
  193. }
  194. addRule() {
  195. const esValue = this !== null && this !== undefined ? this : globalObject;
  196. if (!exports.is(esValue)) {
  197. throw new globalObject.TypeError(
  198. "'addRule' called on an object that is not a valid instance of CSSStyleSheet."
  199. );
  200. }
  201. const args = [];
  202. {
  203. let curArg = arguments[0];
  204. if (curArg !== undefined) {
  205. curArg = conversions["DOMString"](curArg, {
  206. context: "Failed to execute 'addRule' on 'CSSStyleSheet': parameter 1",
  207. globals: globalObject
  208. });
  209. } else {
  210. curArg = "undefined";
  211. }
  212. args.push(curArg);
  213. }
  214. {
  215. let curArg = arguments[1];
  216. if (curArg !== undefined) {
  217. curArg = conversions["DOMString"](curArg, {
  218. context: "Failed to execute 'addRule' on 'CSSStyleSheet': parameter 2",
  219. globals: globalObject
  220. });
  221. } else {
  222. curArg = "undefined";
  223. }
  224. args.push(curArg);
  225. }
  226. {
  227. let curArg = arguments[2];
  228. if (curArg !== undefined) {
  229. curArg = conversions["unsigned long"](curArg, {
  230. context: "Failed to execute 'addRule' on 'CSSStyleSheet': parameter 3",
  231. globals: globalObject
  232. });
  233. }
  234. args.push(curArg);
  235. }
  236. return esValue[implSymbol].addRule(...args);
  237. }
  238. removeRule() {
  239. const esValue = this !== null && this !== undefined ? this : globalObject;
  240. if (!exports.is(esValue)) {
  241. throw new globalObject.TypeError(
  242. "'removeRule' called on an object that is not a valid instance of CSSStyleSheet."
  243. );
  244. }
  245. const args = [];
  246. {
  247. let curArg = arguments[0];
  248. if (curArg !== undefined) {
  249. curArg = conversions["unsigned long"](curArg, {
  250. context: "Failed to execute 'removeRule' on 'CSSStyleSheet': parameter 1",
  251. globals: globalObject
  252. });
  253. } else {
  254. curArg = 0;
  255. }
  256. args.push(curArg);
  257. }
  258. return esValue[implSymbol].removeRule(...args);
  259. }
  260. get ownerRule() {
  261. const esValue = this !== null && this !== undefined ? this : globalObject;
  262. if (!exports.is(esValue)) {
  263. throw new globalObject.TypeError(
  264. "'get ownerRule' called on an object that is not a valid instance of CSSStyleSheet."
  265. );
  266. }
  267. return utils.tryWrapperForImpl(esValue[implSymbol]["ownerRule"]);
  268. }
  269. get cssRules() {
  270. const esValue = this !== null && this !== undefined ? this : globalObject;
  271. if (!exports.is(esValue)) {
  272. throw new globalObject.TypeError(
  273. "'get cssRules' called on an object that is not a valid instance of CSSStyleSheet."
  274. );
  275. }
  276. return utils.getSameObject(this, "cssRules", () => {
  277. return utils.tryWrapperForImpl(esValue[implSymbol]["cssRules"]);
  278. });
  279. }
  280. get rules() {
  281. const esValue = this !== null && this !== undefined ? this : globalObject;
  282. if (!exports.is(esValue)) {
  283. throw new globalObject.TypeError(
  284. "'get rules' called on an object that is not a valid instance of CSSStyleSheet."
  285. );
  286. }
  287. return utils.getSameObject(this, "rules", () => {
  288. return utils.tryWrapperForImpl(esValue[implSymbol]["rules"]);
  289. });
  290. }
  291. }
  292. Object.defineProperties(CSSStyleSheet.prototype, {
  293. insertRule: { enumerable: true },
  294. deleteRule: { enumerable: true },
  295. replace: { enumerable: true },
  296. replaceSync: { enumerable: true },
  297. addRule: { enumerable: true },
  298. removeRule: { enumerable: true },
  299. ownerRule: { enumerable: true },
  300. cssRules: { enumerable: true },
  301. rules: { enumerable: true },
  302. [Symbol.toStringTag]: { value: "CSSStyleSheet", configurable: true }
  303. });
  304. ctorRegistry[interfaceName] = CSSStyleSheet;
  305. Object.defineProperty(globalObject, interfaceName, {
  306. configurable: true,
  307. writable: true,
  308. value: CSSStyleSheet
  309. });
  310. };
  311. const Impl = require("../../jsdom/living/css/CSSStyleSheet-impl.js");