wikiparser-node 1.4.5 → 1.5.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 (162) hide show
  1. package/bundle/bundle.min.js +37 -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 +59 -0
  9. package/extensions/dist/lint.js +60 -0
  10. package/extensions/editor.css +63 -0
  11. package/extensions/ui.css +141 -0
  12. package/package.json +28 -14
  13. package/config/.schema.json +0 -172
  14. package/dist/addon/table.d.ts +0 -6
  15. package/dist/addon/table.js +0 -564
  16. package/dist/base.d.ts +0 -60
  17. package/dist/index.d.ts +0 -30
  18. package/dist/index.js +0 -211
  19. package/dist/internal.d.ts +0 -44
  20. package/dist/lib/element.d.ts +0 -155
  21. package/dist/lib/element.js +0 -655
  22. package/dist/lib/node.d.ts +0 -146
  23. package/dist/lib/node.js +0 -428
  24. package/dist/lib/range.d.ts +0 -104
  25. package/dist/lib/range.js +0 -385
  26. package/dist/lib/ranges.d.ts +0 -26
  27. package/dist/lib/ranges.js +0 -118
  28. package/dist/lib/text.d.ts +0 -62
  29. package/dist/lib/text.js +0 -314
  30. package/dist/lib/title.d.ts +0 -38
  31. package/dist/lib/title.js +0 -164
  32. package/dist/mixin/attributesParent.d.ts +0 -50
  33. package/dist/mixin/attributesParent.js +0 -78
  34. package/dist/mixin/fixed.d.ts +0 -6
  35. package/dist/mixin/fixed.js +0 -30
  36. package/dist/mixin/flagsParent.d.ts +0 -72
  37. package/dist/mixin/flagsParent.js +0 -59
  38. package/dist/mixin/hidden.d.ts +0 -5
  39. package/dist/mixin/hidden.js +0 -24
  40. package/dist/mixin/magicLinkParent.d.ts +0 -40
  41. package/dist/mixin/magicLinkParent.js +0 -42
  42. package/dist/mixin/singleLine.d.ts +0 -6
  43. package/dist/mixin/singleLine.js +0 -26
  44. package/dist/mixin/sol.d.ts +0 -6
  45. package/dist/mixin/sol.js +0 -43
  46. package/dist/mixin/syntax.d.ts +0 -9
  47. package/dist/mixin/syntax.js +0 -50
  48. package/dist/parser/braces.js +0 -131
  49. package/dist/parser/commentAndExt.js +0 -90
  50. package/dist/parser/converter.js +0 -41
  51. package/dist/parser/externalLinks.js +0 -32
  52. package/dist/parser/hrAndDoubleUnderscore.js +0 -41
  53. package/dist/parser/html.js +0 -39
  54. package/dist/parser/links.js +0 -95
  55. package/dist/parser/list.js +0 -68
  56. package/dist/parser/magicLinks.js +0 -43
  57. package/dist/parser/quotes.js +0 -68
  58. package/dist/parser/selector.js +0 -162
  59. package/dist/parser/table.js +0 -124
  60. package/dist/src/arg.d.ts +0 -58
  61. package/dist/src/arg.js +0 -196
  62. package/dist/src/atom.d.ts +0 -12
  63. package/dist/src/atom.js +0 -27
  64. package/dist/src/attribute.d.ts +0 -69
  65. package/dist/src/attribute.js +0 -475
  66. package/dist/src/attributes.d.ts +0 -109
  67. package/dist/src/attributes.js +0 -346
  68. package/dist/src/converter.d.ts +0 -33
  69. package/dist/src/converter.js +0 -134
  70. package/dist/src/converterFlags.d.ts +0 -87
  71. package/dist/src/converterFlags.js +0 -226
  72. package/dist/src/converterRule.d.ts +0 -77
  73. package/dist/src/converterRule.js +0 -209
  74. package/dist/src/extLink.d.ts +0 -43
  75. package/dist/src/extLink.js +0 -186
  76. package/dist/src/gallery.d.ts +0 -54
  77. package/dist/src/gallery.js +0 -127
  78. package/dist/src/heading.d.ts +0 -47
  79. package/dist/src/heading.js +0 -190
  80. package/dist/src/hidden.d.ts +0 -7
  81. package/dist/src/hidden.js +0 -23
  82. package/dist/src/html.d.ts +0 -65
  83. package/dist/src/html.js +0 -340
  84. package/dist/src/imageParameter.d.ts +0 -65
  85. package/dist/src/imageParameter.js +0 -246
  86. package/dist/src/imagemap.d.ts +0 -56
  87. package/dist/src/imagemap.js +0 -150
  88. package/dist/src/imagemapLink.d.ts +0 -31
  89. package/dist/src/imagemapLink.js +0 -93
  90. package/dist/src/index.d.ts +0 -143
  91. package/dist/src/index.js +0 -803
  92. package/dist/src/link/base.d.ts +0 -52
  93. package/dist/src/link/base.js +0 -209
  94. package/dist/src/link/category.d.ts +0 -13
  95. package/dist/src/link/category.js +0 -28
  96. package/dist/src/link/file.d.ts +0 -96
  97. package/dist/src/link/file.js +0 -265
  98. package/dist/src/link/galleryImage.d.ts +0 -32
  99. package/dist/src/link/galleryImage.js +0 -157
  100. package/dist/src/link/index.d.ts +0 -56
  101. package/dist/src/link/index.js +0 -133
  102. package/dist/src/magicLink.d.ts +0 -59
  103. package/dist/src/magicLink.js +0 -199
  104. package/dist/src/nested.d.ts +0 -43
  105. package/dist/src/nested.js +0 -92
  106. package/dist/src/nowiki/base.d.ts +0 -31
  107. package/dist/src/nowiki/base.js +0 -93
  108. package/dist/src/nowiki/comment.d.ts +0 -25
  109. package/dist/src/nowiki/comment.js +0 -70
  110. package/dist/src/nowiki/dd.d.ts +0 -8
  111. package/dist/src/nowiki/dd.js +0 -24
  112. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -18
  113. package/dist/src/nowiki/doubleUnderscore.js +0 -101
  114. package/dist/src/nowiki/hr.d.ts +0 -5
  115. package/dist/src/nowiki/hr.js +0 -63
  116. package/dist/src/nowiki/index.d.ts +0 -16
  117. package/dist/src/nowiki/index.js +0 -21
  118. package/dist/src/nowiki/list.d.ts +0 -16
  119. package/dist/src/nowiki/list.js +0 -97
  120. package/dist/src/nowiki/listBase.d.ts +0 -5
  121. package/dist/src/nowiki/listBase.js +0 -61
  122. package/dist/src/nowiki/noinclude.d.ts +0 -10
  123. package/dist/src/nowiki/noinclude.js +0 -23
  124. package/dist/src/nowiki/quote.d.ts +0 -14
  125. package/dist/src/nowiki/quote.js +0 -117
  126. package/dist/src/onlyinclude.d.ts +0 -16
  127. package/dist/src/onlyinclude.js +0 -57
  128. package/dist/src/paramTag/index.d.ts +0 -37
  129. package/dist/src/paramTag/index.js +0 -69
  130. package/dist/src/paramTag/inputbox.d.ts +0 -8
  131. package/dist/src/paramTag/inputbox.js +0 -22
  132. package/dist/src/parameter.d.ts +0 -70
  133. package/dist/src/parameter.js +0 -262
  134. package/dist/src/pre.d.ts +0 -28
  135. package/dist/src/pre.js +0 -55
  136. package/dist/src/syntax.d.ts +0 -20
  137. package/dist/src/syntax.js +0 -86
  138. package/dist/src/table/base.d.ts +0 -30
  139. package/dist/src/table/base.js +0 -132
  140. package/dist/src/table/index.d.ts +0 -233
  141. package/dist/src/table/index.js +0 -396
  142. package/dist/src/table/td.d.ts +0 -92
  143. package/dist/src/table/td.js +0 -337
  144. package/dist/src/table/tr.d.ts +0 -32
  145. package/dist/src/table/tr.js +0 -58
  146. package/dist/src/table/trBase.d.ts +0 -53
  147. package/dist/src/table/trBase.js +0 -154
  148. package/dist/src/tagPair/ext.d.ts +0 -32
  149. package/dist/src/tagPair/ext.js +0 -203
  150. package/dist/src/tagPair/include.d.ts +0 -35
  151. package/dist/src/tagPair/include.js +0 -65
  152. package/dist/src/tagPair/index.d.ts +0 -27
  153. package/dist/src/tagPair/index.js +0 -128
  154. package/dist/src/transclude.d.ts +0 -167
  155. package/dist/src/transclude.js +0 -710
  156. package/dist/util/constants.js +0 -113
  157. package/dist/util/debug.js +0 -88
  158. package/dist/util/diff.js +0 -82
  159. package/dist/util/lint.js +0 -31
  160. package/dist/util/string.js +0 -53
  161. package/errors/README +0 -1
  162. package/printed/README +0 -1
@@ -1,710 +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)((0, string_1.decodeHtml)(title)).split('#')[0].trim();
93
- if (!name || /^:[\s_]*:|\0\d+[eh!+-]\x7F|[<>[\]{}\n]|%[\da-f]{2}/iu.test(name)) {
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()) {
227
- const errors = super.lint(start), { 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
- errors.push((0, lint_1.generateForChild)(childNodes[type === 'template' ? 0 : 1], rect, 'no-ignored', 'useless fragment'));
236
- }
237
- if (!title.valid) {
238
- rect ??= { start, ...this.getRootNode().posFromIndex(start) };
239
- errors.push((0, lint_1.generateForChild)(childNodes[1], rect, 'invalid-invoke', 'illegal module name'));
240
- }
241
- if (type === 'magic-word' && length === 2) {
242
- rect ??= { start, ...this.getRootNode().posFromIndex(start) };
243
- errors.push((0, lint_1.generateForSelf)(this, rect, 'invalid-invoke', 'missing module function'));
244
- return errors;
245
- }
246
- const duplicatedArgs = this.getDuplicatedArgs();
247
- if (duplicatedArgs.length > 0) {
248
- rect ??= { start, ...this.getRootNode().posFromIndex(start) };
249
- errors.push(...duplicatedArgs.flatMap(([, args]) => args).map(arg => (0, lint_1.generateForChild)(arg, rect, 'no-duplicate', 'duplicated parameter')));
250
- }
251
- return errors;
252
- }
253
- /**
254
- * 处理匿名参数更改
255
- * @param addedToken 新增的参数
256
- */
257
- #handleAnonArgChange(addedToken) {
258
- const args = this.getAnonArgs(), added = typeof addedToken !== 'number';
259
- /* NOT FOR BROWSER */
260
- const maxAnon = String(args.length + (added ? 0 : 1));
261
- if (added) {
262
- this.#keys.add(maxAnon);
263
- }
264
- else if (!this.hasArg(maxAnon, true)) {
265
- this.#keys.delete(maxAnon);
266
- }
267
- /* NOT FOR BROWSER END */
268
- for (let i = added ? args.indexOf(addedToken) : addedToken - 1; i < args.length; i++) {
269
- const token = args[i], { name } = token, newName = String(i + 1);
270
- if (name !== newName) {
271
- token.setAttribute('name', newName);
272
- this.getArgs(newName, false, false).add(token);
273
- /* NOT FOR BROWSER */
274
- if (name) {
275
- this.getArgs(name, false, false).delete(token);
276
- }
277
- }
278
- }
279
- }
280
- /**
281
- * @override
282
- * @param token 待插入的子节点
283
- * @param i 插入位置
284
- */
285
- insertAt(token, i = this.length) {
286
- super.insertAt(token, i);
287
- if (token.anon) {
288
- this.#handleAnonArgChange(token);
289
- }
290
- else if (token.name) {
291
- this.getArgs(token.name, false, false).add(token);
292
- /* NOT FOR BROWSER */
293
- this.#keys.add(token.name);
294
- }
295
- return token;
296
- }
297
- /** 获取所有参数 */
298
- getAllArgs() {
299
- return this.childNodes.filter((0, debug_1.isToken)('parameter'));
300
- }
301
- /** 获取所有匿名参数 */
302
- getAnonArgs() {
303
- return this.getAllArgs().filter(({ anon }) => anon);
304
- }
305
- /**
306
- * 获取指定参数
307
- * @param key 参数名
308
- * @param exact 是否匹配匿名性
309
- * @param copy 是否返回一个备份
310
- */
311
- getArgs(key, exact = false, copy = true) {
312
- const keyStr = String(key).replace(/^[ \t\n\0\v]+|([^ \t\n\0\v])[ \t\n\0\v]+$/gu, '$1');
313
- let args;
314
- if (this.#args.has(keyStr)) {
315
- args = this.#args.get(keyStr);
316
- }
317
- else {
318
- args = new Set(this.getAllArgs().filter(({ name }) => keyStr === name));
319
- this.#args.set(keyStr, args);
320
- }
321
- /* NOT FOR BROWSER */
322
- if (exact && !Number.isNaN(Number(keyStr))) {
323
- args = new Set([...args].filter(({ anon }) => typeof key === 'number' === anon));
324
- }
325
- else if (copy) {
326
- args = new Set(args);
327
- }
328
- /* NOT FOR BROWSER END */
329
- return args;
330
- }
331
- /**
332
- * 获取重名参数
333
- * @throws `Error` 仅用于模板
334
- */
335
- getDuplicatedArgs() {
336
- if (this.isTemplate()) {
337
- return [...this.#args].filter(([, { size }]) => size > 1).map(([key, args]) => [key, [...args]]);
338
- }
339
- throw new Error('getDuplicatedArgs 方法仅供模板使用!');
340
- }
341
- /**
342
- * 对特定魔术字获取可能的取值
343
- * @throws `Error` 不是可接受的魔术字
344
- */
345
- getPossibleValues() {
346
- const { type, name, childNodes, constructor: { name: cName } } = this;
347
- if (type === 'template') {
348
- throw new Error(`${cName}.getPossibleValues 方法仅供特定魔术字使用!`);
349
- }
350
- let start;
351
- switch (name) {
352
- case 'if':
353
- case 'ifexist':
354
- case 'ifexpr':
355
- case 'iferror':
356
- start = 2;
357
- break;
358
- case 'ifeq':
359
- start = 3;
360
- break;
361
- default:
362
- throw new Error(`${cName}.getPossibleValues 方法仅供特定魔术字使用!`);
363
- }
364
- const queue = childNodes.slice(start, start + 2).map(({ childNodes: [, value] }) => value);
365
- for (let i = 0; i < queue.length;) {
366
- const { length, 0: first } = queue[i].childNodes.filter(child => child.text().trim());
367
- if (length === 0) {
368
- queue.splice(i, 1);
369
- }
370
- else if (length > 1 || first.type !== 'magic-word') {
371
- i++;
372
- }
373
- else {
374
- try {
375
- const possibleValues = first.getPossibleValues();
376
- queue.splice(i, 1, ...possibleValues);
377
- i += possibleValues.length;
378
- }
379
- catch {
380
- i++;
381
- }
382
- }
383
- }
384
- return queue;
385
- }
386
- /** @override */
387
- print() {
388
- const { childNodes, length, firstChild, modifier, type } = this;
389
- return `<span class="wpb-${type}">{{${modifier}${type === 'magic-word'
390
- ? `${firstChild.print()}${length === 1 ? '' : ':'}${(0, string_1.print)(childNodes.slice(1), { sep: '|' })}`
391
- : (0, string_1.print)(childNodes, { sep: '|' })}}}</span>`;
392
- }
393
- /* NOT FOR BROWSER */
394
- /** @override */
395
- cloneNode() {
396
- const [first, ...cloned] = this.cloneChildNodes(), config = this.getAttribute('config');
397
- return debug_1.Shadow.run(() => {
398
- // @ts-expect-error abstract class
399
- const token = new TranscludeToken(this.type === 'template' ? 'T' : first.text(), [], config);
400
- if (this.#raw) {
401
- token.setModifier(this.modifier);
402
- }
403
- else {
404
- token.setAttribute('modifier', this.modifier);
405
- }
406
- token.firstChild.safeReplaceWith(first);
407
- token.afterBuild();
408
- token.append(...cloned);
409
- return token;
410
- });
411
- }
412
- /** 替换引用 */
413
- subst() {
414
- this.setModifier('subst:');
415
- }
416
- /** 安全的替换引用 */
417
- safesubst() {
418
- this.setModifier('safesubst:');
419
- }
420
- /**
421
- * @override
422
- * @param i 移除位置
423
- */
424
- removeAt(i) {
425
- const token = super.removeAt(i);
426
- if (token.anon) {
427
- this.#handleAnonArgChange(Number(token.name));
428
- }
429
- else {
430
- const args = this.getArgs(token.name, false, false);
431
- args.delete(token);
432
- if (args.size === 0) {
433
- this.#keys.delete(token.name);
434
- }
435
- }
436
- return token;
437
- }
438
- /**
439
- * 是否具有某参数
440
- * @param key 参数名
441
- * @param exact 是否匹配匿名性
442
- */
443
- hasArg(key, exact = false) {
444
- return this.getArgs(key, exact, false).size > 0;
445
- }
446
- /**
447
- * 获取生效的指定参数
448
- * @param key 参数名
449
- * @param exact 是否匹配匿名性
450
- */
451
- getArg(key, exact = false) {
452
- const args = [...this.getArgs(key, exact, false)].sort((a, b) => a.compareDocumentPosition(b));
453
- return args[args.length - 1];
454
- }
455
- /**
456
- * 移除指定参数
457
- * @param key 参数名
458
- * @param exact 是否匹配匿名性
459
- */
460
- removeArg(key, exact = false) {
461
- debug_1.Shadow.run(() => {
462
- for (const token of this.getArgs(key, exact, false)) {
463
- this.removeChild(token);
464
- }
465
- });
466
- }
467
- /** 获取所有参数名 */
468
- getKeys() {
469
- const args = this.getAllArgs();
470
- if (this.#keys.size === 0 && args.length > 0) {
471
- for (const { name } of args) {
472
- this.#keys.add(name);
473
- }
474
- }
475
- return [...this.#keys];
476
- }
477
- /**
478
- * 获取参数值
479
- * @param key 参数名
480
- */
481
- getValues(key) {
482
- return [...this.getArgs(key, false, false)].map(token => token.getValue());
483
- }
484
- /**
485
- * 获取生效的参数值
486
- * @param key 参数名
487
- */
488
- getValue(key) {
489
- return key === undefined
490
- ? Object.fromEntries(this.getKeys().map(k => [k, this.getValue(k)]))
491
- : this.getArg(key)?.getValue();
492
- }
493
- /**
494
- * 插入匿名参数
495
- * @param val 参数值
496
- */
497
- newAnonArg(val) {
498
- const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(val, this.getAttribute('include'), undefined, config),
499
- // @ts-expect-error abstract class
500
- token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
501
- token.lastChild.append(...childNodes);
502
- token.afterBuild();
503
- return this.insertAt(token);
504
- }
505
- /**
506
- * 设置参数值
507
- * @param key 参数名
508
- * @param value 参数值
509
- * @throws `Error` 仅用于模板
510
- */
511
- setValue(key, value) {
512
- if (!this.isTemplate()) {
513
- throw new Error('setValue 方法仅供模板使用!');
514
- }
515
- const arg = this.getArg(key);
516
- if (arg) {
517
- arg.setValue(value);
518
- return;
519
- }
520
- const include = this.getAttribute('include'), config = this.getAttribute('config'), k = index_1.default.parse(key, include, undefined, config), v = index_1.default.parse(value, include, undefined, config),
521
- // @ts-expect-error abstract class
522
- token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
523
- token.firstChild.append(...k.childNodes);
524
- token.lastChild.append(...v.childNodes);
525
- token.afterBuild();
526
- this.insertAt(token);
527
- }
528
- /**
529
- * 将匿名参数改写为命名参数
530
- * @throws `Error` 仅用于模板
531
- */
532
- anonToNamed() {
533
- if (!this.isTemplate()) {
534
- throw new Error('anonToNamed 方法仅供模板使用!');
535
- }
536
- for (const token of this.getAnonArgs()) {
537
- token.firstChild.replaceChildren(token.name);
538
- }
539
- }
540
- /**
541
- * 替换模板名
542
- * @param title 模板名
543
- * @throws `Error` 仅用于模板
544
- */
545
- replaceTemplate(title) {
546
- if (this.type === 'magic-word') {
547
- throw new Error('replaceTemplate 方法仅用于更换模板!');
548
- }
549
- const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, this.getAttribute('config'));
550
- this.firstChild.replaceChildren(...childNodes);
551
- }
552
- /**
553
- * 替换模块名
554
- * @param title 模块名
555
- * @throws `Error` 仅用于模块
556
- */
557
- replaceModule(title) {
558
- if (this.type !== 'magic-word' || this.name !== 'invoke') {
559
- throw new Error('replaceModule 方法仅用于更换模块!');
560
- }
561
- const config = this.getAttribute('config');
562
- if (this.length === 1) {
563
- super.insertAt(new atom_1.AtomToken(undefined, 'invoke-module', config, [], {
564
- 'Stage-1': ':', '!ExtToken': '',
565
- }));
566
- return;
567
- }
568
- const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, config);
569
- this.childNodes[1].replaceChildren(...childNodes);
570
- }
571
- /**
572
- * 替换模块函数
573
- * @param func 模块函数名
574
- * @throws `Error` 仅用于模块
575
- * @throws `Error` 尚未指定模块名称
576
- */
577
- replaceFunction(func) {
578
- if (this.type !== 'magic-word' || this.name !== 'invoke') {
579
- throw new Error('replaceModule 方法仅用于更换模块!');
580
- }
581
- else if (this.length < 2) {
582
- throw new Error('尚未指定模块名称!');
583
- }
584
- const config = this.getAttribute('config');
585
- if (this.length === 2) {
586
- super.insertAt(new atom_1.AtomToken(undefined, 'invoke-function', config, [], {
587
- 'Stage-1': ':', '!ExtToken': '',
588
- }));
589
- return;
590
- }
591
- const { childNodes } = index_1.default.parse(func, this.getAttribute('include'), 2, config);
592
- this.childNodes[2].replaceChildren(...childNodes);
593
- }
594
- /**
595
- * 重复参数计数
596
- * @throws `Error` 仅用于模板
597
- */
598
- hasDuplicatedArgs() {
599
- if (this.isTemplate()) {
600
- return this.getAllArgs().length - this.getKeys().length;
601
- }
602
- throw new Error('hasDuplicatedArgs 方法仅供模板使用!');
603
- }
604
- /**
605
- * 修复重名参数:
606
- * `aggressive = false`时只移除空参数和全同参数,优先保留匿名参数,否则将所有匿名参数更改为命名。
607
- * `aggressive = true`时还会尝试处理连续的以数字编号的参数。
608
- * @param aggressive 是否使用有更大风险的修复手段
609
- */
610
- fixDuplication(aggressive = false) {
611
- if (!this.hasDuplicatedArgs()) {
612
- return [];
613
- }
614
- const duplicatedKeys = [];
615
- let anonCount = this.getAnonArgs().length;
616
- for (const [key, args] of this.getDuplicatedArgs()) {
617
- if (args.length <= 1) {
618
- continue;
619
- }
620
- const values = new Map();
621
- for (const arg of args) {
622
- const val = arg.getValue().trim();
623
- if (values.has(val)) {
624
- values.get(val).push(arg);
625
- }
626
- else {
627
- values.set(val, [arg]);
628
- }
629
- }
630
- let noMoreAnon = anonCount === 0 || Number.isNaN(Number(key));
631
- const emptyArgs = values.get('') ?? [], duplicatedArgs = [...values].filter(([val, { length }]) => val && length > 1).flatMap(([, curArgs]) => {
632
- const anonIndex = noMoreAnon ? -1 : curArgs.findIndex(({ anon }) => anon);
633
- if (anonIndex !== -1) {
634
- noMoreAnon = true;
635
- }
636
- curArgs.splice(anonIndex, 1);
637
- return curArgs;
638
- }), badArgs = [...emptyArgs, ...duplicatedArgs], index = noMoreAnon ? -1 : emptyArgs.findIndex(({ anon }) => anon);
639
- if (badArgs.length === args.length) {
640
- badArgs.splice(index, 1);
641
- }
642
- else if (index !== -1) {
643
- this.anonToNamed();
644
- anonCount = 0;
645
- }
646
- for (const arg of badArgs) {
647
- arg.remove();
648
- }
649
- let remaining = args.length - badArgs.length;
650
- if (remaining === 1) {
651
- continue;
652
- }
653
- else if (aggressive && (anonCount ? /\D\d+$/u : /(?:^|\D)\d+$/u).test(key)) {
654
- let last;
655
- // eslint-disable-next-line es-x/no-regexp-lookbehind-assertions
656
- const str = key.slice(0, -/(?<!\d)\d+$/u.exec(key)[0].length), regex = new RegExp(`^${(0, string_1.escapeRegExp)(str)}\\d+$`, 'u'), series = this.getAllArgs().filter(({ name }) => regex.test(name)), ordered = series.every(({ name }, i) => {
657
- const j = Number(name.slice(str.length)), cmp = j <= i + 1 && (i === 0 || j >= last || name === key);
658
- last = j;
659
- return cmp;
660
- });
661
- if (ordered) {
662
- for (let i = 0; i < series.length; i++) {
663
- const name = `${str}${i + 1}`, arg = series[i];
664
- if (arg.name !== name) {
665
- if (arg.name === key) {
666
- remaining--;
667
- }
668
- arg.rename(name, true);
669
- }
670
- }
671
- }
672
- }
673
- if (remaining > 1) {
674
- index_1.default.error(`${this.type === 'template'
675
- ? this.name
676
- : this.normalizeTitle(this.childNodes[1].text(), 828)
677
- .title} 还留有 ${remaining} 个重复的 ${key} 参数:${[...this.getArgs(key)].map(arg => {
678
- const { top, left } = arg.getBoundingClientRect();
679
- return `第 ${String(top)} 行第 ${String(left)} 列`;
680
- }).join('、')}`);
681
- duplicatedKeys.push(key);
682
- continue;
683
- }
684
- }
685
- return duplicatedKeys;
686
- }
687
- /**
688
- * 转义模板内的表格
689
- * @throws `Error` 转义失败
690
- */
691
- escapeTables() {
692
- if (!/\n[^\S\n]*(?::+[^\S\n]*)?\{\|/u.test(this.text())) {
693
- return this;
694
- }
695
- const stripped = String(this).slice(2, -2), include = this.getAttribute('include'), config = this.getAttribute('config'), parsed = index_1.default.parse(stripped, include, 4, config), isTable = (0, debug_1.isToken)('table');
696
- for (const table of parsed.childNodes) {
697
- if (isTable(table)) {
698
- table.escape();
699
- }
700
- }
701
- const { firstChild, length } = index_1.default.parse(`{{${String(parsed)}}}`, include, undefined, config);
702
- if (length !== 1 || !(firstChild instanceof TranscludeToken)) {
703
- throw new Error('转义表格失败!');
704
- }
705
- this.safeReplaceWith(firstChild);
706
- return firstChild;
707
- }
708
- }
709
- exports.TranscludeToken = TranscludeToken;
710
- constants_1.classes['TranscludeToken'] = __filename;