ValidityState.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 interfaceName = "ValidityState";
  7. exports.is = value => {
  8. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  9. };
  10. exports.isImpl = value => {
  11. return utils.isObject(value) && value instanceof Impl.implementation;
  12. };
  13. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  14. if (exports.is(value)) {
  15. return utils.implForWrapper(value);
  16. }
  17. throw new globalObject.TypeError(`${context} is not of type 'ValidityState'.`);
  18. };
  19. function makeWrapper(globalObject, newTarget) {
  20. let proto;
  21. if (newTarget !== undefined) {
  22. proto = newTarget.prototype;
  23. }
  24. if (!utils.isObject(proto)) {
  25. proto = globalObject[ctorRegistrySymbol]["ValidityState"].prototype;
  26. }
  27. return Object.create(proto);
  28. }
  29. exports.create = (globalObject, constructorArgs, privateData) => {
  30. const wrapper = makeWrapper(globalObject);
  31. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  32. };
  33. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  34. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  35. return utils.implForWrapper(wrapper);
  36. };
  37. exports._internalSetup = (wrapper, globalObject) => {};
  38. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  39. privateData.wrapper = wrapper;
  40. exports._internalSetup(wrapper, globalObject);
  41. Object.defineProperty(wrapper, implSymbol, {
  42. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  43. configurable: true
  44. });
  45. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  46. if (Impl.init) {
  47. Impl.init(wrapper[implSymbol]);
  48. }
  49. return wrapper;
  50. };
  51. exports.new = (globalObject, newTarget) => {
  52. const wrapper = makeWrapper(globalObject, newTarget);
  53. exports._internalSetup(wrapper, globalObject);
  54. Object.defineProperty(wrapper, implSymbol, {
  55. value: Object.create(Impl.implementation.prototype),
  56. configurable: true
  57. });
  58. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  59. if (Impl.init) {
  60. Impl.init(wrapper[implSymbol]);
  61. }
  62. return wrapper[implSymbol];
  63. };
  64. const exposed = new Set(["Window"]);
  65. exports.install = (globalObject, globalNames) => {
  66. if (!globalNames.some(globalName => exposed.has(globalName))) {
  67. return;
  68. }
  69. const ctorRegistry = utils.initCtorRegistry(globalObject);
  70. class ValidityState {
  71. constructor() {
  72. throw new globalObject.TypeError("Illegal constructor");
  73. }
  74. get valueMissing() {
  75. const esValue = this !== null && this !== undefined ? this : globalObject;
  76. if (!exports.is(esValue)) {
  77. throw new globalObject.TypeError(
  78. "'get valueMissing' called on an object that is not a valid instance of ValidityState."
  79. );
  80. }
  81. return esValue[implSymbol]["valueMissing"];
  82. }
  83. get typeMismatch() {
  84. const esValue = this !== null && this !== undefined ? this : globalObject;
  85. if (!exports.is(esValue)) {
  86. throw new globalObject.TypeError(
  87. "'get typeMismatch' called on an object that is not a valid instance of ValidityState."
  88. );
  89. }
  90. return esValue[implSymbol]["typeMismatch"];
  91. }
  92. get patternMismatch() {
  93. const esValue = this !== null && this !== undefined ? this : globalObject;
  94. if (!exports.is(esValue)) {
  95. throw new globalObject.TypeError(
  96. "'get patternMismatch' called on an object that is not a valid instance of ValidityState."
  97. );
  98. }
  99. return esValue[implSymbol]["patternMismatch"];
  100. }
  101. get tooLong() {
  102. const esValue = this !== null && this !== undefined ? this : globalObject;
  103. if (!exports.is(esValue)) {
  104. throw new globalObject.TypeError(
  105. "'get tooLong' called on an object that is not a valid instance of ValidityState."
  106. );
  107. }
  108. return esValue[implSymbol]["tooLong"];
  109. }
  110. get tooShort() {
  111. const esValue = this !== null && this !== undefined ? this : globalObject;
  112. if (!exports.is(esValue)) {
  113. throw new globalObject.TypeError(
  114. "'get tooShort' called on an object that is not a valid instance of ValidityState."
  115. );
  116. }
  117. return esValue[implSymbol]["tooShort"];
  118. }
  119. get rangeUnderflow() {
  120. const esValue = this !== null && this !== undefined ? this : globalObject;
  121. if (!exports.is(esValue)) {
  122. throw new globalObject.TypeError(
  123. "'get rangeUnderflow' called on an object that is not a valid instance of ValidityState."
  124. );
  125. }
  126. return esValue[implSymbol]["rangeUnderflow"];
  127. }
  128. get rangeOverflow() {
  129. const esValue = this !== null && this !== undefined ? this : globalObject;
  130. if (!exports.is(esValue)) {
  131. throw new globalObject.TypeError(
  132. "'get rangeOverflow' called on an object that is not a valid instance of ValidityState."
  133. );
  134. }
  135. return esValue[implSymbol]["rangeOverflow"];
  136. }
  137. get stepMismatch() {
  138. const esValue = this !== null && this !== undefined ? this : globalObject;
  139. if (!exports.is(esValue)) {
  140. throw new globalObject.TypeError(
  141. "'get stepMismatch' called on an object that is not a valid instance of ValidityState."
  142. );
  143. }
  144. return esValue[implSymbol]["stepMismatch"];
  145. }
  146. get badInput() {
  147. const esValue = this !== null && this !== undefined ? this : globalObject;
  148. if (!exports.is(esValue)) {
  149. throw new globalObject.TypeError(
  150. "'get badInput' called on an object that is not a valid instance of ValidityState."
  151. );
  152. }
  153. return esValue[implSymbol]["badInput"];
  154. }
  155. get customError() {
  156. const esValue = this !== null && this !== undefined ? this : globalObject;
  157. if (!exports.is(esValue)) {
  158. throw new globalObject.TypeError(
  159. "'get customError' called on an object that is not a valid instance of ValidityState."
  160. );
  161. }
  162. return esValue[implSymbol]["customError"];
  163. }
  164. get valid() {
  165. const esValue = this !== null && this !== undefined ? this : globalObject;
  166. if (!exports.is(esValue)) {
  167. throw new globalObject.TypeError(
  168. "'get valid' called on an object that is not a valid instance of ValidityState."
  169. );
  170. }
  171. return esValue[implSymbol]["valid"];
  172. }
  173. }
  174. Object.defineProperties(ValidityState.prototype, {
  175. valueMissing: { enumerable: true },
  176. typeMismatch: { enumerable: true },
  177. patternMismatch: { enumerable: true },
  178. tooLong: { enumerable: true },
  179. tooShort: { enumerable: true },
  180. rangeUnderflow: { enumerable: true },
  181. rangeOverflow: { enumerable: true },
  182. stepMismatch: { enumerable: true },
  183. badInput: { enumerable: true },
  184. customError: { enumerable: true },
  185. valid: { enumerable: true },
  186. [Symbol.toStringTag]: { value: "ValidityState", configurable: true }
  187. });
  188. ctorRegistry[interfaceName] = ValidityState;
  189. Object.defineProperty(globalObject, interfaceName, {
  190. configurable: true,
  191. writable: true,
  192. value: ValidityState
  193. });
  194. };
  195. const Impl = require("../../jsdom/living/constraint-validation/ValidityState-impl.js");