版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

655 行
29 KiB

  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. const CustomElementRegistry_1 = __importDefault(require("../custom-element/CustomElementRegistry"));
  30. const Document_1 = __importDefault(require("../nodes/document/Document"));
  31. const HTMLDocument_1 = __importDefault(require("../nodes/html-document/HTMLDocument"));
  32. const XMLDocument_1 = __importDefault(require("../nodes/xml-document/XMLDocument"));
  33. const SVGDocument_1 = __importDefault(require("../nodes/svg-document/SVGDocument"));
  34. const Node_1 = __importDefault(require("../nodes/node/Node"));
  35. const NodeFilter_1 = __importDefault(require("../tree-walker/NodeFilter"));
  36. const Text_1 = __importDefault(require("../nodes/text/Text"));
  37. const Comment_1 = __importDefault(require("../nodes/comment/Comment"));
  38. const ShadowRoot_1 = __importDefault(require("../nodes/shadow-root/ShadowRoot"));
  39. const Element_1 = __importDefault(require("../nodes/element/Element"));
  40. const HTMLTemplateElement_1 = __importDefault(require("../nodes/html-template-element/HTMLTemplateElement"));
  41. const HTMLFormElement_1 = __importDefault(require("../nodes/html-form-element/HTMLFormElement"));
  42. const HTMLElement_1 = __importDefault(require("../nodes/html-element/HTMLElement"));
  43. const HTMLUnknownElement_1 = __importDefault(require("../nodes/html-unknown-element/HTMLUnknownElement"));
  44. const HTMLInputElement_1 = __importDefault(require("../nodes/html-input-element/HTMLInputElement"));
  45. const HTMLSelectElement_1 = __importDefault(require("../nodes/html-select-element/HTMLSelectElement"));
  46. const HTMLTextAreaElement_1 = __importDefault(require("../nodes/html-text-area-element/HTMLTextAreaElement"));
  47. const HTMLLinkElement_1 = __importDefault(require("../nodes/html-link-element/HTMLLinkElement"));
  48. const HTMLStyleElement_1 = __importDefault(require("../nodes/html-style-element/HTMLStyleElement"));
  49. const HTMLSlotElement_1 = __importDefault(require("../nodes/html-slot-element/HTMLSlotElement"));
  50. const HTMLLabelElement_1 = __importDefault(require("../nodes/html-label-element/HTMLLabelElement"));
  51. const HTMLMetaElement_1 = __importDefault(require("../nodes/html-meta-element/HTMLMetaElement"));
  52. const HTMLMediaElement_1 = __importDefault(require("../nodes/html-media-element/HTMLMediaElement"));
  53. const HTMLAudioElement_1 = __importDefault(require("../nodes/html-audio-element/HTMLAudioElement"));
  54. const Audio_1 = __importDefault(require("../nodes/html-audio-element/Audio"));
  55. const HTMLVideoElement_1 = __importDefault(require("../nodes/html-video-element/HTMLVideoElement"));
  56. const HTMLBaseElement_1 = __importDefault(require("../nodes/html-base-element/HTMLBaseElement"));
  57. const HTMLIFrameElement_1 = __importDefault(require("../nodes/html-iframe-element/HTMLIFrameElement"));
  58. const HTMLDialogElement_1 = __importDefault(require("../nodes/html-dialog-element/HTMLDialogElement"));
  59. const SVGSVGElement_1 = __importDefault(require("../nodes/svg-element/SVGSVGElement"));
  60. const SVGElement_1 = __importDefault(require("../nodes/svg-element/SVGElement"));
  61. const SVGGraphicsElement_1 = __importDefault(require("../nodes/svg-element/SVGGraphicsElement"));
  62. const HTMLScriptElement_1 = __importDefault(require("../nodes/html-script-element/HTMLScriptElement"));
  63. const HTMLImageElement_1 = __importDefault(require("../nodes/html-image-element/HTMLImageElement"));
  64. const Image_1 = __importDefault(require("../nodes/html-image-element/Image"));
  65. const DocumentFragment_1 = __importDefault(require("../nodes/document-fragment/DocumentFragment"));
  66. const CharacterData_1 = __importDefault(require("../nodes/character-data/CharacterData"));
  67. const TreeWalker_1 = __importDefault(require("../tree-walker/TreeWalker"));
  68. const Event_1 = __importDefault(require("../event/Event"));
  69. const CustomEvent_1 = __importDefault(require("../event/events/CustomEvent"));
  70. const AnimationEvent_1 = __importDefault(require("../event/events/AnimationEvent"));
  71. const KeyboardEvent_1 = __importDefault(require("../event/events/KeyboardEvent"));
  72. const MessageEvent_1 = __importDefault(require("../event/events/MessageEvent"));
  73. const ProgressEvent_1 = __importDefault(require("../event/events/ProgressEvent"));
  74. const MediaQueryListEvent_1 = __importDefault(require("../event/events/MediaQueryListEvent"));
  75. const EventTarget_1 = __importDefault(require("../event/EventTarget"));
  76. const MessagePort_1 = __importDefault(require("../event/MessagePort"));
  77. const url_1 = require("url");
  78. const Location_1 = __importDefault(require("../location/Location"));
  79. const NonImplementedEventTypes_1 = __importDefault(require("../event/NonImplementedEventTypes"));
  80. const MutationObserver_1 = __importDefault(require("../mutation-observer/MutationObserver"));
  81. const NonImplemenetedElementClasses_1 = __importDefault(require("../config/NonImplemenetedElementClasses"));
  82. const DOMParser_1 = __importDefault(require("../dom-parser/DOMParser"));
  83. const XMLSerializer_1 = __importDefault(require("../xml-serializer/XMLSerializer"));
  84. const ResizeObserver_1 = __importDefault(require("../resize-observer/ResizeObserver"));
  85. const Blob_1 = __importDefault(require("../file/Blob"));
  86. const File_1 = __importDefault(require("../file/File"));
  87. const DOMException_1 = __importDefault(require("../exception/DOMException"));
  88. const FileReader_1 = __importDefault(require("../file/FileReader"));
  89. const History_1 = __importDefault(require("../history/History"));
  90. const CSSStyleSheet_1 = __importDefault(require("../css/CSSStyleSheet"));
  91. const CSSStyleDeclaration_1 = __importDefault(require("../css/declaration/CSSStyleDeclaration"));
  92. const CSS_1 = __importDefault(require("../css/CSS"));
  93. const CSSUnitValue_1 = __importDefault(require("../css/CSSUnitValue"));
  94. const CSSRule_1 = __importDefault(require("../css/CSSRule"));
  95. const CSSContainerRule_1 = __importDefault(require("../css/rules/CSSContainerRule"));
  96. const CSSFontFaceRule_1 = __importDefault(require("../css/rules/CSSFontFaceRule"));
  97. const CSSKeyframeRule_1 = __importDefault(require("../css/rules/CSSKeyframeRule"));
  98. const CSSKeyframesRule_1 = __importDefault(require("../css/rules/CSSKeyframesRule"));
  99. const CSSMediaRule_1 = __importDefault(require("../css/rules/CSSMediaRule"));
  100. const CSSStyleRule_1 = __importDefault(require("../css/rules/CSSStyleRule"));
  101. const MouseEvent_1 = __importDefault(require("../event/events/MouseEvent"));
  102. const PointerEvent_1 = __importDefault(require("../event/events/PointerEvent"));
  103. const FocusEvent_1 = __importDefault(require("../event/events/FocusEvent"));
  104. const WheelEvent_1 = __importDefault(require("../event/events/WheelEvent"));
  105. const DataTransfer_1 = __importDefault(require("../event/DataTransfer"));
  106. const DataTransferItem_1 = __importDefault(require("../event/DataTransferItem"));
  107. const DataTransferItemList_1 = __importDefault(require("../event/DataTransferItemList"));
  108. const InputEvent_1 = __importDefault(require("../event/events/InputEvent"));
  109. const UIEvent_1 = __importDefault(require("../event/UIEvent"));
  110. const ErrorEvent_1 = __importDefault(require("../event/events/ErrorEvent"));
  111. const StorageEvent_1 = __importDefault(require("../event/events/StorageEvent"));
  112. const Screen_1 = __importDefault(require("../screen/Screen"));
  113. const AsyncTaskManager_1 = __importDefault(require("../async-task-manager/AsyncTaskManager"));
  114. const Headers_1 = __importDefault(require("../fetch/Headers"));
  115. const Request_1 = __importDefault(require("../fetch/Request"));
  116. const Response_1 = __importDefault(require("../fetch/Response"));
  117. const Storage_1 = __importDefault(require("../storage/Storage"));
  118. const HTMLCollection_1 = __importDefault(require("../nodes/element/HTMLCollection"));
  119. const NodeList_1 = __importDefault(require("../nodes/node/NodeList"));
  120. const MediaQueryList_1 = __importDefault(require("../match-media/MediaQueryList"));
  121. const Selection_1 = __importDefault(require("../selection/Selection"));
  122. const Navigator_1 = __importDefault(require("../navigator/Navigator"));
  123. const MimeType_1 = __importDefault(require("../navigator/MimeType"));
  124. const MimeTypeArray_1 = __importDefault(require("../navigator/MimeTypeArray"));
  125. const Plugin_1 = __importDefault(require("../navigator/Plugin"));
  126. const PluginArray_1 = __importDefault(require("../navigator/PluginArray"));
  127. const FetchHandler_1 = __importDefault(require("../fetch/FetchHandler"));
  128. const Range_1 = __importDefault(require("../range/Range"));
  129. const VMGlobalPropertyScript_1 = __importDefault(require("./VMGlobalPropertyScript"));
  130. const PerfHooks = __importStar(require("perf_hooks"));
  131. const vm_1 = __importDefault(require("vm"));
  132. const buffer_1 = require("buffer");
  133. const XMLHttpRequest_1 = __importDefault(require("../xml-http-request/XMLHttpRequest"));
  134. const XMLHttpRequestUpload_1 = __importDefault(require("../xml-http-request/XMLHttpRequestUpload"));
  135. const XMLHttpRequestEventTarget_1 = __importDefault(require("../xml-http-request/XMLHttpRequestEventTarget"));
  136. const Base64_1 = __importDefault(require("../base64/Base64"));
  137. const Attr_1 = __importDefault(require("../nodes/attr/Attr"));
  138. const NamedNodeMap_1 = __importDefault(require("../named-node-map/NamedNodeMap"));
  139. const ProcessingInstruction_1 = __importDefault(require("../nodes/processing-instruction/ProcessingInstruction"));
  140. const FileList_1 = __importDefault(require("../nodes/html-input-element/FileList"));
  141. const DOMExceptionNameEnum_1 = __importDefault(require("../exception/DOMExceptionNameEnum"));
  142. const ORIGINAL_SET_TIMEOUT = setTimeout;
  143. const ORIGINAL_CLEAR_TIMEOUT = clearTimeout;
  144. const ORIGINAL_SET_INTERVAL = setInterval;
  145. const ORIGINAL_CLEAR_INTERVAL = clearInterval;
  146. /**
  147. * Browser window.
  148. *
  149. * Reference:
  150. * https://developer.mozilla.org/en-US/docs/Web/API/Window.
  151. */
  152. class Window extends EventTarget_1.default {
  153. /**
  154. * Constructor.
  155. *
  156. * @param [options] Options.
  157. * @param [options.innerWidth] Inner width. Defaults to "1024".
  158. * @param [options.innerHeight] Inner height. Defaults to "768".
  159. * @param [options.url] URL.
  160. * @param [options.settings] Settings.
  161. */
  162. constructor(options) {
  163. super();
  164. this.customElements = new CustomElementRegistry_1.default();
  165. this.location = new Location_1.default();
  166. this.navigator = new Navigator_1.default();
  167. this.history = new History_1.default();
  168. this.screen = new Screen_1.default();
  169. this.sessionStorage = new Storage_1.default();
  170. this.localStorage = new Storage_1.default();
  171. this.innerWidth = options?.innerWidth ? options.innerWidth : 1024;
  172. // The Happy DOM property
  173. this.happyDOM = {
  174. whenAsyncComplete: async () => {
  175. return await this.happyDOM.asyncTaskManager.whenComplete();
  176. },
  177. cancelAsync: () => {
  178. this.happyDOM.asyncTaskManager.cancelAll();
  179. },
  180. asyncTaskManager: new AsyncTaskManager_1.default(),
  181. setInnerWidth: (width) => {
  182. if (this.innerWidth !== width) {
  183. this.innerWidth = width;
  184. this.dispatchEvent(new Event_1.default('resize'));
  185. }
  186. },
  187. setInnerHeight: (height) => {
  188. if (this.innerHeight !== height) {
  189. this.innerHeight = height;
  190. this.dispatchEvent(new Event_1.default('resize'));
  191. }
  192. },
  193. setURL: (url) => {
  194. this.location.href = url;
  195. },
  196. settings: {
  197. disableJavaScriptEvaluation: false,
  198. disableJavaScriptFileLoading: false,
  199. disableCSSFileLoading: false,
  200. disableIframePageLoading: false,
  201. enableFileSystemHttpRequests: false
  202. }
  203. };
  204. // Global classes
  205. this.Node = Node_1.default;
  206. this.HTMLElement = HTMLElement_1.default;
  207. this.HTMLUnknownElement = HTMLUnknownElement_1.default;
  208. this.HTMLTemplateElement = HTMLTemplateElement_1.default;
  209. this.HTMLFormElement = HTMLFormElement_1.default;
  210. this.HTMLInputElement = HTMLInputElement_1.default;
  211. this.HTMLSelectElement = HTMLSelectElement_1.default;
  212. this.HTMLTextAreaElement = HTMLTextAreaElement_1.default;
  213. this.HTMLImageElement = HTMLImageElement_1.default;
  214. this.HTMLScriptElement = HTMLScriptElement_1.default;
  215. this.HTMLLinkElement = HTMLLinkElement_1.default;
  216. this.HTMLStyleElement = HTMLStyleElement_1.default;
  217. this.HTMLLabelElement = HTMLLabelElement_1.default;
  218. this.HTMLSlotElement = HTMLSlotElement_1.default;
  219. this.HTMLMetaElement = HTMLMetaElement_1.default;
  220. this.HTMLMediaElement = HTMLMediaElement_1.default;
  221. this.HTMLAudioElement = HTMLAudioElement_1.default;
  222. this.HTMLVideoElement = HTMLVideoElement_1.default;
  223. this.HTMLBaseElement = HTMLBaseElement_1.default;
  224. this.HTMLIFrameElement = HTMLIFrameElement_1.default;
  225. this.HTMLDialogElement = HTMLDialogElement_1.default;
  226. this.Attr = Attr_1.default;
  227. this.NamedNodeMap = NamedNodeMap_1.default;
  228. this.SVGSVGElement = SVGSVGElement_1.default;
  229. this.SVGElement = SVGElement_1.default;
  230. this.SVGGraphicsElement = SVGGraphicsElement_1.default;
  231. this.Text = Text_1.default;
  232. this.Comment = Comment_1.default;
  233. this.ShadowRoot = ShadowRoot_1.default;
  234. this.ProcessingInstruction = ProcessingInstruction_1.default;
  235. this.Element = Element_1.default;
  236. this.DocumentFragment = DocumentFragment_1.default;
  237. this.CharacterData = CharacterData_1.default;
  238. this.NodeFilter = NodeFilter_1.default;
  239. this.TreeWalker = TreeWalker_1.default;
  240. this.MutationObserver = MutationObserver_1.default;
  241. this.Document = Document_1.default;
  242. this.HTMLDocument = HTMLDocument_1.default;
  243. this.XMLDocument = XMLDocument_1.default;
  244. this.SVGDocument = SVGDocument_1.default;
  245. this.Event = Event_1.default;
  246. this.UIEvent = UIEvent_1.default;
  247. this.CustomEvent = CustomEvent_1.default;
  248. this.AnimationEvent = AnimationEvent_1.default;
  249. this.KeyboardEvent = KeyboardEvent_1.default;
  250. this.MessageEvent = MessageEvent_1.default;
  251. this.MouseEvent = MouseEvent_1.default;
  252. this.PointerEvent = PointerEvent_1.default;
  253. this.FocusEvent = FocusEvent_1.default;
  254. this.WheelEvent = WheelEvent_1.default;
  255. this.InputEvent = InputEvent_1.default;
  256. this.ErrorEvent = ErrorEvent_1.default;
  257. this.StorageEvent = StorageEvent_1.default;
  258. this.ProgressEvent = ProgressEvent_1.default;
  259. this.MediaQueryListEvent = MediaQueryListEvent_1.default;
  260. this.EventTarget = EventTarget_1.default;
  261. this.MessagePort = MessagePort_1.default;
  262. this.DataTransfer = DataTransfer_1.default;
  263. this.DataTransferItem = DataTransferItem_1.default;
  264. this.DataTransferItemList = DataTransferItemList_1.default;
  265. this.URL = url_1.URL;
  266. this.Location = Location_1.default;
  267. this.CustomElementRegistry = CustomElementRegistry_1.default;
  268. this.Window = this.constructor;
  269. this.XMLSerializer = XMLSerializer_1.default;
  270. this.ResizeObserver = ResizeObserver_1.default;
  271. this.CSSStyleSheet = CSSStyleSheet_1.default;
  272. this.Blob = Blob_1.default;
  273. this.File = File_1.default;
  274. this.DOMException = DOMException_1.default;
  275. this.History = History_1.default;
  276. this.Screen = Screen_1.default;
  277. this.Storage = Storage_1.default;
  278. this.URLSearchParams = url_1.URLSearchParams;
  279. this.HTMLCollection = HTMLCollection_1.default;
  280. this.NodeList = NodeList_1.default;
  281. this.CSSUnitValue = CSSUnitValue_1.default;
  282. this.CSSRule = CSSRule_1.default;
  283. this.CSSContainerRule = CSSContainerRule_1.default;
  284. this.CSSFontFaceRule = CSSFontFaceRule_1.default;
  285. this.CSSKeyframeRule = CSSKeyframeRule_1.default;
  286. this.CSSKeyframesRule = CSSKeyframesRule_1.default;
  287. this.CSSMediaRule = CSSMediaRule_1.default;
  288. this.CSSStyleRule = CSSStyleRule_1.default;
  289. this.Selection = Selection_1.default;
  290. this.Navigator = Navigator_1.default;
  291. this.MimeType = MimeType_1.default;
  292. this.MimeTypeArray = MimeTypeArray_1.default;
  293. this.Plugin = Plugin_1.default;
  294. this.PluginArray = PluginArray_1.default;
  295. this.FileList = FileList_1.default;
  296. this.Headers = Headers_1.default;
  297. this.XMLHttpRequestUpload = XMLHttpRequestUpload_1.default;
  298. this.XMLHttpRequestEventTarget = XMLHttpRequestEventTarget_1.default;
  299. // Events
  300. this.onload = null;
  301. this.onerror = null;
  302. this.console = console;
  303. this.self = this;
  304. this.top = this;
  305. this.parent = this;
  306. this.window = this;
  307. this.globalThis = this;
  308. this.devicePixelRatio = 1;
  309. this.performance = PerfHooks.performance;
  310. this.Buffer = buffer_1.Buffer;
  311. this.innerHeight = options?.innerHeight ? options.innerHeight : 768;
  312. if (options?.url) {
  313. this.location.href = options.url;
  314. }
  315. if (options?.settings) {
  316. this.happyDOM.settings = Object.assign(this.happyDOM.settings, options.settings);
  317. }
  318. this._setTimeout = ORIGINAL_SET_TIMEOUT;
  319. this._clearTimeout = ORIGINAL_CLEAR_TIMEOUT;
  320. this._setInterval = ORIGINAL_SET_INTERVAL;
  321. this._clearInterval = ORIGINAL_CLEAR_INTERVAL;
  322. // Non-implemented event types
  323. for (const eventType of NonImplementedEventTypes_1.default) {
  324. if (!this[eventType]) {
  325. this[eventType] = Event_1.default;
  326. }
  327. }
  328. // Non implemented element classes
  329. for (const className of NonImplemenetedElementClasses_1.default) {
  330. if (!this[className]) {
  331. this[className] = HTMLElement_1.default;
  332. }
  333. }
  334. // Binds all methods to "this", so that it will use the correct context when called globally.
  335. for (const key of Object.getOwnPropertyNames(Window.prototype).concat(Object.getOwnPropertyNames(EventTarget_1.default.prototype))) {
  336. if (key !== 'constructor' &&
  337. key[0] !== '_' &&
  338. key[0] === key[0].toLowerCase() &&
  339. typeof this[key] === 'function') {
  340. this[key] = this[key].bind(this);
  341. }
  342. }
  343. HTMLDocument_1.default._defaultView = this;
  344. const document = new HTMLDocument_1.default();
  345. this.document = document;
  346. // We need to set the correct owner document when the class is constructed.
  347. // To achieve this we will extend the original implementation with a class that sets the owner document.
  348. Response_1.default._ownerDocument = document;
  349. Request_1.default._ownerDocument = document;
  350. Image_1.default._ownerDocument = document;
  351. FileReader_1.default._ownerDocument = document;
  352. DOMParser_1.default._ownerDocument = document;
  353. Range_1.default._ownerDocument = document;
  354. XMLHttpRequest_1.default._ownerDocument = document;
  355. /* eslint-disable jsdoc/require-jsdoc */
  356. class Response extends Response_1.default {
  357. }
  358. Response._ownerDocument = document;
  359. class Request extends Request_1.default {
  360. }
  361. Request._ownerDocument = document;
  362. class Image extends Image_1.default {
  363. }
  364. Image._ownerDocument = document;
  365. class FileReader extends FileReader_1.default {
  366. }
  367. FileReader._ownerDocument = document;
  368. class DOMParser extends DOMParser_1.default {
  369. }
  370. DOMParser._ownerDocument = document;
  371. class XMLHttpRequest extends XMLHttpRequest_1.default {
  372. }
  373. XMLHttpRequest._ownerDocument = document;
  374. class Range extends Range_1.default {
  375. }
  376. Range._ownerDocument = document;
  377. class Audio extends Audio_1.default {
  378. }
  379. Audio._ownerDocument = document;
  380. /* eslint-enable jsdoc/require-jsdoc */
  381. this.Response = Response;
  382. this.Request = Request;
  383. this.Image = Image;
  384. this.FileReader = FileReader;
  385. this.DOMParser = DOMParser;
  386. this.XMLHttpRequest = XMLHttpRequest;
  387. this.Range = Range;
  388. this.Audio = Audio;
  389. this._setupVMContext();
  390. this.document._onWindowReady();
  391. }
  392. /**
  393. * The number of pixels that the document is currently scrolled horizontally
  394. *
  395. * @returns number
  396. */
  397. get scrollX() {
  398. return this.document?.documentElement?.scrollLeft ?? 0;
  399. }
  400. /**
  401. * The read-only Window property pageXOffset is an alias for scrollX.
  402. *
  403. * @returns number
  404. */
  405. get pageXOffset() {
  406. return this.scrollX;
  407. }
  408. /**
  409. * The number of pixels that the document is currently scrolled vertically
  410. *
  411. * @returns number
  412. */
  413. get scrollY() {
  414. return this.document?.documentElement?.scrollTop ?? 0;
  415. }
  416. /**
  417. * The read-only Window property pageYOffset is an alias for scrollY.
  418. *
  419. * @returns number
  420. */
  421. get pageYOffset() {
  422. return this.scrollY;
  423. }
  424. /**
  425. * The CSS interface holds useful CSS-related methods.
  426. *
  427. * @returns CSS interface.
  428. */
  429. get CSS() {
  430. return new CSS_1.default();
  431. }
  432. /**
  433. * Evaluates code.
  434. *
  435. * @override
  436. * @param code Code.
  437. * @returns Result.
  438. */
  439. eval(code) {
  440. if (vm_1.default.isContext(this)) {
  441. return vm_1.default.runInContext(code, this);
  442. }
  443. return eval(code);
  444. }
  445. /**
  446. * Returns an object containing the values of all CSS properties of an element.
  447. *
  448. * @param element Element.
  449. * @returns CSS style declaration.
  450. */
  451. getComputedStyle(element) {
  452. return new CSSStyleDeclaration_1.default(element, true);
  453. }
  454. /**
  455. * Returns selection.
  456. *
  457. * @returns Selection.
  458. */
  459. getSelection() {
  460. return this.document.getSelection();
  461. }
  462. /**
  463. * Scrolls to a particular set of coordinates.
  464. *
  465. * @param x X position or options object.
  466. * @param y Y position.
  467. */
  468. scroll(x, y) {
  469. if (typeof x === 'object') {
  470. if (x.behavior === 'smooth') {
  471. this.setTimeout(() => {
  472. if (x.top !== undefined) {
  473. this.document.documentElement.scrollTop = x.top;
  474. }
  475. if (x.left !== undefined) {
  476. this.document.documentElement.scrollLeft = x.left;
  477. }
  478. });
  479. }
  480. else {
  481. if (x.top !== undefined) {
  482. this.document.documentElement.scrollTop = x.top;
  483. }
  484. if (x.left !== undefined) {
  485. this.document.documentElement.scrollLeft = x.left;
  486. }
  487. }
  488. }
  489. else if (x !== undefined && y !== undefined) {
  490. this.document.documentElement.scrollLeft = x;
  491. this.document.documentElement.scrollTop = y;
  492. }
  493. }
  494. /**
  495. * Scrolls to a particular set of coordinates.
  496. *
  497. * @param x X position or options object.
  498. * @param y Y position.
  499. */
  500. scrollTo(x, y) {
  501. this.scroll(x, y);
  502. }
  503. /**
  504. * Returns a new MediaQueryList object that can then be used to determine if the document matches the media query string.
  505. *
  506. * @param mediaQueryString A string specifying the media query to parse into a MediaQueryList.
  507. * @returns A new MediaQueryList.
  508. */
  509. matchMedia(mediaQueryString) {
  510. return new MediaQueryList_1.default(this, mediaQueryString);
  511. }
  512. /**
  513. * Sets a timer which executes a function once the timer expires.
  514. *
  515. * @override
  516. * @param callback Function to be executed.
  517. * @param [delay=0] Delay in ms.
  518. * @param args Arguments passed to the callback function.
  519. * @returns Timeout ID.
  520. */
  521. setTimeout(callback, delay = 0, ...args) {
  522. const id = this._setTimeout(() => {
  523. this.happyDOM.asyncTaskManager.endTimer(id);
  524. callback(...args);
  525. }, delay);
  526. this.happyDOM.asyncTaskManager.startTimer(id);
  527. return id;
  528. }
  529. /**
  530. * Cancels a timeout previously established by calling setTimeout().
  531. *
  532. * @override
  533. * @param id ID of the timeout.
  534. */
  535. clearTimeout(id) {
  536. this._clearTimeout(id);
  537. this.happyDOM.asyncTaskManager.endTimer(id);
  538. }
  539. /**
  540. * Calls a function with a fixed time delay between each call.
  541. *
  542. * @override
  543. * @param callback Function to be executed.
  544. * @param [delay=0] Delay in ms.
  545. * @param args Arguments passed to the callback function.
  546. * @returns Interval ID.
  547. */
  548. setInterval(callback, delay = 0, ...args) {
  549. const id = this._setInterval(callback, delay, ...args);
  550. this.happyDOM.asyncTaskManager.startTimer(id);
  551. return id;
  552. }
  553. /**
  554. * Cancels a timed repeating action which was previously established by a call to setInterval().
  555. *
  556. * @override
  557. * @param id ID of the interval.
  558. */
  559. clearInterval(id) {
  560. this._clearInterval(id);
  561. this.happyDOM.asyncTaskManager.endTimer(id);
  562. }
  563. /**
  564. * Mock animation frames with timeouts.
  565. *
  566. * @override
  567. * @param callback Callback.
  568. * @returns Timeout ID.
  569. */
  570. requestAnimationFrame(callback) {
  571. return this.setTimeout(() => {
  572. callback(this.performance.now());
  573. });
  574. }
  575. /**
  576. * Mock animation frames with timeouts.
  577. *
  578. * @override
  579. * @param id Timeout ID.
  580. */
  581. cancelAnimationFrame(id) {
  582. this.clearTimeout(id);
  583. }
  584. /**
  585. * This method provides an easy, logical way to fetch resources asynchronously across the network.
  586. *
  587. * @override
  588. * @param url URL.
  589. * @param [init] Init.
  590. * @returns Promise.
  591. */
  592. async fetch(url, init) {
  593. return await FetchHandler_1.default.fetch(this.document, url, init);
  594. }
  595. /**
  596. * Creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).
  597. *
  598. * @see https://developer.mozilla.org/en-US/docs/Web/API/btoa
  599. * @param data Binay data.
  600. * @returns Base64-encoded string.
  601. */
  602. btoa(data) {
  603. return Base64_1.default.btoa(data);
  604. }
  605. /**
  606. * Decodes a string of data which has been encoded using Base64 encoding.
  607. *
  608. * @see https://developer.mozilla.org/en-US/docs/Web/API/atob
  609. * @see https://infra.spec.whatwg.org/#forgiving-base64-encode.
  610. * @see Https://html.spec.whatwg.org/multipage/webappapis.html#btoa.
  611. * @param data Binay string.
  612. * @returns An ASCII string containing decoded data from encodedData.
  613. */
  614. atob(data) {
  615. return Base64_1.default.atob(data);
  616. }
  617. /**
  618. * Safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.
  619. *
  620. * @param message Message.
  621. * @param [targetOrigin=*] Target origin.
  622. * @param _transfer Transfer. Not implemented.
  623. */
  624. postMessage(message, targetOrigin = '*', _transfer) {
  625. // TODO: Implement transfer.
  626. if (targetOrigin && targetOrigin !== '*' && this.location.origin !== targetOrigin) {
  627. throw new DOMException_1.default(`Failed to execute 'postMessage' on 'Window': The target origin provided ('${targetOrigin}') does not match the recipient window\'s origin ('${this.location.origin}').`, DOMExceptionNameEnum_1.default.securityError);
  628. }
  629. try {
  630. JSON.stringify(message);
  631. }
  632. catch (error) {
  633. throw new DOMException_1.default(`Failed to execute 'postMessage' on 'Window': The provided message cannot be serialized.`, DOMExceptionNameEnum_1.default.invalidStateError);
  634. }
  635. this.dispatchEvent(new MessageEvent_1.default('message', {
  636. data: message,
  637. origin: this.parent.location.origin,
  638. source: this.parent,
  639. lastEventId: ''
  640. }));
  641. }
  642. /**
  643. * Setup of VM context.
  644. */
  645. _setupVMContext() {
  646. if (!vm_1.default.isContext(this)) {
  647. vm_1.default.createContext(this);
  648. // Sets global properties from the VM to the Window object.
  649. // Otherwise "this.Array" will be undefined for example.
  650. vm_1.default.runInContext(VMGlobalPropertyScript_1.default, this);
  651. }
  652. }
  653. }
  654. exports.default = Window;
  655. //# sourceMappingURL=Window.js.map