@schukai/monster 3.4.2 → 3.7.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.
Files changed (109) hide show
  1. package/example/i18n/providers/embed.mjs +5 -0
  2. package/package.json +1 -1
  3. package/source/constants.mjs +4 -9
  4. package/source/constraints/abstract.mjs +4 -6
  5. package/source/constraints/abstractoperator.mjs +6 -12
  6. package/source/constraints/andoperator.mjs +3 -6
  7. package/source/constraints/invalid.mjs +3 -6
  8. package/source/constraints/isarray.mjs +6 -9
  9. package/source/constraints/isobject.mjs +4 -7
  10. package/source/constraints/namespace.mjs +1 -3
  11. package/source/constraints/oroperator.mjs +26 -25
  12. package/source/constraints/valid.mjs +3 -6
  13. package/source/data/buildmap.mjs +20 -36
  14. package/source/data/buildtree.mjs +29 -29
  15. package/source/data/datasource/namespace.mjs +1 -1
  16. package/source/data/datasource/server/restapi/writeerror.mjs +6 -6
  17. package/source/data/datasource/server/restapi.mjs +51 -65
  18. package/source/data/datasource/server/webconnect.mjs +26 -35
  19. package/source/data/datasource/server.mjs +17 -21
  20. package/source/data/datasource/storage/localstorage.mjs +8 -11
  21. package/source/data/datasource/storage/sessionstorage.mjs +8 -10
  22. package/source/data/datasource/storage.mjs +18 -22
  23. package/source/data/datasource.mjs +27 -43
  24. package/source/data/diff.mjs +20 -32
  25. package/source/data/extend.mjs +20 -14
  26. package/source/data/namespace.mjs +1 -3
  27. package/source/data/pathfinder.mjs +40 -67
  28. package/source/data/pipe.mjs +6 -10
  29. package/source/data/transformer.mjs +141 -157
  30. package/source/dom/assembler.mjs +6 -9
  31. package/source/dom/attributes.mjs +36 -48
  32. package/source/dom/constants.mjs +55 -71
  33. package/source/dom/customcontrol.mjs +16 -21
  34. package/source/dom/customelement.mjs +140 -164
  35. package/source/dom/events.mjs +17 -24
  36. package/source/dom/focusmanager.mjs +30 -48
  37. package/source/dom/locale.mjs +41 -10
  38. package/source/dom/namespace.mjs +1 -1
  39. package/source/dom/ready.mjs +7 -10
  40. package/source/dom/resource/data.mjs +38 -46
  41. package/source/dom/resource/link/namespace.mjs +1 -3
  42. package/source/dom/resource/link/stylesheet.mjs +7 -11
  43. package/source/dom/resource/link.mjs +36 -17
  44. package/source/dom/resource/namespace.mjs +1 -1
  45. package/source/dom/resource/script.mjs +23 -13
  46. package/source/dom/resource.mjs +47 -60
  47. package/source/dom/resourcemanager.mjs +36 -43
  48. package/source/dom/template.mjs +17 -24
  49. package/source/dom/theme.mjs +8 -12
  50. package/source/dom/updater.mjs +150 -196
  51. package/source/dom/util.mjs +11 -12
  52. package/source/dom/worker/factory.mjs +21 -25
  53. package/source/dom/worker/namespace.mjs +1 -1
  54. package/source/i18n/formatter.mjs +20 -24
  55. package/source/i18n/locale.mjs +40 -42
  56. package/source/i18n/namespace.mjs +1 -1
  57. package/source/i18n/provider.mjs +4 -7
  58. package/source/i18n/providers/embed.mjs +115 -0
  59. package/source/i18n/providers/fetch.mjs +33 -38
  60. package/source/i18n/translations.mjs +16 -20
  61. package/source/logging/handler/console.mjs +6 -10
  62. package/source/logging/handler.mjs +15 -18
  63. package/source/logging/logentry.mjs +8 -9
  64. package/source/logging/logger.mjs +73 -55
  65. package/source/logging/namespace.mjs +1 -1
  66. package/source/math/namespace.mjs +1 -1
  67. package/source/math/random.mjs +13 -14
  68. package/source/monster.mjs +132 -100
  69. package/source/net/namespace.mjs +1 -3
  70. package/source/net/webconnect/message.mjs +6 -8
  71. package/source/net/webconnect/namespace.mjs +1 -3
  72. package/source/net/webconnect.mjs +28 -39
  73. package/source/text/formatter.mjs +44 -55
  74. package/source/types/base.mjs +16 -26
  75. package/source/types/basewithoptions.mjs +8 -14
  76. package/source/types/binary.mjs +4 -5
  77. package/source/types/dataurl.mjs +19 -27
  78. package/source/types/global.mjs +14 -26
  79. package/source/types/id.mjs +4 -6
  80. package/source/types/is.mjs +11 -16
  81. package/source/types/mediatype.mjs +29 -44
  82. package/source/types/namespace.mjs +0 -1
  83. package/source/types/node.mjs +16 -22
  84. package/source/types/nodelist.mjs +10 -14
  85. package/source/types/noderecursiveiterator.mjs +11 -14
  86. package/source/types/observablequeue.mjs +13 -16
  87. package/source/types/observer.mjs +16 -23
  88. package/source/types/observerlist.mjs +18 -21
  89. package/source/types/proxyobserver.mjs +24 -37
  90. package/source/types/queue.mjs +8 -13
  91. package/source/types/randomid.mjs +10 -10
  92. package/source/types/regex.mjs +3 -5
  93. package/source/types/stack.mjs +4 -8
  94. package/source/types/tokenlist.mjs +24 -31
  95. package/source/types/typeof.mjs +5 -7
  96. package/source/types/uniquequeue.mjs +8 -13
  97. package/source/types/uuid.mjs +18 -25
  98. package/source/types/validate.mjs +26 -27
  99. package/source/types/version.mjs +17 -25
  100. package/source/util/clone.mjs +23 -32
  101. package/source/util/comparator.mjs +7 -15
  102. package/source/util/deadmansswitch.mjs +16 -23
  103. package/source/util/freeze.mjs +5 -7
  104. package/source/util/namespace.mjs +1 -1
  105. package/source/util/processing.mjs +33 -39
  106. package/source/util/trimspaces.mjs +17 -24
  107. package/test/cases/dom/locale.mjs +15 -0
  108. package/test/cases/i18n/providers/embed.mjs +72 -0
  109. package/test/cases/monster.mjs +1 -1
@@ -5,10 +5,10 @@
5
5
  * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
6
6
  */
7
7
 
8
- import {getGlobal} from "../types/global.mjs";
9
- import {validateString} from "../types/validate.mjs";
8
+ import { getGlobal } from "../types/global.mjs";
9
+ import { validateString } from "../types/validate.mjs";
10
10
 
11
- export {getDocument, getWindow, getDocumentFragmentFromString}
11
+ export { getDocument, getWindow, getDocumentFragmentFromString };
12
12
 
13
13
  /**
14
14
  * This method fetches the document object
@@ -48,9 +48,9 @@ export {getDocument, getWindow, getDocumentFragmentFromString}
48
48
  * @throws {Error} not supported environment
49
49
  */
50
50
  function getDocument() {
51
- let document = getGlobal()?.['document'];
52
- if (typeof document !== 'object') {
53
- throw new Error("not supported environment")
51
+ let document = getGlobal()?.["document"];
52
+ if (typeof document !== "object") {
53
+ throw new Error("not supported environment");
54
54
  }
55
55
 
56
56
  return document;
@@ -70,7 +70,7 @@ function getDocument() {
70
70
  * });
71
71
  *
72
72
  * getGlobal()['window']=window;
73
- *
73
+ *
74
74
  * [
75
75
  * 'self',
76
76
  * 'document',
@@ -96,15 +96,14 @@ function getDocument() {
96
96
  * @throws {Error} not supported environment
97
97
  */
98
98
  function getWindow() {
99
- let window = getGlobal()?.['window'];
100
- if (typeof window !== 'object') {
101
- throw new Error("not supported environment")
99
+ let window = getGlobal()?.["window"];
100
+ if (typeof window !== "object") {
101
+ throw new Error("not supported environment");
102
102
  }
103
103
 
104
104
  return window;
105
105
  }
106
106
 
107
-
108
107
  /**
109
108
  * This method fetches the document object
110
109
  *
@@ -147,7 +146,7 @@ function getDocumentFragmentFromString(html) {
147
146
  validateString(html);
148
147
 
149
148
  const document = getDocument();
150
- const template = document.createElement('template');
149
+ const template = document.createElement("template");
151
150
  template.innerHTML = html;
152
151
 
153
152
  return template.content;
@@ -5,17 +5,17 @@
5
5
  * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
6
6
  */
7
7
 
8
- import {internalSymbol} from "../../constants.mjs";
9
- import {Base} from "../../types/base.mjs";
10
- import {getGlobal, getGlobalFunction} from "../../types/global.mjs";
11
- import {isFunction} from "../../types/is.mjs";
12
- import {validateInstance, validateString} from "../../types/validate.mjs";
8
+ import { internalSymbol } from "../../constants.mjs";
9
+ import { Base } from "../../types/base.mjs";
10
+ import { getGlobal, getGlobalFunction } from "../../types/global.mjs";
11
+ import { isFunction } from "../../types/is.mjs";
12
+ import { validateInstance, validateString } from "../../types/validate.mjs";
13
13
 
14
- export {Factory}
14
+ export { Factory };
15
15
 
16
16
  /**
17
17
  * A factory for creating worker instances.
18
- *
18
+ *
19
19
  * @license AGPLv3
20
20
  * @since 1.25.0
21
21
  * @copyright schukai GmbH
@@ -23,15 +23,14 @@ export {Factory}
23
23
  * @summary A small factory to create worker
24
24
  */
25
25
  class Factory extends Base {
26
-
27
26
  /**
28
27
  *
29
28
  */
30
29
  constructor() {
31
30
  super();
32
31
  this[internalSymbol] = {
33
- worker: new WeakMap
34
- }
32
+ worker: new WeakMap(),
33
+ };
35
34
  }
36
35
 
37
36
  /**
@@ -43,24 +42,23 @@ class Factory extends Base {
43
42
  * @return {Worker}
44
43
  */
45
44
  createFromURL = function (url, messageHandler, errorHandler) {
46
-
47
45
  if (url instanceof URL) {
48
46
  url = url.toString();
49
47
  }
50
48
 
51
- const workerClass = getGlobalFunction('Worker');
49
+ const workerClass = getGlobalFunction("Worker");
52
50
  var worker = new workerClass(validateString(url));
53
51
 
54
52
  if (isFunction(messageHandler)) {
55
53
  worker.onmessage = (event) => {
56
54
  messageHandler.call(worker, event);
57
- }
55
+ };
58
56
  }
59
57
 
60
58
  if (isFunction(errorHandler)) {
61
59
  worker.onerror = (event) => {
62
60
  errorHandler.call(worker, event);
63
- }
61
+ };
64
62
  }
65
63
 
66
64
  return worker;
@@ -76,16 +74,17 @@ class Factory extends Base {
76
74
  * @see https://developer.mozilla.org/de/docs/Web/API/URL/createObjectURL
77
75
  */
78
76
  createFromScript = function (content, messageHandler, errorHandler) {
79
- const blobFunction = new getGlobalFunction('Blob')
80
- const blob = new blobFunction([validateString(content)], {type: 'script/javascript'});
77
+ const blobFunction = new getGlobalFunction("Blob");
78
+ const blob = new blobFunction([validateString(content)], {
79
+ type: "script/javascript",
80
+ });
81
81
 
82
- const url = getGlobalFunction('URL').createObjectURL(blob);
82
+ const url = getGlobalFunction("URL").createObjectURL(blob);
83
83
  const worker = this.createFromURL(url, messageHandler, errorHandler);
84
84
 
85
- this[internalSymbol]['worker'].set(worker, url);
85
+ this[internalSymbol]["worker"].set(worker, url);
86
86
 
87
87
  return worker;
88
-
89
88
  };
90
89
 
91
90
  /**
@@ -95,19 +94,16 @@ class Factory extends Base {
95
94
  * @return {Monster.DOM.Worker.Factory}
96
95
  */
97
96
  terminate(worker) {
98
-
99
- const workerClass = getGlobalFunction('Worker');
97
+ const workerClass = getGlobalFunction("Worker");
100
98
  validateInstance(worker, workerClass);
101
99
 
102
100
  worker.terminate();
103
101
 
104
- if (this[internalSymbol]['worker'].has(worker)) {
105
- const url = this[internalSymbol]['worker'].get(worker);
102
+ if (this[internalSymbol]["worker"].has(worker)) {
103
+ const url = this[internalSymbol]["worker"].get(worker);
106
104
  URL.revokeObjectURL(url);
107
105
  }
108
106
 
109
107
  return this;
110
108
  }
111
-
112
-
113
109
  }
@@ -10,4 +10,4 @@
10
10
  * @memberOf Monster
11
11
  * @author schukai GmbH
12
12
  */
13
- const ns = {};
13
+ const ns = {};
@@ -5,20 +5,20 @@
5
5
  * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
6
6
  */
7
7
 
8
- import {internalSymbol} from "../constants.mjs";
9
- import {extend} from "../data/extend.mjs";
8
+ import { internalSymbol } from "../constants.mjs";
9
+ import { extend } from "../data/extend.mjs";
10
10
 
11
- import {Formatter as TextFormatter} from "../text/formatter.mjs";
12
- import {validateInstance, validateString} from "../types/validate.mjs";
13
- import {Translations} from "./translations.mjs";
11
+ import { Formatter as TextFormatter } from "../text/formatter.mjs";
12
+ import { validateInstance, validateString } from "../types/validate.mjs";
13
+ import { Translations } from "./translations.mjs";
14
14
 
15
- export {Formatter}
15
+ export { Formatter };
16
16
 
17
17
  /**
18
18
  * @private
19
19
  * @type {symbol}
20
20
  */
21
- const internalTranslationSymbol = Symbol('internalTranslation')
21
+ const internalTranslationSymbol = Symbol("internalTranslation");
22
22
 
23
23
  /**
24
24
  * The Formatter extends the Text.Formatter with the possibility to replace the key by a translation.
@@ -30,7 +30,6 @@ const internalTranslationSymbol = Symbol('internalTranslation')
30
30
  * @memberOf Monster.I18n
31
31
  */
32
32
  class Formatter extends TextFormatter {
33
-
34
33
  /**
35
34
  * Default values for the markers are `${` and `}`
36
35
  *
@@ -58,13 +57,13 @@ class Formatter extends TextFormatter {
58
57
  callbacks: {
59
58
  i18n: (value) => {
60
59
  return self[internalTranslationSymbol].getText(validateString(value));
61
- }
60
+ },
62
61
  },
63
62
  marker: {
64
- open: ['i18n{', '${'],
65
- close: ['}'],
63
+ open: ["i18n{", "${"],
64
+ close: ["}"],
66
65
  },
67
- })
66
+ });
68
67
  }
69
68
 
70
69
  /**
@@ -77,10 +76,10 @@ class Formatter extends TextFormatter {
77
76
  * @throws {Error} the closing marker is missing
78
77
  */
79
78
  format(text) {
80
- validateString(text)
79
+ validateString(text);
81
80
 
82
- const openMarker = this[internalSymbol]['marker']['open']?.[0];
83
- const closeMarker = this[internalSymbol]['marker']['close']?.[0];
81
+ const openMarker = this[internalSymbol]["marker"]["open"]?.[0];
82
+ const closeMarker = this[internalSymbol]["marker"]["close"]?.[0];
84
83
 
85
84
  if (text.indexOf(openMarker) === 0) {
86
85
  text = text.substring(openMarker.length);
@@ -88,22 +87,19 @@ class Formatter extends TextFormatter {
88
87
  if (text.indexOf(closeMarker) === text.length - closeMarker.length) {
89
88
  text = text.substring(0, text.length - closeMarker.length);
90
89
  } else {
91
- throw new Error("the closing marker is missing")
90
+ throw new Error("the closing marker is missing");
92
91
  }
93
92
  }
94
-
95
- const parts = validateString(text).split('::')
96
- const translationKey = parts.shift().trim(); // key value delimiter
97
- const parameter = parts.join('::').trim();
98
93
 
94
+ const parts = validateString(text).split("::");
95
+ const translationKey = parts.shift().trim(); // key value delimiter
96
+ const parameter = parts.join("::").trim();
99
97
 
100
- let assembledText = openMarker + 'static:' + translationKey + ' | call:i18n';
98
+ let assembledText = `${openMarker}static:${translationKey} | call:i18n`;
101
99
  if (parameter.length > 0) {
102
- assembledText += '::' + parameter;
100
+ assembledText += `::${parameter}`;
103
101
  }
104
102
  assembledText += closeMarker;
105
103
  return super.format(assembledText);
106
104
  }
107
-
108
-
109
105
  }
@@ -5,23 +5,23 @@
5
5
  * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
6
6
  */
7
7
 
8
- import {Base} from "../types/base.mjs";
9
- import {validateString} from "../types/validate.mjs";
10
- import {clone} from "../util/clone.mjs";
8
+ import { Base } from "../types/base.mjs";
9
+ import { validateString } from "../types/validate.mjs";
10
+ import { clone } from "../util/clone.mjs";
11
11
 
12
- export {Locale, parseLocale}
12
+ export { Locale, parseLocale };
13
13
 
14
14
  /**
15
15
  * @memberOf Monster.I18n
16
16
  * @type {symbol}
17
17
  */
18
- const propertiesSymbol = Symbol('properties');
18
+ const propertiesSymbol = Symbol("properties");
19
19
 
20
20
  /**
21
21
  * @type {symbol}
22
22
  * @memberOf Monster.I18n
23
23
  */
24
- const localeStringSymbol = Symbol('localeString');
24
+ const localeStringSymbol = Symbol("localeString");
25
25
 
26
26
  /**
27
27
  * The Locale class is a base class for the language classes.
@@ -64,7 +64,6 @@ const localeStringSymbol = Symbol('localeString');
64
64
  * @see https://datatracker.ietf.org/doc/html/rfc3066
65
65
  */
66
66
  class Locale extends Base {
67
-
68
67
  /**
69
68
  * @param {string} language
70
69
  * @param {string} [region]
@@ -78,12 +77,12 @@ class Locale extends Base {
78
77
  super();
79
78
 
80
79
  this[propertiesSymbol] = {
81
- language: (language === undefined) ? undefined : validateString(language),
82
- script: (script === undefined) ? undefined : validateString(script),
83
- region: (region === undefined) ? undefined : validateString(region),
84
- variants: (variants === undefined) ? undefined : validateString(variants),
85
- extlang: (extlang === undefined) ? undefined : validateString(extlang),
86
- privateUse: (privateUse === undefined) ? undefined : validateString(privateUse),
80
+ language: language === undefined ? undefined : validateString(language),
81
+ script: script === undefined ? undefined : validateString(script),
82
+ region: region === undefined ? undefined : validateString(region),
83
+ variants: variants === undefined ? undefined : validateString(variants),
84
+ extlang: extlang === undefined ? undefined : validateString(extlang),
85
+ privateUse: privateUse === undefined ? undefined : validateString(privateUse),
87
86
  };
88
87
 
89
88
  let s = [];
@@ -95,11 +94,10 @@ class Locale extends Base {
95
94
  if (privateUse !== undefined) s.push(privateUse);
96
95
 
97
96
  if (s.length === 0) {
98
- throw new Error('unsupported locale');
97
+ throw new Error("unsupported locale");
99
98
  }
100
99
 
101
- this[localeStringSymbol] = s.join('-');
102
-
100
+ this[localeStringSymbol] = s.join("-");
103
101
  }
104
102
 
105
103
  /**
@@ -151,12 +149,11 @@ class Locale extends Base {
151
149
  return this[propertiesSymbol].privateValue;
152
150
  }
153
151
 
154
-
155
152
  /**
156
153
  * @return {string}
157
154
  */
158
155
  toString() {
159
- return "" + this.localeString;
156
+ return `${this.localeString}`;
160
157
  }
161
158
 
162
159
  /**
@@ -165,10 +162,8 @@ class Locale extends Base {
165
162
  * @return {Monster.I18n.LocaleMap}
166
163
  */
167
164
  getMap() {
168
- return clone(this[propertiesSymbol])
165
+ return clone(this[propertiesSymbol]);
169
166
  }
170
-
171
-
172
167
  }
173
168
 
174
169
  /**
@@ -263,25 +258,30 @@ class Locale extends Base {
263
258
  * @throws {Error} unsupported locale
264
259
  */
265
260
  function parseLocale(locale) {
266
-
267
261
  locale = validateString(locale).replace(/_/g, "-");
268
262
 
269
- let language, region, variants, parts, script, extlang,
270
- regexRegular = "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang)",
271
- regexIrregular = "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)",
272
- regexGrandfathered = "(" + regexIrregular + "|" + regexRegular + ")",
273
- regexPrivateUse = "(x(-[A-Za-z0-9]{1,8})+)",
274
- regexSingleton = "[0-9A-WY-Za-wy-z]",
275
- regexExtension = "(" + regexSingleton + "(-[A-Za-z0-9]{2,8})+)",
276
- regexVariant = "([A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3})",
277
- regexRegion = "([A-Za-z]{2}|[0-9]{3})",
278
- regexScript = "([A-Za-z]{4})",
279
- regexExtlang = "([A-Za-z]{3}(-[A-Za-z]{3}){0,2})",
280
- regexLanguage = "(([A-Za-z]{2,3}(-" + regexExtlang + ")?)|[A-Za-z]{4}|[A-Za-z]{5,8})",
281
- regexLangtag = "(" + regexLanguage + "(-" + regexScript + ")?" + "(-" + regexRegion + ")?" + "(-" + regexVariant + ")*" + "(-" + regexExtension + ")*" + "(-" + regexPrivateUse + ")?" + ")",
282
- regexLanguageTag = "^(" + regexGrandfathered + "|" + regexLangtag + "|" + regexPrivateUse + ")$",
283
- regex = new RegExp(regexLanguageTag), match;
284
-
263
+ let language;
264
+ let region;
265
+ let variants;
266
+ let parts;
267
+ let script;
268
+ let extlang;
269
+ let regexRegular = "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang)";
270
+ let regexIrregular =
271
+ "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)";
272
+ let regexGrandfathered = `(${regexIrregular}|${regexRegular})`;
273
+ let regexPrivateUse = "(x(-[A-Za-z0-9]{1,8})+)";
274
+ let regexSingleton = "[0-9A-WY-Za-wy-z]";
275
+ let regexExtension = `(${regexSingleton}(-[A-Za-z0-9]{2,8})+)`;
276
+ let regexVariant = "([A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3})";
277
+ let regexRegion = "([A-Za-z]{2}|[0-9]{3})";
278
+ let regexScript = "([A-Za-z]{4})";
279
+ let regexExtlang = "([A-Za-z]{3}(-[A-Za-z]{3}){0,2})";
280
+ let regexLanguage = `(([A-Za-z]{2,3}(-${regexExtlang})?)|[A-Za-z]{4}|[A-Za-z]{5,8})`;
281
+ let regexLangtag = `(${regexLanguage}(-${regexScript})?(-${regexRegion})?(-${regexVariant})*(-${regexExtension})*(-${regexPrivateUse})?)`;
282
+ let regexLanguageTag = `^(${regexGrandfathered}|${regexLangtag}|${regexPrivateUse})$`;
283
+ let regex = new RegExp(regexLanguageTag);
284
+ let match;
285
285
 
286
286
  if ((match = regex.exec(locale)) !== null) {
287
287
  if (match.index === regex.lastIndex) {
@@ -290,18 +290,17 @@ function parseLocale(locale) {
290
290
  }
291
291
 
292
292
  if (match === undefined || match === null) {
293
- throw new Error('unsupported locale');
293
+ throw new Error("unsupported locale");
294
294
  }
295
295
 
296
296
  if (match[6] !== undefined) {
297
297
  language = match[6];
298
298
 
299
- parts = language.split('-');
299
+ parts = language.split("-");
300
300
  if (parts.length > 1) {
301
301
  language = parts[0];
302
302
  extlang = parts[1];
303
303
  }
304
-
305
304
  }
306
305
 
307
306
  if (match[14] !== undefined) {
@@ -317,5 +316,4 @@ function parseLocale(locale) {
317
316
  }
318
317
 
319
318
  return new Locale(language, region, script, variants, extlang);
320
-
321
319
  }
@@ -10,4 +10,4 @@
10
10
  * @memberOf Monster
11
11
  * @author schukai GmbH
12
12
  */
13
- const ns = {};
13
+ const ns = {};
@@ -5,11 +5,11 @@
5
5
  * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
6
6
  */
7
7
 
8
- import {BaseWithOptions} from "../types/basewithoptions.mjs";
9
- import {Locale} from "./locale.mjs"
10
- import {Translations} from "./translations.mjs"
8
+ import { BaseWithOptions } from "../types/basewithoptions.mjs";
9
+ import { Locale } from "./locale.mjs";
10
+ import { Translations } from "./translations.mjs";
11
11
 
12
- export {Provider}
12
+ export { Provider };
13
13
 
14
14
  /**
15
15
  * A provider makes a translation object available.
@@ -21,7 +21,6 @@ export {Provider}
21
21
  * @see {@link https://datatracker.ietf.org/doc/html/rfc3066}
22
22
  */
23
23
  class Provider extends BaseWithOptions {
24
-
25
24
  /**
26
25
  * @param {Locale|string} locale
27
26
  * @return {Promise}
@@ -33,8 +32,6 @@ class Provider extends BaseWithOptions {
33
32
  } catch (e) {
34
33
  reject(e);
35
34
  }
36
-
37
35
  });
38
36
  }
39
-
40
37
  }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Copyright schukai GmbH and contributors 2022. All Rights Reserved.
3
+ * Node module: @schukai/monster
4
+ * This file is licensed under the AGPLv3 License.
5
+ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
6
+ */
7
+
8
+ import { internalSymbol } from "../../constants.mjs";
9
+ import { extend } from "../../data/extend.mjs";
10
+ import { getGlobalObject } from "../../types/global.mjs";
11
+ import { isString } from "../../types/is.mjs";
12
+ import { validateObject, validateString } from "../../types/validate.mjs";
13
+ import { parseLocale } from "../locale.mjs";
14
+ import { Provider } from "../provider.mjs";
15
+ import { Translations } from "../translations.mjs";
16
+
17
+ export { Embed };
18
+
19
+ /**
20
+ * The Embed provider retrieves a JSON file from the given Script Tag.
21
+ *
22
+ * @externalExample ../../../example/i18n/providers/embed.mjs
23
+ * @license AGPLv3
24
+ * @since 1.13.0
25
+ * @copyright schukai GmbH
26
+ * @memberOf Monster.I18n.Providers
27
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc3066}
28
+ * @tutorial i18n-locale-and-formatter
29
+ */
30
+ class Embed extends Provider {
31
+ /**
32
+ * ```html
33
+ * <script id="translations" type="application/json">
34
+ * {
35
+ * "hello": "Hallo"
36
+ * }
37
+ * </script>
38
+ * ```
39
+ *
40
+ *
41
+ * ```javascript
42
+ * new Embed('translations')
43
+ * ```
44
+ *
45
+ * @param {string} id
46
+ * @param {Object} options
47
+ */
48
+ constructor(id, options) {
49
+ super(options);
50
+
51
+ if (options === undefined) {
52
+ options = {};
53
+ }
54
+
55
+ validateString(id);
56
+
57
+ /**
58
+ * @property {string}
59
+ */
60
+ this.textId = id;
61
+
62
+ /**
63
+ * @private
64
+ * @property {Object} options
65
+ */
66
+ this[internalSymbol] = extend({}, super.defaults, this.defaults, validateObject(options));
67
+ }
68
+
69
+ /**
70
+ * Defaults
71
+ *
72
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API}
73
+ */
74
+ get defaults() {
75
+ return extend({}, super.defaults);
76
+ }
77
+
78
+ /**
79
+ *
80
+ * @param {Locale|string} locale
81
+ * @return {Promise}
82
+ */
83
+ getTranslations(locale) {
84
+ if (isString(locale)) {
85
+ locale = parseLocale(locale);
86
+ }
87
+
88
+ return new Promise((resolve, reject) => {
89
+ let text = getGlobalObject("document").getElementById(this.textId);
90
+
91
+ if (text === null) {
92
+ reject(new Error("Text not found"));
93
+ return;
94
+ }
95
+
96
+ let translations = null;
97
+ try {
98
+ translations = JSON.parse(text.innerHTML);
99
+ } catch (e) {
100
+ reject(e);
101
+ return;
102
+ }
103
+
104
+ if (translations === null) {
105
+ reject(new Error("Translations not found or invalid"));
106
+ return;
107
+ }
108
+
109
+ const t = new Translations(locale);
110
+ t.assignTranslations(translations);
111
+
112
+ resolve(t);
113
+ });
114
+ }
115
+ }