CSSMediaRule.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 CSSConditionRule = require("./CSSConditionRule.js");
  7. const interfaceName = "CSSMediaRule";
  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 'CSSMediaRule'.`);
  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]["CSSMediaRule"].prototype;
  27. }
  28. return Object.create(proto);
  29. }
  30. exports.create = (globalObject, constructorArgs, privateData) => {
  31. const wrapper = makeWrapper(globalObject);
  32. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  33. };
  34. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  35. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  36. return utils.implForWrapper(wrapper);
  37. };
  38. exports._internalSetup = (wrapper, globalObject) => {
  39. CSSConditionRule._internalSetup(wrapper, globalObject);
  40. };
  41. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  42. privateData.wrapper = wrapper;
  43. exports._internalSetup(wrapper, globalObject);
  44. Object.defineProperty(wrapper, implSymbol, {
  45. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  46. configurable: true
  47. });
  48. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  49. if (Impl.init) {
  50. Impl.init(wrapper[implSymbol]);
  51. }
  52. return wrapper;
  53. };
  54. exports.new = (globalObject, newTarget) => {
  55. const wrapper = makeWrapper(globalObject, newTarget);
  56. exports._internalSetup(wrapper, globalObject);
  57. Object.defineProperty(wrapper, implSymbol, {
  58. value: Object.create(Impl.implementation.prototype),
  59. configurable: true
  60. });
  61. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  62. if (Impl.init) {
  63. Impl.init(wrapper[implSymbol]);
  64. }
  65. return wrapper[implSymbol];
  66. };
  67. const exposed = new Set(["Window"]);
  68. exports.install = (globalObject, globalNames) => {
  69. if (!globalNames.some(globalName => exposed.has(globalName))) {
  70. return;
  71. }
  72. const ctorRegistry = utils.initCtorRegistry(globalObject);
  73. class CSSMediaRule extends globalObject.CSSConditionRule {
  74. constructor() {
  75. throw new globalObject.TypeError("Illegal constructor");
  76. }
  77. get media() {
  78. const esValue = this !== null && this !== undefined ? this : globalObject;
  79. if (!exports.is(esValue)) {
  80. throw new globalObject.TypeError(
  81. "'get media' called on an object that is not a valid instance of CSSMediaRule."
  82. );
  83. }
  84. return utils.getSameObject(this, "media", () => {
  85. return utils.tryWrapperForImpl(esValue[implSymbol]["media"]);
  86. });
  87. }
  88. set media(V) {
  89. const esValue = this !== null && this !== undefined ? this : globalObject;
  90. if (!exports.is(esValue)) {
  91. throw new globalObject.TypeError(
  92. "'set media' called on an object that is not a valid instance of CSSMediaRule."
  93. );
  94. }
  95. const Q = esValue["media"];
  96. if (!utils.isObject(Q)) {
  97. throw new globalObject.TypeError("Property 'media' is not an object");
  98. }
  99. Reflect.set(Q, "mediaText", V);
  100. }
  101. get matches() {
  102. const esValue = this !== null && this !== undefined ? this : globalObject;
  103. if (!exports.is(esValue)) {
  104. throw new globalObject.TypeError(
  105. "'get matches' called on an object that is not a valid instance of CSSMediaRule."
  106. );
  107. }
  108. return esValue[implSymbol]["matches"];
  109. }
  110. }
  111. Object.defineProperties(CSSMediaRule.prototype, {
  112. media: { enumerable: true },
  113. matches: { enumerable: true },
  114. [Symbol.toStringTag]: { value: "CSSMediaRule", configurable: true }
  115. });
  116. ctorRegistry[interfaceName] = CSSMediaRule;
  117. Object.defineProperty(globalObject, interfaceName, {
  118. configurable: true,
  119. writable: true,
  120. value: CSSMediaRule
  121. });
  122. };
  123. const Impl = require("../../jsdom/living/css/CSSMediaRule-impl.js");