happy-dom 2.55.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of happy-dom might be problematic. Click here for more details.

Files changed (284) hide show
  1. package/README.md +26 -6
  2. package/lib/async-task-manager/AsyncTaskManager.js +34 -78
  3. package/lib/async-task-manager/AsyncTaskManager.js.map +1 -1
  4. package/lib/attribute/Attr.js +19 -28
  5. package/lib/attribute/Attr.js.map +1 -1
  6. package/lib/config/ElementTag.js +15 -15
  7. package/lib/config/ElementTag.js.map +1 -1
  8. package/lib/cookie/CookieUtility.js +21 -26
  9. package/lib/cookie/CookieUtility.js.map +1 -1
  10. package/lib/css/CSS.js +12 -18
  11. package/lib/css/CSS.js.map +1 -1
  12. package/lib/css/CSSParser.js +29 -33
  13. package/lib/css/CSSParser.js.map +1 -1
  14. package/lib/css/CSSRule.js +24 -29
  15. package/lib/css/CSSRule.js.map +1 -1
  16. package/lib/css/CSSStyleDeclaration.js +4516 -6005
  17. package/lib/css/CSSStyleDeclaration.js.map +1 -1
  18. package/lib/css/CSSStyleSheet.js +17 -59
  19. package/lib/css/CSSStyleSheet.js.map +1 -1
  20. package/lib/css/CSSUnitValue.js +4 -5
  21. package/lib/css/CSSUnitValue.js.map +1 -1
  22. package/lib/css/MediaList.js +23 -28
  23. package/lib/css/MediaList.js.map +1 -1
  24. package/lib/css/rules/CSSFontFaceRule.js +6 -24
  25. package/lib/css/rules/CSSFontFaceRule.js.map +1 -1
  26. package/lib/css/rules/CSSKeyframeRule.js +14 -36
  27. package/lib/css/rules/CSSKeyframeRule.js.map +1 -1
  28. package/lib/css/rules/CSSKeyframesRule.js +31 -54
  29. package/lib/css/rules/CSSKeyframesRule.js.map +1 -1
  30. package/lib/css/rules/CSSMediaRule.js +29 -56
  31. package/lib/css/rules/CSSMediaRule.js.map +1 -1
  32. package/lib/css/rules/CSSStyleRule.js +16 -38
  33. package/lib/css/rules/CSSStyleRule.js.map +1 -1
  34. package/lib/custom-element/CustomElementRegistry.js +21 -24
  35. package/lib/custom-element/CustomElementRegistry.js.map +1 -1
  36. package/lib/dom-implementation/DOMImplementation.js +12 -13
  37. package/lib/dom-implementation/DOMImplementation.js.map +1 -1
  38. package/lib/dom-parser/DOMParser.js +29 -35
  39. package/lib/dom-parser/DOMParser.js.map +1 -1
  40. package/lib/dom-token-list/DOMTokenList.js +64 -79
  41. package/lib/dom-token-list/DOMTokenList.js.map +1 -1
  42. package/lib/event/DataTransfer.js +4 -5
  43. package/lib/event/DataTransfer.js.map +1 -1
  44. package/lib/event/DataTransferItem.js +7 -8
  45. package/lib/event/DataTransferItem.js.map +1 -1
  46. package/lib/event/DataTransferItemList.js +10 -11
  47. package/lib/event/DataTransferItemList.js.map +1 -1
  48. package/lib/event/Event.js +11 -15
  49. package/lib/event/Event.js.map +1 -1
  50. package/lib/event/EventTarget.js +14 -16
  51. package/lib/event/EventTarget.js.map +1 -1
  52. package/lib/event/IUIEventInit.d.ts +2 -2
  53. package/lib/event/UIEvent.d.ts +2 -2
  54. package/lib/event/UIEvent.js +25 -46
  55. package/lib/event/UIEvent.js.map +1 -1
  56. package/lib/event/events/AnimationEvent.js +11 -29
  57. package/lib/event/events/AnimationEvent.js.map +1 -1
  58. package/lib/event/events/CustomEvent.js +9 -31
  59. package/lib/event/events/CustomEvent.js.map +1 -1
  60. package/lib/event/events/ErrorEvent.js +15 -34
  61. package/lib/event/events/ErrorEvent.js.map +1 -1
  62. package/lib/event/events/FocusEvent.js +7 -26
  63. package/lib/event/events/FocusEvent.js.map +1 -1
  64. package/lib/event/events/InputEvent.js +13 -32
  65. package/lib/event/events/InputEvent.js.map +1 -1
  66. package/lib/event/events/KeyboardEvent.js +27 -46
  67. package/lib/event/events/KeyboardEvent.js.map +1 -1
  68. package/lib/event/events/MouseEvent.js +33 -52
  69. package/lib/event/events/MouseEvent.js.map +1 -1
  70. package/lib/event/events/PointerEvent.js +25 -44
  71. package/lib/event/events/PointerEvent.js.map +1 -1
  72. package/lib/event/events/ProgressEvent.js +11 -30
  73. package/lib/event/events/ProgressEvent.js.map +1 -1
  74. package/lib/event/events/StorageEvent.js +13 -32
  75. package/lib/event/events/StorageEvent.js.map +1 -1
  76. package/lib/event/events/WheelEvent.js +16 -35
  77. package/lib/event/events/WheelEvent.js.map +1 -1
  78. package/lib/exception/DOMException.js +5 -24
  79. package/lib/exception/DOMException.js.map +1 -1
  80. package/lib/fetch/FetchHandler.js +18 -22
  81. package/lib/fetch/FetchHandler.js.map +1 -1
  82. package/lib/fetch/Headers.js +3 -23
  83. package/lib/fetch/Headers.js.map +1 -1
  84. package/lib/fetch/Request.js +61 -81
  85. package/lib/fetch/Request.js.map +1 -1
  86. package/lib/fetch/ResourceFetchHandler.js +18 -67
  87. package/lib/fetch/ResourceFetchHandler.js.map +1 -1
  88. package/lib/fetch/Response.js +61 -81
  89. package/lib/fetch/Response.js.map +1 -1
  90. package/lib/file/Blob.js +29 -78
  91. package/lib/file/Blob.js.map +1 -1
  92. package/lib/file/File.js +9 -27
  93. package/lib/file/File.js.map +1 -1
  94. package/lib/file/FileReader.js +57 -78
  95. package/lib/file/FileReader.js.map +1 -1
  96. package/lib/history/History.js +32 -37
  97. package/lib/history/History.js.map +1 -1
  98. package/lib/index.d.ts +3 -3
  99. package/lib/index.js +69 -69
  100. package/lib/index.js.map +1 -1
  101. package/lib/location/Location.js +11 -28
  102. package/lib/location/Location.js.map +1 -1
  103. package/lib/location/RelativeURL.js +5 -8
  104. package/lib/location/RelativeURL.js.map +1 -1
  105. package/lib/location/URL.js +46 -59
  106. package/lib/location/URL.js.map +1 -1
  107. package/lib/match-media/MediaQueryList.js +28 -54
  108. package/lib/match-media/MediaQueryList.js.map +1 -1
  109. package/lib/mutation-observer/MutationListener.js +3 -4
  110. package/lib/mutation-observer/MutationListener.js.map +1 -1
  111. package/lib/mutation-observer/MutationObserver.js +12 -13
  112. package/lib/mutation-observer/MutationObserver.js.map +1 -1
  113. package/lib/mutation-observer/MutationRecord.js +3 -4
  114. package/lib/mutation-observer/MutationRecord.js.map +1 -1
  115. package/lib/navigator/MimeType.js +5 -6
  116. package/lib/navigator/MimeType.js.map +1 -1
  117. package/lib/navigator/MimeTypeArray.js +10 -11
  118. package/lib/navigator/MimeTypeArray.js.map +1 -1
  119. package/lib/navigator/Navigator.js +7 -8
  120. package/lib/navigator/Navigator.js.map +1 -1
  121. package/lib/navigator/Plugin.js +10 -11
  122. package/lib/navigator/Plugin.js.map +1 -1
  123. package/lib/navigator/PluginArray.js +12 -13
  124. package/lib/navigator/PluginArray.js.map +1 -1
  125. package/lib/nodes/character-data/CharacterData.js +119 -184
  126. package/lib/nodes/character-data/CharacterData.js.map +1 -1
  127. package/lib/nodes/character-data/CharacterDataUtility.js +12 -15
  128. package/lib/nodes/character-data/CharacterDataUtility.js.map +1 -1
  129. package/lib/nodes/child-node/ChildNodeUtility.js +24 -45
  130. package/lib/nodes/child-node/ChildNodeUtility.js.map +1 -1
  131. package/lib/nodes/child-node/NonDocumentChildNodeUtility.js +9 -12
  132. package/lib/nodes/child-node/NonDocumentChildNodeUtility.js.map +1 -1
  133. package/lib/nodes/comment/Comment.js +20 -43
  134. package/lib/nodes/comment/Comment.js.map +1 -1
  135. package/lib/nodes/document/Document.d.ts +3 -3
  136. package/lib/nodes/document/Document.js +334 -461
  137. package/lib/nodes/document/Document.js.map +1 -1
  138. package/lib/nodes/document/DocumentReadyStateManager.js +16 -19
  139. package/lib/nodes/document/DocumentReadyStateManager.js.map +1 -1
  140. package/lib/nodes/document/IDocument.d.ts +2 -2
  141. package/lib/nodes/document-fragment/DocumentFragment.js +93 -153
  142. package/lib/nodes/document-fragment/DocumentFragment.js.map +1 -1
  143. package/lib/nodes/document-type/DocumentType.js +22 -45
  144. package/lib/nodes/document-type/DocumentType.js.map +1 -1
  145. package/lib/nodes/element/DOMRect.js +3 -4
  146. package/lib/nodes/element/DOMRect.js.map +1 -1
  147. package/lib/nodes/element/Element.js +367 -496
  148. package/lib/nodes/element/Element.js.map +1 -1
  149. package/lib/nodes/element/HTMLCollection.js +4 -24
  150. package/lib/nodes/element/HTMLCollection.js.map +1 -1
  151. package/lib/nodes/element/HTMLCollectionFactory.js +6 -9
  152. package/lib/nodes/element/HTMLCollectionFactory.js.map +1 -1
  153. package/lib/nodes/element/Range.js +89 -114
  154. package/lib/nodes/element/Range.js.map +1 -1
  155. package/lib/nodes/html-base-element/HTMLBaseElement.js +41 -70
  156. package/lib/nodes/html-base-element/HTMLBaseElement.js.map +1 -1
  157. package/lib/nodes/html-document/HTMLDocument.js +3 -23
  158. package/lib/nodes/html-document/HTMLDocument.js.map +1 -1
  159. package/lib/nodes/html-element/DatasetUtility.js +9 -12
  160. package/lib/nodes/html-element/DatasetUtility.js.map +1 -1
  161. package/lib/nodes/html-element/HTMLElement.js +232 -290
  162. package/lib/nodes/html-element/HTMLElement.js.map +1 -1
  163. package/lib/nodes/html-form-element/HTMLFormElement.js +170 -235
  164. package/lib/nodes/html-form-element/HTMLFormElement.js.map +1 -1
  165. package/lib/nodes/html-image-element/HTMLImageElement.js +178 -237
  166. package/lib/nodes/html-image-element/HTMLImageElement.js.map +1 -1
  167. package/lib/nodes/html-image-element/Image.js +7 -27
  168. package/lib/nodes/html-image-element/Image.js.map +1 -1
  169. package/lib/nodes/html-input-element/HTMLInputElement.js +705 -872
  170. package/lib/nodes/html-input-element/HTMLInputElement.js.map +1 -1
  171. package/lib/nodes/html-input-element/HTMLInputElementValueSanitizer.js +9 -12
  172. package/lib/nodes/html-input-element/HTMLInputElementValueSanitizer.js.map +1 -1
  173. package/lib/nodes/html-input-element/HTMLInputElementValueStepping.js +5 -8
  174. package/lib/nodes/html-input-element/HTMLInputElementValueStepping.js.map +1 -1
  175. package/lib/nodes/html-input-element/ValidityState.js +47 -69
  176. package/lib/nodes/html-input-element/ValidityState.js.map +1 -1
  177. package/lib/nodes/html-label-element/HTMLLabelElement.js +53 -87
  178. package/lib/nodes/html-label-element/HTMLLabelElement.js.map +1 -1
  179. package/lib/nodes/html-link-element/HTMLLinkElement.js +197 -252
  180. package/lib/nodes/html-link-element/HTMLLinkElement.js.map +1 -1
  181. package/lib/nodes/html-meta-element/HTMLMetaElement.js +66 -102
  182. package/lib/nodes/html-meta-element/HTMLMetaElement.js.map +1 -1
  183. package/lib/nodes/html-script-element/HTMLScriptElement.js +145 -193
  184. package/lib/nodes/html-script-element/HTMLScriptElement.js.map +1 -1
  185. package/lib/nodes/html-script-element/ScriptUtility.js +58 -112
  186. package/lib/nodes/html-script-element/ScriptUtility.js.map +1 -1
  187. package/lib/nodes/html-slot-element/HTMLSlotElement.js +33 -65
  188. package/lib/nodes/html-slot-element/HTMLSlotElement.js.map +1 -1
  189. package/lib/nodes/html-style-element/HTMLStyleElement.js +75 -109
  190. package/lib/nodes/html-style-element/HTMLStyleElement.js.map +1 -1
  191. package/lib/nodes/html-template-element/HTMLTemplateElement.js +60 -99
  192. package/lib/nodes/html-template-element/HTMLTemplateElement.js.map +1 -1
  193. package/lib/nodes/html-text-area-element/HTMLTextAreaElement.js +349 -444
  194. package/lib/nodes/html-text-area-element/HTMLTextAreaElement.js.map +1 -1
  195. package/lib/nodes/html-unknown-element/HTMLUnknownElement.js +17 -37
  196. package/lib/nodes/html-unknown-element/HTMLUnknownElement.js.map +1 -1
  197. package/lib/nodes/node/Node.js +193 -260
  198. package/lib/nodes/node/Node.js.map +1 -1
  199. package/lib/nodes/node/NodeList.js +4 -24
  200. package/lib/nodes/node/NodeList.js.map +1 -1
  201. package/lib/nodes/node/NodeListFactory.js +6 -9
  202. package/lib/nodes/node/NodeListFactory.js.map +1 -1
  203. package/lib/nodes/parent-node/ParentNodeUtility.js +43 -78
  204. package/lib/nodes/parent-node/ParentNodeUtility.js.map +1 -1
  205. package/lib/nodes/shadow-root/ShadowRoot.js +53 -83
  206. package/lib/nodes/shadow-root/ShadowRoot.js.map +1 -1
  207. package/lib/nodes/svg-document/SVGDocument.js +3 -23
  208. package/lib/nodes/svg-document/SVGDocument.js.map +1 -1
  209. package/lib/nodes/svg-element/DOMMatrix.js +3 -4
  210. package/lib/nodes/svg-element/DOMMatrix.js.map +1 -1
  211. package/lib/nodes/svg-element/SVGAngle.js +10 -11
  212. package/lib/nodes/svg-element/SVGAngle.js.map +1 -1
  213. package/lib/nodes/svg-element/SVGAnimatedRect.js +4 -5
  214. package/lib/nodes/svg-element/SVGAnimatedRect.js.map +1 -1
  215. package/lib/nodes/svg-element/SVGElement.js +58 -93
  216. package/lib/nodes/svg-element/SVGElement.js.map +1 -1
  217. package/lib/nodes/svg-element/SVGGraphicsElement.js +14 -32
  218. package/lib/nodes/svg-element/SVGGraphicsElement.js.map +1 -1
  219. package/lib/nodes/svg-element/SVGLength.js +16 -17
  220. package/lib/nodes/svg-element/SVGLength.js.map +1 -1
  221. package/lib/nodes/svg-element/SVGNumber.js +3 -4
  222. package/lib/nodes/svg-element/SVGNumber.js.map +1 -1
  223. package/lib/nodes/svg-element/SVGPoint.js +3 -4
  224. package/lib/nodes/svg-element/SVGPoint.js.map +1 -1
  225. package/lib/nodes/svg-element/SVGRect.js +3 -4
  226. package/lib/nodes/svg-element/SVGRect.js.map +1 -1
  227. package/lib/nodes/svg-element/SVGSVGElement.js +187 -248
  228. package/lib/nodes/svg-element/SVGSVGElement.js.map +1 -1
  229. package/lib/nodes/svg-element/SVGTransform.js +16 -17
  230. package/lib/nodes/svg-element/SVGTransform.js.map +1 -1
  231. package/lib/nodes/text/Text.js +20 -43
  232. package/lib/nodes/text/Text.js.map +1 -1
  233. package/lib/nodes/xml-document/XMLDocument.js +3 -23
  234. package/lib/nodes/xml-document/XMLDocument.js.map +1 -1
  235. package/lib/query-selector/QuerySelector.js +36 -44
  236. package/lib/query-selector/QuerySelector.js.map +1 -1
  237. package/lib/query-selector/SelectorItem.js +62 -66
  238. package/lib/query-selector/SelectorItem.js.map +1 -1
  239. package/lib/resize-observer/ResizeObserver.js +8 -11
  240. package/lib/resize-observer/ResizeObserver.js.map +1 -1
  241. package/lib/screen/Screen.js +3 -4
  242. package/lib/screen/Screen.js.map +1 -1
  243. package/lib/selection/Selection.js +29 -30
  244. package/lib/selection/Selection.js.map +1 -1
  245. package/lib/storage/Storage.js +22 -27
  246. package/lib/storage/Storage.js.map +1 -1
  247. package/lib/tree-walker/NodeFilterMask.js +1 -1
  248. package/lib/tree-walker/NodeFilterMask.js.map +1 -1
  249. package/lib/tree-walker/TreeWalker.js +30 -33
  250. package/lib/tree-walker/TreeWalker.js.map +1 -1
  251. package/lib/window/GlobalWindow.d.ts +90 -0
  252. package/lib/window/GlobalWindow.js +98 -0
  253. package/lib/window/GlobalWindow.js.map +1 -0
  254. package/lib/window/IWindow.d.ts +35 -18
  255. package/lib/window/VMGlobalPropertyScript.d.ts +2 -0
  256. package/lib/window/VMGlobalPropertyScript.js +65 -0
  257. package/lib/window/VMGlobalPropertyScript.js.map +1 -0
  258. package/lib/window/Window.d.ts +84 -105
  259. package/lib/window/Window.js +286 -490
  260. package/lib/window/Window.js.map +1 -1
  261. package/lib/xml-parser/XMLParser.js +65 -71
  262. package/lib/xml-parser/XMLParser.js.map +1 -1
  263. package/lib/xml-serializer/XMLSerializer.js +30 -37
  264. package/lib/xml-serializer/XMLSerializer.js.map +1 -1
  265. package/package.json +6 -7
  266. package/src/dom-parser/DOMParser.ts +4 -5
  267. package/src/event/IUIEventInit.ts +2 -2
  268. package/src/event/UIEvent.ts +2 -2
  269. package/src/fetch/FetchHandler.ts +3 -3
  270. package/src/fetch/ResourceFetchHandler.ts +2 -2
  271. package/src/index.ts +3 -3
  272. package/src/nodes/document/Document.ts +4 -4
  273. package/src/nodes/document/IDocument.ts +2 -2
  274. package/src/nodes/html-link-element/HTMLLinkElement.ts +8 -2
  275. package/src/nodes/html-script-element/ScriptUtility.ts +4 -4
  276. package/src/window/GlobalWindow.ts +91 -0
  277. package/src/window/IWindow.ts +31 -12
  278. package/src/window/VMGlobalPropertyScript.ts +62 -0
  279. package/src/window/Window.ts +125 -173
  280. package/tsconfig.json +1 -1
  281. package/lib/window/AsyncWindow.d.ts +0 -10
  282. package/lib/window/AsyncWindow.js +0 -38
  283. package/lib/window/AsyncWindow.js.map +0 -1
  284. package/src/window/AsyncWindow.ts +0 -17
@@ -71,25 +71,34 @@ import IRequest from '../fetch/IRequest';
71
71
  import IRequestInit from '../fetch/IRequestInit';
72
72
  import IHeaders from '../fetch/IHeaders';
73
73
  import IHeadersInit from '../fetch/IHeadersInit';
74
+ import Headers from '../fetch/Headers';
75
+ import Request from '../fetch/Request';
76
+ import Response from '../fetch/Response';
74
77
  import Storage from '../storage/Storage';
75
78
  import IWindow from './IWindow';
76
79
  import HTMLCollection from '../nodes/element/HTMLCollection';
77
80
  import NodeList from '../nodes/node/NodeList';
78
81
  import MediaQueryList from '../match-media/MediaQueryList';
79
82
  import Selection from '../selection/Selection';
80
- import * as PerfHooks from 'perf_hooks';
81
83
  import Navigator from '../navigator/Navigator';
82
84
  import MimeType from '../navigator/MimeType';
83
85
  import MimeTypeArray from '../navigator/MimeTypeArray';
84
86
  import Plugin from '../navigator/Plugin';
85
87
  import PluginArray from '../navigator/PluginArray';
86
- import FetchHandler from '../fetch/FetchHandler';
87
88
  import { URLSearchParams } from 'url';
89
+ import FetchHandler from '../fetch/FetchHandler';
90
+ import VMGlobalPropertyScript from './VMGlobalPropertyScript';
91
+ import * as PerfHooks from 'perf_hooks';
92
+ import VM from 'vm';
93
+ import { Buffer } from 'buffer';
88
94
 
89
95
  /**
90
- * Handles the Window.
96
+ * Browser window.
97
+ *
98
+ * Reference:
99
+ * https://developer.mozilla.org/en-US/docs/Web/API/Window.
91
100
  */
92
- export default class Window extends EventTarget implements IWindow, NodeJS.Global {
101
+ export default class Window extends EventTarget implements IWindow {
93
102
  // Public Properties
94
103
  public readonly happyDOM = {
95
104
  whenAsyncComplete: async (): Promise<void> => {
@@ -154,7 +163,7 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
154
163
  public readonly URL = URL;
155
164
  public readonly Location = Location;
156
165
  public readonly CustomElementRegistry = CustomElementRegistry;
157
- public readonly Window = Window;
166
+ public readonly Window = <typeof Window>this.constructor;
158
167
  public readonly XMLSerializer = XMLSerializer;
159
168
  public readonly ResizeObserver = ResizeObserver;
160
169
  public readonly CSSStyleSheet = CSSStyleSheet;
@@ -176,6 +185,13 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
176
185
  public readonly MimeTypeArray = MimeTypeArray;
177
186
  public readonly Plugin = Plugin;
178
187
  public readonly PluginArray = PluginArray;
188
+ public readonly Headers: { new (init?: IHeadersInit): IHeaders } = Headers;
189
+ public readonly Request: {
190
+ new (input: string | { href: string } | IRequest, init?: IRequestInit): IRequest;
191
+ } = Request;
192
+ public readonly Response: {
193
+ new (body?: NodeJS.ReadableStream | null, init?: IResponseInit): IResponse;
194
+ } = Response;
179
195
 
180
196
  // Events
181
197
  public onload: (event: Event) => void = null;
@@ -187,7 +203,7 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
187
203
  public readonly location = new Location();
188
204
  public readonly history = new History();
189
205
  public readonly navigator = new Navigator();
190
- public readonly console = global ? global.console : null;
206
+ public readonly console = console;
191
207
  public readonly self = this;
192
208
  public readonly top = this;
193
209
  public readonly parent = this;
@@ -202,69 +218,73 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
202
218
  public readonly performance = PerfHooks.performance;
203
219
 
204
220
  // Node.js Globals
205
- public ArrayBuffer = global ? global.ArrayBuffer : null;
206
- public Boolean = global ? global.Boolean : null;
207
- public Buffer = null;
208
- public DataView = global ? global.DataView : null;
209
- public Date = global ? global.Date : null;
210
- public Error = global ? global.Error : null;
211
- public EvalError = global ? global.EvalError : null;
212
- public Float32Array = global ? global.Float32Array : null;
213
- public Float64Array = global ? global.Float64Array : null;
214
- public GLOBAL = null;
215
- public Infinity = global ? global.Infinity : null;
216
- public Int16Array = global ? global.Int16Array : null;
217
- public Int32Array = global ? global.Int32Array : null;
218
- public Int8Array = global ? global.Int8Array : null;
219
- public Intl = global ? global.Intl : null;
220
- public JSON = global ? global.JSON : null;
221
- public Map = global ? global.Map : null;
222
- public Math = global ? global.Math : null;
223
- public NaN = global ? global.NaN : null;
224
- public Number = global ? global.Number : null;
225
- public Promise = global ? global.Promise : null;
226
- public RangeError = global ? global.RangeError : null;
227
- public ReferenceError = global ? global.ReferenceError : null;
228
- public RegExp = global ? global.RegExp : null;
229
- public Reflect = global ? global.Reflect : null;
230
- public Set = global ? global.Set : null;
231
- public Symbol = global ? global.Symbol : null;
232
- public SyntaxError = global ? global.SyntaxError : null;
233
- public String = global ? global.String : null;
234
- public TypeError = global ? global.TypeError : null;
235
- public URIError = global ? global.URIError : null;
236
- public Uint16Array = global ? global.Uint16Array : null;
237
- public Uint32Array = global ? global.Uint32Array : null;
238
- public Uint8Array = global ? global.Uint8Array : null;
239
- public Uint8ClampedArray = global ? global.Uint8ClampedArray : null;
240
- public WeakMap = global ? global.WeakMap : null;
241
- public WeakSet = global ? global.WeakSet : null;
242
- public clearImmediate = null;
243
- public decodeURI = global ? global.decodeURI : null;
244
- public decodeURIComponent = global ? global.decodeURIComponent : null;
245
- public encodeURI = global ? global.encodeURI : null;
246
- public encodeURIComponent = global ? global.encodeURIComponent : null;
247
- public escape = global ? global.escape : null;
248
- public global = null;
249
- public isFinite = global ? global.isFinite : null;
250
- public isNaN = global ? global.isNaN : null;
251
- public parseFloat = global ? global.parseFloat : null;
252
- public parseInt = global ? global.parseInt : null;
253
- public process = null;
254
- public root = null;
255
- public setImmediate = null;
256
- public queueMicrotask = global ? global.queueMicrotask : null;
257
- public undefined = global ? global.undefined : null;
258
- public unescape = global ? global.unescape : null;
259
- public gc = null;
260
- public v8debug = null;
261
- public AbortController = global ? global.AbortController : null;
262
- public AbortSignal = global ? global.AbortSignal : null;
221
+ public ArrayBuffer;
222
+ public Boolean;
223
+ public Buffer = Buffer;
224
+ public DataView;
225
+ public Date;
226
+ public Error;
227
+ public EvalError;
228
+ public Float32Array;
229
+ public Float64Array;
230
+ public GLOBAL;
231
+ public Infinity;
232
+ public Int16Array;
233
+ public Int32Array;
234
+ public Int8Array;
235
+ public Intl;
236
+ public JSON;
237
+ public Map;
238
+ public Math;
239
+ public NaN;
240
+ public Number;
241
+ public Promise;
242
+ public RangeError;
243
+ public ReferenceError;
244
+ public RegExp;
245
+ public Reflect;
246
+ public Set;
247
+ public Symbol;
248
+ public SyntaxError;
249
+ public String;
250
+ public TypeError;
251
+ public URIError;
252
+ public Uint16Array;
253
+ public Uint32Array;
254
+ public Uint8Array;
255
+ public Uint8ClampedArray;
256
+ public WeakMap;
257
+ public WeakSet;
258
+ public clearImmediate;
259
+ public decodeURI;
260
+ public decodeURIComponent;
261
+ public encodeURI;
262
+ public encodeURIComponent;
263
+ public escape;
264
+ public global;
265
+ public isFinite;
266
+ public isNaN;
267
+ public parseFloat;
268
+ public parseInt;
269
+ public process;
270
+ public root;
271
+ public setImmediate;
272
+ public queueMicrotask;
273
+ public undefined;
274
+ public unescape;
275
+ public gc;
276
+ public v8debug;
277
+ public AbortController;
278
+ public AbortSignal;
279
+ public Array;
280
+ public Object;
281
+ public Function;
263
282
 
264
283
  // Private properties
265
- private _objectClass: typeof globalThis.Object = null;
266
- private _arrayClass: typeof globalThis.Array = null;
267
- private _functionClass: typeof globalThis.Function = null;
284
+ private _setTimeout = setTimeout;
285
+ private _clearTimeout = clearTimeout;
286
+ private _setInterval = setInterval;
287
+ private _clearInterval = clearInterval;
268
288
 
269
289
  /**
270
290
  * Constructor.
@@ -278,9 +298,11 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
278
298
  this.dispatchEvent(new Event('load'));
279
299
  });
280
300
 
281
- DOMParser._ownerDocument = DOMParser._ownerDocument || this.document;
282
- FileReader._ownerDocument = FileReader._ownerDocument || this.document;
283
- Image.ownerDocument = Image.ownerDocument || this.document;
301
+ DOMParser._ownerDocument = this.document;
302
+ FileReader._ownerDocument = this.document;
303
+ Image.ownerDocument = this.document;
304
+ Request._ownerDocument = this.document;
305
+ Response._ownerDocument = this.document;
284
306
 
285
307
  for (const eventType of NonImplementedEventTypes) {
286
308
  if (!this[eventType]) {
@@ -295,56 +317,13 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
295
317
  }
296
318
 
297
319
  // Binds all methods to "this", so that it will use the correct context when called globally.
298
- for (const key of Object.keys(Window.prototype)) {
299
- if (typeof this[key] === 'function') {
320
+ for (const key of Object.getOwnPropertyNames(this.constructor.prototype)) {
321
+ if (key !== 'constructor' && key !== 'CSS' && typeof this[key] === 'function') {
300
322
  this[key] = this[key].bind(this);
301
323
  }
302
324
  }
303
- }
304
325
 
305
- /**
306
- * Returns Object class.
307
- *
308
- * @returns Object class.
309
- */
310
- public get Object(): typeof globalThis.Object {
311
- if (this._objectClass) {
312
- return this._objectClass;
313
- }
314
- // When inside a VM global.Object is not the same as ({}).constructor
315
- // We will therefore run the code inside the VM to get the real constructor
316
- this._objectClass = <typeof globalThis.Object>this.eval('({}).constructor');
317
- return this._objectClass;
318
- }
319
-
320
- /**
321
- * Returns Array class.
322
- *
323
- * @returns Array class.
324
- */
325
- public get Array(): typeof globalThis.Array {
326
- if (this._arrayClass) {
327
- return this._arrayClass;
328
- }
329
- // When inside a VM global.Object is not the same as [].constructor
330
- // We will therefore run the code inside the VM to get the real constructor
331
- this._arrayClass = <typeof globalThis.Array>this.eval('[].constructor');
332
- return this._arrayClass;
333
- }
334
-
335
- /**
336
- * Returns Function class.
337
- *
338
- * @returns Function class.
339
- */
340
- public get Function(): typeof globalThis.Function {
341
- if (this._functionClass) {
342
- return this._functionClass;
343
- }
344
- // When inside a VM global.Function is not the same as (() => {}).constructor
345
- // We will therefore run the code inside the VM to get the real constructor
346
- this._functionClass = <typeof globalThis.Function>this.eval('(() => {}).constructor');
347
- return this._functionClass;
326
+ this._setupVMContext();
348
327
  }
349
328
 
350
329
  /**
@@ -356,68 +335,19 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
356
335
  return new CSS();
357
336
  }
358
337
 
359
- /**
360
- * Returns Headers class.
361
- *
362
- * @returns Headers.
363
- */
364
- public get Headers(): {
365
- new (init?: IHeadersInit): IHeaders;
366
- } {
367
- return require('../fetch/Headers').default;
368
- }
369
-
370
- /**
371
- * Returns Request class.
372
- *
373
- * @returns Request.
374
- */
375
- public get Request(): {
376
- new (input: string | { href: string } | IRequest, init?: IRequestInit): IRequest;
377
- } {
378
- const Request = require('../fetch/Request').default;
379
- Request._ownerDocument = Request._ownerDocument || this.document;
380
- return Request;
381
- }
382
-
383
- /**
384
- * Returns Response class.
385
- *
386
- * @returns Response.
387
- */
388
- public get Response(): {
389
- new (body?: NodeJS.ReadableStream | null, init?: IResponseInit): IResponse;
390
- } {
391
- const Response = require('../fetch/Response').default;
392
- Response._ownerDocument = Response._ownerDocument || this.document;
393
- return Response;
394
- }
395
-
396
338
  /**
397
339
  * Evaluates code.
398
340
  *
341
+ * @override
399
342
  * @param code Code.
400
343
  * @returns Result.
401
344
  */
402
345
  public eval(code: string): unknown {
403
- let vmExists = false;
404
- let vm = null;
405
-
406
- try {
407
- vmExists = !!require.resolve('vm');
408
- } catch (error) {
409
- // Ignore error;
410
- }
411
-
412
- if (vmExists) {
413
- vm = require('vm');
414
- }
415
-
416
- if (vm && vm.isContext(this)) {
417
- return vm.runInContext(code, this);
346
+ if (VM.isContext(this)) {
347
+ return VM.runInContext(code, this);
418
348
  }
419
349
 
420
- return global.eval(code);
350
+ return eval(code);
421
351
  }
422
352
 
423
353
  /**
@@ -430,6 +360,15 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
430
360
  return new CSSStyleDeclaration(element._attributes, element);
431
361
  }
432
362
 
363
+ /**
364
+ * Returns selection.
365
+ *
366
+ * @returns Selection.
367
+ */
368
+ public getSelection(): Selection {
369
+ return this.document.getSelection();
370
+ }
371
+
433
372
  /**
434
373
  * Scrolls to a particular set of coordinates.
435
374
  *
@@ -495,7 +434,7 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
495
434
  * @returns Timeout ID.
496
435
  */
497
436
  public setTimeout(callback: () => void, delay = 0): NodeJS.Timeout {
498
- const id = global.setTimeout(() => {
437
+ const id = this._setTimeout(() => {
499
438
  this.happyDOM.asyncTaskManager.endTimer(id);
500
439
  callback();
501
440
  }, delay);
@@ -510,7 +449,7 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
510
449
  * @param id ID of the timeout.
511
450
  */
512
451
  public clearTimeout(id: NodeJS.Timeout): void {
513
- global.clearTimeout(id);
452
+ this._clearTimeout(id);
514
453
  this.happyDOM.asyncTaskManager.endTimer(id);
515
454
  }
516
455
 
@@ -523,7 +462,7 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
523
462
  * @returns Interval ID.
524
463
  */
525
464
  public setInterval(callback: () => void, delay = 0): NodeJS.Timeout {
526
- const id = global.setInterval(callback, delay);
465
+ const id = this._setInterval(callback, delay);
527
466
  this.happyDOM.asyncTaskManager.startTimer(id);
528
467
  return id;
529
468
  }
@@ -535,7 +474,7 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
535
474
  * @param id ID of the interval.
536
475
  */
537
476
  public clearInterval(id: NodeJS.Timeout): void {
538
- global.clearInterval(id);
477
+ this._clearInterval(id);
539
478
  this.happyDOM.asyncTaskManager.endTimer(id);
540
479
  }
541
480
 
@@ -573,4 +512,17 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
573
512
  public async fetch(url: string, init?: IRequestInit): Promise<IResponse> {
574
513
  return await FetchHandler.fetch(this.document, url, init);
575
514
  }
515
+
516
+ /**
517
+ * Setup of VM context.
518
+ */
519
+ protected _setupVMContext(): void {
520
+ if (!VM.isContext(this)) {
521
+ VM.createContext(this);
522
+
523
+ // Sets global properties from the VM to the Window object.
524
+ // Otherwise "this.Array" will be undefined for example.
525
+ VM.runInContext(VMGlobalPropertyScript, this);
526
+ }
527
+ }
576
528
  }
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "outDir": "lib",
4
4
  "rootDir": "src",
5
5
  "tsBuildInfoFile": "tmp/.tsbuildinfo",
6
- "target": "es5",
6
+ "target": "es2020",
7
7
  "declaration": true,
8
8
  "module": "CommonJS",
9
9
  "moduleResolution": "node",
@@ -1,10 +0,0 @@
1
- import Window from './Window';
2
- /**
3
- * The async Window makes it possible to wait for asyncrounous tasks to complete by calling the method whenAsyncComplete(). It also adds support for the method fetch().
4
- */
5
- export default class AsyncWindow extends Window {
6
- /**
7
- * Constructor.
8
- */
9
- constructor();
10
- }
@@ -1,38 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __importDefault = (this && this.__importDefault) || function (mod) {
18
- return (mod && mod.__esModule) ? mod : { "default": mod };
19
- };
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- var Window_1 = __importDefault(require("./Window"));
22
- /**
23
- * The async Window makes it possible to wait for asyncrounous tasks to complete by calling the method whenAsyncComplete(). It also adds support for the method fetch().
24
- */
25
- var AsyncWindow = /** @class */ (function (_super) {
26
- __extends(AsyncWindow, _super);
27
- /**
28
- * Constructor.
29
- */
30
- function AsyncWindow() {
31
- var _this = _super.call(this) || this;
32
- _this.console.warn('⚠ AsyncWindow has been deprecated in Happy DOM. All the functionality of AsyncWindow has been added to Window, so there is no need to use AsyncWindow. AsyncWindow will be removed in the future.');
33
- return _this;
34
- }
35
- return AsyncWindow;
36
- }(Window_1.default));
37
- exports.default = AsyncWindow;
38
- //# sourceMappingURL=AsyncWindow.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AsyncWindow.js","sourceRoot":"","sources":["../../src/window/AsyncWindow.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,oDAA8B;AAE9B;;GAEG;AACH;IAAyC,+BAAM;IAC9C;;OAEG;IACH;QAAA,YACC,iBAAO,SAKP;QAHA,KAAI,CAAC,OAAO,CAAC,IAAI,CAChB,mMAAmM,CACnM,CAAC;;IACH,CAAC;IACF,kBAAC;AAAD,CAAC,AAXD,CAAyC,gBAAM,GAW9C"}
@@ -1,17 +0,0 @@
1
- import Window from './Window';
2
-
3
- /**
4
- * The async Window makes it possible to wait for asyncrounous tasks to complete by calling the method whenAsyncComplete(). It also adds support for the method fetch().
5
- */
6
- export default class AsyncWindow extends Window {
7
- /**
8
- * Constructor.
9
- */
10
- constructor() {
11
- super();
12
-
13
- this.console.warn(
14
- '⚠ AsyncWindow has been deprecated in Happy DOM. All the functionality of AsyncWindow has been added to Window, so there is no need to use AsyncWindow. AsyncWindow will be removed in the future.'
15
- );
16
- }
17
- }