wikiparser-node 1.7.0 → 1.8.0-b

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 (172) hide show
  1. package/bundle/bundle.min.js +38 -0
  2. package/config/enwiki.json +1 -814
  3. package/config/llwiki.json +1 -35
  4. package/config/moegirl.json +1 -44
  5. package/config/zhwiki.json +1 -466
  6. package/extensions/dist/base.js +64 -0
  7. package/extensions/dist/editor.js +159 -0
  8. package/extensions/dist/highlight.js +58 -0
  9. package/extensions/dist/lint.js +72 -0
  10. package/extensions/editor.css +64 -0
  11. package/extensions/ui.css +144 -0
  12. package/package.json +30 -18
  13. package/config/.schema.json +0 -181
  14. package/dist/addon/table.d.ts +0 -1
  15. package/dist/addon/table.js +0 -485
  16. package/dist/addon/token.d.ts +0 -1
  17. package/dist/addon/token.js +0 -220
  18. package/dist/addon/transclude.d.ts +0 -1
  19. package/dist/addon/transclude.js +0 -183
  20. package/dist/base.d.ts +0 -71
  21. package/dist/base.js +0 -38
  22. package/dist/index.d.ts +0 -31
  23. package/dist/index.js +0 -211
  24. package/dist/internal.d.ts +0 -46
  25. package/dist/lib/element.d.ts +0 -151
  26. package/dist/lib/element.js +0 -628
  27. package/dist/lib/node.d.ts +0 -145
  28. package/dist/lib/node.js +0 -420
  29. package/dist/lib/range.d.ts +0 -105
  30. package/dist/lib/range.js +0 -406
  31. package/dist/lib/ranges.d.ts +0 -28
  32. package/dist/lib/ranges.js +0 -126
  33. package/dist/lib/text.d.ts +0 -63
  34. package/dist/lib/text.js +0 -355
  35. package/dist/lib/title.d.ts +0 -40
  36. package/dist/lib/title.js +0 -197
  37. package/dist/mixin/attributesParent.d.ts +0 -50
  38. package/dist/mixin/attributesParent.js +0 -81
  39. package/dist/mixin/fixed.d.ts +0 -6
  40. package/dist/mixin/fixed.js +0 -30
  41. package/dist/mixin/flagsParent.d.ts +0 -72
  42. package/dist/mixin/flagsParent.js +0 -59
  43. package/dist/mixin/hidden.d.ts +0 -5
  44. package/dist/mixin/hidden.js +0 -24
  45. package/dist/mixin/magicLinkParent.d.ts +0 -41
  46. package/dist/mixin/magicLinkParent.js +0 -42
  47. package/dist/mixin/singleLine.d.ts +0 -6
  48. package/dist/mixin/singleLine.js +0 -26
  49. package/dist/mixin/sol.d.ts +0 -6
  50. package/dist/mixin/sol.js +0 -43
  51. package/dist/mixin/syntax.d.ts +0 -9
  52. package/dist/mixin/syntax.js +0 -50
  53. package/dist/parser/braces.js +0 -130
  54. package/dist/parser/commentAndExt.js +0 -89
  55. package/dist/parser/converter.js +0 -40
  56. package/dist/parser/externalLinks.js +0 -44
  57. package/dist/parser/hrAndDoubleUnderscore.js +0 -40
  58. package/dist/parser/html.js +0 -38
  59. package/dist/parser/links.js +0 -99
  60. package/dist/parser/list.js +0 -97
  61. package/dist/parser/magicLinks.js +0 -42
  62. package/dist/parser/quotes.js +0 -67
  63. package/dist/parser/redirect.js +0 -26
  64. package/dist/parser/selector.js +0 -190
  65. package/dist/parser/table.js +0 -123
  66. package/dist/src/arg.d.ts +0 -58
  67. package/dist/src/arg.js +0 -214
  68. package/dist/src/atom.d.ts +0 -12
  69. package/dist/src/atom.js +0 -27
  70. package/dist/src/attribute.d.ts +0 -71
  71. package/dist/src/attribute.js +0 -502
  72. package/dist/src/attributes.d.ts +0 -109
  73. package/dist/src/attributes.js +0 -356
  74. package/dist/src/converter.d.ts +0 -33
  75. package/dist/src/converter.js +0 -134
  76. package/dist/src/converterFlags.d.ts +0 -87
  77. package/dist/src/converterFlags.js +0 -242
  78. package/dist/src/converterRule.d.ts +0 -77
  79. package/dist/src/converterRule.js +0 -209
  80. package/dist/src/extLink.d.ts +0 -43
  81. package/dist/src/extLink.js +0 -191
  82. package/dist/src/gallery.d.ts +0 -54
  83. package/dist/src/gallery.js +0 -139
  84. package/dist/src/heading.d.ts +0 -47
  85. package/dist/src/heading.js +0 -190
  86. package/dist/src/hidden.d.ts +0 -7
  87. package/dist/src/hidden.js +0 -23
  88. package/dist/src/html.d.ts +0 -65
  89. package/dist/src/html.js +0 -358
  90. package/dist/src/imageParameter.d.ts +0 -65
  91. package/dist/src/imageParameter.js +0 -257
  92. package/dist/src/imagemap.d.ts +0 -54
  93. package/dist/src/imagemap.js +0 -144
  94. package/dist/src/imagemapLink.d.ts +0 -31
  95. package/dist/src/imagemapLink.js +0 -93
  96. package/dist/src/index.d.ts +0 -143
  97. package/dist/src/index.js +0 -701
  98. package/dist/src/link/base.d.ts +0 -52
  99. package/dist/src/link/base.js +0 -228
  100. package/dist/src/link/category.d.ts +0 -22
  101. package/dist/src/link/category.js +0 -30
  102. package/dist/src/link/file.d.ts +0 -98
  103. package/dist/src/link/file.js +0 -272
  104. package/dist/src/link/galleryImage.d.ts +0 -32
  105. package/dist/src/link/galleryImage.js +0 -158
  106. package/dist/src/link/index.d.ts +0 -56
  107. package/dist/src/link/index.js +0 -133
  108. package/dist/src/link/redirectTarget.d.ts +0 -51
  109. package/dist/src/link/redirectTarget.js +0 -101
  110. package/dist/src/magicLink.d.ts +0 -59
  111. package/dist/src/magicLink.js +0 -220
  112. package/dist/src/nested.d.ts +0 -43
  113. package/dist/src/nested.js +0 -105
  114. package/dist/src/nowiki/base.d.ts +0 -31
  115. package/dist/src/nowiki/base.js +0 -93
  116. package/dist/src/nowiki/comment.d.ts +0 -25
  117. package/dist/src/nowiki/comment.js +0 -76
  118. package/dist/src/nowiki/dd.d.ts +0 -8
  119. package/dist/src/nowiki/dd.js +0 -24
  120. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -18
  121. package/dist/src/nowiki/doubleUnderscore.js +0 -101
  122. package/dist/src/nowiki/hr.d.ts +0 -5
  123. package/dist/src/nowiki/hr.js +0 -63
  124. package/dist/src/nowiki/index.d.ts +0 -16
  125. package/dist/src/nowiki/index.js +0 -27
  126. package/dist/src/nowiki/list.d.ts +0 -16
  127. package/dist/src/nowiki/list.js +0 -109
  128. package/dist/src/nowiki/listBase.d.ts +0 -5
  129. package/dist/src/nowiki/listBase.js +0 -61
  130. package/dist/src/nowiki/noinclude.d.ts +0 -10
  131. package/dist/src/nowiki/noinclude.js +0 -23
  132. package/dist/src/nowiki/quote.d.ts +0 -14
  133. package/dist/src/nowiki/quote.js +0 -131
  134. package/dist/src/onlyinclude.d.ts +0 -16
  135. package/dist/src/onlyinclude.js +0 -57
  136. package/dist/src/paramTag/index.d.ts +0 -37
  137. package/dist/src/paramTag/index.js +0 -77
  138. package/dist/src/paramTag/inputbox.d.ts +0 -8
  139. package/dist/src/paramTag/inputbox.js +0 -22
  140. package/dist/src/parameter.d.ts +0 -70
  141. package/dist/src/parameter.js +0 -264
  142. package/dist/src/pre.d.ts +0 -31
  143. package/dist/src/pre.js +0 -59
  144. package/dist/src/redirect.d.ts +0 -34
  145. package/dist/src/redirect.js +0 -119
  146. package/dist/src/syntax.d.ts +0 -19
  147. package/dist/src/syntax.js +0 -86
  148. package/dist/src/table/base.d.ts +0 -27
  149. package/dist/src/table/base.js +0 -81
  150. package/dist/src/table/index.d.ts +0 -242
  151. package/dist/src/table/index.js +0 -498
  152. package/dist/src/table/td.d.ts +0 -86
  153. package/dist/src/table/td.js +0 -355
  154. package/dist/src/table/tr.d.ts +0 -32
  155. package/dist/src/table/tr.js +0 -58
  156. package/dist/src/table/trBase.d.ts +0 -53
  157. package/dist/src/table/trBase.js +0 -156
  158. package/dist/src/tagPair/ext.d.ts +0 -32
  159. package/dist/src/tagPair/ext.js +0 -203
  160. package/dist/src/tagPair/include.d.ts +0 -35
  161. package/dist/src/tagPair/include.js +0 -74
  162. package/dist/src/tagPair/index.d.ts +0 -27
  163. package/dist/src/tagPair/index.js +0 -128
  164. package/dist/src/transclude.d.ts +0 -166
  165. package/dist/src/transclude.js +0 -584
  166. package/dist/util/constants.js +0 -27
  167. package/dist/util/debug.js +0 -81
  168. package/dist/util/diff.js +0 -83
  169. package/dist/util/lint.js +0 -31
  170. package/dist/util/string.js +0 -60
  171. package/errors/README +0 -1
  172. package/printed/README +0 -1
@@ -1,584 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TranscludeToken = void 0;
4
- const string_1 = require("../util/string");
5
- const lint_1 = require("../util/lint");
6
- const debug_1 = require("../util/debug");
7
- const constants_1 = require("../util/constants");
8
- const index_1 = require("../index");
9
- const index_2 = require("./index");
10
- const parameter_1 = require("./parameter");
11
- const atom_1 = require("./atom");
12
- const syntax_1 = require("./syntax");
13
- const insensitiveVars = new Set([
14
- 'pageid',
15
- 'articlepath',
16
- 'server',
17
- 'servername',
18
- 'scriptpath',
19
- 'stylepath',
20
- ]);
21
- /**
22
- * 模板或魔术字
23
- * @classdesc `{childNodes: [AtomToken|SyntaxToken, ...AtomToken, ...ParameterToken]}`
24
- */
25
- class TranscludeToken extends index_2.Token {
26
- type = 'template';
27
- modifier = '';
28
- #keys = new Set();
29
- /* NOT FOR BROWSER END */
30
- #raw = false;
31
- #args = new Map();
32
- /** 是否存在重复参数 */
33
- get duplication() {
34
- return this.isTemplate() && Boolean(this.hasDuplicatedArgs());
35
- }
36
- set duplication(duplication) {
37
- if (this.duplication && !duplication) {
38
- this.fixDuplication();
39
- }
40
- }
41
- /* NOT FOR BROWSER END */
42
- /**
43
- * @param title 模板标题或魔术字
44
- * @param parts 参数各部分
45
- * @throws `SyntaxError` 非法的模板名称
46
- */
47
- constructor(title, parts, config = index_1.default.getConfig(), accum = []) {
48
- super(undefined, config, accum, {
49
- AtomToken: 0, SyntaxToken: 0, ParameterToken: '1:',
50
- });
51
- const { parserFunction: [insensitive, sensitive] } = config, argSubst = /^(?:\s|\0\d+c\x7F)*\0\d+s\x7F/u.exec(title)?.[0];
52
- if (argSubst) {
53
- this.setAttribute('modifier', argSubst);
54
- title = title.slice(argSubst.length);
55
- }
56
- else if (title.includes(':')) {
57
- const [modifier, ...arg] = title.split(':'), [mt] = /^(?:\s|\0\d+c\x7F)*/u.exec(arg[0] ?? '');
58
- if (this.setModifier(`${modifier}:${mt}`)) {
59
- title = arg.join(':').slice(mt.length);
60
- }
61
- }
62
- const isFunction = title.includes(':');
63
- if (isFunction || parts.length === 0 && !this.#raw) {
64
- const [magicWord, ...arg] = title.split(':'), cleaned = (0, string_1.removeComment)(magicWord), name = cleaned[arg.length > 0 ? 'trimStart' : 'trim'](), lcName = name.toLowerCase(), canonicalName = insensitive[lcName], isSensitive = sensitive.includes(name), isVar = isSensitive || insensitiveVars.has(canonicalName);
65
- if (isVar || isFunction && canonicalName) {
66
- this.setAttribute('name', canonicalName ?? lcName);
67
- this.type = 'magic-word';
68
- const pattern = new RegExp(`^\\s*${name}\\s*$`, isSensitive ? 'u' : 'iu'), token = new syntax_1.SyntaxToken(magicWord, pattern, 'magic-word-name', config, accum, {
69
- 'Stage-1': ':', '!ExtToken': '',
70
- });
71
- super.insertAt(token);
72
- if (arg.length > 0) {
73
- parts.unshift([arg.join(':')]);
74
- }
75
- if (this.name === 'invoke') {
76
- /* NOT FOR BROWSER */
77
- this.setAttribute('acceptable', { SyntaxToken: 0, AtomToken: '1:3', ParameterToken: '3:' });
78
- this.protectChildren('1:3');
79
- /* NOT FOR BROWSER END */
80
- for (let i = 0; i < 2; i++) {
81
- const part = parts.shift();
82
- if (!part) {
83
- break;
84
- }
85
- const invoke = new atom_1.AtomToken(part.join('='), `invoke-${i ? 'function' : 'module'}`, config, accum, { 'Stage-1': ':', '!ExtToken': '' });
86
- super.insertAt(invoke);
87
- }
88
- }
89
- }
90
- }
91
- if (this.type === 'template') {
92
- const name = (0, string_1.removeComment)(title).trim();
93
- if (!this.normalizeTitle(name, 10, true).valid) {
94
- accum.pop();
95
- /* NOT FOR BROWSER */
96
- index_1.default.debug(`非法的模板名称:${(0, string_1.noWrap)(name)}`);
97
- /* NOT FOR BROWSER END */
98
- throw new SyntaxError('非法的模板名称');
99
- }
100
- const token = new atom_1.AtomToken(title, 'template-name', config, accum, {
101
- 'Stage-2': ':', '!HeadingToken': '',
102
- });
103
- super.insertAt(token);
104
- }
105
- const templateLike = this.isTemplate();
106
- let i = 1;
107
- for (let j = 0; j < parts.length; j++) {
108
- const part = parts[j];
109
- if (!templateLike && !(this.name === 'switch' && j > 0)) {
110
- part[0] = part.join('=');
111
- part.length = 1;
112
- }
113
- if (part.length === 1) {
114
- part.unshift(i);
115
- i++;
116
- }
117
- // @ts-expect-error abstract class
118
- this.insertAt(new parameter_1.ParameterToken(...part, config, accum));
119
- }
120
- this.seal('modifier');
121
- /* NOT FOR BROWSER */
122
- this.protectChildren(0);
123
- }
124
- /**
125
- * 设置引用修饰符
126
- * @param modifier 引用修饰符
127
- */
128
- setModifier(modifier) {
129
- const { parserFunction: [, , raw, subst] } = this.getAttribute('config'), lcModifier = (0, string_1.removeComment)(modifier).trim();
130
- if (modifier && !lcModifier.endsWith(':')) {
131
- return false;
132
- }
133
- const magicWord = lcModifier.slice(0, -1).toLowerCase(), isRaw = raw.includes(magicWord), isSubst = subst.includes(magicWord);
134
- if (this.#raw && isRaw
135
- || !this.#raw && (isSubst || modifier === '')
136
- || (debug_1.Shadow.running || this.length > 1) && (isRaw || isSubst || modifier === '')) {
137
- this.setAttribute('modifier', modifier);
138
- this.#raw = isRaw;
139
- return Boolean(modifier);
140
- }
141
- return false;
142
- }
143
- /** 是否是模板或模块 */
144
- isTemplate() {
145
- return this.type === 'template' || this.name === 'invoke';
146
- }
147
- /** 获取模板或模块名 */
148
- #getTitle() {
149
- const isTemplate = this.type === 'template', child = this.childNodes[isTemplate ? 0 : 1];
150
- return this.normalizeTitle(child.text(), isTemplate ? 10 : 828);
151
- }
152
- /** @private */
153
- afterBuild() {
154
- if (this.modifier.includes('\0')) {
155
- this.setAttribute('modifier', this.buildFromStr(this.modifier, constants_1.BuildMethod.String));
156
- }
157
- /* NOT FOR BROWSER */
158
- if (this.isTemplate()) {
159
- const isTemplate = this.type === 'template';
160
- if (isTemplate || this.length > 1) {
161
- this.setAttribute(isTemplate ? 'name' : 'module', this.#getTitle().title);
162
- }
163
- /**
164
- * 当事件bubble到`parameter`时,将`oldKey`和`newKey`保存进AstEventData。
165
- * 当继续bubble到`template`时,处理并删除`oldKey`和`newKey`。
166
- * @implements
167
- */
168
- const transcludeListener = (e, data) => {
169
- const { prevTarget } = e, { oldKey, newKey } = data;
170
- if (typeof oldKey === 'string') {
171
- delete data.oldKey;
172
- delete data.newKey;
173
- }
174
- if (prevTarget === this.firstChild && isTemplate
175
- || prevTarget === this.childNodes[1] && !isTemplate && this.name === 'invoke') {
176
- this.setAttribute(isTemplate ? 'name' : 'module', this.#getTitle().title);
177
- }
178
- else if (oldKey !== newKey && prevTarget instanceof parameter_1.ParameterToken) {
179
- const oldArgs = this.getArgs(oldKey, false, false);
180
- oldArgs.delete(prevTarget);
181
- this.getArgs(newKey, false, false).add(prevTarget);
182
- this.#keys.add(newKey);
183
- if (oldArgs.size === 0) {
184
- this.#keys.delete(oldKey);
185
- }
186
- }
187
- };
188
- this.addEventListener(['remove', 'insert', 'replace', 'text'], transcludeListener);
189
- }
190
- }
191
- /** @private */
192
- toString() {
193
- return `{{${this.modifier}${this.type === 'magic-word'
194
- ? `${String(this.firstChild)}${this.length === 1 ? '' : ':'}${this.childNodes.slice(1).map(String).join('|')}`
195
- : super.toString('|')}}}`;
196
- }
197
- /** @override */
198
- text() {
199
- const { childNodes, length, firstChild, modifier, type, name } = this;
200
- return type === 'magic-word' && name === 'vardefine'
201
- ? ''
202
- : `{{${modifier}${type === 'magic-word'
203
- ? firstChild.text() + (length === 1 ? '' : ':') + (0, string_1.text)(childNodes.slice(1), '|')
204
- : super.text('|')}}}`;
205
- }
206
- /** @private */
207
- getAttribute(key) {
208
- switch (key) {
209
- case 'padding':
210
- return this.modifier.length + 2;
211
- /* NOT FOR BROWSER */
212
- case 'args':
213
- return new Map(this.#args);
214
- case 'keys':
215
- return this.#keys;
216
- /* NOT FOR BROWSER END */
217
- default:
218
- return super.getAttribute(key);
219
- }
220
- }
221
- /** @private */
222
- getGaps() {
223
- return 1;
224
- }
225
- /** @override */
226
- lint(start = this.getAbsoluteIndex(), re) {
227
- const errors = super.lint(start, re), { type, childNodes, length } = this;
228
- let rect;
229
- if (!this.isTemplate()) {
230
- return errors;
231
- }
232
- const title = this.#getTitle();
233
- if (title.fragment !== undefined) {
234
- rect = { start, ...this.getRootNode().posFromIndex(start) };
235
- const child = childNodes[type === 'template' ? 0 : 1], e = (0, lint_1.generateForChild)(child, rect, 'no-ignored', 'useless fragment'), textNode = child.childNodes.find((c) => c.type === 'text' && c.data.includes('#'));
236
- if (textNode) {
237
- e.fix = {
238
- range: [e.startIndex + textNode.getRelativeIndex() + textNode.data.indexOf('#'), e.endIndex],
239
- text: '',
240
- };
241
- }
242
- errors.push(e);
243
- }
244
- if (!title.valid) {
245
- rect ??= { start, ...this.getRootNode().posFromIndex(start) };
246
- errors.push((0, lint_1.generateForChild)(childNodes[1], rect, 'invalid-invoke', 'illegal module name'));
247
- }
248
- if (type === 'magic-word' && length === 2) {
249
- rect ??= { start, ...this.getRootNode().posFromIndex(start) };
250
- errors.push((0, lint_1.generateForSelf)(this, rect, 'invalid-invoke', 'missing module function'));
251
- return errors;
252
- }
253
- const duplicatedArgs = this.getDuplicatedArgs().filter(([, parameter]) => !parameter[0].querySelector('ext'));
254
- if (duplicatedArgs.length > 0) {
255
- rect ??= { start, ...this.getRootNode().posFromIndex(start) };
256
- errors.push(...duplicatedArgs.flatMap(([, args]) => args).map(arg => (0, lint_1.generateForChild)(arg, rect, 'no-duplicate', 'duplicated parameter')));
257
- }
258
- return errors;
259
- }
260
- /**
261
- * 处理匿名参数更改
262
- * @param addedToken 新增的参数
263
- */
264
- #handleAnonArgChange(addedToken) {
265
- const args = this.getAnonArgs(), added = typeof addedToken !== 'number';
266
- /* NOT FOR BROWSER */
267
- const maxAnon = String(args.length + (added ? 0 : 1));
268
- if (added) {
269
- this.#keys.add(maxAnon);
270
- }
271
- else if (!this.hasArg(maxAnon, true)) {
272
- this.#keys.delete(maxAnon);
273
- }
274
- /* NOT FOR BROWSER END */
275
- for (let i = added ? args.indexOf(addedToken) : addedToken - 1; i < args.length; i++) {
276
- const token = args[i], { name } = token, newName = String(i + 1);
277
- if (name !== newName) {
278
- token.setAttribute('name', newName);
279
- this.getArgs(newName, false, false).add(token);
280
- /* NOT FOR BROWSER */
281
- if (name) {
282
- this.getArgs(name, false, false).delete(token);
283
- }
284
- }
285
- }
286
- }
287
- /**
288
- * @override
289
- * @param token 待插入的子节点
290
- * @param i 插入位置
291
- */
292
- insertAt(token, i = this.length) {
293
- super.insertAt(token, i);
294
- if (token.anon) {
295
- this.#handleAnonArgChange(token);
296
- }
297
- else if (token.name) {
298
- this.getArgs(token.name, false, false).add(token);
299
- /* NOT FOR BROWSER */
300
- this.#keys.add(token.name);
301
- }
302
- return token;
303
- }
304
- /** 获取所有参数 */
305
- getAllArgs() {
306
- return this.childNodes.filter((0, debug_1.isToken)('parameter'));
307
- }
308
- /** 获取所有匿名参数 */
309
- getAnonArgs() {
310
- return this.getAllArgs().filter(({ anon }) => anon);
311
- }
312
- /**
313
- * 获取指定参数
314
- * @param key 参数名
315
- * @param exact 是否匹配匿名性
316
- * @param copy 是否返回一个备份
317
- */
318
- getArgs(key, exact, copy = true) {
319
- const keyStr = String(key).replace(/^[ \t\n\0\v]+|([^ \t\n\0\v])[ \t\n\0\v]+$/gu, '$1');
320
- let args;
321
- if (this.#args.has(keyStr)) {
322
- args = this.#args.get(keyStr);
323
- }
324
- else {
325
- args = new Set(this.getAllArgs().filter(({ name }) => keyStr === name));
326
- this.#args.set(keyStr, args);
327
- }
328
- /* NOT FOR BROWSER */
329
- if (exact && keyStr.trim() && !isNaN(keyStr)) {
330
- args = new Set([...args].filter(({ anon }) => typeof key === 'number' === anon));
331
- }
332
- else if (copy) {
333
- args = new Set(args);
334
- }
335
- /* NOT FOR BROWSER END */
336
- return args;
337
- }
338
- /**
339
- * 获取重名参数
340
- * @throws `Error` 仅用于模板
341
- */
342
- getDuplicatedArgs() {
343
- if (this.isTemplate()) {
344
- return [...this.#args].filter(([, { size }]) => size > 1).map(([key, args]) => [key, [...args]]);
345
- }
346
- throw new Error('getDuplicatedArgs 方法仅供模板使用!');
347
- }
348
- /**
349
- * 对特定魔术字获取可能的取值
350
- * @throws `Error` 不是可接受的魔术字
351
- */
352
- getPossibleValues() {
353
- const { type, name, childNodes, constructor: { name: cName } } = this;
354
- if (type === 'template') {
355
- throw new Error(`${cName}.getPossibleValues 方法仅供特定魔术字使用!`);
356
- }
357
- let start;
358
- switch (name) {
359
- case 'if':
360
- case 'ifexist':
361
- case 'ifexpr':
362
- case 'iferror':
363
- start = 2;
364
- break;
365
- case 'ifeq':
366
- start = 3;
367
- break;
368
- default:
369
- throw new Error(`${cName}.getPossibleValues 方法仅供特定魔术字使用!`);
370
- }
371
- const queue = childNodes.slice(start, start + 2).map(({ childNodes: [, value] }) => value);
372
- for (let i = 0; i < queue.length;) {
373
- const { length, 0: first } = queue[i].childNodes.filter(child => child.text().trim());
374
- if (length === 0) {
375
- queue.splice(i, 1);
376
- }
377
- else if (length > 1 || first.type !== 'magic-word') {
378
- i++;
379
- }
380
- else {
381
- try {
382
- const possibleValues = first.getPossibleValues();
383
- queue.splice(i, 1, ...possibleValues);
384
- i += possibleValues.length;
385
- }
386
- catch {
387
- i++;
388
- }
389
- }
390
- }
391
- return queue;
392
- }
393
- /** @override */
394
- print() {
395
- const { childNodes, length, firstChild, modifier, type } = this;
396
- return `<span class="wpb-${type}">{{${(0, string_1.escape)(modifier)}${type === 'magic-word'
397
- ? firstChild.print() + (length === 1 ? '' : ':') + (0, string_1.print)(childNodes.slice(1), { sep: '|' })
398
- : (0, string_1.print)(childNodes, { sep: '|' })}}}</span>`;
399
- }
400
- /* NOT FOR BROWSER */
401
- /** @override */
402
- cloneNode() {
403
- const [first, ...cloned] = this.cloneChildNodes(), config = this.getAttribute('config');
404
- return debug_1.Shadow.run(() => {
405
- // @ts-expect-error abstract class
406
- const token = new TranscludeToken(this.type === 'template' ? 'T' : first.text(), [], config);
407
- if (this.#raw) {
408
- token.setModifier(this.modifier);
409
- }
410
- else {
411
- token.setAttribute('modifier', this.modifier);
412
- }
413
- token.firstChild.safeReplaceWith(first);
414
- token.afterBuild();
415
- token.append(...cloned);
416
- return token;
417
- });
418
- }
419
- /** 替换引用 */
420
- subst() {
421
- this.setModifier('subst:');
422
- }
423
- /** 安全的替换引用 */
424
- safesubst() {
425
- this.setModifier('safesubst:');
426
- }
427
- /**
428
- * @override
429
- * @param i 移除位置
430
- */
431
- removeAt(i) {
432
- const token = super.removeAt(i);
433
- if (token.anon) {
434
- this.#handleAnonArgChange(Number(token.name));
435
- }
436
- else {
437
- const args = this.getArgs(token.name, false, false);
438
- args.delete(token);
439
- if (args.size === 0) {
440
- this.#keys.delete(token.name);
441
- }
442
- }
443
- return token;
444
- }
445
- /**
446
- * 是否具有某参数
447
- * @param key 参数名
448
- * @param exact 是否匹配匿名性
449
- */
450
- hasArg(key, exact) {
451
- return this.getArgs(key, exact, false).size > 0;
452
- }
453
- /**
454
- * 获取生效的指定参数
455
- * @param key 参数名
456
- * @param exact 是否匹配匿名性
457
- */
458
- getArg(key, exact) {
459
- const args = [...this.getArgs(key, exact, false)].sort((a, b) => a.compareDocumentPosition(b));
460
- return args[args.length - 1];
461
- }
462
- /**
463
- * 移除指定参数
464
- * @param key 参数名
465
- * @param exact 是否匹配匿名性
466
- */
467
- removeArg(key, exact) {
468
- debug_1.Shadow.run(() => {
469
- for (const token of this.getArgs(key, exact, false)) {
470
- this.removeChild(token);
471
- }
472
- });
473
- }
474
- /** 获取所有参数名 */
475
- getKeys() {
476
- const args = this.getAllArgs();
477
- if (this.#keys.size === 0 && args.length > 0) {
478
- for (const { name } of args) {
479
- this.#keys.add(name);
480
- }
481
- }
482
- return [...this.#keys];
483
- }
484
- /**
485
- * 获取参数值
486
- * @param key 参数名
487
- */
488
- getValues(key) {
489
- return [...this.getArgs(key, false, false)].map(token => token.getValue());
490
- }
491
- getValue(key) {
492
- return key === undefined
493
- ? Object.fromEntries(this.getKeys().map(k => [k, this.getValue(k)]))
494
- : this.getArg(key)?.getValue();
495
- }
496
- /**
497
- * 插入匿名参数
498
- * @param val 参数值
499
- */
500
- newAnonArg(val) {
501
- require('../addon/transclude');
502
- return this.newAnonArg(val);
503
- }
504
- /**
505
- * 设置参数值
506
- * @param key 参数名
507
- * @param value 参数值
508
- * @throws `Error` 仅用于模板
509
- */
510
- setValue(key, value) {
511
- require('../addon/transclude');
512
- this.setValue(key, value);
513
- }
514
- /**
515
- * 将匿名参数改写为命名参数
516
- * @throws `Error` 仅用于模板
517
- */
518
- anonToNamed() {
519
- if (!this.isTemplate()) {
520
- throw new Error('anonToNamed 方法仅供模板使用!');
521
- }
522
- for (const token of this.getAnonArgs()) {
523
- token.firstChild.replaceChildren(token.name);
524
- }
525
- }
526
- /**
527
- * 替换模板名
528
- * @param title 模板名
529
- * @throws `Error` 仅用于模板
530
- */
531
- replaceTemplate(title) {
532
- require('../addon/transclude');
533
- this.replaceTemplate(title);
534
- }
535
- /**
536
- * 替换模块名
537
- * @param title 模块名
538
- * @throws `Error` 仅用于模块
539
- */
540
- replaceModule(title) {
541
- require('../addon/transclude');
542
- this.replaceModule(title);
543
- }
544
- /**
545
- * 替换模块函数
546
- * @param func 模块函数名
547
- * @throws `Error` 仅用于模块
548
- * @throws `Error` 尚未指定模块名称
549
- */
550
- replaceFunction(func) {
551
- require('../addon/transclude');
552
- this.replaceFunction(func);
553
- }
554
- /**
555
- * 重复参数计数
556
- * @throws `Error` 仅用于模板
557
- */
558
- hasDuplicatedArgs() {
559
- if (this.isTemplate()) {
560
- return this.getAllArgs().length - this.getKeys().length;
561
- }
562
- throw new Error('hasDuplicatedArgs 方法仅供模板使用!');
563
- }
564
- /**
565
- * 修复重名参数:
566
- * `aggressive = false`时只移除空参数和全同参数,优先保留匿名参数,否则将所有匿名参数更改为命名。
567
- * `aggressive = true`时还会尝试处理连续的以数字编号的参数。
568
- * @param aggressive 是否使用有更大风险的修复手段
569
- */
570
- fixDuplication(aggressive) {
571
- require('../addon/transclude');
572
- return this.fixDuplication(aggressive);
573
- }
574
- /**
575
- * 转义模板内的表格
576
- * @throws `Error` 转义失败
577
- */
578
- escapeTables() {
579
- require('../addon/transclude');
580
- return this.escapeTables();
581
- }
582
- }
583
- exports.TranscludeToken = TranscludeToken;
584
- constants_1.classes['TranscludeToken'] = __filename;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.aliases = exports.parsers = exports.mixins = exports.classes = exports.BuildMethod = exports.MAX_STAGE = void 0;
4
- exports.MAX_STAGE = 11;
5
- var BuildMethod;
6
- (function (BuildMethod) {
7
- BuildMethod[BuildMethod["String"] = 0] = "String";
8
- BuildMethod[BuildMethod["Text"] = 1] = "Text";
9
- })(BuildMethod || (exports.BuildMethod = BuildMethod = {}));
10
- /* NOT FOR BROWSER */
11
- exports.classes = {};
12
- exports.mixins = {};
13
- exports.parsers = {};
14
- exports.aliases = [
15
- ['AstText'],
16
- ['CommentToken', 'ExtToken', 'IncludeToken', 'NoincludeToken'],
17
- ['ArgToken', 'TranscludeToken', 'HeadingToken'],
18
- ['HtmlToken'],
19
- ['TableToken'],
20
- ['HrToken', 'DoubleUnderscoreToken'],
21
- ['LinkToken', 'FileToken', 'CategoryToken'],
22
- ['QuoteToken'],
23
- ['ExtLinkToken'],
24
- ['MagicLinkToken'],
25
- ['ListToken', 'DdToken'],
26
- ['ConverterToken'],
27
- ];
@@ -1,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.undo = exports.emptyArray = exports.setChildNodes = exports.isToken = exports.Shadow = void 0;
4
- exports.Shadow = {
5
- running: false,
6
- /** @private */
7
- run(callback) {
8
- const { running } = this;
9
- this.running = true;
10
- try {
11
- const result = callback();
12
- this.running = running;
13
- return result;
14
- }
15
- catch (e) {
16
- this.running = running;
17
- throw e;
18
- }
19
- },
20
- };
21
- /**
22
- * 是否是某一特定类型的节点
23
- * @param type 节点类型
24
- */
25
- const isToken = (type) => (node) => node.type === type;
26
- exports.isToken = isToken;
27
- /**
28
- * 更新chldNodes
29
- * @param parent 父节点
30
- * @param position 子节点位置
31
- * @param deleteCount 移除的子节点数量
32
- * @param inserted 插入的子节点
33
- */
34
- const setChildNodes = (parent, position, deleteCount, inserted = []) => {
35
- const childNodes = [...parent.childNodes], removed = childNodes.splice(position, deleteCount, ...inserted);
36
- parent.setAttribute('childNodes', childNodes);
37
- for (const node of inserted) {
38
- node.setAttribute('parentNode', parent);
39
- }
40
- /* NOT FOR BROWSER */
41
- for (const node of removed) {
42
- node.setAttribute('parentNode', undefined);
43
- }
44
- /* NOT FOR BROWSER END */
45
- return removed;
46
- };
47
- exports.setChildNodes = setChildNodes;
48
- /**
49
- * 生成一个指定长度的空数组
50
- * @param n 数组长度
51
- * @param callback 回调函数
52
- */
53
- const emptyArray = (n, callback) => new Array(n).fill(undefined).map((_, i) => callback(i));
54
- exports.emptyArray = emptyArray;
55
- /* NOT FOR BROWSER */
56
- /**
57
- * 撤销最近一次Mutation
58
- * @param e 事件
59
- * @param data 事件数据
60
- * @throws `RangeError` 无法撤销的事件类型
61
- */
62
- const undo = (e, data) => {
63
- const { target, type } = e;
64
- switch (data.type) {
65
- case 'remove':
66
- (0, exports.setChildNodes)(target, data.position, 0, [data.removed]);
67
- break;
68
- case 'insert':
69
- (0, exports.setChildNodes)(target, data.position, 1);
70
- break;
71
- case 'replace':
72
- (0, exports.setChildNodes)(target.parentNode, data.position, 1, [data.oldToken]);
73
- break;
74
- case 'text':
75
- target.replaceData(data.oldText);
76
- break;
77
- default:
78
- throw new RangeError(`Unable to undo events with an unknown type: ${type}`);
79
- }
80
- };
81
- exports.undo = undo;