@schukai/monster 3.5.0 → 3.8.0
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/source/constants.mjs +4 -9
- package/source/constraints/abstract.mjs +4 -6
- package/source/constraints/abstractoperator.mjs +6 -12
- package/source/constraints/andoperator.mjs +3 -6
- package/source/constraints/invalid.mjs +3 -6
- package/source/constraints/isarray.mjs +6 -9
- package/source/constraints/isobject.mjs +4 -7
- package/source/constraints/namespace.mjs +1 -3
- package/source/constraints/oroperator.mjs +26 -25
- package/source/constraints/valid.mjs +3 -6
- package/source/data/buildmap.mjs +20 -36
- package/source/data/buildtree.mjs +29 -29
- package/source/data/datasource/namespace.mjs +1 -1
- package/source/data/datasource/server/restapi/writeerror.mjs +6 -6
- package/source/data/datasource/server/restapi.mjs +51 -65
- package/source/data/datasource/server/webconnect.mjs +26 -35
- package/source/data/datasource/server.mjs +16 -20
- package/source/data/datasource/storage/localstorage.mjs +8 -11
- package/source/data/datasource/storage/sessionstorage.mjs +8 -10
- package/source/data/datasource/storage.mjs +18 -22
- package/source/data/datasource.mjs +27 -43
- package/source/data/diff.mjs +20 -32
- package/source/data/extend.mjs +20 -14
- package/source/data/namespace.mjs +1 -3
- package/source/data/pathfinder.mjs +105 -87
- package/source/data/pipe.mjs +6 -10
- package/source/data/transformer.mjs +141 -157
- package/source/dom/assembler.mjs +6 -9
- package/source/dom/attributes.mjs +36 -48
- package/source/dom/constants.mjs +55 -71
- package/source/dom/customcontrol.mjs +16 -21
- package/source/dom/customelement.mjs +140 -164
- package/source/dom/events.mjs +17 -24
- package/source/dom/focusmanager.mjs +31 -49
- package/source/dom/locale.mjs +18 -20
- package/source/dom/namespace.mjs +1 -1
- package/source/dom/ready.mjs +7 -10
- package/source/dom/resource/data.mjs +38 -46
- package/source/dom/resource/link/namespace.mjs +1 -3
- package/source/dom/resource/link/stylesheet.mjs +7 -11
- package/source/dom/resource/link.mjs +36 -17
- package/source/dom/resource/namespace.mjs +1 -1
- package/source/dom/resource/script.mjs +23 -13
- package/source/dom/resource.mjs +47 -60
- package/source/dom/resourcemanager.mjs +36 -43
- package/source/dom/template.mjs +17 -24
- package/source/dom/theme.mjs +8 -12
- package/source/dom/updater.mjs +150 -196
- package/source/dom/util.mjs +11 -12
- package/source/dom/worker/factory.mjs +21 -25
- package/source/dom/worker/namespace.mjs +1 -1
- package/source/i18n/formatter.mjs +20 -24
- package/source/i18n/locale.mjs +42 -55
- package/source/i18n/namespace.mjs +1 -1
- package/source/i18n/provider.mjs +4 -7
- package/source/i18n/providers/embed.mjs +16 -26
- package/source/i18n/providers/fetch.mjs +30 -36
- package/source/i18n/translations.mjs +17 -24
- package/source/logging/handler/console.mjs +6 -10
- package/source/logging/handler.mjs +15 -18
- package/source/logging/logentry.mjs +8 -9
- package/source/logging/logger.mjs +73 -55
- package/source/logging/namespace.mjs +1 -1
- package/source/math/namespace.mjs +1 -1
- package/source/math/random.mjs +13 -14
- package/source/monster.mjs +132 -100
- package/source/net/namespace.mjs +1 -3
- package/source/net/webconnect/message.mjs +6 -8
- package/source/net/webconnect/namespace.mjs +1 -3
- package/source/net/webconnect.mjs +28 -39
- package/source/text/formatter.mjs +44 -55
- package/source/types/base.mjs +16 -26
- package/source/types/basewithoptions.mjs +8 -14
- package/source/types/binary.mjs +4 -5
- package/source/types/dataurl.mjs +19 -27
- package/source/types/global.mjs +14 -26
- package/source/types/id.mjs +4 -6
- package/source/types/is.mjs +11 -16
- package/source/types/mediatype.mjs +29 -44
- package/source/types/namespace.mjs +0 -1
- package/source/types/node.mjs +16 -22
- package/source/types/nodelist.mjs +10 -14
- package/source/types/noderecursiveiterator.mjs +11 -14
- package/source/types/observablequeue.mjs +13 -16
- package/source/types/observer.mjs +16 -23
- package/source/types/observerlist.mjs +18 -21
- package/source/types/proxyobserver.mjs +24 -37
- package/source/types/queue.mjs +8 -13
- package/source/types/randomid.mjs +10 -10
- package/source/types/regex.mjs +3 -5
- package/source/types/stack.mjs +4 -8
- package/source/types/tokenlist.mjs +24 -31
- package/source/types/typeof.mjs +5 -7
- package/source/types/uniquequeue.mjs +8 -13
- package/source/types/uuid.mjs +18 -25
- package/source/types/validate.mjs +26 -27
- package/source/types/version.mjs +17 -25
- package/source/util/clone.mjs +23 -32
- package/source/util/comparator.mjs +7 -15
- package/source/util/deadmansswitch.mjs +16 -23
- package/source/util/freeze.mjs +5 -7
- package/source/util/namespace.mjs +1 -1
- package/source/util/processing.mjs +33 -39
- package/source/util/trimspaces.mjs +17 -24
- package/test/cases/monster.mjs +1 -1
package/source/dom/util.mjs
CHANGED
@@ -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()?.[
|
52
|
-
if (typeof document !==
|
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()?.[
|
100
|
-
if (typeof window !==
|
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(
|
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(
|
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(
|
80
|
-
const blob = new blobFunction([validateString(content)], {
|
77
|
+
const blobFunction = new getGlobalFunction("Blob");
|
78
|
+
const blob = new blobFunction([validateString(content)], {
|
79
|
+
type: "script/javascript",
|
80
|
+
});
|
81
81
|
|
82
|
-
const url = getGlobalFunction(
|
82
|
+
const url = getGlobalFunction("URL").createObjectURL(blob);
|
83
83
|
const worker = this.createFromURL(url, messageHandler, errorHandler);
|
84
84
|
|
85
|
-
this[internalSymbol][
|
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][
|
105
|
-
const url = this[internalSymbol][
|
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
|
}
|
@@ -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(
|
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: [
|
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][
|
83
|
-
const closeMarker = this[internalSymbol][
|
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
|
98
|
+
let assembledText = `${openMarker}static:${translationKey} | call:i18n`;
|
101
99
|
if (parameter.length > 0) {
|
102
|
-
assembledText +=
|
100
|
+
assembledText += `::${parameter}`;
|
103
101
|
}
|
104
102
|
assembledText += closeMarker;
|
105
103
|
return super.format(assembledText);
|
106
104
|
}
|
107
|
-
|
108
|
-
|
109
105
|
}
|
package/source/i18n/locale.mjs
CHANGED
@@ -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(
|
18
|
+
const propertiesSymbol = Symbol("properties");
|
19
19
|
|
20
20
|
/**
|
21
21
|
* @type {symbol}
|
22
22
|
* @memberOf Monster.I18n
|
23
23
|
*/
|
24
|
-
const localeStringSymbol = Symbol(
|
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:
|
82
|
-
script:
|
83
|
-
region:
|
84
|
-
variants:
|
85
|
-
extlang:
|
86
|
-
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(
|
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
|
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
|
/**
|
@@ -187,22 +182,11 @@ class Locale extends Base {
|
|
187
182
|
*
|
188
183
|
* Limitations: The regex cannot handle multiple variants or private.
|
189
184
|
*
|
190
|
-
* You can call the method via
|
191
|
-
*
|
192
|
-
* ```
|
193
|
-
* <script type="module">
|
194
|
-
* import {Monster} from '@schukai/monster/source/monster.mjs';
|
195
|
-
* new Monster.I18n.createLocale()
|
196
|
-
* </script>
|
197
|
-
* ```
|
198
|
-
*
|
199
|
-
* Alternatively, you can also integrate this function individually.
|
185
|
+
* You can call the method via this function individually:
|
200
186
|
*
|
201
|
-
* ```
|
202
|
-
* <script type="module">
|
187
|
+
* ```javascript
|
203
188
|
* import {createLocale} from '@schukai/monster/source/i18n/locale.mjs';
|
204
189
|
* createLocale()
|
205
|
-
* </script>
|
206
190
|
* ```
|
207
191
|
*
|
208
192
|
* RFC
|
@@ -263,25 +247,30 @@ class Locale extends Base {
|
|
263
247
|
* @throws {Error} unsupported locale
|
264
248
|
*/
|
265
249
|
function parseLocale(locale) {
|
266
|
-
|
267
250
|
locale = validateString(locale).replace(/_/g, "-");
|
268
251
|
|
269
|
-
let language
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
252
|
+
let language;
|
253
|
+
let region;
|
254
|
+
let variants;
|
255
|
+
let parts;
|
256
|
+
let script;
|
257
|
+
let extlang;
|
258
|
+
let regexRegular = "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang)";
|
259
|
+
let regexIrregular =
|
260
|
+
"(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)";
|
261
|
+
let regexGrandfathered = `(${regexIrregular}|${regexRegular})`;
|
262
|
+
let regexPrivateUse = "(x(-[A-Za-z0-9]{1,8})+)";
|
263
|
+
let regexSingleton = "[0-9A-WY-Za-wy-z]";
|
264
|
+
let regexExtension = `(${regexSingleton}(-[A-Za-z0-9]{2,8})+)`;
|
265
|
+
let regexVariant = "([A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3})";
|
266
|
+
let regexRegion = "([A-Za-z]{2}|[0-9]{3})";
|
267
|
+
let regexScript = "([A-Za-z]{4})";
|
268
|
+
let regexExtlang = "([A-Za-z]{3}(-[A-Za-z]{3}){0,2})";
|
269
|
+
let regexLanguage = `(([A-Za-z]{2,3}(-${regexExtlang})?)|[A-Za-z]{4}|[A-Za-z]{5,8})`;
|
270
|
+
let regexLangtag = `(${regexLanguage}(-${regexScript})?(-${regexRegion})?(-${regexVariant})*(-${regexExtension})*(-${regexPrivateUse})?)`;
|
271
|
+
let regexLanguageTag = `^(${regexGrandfathered}|${regexLangtag}|${regexPrivateUse})$`;
|
272
|
+
let regex = new RegExp(regexLanguageTag);
|
273
|
+
let match;
|
285
274
|
|
286
275
|
if ((match = regex.exec(locale)) !== null) {
|
287
276
|
if (match.index === regex.lastIndex) {
|
@@ -290,18 +279,17 @@ function parseLocale(locale) {
|
|
290
279
|
}
|
291
280
|
|
292
281
|
if (match === undefined || match === null) {
|
293
|
-
throw new Error(
|
282
|
+
throw new Error("unsupported locale");
|
294
283
|
}
|
295
284
|
|
296
285
|
if (match[6] !== undefined) {
|
297
286
|
language = match[6];
|
298
287
|
|
299
|
-
parts = language.split(
|
288
|
+
parts = language.split("-");
|
300
289
|
if (parts.length > 1) {
|
301
290
|
language = parts[0];
|
302
291
|
extlang = parts[1];
|
303
292
|
}
|
304
|
-
|
305
293
|
}
|
306
294
|
|
307
295
|
if (match[14] !== undefined) {
|
@@ -317,5 +305,4 @@ function parseLocale(locale) {
|
|
317
305
|
}
|
318
306
|
|
319
307
|
return new Locale(language, region, script, variants, extlang);
|
320
|
-
|
321
308
|
}
|
package/source/i18n/provider.mjs
CHANGED
@@ -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
|
}
|
@@ -5,16 +5,16 @@
|
|
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";
|
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";
|
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
16
|
|
17
|
-
export {Embed}
|
17
|
+
export { Embed };
|
18
18
|
|
19
19
|
/**
|
20
20
|
* The Embed provider retrieves a JSON file from the given Script Tag.
|
@@ -28,7 +28,6 @@ export {Embed}
|
|
28
28
|
* @tutorial i18n-locale-and-formatter
|
29
29
|
*/
|
30
30
|
class Embed extends Provider {
|
31
|
-
|
32
31
|
/**
|
33
32
|
* ```html
|
34
33
|
* <script id="translations" type="application/json">
|
@@ -37,8 +36,8 @@ class Embed extends Provider {
|
|
37
36
|
* }
|
38
37
|
* </script>
|
39
38
|
* ```
|
40
|
-
*
|
41
|
-
*
|
39
|
+
*
|
40
|
+
*
|
42
41
|
* ```javascript
|
43
42
|
* new Embed('translations')
|
44
43
|
* ```
|
@@ -65,7 +64,6 @@ class Embed extends Provider {
|
|
65
64
|
* @property {Object} options
|
66
65
|
*/
|
67
66
|
this[internalSymbol] = extend({}, super.defaults, this.defaults, validateObject(options));
|
68
|
-
|
69
67
|
}
|
70
68
|
|
71
69
|
/**
|
@@ -83,17 +81,15 @@ class Embed extends Provider {
|
|
83
81
|
* @return {Promise}
|
84
82
|
*/
|
85
83
|
getTranslations(locale) {
|
86
|
-
|
87
84
|
if (isString(locale)) {
|
88
85
|
locale = parseLocale(locale);
|
89
86
|
}
|
90
87
|
|
91
88
|
return new Promise((resolve, reject) => {
|
92
|
-
|
93
|
-
let text = getGlobalObject('document').getElementById(this.textId);
|
89
|
+
let text = getGlobalObject("document").getElementById(this.textId);
|
94
90
|
|
95
91
|
if (text === null) {
|
96
|
-
reject(new Error(
|
92
|
+
reject(new Error("Text not found"));
|
97
93
|
return;
|
98
94
|
}
|
99
95
|
|
@@ -105,21 +101,15 @@ class Embed extends Provider {
|
|
105
101
|
return;
|
106
102
|
}
|
107
103
|
|
108
|
-
|
109
104
|
if (translations === null) {
|
110
|
-
reject(new Error(
|
105
|
+
reject(new Error("Translations not found or invalid"));
|
111
106
|
return;
|
112
107
|
}
|
113
|
-
|
108
|
+
|
114
109
|
const t = new Translations(locale);
|
115
|
-
t.assignTranslations(translations)
|
110
|
+
t.assignTranslations(translations);
|
116
111
|
|
117
112
|
resolve(t);
|
118
|
-
|
119
113
|
});
|
120
|
-
|
121
114
|
}
|
122
|
-
|
123
|
-
|
124
115
|
}
|
125
|
-
|