wikiparser-node 0.11.0 → 1.0.0-beta.1

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 (252) hide show
  1. package/config/.schema.json +26 -0
  2. package/dist/index.d.ts +61 -113
  3. package/dist/index.js +314 -0
  4. package/dist/lib/element.d.ts +122 -108
  5. package/dist/lib/element.js +656 -0
  6. package/dist/lib/node.d.ts +115 -221
  7. package/dist/lib/node.js +473 -0
  8. package/dist/lib/ranges.d.ts +27 -26
  9. package/dist/lib/ranges.js +130 -0
  10. package/dist/lib/text.d.ts +36 -28
  11. package/dist/lib/text.js +215 -0
  12. package/dist/lib/title.d.ts +26 -12
  13. package/dist/lib/title.js +108 -0
  14. package/dist/mixin/attributesParent.js +90 -0
  15. package/dist/mixin/fixed.js +29 -0
  16. package/dist/mixin/hidden.js +19 -0
  17. package/dist/mixin/singleLine.js +23 -0
  18. package/dist/mixin/sol.js +41 -0
  19. package/dist/parser/brackets.js +118 -0
  20. package/dist/parser/commentAndExt.js +48 -0
  21. package/dist/parser/converter.js +31 -0
  22. package/dist/parser/externalLinks.js +22 -0
  23. package/dist/parser/hrAndDoubleUnderscore.js +35 -0
  24. package/dist/parser/html.js +29 -0
  25. package/dist/parser/links.js +86 -0
  26. package/dist/parser/list.js +51 -0
  27. package/dist/parser/magicLinks.js +32 -0
  28. package/dist/parser/quotes.js +57 -0
  29. package/dist/parser/selector.js +158 -0
  30. package/dist/parser/table.js +108 -0
  31. package/dist/src/arg.d.ts +47 -23
  32. package/dist/src/arg.js +196 -0
  33. package/dist/src/atom.d.ts +12 -0
  34. package/dist/src/atom.js +22 -0
  35. package/dist/src/attribute.d.ts +74 -33
  36. package/dist/src/attribute.js +433 -0
  37. package/dist/src/attributes.d.ts +61 -55
  38. package/dist/src/attributes.js +371 -0
  39. package/dist/src/converter.d.ts +45 -71
  40. package/dist/src/converter.js +135 -0
  41. package/dist/src/converterFlags.d.ts +64 -39
  42. package/dist/src/converterFlags.js +235 -0
  43. package/dist/src/converterRule.d.ts +49 -27
  44. package/dist/src/converterRule.js +255 -0
  45. package/dist/src/extLink.d.ts +41 -38
  46. package/dist/src/extLink.js +154 -0
  47. package/dist/src/gallery.d.ts +59 -18
  48. package/dist/src/gallery.js +132 -0
  49. package/dist/src/heading.d.ts +60 -22
  50. package/dist/src/heading.js +135 -0
  51. package/dist/src/hidden.d.ts +20 -0
  52. package/dist/src/hidden.js +24 -0
  53. package/dist/src/html.d.ts +83 -29
  54. package/dist/src/html.js +242 -0
  55. package/dist/src/imageParameter.d.ts +59 -40
  56. package/dist/src/imageParameter.js +251 -0
  57. package/dist/src/imagemap.d.ts +65 -21
  58. package/dist/src/imagemap.js +169 -0
  59. package/dist/src/imagemapLink.d.ts +46 -14
  60. package/dist/src/imagemapLink.js +38 -0
  61. package/dist/src/index.d.ts +71 -105
  62. package/dist/src/index.js +826 -0
  63. package/dist/src/link/base.d.ts +71 -0
  64. package/dist/src/link/base.js +225 -0
  65. package/dist/src/link/category.d.ts +10 -11
  66. package/dist/src/link/category.js +26 -0
  67. package/dist/src/link/file.d.ts +61 -39
  68. package/dist/src/link/file.js +242 -0
  69. package/dist/src/link/galleryImage.d.ts +34 -12
  70. package/dist/src/link/galleryImage.js +98 -0
  71. package/dist/src/link/index.d.ts +25 -63
  72. package/dist/src/link/index.js +136 -0
  73. package/dist/src/magicLink.d.ts +22 -15
  74. package/dist/src/magicLink.js +126 -0
  75. package/dist/src/nested.d.ts +47 -0
  76. package/dist/src/nested.js +84 -0
  77. package/dist/src/nowiki/base.d.ts +39 -0
  78. package/dist/src/nowiki/base.js +29 -0
  79. package/dist/src/nowiki/comment.d.ts +31 -20
  80. package/dist/src/nowiki/comment.js +61 -0
  81. package/dist/src/nowiki/dd.d.ts +17 -11
  82. package/dist/src/nowiki/dd.js +50 -0
  83. package/dist/src/nowiki/doubleUnderscore.d.ts +28 -13
  84. package/dist/src/nowiki/doubleUnderscore.js +45 -0
  85. package/dist/src/nowiki/hr.d.ts +28 -10
  86. package/dist/src/nowiki/hr.js +33 -0
  87. package/dist/src/nowiki/index.d.ts +17 -23
  88. package/dist/src/nowiki/index.js +21 -0
  89. package/dist/src/nowiki/list.d.ts +15 -7
  90. package/dist/src/nowiki/list.js +11 -0
  91. package/dist/src/nowiki/noinclude.d.ts +20 -7
  92. package/dist/src/nowiki/noinclude.js +22 -0
  93. package/dist/src/nowiki/quote.d.ts +25 -10
  94. package/dist/src/nowiki/quote.js +55 -0
  95. package/dist/src/onlyinclude.d.ts +28 -12
  96. package/dist/src/onlyinclude.js +64 -0
  97. package/dist/src/paramTag/index.d.ts +40 -17
  98. package/dist/src/paramTag/index.js +76 -0
  99. package/dist/src/paramTag/inputbox.d.ts +8 -7
  100. package/dist/src/paramTag/inputbox.js +19 -0
  101. package/dist/src/parameter.d.ts +62 -41
  102. package/dist/src/parameter.js +201 -0
  103. package/dist/src/pre.d.ts +32 -0
  104. package/dist/src/pre.js +39 -0
  105. package/dist/src/syntax.d.ts +17 -14
  106. package/dist/src/syntax.js +65 -0
  107. package/dist/src/table/base.d.ts +55 -0
  108. package/dist/src/table/base.js +77 -0
  109. package/dist/src/table/index.d.ts +123 -156
  110. package/dist/src/table/index.js +811 -0
  111. package/dist/src/table/td.d.ts +90 -67
  112. package/dist/src/table/td.js +276 -0
  113. package/dist/src/table/tr.d.ts +30 -85
  114. package/dist/src/table/tr.js +48 -0
  115. package/dist/src/table/trBase.d.ts +72 -0
  116. package/dist/src/table/trBase.js +153 -0
  117. package/dist/src/tagPair/ext.d.ts +47 -11
  118. package/dist/src/tagPair/ext.js +127 -0
  119. package/dist/src/tagPair/include.d.ts +32 -13
  120. package/dist/src/tagPair/include.js +40 -0
  121. package/dist/src/tagPair/index.d.ts +44 -29
  122. package/dist/src/tagPair/index.js +111 -0
  123. package/dist/src/transclude.d.ts +88 -85
  124. package/dist/src/transclude.js +739 -0
  125. package/dist/util/base.js +26 -0
  126. package/dist/util/debug.js +52 -0
  127. package/dist/util/diff.js +69 -0
  128. package/dist/util/lint.js +38 -0
  129. package/dist/util/string.js +103 -0
  130. package/errors/README +1 -0
  131. package/i18n/zh-hans.json +1 -0
  132. package/i18n/zh-hant.json +1 -0
  133. package/package.json +21 -24
  134. package/printed/README +1 -0
  135. package/dist/mixin/attributeParent.d.ts +0 -9
  136. package/dist/mixin/fixedToken.d.ts +0 -8
  137. package/dist/mixin/hidden.d.ts +0 -8
  138. package/dist/mixin/singleLine.d.ts +0 -8
  139. package/dist/mixin/sol.d.ts +0 -8
  140. package/dist/parser/brackets.d.ts +0 -12
  141. package/dist/parser/commentAndExt.d.ts +0 -8
  142. package/dist/parser/converter.d.ts +0 -7
  143. package/dist/parser/externalLinks.d.ts +0 -7
  144. package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
  145. package/dist/parser/html.d.ts +0 -7
  146. package/dist/parser/links.d.ts +0 -7
  147. package/dist/parser/list.d.ts +0 -7
  148. package/dist/parser/magicLinks.d.ts +0 -7
  149. package/dist/parser/quotes.d.ts +0 -7
  150. package/dist/parser/selector.d.ts +0 -12
  151. package/dist/parser/table.d.ts +0 -11
  152. package/dist/src/atom/hidden.d.ts +0 -5
  153. package/dist/src/atom/index.d.ts +0 -15
  154. package/dist/src/charinsert.d.ts +0 -32
  155. package/dist/src/hasNowiki/index.d.ts +0 -14
  156. package/dist/src/hasNowiki/pre.d.ts +0 -13
  157. package/dist/src/nested/choose.d.ts +0 -13
  158. package/dist/src/nested/combobox.d.ts +0 -13
  159. package/dist/src/nested/index.d.ts +0 -18
  160. package/dist/src/nested/references.d.ts +0 -13
  161. package/dist/tool/index.d.ts +0 -420
  162. package/dist/util/base.d.ts +0 -10
  163. package/dist/util/debug.d.ts +0 -20
  164. package/dist/util/diff.d.ts +0 -8
  165. package/dist/util/lint.d.ts +0 -28
  166. package/dist/util/string.d.ts +0 -55
  167. package/index.js +0 -333
  168. package/lib/element.js +0 -618
  169. package/lib/node.js +0 -730
  170. package/lib/ranges.js +0 -130
  171. package/lib/text.js +0 -265
  172. package/lib/title.js +0 -83
  173. package/mixin/attributeParent.js +0 -117
  174. package/mixin/fixedToken.js +0 -40
  175. package/mixin/hidden.js +0 -21
  176. package/mixin/singleLine.js +0 -31
  177. package/mixin/sol.js +0 -54
  178. package/parser/brackets.js +0 -128
  179. package/parser/commentAndExt.js +0 -62
  180. package/parser/converter.js +0 -46
  181. package/parser/externalLinks.js +0 -33
  182. package/parser/hrAndDoubleUnderscore.js +0 -49
  183. package/parser/html.js +0 -42
  184. package/parser/links.js +0 -94
  185. package/parser/list.js +0 -59
  186. package/parser/magicLinks.js +0 -41
  187. package/parser/quotes.js +0 -64
  188. package/parser/selector.js +0 -180
  189. package/parser/table.js +0 -114
  190. package/src/arg.js +0 -207
  191. package/src/atom/hidden.js +0 -13
  192. package/src/atom/index.js +0 -43
  193. package/src/attribute.js +0 -472
  194. package/src/attributes.js +0 -453
  195. package/src/charinsert.js +0 -97
  196. package/src/converter.js +0 -176
  197. package/src/converterFlags.js +0 -284
  198. package/src/converterRule.js +0 -256
  199. package/src/extLink.js +0 -180
  200. package/src/gallery.js +0 -149
  201. package/src/hasNowiki/index.js +0 -44
  202. package/src/hasNowiki/pre.js +0 -40
  203. package/src/heading.js +0 -134
  204. package/src/html.js +0 -254
  205. package/src/imageParameter.js +0 -303
  206. package/src/imagemap.js +0 -199
  207. package/src/imagemapLink.js +0 -41
  208. package/src/index.js +0 -938
  209. package/src/link/category.js +0 -44
  210. package/src/link/file.js +0 -287
  211. package/src/link/galleryImage.js +0 -120
  212. package/src/link/index.js +0 -388
  213. package/src/magicLink.js +0 -151
  214. package/src/nested/choose.js +0 -24
  215. package/src/nested/combobox.js +0 -23
  216. package/src/nested/index.js +0 -96
  217. package/src/nested/references.js +0 -23
  218. package/src/nowiki/comment.js +0 -71
  219. package/src/nowiki/dd.js +0 -59
  220. package/src/nowiki/doubleUnderscore.js +0 -56
  221. package/src/nowiki/hr.js +0 -41
  222. package/src/nowiki/index.js +0 -56
  223. package/src/nowiki/list.js +0 -16
  224. package/src/nowiki/noinclude.js +0 -28
  225. package/src/nowiki/quote.js +0 -69
  226. package/src/onlyinclude.js +0 -64
  227. package/src/paramTag/index.js +0 -89
  228. package/src/paramTag/inputbox.js +0 -35
  229. package/src/parameter.js +0 -239
  230. package/src/syntax.js +0 -91
  231. package/src/table/index.js +0 -985
  232. package/src/table/td.js +0 -343
  233. package/src/table/tr.js +0 -319
  234. package/src/tagPair/ext.js +0 -146
  235. package/src/tagPair/include.js +0 -50
  236. package/src/tagPair/index.js +0 -131
  237. package/src/transclude.js +0 -843
  238. package/tool/index.js +0 -1209
  239. package/typings/api.d.ts +0 -9
  240. package/typings/array.d.ts +0 -29
  241. package/typings/event.d.ts +0 -22
  242. package/typings/index.d.ts +0 -118
  243. package/typings/node.d.ts +0 -35
  244. package/typings/parser.d.ts +0 -12
  245. package/typings/table.d.ts +0 -10
  246. package/typings/token.d.ts +0 -31
  247. package/typings/tool.d.ts +0 -6
  248. package/util/base.js +0 -17
  249. package/util/debug.js +0 -73
  250. package/util/diff.js +0 -76
  251. package/util/lint.js +0 -57
  252. package/util/string.js +0 -126
@@ -0,0 +1,433 @@
1
+ "use strict";
2
+ const lint_1 = require("../util/lint");
3
+ const string_1 = require("../util/string");
4
+ const fixed = require("../mixin/fixed");
5
+ const Parser = require("../index");
6
+ const Token = require(".");
7
+ const AtomToken = require("./atom");
8
+ const stages = { 'ext-attr': 0, 'html-attr': 2, 'table-attr': 3 }, pre = { 'ext-attr': '<pre ', 'html-attr': '<p ', 'table-attr': '{|' }, post = { 'ext-attr': '/>', 'html-attr': '>', 'table-attr': '' }, commonHtmlAttrs = new Set([
9
+ 'id',
10
+ 'class',
11
+ 'style',
12
+ 'lang',
13
+ 'dir',
14
+ 'title',
15
+ 'tabindex',
16
+ 'aria-describedby',
17
+ 'aria-flowto',
18
+ 'aria-hidden',
19
+ 'aria-label',
20
+ 'aria-labelledby',
21
+ 'aria-owns',
22
+ 'role',
23
+ 'about',
24
+ 'property',
25
+ 'resource',
26
+ 'datatype',
27
+ 'typeof',
28
+ 'itemid',
29
+ 'itemprop',
30
+ 'itemref',
31
+ 'itemscope',
32
+ 'itemtype',
33
+ ]), blockAttrs = new Set(['align']), citeAttrs = new Set(['cite']), citeAndAttrs = new Set(['cite', 'datetime']), widthAttrs = new Set(['width']), tdAttrs = new Set(['align', 'valign', 'abbr', 'axis', 'headers', 'scope', 'rowspan', 'colspan', 'width', 'height', 'bgcolor']), typeAttrs = new Set(['type']), htmlAttrs = {
34
+ div: blockAttrs,
35
+ h1: blockAttrs,
36
+ h2: blockAttrs,
37
+ h3: blockAttrs,
38
+ h4: blockAttrs,
39
+ h5: blockAttrs,
40
+ h6: blockAttrs,
41
+ blockquote: citeAttrs,
42
+ q: citeAttrs,
43
+ p: blockAttrs,
44
+ br: new Set(['clear']),
45
+ pre: widthAttrs,
46
+ ins: citeAndAttrs,
47
+ del: citeAndAttrs,
48
+ ul: typeAttrs,
49
+ ol: new Set(['type', 'start', 'reversed']),
50
+ li: new Set(['type', 'value']),
51
+ table: new Set(['summary', 'width', 'border', 'frame', 'rules', 'cellspacing', 'cellpadding', 'align', 'bgcolor']),
52
+ caption: blockAttrs,
53
+ tr: new Set(['bgcolor', 'align', 'valign']),
54
+ td: tdAttrs,
55
+ th: tdAttrs,
56
+ img: new Set(['alt', 'src', 'width', 'height', 'srcset']),
57
+ font: new Set(['size', 'color', 'face']),
58
+ hr: widthAttrs,
59
+ rt: new Set(['rbspan']),
60
+ data: new Set(['value']),
61
+ time: new Set(['datetime']),
62
+ meta: new Set(['itemprop', 'content']),
63
+ link: new Set(['itemprop', 'href', 'title']),
64
+ gallery: new Set(['mode', 'showfilename', 'caption', 'perrow', 'widths', 'heights', 'showthumbnails', 'type']),
65
+ poem: new Set(['compact', 'align']),
66
+ categorytree: new Set([
67
+ 'align',
68
+ 'hideroot',
69
+ 'onlyroot',
70
+ 'depth',
71
+ 'mode',
72
+ 'hideprefix',
73
+ 'namespaces',
74
+ 'showcount',
75
+ 'notranslations',
76
+ ]),
77
+ combooption: new Set(['name', 'for', 'inline', 'align']),
78
+ }, empty = new Set(), extAttrs = {
79
+ nowiki: empty,
80
+ indicator: new Set(['name']),
81
+ langconvert: new Set(['from', 'to']),
82
+ ref: new Set(['group', 'name', 'extends', 'follow', 'dir']),
83
+ references: new Set(['group', 'responsive']),
84
+ charinsert: new Set(['label']),
85
+ choose: new Set(['uncached', 'before', 'after']),
86
+ option: new Set(['weight']),
87
+ imagemap: empty,
88
+ inputbox: empty,
89
+ templatestyles: new Set(['src', 'wrapper']),
90
+ dynamicpagelist: empty,
91
+ poll: new Set(['id', 'show-results-before-voting']),
92
+ sm2: typeAttrs,
93
+ flashmp3: typeAttrs,
94
+ score: new Set([
95
+ 'line_width_inches',
96
+ 'lang',
97
+ 'override_midi',
98
+ 'raw',
99
+ 'note-language',
100
+ 'override_audio',
101
+ 'override_ogg',
102
+ 'sound',
103
+ 'vorbis',
104
+ ]),
105
+ seo: new Set([
106
+ 'title',
107
+ 'title_mode',
108
+ 'title_separator',
109
+ 'keywords',
110
+ 'description',
111
+ 'robots',
112
+ 'google_bot',
113
+ 'image',
114
+ 'image_width',
115
+ 'image_height',
116
+ 'image_alt',
117
+ 'type',
118
+ 'site_name',
119
+ 'locale',
120
+ 'section',
121
+ 'author',
122
+ 'published_time',
123
+ 'twitter_site',
124
+ ]),
125
+ tab: new Set([
126
+ 'nested',
127
+ 'name',
128
+ 'index',
129
+ 'class',
130
+ 'block',
131
+ 'inline',
132
+ 'openname',
133
+ 'closename',
134
+ 'collapsed',
135
+ 'dropdown',
136
+ 'style',
137
+ 'bgcolor',
138
+ 'container',
139
+ 'id',
140
+ 'title',
141
+ ]),
142
+ tabs: new Set(['plain', 'class', 'container', 'id', 'title', 'style']),
143
+ combobox: new Set(['placeholder', 'value', 'id', 'class', 'text', 'dropdown', 'style']),
144
+ }, insecureStyle = new RegExp(`${'expression'}|${'(?:filter|accelerator|-o-link(?:-source)?|-o-replace)\\s*:'}|${'(?:url|image(?:-set)?)\\s*\\('}|${'attr\\s*\\([^)]+[\\s,]url'}`, 'u');
145
+ /**
146
+ * 扩展和HTML标签属性
147
+ * @classdesc `{childNodes: [AtomToken, Token|AtomToken]}`
148
+ */
149
+ class AttributeToken extends fixed(Token) {
150
+ /** @browser */
151
+ #equal;
152
+ /** @browser */
153
+ #quotes;
154
+ /** @browser */
155
+ #tag;
156
+ /**
157
+ * 引号是否匹配
158
+ * @browser
159
+ */
160
+ get balanced() {
161
+ return !this.#equal || this.#quotes[0] === this.#quotes[1];
162
+ }
163
+ /**
164
+ * 标签名
165
+ * @browser
166
+ */
167
+ get tag() {
168
+ return this.#tag;
169
+ }
170
+ /**
171
+ * getValue()的getter
172
+ * @browser
173
+ */
174
+ get value() {
175
+ return this.getValue();
176
+ }
177
+ set value(value) {
178
+ this.setValue(value);
179
+ }
180
+ /**
181
+ * @browser
182
+ * @param type 标签类型
183
+ * @param tag 标签名
184
+ * @param key 属性名
185
+ * @param equal 等号
186
+ * @param value 属性值
187
+ * @param quotes 引号
188
+ */
189
+ constructor(type, tag, key, equal = '', value = '', quotes = [], config = Parser.getConfig(), accum = []) {
190
+ const keyToken = new AtomToken(key, 'attr-key', config, accum, {
191
+ [type === 'ext-attr' ? 'AstText' : 'Stage-1']: ':', ArgToken: ':', TranscludeToken: ':',
192
+ });
193
+ let valueToken;
194
+ if (key === 'title') {
195
+ valueToken = new Token(value, config, true, accum, {
196
+ [`Stage-${stages[type]}`]: ':', ConverterToken: ':',
197
+ });
198
+ valueToken.type = 'attr-value';
199
+ valueToken.setAttribute('stage', Parser.MAX_STAGE - 1);
200
+ }
201
+ else if (tag === 'gallery' && key === 'caption') {
202
+ const newConfig = { ...config, excludes: [...config.excludes, 'quote', 'extLink', 'magicLink', 'list'] };
203
+ valueToken = new Token(value, newConfig, true, accum, {
204
+ AstText: ':', LinkToken: ':', FileToken: ':', CategoryToken: ':', ConverterToken: ':',
205
+ });
206
+ valueToken.type = 'attr-value';
207
+ valueToken.setAttribute('stage', 5);
208
+ }
209
+ else if (tag === 'choose' && (key === 'before' || key === 'after')) {
210
+ const newConfig = { ...config, excludes: [...config.excludes, 'heading', 'html', 'table', 'hr', 'list'] };
211
+ valueToken = new Token(value, newConfig, true, accum, {
212
+ ArgToken: ':',
213
+ TranscludeToken: ':',
214
+ LinkToken: ':',
215
+ FileToken: ':',
216
+ CategoryToken: ':',
217
+ QuoteToken: ':',
218
+ ExtLinkToken: ':',
219
+ MagicLinkToken: ':',
220
+ ConverterToken: ':',
221
+ });
222
+ valueToken.type = 'attr-value';
223
+ valueToken.setAttribute('stage', 1);
224
+ }
225
+ else {
226
+ valueToken = new AtomToken(value, 'attr-value', config, accum, {
227
+ [`Stage-${stages[type]}`]: ':',
228
+ });
229
+ }
230
+ super(undefined, config, true, accum);
231
+ this.type = type;
232
+ this.append(keyToken, valueToken);
233
+ this.#equal = equal;
234
+ this.#quotes = quotes;
235
+ this.#tag = tag;
236
+ this.setAttribute('name', (0, string_1.removeComment)(key).trim().toLowerCase());
237
+ }
238
+ /** @private */
239
+ afterBuild() {
240
+ if (this.#equal.includes('\0')) {
241
+ this.#equal = this.buildFromStr(this.#equal, 'string');
242
+ }
243
+ if (this.parentNode) {
244
+ this.#tag = this.parentNode.name;
245
+ }
246
+ this.setAttribute('name', this.firstChild.text().trim().toLowerCase());
247
+ }
248
+ /**
249
+ * @override
250
+ * @browser
251
+ */
252
+ toString(selector) {
253
+ if (selector && this.matches(selector)) {
254
+ return '';
255
+ }
256
+ const [quoteStart = '', quoteEnd = ''] = this.#quotes;
257
+ return this.#equal
258
+ ? `${super.toString(selector, `${this.#equal}${quoteStart}`)}${quoteEnd}`
259
+ : this.firstChild.toString(selector);
260
+ }
261
+ /**
262
+ * @override
263
+ * @browser
264
+ */
265
+ text() {
266
+ return this.#equal ? `${super.text(`${this.#equal.trim()}"`)}"` : this.firstChild.text();
267
+ }
268
+ /** @private */
269
+ getGaps() {
270
+ return this.#equal ? this.#equal.length + (this.#quotes[0]?.length ?? 0) : 0;
271
+ }
272
+ /**
273
+ * @override
274
+ * @browser
275
+ */
276
+ print() {
277
+ const [quoteStart = '', quoteEnd = ''] = this.#quotes;
278
+ return this.#equal ? super.print({ sep: `${this.#equal}${quoteStart}`, post: quoteEnd }) : super.print();
279
+ }
280
+ /**
281
+ * @override
282
+ * @browser
283
+ */
284
+ lint(start = this.getAbsoluteIndex()) {
285
+ const errors = super.lint(start), { balanced, firstChild, lastChild, type, name, value } = this, tag = this.#tag;
286
+ let rect;
287
+ if (!balanced) {
288
+ const root = this.getRootNode();
289
+ rect = { start, ...root.posFromIndex(start) };
290
+ const e = (0, lint_1.generateForChild)(lastChild, rect, 'unclosed quotes', 'warning'), startIndex = e.startIndex - 1, startCol = e.startCol - 1;
291
+ errors.push({ ...e, startIndex, startCol, excerpt: String(root).slice(startIndex, startIndex + 50) });
292
+ }
293
+ if (extAttrs[tag] && !extAttrs[tag].has(name)
294
+ || (type !== 'ext-attr' && !/\{\{[^{]+\}\}/u.test(name) || tag in htmlAttrs)
295
+ && !htmlAttrs[tag]?.has(name) && !/^(?:xmlns:[\w:.-]+|data-[^:]*)$/u.test(name)
296
+ && (tag === 'meta' || tag === 'link' || !commonHtmlAttrs.has(name))) {
297
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
298
+ errors.push((0, lint_1.generateForChild)(firstChild, rect, 'illegal attribute name'));
299
+ }
300
+ else if (name === 'style' && typeof value === 'string' && insecureStyle.test(value)) {
301
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
302
+ errors.push((0, lint_1.generateForChild)(lastChild, rect, 'insecure style'));
303
+ }
304
+ else if (name === 'tabindex' && typeof value === 'string' && value.trim() !== '0') {
305
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
306
+ errors.push((0, lint_1.generateForChild)(lastChild, rect, 'nonzero tabindex'));
307
+ }
308
+ return errors;
309
+ }
310
+ /**
311
+ * 获取属性值
312
+ * @browser
313
+ */
314
+ getValue() {
315
+ if (this.#equal) {
316
+ const value = this.lastChild.text();
317
+ if (this.#quotes[1]) {
318
+ return value;
319
+ }
320
+ return this.#quotes[0] ? value.trimEnd() : value.trim();
321
+ }
322
+ return this.type === 'ext-attr' || '';
323
+ }
324
+ /** @private */
325
+ getAttribute(key) {
326
+ if (key === 'equal') {
327
+ return this.#equal;
328
+ }
329
+ return key === 'quotes' ? this.#quotes : super.getAttribute(key);
330
+ }
331
+ /** @private */
332
+ hasAttribute(key) {
333
+ return key === 'equal' || key === 'quotes' || super.hasAttribute(key);
334
+ }
335
+ /** @override */
336
+ cloneNode() {
337
+ const [key, value] = this.cloneChildNodes(), config = this.getAttribute('config');
338
+ return Parser.run(() => {
339
+ const token = new AttributeToken(this.type, this.#tag, '', this.#equal, '', this.#quotes, config);
340
+ token.firstChild.safeReplaceWith(key);
341
+ token.lastChild.safeReplaceWith(value);
342
+ token.afterBuild();
343
+ return token;
344
+ });
345
+ }
346
+ /** 转义等号 */
347
+ escape() {
348
+ this.#equal = '{{=}}';
349
+ }
350
+ /** 闭合引号 */
351
+ close() {
352
+ const [opening] = this.#quotes;
353
+ if (opening) {
354
+ this.#quotes[1] = opening;
355
+ }
356
+ }
357
+ /**
358
+ * 设置属性值
359
+ * @param value 参数值
360
+ * @throws `SyntaxError` 非法的标签属性
361
+ */
362
+ setValue(value) {
363
+ if (value === false) {
364
+ this.remove();
365
+ return;
366
+ }
367
+ else if (value === true) {
368
+ this.#equal = '';
369
+ return;
370
+ }
371
+ const { type } = this, key = this.name === 'title' ? 'title' : 'data', wikitext = `${pre[type]}${key}="${value}"${post[type]}`, root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')), { length, firstChild: tag } = root;
372
+ let attrs;
373
+ if (length !== 1 || tag.type !== type.slice(0, -5)) {
374
+ throw new SyntaxError(`非法的标签属性:${(0, string_1.noWrap)(value)}`);
375
+ }
376
+ else if (type === 'table-attr') {
377
+ if (tag.length !== 2) {
378
+ throw new SyntaxError(`非法的标签属性:${(0, string_1.noWrap)(value)}`);
379
+ }
380
+ attrs = tag.lastChild;
381
+ }
382
+ else {
383
+ attrs = tag.firstChild;
384
+ }
385
+ const { firstChild } = attrs;
386
+ if (attrs.length !== 1 || firstChild.type !== this.type || firstChild.name !== key) {
387
+ throw new SyntaxError(`非法的标签属性:${(0, string_1.noWrap)(value)}`);
388
+ }
389
+ const { lastChild } = firstChild;
390
+ firstChild.destroy();
391
+ this.lastChild.safeReplaceWith(lastChild);
392
+ if (this.#quotes[0]) {
393
+ this.close();
394
+ }
395
+ else {
396
+ this.#quotes = ['"', '"'];
397
+ }
398
+ }
399
+ /**
400
+ * 修改属性名
401
+ * @param key 新属性名
402
+ * @throws `Error` title属性不能更名
403
+ * @throws `SyntaxError` 非法的模板参数名
404
+ */
405
+ rename(key) {
406
+ if (this.name === 'title') {
407
+ throw new Error('title 属性不能更名!');
408
+ }
409
+ const { type } = this, wikitext = `${pre[type]}${key}${post[type]}`, root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')), { length, firstChild: tag } = root;
410
+ let attrs;
411
+ if (length !== 1 || tag.type !== type.slice(0, -5)) {
412
+ throw new SyntaxError(`非法的标签属性名:${(0, string_1.noWrap)(key)}`);
413
+ }
414
+ else if (type === 'table-attr') {
415
+ if (tag.length !== 2) {
416
+ throw new SyntaxError(`非法的标签属性名:${(0, string_1.noWrap)(key)}`);
417
+ }
418
+ attrs = tag.lastChild;
419
+ }
420
+ else {
421
+ attrs = tag.firstChild;
422
+ }
423
+ const { firstChild: attr } = attrs;
424
+ if (attrs.length !== 1 || attr.type !== this.type || attr.value !== true) {
425
+ throw new SyntaxError(`非法的标签属性名:${(0, string_1.noWrap)(key)}`);
426
+ }
427
+ const { firstChild } = attr;
428
+ attr.destroy();
429
+ this.firstChild.safeReplaceWith(firstChild);
430
+ }
431
+ }
432
+ Parser.classes['AttributeToken'] = __filename;
433
+ module.exports = AttributeToken;
@@ -1,112 +1,118 @@
1
- export = AttributesToken;
1
+ import * as Parser from '../index';
2
+ import Token = require('.');
3
+ import AtomToken = require('./atom');
4
+ import AttributeToken = require('./attribute');
5
+ declare type AttributesTypes = 'ext-attrs' | 'html-attrs' | 'table-attrs';
2
6
  /**
3
7
  * 扩展和HTML标签属性
4
8
  * @classdesc `{childNodes: ...AtomToken|AttributeToken}`
5
9
  */
6
- declare class AttributesToken extends Token {
7
- /**
8
- * @param {string} attr 标签属性
9
- * @param {'ext-attrs'|'html-attrs'|'table-attrs'} type 标签类型
10
- * @param {string} name 标签名
11
- * @param {import('../../typings/token').accum} accum
12
- */
13
- constructor(attr: string, type: 'ext-attrs' | 'html-attrs' | 'table-attrs', name: string, config?: import("../../typings/token").ParserConfig, accum?: import('../../typings/token').accum);
10
+ declare abstract class AttributesToken extends Token {
11
+ #private;
12
+ type: AttributesTypes;
13
+ name: string;
14
+ childNodes: (AtomToken | AttributeToken)[];
15
+ abstract get children(): (AtomToken | AttributeToken)[];
16
+ abstract get firstChild(): AtomToken | AttributeToken;
17
+ abstract get firstElementChild(): AtomToken | AttributeToken;
18
+ abstract get lastChild(): AtomToken | AttributeToken;
19
+ abstract get lastElementChild(): AtomToken | AttributeToken;
20
+ abstract get parentNode(): import('./tagPair/ext') | import('./html') | import('./table/base') | undefined;
21
+ abstract get parentElement(): import('./tagPair/ext') | import('./html') | import('./table/base') | undefined;
14
22
  /** getAttrs()方法的getter写法 */
15
- get attributes(): {
16
- [k: string]: string | boolean;
17
- };
18
- set className(arg: string);
23
+ get attributes(): Record<string, string | true>;
19
24
  /** 以字符串表示的class属性 */
20
25
  get className(): string;
26
+ set className(className: string);
21
27
  /** 以Set表示的class属性 */
22
28
  get classList(): Set<string>;
23
- set id(arg: string);
24
29
  /** id属性 */
25
30
  get id(): string;
31
+ set id(id: string);
26
32
  /** 是否含有无效属性 */
27
33
  get sanitized(): boolean;
28
- type: "ext-attrs" | "html-attrs" | "table-attrs";
34
+ /**
35
+ * @browser
36
+ * @param attr 标签属性
37
+ * @param type 标签类型
38
+ * @param name 标签名
39
+ */
40
+ constructor(attr: string | undefined, type: AttributesTypes, name: string, config?: Parser.Config, accum?: Token[]);
29
41
  /**
30
42
  * 所有指定属性名的AttributeToken
31
- * @param {string} key 属性名
32
- * @returns {AttributeToken[]}
43
+ * @browser
44
+ * @param key 属性名
33
45
  */
34
46
  getAttrTokens(key: string): AttributeToken[];
35
47
  /**
36
- * 制定属性名的最后一个AttributeToken
37
- * @param {string} key 属性名
48
+ * 指定属性名的最后一个AttributeToken
49
+ * @browser
50
+ * @param key 属性名
38
51
  */
39
- getAttrToken(key: string): AttributeToken;
52
+ getAttrToken(key: string): AttributeToken | undefined;
40
53
  /**
41
54
  * 获取标签属性
42
- * @param {string} key 属性键
55
+ * @browser
56
+ * @param key 属性键
57
+ */
58
+ getAttr(key: string): string | true | undefined;
59
+ /**
60
+ * @override
61
+ * @browser
43
62
  */
44
- getAttr(key: string): string | true;
63
+ lint(start?: number): Parser.LintError[];
45
64
  /**
46
65
  * @override
47
- * @this {AttributesToken & {parentNode: HtmlToken}}
66
+ * @browser
48
67
  */
49
- override print(this: AttributesToken & {
50
- parentNode: import("./html");
51
- }): string;
68
+ print(): string;
52
69
  /** 清理标签属性 */
53
70
  sanitize(): void;
54
71
  /** @override */
55
- override cloneNode(): AttributesToken;
56
- /**
57
- * 所有无效属性
58
- * @returns {AtomToken[]}
59
- */
72
+ cloneNode(): this;
73
+ /** 所有无效属性 */
60
74
  getDirtyAttrs(): AtomToken[];
61
75
  /**
62
76
  * @override
63
- * @param {AttributeToken} token 待插入的子节点
64
- * @param {number} i 插入位置
77
+ * @param token 待插入的子节点
78
+ * @param i 插入位置
65
79
  * @throws `RangeError` 不是AttributeToken或标签不匹配
66
80
  */
67
- override insertAt(token: AttributeToken, i?: number): AttributeToken;
81
+ insertAt<T extends AttributeToken>(token: T, i?: number): T;
68
82
  /**
69
83
  * 设置标签属性
70
- * @param {string} key 属性键
71
- * @param {string|boolean} value 属性值
84
+ * @param key 属性键
85
+ * @param value 属性值
72
86
  * @throws `RangeError` 扩展标签属性不能包含">"
73
87
  * @throws `RangeError` 无效的属性名
74
88
  */
75
89
  setAttr(key: string, value: string | boolean): void;
76
90
  /**
77
91
  * 标签是否具有某属性
78
- * @param {string} key 属性键
92
+ * @param key 属性键
79
93
  */
80
94
  hasAttr(key: string): boolean;
81
95
  /** 获取全部的标签属性名 */
82
- getAttrNames(): Set<any>;
96
+ getAttrNames(): Set<string>;
83
97
  /** 标签是否具有任意属性 */
84
98
  hasAttrs(): boolean;
85
99
  /** 获取全部标签属性 */
86
- getAttrs(): {
87
- [k: string]: string | boolean;
88
- };
100
+ getAttrs(): Record<string, string | true>;
89
101
  /**
90
102
  * 移除标签属性
91
- * @param {string} key 属性键
103
+ * @param key 属性键
92
104
  */
93
105
  removeAttr(key: string): void;
94
106
  /**
95
107
  * 开关标签属性
96
- * @param {string} key 属性键
97
- * @param {boolean|undefined} force 强制开启或关闭
108
+ * @param key 属性键
109
+ * @param force 强制开启或关闭
98
110
  * @throws `RangeError` 不为Boolean类型的属性值
99
111
  */
100
- toggleAttr(key: string, force: boolean | undefined): void;
101
- /**
102
- * @override
103
- * @param {string} selector
104
- */
105
- override toString(selector: string): string;
112
+ toggleAttr(key: string, force?: boolean): void;
106
113
  /** @override */
107
- override text(): string;
108
- #private;
114
+ toString(selector?: string): string;
115
+ /** @override */
116
+ text(): string;
109
117
  }
110
- import Token = require(".");
111
- import AttributeToken = require("./attribute");
112
- import AtomToken = require("./atom");
118
+ export = AttributesToken;