@lango-project/lango-js 0.1.2 → 0.1.4

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.
@@ -0,0 +1,559 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Detects relevant unicode support for regular expressions in the runtime.
5
+ * Should the runtime not accepts the flag `u` or unicode ranges,
6
+ * character classes without unicode handling will be used.
7
+ *
8
+ * @param {typeof RegExp} [RegExpImpl=RegExp]
9
+ * For testing: the RegExp class.
10
+ * @returns {boolean}
11
+ * @see https://node.green/#ES2015-syntax-RegExp--y--and--u--flags
12
+ */
13
+ function detectUnicodeSupport(RegExpImpl) {
14
+ try {
15
+ if (typeof RegExpImpl !== 'function') {
16
+ RegExpImpl = RegExp;
17
+ }
18
+ // eslint-disable-next-line es5/no-unicode-regex,es5/no-unicode-code-point-escape
19
+ var match = new RegExpImpl('\u{1d306}', 'u').exec('𝌆');
20
+ return !!match && match[0].length === 2;
21
+ } catch (error) {}
22
+ return false;
23
+ }
24
+ var UNICODE_SUPPORT = detectUnicodeSupport();
25
+
26
+ /**
27
+ * Removes `[`, `]` and any trailing quantifiers from the source of a RegExp.
28
+ *
29
+ * @param {RegExp} regexp
30
+ */
31
+ function chars(regexp) {
32
+ if (regexp.source[0] !== '[') {
33
+ throw new Error(regexp + ' can not be used with chars');
34
+ }
35
+ return regexp.source.slice(1, regexp.source.lastIndexOf(']'));
36
+ }
37
+
38
+ /**
39
+ * Creates a new character list regular expression,
40
+ * by removing `search` from the source of `regexp`.
41
+ *
42
+ * @param {RegExp} regexp
43
+ * @param {string} search
44
+ * The character(s) to remove.
45
+ * @returns {RegExp}
46
+ */
47
+ function chars_without(regexp, search) {
48
+ if (regexp.source[0] !== '[') {
49
+ throw new Error('/' + regexp.source + '/ can not be used with chars_without');
50
+ }
51
+ if (!search || typeof search !== 'string') {
52
+ throw new Error(JSON.stringify(search) + ' is not a valid search');
53
+ }
54
+ if (regexp.source.indexOf(search) === -1) {
55
+ throw new Error('"' + search + '" is not is /' + regexp.source + '/');
56
+ }
57
+ if (search === '-' && regexp.source.indexOf(search) !== 1) {
58
+ throw new Error('"' + search + '" is not at the first postion of /' + regexp.source + '/');
59
+ }
60
+ return new RegExp(regexp.source.replace(search, ''), UNICODE_SUPPORT ? 'u' : '');
61
+ }
62
+
63
+ /**
64
+ * Combines and Regular expressions correctly by using `RegExp.source`.
65
+ *
66
+ * @param {...(RegExp | string)[]} args
67
+ * @returns {RegExp}
68
+ */
69
+ function reg(args) {
70
+ var self = this;
71
+ return new RegExp(
72
+ Array.prototype.slice
73
+ .call(arguments)
74
+ .map(function (part) {
75
+ var isStr = typeof part === 'string';
76
+ if (isStr && self === undefined && part === '|') {
77
+ throw new Error('use regg instead of reg to wrap expressions with `|`!');
78
+ }
79
+ return isStr ? part : part.source;
80
+ })
81
+ .join(''),
82
+ UNICODE_SUPPORT ? 'u' : ''
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Like `reg` but wraps the expression in `(?:`,`)` to create a non tracking group.
88
+ *
89
+ * @param {...(RegExp | string)[]} args
90
+ * @returns {RegExp}
91
+ */
92
+ function regg(args) {
93
+ if (arguments.length === 0) {
94
+ throw new Error('no parameters provided');
95
+ }
96
+ return reg.apply(regg, ['(?:'].concat(Array.prototype.slice.call(arguments), [')']));
97
+ }
98
+
99
+ // /**
100
+ // * Append ^ to the beginning of the expression.
101
+ // * @param {...(RegExp | string)[]} args
102
+ // * @returns {RegExp}
103
+ // */
104
+ // function reg_start(args) {
105
+ // if (arguments.length === 0) {
106
+ // throw new Error('no parameters provided');
107
+ // }
108
+ // return reg.apply(reg_start, ['^'].concat(Array.prototype.slice.call(arguments)));
109
+ // }
110
+
111
+ // https://www.w3.org/TR/xml/#document
112
+ // `[1] document ::= prolog element Misc*`
113
+ // https://www.w3.org/TR/xml11/#NT-document
114
+ // `[1] document ::= ( prolog element Misc* ) - ( Char* RestrictedChar Char* )`
115
+
116
+ /**
117
+ * A character usually appearing in wrongly converted strings.
118
+ *
119
+ * @type {string}
120
+ * @see https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
121
+ * @see https://nodejs.dev/en/api/v18/buffer/#buffers-and-character-encodings
122
+ * @see https://www.unicode.org/faq/utf_bom.html#BOM
123
+ * @readonly
124
+ */
125
+ var UNICODE_REPLACEMENT_CHARACTER = '\uFFFD';
126
+ // https://www.w3.org/TR/xml/#NT-Char
127
+ // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
128
+ // `[2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]`
129
+ // https://www.w3.org/TR/xml11/#NT-Char
130
+ // `[2] Char ::= [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]`
131
+ // https://www.w3.org/TR/xml11/#NT-RestrictedChar
132
+ // `[2a] RestrictedChar ::= [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F]`
133
+ // https://www.w3.org/TR/xml11/#charsets
134
+ var Char = /[-\x09\x0A\x0D\x20-\x2C\x2E-\uD7FF\uE000-\uFFFD]/; // without \u10000-\uEFFFF
135
+ if (UNICODE_SUPPORT) {
136
+ // eslint-disable-next-line es5/no-unicode-code-point-escape
137
+ Char = reg('[', chars(Char), '\\u{10000}-\\u{10FFFF}', ']');
138
+ }
139
+ // Negation of Char: matches any character that is NOT a valid XML 1.0 Char.
140
+ // Derived directly from the Char character class above (after the unicode-support extension).
141
+ // XML 1.0 Char production [2]: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
142
+ // @see https://www.w3.org/TR/xml/#NT-Char
143
+ var InvalidChar = new RegExp('[^' + chars(Char) + ']', UNICODE_SUPPORT ? 'u' : '');
144
+
145
+ var _SChar = /[\x20\x09\x0D\x0A]/;
146
+ var SChar_s = chars(_SChar);
147
+ // https://www.w3.org/TR/xml11/#NT-S
148
+ // `[3] S ::= (#x20 | #x9 | #xD | #xA)+`
149
+ var S = reg(_SChar, '+');
150
+ // optional whitespace described as `S?` in the grammar,
151
+ // simplified to 0-n occurrences of the character class
152
+ // instead of 0-1 occurrences of a non-capturing group around S
153
+ var S_OPT = reg(_SChar, '*');
154
+
155
+ // https://www.w3.org/TR/xml11/#NT-NameStartChar
156
+ // `[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]`
157
+ var NameStartChar =
158
+ /[:_a-zA-Z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/; // without \u10000-\uEFFFF
159
+ if (UNICODE_SUPPORT) {
160
+ // eslint-disable-next-line es5/no-unicode-code-point-escape
161
+ NameStartChar = reg('[', chars(NameStartChar), '\\u{10000}-\\u{10FFFF}', ']');
162
+ }
163
+ var NameStartChar_s = chars(NameStartChar);
164
+
165
+ // https://www.w3.org/TR/xml11/#NT-NameChar
166
+ // `[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]`
167
+ var NameChar = reg('[', NameStartChar_s, chars(/[-.0-9\xB7]/), chars(/[\u0300-\u036F\u203F-\u2040]/), ']');
168
+ // https://www.w3.org/TR/xml11/#NT-Name
169
+ // `[5] Name ::= NameStartChar (NameChar)*`
170
+ var Name = reg(NameStartChar, NameChar, '*');
171
+ // Full-string anchored matcher for requireWellFormed serializer checks
172
+ // https://w3c.github.io/DOM-Parsing/#xml-serializing-a-document-node
173
+ var Name_exact = reg('^', Name, '$');
174
+ /*
175
+ https://www.w3.org/TR/xml11/#NT-Names
176
+ `[6] Names ::= Name (#x20 Name)*`
177
+ */
178
+
179
+ // https://www.w3.org/TR/xml11/#NT-Nmtoken
180
+ // `[7] Nmtoken ::= (NameChar)+`
181
+ var Nmtoken = reg(NameChar, '+');
182
+ /*
183
+ https://www.w3.org/TR/xml11/#NT-Nmtokens
184
+ `[8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*`
185
+ var Nmtokens = reg(Nmtoken, regg(/\x20/, Nmtoken), '*');
186
+ */
187
+
188
+ // https://www.w3.org/TR/xml11/#NT-EntityRef
189
+ // `[68] EntityRef ::= '&' Name ';'` [WFC: Entity Declared] [VC: Entity Declared] [WFC: Parsed Entity] [WFC: No Recursion]
190
+ var EntityRef = reg('&', Name, ';');
191
+ // https://www.w3.org/TR/xml11/#NT-CharRef
192
+ // `[66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'` [WFC: Legal Character]
193
+ var CharRef = regg(/&#[0-9]+;|&#x[0-9a-fA-F]+;/);
194
+
195
+ /*
196
+ https://www.w3.org/TR/xml11/#NT-Reference
197
+ - `[67] Reference ::= EntityRef | CharRef`
198
+ - `[66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'` [WFC: Legal Character]
199
+ - `[68] EntityRef ::= '&' Name ';'` [WFC: Entity Declared] [VC: Entity Declared] [WFC: Parsed Entity] [WFC: No Recursion]
200
+ */
201
+ var Reference = regg(EntityRef, '|', CharRef);
202
+
203
+ // https://www.w3.org/TR/xml11/#NT-PEReference
204
+ // `[69] PEReference ::= '%' Name ';'`
205
+ // [VC: Entity Declared] [WFC: No Recursion] [WFC: In DTD]
206
+ var PEReference = reg('%', Name, ';');
207
+
208
+ // https://www.w3.org/TR/xml11/#NT-EntityValue
209
+ // `[9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | "'" ([^%&'] | PEReference | Reference)* "'"`
210
+ var EntityValue = regg(
211
+ reg('"', regg(/[^%&"]/, '|', PEReference, '|', Reference), '*', '"'),
212
+ '|',
213
+ reg("'", regg(/[^%&']/, '|', PEReference, '|', Reference), '*', "'")
214
+ );
215
+
216
+ // https://www.w3.org/TR/xml11/#NT-AttValue
217
+ // `[10] AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"`
218
+ var AttValue = regg('"', regg(/[^<&"]/, '|', Reference), '*', '"', '|', "'", regg(/[^<&']/, '|', Reference), '*', "'");
219
+
220
+ // https://www.w3.org/TR/xml-names/#ns-decl
221
+ // https://www.w3.org/TR/xml-names/#ns-qualnames
222
+ // NameStartChar without ":"
223
+ var NCNameStartChar = chars_without(NameStartChar, ':');
224
+ // https://www.w3.org/TR/xml-names/#orphans
225
+ // `[5] NCNameChar ::= NameChar - ':'`
226
+ // An XML NameChar, minus the ":"
227
+ var NCNameChar = chars_without(NameChar, ':');
228
+ // https://www.w3.org/TR/xml-names/#NT-NCName
229
+ // `[4] NCName ::= Name - (Char* ':' Char*)`
230
+ // An XML Name, minus the ":"
231
+ var NCName = reg(NCNameStartChar, NCNameChar, '*');
232
+ // Full-string anchored matcher for requireWellFormed serializer checks
233
+ // https://w3c.github.io/DOM-Parsing/#xml-serializing-a-document-node
234
+ var NCName_exact = reg('^', NCName, '$');
235
+
236
+ /**
237
+ https://www.w3.org/TR/xml-names/#ns-qualnames
238
+
239
+ ```
240
+ [7] QName ::= PrefixedName | UnprefixedName
241
+ === (NCName ':' NCName) | NCName
242
+ === NCName (':' NCName)?
243
+ [8] PrefixedName ::= Prefix ':' LocalPart
244
+ === NCName ':' NCName
245
+ [9] UnprefixedName ::= LocalPart
246
+ === NCName
247
+ [10] Prefix ::= NCName
248
+ [11] LocalPart ::= NCName
249
+ ```
250
+ */
251
+ var QName = reg(NCName, regg(':', NCName), '?');
252
+ var QName_exact = reg('^', QName, '$');
253
+ var QName_group = reg('(', QName, ')');
254
+
255
+ // https://www.w3.org/TR/xml11/#NT-SystemLiteral
256
+ // `[11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")`
257
+ var SystemLiteral = regg(/"[^"]*"|'[^']*'/);
258
+
259
+ /*
260
+ https://www.w3.org/TR/xml11/#NT-PI
261
+ ```
262
+ [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
263
+ [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
264
+ ```
265
+ target /xml/i is not excluded!
266
+ */
267
+ // The `(?!S)` after the leading `S+` asserts the data starts with a non-whitespace
268
+ // character (greedy `S+` already consumes all separating whitespace), pruning the
269
+ // `S+`/`Char*?` whitespace overlap that otherwise makes an unterminated PI (no `?>`)
270
+ // backtrack quadratically. The lookahead is non-capturing, so the data stays group 2.
271
+ var PI = reg(/^<\?/, '(', Name, ')', regg(S, '(?!', _SChar, ')(', Char, '*?)'), '?', /\?>/);
272
+
273
+ // https://www.w3.org/TR/xml11/#NT-PubidChar
274
+ // `[13] PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]`
275
+ var PubidChar = /[\x20\x0D\x0Aa-zA-Z0-9-'()+,./:=?;!*#@$_%]/;
276
+
277
+ // https://www.w3.org/TR/xml11/#NT-PubidLiteral
278
+ // `[12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"`
279
+ var PubidLiteral = regg('"', PubidChar, '*"', '|', "'", chars_without(PubidChar, "'"), "*'");
280
+
281
+ // https://www.w3.org/TR/xml11/#NT-CharData
282
+ // `[14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)`
283
+
284
+ var COMMENT_START = '<!--';
285
+ var COMMENT_END = '-->';
286
+ // https://www.w3.org/TR/xml11/#NT-Comment
287
+ // `[15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'`
288
+ var Comment = reg(COMMENT_START, regg(chars_without(Char, '-'), '|', reg('-', chars_without(Char, '-'))), '*', COMMENT_END);
289
+
290
+ var PCDATA = '#PCDATA';
291
+ // https://www.w3.org/TR/xml11/#NT-Mixed
292
+ // `[51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | '(' S? '#PCDATA' S? ')'`
293
+ // https://www.w3.org/TR/xml-names/#NT-Mixed
294
+ // `[51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? QName)* S? ')*' | '(' S? '#PCDATA' S? ')'`
295
+ // [VC: Proper Group/PE Nesting] [VC: No Duplicate Types]
296
+ var Mixed = regg(
297
+ reg(/\(/, S_OPT, PCDATA, regg(S_OPT, /\|/, S_OPT, QName), '*', S_OPT, /\)\*/),
298
+ '|',
299
+ reg(/\(/, S_OPT, PCDATA, S_OPT, /\)/)
300
+ );
301
+
302
+ var _children_quantity = /[?*+]?/;
303
+ /*
304
+ `[49] choice ::= '(' S? cp ( S? '|' S? cp )+ S? ')'` [VC: Proper Group/PE Nesting]
305
+ `[50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'` [VC: Proper Group/PE Nesting]
306
+ simplification to solve circular referencing, but doesn't check validity constraint "Proper Group/PE Nesting"
307
+ var _choice_or_seq = reg('[', NameChar_s, SChar_s, chars(_children_quantity), '()|,]*');
308
+ ```
309
+ [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
310
+ === (Name | '(' S? cp ( S? '|' S? cp )+ S? ')' | '(' S? cp ( S? ',' S? cp )* S? ')') ('?' | '*' | '+')?
311
+ !== (Name | [_choice_or_seq]*) ('?' | '*' | '+')?
312
+ ```
313
+ simplification to solve circular referencing, but doesn't check validity constraint "Proper Group/PE Nesting"
314
+ var cp = reg(regg(Name, '|', _choice_or_seq), _children_quantity);
315
+ */
316
+ /*
317
+ Inefficient regular expression (High)
318
+ This part of the regular expression may cause exponential backtracking on strings starting with '(|' and containing many repetitions of '|'.
319
+ https://github.com/xmldom/xmldom/security/code-scanning/91
320
+ var choice = regg(/\(/, S_OPT, cp, regg(S_OPT, /\|/, S_OPT, cp), '+', S_OPT, /\)/);
321
+ */
322
+ /*
323
+ Inefficient regular expression (High)
324
+ This part of the regular expression may cause exponential backtracking on strings starting with '(,' and containing many repetitions of ','.
325
+ https://github.com/xmldom/xmldom/security/code-scanning/92
326
+ var seq = regg(/\(/, S_OPT, cp, regg(S_OPT, /,/, S_OPT, cp), '*', S_OPT, /\)/);
327
+ */
328
+
329
+ // `[47] children ::= (choice | seq) ('?' | '*' | '+')?`
330
+ // simplification to solve circular referencing, but doesn't check validity constraint "Proper Group/PE Nesting"
331
+ var children = reg(/\([^>]+\)/, _children_quantity /*regg(choice, '|', seq), _children_quantity*/);
332
+
333
+ // https://www.w3.org/TR/xml11/#NT-contentspec
334
+ // `[46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children`
335
+ var contentspec = regg('EMPTY', '|', 'ANY', '|', Mixed, '|', children);
336
+
337
+ var ELEMENTDECL_START = '<!ELEMENT';
338
+ // https://www.w3.org/TR/xml11/#NT-elementdecl
339
+ // `[45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'`
340
+ // https://www.w3.org/TR/xml-names/#NT-elementdecl
341
+ // `[17] elementdecl ::= '<!ELEMENT' S QName S contentspec S? '>'`
342
+ // because of https://www.w3.org/TR/xml11/#NT-PEReference
343
+ // since xmldom is not supporting replacements of PEReferences in the DTD
344
+ // this also supports PEReference in the possible places
345
+ var elementdecl = reg(ELEMENTDECL_START, S, regg(QName, '|', PEReference), S, regg(contentspec, '|', PEReference), S_OPT, '>');
346
+
347
+ // https://www.w3.org/TR/xml11/#NT-NotationType
348
+ // `[58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'`
349
+ // [VC: Notation Attributes] [VC: One Notation Per Element Type] [VC: No Notation on Empty Element] [VC: No Duplicate Tokens]
350
+ var NotationType = reg('NOTATION', S, /\(/, S_OPT, Name, regg(S_OPT, /\|/, S_OPT, Name), '*', S_OPT, /\)/);
351
+ // https://www.w3.org/TR/xml11/#NT-Enumeration
352
+ // `[59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'`
353
+ // [VC: Enumeration] [VC: No Duplicate Tokens]
354
+ var Enumeration = reg(/\(/, S_OPT, Nmtoken, regg(S_OPT, /\|/, S_OPT, Nmtoken), '*', S_OPT, /\)/);
355
+
356
+ // https://www.w3.org/TR/xml11/#NT-EnumeratedType
357
+ // `[57] EnumeratedType ::= NotationType | Enumeration`
358
+ var EnumeratedType = regg(NotationType, '|', Enumeration);
359
+
360
+ /*
361
+ ```
362
+ [55] StringType ::= 'CDATA'
363
+ [56] TokenizedType ::= 'ID' [VC: ID] [VC: One ID per Element Type] [VC: ID Attribute Default]
364
+ | 'IDREF' [VC: IDREF]
365
+ | 'IDREFS' [VC: IDREF]
366
+ | 'ENTITY' [VC: Entity Name]
367
+ | 'ENTITIES' [VC: Entity Name]
368
+ | 'NMTOKEN' [VC: Name Token]
369
+ | 'NMTOKENS' [VC: Name Token]
370
+ [54] AttType ::= StringType | TokenizedType | EnumeratedType
371
+ ```*/
372
+ var AttType = regg(/CDATA|ID|IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS/, '|', EnumeratedType);
373
+
374
+ // `[60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)`
375
+ // [WFC: No < in Attribute Values] [WFC: No External Entity References]
376
+ // [VC: Fixed Attribute Default] [VC: Required Attribute] [VC: Attribute Default Value Syntactically Correct]
377
+ var DefaultDecl = regg(/#REQUIRED|#IMPLIED/, '|', regg(regg('#FIXED', S), '?', AttValue));
378
+
379
+ // https://www.w3.org/TR/xml11/#NT-AttDef
380
+ // [53] AttDef ::= S Name S AttType S DefaultDecl
381
+ // https://www.w3.org/TR/xml-names/#NT-AttDef
382
+ // [1] NSAttName ::= PrefixedAttName | DefaultAttName
383
+ // [2] PrefixedAttName ::= 'xmlns:' NCName [NSC: Reserved Prefixes and Namespace Names]
384
+ // [3] DefaultAttName ::= 'xmlns'
385
+ // [21] AttDef ::= S (QName | NSAttName) S AttType S DefaultDecl
386
+ // === S Name S AttType S DefaultDecl
387
+ // xmldom is not distinguishing between QName and NSAttName on this level
388
+ // to support XML without namespaces in DTD we can not restrict it to QName
389
+ var AttDef = regg(S, Name, S, AttType, S, DefaultDecl);
390
+
391
+ var ATTLIST_DECL_START = '<!ATTLIST';
392
+ // https://www.w3.org/TR/xml11/#NT-AttlistDecl
393
+ // `[52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'`
394
+ // https://www.w3.org/TR/xml-names/#NT-AttlistDecl
395
+ // `[20] AttlistDecl ::= '<!ATTLIST' S QName AttDef* S? '>'`
396
+ // to support XML without namespaces in DTD we can not restrict it to QName
397
+ var AttlistDecl = reg(ATTLIST_DECL_START, S, Name, AttDef, '*', S_OPT, '>');
398
+
399
+ // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#about:legacy-compat
400
+ var ABOUT_LEGACY_COMPAT = 'about:legacy-compat';
401
+ var ABOUT_LEGACY_COMPAT_SystemLiteral = regg('"' + ABOUT_LEGACY_COMPAT + '"', '|', "'" + ABOUT_LEGACY_COMPAT + "'");
402
+ var SYSTEM = 'SYSTEM';
403
+ var PUBLIC = 'PUBLIC';
404
+ // https://www.w3.org/TR/xml11/#NT-ExternalID
405
+ // `[75] ExternalID ::= 'SYSTEM' S SystemLiteral | 'PUBLIC' S PubidLiteral S SystemLiteral`
406
+ var ExternalID = regg(regg(SYSTEM, S, SystemLiteral), '|', regg(PUBLIC, S, PubidLiteral, S, SystemLiteral));
407
+ var ExternalID_match = reg(
408
+ '^',
409
+ regg(
410
+ regg(SYSTEM, S, '(?<SystemLiteralOnly>', SystemLiteral, ')'),
411
+ '|',
412
+ regg(PUBLIC, S, '(?<PubidLiteral>', PubidLiteral, ')', S, '(?<SystemLiteral>', SystemLiteral, ')')
413
+ )
414
+ );
415
+ // Full-string anchored matcher for requireWellFormed serializer checks
416
+ // https://w3c.github.io/DOM-Parsing/#xml-serializing-a-document-node
417
+ var PubidLiteral_match = reg('^', PubidLiteral, '$');
418
+ // Full-string anchored matcher for requireWellFormed serializer checks
419
+ // https://w3c.github.io/DOM-Parsing/#xml-serializing-a-document-node
420
+ var SystemLiteral_match = reg('^', SystemLiteral, '$');
421
+
422
+ // https://www.w3.org/TR/xml11/#NT-NDataDecl
423
+ // `[76] NDataDecl ::= S 'NDATA' S Name` [VC: Notation Declared]
424
+ var NDataDecl = regg(S, 'NDATA', S, Name);
425
+
426
+ // https://www.w3.org/TR/xml11/#NT-EntityDef
427
+ // `[73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)`
428
+ var EntityDef = regg(EntityValue, '|', regg(ExternalID, NDataDecl, '?'));
429
+
430
+ var ENTITY_DECL_START = '<!ENTITY';
431
+ // https://www.w3.org/TR/xml11/#NT-GEDecl
432
+ // `[71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'`
433
+ var GEDecl = reg(ENTITY_DECL_START, S, Name, S, EntityDef, S_OPT, '>');
434
+ // https://www.w3.org/TR/xml11/#NT-PEDef
435
+ // `[74] PEDef ::= EntityValue | ExternalID`
436
+ var PEDef = regg(EntityValue, '|', ExternalID);
437
+ // https://www.w3.org/TR/xml11/#NT-PEDecl
438
+ // `[72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'`
439
+ var PEDecl = reg(ENTITY_DECL_START, S, '%', S, Name, S, PEDef, S_OPT, '>');
440
+ // https://www.w3.org/TR/xml11/#NT-EntityDecl
441
+ // `[70] EntityDecl ::= GEDecl | PEDecl`
442
+ var EntityDecl = regg(GEDecl, '|', PEDecl);
443
+
444
+ // https://www.w3.org/TR/xml11/#NT-PublicID
445
+ // `[83] PublicID ::= 'PUBLIC' S PubidLiteral`
446
+ var PublicID = reg(PUBLIC, S, PubidLiteral);
447
+ // https://www.w3.org/TR/xml11/#NT-NotationDecl
448
+ // `[82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'` [VC: Unique Notation Name]
449
+ var NotationDecl = reg('<!NOTATION', S, Name, S, regg(ExternalID, '|', PublicID), S_OPT, '>');
450
+
451
+ // https://www.w3.org/TR/xml11/#NT-Eq
452
+ // `[25] Eq ::= S? '=' S?`
453
+ var Eq = reg(S_OPT, '=', S_OPT);
454
+ // https://www.w3.org/TR/xml/#NT-VersionNum
455
+ // `[26] VersionNum ::= '1.' [0-9]+`
456
+ // https://www.w3.org/TR/xml11/#NT-VersionNum
457
+ // `[26] VersionNum ::= '1.1'`
458
+ var VersionNum = /1[.]\d+/;
459
+ // https://www.w3.org/TR/xml11/#NT-VersionInfo
460
+ // `[24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')`
461
+ var VersionInfo = reg(S, 'version', Eq, regg("'", VersionNum, "'", '|', '"', VersionNum, '"'));
462
+ // https://www.w3.org/TR/xml11/#NT-EncName
463
+ // `[81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*`
464
+ var EncName = /[A-Za-z][-A-Za-z0-9._]*/;
465
+ // https://www.w3.org/TR/xml11/#NT-EncDecl
466
+ // `[80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'" )`
467
+ var EncodingDecl = regg(S, 'encoding', Eq, regg('"', EncName, '"', '|', "'", EncName, "'"));
468
+ // https://www.w3.org/TR/xml11/#NT-SDDecl
469
+ // `[32] SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))`
470
+ var SDDecl = regg(S, 'standalone', Eq, regg("'", regg('yes', '|', 'no'), "'", '|', '"', regg('yes', '|', 'no'), '"'));
471
+ // https://www.w3.org/TR/xml11/#NT-XMLDecl
472
+ // [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
473
+ var XMLDecl = reg(/^<\?xml/, VersionInfo, EncodingDecl, '?', SDDecl, '?', S_OPT, /\?>/);
474
+
475
+ /*
476
+ https://www.w3.org/TR/xml/#NT-markupdecl
477
+ https://www.w3.org/TR/xml11/#NT-markupdecl
478
+ `[29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | NotationDecl | PI | Comment`
479
+ var markupdecl = regg(elementdecl, '|', AttlistDecl, '|', EntityDecl, '|', NotationDecl, '|', PI_unsafe, '|', Comment);
480
+ */
481
+ /*
482
+ https://www.w3.org/TR/xml-names/#NT-doctypedecl
483
+ `[28a] DeclSep ::= PEReference | S`
484
+ https://www.w3.org/TR/xml11/#NT-intSubset
485
+ ```
486
+ [28b] intSubset ::= (markupdecl | DeclSep)*
487
+ === (markupdecl | PEReference | S)*
488
+ ```
489
+ [WFC: PE Between Declarations]
490
+ var intSubset = reg(regg(markupdecl, '|', PEReference, '|', S), '*');
491
+ */
492
+ var DOCTYPE_DECL_START = '<!DOCTYPE';
493
+ /*
494
+ https://www.w3.org/TR/xml11/#NT-doctypedecl
495
+ `[28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'`
496
+ https://www.afterwardsw3.org/TR/xml-names/#NT-doctypedecl
497
+ `[16] doctypedecl ::= '<!DOCTYPE' S QName (S ExternalID)? S? ('[' (markupdecl | PEReference | S)* ']' S?)? '>'`
498
+ var doctypedecl = reg('<!DOCTYPE', S, Name, regg(S, ExternalID), '?', S_OPT, regg(/\[/, intSubset, /]/, S_OPT), '?', '>');
499
+ */
500
+
501
+ var CDATA_START = '<![CDATA[';
502
+ var CDATA_END = ']]>';
503
+ var CDStart = /<!\[CDATA\[/;
504
+ var CDEnd = /\]\]>/;
505
+ var CData = reg(Char, '*?', CDEnd);
506
+ /*
507
+ https://www.w3.org/TR/xml/#dt-cdsection
508
+ `[18] CDSect ::= CDStart CData CDEnd`
509
+ `[19] CDStart ::= '<![CDATA['`
510
+ `[20] CData ::= (Char* - (Char* ']]>' Char*))`
511
+ `[21] CDEnd ::= ']]>'`
512
+ */
513
+ var CDSect = reg(CDStart, CData);
514
+
515
+ // unit tested
516
+ exports.chars = chars;
517
+ exports.chars_without = chars_without;
518
+ exports.detectUnicodeSupport = detectUnicodeSupport;
519
+ exports.reg = reg;
520
+ exports.regg = regg;
521
+ exports.ABOUT_LEGACY_COMPAT = ABOUT_LEGACY_COMPAT;
522
+ exports.ABOUT_LEGACY_COMPAT_SystemLiteral = ABOUT_LEGACY_COMPAT_SystemLiteral;
523
+ exports.AttlistDecl = AttlistDecl;
524
+ exports.CDATA_START = CDATA_START;
525
+ exports.CDATA_END = CDATA_END;
526
+ exports.CDSect = CDSect;
527
+ exports.Char = Char;
528
+ exports.Comment = Comment;
529
+ exports.COMMENT_START = COMMENT_START;
530
+ exports.COMMENT_END = COMMENT_END;
531
+ exports.DOCTYPE_DECL_START = DOCTYPE_DECL_START;
532
+ exports.elementdecl = elementdecl;
533
+ exports.EntityDecl = EntityDecl;
534
+ exports.EntityValue = EntityValue;
535
+ exports.ExternalID = ExternalID;
536
+ exports.ExternalID_match = ExternalID_match;
537
+ exports.Name = Name;
538
+ exports.Name_exact = Name_exact;
539
+ exports.NCName_exact = NCName_exact;
540
+ exports.NotationDecl = NotationDecl;
541
+ exports.Reference = Reference;
542
+ exports.PEReference = PEReference;
543
+ exports.PI = PI;
544
+ exports.PUBLIC = PUBLIC;
545
+ exports.PubidLiteral = PubidLiteral;
546
+ exports.PubidLiteral_match = PubidLiteral_match;
547
+ exports.QName = QName;
548
+ exports.QName_exact = QName_exact;
549
+ exports.QName_group = QName_group;
550
+ exports.S = S;
551
+ exports.SChar_s = SChar_s;
552
+ exports.S_OPT = S_OPT;
553
+ exports.SYSTEM = SYSTEM;
554
+ exports.SystemLiteral = SystemLiteral;
555
+ exports.SystemLiteral_match = SystemLiteral_match;
556
+ exports.InvalidChar = InvalidChar;
557
+ exports.UNICODE_REPLACEMENT_CHARACTER = UNICODE_REPLACEMENT_CHARACTER;
558
+ exports.UNICODE_SUPPORT = UNICODE_SUPPORT;
559
+ exports.XMLDecl = XMLDecl;
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+ var conventions = require('./conventions');
3
+ exports.assign = conventions.assign;
4
+ exports.hasDefaultHTMLNamespace = conventions.hasDefaultHTMLNamespace;
5
+ exports.isHTMLMimeType = conventions.isHTMLMimeType;
6
+ exports.isValidMimeType = conventions.isValidMimeType;
7
+ exports.MIME_TYPE = conventions.MIME_TYPE;
8
+ exports.NAMESPACE = conventions.NAMESPACE;
9
+
10
+ var errors = require('./errors');
11
+ exports.DOMException = errors.DOMException;
12
+ exports.DOMExceptionName = errors.DOMExceptionName;
13
+ exports.ExceptionCode = errors.ExceptionCode;
14
+ exports.ParseError = errors.ParseError;
15
+
16
+ var dom = require('./dom');
17
+ exports.Attr = dom.Attr;
18
+ exports.CDATASection = dom.CDATASection;
19
+ exports.CharacterData = dom.CharacterData;
20
+ exports.Comment = dom.Comment;
21
+ exports.Document = dom.Document;
22
+ exports.DocumentFragment = dom.DocumentFragment;
23
+ exports.DocumentType = dom.DocumentType;
24
+ exports.DOMImplementation = dom.DOMImplementation;
25
+ exports.Element = dom.Element;
26
+ exports.Entity = dom.Entity;
27
+ exports.EntityReference = dom.EntityReference;
28
+ exports.LiveNodeList = dom.LiveNodeList;
29
+ exports.NamedNodeMap = dom.NamedNodeMap;
30
+ exports.Node = dom.Node;
31
+ exports.NodeList = dom.NodeList;
32
+ exports.Notation = dom.Notation;
33
+ exports.ProcessingInstruction = dom.ProcessingInstruction;
34
+ exports.Text = dom.Text;
35
+ exports.XMLSerializer = dom.XMLSerializer;
36
+
37
+ var domParser = require('./dom-parser');
38
+ exports.DOMParser = domParser.DOMParser;
39
+ exports.normalizeLineEndings = domParser.normalizeLineEndings;
40
+ exports.onErrorStopParsing = domParser.onErrorStopParsing;
41
+ exports.onWarningStopParsing = domParser.onWarningStopParsing;