happy-dom 2.42.0 → 2.45.0

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 (67) hide show
  1. package/lib/css/CSSStyleDeclaration.js +1 -1
  2. package/lib/css/CSSStyleDeclaration.js.map +1 -1
  3. package/lib/{nodes/element → dom-token-list}/DOMTokenList.d.ts +32 -29
  4. package/lib/dom-token-list/DOMTokenList.js +209 -0
  5. package/lib/dom-token-list/DOMTokenList.js.map +1 -0
  6. package/lib/{nodes/element → dom-token-list}/IDOMTokenList.d.ts +6 -4
  7. package/lib/{nodes/element → dom-token-list}/IDOMTokenList.js +0 -0
  8. package/lib/dom-token-list/IDOMTokenList.js.map +1 -0
  9. package/lib/navigator/MimeType.d.ts +25 -0
  10. package/lib/navigator/MimeType.js +32 -0
  11. package/lib/navigator/MimeType.js.map +1 -0
  12. package/lib/navigator/MimeTypeArray.d.ts +31 -0
  13. package/lib/navigator/MimeTypeArray.js +45 -0
  14. package/lib/navigator/MimeTypeArray.js.map +1 -0
  15. package/lib/navigator/Navigator.d.ts +41 -0
  16. package/lib/navigator/Navigator.js +78 -0
  17. package/lib/navigator/Navigator.js.map +1 -0
  18. package/lib/navigator/Plugin.d.ts +40 -0
  19. package/lib/navigator/Plugin.js +55 -0
  20. package/lib/navigator/Plugin.js.map +1 -0
  21. package/lib/navigator/PluginArray.d.ts +38 -0
  22. package/lib/navigator/PluginArray.js +54 -0
  23. package/lib/navigator/PluginArray.js.map +1 -0
  24. package/lib/nodes/document/Document.d.ts +2 -1
  25. package/lib/nodes/document/Document.js.map +1 -1
  26. package/lib/nodes/document/IDocument.d.ts +13 -2
  27. package/lib/nodes/element/Element.d.ts +12 -2
  28. package/lib/nodes/element/Element.js +27 -2
  29. package/lib/nodes/element/Element.js.map +1 -1
  30. package/lib/nodes/element/IElement.d.ts +2 -2
  31. package/lib/nodes/html-label-element/HTMLLabelElement.d.ts +0 -1
  32. package/lib/nodes/html-label-element/HTMLLabelElement.js +2 -6
  33. package/lib/nodes/html-label-element/HTMLLabelElement.js.map +1 -1
  34. package/lib/nodes/html-link-element/HTMLLinkElement.d.ts +12 -0
  35. package/lib/nodes/html-link-element/HTMLLinkElement.js +26 -0
  36. package/lib/nodes/html-link-element/HTMLLinkElement.js.map +1 -1
  37. package/lib/nodes/html-link-element/IHTMLLinkElement.d.ts +2 -0
  38. package/lib/query-selector/SelectorItem.d.ts +18 -2
  39. package/lib/query-selector/SelectorItem.js +54 -25
  40. package/lib/query-selector/SelectorItem.js.map +1 -1
  41. package/lib/window/IWindow.d.ts +13 -4
  42. package/lib/window/Window.d.ts +11 -3
  43. package/lib/window/Window.js +11 -1
  44. package/lib/window/Window.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/css/CSSStyleDeclaration.ts +1 -1
  47. package/src/dom-token-list/DOMTokenList.ts +219 -0
  48. package/src/{nodes/element → dom-token-list}/IDOMTokenList.ts +6 -4
  49. package/src/navigator/MimeType.ts +35 -0
  50. package/src/navigator/MimeTypeArray.ts +48 -0
  51. package/src/navigator/Navigator.ts +93 -0
  52. package/src/navigator/Plugin.ts +61 -0
  53. package/src/navigator/PluginArray.ts +58 -0
  54. package/src/nodes/document/Document.ts +3 -2
  55. package/src/nodes/document/IDocument.ts +13 -2
  56. package/src/nodes/element/Element.ts +29 -3
  57. package/src/nodes/element/IElement.ts +2 -2
  58. package/src/nodes/html-label-element/HTMLLabelElement.ts +1 -5
  59. package/src/nodes/html-link-element/HTMLLinkElement.ts +26 -0
  60. package/src/nodes/html-link-element/IHTMLLinkElement.ts +2 -0
  61. package/src/query-selector/SelectorItem.ts +62 -26
  62. package/src/window/IWindow.ts +13 -2
  63. package/src/window/Window.ts +11 -1
  64. package/lib/nodes/element/DOMTokenList.js +0 -205
  65. package/lib/nodes/element/DOMTokenList.js.map +0 -1
  66. package/lib/nodes/element/IDOMTokenList.js.map +0 -1
  67. package/src/nodes/element/DOMTokenList.ts +0 -201
@@ -75,6 +75,11 @@ import NodeList from '../nodes/node/NodeList';
75
75
  import MediaQueryList from '../match-media/MediaQueryList';
76
76
  import Selection from '../selection/Selection';
77
77
  import * as PerfHooks from 'perf_hooks';
78
+ import Navigator from '../navigator/Navigator';
79
+ import MimeType from '../navigator/MimeType';
80
+ import MimeTypeArray from '../navigator/MimeTypeArray';
81
+ import Plugin from '../navigator/Plugin';
82
+ import PluginArray from '../navigator/PluginArray';
78
83
 
79
84
  const FETCH_RESPONSE_TYPE_METHODS = ['blob', 'json', 'text'];
80
85
 
@@ -162,6 +167,11 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
162
167
  public readonly MediaQueryList = MediaQueryList;
163
168
  public readonly CSSUnitValue = CSSUnitValue;
164
169
  public readonly Selection = Selection;
170
+ public readonly Navigator = Navigator;
171
+ public readonly MimeType = MimeType;
172
+ public readonly MimeTypeArray = MimeTypeArray;
173
+ public readonly Plugin = Plugin;
174
+ public readonly PluginArray = PluginArray;
165
175
 
166
176
  // Events
167
177
  public onload: (event: Event) => void = null;
@@ -172,7 +182,7 @@ export default class Window extends EventTarget implements IWindow, NodeJS.Globa
172
182
  public readonly customElements: CustomElementRegistry = new CustomElementRegistry();
173
183
  public readonly location = new Location();
174
184
  public readonly history = new History();
175
- public readonly navigator = { userAgent: 'happy-dom' };
185
+ public readonly navigator = new Navigator();
176
186
  public readonly console = global ? global.console : null;
177
187
  public readonly self = this;
178
188
  public readonly top = this;
@@ -1,205 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- var DOMException_1 = __importDefault(require("../../exception/DOMException"));
7
- /**
8
- * DOM Token List.
9
- */
10
- var DOMTokenList = /** @class */ (function () {
11
- /**
12
- * Adds class names.
13
- *
14
- * @param ownerElement Owner element.
15
- */
16
- function DOMTokenList(ownerElement) {
17
- this._ownerElement = ownerElement;
18
- }
19
- /**
20
- * Get ClassName.
21
- *
22
- * @param index Index.
23
- * */
24
- DOMTokenList.prototype.item = function (index) {
25
- this._refreshListFromClass();
26
- return index >= 0 && this._list[index] ? this._list[index] : null;
27
- };
28
- /**
29
- * Replace Token.
30
- *
31
- * @param token Token.
32
- * @param newToken NewToken.
33
- */
34
- DOMTokenList.prototype.replace = function (token, newToken) {
35
- if (!this.contains(token)) {
36
- return false;
37
- }
38
- var index = this._list.indexOf(token);
39
- this._list[index] = newToken;
40
- var value = this._list ? this._list.join(' ') : '';
41
- this._ownerElement.setAttribute('class', value);
42
- return true;
43
- };
44
- /**
45
- * Supports.
46
- *
47
- * @param token Token.
48
- */
49
- DOMTokenList.prototype.supports = function (token) {
50
- // TODO May IT IN ERROR.
51
- throw new DOMException_1.default("Failed to execute '".concat(token, "' on 'DOMTokenList': DOMTokenList has no supported tokens."), 'TypeError');
52
- };
53
- /**
54
- * Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
55
- *
56
- *
57
- */
58
- DOMTokenList.prototype.values = function () {
59
- this._refreshListFromClass();
60
- return this._list.values();
61
- };
62
- /**
63
- * Returns an iterator, allowing you to go through all key/value pairs contained in this object.
64
- *
65
- *
66
- */
67
- DOMTokenList.prototype.entries = function () {
68
- this._refreshListFromClass();
69
- return this._list.entries();
70
- };
71
- /**
72
- * Executes a provided callback function once for each DOMTokenList element.
73
- *
74
- * @param callback
75
- * @param thisArg
76
- */
77
- DOMTokenList.prototype.forEach = function (callback, thisArg) {
78
- this._refreshListFromClass();
79
- return this._list.forEach(callback, thisArg);
80
- };
81
- /**
82
- * Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
83
- *
84
- */
85
- DOMTokenList.prototype.keys = function () {
86
- this._refreshListFromClass();
87
- return this._list.keys();
88
- };
89
- /**
90
- * Adds class names.
91
- *
92
- * @param classNames Class names.
93
- */
94
- DOMTokenList.prototype.add = function () {
95
- var classNames = [];
96
- for (var _i = 0; _i < arguments.length; _i++) {
97
- classNames[_i] = arguments[_i];
98
- }
99
- this._refreshListFromClass();
100
- for (var _a = 0, classNames_1 = classNames; _a < classNames_1.length; _a++) {
101
- var className = classNames_1[_a];
102
- if (!this._list.includes(className)) {
103
- if (className.includes(' ')) {
104
- throw new DOMException_1.default("Failed to execute 'add' on 'DOMTokenList': The token provided ('".concat(className, "') contains HTML space characters, which are not valid in tokens."));
105
- }
106
- this._list.push(className);
107
- }
108
- }
109
- this._list = Array.from(new Set(this._list));
110
- var value = this._list ? this._list.join(' ') : '';
111
- this._ownerElement.setAttribute('class', value);
112
- };
113
- /**
114
- * Removes class names.
115
- *
116
- * @param classNames Class names.
117
- */
118
- DOMTokenList.prototype.remove = function () {
119
- var classNames = [];
120
- for (var _i = 0; _i < arguments.length; _i++) {
121
- classNames[_i] = arguments[_i];
122
- }
123
- this._refreshListFromClass();
124
- for (var _a = 0, classNames_2 = classNames; _a < classNames_2.length; _a++) {
125
- var className = classNames_2[_a];
126
- var index = this._list.indexOf(className);
127
- if (index !== -1) {
128
- this._list.splice(index, 1);
129
- }
130
- }
131
- var value = this._list ? this._list.join(' ') : '';
132
- this._ownerElement.setAttribute('class', value);
133
- };
134
- /**
135
- * Check if the list contains a class.
136
- *
137
- * @param className Class name.
138
- * @returns TRUE if it contains.
139
- */
140
- DOMTokenList.prototype.contains = function (className) {
141
- this._refreshListFromClass();
142
- return this._list.includes(className);
143
- };
144
- /**
145
- * Toggle a class name.
146
- *
147
- * @param className A string representing the class name you want to toggle.
148
- * @param force If included, turns the toggle into a one way-only operation. If set to `false`, then class name will only be removed, but not added. If set to `true`, then class name will only be added, but not removed.
149
- * @returns A boolean value, `true` or `false`, indicating whether class name is in the list after the call or not.
150
- */
151
- DOMTokenList.prototype.toggle = function (className, force) {
152
- var shouldAdd;
153
- if (force !== undefined) {
154
- shouldAdd = force;
155
- }
156
- else {
157
- shouldAdd = !this.contains(className);
158
- }
159
- if (shouldAdd) {
160
- this.add(className);
161
- return true;
162
- }
163
- this.remove(className);
164
- return false;
165
- };
166
- /**
167
- * Refresh list from class.
168
- */
169
- DOMTokenList.prototype._refreshListFromClass = function () {
170
- var attr = this._ownerElement.getAttribute('class');
171
- this._list = attr ? Array.from(new Set(attr.split(' '))) : [];
172
- };
173
- Object.defineProperty(DOMTokenList.prototype, "value", {
174
- /**
175
- * Get Value.
176
- */
177
- get: function () {
178
- this._refreshListFromClass();
179
- return this._list ? this._list.join(' ') : '';
180
- },
181
- /**
182
- * Set Value.
183
- */
184
- set: function (value) {
185
- this._ownerElement.setAttribute('class', value);
186
- this._list = value ? Array.from(new Set(value.split(' '))) : [];
187
- },
188
- enumerable: false,
189
- configurable: true
190
- });
191
- Object.defineProperty(DOMTokenList.prototype, "length", {
192
- /**
193
- * Get Length.
194
- */
195
- get: function () {
196
- this._refreshListFromClass();
197
- return this._list.length;
198
- },
199
- enumerable: false,
200
- configurable: true
201
- });
202
- return DOMTokenList;
203
- }());
204
- exports.default = DOMTokenList;
205
- //# sourceMappingURL=DOMTokenList.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DOMTokenList.js","sourceRoot":"","sources":["../../../src/nodes/element/DOMTokenList.ts"],"names":[],"mappings":";;;;;AAAA,8EAAwD;AAIxD;;GAEG;AACH;IAGC;;;;OAIG;IACH,sBAAY,YAAqB;QAChC,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;IACnC,CAAC;IAED;;;;SAIK;IACE,2BAAI,GAAX,UAAY,KAAa;QACxB,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAC7B,OAAO,KAAK,IAAI,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACnE,CAAC;IAED;;;;;OAKG;IACI,8BAAO,GAAd,UAAe,KAAa,EAAE,QAAgB;QAC7C,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK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CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/C,CAAC;QAdD;;WAEG;aACH,UAAiB,KAAa;YAC7B,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAChD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACjE,CAAC;;;OAAA;IAaD,sBAAW,gCAAM;QAHjB;;WAEG;aACH;YACC,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC7B,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;QAC1B,CAAC;;;OAAA;IACF,mBAAC;AAAD,CAAC,AAjMD,IAiMC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"IDOMTokenList.js","sourceRoot":"","sources":["../../../src/nodes/element/IDOMTokenList.ts"],"names":[],"mappings":""}
@@ -1,201 +0,0 @@
1
- import DOMException from '../../exception/DOMException';
2
- import Element from './Element';
3
- import IDOMTokenList from './IDOMTokenList';
4
-
5
- /**
6
- * DOM Token List.
7
- */
8
- export default class DOMTokenList implements IDOMTokenList<string> {
9
- private _ownerElement: Element;
10
- private _list: string[];
11
- /**
12
- * Adds class names.
13
- *
14
- * @param ownerElement Owner element.
15
- */
16
- constructor(ownerElement: Element) {
17
- this._ownerElement = ownerElement;
18
- }
19
-
20
- /**
21
- * Get ClassName.
22
- *
23
- * @param index Index.
24
- * */
25
- public item(index: number): string {
26
- this._refreshListFromClass();
27
- return index >= 0 && this._list[index] ? this._list[index] : null;
28
- }
29
-
30
- /**
31
- * Replace Token.
32
- *
33
- * @param token Token.
34
- * @param newToken NewToken.
35
- */
36
- public replace(token: string, newToken: string): boolean {
37
- if (!this.contains(token)) {
38
- return false;
39
- }
40
- const index = this._list.indexOf(token);
41
- this._list[index] = newToken;
42
- const value = this._list ? this._list.join(' ') : '';
43
- this._ownerElement.setAttribute('class', value);
44
- return true;
45
- }
46
-
47
- /**
48
- * Supports.
49
- *
50
- * @param token Token.
51
- */
52
- public supports(token: string): boolean {
53
- // TODO May IT IN ERROR.
54
- throw new DOMException(
55
- `Failed to execute '${token}' on 'DOMTokenList': DOMTokenList has no supported tokens.`,
56
- 'TypeError'
57
- );
58
- }
59
- /**
60
- * Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
61
- *
62
- *
63
- */
64
- public values(): IterableIterator<string> {
65
- this._refreshListFromClass();
66
- return this._list.values();
67
- }
68
- /**
69
- * Returns an iterator, allowing you to go through all key/value pairs contained in this object.
70
- *
71
- *
72
- */
73
- public entries(): IterableIterator<[number, string]> {
74
- this._refreshListFromClass();
75
- return this._list.entries();
76
- }
77
- /**
78
- * Executes a provided callback function once for each DOMTokenList element.
79
- *
80
- * @param callback
81
- * @param thisArg
82
- */
83
- public forEach(callback: (currentValue, currentIndex, listObj) => void, thisArg?: this): void {
84
- this._refreshListFromClass();
85
- return this._list.forEach(callback, thisArg);
86
- }
87
- /**
88
- * Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
89
- *
90
- */
91
- public keys(): IterableIterator<number> {
92
- this._refreshListFromClass();
93
- return this._list.keys();
94
- }
95
-
96
- /**
97
- * Adds class names.
98
- *
99
- * @param classNames Class names.
100
- */
101
- public add(...classNames: string[]): void {
102
- this._refreshListFromClass();
103
- for (const className of classNames) {
104
- if (!this._list.includes(className)) {
105
- if (className.includes(' ')) {
106
- throw new DOMException(
107
- `Failed to execute 'add' on 'DOMTokenList': The token provided ('${className}') contains HTML space characters, which are not valid in tokens.`
108
- );
109
- }
110
- this._list.push(className);
111
- }
112
- }
113
- this._list = Array.from(new Set(this._list));
114
- const value = this._list ? this._list.join(' ') : '';
115
- this._ownerElement.setAttribute('class', value);
116
- }
117
-
118
- /**
119
- * Removes class names.
120
- *
121
- * @param classNames Class names.
122
- */
123
- public remove(...classNames: string[]): void {
124
- this._refreshListFromClass();
125
- for (const className of classNames) {
126
- const index = this._list.indexOf(className);
127
- if (index !== -1) {
128
- this._list.splice(index, 1);
129
- }
130
- }
131
- const value = this._list ? this._list.join(' ') : '';
132
- this._ownerElement.setAttribute('class', value);
133
- }
134
-
135
- /**
136
- * Check if the list contains a class.
137
- *
138
- * @param className Class name.
139
- * @returns TRUE if it contains.
140
- */
141
- public contains(className: string): boolean {
142
- this._refreshListFromClass();
143
- return this._list.includes(className);
144
- }
145
-
146
- /**
147
- * Toggle a class name.
148
- *
149
- * @param className A string representing the class name you want to toggle.
150
- * @param force If included, turns the toggle into a one way-only operation. If set to `false`, then class name will only be removed, but not added. If set to `true`, then class name will only be added, but not removed.
151
- * @returns A boolean value, `true` or `false`, indicating whether class name is in the list after the call or not.
152
- */
153
- public toggle(className: string, force?: boolean): boolean {
154
- let shouldAdd: boolean;
155
- if (force !== undefined) {
156
- shouldAdd = force;
157
- } else {
158
- shouldAdd = !this.contains(className);
159
- }
160
-
161
- if (shouldAdd) {
162
- this.add(className);
163
- return true;
164
- }
165
-
166
- this.remove(className);
167
- return false;
168
- }
169
-
170
- /**
171
- * Refresh list from class.
172
- */
173
- private _refreshListFromClass(): void {
174
- const attr = this._ownerElement.getAttribute('class');
175
- this._list = attr ? Array.from(new Set(attr.split(' '))) : [];
176
- }
177
-
178
- /**
179
- * Set Value.
180
- */
181
- public set value(value: string) {
182
- this._ownerElement.setAttribute('class', value);
183
- this._list = value ? Array.from(new Set(value.split(' '))) : [];
184
- }
185
-
186
- /**
187
- * Get Value.
188
- */
189
- public get value(): string {
190
- this._refreshListFromClass();
191
- return this._list ? this._list.join(' ') : '';
192
- }
193
-
194
- /**
195
- * Get Length.
196
- */
197
- public get length(): number {
198
- this._refreshListFromClass();
199
- return this._list.length;
200
- }
201
- }