wikiparser-node 1.5.2 → 1.5.3-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 (163) 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 +30 -17
  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 -72
  17. package/dist/base.js +0 -38
  18. package/dist/index.d.ts +0 -30
  19. package/dist/index.js +0 -214
  20. package/dist/internal.d.ts +0 -44
  21. package/dist/lib/element.d.ts +0 -150
  22. package/dist/lib/element.js +0 -613
  23. package/dist/lib/node.d.ts +0 -146
  24. package/dist/lib/node.js +0 -428
  25. package/dist/lib/range.d.ts +0 -104
  26. package/dist/lib/range.js +0 -385
  27. package/dist/lib/ranges.d.ts +0 -26
  28. package/dist/lib/ranges.js +0 -118
  29. package/dist/lib/text.d.ts +0 -62
  30. package/dist/lib/text.js +0 -361
  31. package/dist/lib/title.d.ts +0 -38
  32. package/dist/lib/title.js +0 -166
  33. package/dist/mixin/attributesParent.d.ts +0 -50
  34. package/dist/mixin/attributesParent.js +0 -78
  35. package/dist/mixin/fixed.d.ts +0 -6
  36. package/dist/mixin/fixed.js +0 -30
  37. package/dist/mixin/flagsParent.d.ts +0 -72
  38. package/dist/mixin/flagsParent.js +0 -59
  39. package/dist/mixin/hidden.d.ts +0 -5
  40. package/dist/mixin/hidden.js +0 -24
  41. package/dist/mixin/magicLinkParent.d.ts +0 -40
  42. package/dist/mixin/magicLinkParent.js +0 -42
  43. package/dist/mixin/singleLine.d.ts +0 -6
  44. package/dist/mixin/singleLine.js +0 -26
  45. package/dist/mixin/sol.d.ts +0 -6
  46. package/dist/mixin/sol.js +0 -43
  47. package/dist/mixin/syntax.d.ts +0 -9
  48. package/dist/mixin/syntax.js +0 -50
  49. package/dist/parser/braces.js +0 -131
  50. package/dist/parser/commentAndExt.js +0 -90
  51. package/dist/parser/converter.js +0 -41
  52. package/dist/parser/externalLinks.js +0 -32
  53. package/dist/parser/hrAndDoubleUnderscore.js +0 -41
  54. package/dist/parser/html.js +0 -39
  55. package/dist/parser/links.js +0 -95
  56. package/dist/parser/list.js +0 -98
  57. package/dist/parser/magicLinks.js +0 -43
  58. package/dist/parser/quotes.js +0 -68
  59. package/dist/parser/selector.js +0 -162
  60. package/dist/parser/table.js +0 -124
  61. package/dist/src/arg.d.ts +0 -58
  62. package/dist/src/arg.js +0 -214
  63. package/dist/src/atom.d.ts +0 -12
  64. package/dist/src/atom.js +0 -27
  65. package/dist/src/attribute.d.ts +0 -71
  66. package/dist/src/attribute.js +0 -502
  67. package/dist/src/attributes.d.ts +0 -109
  68. package/dist/src/attributes.js +0 -356
  69. package/dist/src/converter.d.ts +0 -33
  70. package/dist/src/converter.js +0 -134
  71. package/dist/src/converterFlags.d.ts +0 -87
  72. package/dist/src/converterFlags.js +0 -242
  73. package/dist/src/converterRule.d.ts +0 -77
  74. package/dist/src/converterRule.js +0 -209
  75. package/dist/src/extLink.d.ts +0 -43
  76. package/dist/src/extLink.js +0 -186
  77. package/dist/src/gallery.d.ts +0 -54
  78. package/dist/src/gallery.js +0 -139
  79. package/dist/src/heading.d.ts +0 -47
  80. package/dist/src/heading.js +0 -190
  81. package/dist/src/hidden.d.ts +0 -7
  82. package/dist/src/hidden.js +0 -23
  83. package/dist/src/html.d.ts +0 -65
  84. package/dist/src/html.js +0 -358
  85. package/dist/src/imageParameter.d.ts +0 -65
  86. package/dist/src/imageParameter.js +0 -257
  87. package/dist/src/imagemap.d.ts +0 -56
  88. package/dist/src/imagemap.js +0 -150
  89. package/dist/src/imagemapLink.d.ts +0 -31
  90. package/dist/src/imagemapLink.js +0 -93
  91. package/dist/src/index.d.ts +0 -143
  92. package/dist/src/index.js +0 -813
  93. package/dist/src/link/base.d.ts +0 -52
  94. package/dist/src/link/base.js +0 -229
  95. package/dist/src/link/category.d.ts +0 -13
  96. package/dist/src/link/category.js +0 -28
  97. package/dist/src/link/file.d.ts +0 -98
  98. package/dist/src/link/file.js +0 -272
  99. package/dist/src/link/galleryImage.d.ts +0 -32
  100. package/dist/src/link/galleryImage.js +0 -157
  101. package/dist/src/link/index.d.ts +0 -56
  102. package/dist/src/link/index.js +0 -133
  103. package/dist/src/magicLink.d.ts +0 -59
  104. package/dist/src/magicLink.js +0 -223
  105. package/dist/src/nested.d.ts +0 -43
  106. package/dist/src/nested.js +0 -105
  107. package/dist/src/nowiki/base.d.ts +0 -31
  108. package/dist/src/nowiki/base.js +0 -93
  109. package/dist/src/nowiki/comment.d.ts +0 -25
  110. package/dist/src/nowiki/comment.js +0 -76
  111. package/dist/src/nowiki/dd.d.ts +0 -8
  112. package/dist/src/nowiki/dd.js +0 -24
  113. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -18
  114. package/dist/src/nowiki/doubleUnderscore.js +0 -101
  115. package/dist/src/nowiki/hr.d.ts +0 -5
  116. package/dist/src/nowiki/hr.js +0 -63
  117. package/dist/src/nowiki/index.d.ts +0 -16
  118. package/dist/src/nowiki/index.js +0 -27
  119. package/dist/src/nowiki/list.d.ts +0 -16
  120. package/dist/src/nowiki/list.js +0 -97
  121. package/dist/src/nowiki/listBase.d.ts +0 -5
  122. package/dist/src/nowiki/listBase.js +0 -61
  123. package/dist/src/nowiki/noinclude.d.ts +0 -10
  124. package/dist/src/nowiki/noinclude.js +0 -23
  125. package/dist/src/nowiki/quote.d.ts +0 -14
  126. package/dist/src/nowiki/quote.js +0 -131
  127. package/dist/src/onlyinclude.d.ts +0 -16
  128. package/dist/src/onlyinclude.js +0 -57
  129. package/dist/src/paramTag/index.d.ts +0 -37
  130. package/dist/src/paramTag/index.js +0 -77
  131. package/dist/src/paramTag/inputbox.d.ts +0 -8
  132. package/dist/src/paramTag/inputbox.js +0 -22
  133. package/dist/src/parameter.d.ts +0 -70
  134. package/dist/src/parameter.js +0 -264
  135. package/dist/src/pre.d.ts +0 -28
  136. package/dist/src/pre.js +0 -55
  137. package/dist/src/syntax.d.ts +0 -20
  138. package/dist/src/syntax.js +0 -86
  139. package/dist/src/table/base.d.ts +0 -30
  140. package/dist/src/table/base.js +0 -132
  141. package/dist/src/table/index.d.ts +0 -233
  142. package/dist/src/table/index.js +0 -396
  143. package/dist/src/table/td.d.ts +0 -86
  144. package/dist/src/table/td.js +0 -350
  145. package/dist/src/table/tr.d.ts +0 -32
  146. package/dist/src/table/tr.js +0 -58
  147. package/dist/src/table/trBase.d.ts +0 -53
  148. package/dist/src/table/trBase.js +0 -152
  149. package/dist/src/tagPair/ext.d.ts +0 -32
  150. package/dist/src/tagPair/ext.js +0 -203
  151. package/dist/src/tagPair/include.d.ts +0 -35
  152. package/dist/src/tagPair/include.js +0 -74
  153. package/dist/src/tagPair/index.d.ts +0 -27
  154. package/dist/src/tagPair/index.js +0 -128
  155. package/dist/src/transclude.d.ts +0 -167
  156. package/dist/src/transclude.js +0 -717
  157. package/dist/util/constants.js +0 -28
  158. package/dist/util/debug.js +0 -88
  159. package/dist/util/diff.js +0 -83
  160. package/dist/util/lint.js +0 -31
  161. package/dist/util/string.js +0 -54
  162. package/errors/README +0 -1
  163. package/printed/README +0 -1
@@ -1,717 +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
- 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();
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 = false, 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 && !Number.isNaN(Number(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}">{{${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 = false) {
451
- return this.getArgs(key, exact, false).size > 0;
452
- }
453
- /**
454
- * 获取生效的指定参数
455
- * @param key 参数名
456
- * @param exact 是否匹配匿名性
457
- */
458
- getArg(key, exact = false) {
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 = false) {
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
- /**
492
- * 获取生效的参数值
493
- * @param key 参数名
494
- */
495
- getValue(key) {
496
- return key === undefined
497
- ? Object.fromEntries(this.getKeys().map(k => [k, this.getValue(k)]))
498
- : this.getArg(key)?.getValue();
499
- }
500
- /**
501
- * 插入匿名参数
502
- * @param val 参数值
503
- */
504
- newAnonArg(val) {
505
- const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(val, this.getAttribute('include'), undefined, config),
506
- // @ts-expect-error abstract class
507
- token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
508
- token.lastChild.append(...childNodes);
509
- token.afterBuild();
510
- return this.insertAt(token);
511
- }
512
- /**
513
- * 设置参数值
514
- * @param key 参数名
515
- * @param value 参数值
516
- * @throws `Error` 仅用于模板
517
- */
518
- setValue(key, value) {
519
- if (!this.isTemplate()) {
520
- throw new Error('setValue 方法仅供模板使用!');
521
- }
522
- const arg = this.getArg(key);
523
- if (arg) {
524
- arg.setValue(value);
525
- return;
526
- }
527
- 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),
528
- // @ts-expect-error abstract class
529
- token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
530
- token.firstChild.append(...k.childNodes);
531
- token.lastChild.append(...v.childNodes);
532
- token.afterBuild();
533
- this.insertAt(token);
534
- }
535
- /**
536
- * 将匿名参数改写为命名参数
537
- * @throws `Error` 仅用于模板
538
- */
539
- anonToNamed() {
540
- if (!this.isTemplate()) {
541
- throw new Error('anonToNamed 方法仅供模板使用!');
542
- }
543
- for (const token of this.getAnonArgs()) {
544
- token.firstChild.replaceChildren(token.name);
545
- }
546
- }
547
- /**
548
- * 替换模板名
549
- * @param title 模板名
550
- * @throws `Error` 仅用于模板
551
- */
552
- replaceTemplate(title) {
553
- if (this.type === 'magic-word') {
554
- throw new Error('replaceTemplate 方法仅用于更换模板!');
555
- }
556
- const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, this.getAttribute('config'));
557
- this.firstChild.replaceChildren(...childNodes);
558
- }
559
- /**
560
- * 替换模块名
561
- * @param title 模块名
562
- * @throws `Error` 仅用于模块
563
- */
564
- replaceModule(title) {
565
- if (this.type !== 'magic-word' || this.name !== 'invoke') {
566
- throw new Error('replaceModule 方法仅用于更换模块!');
567
- }
568
- const config = this.getAttribute('config');
569
- if (this.length === 1) {
570
- super.insertAt(new atom_1.AtomToken(undefined, 'invoke-module', config, [], {
571
- 'Stage-1': ':', '!ExtToken': '',
572
- }));
573
- return;
574
- }
575
- const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, config);
576
- this.childNodes[1].replaceChildren(...childNodes);
577
- }
578
- /**
579
- * 替换模块函数
580
- * @param func 模块函数名
581
- * @throws `Error` 仅用于模块
582
- * @throws `Error` 尚未指定模块名称
583
- */
584
- replaceFunction(func) {
585
- if (this.type !== 'magic-word' || this.name !== 'invoke') {
586
- throw new Error('replaceModule 方法仅用于更换模块!');
587
- }
588
- else if (this.length < 2) {
589
- throw new Error('尚未指定模块名称!');
590
- }
591
- const config = this.getAttribute('config');
592
- if (this.length === 2) {
593
- super.insertAt(new atom_1.AtomToken(undefined, 'invoke-function', config, [], {
594
- 'Stage-1': ':', '!ExtToken': '',
595
- }));
596
- return;
597
- }
598
- const { childNodes } = index_1.default.parse(func, this.getAttribute('include'), 2, config);
599
- this.childNodes[2].replaceChildren(...childNodes);
600
- }
601
- /**
602
- * 重复参数计数
603
- * @throws `Error` 仅用于模板
604
- */
605
- hasDuplicatedArgs() {
606
- if (this.isTemplate()) {
607
- return this.getAllArgs().length - this.getKeys().length;
608
- }
609
- throw new Error('hasDuplicatedArgs 方法仅供模板使用!');
610
- }
611
- /**
612
- * 修复重名参数:
613
- * `aggressive = false`时只移除空参数和全同参数,优先保留匿名参数,否则将所有匿名参数更改为命名。
614
- * `aggressive = true`时还会尝试处理连续的以数字编号的参数。
615
- * @param aggressive 是否使用有更大风险的修复手段
616
- */
617
- fixDuplication(aggressive = false) {
618
- if (!this.hasDuplicatedArgs()) {
619
- return [];
620
- }
621
- const duplicatedKeys = [];
622
- let anonCount = this.getAnonArgs().length;
623
- for (const [key, args] of this.getDuplicatedArgs()) {
624
- if (args.length <= 1) {
625
- continue;
626
- }
627
- const values = new Map();
628
- for (const arg of args) {
629
- const val = arg.getValue().trim();
630
- if (values.has(val)) {
631
- values.get(val).push(arg);
632
- }
633
- else {
634
- values.set(val, [arg]);
635
- }
636
- }
637
- let noMoreAnon = anonCount === 0 || Number.isNaN(Number(key));
638
- const emptyArgs = values.get('') ?? [], duplicatedArgs = [...values].filter(([val, { length }]) => val && length > 1).flatMap(([, curArgs]) => {
639
- const anonIndex = noMoreAnon ? -1 : curArgs.findIndex(({ anon }) => anon);
640
- if (anonIndex !== -1) {
641
- noMoreAnon = true;
642
- }
643
- curArgs.splice(anonIndex, 1);
644
- return curArgs;
645
- }), badArgs = [...emptyArgs, ...duplicatedArgs], index = noMoreAnon ? -1 : emptyArgs.findIndex(({ anon }) => anon);
646
- if (badArgs.length === args.length) {
647
- badArgs.splice(index, 1);
648
- }
649
- else if (index !== -1) {
650
- this.anonToNamed();
651
- anonCount = 0;
652
- }
653
- for (const arg of badArgs) {
654
- arg.remove();
655
- }
656
- let remaining = args.length - badArgs.length;
657
- if (remaining === 1) {
658
- continue;
659
- }
660
- else if (aggressive && (anonCount ? /\D\d+$/u : /(?:^|\D)\d+$/u).test(key)) {
661
- let last;
662
- // eslint-disable-next-line es-x/no-regexp-lookbehind-assertions
663
- 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) => {
664
- const j = Number(name.slice(str.length)), cmp = j <= i + 1 && (i === 0 || j >= last || name === key);
665
- last = j;
666
- return cmp;
667
- });
668
- if (ordered) {
669
- for (let i = 0; i < series.length; i++) {
670
- const name = `${str}${i + 1}`, arg = series[i];
671
- if (arg.name !== name) {
672
- if (arg.name === key) {
673
- remaining--;
674
- }
675
- arg.rename(name, true);
676
- }
677
- }
678
- }
679
- }
680
- if (remaining > 1) {
681
- index_1.default.error(`${this.type === 'template'
682
- ? this.name
683
- : this.normalizeTitle(this.childNodes[1].text(), 828)
684
- .title} 还留有 ${remaining} 个重复的 ${key} 参数:${[...this.getArgs(key)].map(arg => {
685
- const { top, left } = arg.getBoundingClientRect();
686
- return `第 ${String(top)} 行第 ${String(left)} 列`;
687
- }).join('、')}`);
688
- duplicatedKeys.push(key);
689
- continue;
690
- }
691
- }
692
- return duplicatedKeys;
693
- }
694
- /**
695
- * 转义模板内的表格
696
- * @throws `Error` 转义失败
697
- */
698
- escapeTables() {
699
- if (!/\n[^\S\n]*(?::+[^\S\n]*)?\{\|/u.test(this.text())) {
700
- return this;
701
- }
702
- 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');
703
- for (const table of parsed.childNodes) {
704
- if (isTable(table)) {
705
- table.escape();
706
- }
707
- }
708
- const { firstChild, length } = index_1.default.parse(`{{${String(parsed)}}}`, include, undefined, config);
709
- if (length !== 1 || !(firstChild instanceof TranscludeToken)) {
710
- throw new Error('转义表格失败!');
711
- }
712
- this.safeReplaceWith(firstChild);
713
- return firstChild;
714
- }
715
- }
716
- exports.TranscludeToken = TranscludeToken;
717
- constants_1.classes['TranscludeToken'] = __filename;