wikiparser-node 0.11.0 → 1.0.0-beta.0

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 (251) hide show
  1. package/dist/index.d.ts +59 -113
  2. package/dist/index.js +307 -0
  3. package/dist/lib/element.d.ts +122 -108
  4. package/dist/lib/element.js +656 -0
  5. package/dist/lib/node.d.ts +115 -221
  6. package/dist/lib/node.js +473 -0
  7. package/dist/lib/ranges.d.ts +27 -26
  8. package/dist/lib/ranges.js +130 -0
  9. package/dist/lib/text.d.ts +36 -28
  10. package/dist/lib/text.js +215 -0
  11. package/dist/lib/title.d.ts +25 -12
  12. package/dist/lib/title.js +100 -0
  13. package/dist/mixin/attributesParent.js +90 -0
  14. package/dist/mixin/fixed.js +29 -0
  15. package/dist/mixin/hidden.js +19 -0
  16. package/dist/mixin/singleLine.js +23 -0
  17. package/dist/mixin/sol.js +41 -0
  18. package/dist/parser/brackets.js +117 -0
  19. package/dist/parser/commentAndExt.js +48 -0
  20. package/dist/parser/converter.js +31 -0
  21. package/dist/parser/externalLinks.js +22 -0
  22. package/dist/parser/hrAndDoubleUnderscore.js +33 -0
  23. package/dist/parser/html.js +29 -0
  24. package/dist/parser/links.js +86 -0
  25. package/dist/parser/list.js +51 -0
  26. package/dist/parser/magicLinks.js +32 -0
  27. package/dist/parser/quotes.js +57 -0
  28. package/dist/parser/selector.js +158 -0
  29. package/dist/parser/table.js +108 -0
  30. package/dist/src/arg.d.ts +47 -23
  31. package/dist/src/arg.js +196 -0
  32. package/dist/src/atom.d.ts +12 -0
  33. package/dist/src/atom.js +22 -0
  34. package/dist/src/attribute.d.ts +74 -33
  35. package/dist/src/attribute.js +433 -0
  36. package/dist/src/attributes.d.ts +61 -55
  37. package/dist/src/attributes.js +371 -0
  38. package/dist/src/converter.d.ts +45 -71
  39. package/dist/src/converter.js +135 -0
  40. package/dist/src/converterFlags.d.ts +64 -39
  41. package/dist/src/converterFlags.js +235 -0
  42. package/dist/src/converterRule.d.ts +49 -27
  43. package/dist/src/converterRule.js +255 -0
  44. package/dist/src/extLink.d.ts +41 -38
  45. package/dist/src/extLink.js +154 -0
  46. package/dist/src/gallery.d.ts +59 -18
  47. package/dist/src/gallery.js +132 -0
  48. package/dist/src/heading.d.ts +60 -22
  49. package/dist/src/heading.js +135 -0
  50. package/dist/src/hidden.d.ts +20 -0
  51. package/dist/src/hidden.js +24 -0
  52. package/dist/src/html.d.ts +83 -29
  53. package/dist/src/html.js +242 -0
  54. package/dist/src/imageParameter.d.ts +59 -40
  55. package/dist/src/imageParameter.js +251 -0
  56. package/dist/src/imagemap.d.ts +65 -21
  57. package/dist/src/imagemap.js +164 -0
  58. package/dist/src/imagemapLink.d.ts +46 -14
  59. package/dist/src/imagemapLink.js +38 -0
  60. package/dist/src/index.d.ts +71 -105
  61. package/dist/src/index.js +826 -0
  62. package/dist/src/link/base.d.ts +71 -0
  63. package/dist/src/link/base.js +225 -0
  64. package/dist/src/link/category.d.ts +10 -11
  65. package/dist/src/link/category.js +26 -0
  66. package/dist/src/link/file.d.ts +61 -39
  67. package/dist/src/link/file.js +242 -0
  68. package/dist/src/link/galleryImage.d.ts +34 -12
  69. package/dist/src/link/galleryImage.js +98 -0
  70. package/dist/src/link/index.d.ts +25 -63
  71. package/dist/src/link/index.js +136 -0
  72. package/dist/src/magicLink.d.ts +22 -15
  73. package/dist/src/magicLink.js +126 -0
  74. package/dist/src/nested.d.ts +47 -0
  75. package/dist/src/nested.js +84 -0
  76. package/dist/src/nowiki/base.d.ts +39 -0
  77. package/dist/src/nowiki/base.js +29 -0
  78. package/dist/src/nowiki/comment.d.ts +31 -20
  79. package/dist/src/nowiki/comment.js +61 -0
  80. package/dist/src/nowiki/dd.d.ts +17 -11
  81. package/dist/src/nowiki/dd.js +50 -0
  82. package/dist/src/nowiki/doubleUnderscore.d.ts +28 -13
  83. package/dist/src/nowiki/doubleUnderscore.js +45 -0
  84. package/dist/src/nowiki/hr.d.ts +28 -10
  85. package/dist/src/nowiki/hr.js +33 -0
  86. package/dist/src/nowiki/index.d.ts +17 -23
  87. package/dist/src/nowiki/index.js +21 -0
  88. package/dist/src/nowiki/list.d.ts +15 -7
  89. package/dist/src/nowiki/list.js +11 -0
  90. package/dist/src/nowiki/noinclude.d.ts +20 -7
  91. package/dist/src/nowiki/noinclude.js +22 -0
  92. package/dist/src/nowiki/quote.d.ts +25 -10
  93. package/dist/src/nowiki/quote.js +55 -0
  94. package/dist/src/onlyinclude.d.ts +28 -12
  95. package/dist/src/onlyinclude.js +64 -0
  96. package/dist/src/paramTag/index.d.ts +40 -17
  97. package/dist/src/paramTag/index.js +76 -0
  98. package/dist/src/paramTag/inputbox.d.ts +8 -7
  99. package/dist/src/paramTag/inputbox.js +19 -0
  100. package/dist/src/parameter.d.ts +62 -41
  101. package/dist/src/parameter.js +201 -0
  102. package/dist/src/pre.d.ts +32 -0
  103. package/dist/src/pre.js +39 -0
  104. package/dist/src/syntax.d.ts +17 -14
  105. package/dist/src/syntax.js +65 -0
  106. package/dist/src/table/base.d.ts +55 -0
  107. package/dist/src/table/base.js +77 -0
  108. package/dist/src/table/index.d.ts +123 -156
  109. package/dist/src/table/index.js +811 -0
  110. package/dist/src/table/td.d.ts +90 -67
  111. package/dist/src/table/td.js +276 -0
  112. package/dist/src/table/tr.d.ts +30 -85
  113. package/dist/src/table/tr.js +48 -0
  114. package/dist/src/table/trBase.d.ts +72 -0
  115. package/dist/src/table/trBase.js +153 -0
  116. package/dist/src/tagPair/ext.d.ts +47 -11
  117. package/dist/src/tagPair/ext.js +127 -0
  118. package/dist/src/tagPair/include.d.ts +32 -13
  119. package/dist/src/tagPair/include.js +40 -0
  120. package/dist/src/tagPair/index.d.ts +44 -29
  121. package/dist/src/tagPair/index.js +111 -0
  122. package/dist/src/transclude.d.ts +88 -85
  123. package/dist/src/transclude.js +739 -0
  124. package/dist/util/base.js +26 -0
  125. package/dist/util/debug.js +52 -0
  126. package/dist/util/diff.js +69 -0
  127. package/dist/util/lint.js +38 -0
  128. package/dist/util/string.js +103 -0
  129. package/errors/README +1 -0
  130. package/i18n/zh-hans.json +1 -0
  131. package/i18n/zh-hant.json +1 -0
  132. package/package.json +11 -14
  133. package/printed/README +1 -0
  134. package/dist/mixin/attributeParent.d.ts +0 -9
  135. package/dist/mixin/fixedToken.d.ts +0 -8
  136. package/dist/mixin/hidden.d.ts +0 -8
  137. package/dist/mixin/singleLine.d.ts +0 -8
  138. package/dist/mixin/sol.d.ts +0 -8
  139. package/dist/parser/brackets.d.ts +0 -12
  140. package/dist/parser/commentAndExt.d.ts +0 -8
  141. package/dist/parser/converter.d.ts +0 -7
  142. package/dist/parser/externalLinks.d.ts +0 -7
  143. package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
  144. package/dist/parser/html.d.ts +0 -7
  145. package/dist/parser/links.d.ts +0 -7
  146. package/dist/parser/list.d.ts +0 -7
  147. package/dist/parser/magicLinks.d.ts +0 -7
  148. package/dist/parser/quotes.d.ts +0 -7
  149. package/dist/parser/selector.d.ts +0 -12
  150. package/dist/parser/table.d.ts +0 -11
  151. package/dist/src/atom/hidden.d.ts +0 -5
  152. package/dist/src/atom/index.d.ts +0 -15
  153. package/dist/src/charinsert.d.ts +0 -32
  154. package/dist/src/hasNowiki/index.d.ts +0 -14
  155. package/dist/src/hasNowiki/pre.d.ts +0 -13
  156. package/dist/src/nested/choose.d.ts +0 -13
  157. package/dist/src/nested/combobox.d.ts +0 -13
  158. package/dist/src/nested/index.d.ts +0 -18
  159. package/dist/src/nested/references.d.ts +0 -13
  160. package/dist/tool/index.d.ts +0 -420
  161. package/dist/util/base.d.ts +0 -10
  162. package/dist/util/debug.d.ts +0 -20
  163. package/dist/util/diff.d.ts +0 -8
  164. package/dist/util/lint.d.ts +0 -28
  165. package/dist/util/string.d.ts +0 -55
  166. package/index.js +0 -333
  167. package/lib/element.js +0 -618
  168. package/lib/node.js +0 -730
  169. package/lib/ranges.js +0 -130
  170. package/lib/text.js +0 -265
  171. package/lib/title.js +0 -83
  172. package/mixin/attributeParent.js +0 -117
  173. package/mixin/fixedToken.js +0 -40
  174. package/mixin/hidden.js +0 -21
  175. package/mixin/singleLine.js +0 -31
  176. package/mixin/sol.js +0 -54
  177. package/parser/brackets.js +0 -128
  178. package/parser/commentAndExt.js +0 -62
  179. package/parser/converter.js +0 -46
  180. package/parser/externalLinks.js +0 -33
  181. package/parser/hrAndDoubleUnderscore.js +0 -49
  182. package/parser/html.js +0 -42
  183. package/parser/links.js +0 -94
  184. package/parser/list.js +0 -59
  185. package/parser/magicLinks.js +0 -41
  186. package/parser/quotes.js +0 -64
  187. package/parser/selector.js +0 -180
  188. package/parser/table.js +0 -114
  189. package/src/arg.js +0 -207
  190. package/src/atom/hidden.js +0 -13
  191. package/src/atom/index.js +0 -43
  192. package/src/attribute.js +0 -472
  193. package/src/attributes.js +0 -453
  194. package/src/charinsert.js +0 -97
  195. package/src/converter.js +0 -176
  196. package/src/converterFlags.js +0 -284
  197. package/src/converterRule.js +0 -256
  198. package/src/extLink.js +0 -180
  199. package/src/gallery.js +0 -149
  200. package/src/hasNowiki/index.js +0 -44
  201. package/src/hasNowiki/pre.js +0 -40
  202. package/src/heading.js +0 -134
  203. package/src/html.js +0 -254
  204. package/src/imageParameter.js +0 -303
  205. package/src/imagemap.js +0 -199
  206. package/src/imagemapLink.js +0 -41
  207. package/src/index.js +0 -938
  208. package/src/link/category.js +0 -44
  209. package/src/link/file.js +0 -287
  210. package/src/link/galleryImage.js +0 -120
  211. package/src/link/index.js +0 -388
  212. package/src/magicLink.js +0 -151
  213. package/src/nested/choose.js +0 -24
  214. package/src/nested/combobox.js +0 -23
  215. package/src/nested/index.js +0 -96
  216. package/src/nested/references.js +0 -23
  217. package/src/nowiki/comment.js +0 -71
  218. package/src/nowiki/dd.js +0 -59
  219. package/src/nowiki/doubleUnderscore.js +0 -56
  220. package/src/nowiki/hr.js +0 -41
  221. package/src/nowiki/index.js +0 -56
  222. package/src/nowiki/list.js +0 -16
  223. package/src/nowiki/noinclude.js +0 -28
  224. package/src/nowiki/quote.js +0 -69
  225. package/src/onlyinclude.js +0 -64
  226. package/src/paramTag/index.js +0 -89
  227. package/src/paramTag/inputbox.js +0 -35
  228. package/src/parameter.js +0 -239
  229. package/src/syntax.js +0 -91
  230. package/src/table/index.js +0 -985
  231. package/src/table/td.js +0 -343
  232. package/src/table/tr.js +0 -319
  233. package/src/tagPair/ext.js +0 -146
  234. package/src/tagPair/include.js +0 -50
  235. package/src/tagPair/index.js +0 -131
  236. package/src/transclude.js +0 -843
  237. package/tool/index.js +0 -1209
  238. package/typings/api.d.ts +0 -9
  239. package/typings/array.d.ts +0 -29
  240. package/typings/event.d.ts +0 -22
  241. package/typings/index.d.ts +0 -118
  242. package/typings/node.d.ts +0 -35
  243. package/typings/parser.d.ts +0 -12
  244. package/typings/table.d.ts +0 -10
  245. package/typings/token.d.ts +0 -31
  246. package/typings/tool.d.ts +0 -6
  247. package/util/base.js +0 -17
  248. package/util/debug.js +0 -73
  249. package/util/diff.js +0 -76
  250. package/util/lint.js +0 -57
  251. package/util/string.js +0 -126
package/src/table/td.js DELETED
@@ -1,343 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * @template T
5
- * @typedef {import('../../typings/node').TokenAttribute<T>} TokenAttribute
6
- */
7
-
8
- const {generateForChild} = require('../../util/lint'),
9
- fixedToken = require('../../mixin/fixedToken'),
10
- {typeError} = require('../../util/debug'),
11
- {isPlainObject} = require('../../util/base'),
12
- Parser = require('../..'),
13
- Token = require('..'),
14
- TrToken = require('./tr');
15
-
16
- const aliases = {td: '\n|', th: '\n!', caption: '\n|+'},
17
- openingPattern = /^(?:\n[^\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u;
18
-
19
- /**
20
- * `<td>`、`<th>`和`<caption>`
21
- * @classdesc `{childNodes: [SyntaxToken, AttributesToken, Token]}`
22
- */
23
- class TdToken extends fixedToken(TrToken) {
24
- type = 'td';
25
- #innerSyntax = '';
26
-
27
- /**
28
- * 单元格类型
29
- * @complexity `n`
30
- */
31
- get subtype() {
32
- return this.getSyntax().subtype;
33
- }
34
-
35
- set subtype(subtype) {
36
- this.setSyntax(subtype);
37
- }
38
-
39
- /** rowspan */
40
- get rowspan() {
41
- return this.getAttr('rowspan');
42
- }
43
-
44
- set rowspan(rowspan) {
45
- this.setAttr('rowspan', rowspan);
46
- }
47
-
48
- /** colspan */
49
- get colspan() {
50
- return this.getAttr('colspan');
51
- }
52
-
53
- set colspan(colspan) {
54
- this.setAttr('colspan', colspan);
55
- }
56
-
57
- /** 内部wikitext */
58
- get innerText() {
59
- return this.lastChild.text();
60
- }
61
-
62
- /** 是否位于行首 */
63
- isIndependent() {
64
- return this.firstChild.text()[0] === '\n';
65
- }
66
-
67
- /**
68
- * 获取单元格语法信息
69
- * @returns {{subtype: 'td'|'th'|'caption', escape: boolean, correction: boolean}}
70
- * @complexity `n`
71
- */
72
- getSyntax() {
73
- const syntax = this.firstChild.text(),
74
- esc = syntax.includes('{{'),
75
- char = syntax.at(-1);
76
- let subtype = 'td';
77
- if (char === '!') {
78
- subtype = 'th';
79
- } else if (char === '+') {
80
- subtype = 'caption';
81
- }
82
- if (this.isIndependent()) {
83
- return {subtype, escape: esc, correction: false};
84
- }
85
- const {previousSibling} = this;
86
- if (previousSibling?.type !== 'td') {
87
- return {subtype, escape: esc, correction: true};
88
- }
89
- const result = previousSibling.getSyntax();
90
- result.escape ||= esc;
91
- result.correction = previousSibling.lastChild
92
- .toString('comment, ext, include, noinclude, arg, template, magic-word')
93
- .includes('\n');
94
- if (subtype === 'th' && result.subtype !== 'th') {
95
- result.subtype = 'th';
96
- result.correction = true;
97
- }
98
- return result;
99
- }
100
-
101
- /**
102
- * @param {string} syntax 单元格语法
103
- * @param {string} inner 内部wikitext
104
- * @param {import('../../typings/token').accum} accum
105
- */
106
- constructor(syntax, inner, config = Parser.getConfig(), accum = []) {
107
- let innerSyntax = inner?.match(/\||\0\d+!\x7F/u),
108
- attr = innerSyntax ? inner.slice(0, innerSyntax.index) : '';
109
- if (/\[\[|-\{/u.test(attr)) {
110
- innerSyntax = undefined;
111
- attr = '';
112
- }
113
- super(syntax, attr, config, accum, openingPattern);
114
- if (innerSyntax) {
115
- [this.#innerSyntax] = innerSyntax;
116
- }
117
- // eslint-disable-next-line no-unsafe-optional-chaining
118
- const innerToken = new Token(inner?.slice(innerSyntax?.index + this.#innerSyntax.length), config, true, accum);
119
- innerToken.type = 'td-inner';
120
- this.insertAt(innerToken.setAttribute('stage', 4));
121
- this.setAttribute('acceptable', {SyntaxToken: 0, AttributesToken: 1, Token: 2})
122
- .seal(['getRowCount', 'getNthCol', 'insertTableCell'], true);
123
- }
124
-
125
- /** @override */
126
- afterBuild() {
127
- if (this.#innerSyntax.includes('\0')) {
128
- this.#innerSyntax = this.getAttribute('buildFromStr')(this.#innerSyntax, 'string');
129
- }
130
- }
131
-
132
- /**
133
- * @override
134
- * @param {string} selector
135
- * @returns {string}
136
- * @complexity `n`
137
- */
138
- toString(selector) {
139
- this.#correct();
140
- const {childNodes: [syntax, attr, inner]} = this;
141
- return selector && this.matches(selector)
142
- ? ''
143
- : `${syntax.toString(selector)}${attr.toString(selector)}${this.#innerSyntax}${inner.toString(selector)}`;
144
- }
145
-
146
- /**
147
- * @override
148
- * @returns {string}
149
- * @complexity `n`
150
- */
151
- text() {
152
- this.#correct();
153
- const {childNodes: [syntax, attr, inner]} = this;
154
- return `${syntax.text()}${attr.text()}${this.#innerSyntax}${inner.text()}`;
155
- }
156
-
157
- /**
158
- * @override
159
- * @param {number} i 子节点位置
160
- */
161
- getGaps(i = 0) {
162
- i = i < 0 ? i + this.length : i;
163
- if (i === 1) {
164
- this.#correct();
165
- return this.#innerSyntax.length;
166
- }
167
- return 0;
168
- }
169
-
170
- /**
171
- * @override
172
- * @param {number} start 起始位置
173
- */
174
- lint(start = this.getAbsoluteIndex()) {
175
- const errors = super.lint(start);
176
- start += this.getRelativeIndex(-1);
177
- for (const child of this.lastChild.childNodes) {
178
- if (child.type === 'text' && child.data.includes('|')) {
179
- errors.push(generateForChild(child, {start}, 'additional "|" in a table cell', 'warning'));
180
- }
181
- }
182
- return errors;
183
- }
184
-
185
- /** @override */
186
- print() {
187
- const {childNodes: [syntax, attr, inner]} = this;
188
- return `<span class="wpb-td">${syntax.print()}${attr.print()}${this.#innerSyntax}${inner.print()}</span>`;
189
- }
190
-
191
- /** @override */
192
- cloneNode() {
193
- const /** @type {TdToken} */ token = super.cloneNode();
194
- token.setAttribute('innerSyntax', this.#innerSyntax);
195
- return token;
196
- }
197
-
198
- /**
199
- * 创建新的单元格
200
- * @param {string|Token} inner 内部wikitext
201
- * @param {'td'|'th'|'caption'} subtype 单元格类型
202
- * @param {Record<string, string>} attr 单元格属性
203
- * @param {boolean} include 是否嵌入
204
- * @throws `RangeError` 非法的单元格类型
205
- */
206
- static create(inner, subtype = 'td', attr = {}, include = false, config = Parser.getConfig()) {
207
- if (typeof inner !== 'string' && inner?.constructor !== Token || !isPlainObject(attr)) {
208
- typeError(this, 'create', 'String', 'Token', 'Object');
209
- } else if (subtype !== 'td' && subtype !== 'th' && subtype !== 'caption') {
210
- throw new RangeError('单元格的子类型只能为 "td"、"th" 或 "caption"!');
211
- } else if (typeof inner === 'string') {
212
- inner = Parser.parse(inner, include, undefined, config);
213
- }
214
- const token = Parser.run(() => new TdToken('\n|', undefined, config));
215
- token.setSyntax(subtype);
216
- token.lastChild.safeReplaceWith(inner);
217
- for (const [k, v] of Object.entries(attr)) {
218
- token.setAttr(k, v);
219
- }
220
- return token;
221
- }
222
-
223
- /**
224
- * @override
225
- * @template {string} T
226
- * @param {T} key 属性键
227
- * @returns {TokenAttribute<T>}
228
- */
229
- getAttribute(key) {
230
- return key === 'innerSyntax' ? this.#innerSyntax : super.getAttribute(key);
231
- }
232
-
233
- /**
234
- * @override
235
- * @template {string} T
236
- * @param {T} key 属性键
237
- * @param {TokenAttribute<T>} value 属性值
238
- * @returns {this}
239
- */
240
- setAttribute(key, value) {
241
- if (key === 'innerSyntax') {
242
- this.#innerSyntax = String(value);
243
- return this;
244
- }
245
- return super.setAttribute(key, value);
246
- }
247
-
248
- /**
249
- * @override
250
- * @param {string} syntax 表格语法
251
- * @param {boolean} esc 是否需要转义
252
- */
253
- setSyntax(syntax, esc) {
254
- super.setSyntax(aliases[syntax] ?? syntax, esc);
255
- }
256
-
257
- /**
258
- * 修复\<td\>语法
259
- * @complexity `n`
260
- */
261
- #correct() {
262
- if (String(this.childNodes[1])) {
263
- this.#innerSyntax ||= '|';
264
- }
265
- const {subtype, escape, correction} = this.getSyntax();
266
- if (correction) {
267
- this.setSyntax(subtype, escape);
268
- }
269
- }
270
-
271
- /**
272
- * 改为独占一行
273
- * @complexity `n`
274
- */
275
- independence() {
276
- if (!this.isIndependent()) {
277
- const {subtype, escape} = this.getSyntax();
278
- this.setSyntax(subtype, escape);
279
- }
280
- }
281
-
282
- /**
283
- * 获取单元格属性
284
- * @template {string} T
285
- * @param {T} key 属性键
286
- * @returns {T extends 'rowspan'|'colspan' ? number : string|true}
287
- */
288
- getAttr(key) {
289
- const /** @type {string|true} */ value = super.getAttr(key);
290
- key = key?.toLowerCase()?.trim();
291
- return key === 'rowspan' || key === 'colspan' ? Number(value) || 1 : value;
292
- }
293
-
294
- /**
295
- * 获取全部单元格属性
296
- * @returns {{rowspan: number, colspan: number, [key: string]: string|true}}
297
- */
298
- getAttrs() {
299
- const /** @type {record<string, string|true>} */ attr = super.getAttrs();
300
- if ('rowspan' in attr) {
301
- attr.rowspan = Number(attr.rowspan);
302
- }
303
- if ('colspan' in attr) {
304
- attr.colspan = Number(attr.colspan);
305
- }
306
- return attr;
307
- }
308
-
309
- /**
310
- * 设置单元格属性
311
- * @template {string} T
312
- * @param {T} key 属性键
313
- * @param {T extends 'rowspan'|'colspan' ? number : string|boolean} value 属性值
314
- */
315
- setAttr(key, value) {
316
- if (typeof key !== 'string') {
317
- this.typeError('setAttr', 'String');
318
- }
319
- key = key.toLowerCase().trim();
320
- if (typeof value === 'number' && (key === 'rowspan' || key === 'colspan')) {
321
- value = value === 1 ? false : String(value);
322
- }
323
- const /** @type {boolean} */ result = super.setAttr(key, value);
324
- if (!String(this.childNodes[1])) {
325
- this.#innerSyntax = '';
326
- }
327
- return result;
328
- }
329
-
330
- /** @override */
331
- escape() {
332
- super.escape();
333
- if (String(this.childNodes[1])) {
334
- this.#innerSyntax ||= '{{!}}';
335
- }
336
- if (this.#innerSyntax === '|') {
337
- this.#innerSyntax = '{{!}}';
338
- }
339
- }
340
- }
341
-
342
- Parser.classes.TdToken = __filename;
343
- module.exports = TdToken;
package/src/table/tr.js DELETED
@@ -1,319 +0,0 @@
1
- 'use strict';
2
-
3
- const {generateForChild} = require('../../util/lint'),
4
- attributeParent = require('../../mixin/attributeParent'),
5
- Parser = require('../..'),
6
- AstText = require('../../lib/text'),
7
- Token = require('..'),
8
- SyntaxToken = require('../syntax'),
9
- AttributesToken = require('../attributes');
10
-
11
- const openingPattern = /^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u;
12
-
13
- /**
14
- * 转义表格语法
15
- * @param {SyntaxToken} syntax 表格语法节点
16
- */
17
- const escapeTable = syntax => {
18
- const templates = {'{|': '(!', '|}': '!)', '||': '!!', '|': '!'},
19
- wikitext = syntax.childNodes.map(
20
- child => child.type === 'text'
21
- ? String(child).replace(/\{\||\|\}|\|{2}|\|/gu, p => `{{${templates[p]}}}`)
22
- : String(child),
23
- ).join(''),
24
- token = Parser.parse(wikitext, syntax.getAttribute('include'), 2, syntax.getAttribute('config'));
25
- syntax.replaceChildren(...token.childNodes);
26
- };
27
-
28
- /**
29
- * 表格行,含开头的换行,不含结尾的换行
30
- * @classdesc `{childNodes: [SyntaxToken, AttributesToken, ?Token, ...TdToken]}`
31
- */
32
- class TrToken extends attributeParent(Token, 1) {
33
- type = 'tr';
34
-
35
- /**
36
- * @param {string} syntax 表格语法
37
- * @param {string} attr 表格属性
38
- * @param {import('../../typings/token').accum} accum
39
- * @param {RegExp} pattern 表格语法正则
40
- */
41
- constructor(syntax, attr = '', config = Parser.getConfig(), accum = [], pattern = openingPattern) {
42
- super(undefined, config, true, accum, {
43
- Token: 2, SyntaxToken: 0, AttributesToken: 1, TdToken: '2:',
44
- });
45
- this.append(
46
- new SyntaxToken(syntax, pattern, 'table-syntax', config, accum, {
47
- 'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':',
48
- }),
49
- new AttributesToken(attr, 'table-attrs', this.type, config, accum),
50
- );
51
- this.getAttribute('protectChildren')(0, 1);
52
- }
53
-
54
- /**
55
- * @override
56
- * @param {number} start 起始位置
57
- */
58
- lint(start = this.getAbsoluteIndex()) {
59
- const TranscludeToken = require('../transclude'),
60
- ArgToken = require('../arg');
61
- const errors = super.lint(start),
62
- inter = this.childNodes.find(({type}) => type === 'table-inter');
63
- if (!inter) {
64
- return errors;
65
- }
66
- const /** @type {TranscludeToken & ArgToken} */ first = inter.childNodes.find(child => child.text().trim()),
67
- tdPattern = /^\s*(?:!|\{\{\s*![!-]?\s*\}\})/u;
68
- if (!first || tdPattern.test(String(first)) || first.type === 'arg' && tdPattern.test(first.default || '')) {
69
- return errors;
70
- } else if (first.type === 'magic-word') {
71
- try {
72
- const possibleValues = first.getPossibleValues();
73
- if (possibleValues.every(token => tdPattern.test(token.text()))) {
74
- return errors;
75
- }
76
- } catch {}
77
- }
78
- const error = generateForChild(inter, {start}, 'content to be moved out from the table');
79
- errors.push({
80
- ...error,
81
- startIndex: error.startIndex + 1,
82
- startLine: error.startLine + 1,
83
- startCol: 0,
84
- excerpt: error.excerpt.slice(1),
85
- });
86
- return errors;
87
- }
88
-
89
- /** @override */
90
- text() {
91
- this.#correct();
92
- const str = super.text();
93
- return this.type === 'tr' && !str.trim().includes('\n') ? '' : str;
94
- }
95
-
96
- /**
97
- * @override
98
- * @this {TrToken & {constructor: typeof TrToken}}
99
- */
100
- cloneNode() {
101
- const [syntax, attr, inner, ...cloned] = this.cloneChildNodes();
102
- return Parser.run(() => {
103
- const token = new this.constructor(undefined, undefined, this.getAttribute('config'));
104
- token.firstChild.safeReplaceWith(syntax);
105
- token.childNodes[1].safeReplaceWith(attr);
106
- if (token.type === 'td') { // TdToken
107
- token.childNodes[2].safeReplaceWith(inner);
108
- } else if (inner !== undefined) {
109
- token.insertAt(inner);
110
- }
111
- token.append(...cloned);
112
- return token;
113
- });
114
- }
115
-
116
- /** 修复简单的表格语法错误 */
117
- #correct() {
118
- const {childNodes: [,, child]} = this;
119
- if (child?.constructor === Token) {
120
- const /** @type {{firstChild: AstText}} */ {firstChild} = child;
121
- if (firstChild.type !== 'text') {
122
- child.prepend('\n');
123
- } else if (firstChild.data[0] !== '\n') {
124
- firstChild.insertData(0, '\n');
125
- }
126
- }
127
- }
128
-
129
- /**
130
- * @override
131
- * @param {string} selector
132
- */
133
- toString(selector) {
134
- this.#correct();
135
- return super.toString(selector);
136
- }
137
-
138
- /**
139
- * 转义表格语法
140
- * @complexity `n`
141
- */
142
- escape() {
143
- for (const child of this.childNodes) {
144
- if (child instanceof SyntaxToken) {
145
- escapeTable(child);
146
- } else if (child instanceof TrToken) {
147
- child.escape();
148
- }
149
- }
150
- }
151
-
152
- /**
153
- * 设置表格语法
154
- * @param {string} syntax 表格语法
155
- * @param {boolean} esc 是否需要转义
156
- */
157
- setSyntax(syntax, esc) {
158
- const {firstChild} = this;
159
- firstChild.replaceChildren(syntax);
160
- if (esc) {
161
- escapeTable(firstChild);
162
- }
163
- }
164
-
165
- /**
166
- * @override
167
- * @param {number} i 移除位置
168
- * @complexity `n`
169
- */
170
- removeAt(i) {
171
- const TdToken = require('./td');
172
- const child = this.childNodes.at(i);
173
- if (child instanceof TdToken && child.isIndependent()) {
174
- const {nextSibling} = child;
175
- if (nextSibling?.type === 'td') {
176
- nextSibling.independence();
177
- }
178
- }
179
- return super.removeAt(i);
180
- }
181
-
182
- /**
183
- * @override
184
- * @template {AstText|Token} T
185
- * @param {T} token 待插入的子节点
186
- * @param {number} i 插入位置
187
- * @returns {T}
188
- * @complexity `n`
189
- */
190
- insertAt(token, i = this.length) {
191
- if (!Parser.running && !(token instanceof TrToken)) {
192
- this.typeError('insertAt', 'TrToken');
193
- }
194
- const TdToken = require('./td');
195
- const child = this.childNodes.at(i);
196
- if (token instanceof TdToken && token.isIndependent() && child instanceof TdToken) {
197
- child.independence();
198
- }
199
- return super.insertAt(token, i);
200
- }
201
-
202
- /**
203
- * 获取行数
204
- * @returns {0|1}
205
- * @complexity `n`
206
- */
207
- getRowCount() {
208
- const TdToken = require('./td');
209
- return Number(this.childNodes.some(
210
- child => child instanceof TdToken && child.isIndependent() && !child.firstChild.text().endsWith('+'),
211
- ));
212
- }
213
-
214
- /**
215
- * 获取相邻行
216
- * @param {(childNodes: Token[], index: number) => Token[]} subset 筛选兄弟节点的方法
217
- * @complexity `n`
218
- */
219
- #getSiblingRow(subset) {
220
- const {parentNode} = this;
221
- if (!parentNode) {
222
- return undefined;
223
- }
224
- const {childNodes} = parentNode,
225
- index = childNodes.indexOf(this);
226
- for (const child of subset(childNodes, index)) {
227
- if (child instanceof TrToken && child.getRowCount()) {
228
- return child;
229
- }
230
- }
231
- return undefined;
232
- }
233
-
234
- /**
235
- * 获取下一行
236
- * @complexity `n`
237
- */
238
- getNextRow() {
239
- return this.#getSiblingRow((childNodes, index) => childNodes.slice(index + 1));
240
- }
241
-
242
- /**
243
- * 获取前一行
244
- * @complexity `n`
245
- */
246
- getPreviousRow() {
247
- return this.#getSiblingRow((childNodes, index) => childNodes.slice(0, index).reverse());
248
- }
249
-
250
- /**
251
- * 获取列数
252
- * @complexity `n`
253
- */
254
- getColCount() {
255
- const TdToken = require('./td');
256
- let count = 0,
257
- last = 0;
258
- for (const child of this.childNodes) {
259
- if (child instanceof TdToken) {
260
- last = child.isIndependent() ? Number(child.subtype !== 'caption') : last;
261
- count += last;
262
- }
263
- }
264
- return count;
265
- }
266
-
267
- /**
268
- * 获取第n列
269
- * @param {number} n 列号
270
- * @param {boolean} insert 是否用于判断插入新列的位置
271
- * @returns {TdToken}
272
- * @complexity `n`
273
- * @throws `RangeError` 不存在对应单元格
274
- */
275
- getNthCol(n, insert) {
276
- if (!Number.isInteger(n)) {
277
- this.typeError('getNthCol', 'Number');
278
- }
279
- const nCols = this.getColCount();
280
- n = n < 0 ? n + nCols : n;
281
- if (n < 0 || n > nCols || n === nCols && !insert) {
282
- throw new RangeError(`不存在第 ${n} 个单元格!`);
283
- }
284
- const TdToken = require('./td');
285
- let last = 0;
286
- for (const child of this.childNodes.slice(2)) {
287
- if (child instanceof TdToken) {
288
- if (child.isIndependent()) {
289
- last = Number(child.subtype !== 'caption');
290
- }
291
- n -= last;
292
- if (n < 0) {
293
- return child;
294
- }
295
- } else if (child.type === 'tr' || child.type === 'table-syntax') {
296
- return child;
297
- }
298
- }
299
- return undefined;
300
- }
301
-
302
- /**
303
- * 插入新的单元格
304
- * @param {string|Token} inner 单元格内部wikitext
305
- * @param {import('../../typings/table').TableCoords} coord 单元格坐标
306
- * @param {'td'|'th'|'caption'} subtype 单元格类型
307
- * @param {Record<string, string|boolean>} attr 单元格属性
308
- * @returns {TdToken}
309
- * @complexity `n`
310
- */
311
- insertTableCell(inner, {column}, subtype = 'td', attr = {}) {
312
- const TdToken = require('./td');
313
- const token = TdToken.create(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
314
- return this.insertBefore(token, this.getNthCol(column, true));
315
- }
316
- }
317
-
318
- Parser.classes.TrToken = __filename;
319
- module.exports = TrToken;