Location.js 13 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 interfaceName = "Location";
  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 'Location'.`);
  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]["Location"].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. function getUnforgeables(globalObject) {
  38. let unforgeables = unforgeablesMap.get(globalObject);
  39. if (unforgeables === undefined) {
  40. unforgeables = Object.create(null);
  41. utils.define(unforgeables, {
  42. assign(url) {
  43. const esValue = this !== null && this !== undefined ? this : globalObject;
  44. if (!exports.is(esValue)) {
  45. throw new globalObject.TypeError("'assign' called on an object that is not a valid instance of Location.");
  46. }
  47. if (arguments.length < 1) {
  48. throw new globalObject.TypeError(
  49. `Failed to execute 'assign' on 'Location': 1 argument required, but only ${arguments.length} present.`
  50. );
  51. }
  52. const args = [];
  53. {
  54. let curArg = arguments[0];
  55. curArg = conversions["USVString"](curArg, {
  56. context: "Failed to execute 'assign' on 'Location': parameter 1",
  57. globals: globalObject
  58. });
  59. args.push(curArg);
  60. }
  61. return esValue[implSymbol].assign(...args);
  62. },
  63. replace(url) {
  64. const esValue = this !== null && this !== undefined ? this : globalObject;
  65. if (!exports.is(esValue)) {
  66. throw new globalObject.TypeError("'replace' called on an object that is not a valid instance of Location.");
  67. }
  68. if (arguments.length < 1) {
  69. throw new globalObject.TypeError(
  70. `Failed to execute 'replace' on 'Location': 1 argument required, but only ${arguments.length} present.`
  71. );
  72. }
  73. const args = [];
  74. {
  75. let curArg = arguments[0];
  76. curArg = conversions["USVString"](curArg, {
  77. context: "Failed to execute 'replace' on 'Location': parameter 1",
  78. globals: globalObject
  79. });
  80. args.push(curArg);
  81. }
  82. return esValue[implSymbol].replace(...args);
  83. },
  84. reload() {
  85. const esValue = this !== null && this !== undefined ? this : globalObject;
  86. if (!exports.is(esValue)) {
  87. throw new globalObject.TypeError("'reload' called on an object that is not a valid instance of Location.");
  88. }
  89. return esValue[implSymbol].reload();
  90. },
  91. get href() {
  92. const esValue = this !== null && this !== undefined ? this : globalObject;
  93. if (!exports.is(esValue)) {
  94. throw new globalObject.TypeError("'get href' called on an object that is not a valid instance of Location.");
  95. }
  96. return esValue[implSymbol]["href"];
  97. },
  98. set href(V) {
  99. const esValue = this !== null && this !== undefined ? this : globalObject;
  100. if (!exports.is(esValue)) {
  101. throw new globalObject.TypeError("'set href' called on an object that is not a valid instance of Location.");
  102. }
  103. V = conversions["USVString"](V, {
  104. context: "Failed to set the 'href' property on 'Location': The provided value",
  105. globals: globalObject
  106. });
  107. esValue[implSymbol]["href"] = V;
  108. },
  109. toString() {
  110. const esValue = this;
  111. if (!exports.is(esValue)) {
  112. throw new globalObject.TypeError("'toString' called on an object that is not a valid instance of Location.");
  113. }
  114. return esValue[implSymbol]["href"];
  115. },
  116. get origin() {
  117. const esValue = this !== null && this !== undefined ? this : globalObject;
  118. if (!exports.is(esValue)) {
  119. throw new globalObject.TypeError(
  120. "'get origin' called on an object that is not a valid instance of Location."
  121. );
  122. }
  123. return esValue[implSymbol]["origin"];
  124. },
  125. get protocol() {
  126. const esValue = this !== null && this !== undefined ? this : globalObject;
  127. if (!exports.is(esValue)) {
  128. throw new globalObject.TypeError(
  129. "'get protocol' called on an object that is not a valid instance of Location."
  130. );
  131. }
  132. return esValue[implSymbol]["protocol"];
  133. },
  134. set protocol(V) {
  135. const esValue = this !== null && this !== undefined ? this : globalObject;
  136. if (!exports.is(esValue)) {
  137. throw new globalObject.TypeError(
  138. "'set protocol' called on an object that is not a valid instance of Location."
  139. );
  140. }
  141. V = conversions["USVString"](V, {
  142. context: "Failed to set the 'protocol' property on 'Location': The provided value",
  143. globals: globalObject
  144. });
  145. esValue[implSymbol]["protocol"] = V;
  146. },
  147. get host() {
  148. const esValue = this !== null && this !== undefined ? this : globalObject;
  149. if (!exports.is(esValue)) {
  150. throw new globalObject.TypeError("'get host' called on an object that is not a valid instance of Location.");
  151. }
  152. return esValue[implSymbol]["host"];
  153. },
  154. set host(V) {
  155. const esValue = this !== null && this !== undefined ? this : globalObject;
  156. if (!exports.is(esValue)) {
  157. throw new globalObject.TypeError("'set host' called on an object that is not a valid instance of Location.");
  158. }
  159. V = conversions["USVString"](V, {
  160. context: "Failed to set the 'host' property on 'Location': The provided value",
  161. globals: globalObject
  162. });
  163. esValue[implSymbol]["host"] = V;
  164. },
  165. get hostname() {
  166. const esValue = this !== null && this !== undefined ? this : globalObject;
  167. if (!exports.is(esValue)) {
  168. throw new globalObject.TypeError(
  169. "'get hostname' called on an object that is not a valid instance of Location."
  170. );
  171. }
  172. return esValue[implSymbol]["hostname"];
  173. },
  174. set hostname(V) {
  175. const esValue = this !== null && this !== undefined ? this : globalObject;
  176. if (!exports.is(esValue)) {
  177. throw new globalObject.TypeError(
  178. "'set hostname' called on an object that is not a valid instance of Location."
  179. );
  180. }
  181. V = conversions["USVString"](V, {
  182. context: "Failed to set the 'hostname' property on 'Location': The provided value",
  183. globals: globalObject
  184. });
  185. esValue[implSymbol]["hostname"] = V;
  186. },
  187. get port() {
  188. const esValue = this !== null && this !== undefined ? this : globalObject;
  189. if (!exports.is(esValue)) {
  190. throw new globalObject.TypeError("'get port' called on an object that is not a valid instance of Location.");
  191. }
  192. return esValue[implSymbol]["port"];
  193. },
  194. set port(V) {
  195. const esValue = this !== null && this !== undefined ? this : globalObject;
  196. if (!exports.is(esValue)) {
  197. throw new globalObject.TypeError("'set port' called on an object that is not a valid instance of Location.");
  198. }
  199. V = conversions["USVString"](V, {
  200. context: "Failed to set the 'port' property on 'Location': The provided value",
  201. globals: globalObject
  202. });
  203. esValue[implSymbol]["port"] = V;
  204. },
  205. get pathname() {
  206. const esValue = this !== null && this !== undefined ? this : globalObject;
  207. if (!exports.is(esValue)) {
  208. throw new globalObject.TypeError(
  209. "'get pathname' called on an object that is not a valid instance of Location."
  210. );
  211. }
  212. return esValue[implSymbol]["pathname"];
  213. },
  214. set pathname(V) {
  215. const esValue = this !== null && this !== undefined ? this : globalObject;
  216. if (!exports.is(esValue)) {
  217. throw new globalObject.TypeError(
  218. "'set pathname' called on an object that is not a valid instance of Location."
  219. );
  220. }
  221. V = conversions["USVString"](V, {
  222. context: "Failed to set the 'pathname' property on 'Location': The provided value",
  223. globals: globalObject
  224. });
  225. esValue[implSymbol]["pathname"] = V;
  226. },
  227. get search() {
  228. const esValue = this !== null && this !== undefined ? this : globalObject;
  229. if (!exports.is(esValue)) {
  230. throw new globalObject.TypeError(
  231. "'get search' called on an object that is not a valid instance of Location."
  232. );
  233. }
  234. return esValue[implSymbol]["search"];
  235. },
  236. set search(V) {
  237. const esValue = this !== null && this !== undefined ? this : globalObject;
  238. if (!exports.is(esValue)) {
  239. throw new globalObject.TypeError(
  240. "'set search' called on an object that is not a valid instance of Location."
  241. );
  242. }
  243. V = conversions["USVString"](V, {
  244. context: "Failed to set the 'search' property on 'Location': The provided value",
  245. globals: globalObject
  246. });
  247. esValue[implSymbol]["search"] = V;
  248. },
  249. get hash() {
  250. const esValue = this !== null && this !== undefined ? this : globalObject;
  251. if (!exports.is(esValue)) {
  252. throw new globalObject.TypeError("'get hash' called on an object that is not a valid instance of Location.");
  253. }
  254. return esValue[implSymbol]["hash"];
  255. },
  256. set hash(V) {
  257. const esValue = this !== null && this !== undefined ? this : globalObject;
  258. if (!exports.is(esValue)) {
  259. throw new globalObject.TypeError("'set hash' called on an object that is not a valid instance of Location.");
  260. }
  261. V = conversions["USVString"](V, {
  262. context: "Failed to set the 'hash' property on 'Location': The provided value",
  263. globals: globalObject
  264. });
  265. esValue[implSymbol]["hash"] = V;
  266. }
  267. });
  268. Object.defineProperties(unforgeables, {
  269. assign: { configurable: false, writable: false },
  270. replace: { configurable: false, writable: false },
  271. reload: { configurable: false, writable: false },
  272. href: { configurable: false },
  273. toString: { configurable: false, writable: false },
  274. origin: { configurable: false },
  275. protocol: { configurable: false },
  276. host: { configurable: false },
  277. hostname: { configurable: false },
  278. port: { configurable: false },
  279. pathname: { configurable: false },
  280. search: { configurable: false },
  281. hash: { configurable: false }
  282. });
  283. unforgeablesMap.set(globalObject, unforgeables);
  284. }
  285. return unforgeables;
  286. }
  287. exports._internalSetup = (wrapper, globalObject) => {
  288. utils.define(wrapper, getUnforgeables(globalObject));
  289. };
  290. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  291. privateData.wrapper = wrapper;
  292. exports._internalSetup(wrapper, globalObject);
  293. Object.defineProperty(wrapper, implSymbol, {
  294. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  295. configurable: true
  296. });
  297. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  298. if (Impl.init) {
  299. Impl.init(wrapper[implSymbol]);
  300. }
  301. return wrapper;
  302. };
  303. exports.new = (globalObject, newTarget) => {
  304. const wrapper = makeWrapper(globalObject, newTarget);
  305. exports._internalSetup(wrapper, globalObject);
  306. Object.defineProperty(wrapper, implSymbol, {
  307. value: Object.create(Impl.implementation.prototype),
  308. configurable: true
  309. });
  310. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  311. if (Impl.init) {
  312. Impl.init(wrapper[implSymbol]);
  313. }
  314. return wrapper[implSymbol];
  315. };
  316. const unforgeablesMap = new WeakMap();
  317. const exposed = new Set(["Window"]);
  318. exports.install = (globalObject, globalNames) => {
  319. if (!globalNames.some(globalName => exposed.has(globalName))) {
  320. return;
  321. }
  322. const ctorRegistry = utils.initCtorRegistry(globalObject);
  323. class Location {
  324. constructor() {
  325. throw new globalObject.TypeError("Illegal constructor");
  326. }
  327. }
  328. Object.defineProperties(Location.prototype, { [Symbol.toStringTag]: { value: "Location", configurable: true } });
  329. ctorRegistry[interfaceName] = Location;
  330. Object.defineProperty(globalObject, interfaceName, {
  331. configurable: true,
  332. writable: true,
  333. value: Location
  334. });
  335. };
  336. const Impl = require("../../jsdom/living/window/Location-impl.js");