Debian Codegraph @

resources/legacy/mode/mediawiki/mediawiki.js

raw symbols SCIP
1 ( function ( CodeMirror ) {
2 'use strict';
3
4 function eatMnemonic( stream, style, mnemonicStyle ) {
5 let ok;
6 if ( stream.eat( '#' ) ) {
7 if ( stream.eat( 'x' ) ) {
8 ok = stream.eatWhile( /[a-fA-F\d]/ ) && stream.eat( ';' );
9 } else {
10 ok = stream.eatWhile( /[\d]/ ) && stream.eat( ';' );
11 }
12 } else {
13 ok = stream.eatWhile( /[\w.\-:]/ ) && stream.eat( ';' );
14 }
15 if ( ok ) {
16 mnemonicStyle += ' mw-mnemonic';
17 return mnemonicStyle;
18 }
19 return style;
20 }
21
22 CodeMirror.defineMode( 'mediawiki', ( config /* , parserConfig */ ) => {
23
24 const mwConfig = config.mwConfig,
25
26 urlProtocols = new RegExp( '^(?:' + mwConfig.urlProtocols + ')', 'i' ),
27 permittedHtmlTags = { b: true, bdi: true, del: true, i: true, ins: true,
28 u: true, font: true, big: true, small: true, sub: true, sup: true,
29 h1: true, h2: true, h3: true, h4: true, h5: true, h6: true, cite: true,
30 code: true, em: true, s: true, strike: true, strong: true, tt: true,
31 var: true, div: true, center: true, blockquote: true, q: true, ol: true, ul: true,
32 dl: true, table: true, caption: true, pre: true, ruby: true, rb: true,
33 rp: true, rt: true, rtc: true, p: true, span: true, abbr: true, dfn: true,
34 kbd: true, samp: true, data: true, time: true, mark: true, br: true,
35 wbr: true, hr: true, li: true, dt: true, dd: true, td: true, th: true,
36 tr: true, noinclude: true, includeonly: true, onlyinclude: true },
37 voidHtmlTags = { br: true, hr: true, wbr: true };
38 let isBold, isItalic, firstsingleletterword, firstmultiletterword, firstspace, mBold, mItalic, mTokens = [],
39 mStyle;
40
41 function makeStyle( style, state, endGround ) {
42 if ( isBold ) {
43 style += ' strong';
44 }
45 if ( isItalic ) {
46 style += ' em';
47 }
48 return makeLocalStyle( style, state, endGround );
49 }
50
51 function makeLocalStyle( style, state, endGround ) {
52 let ground = '';
53 switch ( state.nTemplate ) {
54 case 0:
55 break;
56 case 1:
57 ground += '-template';
58 break;
59 case 2:
60 ground += '-template2';
61 break;
62 default:
63 ground += '-template3';
64 break;
65 }
66 switch ( state.nExt ) {
67 case 0:
68 break;
69 case 1:
70 ground += '-ext';
71 break;
72 case 2:
73 ground += '-ext2';
74 break;
75 default:
76 ground += '-ext3';
77 break;
78 }
79 if ( state.nLink > 0 ) {
80 ground += '-link';
81 }
82 if ( ground !== '' ) {
83 style = 'mw' + ground + '-ground ' + style;
84 }
85 if ( endGround ) {
86 state[ endGround ]--;
87 }
88 return style;
89 }
90
91 function eatBlock( style, terminator, consumeLast ) {
92 return function ( stream, state ) {
93 if ( stream.skipTo( terminator ) ) {
94 if ( consumeLast !== false ) {
95 stream.match( terminator );
96 }
97 state.tokenize = state.stack.pop();
98 } else {
99 stream.skipToEnd();
100 }
101 return makeLocalStyle( style, state );
102 };
103 }
104
105 function eatEnd( style ) {
106 return function ( stream, state ) {
107 stream.skipToEnd();
108 state.tokenize = state.stack.pop();
109 return makeLocalStyle( style, state );
110 };
111 }
112
113 function eatChar( char, style ) {
114 return function ( stream, state ) {
115 state.tokenize = state.stack.pop();
116 if ( stream.eat( char ) ) {
117 return makeLocalStyle( style, state );
118 }
119 return makeLocalStyle( 'error', state );
120 };
121 }
122
123 function eatSectionHeader( count ) {
124 return function ( stream, state ) {
125 if ( stream.match( /^[^&<[{~]+/ ) ) {
126 if ( stream.eol() ) {
127 stream.backUp( count );
128 state.tokenize = eatEnd( 'mw-section-header' );
129 } else if ( stream.match( /^<!--(?!.*?-->.*?=)/, false ) ) {
130 // T171074: handle trailing comments
131 stream.backUp( count );
132 state.tokenize = eatBlock( 'mw-section-header', '<!--', false );
133 }
134 return null; // style is null
135 }
136 return eatWikiText( '', '' )( stream, state );
137 };
138 }
139
140 function inVariable( stream, state ) {
141 if ( stream.match( /^[^{}|]+/ ) ) {
142 return makeLocalStyle( 'mw-templatevariable-name', state );
143 }
144 if ( stream.eat( '|' ) ) {
145 state.tokenize = inVariableDefault;
146 return makeLocalStyle( 'mw-templatevariable-delimiter', state );
147 }
148 if ( stream.match( '}}}' ) ) {
149 state.tokenize = state.stack.pop();
150 return makeLocalStyle( 'mw-templatevariable-bracket', state );
151 }
152 if ( stream.match( '{{{' ) ) {
153 state.stack.push( state.tokenize );
154 return makeLocalStyle( 'mw-templatevariable-bracket', state );
155 }
156 stream.next();
157 return makeLocalStyle( 'mw-templatevariable-name', state );
158 }
159
160 function inVariableDefault( stream, state ) {
161 if ( stream.match( /^[^{}[<&~]+/ ) ) {
162 return makeLocalStyle( 'mw-templatevariable', state );
163 }
164 if ( stream.match( '}}}' ) ) {
165 state.tokenize = state.stack.pop();
166 return makeLocalStyle( 'mw-templatevariable-bracket', state );
167 }
168 return eatWikiText( 'mw-templatevariable', '' )( stream, state );
169 }
170
171 function inParserFunctionName( stream, state ) {
172 if ( stream.match( /^#?[^:}{~]+/ ) ) { // FIXME: {{#name}} and {{uc}} are wrong, must have ':'
173 return makeLocalStyle( 'mw-parserfunction-name', state );
174 }
175 if ( stream.eat( ':' ) ) {
176 state.tokenize = inParserFunctionArguments;
177 return makeLocalStyle( 'mw-parserfunction-delimiter', state );
178 }
179 if ( stream.match( '}}' ) ) {
180 state.tokenize = state.stack.pop();
181 return makeLocalStyle( 'mw-parserfunction-bracket', state, 'nExt' );
182 }
183 return eatWikiText( 'mw-parserfunction', '' )( stream, state );
184 }
185
186 function inParserFunctionArguments( stream, state ) {
187 if ( stream.match( /^[^|}{[<&~]+/ ) ) {
188 return makeLocalStyle( 'mw-parserfunction', state );
189 } else if ( stream.eat( '|' ) ) {
190 return makeLocalStyle( 'mw-parserfunction-delimiter', state );
191 } else if ( stream.match( '}}' ) ) {
192 state.tokenize = state.stack.pop();
193 return makeLocalStyle( 'mw-parserfunction-bracket', state, 'nExt' );
194 }
195 return eatWikiText( 'mw-parserfunction', '' )( stream, state );
196 }
197
198 function eatTemplatePageName( haveAte ) {
199 return function ( stream, state ) {
200 if ( stream.match( /^[\s\u00a0]*\|[\s\u00a0]*/ ) ) {
201 state.tokenize = eatTemplateArgument( true );
202 return makeLocalStyle( 'mw-template-delimiter', state );
203 }
204 if ( stream.match( /^[\s\u00a0]*\}\}/ ) ) {
205 state.tokenize = state.stack.pop();
206 return makeLocalStyle( 'mw-template-bracket', state, 'nTemplate' );
207 }
208 if ( stream.match( /^[\s\u00a0]*<!--.*?-->/ ) ) {
209 return makeLocalStyle( 'mw-comment', state );
210 }
211 if ( haveAte && stream.sol() ) {
212 // @todo error message
213 state.nTemplate--;
214 state.tokenize = state.stack.pop();
215 return;
216 }
217 if ( stream.match( /^[\s\u00a0]*[^\s\u00a0|}<{&~]+/ ) ) {
218 state.tokenize = eatTemplatePageName( true );
219 return makeLocalStyle( 'mw-template-name mw-pagename', state );
220 } else if ( stream.eatSpace() ) {
221 if ( stream.eol() === true ) {
222 return makeLocalStyle( 'mw-template-name', state );
223 }
224 return makeLocalStyle( 'mw-template-name mw-pagename', state );
225 }
226 return eatWikiText( 'mw-template-name mw-pagename', 'mw-template-name-mnemonic mw-pagename' )( stream, state );
227 };
228 }
229
230 function eatTemplateArgument( expectArgName ) {
231 return function ( stream, state ) {
232 if ( expectArgName && stream.eatWhile( /[^=|}{[<&~]/ ) ) {
233 if ( stream.eat( '=' ) ) {
234 state.tokenize = eatTemplateArgument( false );
235 return makeLocalStyle( 'mw-template-argument-name', state );
236 }
237 return makeLocalStyle( 'mw-template', state );
238 } else if ( stream.eatWhile( /[^|}{[<&~]/ ) ) {
239 return makeLocalStyle( 'mw-template', state );
240 } else if ( stream.eat( '|' ) ) {
241 state.tokenize = eatTemplateArgument( true );
242 return makeLocalStyle( 'mw-template-delimiter', state );
243 } else if ( stream.match( '}}' ) ) {
244 state.tokenize = state.stack.pop();
245 return makeLocalStyle( 'mw-template-bracket', state, 'nTemplate' );
246 }
247 return eatWikiText( 'mw-template', '' )( stream, state );
248 };
249 }
250
251 function eatExternalLinkProtocol( chars ) {
252 return function ( stream, state ) {
253 while ( chars > 0 ) {
254 chars--;
255 stream.next();
256 }
257 if ( stream.eol() ) {
258 state.nLink--;
259 // @todo error message
260 state.tokenize = state.stack.pop();
261 } else {
262 state.tokenize = inExternalLink;
263 }
264 return makeLocalStyle( 'mw-extlink-protocol', state );
265 };
266 }
267
268 function inExternalLink( stream, state ) {
269 if ( stream.sol() ) {
270 state.nLink--;
271 // @todo error message
272 state.tokenize = state.stack.pop();
273 return;
274 }
275 if ( stream.match( /^[\s\u00a0]*\]/ ) ) {
276 state.tokenize = state.stack.pop();
277 return makeLocalStyle( 'mw-extlink-bracket', state, 'nLink' );
278 }
279 if ( stream.eatSpace() ) {
280 state.tokenize = inExternalLinkText;
281 return makeStyle( '', state );
282 }
283 if ( stream.match( /^[^\s\u00a0\]{&~']+/ ) || stream.eatSpace() ) {
284 if ( stream.peek() === '\'' ) {
285 if ( stream.match( '\'\'', false ) ) {
286 state.tokenize = inExternalLinkText;
287 } else {
288 stream.next();
289 }
290 }
291 return makeStyle( 'mw-extlink', state );
292 }
293 return eatWikiText( 'mw-extlink', '' )( stream, state );
294 }
295
296 function inExternalLinkText( stream, state ) {
297 if ( stream.sol() ) {
298 state.nLink--;
299 // @todo error message
300 state.tokenize = state.stack.pop();
301 return;
302 }
303 if ( stream.eat( ']' ) ) {
304 state.tokenize = state.stack.pop();
305 return makeLocalStyle( 'mw-extlink-bracket', state, 'nLink' );
306 }
307 if ( stream.match( /^[^'\]{&~<]+/ ) ) {
308 return makeStyle( 'mw-extlink-text', state );
309 }
310 return eatWikiText( 'mw-extlink-text', '' )( stream, state );
311 }
312
313 function inLink( stream, state ) {
314 if ( stream.sol() ) {
315 state.nLink--;
316 // @todo error message
317 state.tokenize = state.stack.pop();
318 return;
319 }
320 if ( stream.match( /^[\s\u00a0]*#[\s\u00a0]*/ ) ) {
321 state.tokenize = inLinkToSection;
322 return makeLocalStyle( 'mw-link', state );
323 }
324 if ( stream.match( /^[\s\u00a0]*\|[\s\u00a0]*/ ) ) {
325 state.tokenize = eatLinkText();
326 return makeLocalStyle( 'mw-link-delimiter', state );
327 }
328 if ( stream.match( /^[\s\u00a0]*\]\]/ ) ) {
329 state.tokenize = state.stack.pop();
330 return makeLocalStyle( 'mw-link-bracket', state, 'nLink' );
331 // if ( !stream.eatSpace() ) {
332 // state.ImInBlock.push( 'LinkTrail' );
333 // }
334 }
335 if ( stream.match( /^[\s\u00a0]*[^\s\u00a0#|\]&~{]+/ ) || stream.eatSpace() ) { // FIXME '{{' brokes Link, sample [[z{{page]]
336 return makeStyle( 'mw-link-pagename mw-pagename', state );
337 }
338 return eatWikiText( 'mw-link-pagename mw-pagename', 'mw-pagename' )( stream, state );
339 }
340
341 function inLinkToSection( stream, state ) {
342 if ( stream.sol() ) {
343 // @todo error message
344 state.nLink--;
345 state.tokenize = state.stack.pop();
346 return;
347 }
348 if ( stream.match( /^[^|\]&~{}]+/ ) ) { // FIXME '{{' brokes Link, sample [[z{{page]]
349 return makeLocalStyle( 'mw-link-tosection', state );
350 }
351 if ( stream.eat( '|' ) ) {
352 state.tokenize = eatLinkText();
353 return makeLocalStyle( 'mw-link-delimiter', state );
354 }
355 if ( stream.match( ']]' ) ) {
356 state.tokenize = state.stack.pop();
357 return makeLocalStyle( 'mw-link-bracket', state, 'nLink' );
358 // if ( !stream.eatSpace() ) {
359 // state.ImInBlock.push( 'LinkTrail' );
360 // }
361 }
362 return eatWikiText( 'mw-link-tosection', '' )( stream, state );
363 }
364
365 function eatLinkText() {
366 let linkIsBold, linkIsItalic;
367 return function ( stream, state ) {
368 let tmpstyle;
369 if ( stream.match( ']]' ) ) {
370 state.tokenize = state.stack.pop();
371 return makeLocalStyle( 'mw-link-bracket', state, 'nLink' );
372 }
373 if ( stream.match( '\'\'\'' ) ) {
374 linkIsBold = !linkIsBold;
375 return makeLocalStyle( 'mw-link-text mw-apostrophes', state );
376 }
377 if ( stream.match( '\'\'' ) ) {
378 linkIsItalic = !linkIsItalic;
379 return makeLocalStyle( 'mw-link-text mw-apostrophes', state );
380 }
381 tmpstyle = 'mw-link-text';
382 if ( linkIsBold ) {
383 tmpstyle += ' strong';
384 }
385 if ( linkIsItalic ) {
386 tmpstyle += ' em';
387 }
388 if ( stream.match( /^[^'\]{&~<]+/ ) ) {
389 return makeStyle( tmpstyle, state );
390 }
391 return eatWikiText( tmpstyle, '' )( stream, state );
392 };
393 }
394
395 function eatTagName( chars, isCloseTag, isHtmlTag ) {
396 return function ( stream, state ) {
397 let name = '';
398 while ( chars > 0 ) {
399 chars--;
400 name = name + stream.next();
401 }
402 stream.eatSpace();
403
404 if ( isHtmlTag ) {
405 if ( isCloseTag && !( name in voidHtmlTags ) ) {
406 state.tokenize = eatChar( '>', 'mw-htmltag-bracket' );
407 } else {
408 state.tokenize = eatHtmlTagAttribute( name );
409 }
410 return makeLocalStyle( 'mw-htmltag-name', state );
411 } // it is the extension tag
412 if ( isCloseTag ) {
413 state.tokenize = eatChar( '>', 'mw-exttag-bracket mw-ext-' + name );
414 } else {
415 state.tokenize = eatExtTagAttribute( name );
416 }
417 return makeLocalStyle( 'mw-exttag-name mw-ext-' + name, state );
418 };
419 }
420
421 function eatHtmlTagAttribute( name ) {
422 return function ( stream, state ) {
423
424 if ( stream.match( /^(?:"[^<">]*"|'[^<'>]*'|[^>/<{&~])+/ ) ) {
425 return makeLocalStyle( 'mw-htmltag-attribute', state );
426 }
427 if ( stream.eat( '>' ) ) {
428 if ( !( name in voidHtmlTags ) ) {
429 state.InHtmlTag.push( name );
430 }
431 state.tokenize = state.stack.pop();
432 return makeLocalStyle( 'mw-htmltag-bracket', state );
433 }
434 if ( stream.match( '/>' ) ) {
435 state.tokenize = state.stack.pop();
436 return makeLocalStyle( 'mw-htmltag-bracket', state );
437 }
438 return eatWikiText( 'mw-htmltag-attribute', '' )( stream, state );
439 };
440 }
441
442 function eatExtTagAttribute( name ) {
443 return function ( stream, state ) {
444
445 if ( stream.match( /^(?:"[^">]*"|'[^'>]*'|[^>/<{&~])+/ ) ) {
446 return makeLocalStyle( 'mw-exttag-attribute mw-ext-' + name, state );
447 }
448 if ( stream.eat( '>' ) ) {
449 state.extName = name;
450 if ( name in mwConfig.tagModes ) {
451 state.extMode = CodeMirror.getMode( config, mwConfig.tagModes[ name ] );
452 state.extState = CodeMirror.startState( state.extMode );
453 }
454 state.tokenize = eatExtTagArea( name );
455 return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + name, state );
456 }
457 if ( stream.match( '/>' ) ) {
458 state.tokenize = state.stack.pop();
459 return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + name, state );
460 }
461 return eatWikiText( 'mw-exttag-attribute mw-ext-' + name, '' )( stream, state );
462 };
463 }
464
465 function eatExtTagArea( name ) {
466 return function ( stream, state ) {
467 let to,
468 origString = false;
469 const from = stream.pos,
470
471 pattern = new RegExp( '</' + name + '\\s*>', 'i' ),
472 m = pattern.exec( from ? stream.string.slice( from ) : stream.string );
473
474 if ( m ) {
475 if ( m.index === 0 ) {
476 state.tokenize = eatExtCloseTag( name );
477 state.extName = false;
478 if ( state.extMode !== false ) {
479 state.extMode = false;
480 state.extState = false;
481 }
482 return state.tokenize( stream, state );
483 }
484 to = m.index + from;
485 origString = stream.string;
486 stream.string = origString.slice( 0, to );
487 }
488
489 state.stack.push( state.tokenize );
490 state.tokenize = eatExtTokens( origString );
491 return state.tokenize( stream, state );
492 };
493 }
494
495 function eatExtCloseTag( name ) {
496 return function ( stream, state ) {
497 stream.next(); // eat <
498 stream.next(); // eat /
499 state.tokenize = eatTagName( name.length, true, false );
500 return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + name, state );
501 };
502 }
503
504 function eatExtTokens( origString ) {
505 return function ( stream, state ) {
506 let ret;
507 if ( state.extMode === false ) {
508 ret = ( origString === false && stream.sol() ? 'line-cm-mw-exttag' : 'mw-exttag' );
509 stream.skipToEnd();
510 } else {
511 ret = ( origString === false && stream.sol() ? 'line-cm-mw-tag-' : 'mw-tag-' ) + state.extName;
512 ret += ' ' + state.extMode.token( stream, state.extState, origString === false );
513 }
514 if ( stream.eol() ) {
515 if ( origString !== false ) {
516 stream.string = origString;
517 }
518 state.tokenize = state.stack.pop();
519 }
520 return makeLocalStyle( ret, state );
521 };
522 }
523
524 function eatStartTable( stream, state ) {
525 stream.match( '{|' );
526 stream.eatSpace();
527 state.tokenize = inTableDefinition;
528 return 'mw-table-bracket';
529 }
530
531 function inTableDefinition( stream, state ) {
532 if ( stream.sol() ) {
533 state.tokenize = inTable;
534 return inTable( stream, state );
535 }
536 return eatWikiText( 'mw-table-definition', '' )( stream, state );
537 }
538
539 function inTableCaption( stream, state ) {
540 if ( stream.sol() && stream.match( /^[\s\u00a0]*[|!]/, false ) ) {
541 state.tokenize = inTable;
542 return inTable( stream, state );
543 }
544 return eatWikiText( 'mw-table-caption', '' )( stream, state );
545 }
546
547 function inTable( stream, state ) {
548 if ( stream.sol() ) {
549 stream.eatSpace();
550 if ( stream.eat( '|' ) ) {
551 if ( stream.eat( '-' ) ) {
552 stream.eatSpace();
553 state.tokenize = inTableDefinition;
554 return makeLocalStyle( 'mw-table-delimiter', state );
555 }
556 if ( stream.eat( '+' ) ) {
557 stream.eatSpace();
558 state.tokenize = inTableCaption;
559 return makeLocalStyle( 'mw-table-delimiter', state );
560 }
561 if ( stream.eat( '}' ) ) {
562 state.tokenize = state.stack.pop();
563 return makeLocalStyle( 'mw-table-bracket', state );
564 }
565 stream.eatSpace();
566 state.tokenize = eatTableRow( true, false );
567 return makeLocalStyle( 'mw-table-delimiter', state );
568 }
569 if ( stream.eat( '!' ) ) {
570 stream.eatSpace();
571 state.tokenize = eatTableRow( true, true );
572 return makeLocalStyle( 'mw-table-delimiter', state );
573 }
574 }
575 return eatWikiText( '', '' )( stream, state );
576 }
577
578 function eatTableRow( isStart, isHead ) {
579 return function ( stream, state ) {
580 if ( stream.sol() ) {
581 if ( stream.match( /^[\s\u00a0]*[|!]/, false ) ) {
582 state.tokenize = inTable;
583 return inTable( stream, state );
584 }
585 } else {
586 if ( stream.match( /^[^'|{[<&~!]+/ ) ) {
587 return makeStyle( ( isHead ? 'strong' : '' ), state );
588 }
589 if ( stream.match( '||' ) || isHead && stream.match( '!!' ) || ( isStart && stream.eat( '|' ) ) ) {
590 isBold = false;
591 isItalic = false;
592 if ( isStart ) {
593 state.tokenize = eatTableRow( false, isHead );
594 }
595 return makeLocalStyle( 'mw-table-delimiter', state );
596 }
597 }
598 return eatWikiText( ( isHead ? 'strong' : '' ), ( isHead ? 'strong' : '' ) )( stream, state );
599 };
600 }
601
602 function eatFreeExternalLinkProtocol( stream, state ) {
603 stream.match( urlProtocols );
604 state.tokenize = eatFreeExternalLink;
605 return makeLocalStyle( 'mw-free-extlink-protocol', state );
606 }
607
608 function eatFreeExternalLink( stream, state ) {
609 if ( stream.eol() ) {
610 // @todo error message
611 } else if ( stream.match( /^[^\s\u00a0{[\]<>~).,']*/ ) ) {
612 if ( stream.peek() === '~' ) {
613 if ( !stream.match( /^~~~+/, false ) ) {
614 stream.match( /^~*/ );
615 return makeLocalStyle( 'mw-free-extlink', state );
616 }
617 } else if ( stream.peek() === '{' ) {
618 if ( !stream.match( '{{', false ) ) {
619 stream.next();
620 return makeLocalStyle( 'mw-free-extlink', state );
621 }
622 } else if ( stream.peek() === '\'' ) {
623 if ( !stream.match( '\'\'', false ) ) {
624 stream.next();
625 return makeLocalStyle( 'mw-free-extlink', state );
626 }
627 } else if ( stream.match( /^[).,]+(?=[^\s\u00a0{[\]<>~).,])/ ) ) {
628 return makeLocalStyle( 'mw-free-extlink', state );
629 }
630 }
631 state.tokenize = state.stack.pop();
632 return makeLocalStyle( 'mw-free-extlink', state );
633 }
634
635 function eatWikiText( style, mnemonicStyle ) {
636 return function ( stream, state ) {
637 let ch, tmp, mt, name, isCloseTag, tagname;
638 const sol = stream.sol();
639
640 function chain( parser ) {
641 state.stack.push( state.tokenize );
642 state.tokenize = parser;
643 return parser( stream, state );
644 }
645
646 if ( sol ) {
647 if ( !stream.match( '//', false ) && stream.match( urlProtocols ) ) { // highlight free external links, bug T108448
648 state.stack.push( state.tokenize );
649 state.tokenize = eatFreeExternalLink;
650 return makeLocalStyle( 'mw-free-extlink-protocol', state );
651 }
652 ch = stream.next();
653 switch ( ch ) {
654 case '-':
655 if ( stream.match( /^---+/ ) ) {
656 return 'mw-hr';
657 }
658 break;
659 case '=':
660
661 tmp = stream.match( /^(={0,5})(.+?(=\1\s*)(<!--(?!.*-->.*\S).*?)?)$/ );
662 if ( tmp ) { // Title
663 stream.backUp( tmp[ 2 ].length );
664 state.stack.push( state.tokenize );
665 state.tokenize = eatSectionHeader( tmp[ 3 ].length );
666 return 'mw-section-header line-cm-mw-section-' + ( tmp[ 1 ].length + 1 );
667 }
668 break;
669 case '*':
670 case '#':
671 // Just consume all nested list and indention syntax when there is more
672 stream.match( /^[*#]*:*/ );
673 return 'mw-list';
674 case ':':
675 if ( stream.match( /^:*{\|/, false ) ) { // Highlight indented tables :{|, bug T108454
676 state.stack.push( state.tokenize );
677 state.tokenize = eatStartTable;
678 }
679 // Just consume all nested list and indention syntax when there is more
680 stream.match( /^:*[*#]*/ );
681 return 'mw-indenting';
682 case ' ':
683 if ( stream.match( /^[\s\u00a0]*:*{\|/, false ) ) { // Leading spaces is the correct syntax for a table, bug T108454
684 stream.eatSpace();
685 if ( stream.match( /^:+/ ) ) { // ::{|
686 state.stack.push( state.tokenize );
687 state.tokenize = eatStartTable;
688 return 'mw-indenting';
689 }
690 stream.eat( '{' );
691 } else {
692 return 'mw-skipformatting';
693 }
694 // break is not necessary here
695 // falls through
696 case '{':
697 if ( stream.eat( '|' ) ) {
698 stream.eatSpace();
699 state.stack.push( state.tokenize );
700 state.tokenize = inTableDefinition;
701 return 'mw-table-bracket';
702 }
703 }
704 } else {
705 ch = stream.next();
706 }
707
708 switch ( ch ) {
709 case '&':
710 return makeStyle( eatMnemonic( stream, style, mnemonicStyle ), state );
711 case '\'':
712 if ( stream.match( /^'*(?=''''')/ ) || stream.match( /^'''(?!')/, false ) ) { // skip the irrelevant apostrophes ( >5 or =4 )
713 break;
714 }
715 if ( stream.match( '\'\'' ) ) { // bold
716 if ( !( firstsingleletterword || stream.match( '\'\'', false ) ) ) {
717 prepareItalicForCorrection( stream );
718 }
719 isBold = !isBold;
720 return makeLocalStyle( 'mw-apostrophes-bold', state );
721 } else if ( stream.eat( '\'' ) ) { // italic
722 isItalic = !isItalic;
723 return makeLocalStyle( 'mw-apostrophes-italic', state );
724 }
725 break;
726 case '[':
727 if ( stream.eat( '[' ) ) { // Link Example: [[ Foo | Bar ]]
728 stream.eatSpace();
729 if ( /[^\]|[]/.test( stream.peek() ) ) {
730 state.nLink++;
731 state.stack.push( state.tokenize );
732 state.tokenize = inLink;
733 return makeLocalStyle( 'mw-link-bracket', state );
734 }
735 } else {
736 mt = stream.match( urlProtocols );
737 if ( mt ) {
738 state.nLink++;
739 stream.backUp( mt[ 0 ].length );
740 state.stack.push( state.tokenize );
741 state.tokenize = eatExternalLinkProtocol( mt[ 0 ].length );
742 return makeLocalStyle( 'mw-extlink-bracket', state );
743 }
744 }
745 break;
746 case '{':
747 // Can't be a variable when it starts with more than 3 brackets (T108450) or
748 // a single { followed by a template. E.g. {{{!}} starts a table (T292967).
749 if ( stream.match( /^{{(?!{|[^{}]*}}(?!}))/ ) ) {
750 stream.eatSpace();
751 state.stack.push( state.tokenize );
752 state.tokenize = inVariable;
753 return makeLocalStyle( 'mw-templatevariable-bracket', state );
754 } else if ( stream.match( /^{(?!{(?!{))[\s\u00a0]*/ ) ) {
755 if ( stream.peek() === '#' ) { // Parser function
756 state.nExt++;
757 state.stack.push( state.tokenize );
758 state.tokenize = inParserFunctionName;
759 return makeLocalStyle( 'mw-parserfunction-bracket', state );
760 }
761 // Check for parser function without '#'
762
763 name = stream.match( /^([^\s\u00a0}[\]<{'|&:]+)(:|[\s\u00a0]*)(\}\}?)?(.)?/ );
764 if ( name ) {
765 stream.backUp( name[ 0 ].length );
766 if ( ( name[ 2 ] === ':' || name[ 4 ] === undefined || name[ 3 ] === '}}' ) && ( name[ 1 ].toLowerCase() in mwConfig.functionSynonyms[ 0 ] || name[ 1 ] in mwConfig.functionSynonyms[ 1 ] ) ) {
767 state.nExt++;
768 state.stack.push( state.tokenize );
769 state.tokenize = inParserFunctionName;
770 return makeLocalStyle( 'mw-parserfunction-bracket', state );
771 }
772 }
773 // Template
774 state.nTemplate++;
775 state.stack.push( state.tokenize );
776 state.tokenize = eatTemplatePageName( false );
777 return makeLocalStyle( 'mw-template-bracket', state );
778 }
779 break;
780 case '<':
781 isCloseTag = !!stream.eat( '/' );
782 tagname = stream.match( /^[^>/\s\u00a0.*,[\]{}$^+?|/\\'`~<=!@#%&()-]+/ );
783 if ( stream.match( '!--' ) ) { // comment
784 return chain( eatBlock( 'mw-comment', '-->' ) );
785 }
786 if ( tagname ) {
787 tagname = tagname[ 0 ].toLowerCase();
788 if ( tagname in mwConfig.tags ) { // Parser function
789 if ( isCloseTag === true ) {
790 // @todo message
791 return 'error';
792 }
793 stream.backUp( tagname.length );
794 state.stack.push( state.tokenize );
795 state.tokenize = eatTagName( tagname.length, isCloseTag, false );
796 return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + tagname, state );
797 }
798 if ( tagname in permittedHtmlTags ) { // Html tag
799 if ( isCloseTag === true && tagname !== state.InHtmlTag.pop() ) {
800 // @todo message
801 return 'error';
802 }
803 if ( isCloseTag === true && tagname in voidHtmlTags ) {
804 // @todo message
805 return 'error';
806 }
807 stream.backUp( tagname.length );
808 state.stack.push( state.tokenize );
809 // || ( tagname in voidHtmlTags ) because opening void tags should also be treated as the closing tag.
810 state.tokenize = eatTagName( tagname.length, isCloseTag || ( tagname in voidHtmlTags ), true );
811 return makeLocalStyle( 'mw-htmltag-bracket', state );
812 }
813 stream.backUp( tagname.length );
814 }
815 break;
816 case '~':
817 if ( stream.match( /^~{2,4}/ ) ) {
818 return 'mw-signature';
819 }
820 break;
821 case '_': // Maybe double undescored Magic Word as __TOC__
822 tmp = 1;
823 while ( stream.eat( '_' ) ) { // Optimize processing of many underscore symbols
824 tmp++;
825 }
826 if ( tmp > 2 ) { // Many underscore symbols
827 if ( !stream.eol() ) {
828 stream.backUp( 2 ); // Leave last two underscore symbols for processing again in next iteration
829 }
830 return makeStyle( style, state ); // Optimization: skip regex function at the end for EOL and backuped symbols
831 } else if ( tmp === 2 ) { // Check on double underscore Magic Word
832 name = stream.match( /^([^\s\u00a0>}[\]<{'|&:~]+?)__/ ); // The same as the end of function except '_' inside and with '__' at the end of string
833 if ( name && name[ 0 ] ) {
834 if ( '__' + name[ 0 ].toLowerCase() in mwConfig.doubleUnderscore[ 0 ] || '__' + name[ 0 ] in mwConfig.doubleUnderscore[ 1 ] ) {
835 return 'mw-doubleUnderscore';
836 }
837 if ( !stream.eol() ) {
838 stream.backUp( 2 ); // Two underscore symbols at the end can be begining of other double undescored Magic Word
839 }
840 return makeStyle( style, state ); // Optimization: skip regex function at the end for EOL and backuped symbols
841 }
842 }
843 break;
844 default:
845 if ( /[\s\u00a0]/.test( ch ) ) {
846 stream.eatSpace();
847 if ( stream.match( urlProtocols, false ) && !stream.match( '//' ) ) { // highlight free external links, bug T108448
848 state.stack.push( state.tokenize );
849 state.tokenize = eatFreeExternalLinkProtocol;
850 return makeStyle( style, state );
851 }
852 }
853 break;
854 }
855 stream.match( /^[^\s\u00a0_>}[\]<{'|&:~=]+/ );
856 return makeStyle( style, state );
857 };
858 }
859
860 /**
861 * Remembers position and status for rollbacking.
862 * It needed for change bold to italic with apostrophe before it if required
863 *
864 * see https://phabricator.wikimedia.org/T108455
865 *
866 * @param {Object} stream CodeMirror.StringStream
867 */
868 function prepareItalicForCorrection( stream ) {
869 // see Parser::doQuotes() in MediaWiki core, it works similar
870 // firstsingleletterword has maximum priority
871 // firstmultiletterword has medium priority
872 // firstspace has low priority
873 const end = stream.pos,
874 str = stream.string.slice( 0, end - 3 ),
875 x1 = str.slice( -1, -1 + 1 ),
876 x2 = str.slice( -2, -2 + 1 );
877
878 // firstsingleletterword olways is undefined here
879 if ( x1 === ' ' ) {
880 if ( firstmultiletterword || firstspace ) {
881 return;
882 }
883 firstspace = end;
884 } else if ( x2 === ' ' ) {
885 firstsingleletterword = end;
886 } else if ( firstmultiletterword ) {
887 return;
888 } else {
889 firstmultiletterword = end;
890 }
891 // remember bold and italic state for restore
892 mBold = isBold;
893 mItalic = isItalic;
894 }
895
896 return {
897 startState: function () {
898 return { tokenize: eatWikiText( '', '' ), stack: [], InHtmlTag: [], extName: false, extMode: false, extState: false, nTemplate: 0, nLink: 0, nExt: 0 };
899 },
900 copyState: function ( state ) {
901 return {
902 tokenize: state.tokenize,
903 stack: state.stack.concat( [] ),
904 InHtmlTag: state.InHtmlTag.concat( [] ),
905 extName: state.extName,
906 extMode: state.extMode,
907 extState: state.extMode !== false && CodeMirror.copyState( state.extMode, state.extState ),
908 nTemplate: state.nTemplate,
909 nLink: state.nLink,
910 nExt: state.nExt
911 };
912 },
913 token: function ( stream, state ) {
914 let style, p, t, f,
915 readyTokens = [],
916 tmpTokens = [];
917
918 if ( mTokens.length > 0 ) { // just send saved tokens till they exists
919 t = mTokens.shift();
920 stream.pos = t.pos;
921 state = t.state;
922 return t.style;
923 }
924
925 if ( stream.sol() ) { // reset bold and italic status in every new line
926 isBold = false;
927 isItalic = false;
928 firstsingleletterword = undefined;
929 firstmultiletterword = undefined;
930 firstspace = undefined;
931 }
932
933 do {
934 style = state.tokenize( stream, state ); // get token style
935 f = firstsingleletterword || firstmultiletterword || firstspace;
936 if ( f ) { // rollback point exists
937 if ( f !== p ) { // new rollbak point
938 p = f;
939 if ( tmpTokens.length > 0 ) { // it's not first rollbak point
940 readyTokens = readyTokens.concat( tmpTokens ); // save tokens
941 tmpTokens = [];
942 }
943 }
944 tmpTokens.push( { // save token
945 pos: stream.pos,
946 style: style,
947 state: CodeMirror.copyState( state.extMode ? state.extMode : 'mediawiki', state )
948 } );
949 } else { // rollback point not exists
950 mStyle = style; // remember style before possible rollback point
951 return style; // just return token style
952 }
953 } while ( !stream.eol() );
954
955 if ( isBold && isItalic ) { // needs to rollback
956 isItalic = mItalic; // restore status
957 isBold = mBold;
958 firstsingleletterword = undefined;
959 firstmultiletterword = undefined;
960 firstspace = undefined;
961 if ( readyTokens.length > 0 ) { // it contains tickets before the point of rollback
962 readyTokens[ readyTokens.length - 1 ].pos++; // add one apostrophe, next token will be italic (two apostrophes)
963 mTokens = readyTokens; // for sending tokens till the point of rollback
964 } else { // there are no tikets before the point of rollback
965 stream.pos = tmpTokens[ 0 ].pos - 2; // eat( '\'')
966 return mStyle; // send saved Style
967 }
968 } else { // not needs to rollback
969 mTokens = readyTokens.concat( tmpTokens ); // send all saved tokens
970 }
971 // return first saved token
972 t = mTokens.shift();
973 stream.pos = t.pos;
974 state = t.state;
975 return t.style;
976 },
977 blankLine: function ( state ) {
978 let ret;
979 if ( state.extName ) {
980 if ( state.extMode ) {
981 ret = '';
982 if ( state.extMode.blankLine ) {
983 ret = ' ' + state.extMode.blankLine( state.extState );
984 }
985 return 'line-cm-mw-tag-' + state.extName + ret;
986 }
987 return 'line-cm-mw-exttag';
988 }
989 }
990 };
991 } );
992
993 CodeMirror.defineMIME( 'text/mediawiki', 'mediawiki' );
994
995 function eatNowiki( style, lineStyle ) {
996 return function ( stream, state, ownLine ) {
997 if ( ownLine && stream.sol() ) {
998 state.ownLine = true;
999 } else if ( ownLine === false && state.ownLine ) {
1000 state.ownLine = false;
1001 }
1002 const s = ( state.ownLine ? lineStyle : style );
1003 if ( stream.match( /^[^&]+/ ) ) {
1004 return s;
1005 }
1006 stream.next(); // eat &
1007 return eatMnemonic( stream, s, s );
1008 };
1009 }
1010
1011 CodeMirror.defineMode( 'mw-tag-pre', ( /* config, parserConfig */ ) => ( {
1012 startState: function () {
1013 return {};
1014 },
1015 token: eatNowiki( 'mw-tag-pre', 'line-cm-mw-tag-pre' )
1016 } ) );
1017
1018 CodeMirror.defineMode( 'mw-tag-nowiki', ( /* config, parserConfig */ ) => ( {
1019 startState: function () {
1020 return {};
1021 },
1022 token: eatNowiki( 'mw-tag-nowiki', 'line-cm-mw-tag-nowiki' )
1023 } ) );
1024
1025 }( CodeMirror ) );