| 1 |
const { |
| 2 |
HighlightStyle, |
| 3 |
LanguageSupport, |
| 4 |
StreamLanguage, |
| 5 |
StreamParser, |
| 6 |
StringStream, |
| 7 |
Tag, |
| 8 |
syntaxHighlighting |
| 9 |
} = require( 'ext.CodeMirror.v6.lib' ); |
| 10 |
const mwModeConfig = require( './codemirror.mediawiki.config.js' ); |
| 11 |
const bidiIsolationExtension = require( './codemirror.mediawiki.bidiIsolation.js' ); |
| 12 |
const templateFoldingExtension = require( './codemirror.mediawiki.templateFolding.js' ); |
| 13 |
|
| 14 |
/** |
| 15 |
* MediaWiki language support for CodeMirror 6. |
| 16 |
* Adapted from the original CodeMirror 5 stream parser by Pavel Astakhov. |
| 17 |
* |
| 18 |
* @module CodeMirrorModeMediaWiki |
| 19 |
* |
| 20 |
* @example |
| 21 |
* mw.loader.using( [ |
| 22 |
* 'ext.CodeMirror.v6', |
| 23 |
* 'ext.CodeMirror.v6.mode.mediawiki' |
| 24 |
* ] ).then( ( require ) => { |
| 25 |
* const CodeMirror = require( 'ext.CodeMirror.v6' ); |
| 26 |
* const mediawikiLang = require( 'ext.CodeMirror.v6.mode.mediawiki' ); |
| 27 |
* const cm = new CodeMirror( myTextarea ); |
| 28 |
* cm.initialize( [ cm.defaultExtensions, mediawikiLang() ] ); |
| 29 |
* } ); |
| 30 |
*/ |
| 31 |
class CodeMirrorModeMediaWiki { |
| 32 |
/** |
| 33 |
* @param {Object} config MediaWiki configuration as generated by DataScript.php |
| 34 |
* @internal |
| 35 |
*/ |
| 36 |
constructor( config ) { |
| 37 |
this.config = config; |
| 38 |
|
| 39 |
this.urlProtocols = new RegExp( `^(?:${ this.config.urlProtocols })(?=[^\\s\u00a0{[\\]<>~).,'])`, 'i' ); |
| 40 |
this.isBold = false; |
| 41 |
this.wasBold = false; |
| 42 |
this.isItalic = false; |
| 43 |
this.wasItalic = false; |
| 44 |
this.firstSingleLetterWord = null; |
| 45 |
this.firstMultiLetterWord = null; |
| 46 |
this.firstSpace = null; |
| 47 |
this.oldStyle = null; |
| 48 |
this.tokens = []; |
| 49 |
this.oldTokens = []; |
| 50 |
this.tokenTable = mwModeConfig.tokenTable; |
| 51 |
this.registerGroundTokens(); |
| 52 |
|
| 53 |
// Dynamically register any tags that aren't already in CodeMirrorModeMediaWikiConfig |
| 54 |
Object.keys( this.config.tags ).forEach( ( tag ) => mwModeConfig.addTag( tag ) ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Register the ground tokens. These aren't referenced directly in the StreamParser, nor do |
| 59 |
* they have a parent Tag, so we don't need them as constants like we do for other tokens. |
| 60 |
* See this.makeLocalStyle() for how these tokens are used. |
| 61 |
* |
| 62 |
* @private |
| 63 |
*/ |
| 64 |
registerGroundTokens() { |
| 65 |
[ |
| 66 |
'mw-ext-ground', |
| 67 |
'mw-ext-link-ground', |
| 68 |
'mw-ext2-ground', |
| 69 |
'mw-ext2-link-ground', |
| 70 |
'mw-ext3-ground', |
| 71 |
'mw-ext3-link-ground', |
| 72 |
'mw-link-ground', |
| 73 |
'mw-template-ext-ground', |
| 74 |
'mw-template-ext-link-ground', |
| 75 |
'mw-template-ext2-ground', |
| 76 |
'mw-template-ext2-link-ground', |
| 77 |
'mw-template-ext3-ground', |
| 78 |
'mw-template-ext3-link-ground', |
| 79 |
'mw-template-ground', |
| 80 |
'mw-template-link-ground', |
| 81 |
'mw-template2-ext-ground', |
| 82 |
'mw-template2-ext-link-ground', |
| 83 |
'mw-template2-ext2-ground', |
| 84 |
'mw-template2-ext2-link-ground', |
| 85 |
'mw-template2-ext3-ground', |
| 86 |
'mw-template2-ext3-link-ground', |
| 87 |
'mw-template2-ground', |
| 88 |
'mw-template2-link-ground', |
| 89 |
'mw-template3-ext-ground', |
| 90 |
'mw-template3-ext-link-ground', |
| 91 |
'mw-template3-ext2-ground', |
| 92 |
'mw-template3-ext2-link-ground', |
| 93 |
'mw-template3-ext3-ground', |
| 94 |
'mw-template3-ext3-link-ground', |
| 95 |
'mw-template3-ground', |
| 96 |
'mw-template3-link-ground' |
| 97 |
].forEach( ( ground ) => mwModeConfig.addToken( ground ) ); |
| 98 |
} |
| 99 |
|
| 100 |
eatHtmlEntity( stream, style ) { |
| 101 |
let ok; |
| 102 |
if ( stream.eat( '#' ) ) { |
| 103 |
if ( stream.eat( 'x' ) ) { |
| 104 |
ok = stream.eatWhile( /[a-fA-F\d]/ ) && stream.eat( ';' ); |
| 105 |
} else { |
| 106 |
ok = stream.eatWhile( /[\d]/ ) && stream.eat( ';' ); |
| 107 |
} |
| 108 |
} else { |
| 109 |
ok = stream.eatWhile( /[\w.\-:]/ ) && stream.eat( ';' ); |
| 110 |
} |
| 111 |
if ( ok ) { |
| 112 |
return mwModeConfig.tags.htmlEntity; |
| 113 |
} |
| 114 |
return style; |
| 115 |
} |
| 116 |
|
| 117 |
isNested( state ) { |
| 118 |
return state.nExt > 0 || state.nTemplate > 0 || state.nLink > 0; |
| 119 |
} |
| 120 |
|
| 121 |
makeStyle( style, state, endGround ) { |
| 122 |
if ( this.isBold || state.nDt > 0 ) { |
| 123 |
style += ' ' + mwModeConfig.tags.strong; |
| 124 |
} |
| 125 |
if ( this.isItalic ) { |
| 126 |
style += ' ' + mwModeConfig.tags.em; |
| 127 |
} |
| 128 |
return this.makeLocalStyle( style, state, endGround ); |
| 129 |
} |
| 130 |
|
| 131 |
makeLocalStyle( style, state, endGround ) { |
| 132 |
let ground = ''; |
| 133 |
switch ( state.nTemplate ) { |
| 134 |
case 0: |
| 135 |
break; |
| 136 |
case 1: |
| 137 |
ground += '-template'; |
| 138 |
break; |
| 139 |
case 2: |
| 140 |
ground += '-template2'; |
| 141 |
break; |
| 142 |
default: |
| 143 |
ground += '-template3'; |
| 144 |
break; |
| 145 |
} |
| 146 |
switch ( state.nExt ) { |
| 147 |
case 0: |
| 148 |
break; |
| 149 |
case 1: |
| 150 |
ground += '-ext'; |
| 151 |
break; |
| 152 |
case 2: |
| 153 |
ground += '-ext2'; |
| 154 |
break; |
| 155 |
default: |
| 156 |
ground += '-ext3'; |
| 157 |
break; |
| 158 |
} |
| 159 |
if ( state.nLink > 0 ) { |
| 160 |
ground += '-link'; |
| 161 |
} |
| 162 |
if ( ground !== '' ) { |
| 163 |
style = `mw${ ground }-ground ${ style }`; |
| 164 |
} |
| 165 |
if ( endGround ) { |
| 166 |
state[ endGround ]--; |
| 167 |
} |
| 168 |
return style.trim(); |
| 169 |
} |
| 170 |
|
| 171 |
eatBlock( style, terminator, consumeLast ) { |
| 172 |
return ( stream, state ) => { |
| 173 |
if ( stream.skipTo( terminator ) ) { |
| 174 |
if ( consumeLast !== false ) { |
| 175 |
stream.match( terminator ); |
| 176 |
} |
| 177 |
state.tokenize = state.stack.pop(); |
| 178 |
} else { |
| 179 |
stream.skipToEnd(); |
| 180 |
} |
| 181 |
return this.makeLocalStyle( style, state ); |
| 182 |
}; |
| 183 |
} |
| 184 |
|
| 185 |
eatEnd( style ) { |
| 186 |
return ( stream, state ) => { |
| 187 |
stream.skipToEnd(); |
| 188 |
state.tokenize = state.stack.pop(); |
| 189 |
return this.makeLocalStyle( style, state ); |
| 190 |
}; |
| 191 |
} |
| 192 |
|
| 193 |
eatChar( char, style ) { |
| 194 |
return ( stream, state ) => { |
| 195 |
state.tokenize = state.stack.pop(); |
| 196 |
if ( stream.eat( char ) ) { |
| 197 |
return this.makeLocalStyle( style, state ); |
| 198 |
} |
| 199 |
return this.makeLocalStyle( mwModeConfig.tags.error, state ); |
| 200 |
}; |
| 201 |
} |
| 202 |
|
| 203 |
eatSectionHeader( count ) { |
| 204 |
return ( stream, state ) => { |
| 205 |
if ( stream.match( /^[^&<[{~]+/ ) ) { |
| 206 |
if ( stream.eol() ) { |
| 207 |
stream.backUp( count ); |
| 208 |
state.tokenize = this.eatEnd( mwModeConfig.tags.sectionHeader ); |
| 209 |
} else if ( stream.match( /^<!--(?!.*?-->.*?=)/, false ) ) { |
| 210 |
// T171074: handle trailing comments |
| 211 |
stream.backUp( count ); |
| 212 |
state.tokenize = this.eatBlock( mwModeConfig.tags.sectionHeader, '<!--', false ); |
| 213 |
} |
| 214 |
return mwModeConfig.tags.section; // style is null |
| 215 |
} |
| 216 |
return this.eatWikiText( mwModeConfig.tags.section )( stream, state ); |
| 217 |
}; |
| 218 |
} |
| 219 |
|
| 220 |
inVariable( stream, state ) { |
| 221 |
if ( stream.match( /^[^{}|]+/ ) ) { |
| 222 |
return this.makeLocalStyle( mwModeConfig.tags.templateVariableName, state ); |
| 223 |
} |
| 224 |
if ( stream.eat( '|' ) ) { |
| 225 |
state.tokenize = this.inVariableDefault.bind( this ); |
| 226 |
return this.makeLocalStyle( mwModeConfig.tags.templateVariableDelimiter, state ); |
| 227 |
} |
| 228 |
if ( stream.match( '}}}' ) ) { |
| 229 |
state.tokenize = state.stack.pop(); |
| 230 |
return this.makeLocalStyle( mwModeConfig.tags.templateVariableBracket, state ); |
| 231 |
} |
| 232 |
if ( stream.match( '{{{' ) ) { |
| 233 |
state.stack.push( state.tokenize ); |
| 234 |
return this.makeLocalStyle( mwModeConfig.tags.templateVariableBracket, state ); |
| 235 |
} |
| 236 |
stream.next(); |
| 237 |
return this.makeLocalStyle( mwModeConfig.tags.templateVariableName, state ); |
| 238 |
} |
| 239 |
|
| 240 |
inVariableDefault( stream, state ) { |
| 241 |
if ( stream.match( /^[^{}[<&~]+/ ) ) { |
| 242 |
return this.makeLocalStyle( mwModeConfig.tags.templateVariable, state ); |
| 243 |
} |
| 244 |
if ( stream.match( '}}}' ) ) { |
| 245 |
state.tokenize = state.stack.pop(); |
| 246 |
return this.makeLocalStyle( mwModeConfig.tags.templateVariableBracket, state ); |
| 247 |
} |
| 248 |
return this.eatWikiText( mwModeConfig.tags.templateVariable )( stream, state ); |
| 249 |
} |
| 250 |
|
| 251 |
inParserFunctionName( stream, state ) { |
| 252 |
// FIXME: {{#name}} and {{uc}} are wrong, must have ':' |
| 253 |
if ( stream.match( /^#?[^:}{~]+/ ) ) { |
| 254 |
return this.makeLocalStyle( mwModeConfig.tags.parserFunctionName, state ); |
| 255 |
} |
| 256 |
if ( stream.eat( ':' ) ) { |
| 257 |
state.tokenize = this.inParserFunctionArguments.bind( this ); |
| 258 |
return this.makeLocalStyle( mwModeConfig.tags.parserFunctionDelimiter, state ); |
| 259 |
} |
| 260 |
if ( stream.match( '}}' ) ) { |
| 261 |
state.tokenize = state.stack.pop(); |
| 262 |
return this.makeLocalStyle( mwModeConfig.tags.parserFunctionBracket, state, 'nExt' ); |
| 263 |
} |
| 264 |
return this.eatWikiText( mwModeConfig.tags.parserFunction )( stream, state ); |
| 265 |
} |
| 266 |
|
| 267 |
inParserFunctionArguments( stream, state ) { |
| 268 |
if ( stream.match( /^[^|}{[<&~]+/ ) ) { |
| 269 |
return this.makeLocalStyle( mwModeConfig.tags.parserFunction, state ); |
| 270 |
} else if ( stream.eat( '|' ) ) { |
| 271 |
return this.makeLocalStyle( mwModeConfig.tags.parserFunctionDelimiter, state ); |
| 272 |
} else if ( stream.match( '}}' ) ) { |
| 273 |
state.tokenize = state.stack.pop(); |
| 274 |
return this.makeLocalStyle( mwModeConfig.tags.parserFunctionBracket, state, 'nExt' ); |
| 275 |
} |
| 276 |
return this.eatWikiText( mwModeConfig.tags.parserFunction )( stream, state ); |
| 277 |
} |
| 278 |
|
| 279 |
eatTemplatePageName( haveAte ) { |
| 280 |
return ( stream, state ) => { |
| 281 |
if ( stream.match( /^[\s\u00a0]*\|[\s\u00a0]*/ ) ) { |
| 282 |
state.tokenize = this.eatTemplateArgument( true ); |
| 283 |
return this.makeLocalStyle( mwModeConfig.tags.templateDelimiter, state ); |
| 284 |
} |
| 285 |
if ( stream.match( /^[\s\u00a0]*\}\}/ ) ) { |
| 286 |
state.tokenize = state.stack.pop(); |
| 287 |
return this.makeLocalStyle( mwModeConfig.tags.templateBracket, state, 'nTemplate' ); |
| 288 |
} |
| 289 |
if ( stream.match( /^[\s\u00a0]*<!--.*?-->/ ) ) { |
| 290 |
return this.makeLocalStyle( mwModeConfig.tags.comment, state ); |
| 291 |
} |
| 292 |
if ( haveAte && stream.sol() ) { |
| 293 |
// @todo error message |
| 294 |
state.nTemplate--; |
| 295 |
state.tokenize = state.stack.pop(); |
| 296 |
return; |
| 297 |
} |
| 298 |
if ( stream.match( /^[\s\u00a0]*[^\s\u00a0|}<{&~]+/ ) ) { |
| 299 |
state.tokenize = this.eatTemplatePageName( true ); |
| 300 |
return this.makeLocalStyle( mwModeConfig.tags.templateName, state ); |
| 301 |
} else if ( stream.eatSpace() ) { |
| 302 |
if ( stream.eol() === true ) { |
| 303 |
return this.makeLocalStyle( mwModeConfig.tags.templateName, state ); |
| 304 |
} |
| 305 |
return this.makeLocalStyle( mwModeConfig.tags.templateName, state ); |
| 306 |
} |
| 307 |
return this.eatWikiText( mwModeConfig.tags.templateName )( stream, state ); |
| 308 |
}; |
| 309 |
} |
| 310 |
|
| 311 |
eatTemplateArgument( expectArgName ) { |
| 312 |
return ( stream, state ) => { |
| 313 |
if ( expectArgName && stream.eatWhile( /[^=|}{[<&~]/ ) ) { |
| 314 |
if ( stream.eat( '=' ) ) { |
| 315 |
state.tokenize = this.eatTemplateArgument( false ); |
| 316 |
return this.makeLocalStyle( mwModeConfig.tags.templateArgumentName, state ); |
| 317 |
} |
| 318 |
return this.makeLocalStyle( mwModeConfig.tags.template, state ); |
| 319 |
} else if ( stream.eatWhile( /[^|}{[<&~]/ ) ) { |
| 320 |
return this.makeLocalStyle( mwModeConfig.tags.template, state ); |
| 321 |
} else if ( stream.eat( '|' ) ) { |
| 322 |
state.tokenize = this.eatTemplateArgument( true ); |
| 323 |
return this.makeLocalStyle( mwModeConfig.tags.templateDelimiter, state ); |
| 324 |
} else if ( stream.match( '}}' ) ) { |
| 325 |
state.tokenize = state.stack.pop(); |
| 326 |
return this.makeLocalStyle( mwModeConfig.tags.templateBracket, state, 'nTemplate' ); |
| 327 |
} |
| 328 |
return this.eatWikiText( mwModeConfig.tags.template )( stream, state ); |
| 329 |
}; |
| 330 |
} |
| 331 |
|
| 332 |
eatExternalLinkProtocol( chars ) { |
| 333 |
return ( stream, state ) => { |
| 334 |
while ( chars > 0 ) { |
| 335 |
chars--; |
| 336 |
stream.next(); |
| 337 |
} |
| 338 |
if ( stream.eol() ) { |
| 339 |
state.nLink--; |
| 340 |
// @todo error message |
| 341 |
state.tokenize = state.stack.pop(); |
| 342 |
} else { |
| 343 |
state.tokenize = this.inExternalLink.bind( this ); |
| 344 |
} |
| 345 |
return this.makeLocalStyle( mwModeConfig.tags.extLinkProtocol, state ); |
| 346 |
}; |
| 347 |
} |
| 348 |
|
| 349 |
inExternalLink( stream, state ) { |
| 350 |
if ( stream.sol() ) { |
| 351 |
state.nLink--; |
| 352 |
// @todo error message |
| 353 |
state.tokenize = state.stack.pop(); |
| 354 |
return; |
| 355 |
} |
| 356 |
if ( stream.match( /^[\s\u00a0]*\]/ ) ) { |
| 357 |
state.tokenize = state.stack.pop(); |
| 358 |
return this.makeLocalStyle( mwModeConfig.tags.extLinkBracket, state, 'nLink' ); |
| 359 |
} |
| 360 |
if ( stream.eatSpace() ) { |
| 361 |
state.tokenize = this.inExternalLinkText.bind( this ); |
| 362 |
return this.makeStyle( '', state ); |
| 363 |
} |
| 364 |
if ( stream.match( /^[^\s\u00a0\]{&~']+/ ) || stream.eatSpace() ) { |
| 365 |
if ( stream.peek() === '\'' ) { |
| 366 |
if ( stream.match( '\'\'', false ) ) { |
| 367 |
state.tokenize = this.inExternalLinkText.bind( this ); |
| 368 |
} else { |
| 369 |
stream.next(); |
| 370 |
} |
| 371 |
} |
| 372 |
return this.makeStyle( mwModeConfig.tags.extLink, state ); |
| 373 |
} |
| 374 |
return this.eatWikiText( mwModeConfig.tags.extLink )( stream, state ); |
| 375 |
} |
| 376 |
|
| 377 |
inExternalLinkText( stream, state ) { |
| 378 |
if ( stream.sol() ) { |
| 379 |
state.nLink--; |
| 380 |
// @todo error message |
| 381 |
state.tokenize = state.stack.pop(); |
| 382 |
return; |
| 383 |
} |
| 384 |
if ( stream.eat( ']' ) ) { |
| 385 |
state.tokenize = state.stack.pop(); |
| 386 |
return this.makeLocalStyle( mwModeConfig.tags.extLinkBracket, state, 'nLink' ); |
| 387 |
} |
| 388 |
if ( stream.match( /^[^'\]{&~<]+/ ) ) { |
| 389 |
return this.makeStyle( mwModeConfig.tags.extLinkText, state ); |
| 390 |
} |
| 391 |
return this.eatWikiText( mwModeConfig.tags.extLinkText )( stream, state ); |
| 392 |
} |
| 393 |
|
| 394 |
inLink( stream, state ) { |
| 395 |
if ( stream.sol() ) { |
| 396 |
state.nLink--; |
| 397 |
// @todo error message |
| 398 |
state.tokenize = state.stack.pop(); |
| 399 |
return; |
| 400 |
} |
| 401 |
if ( stream.match( /^[\s\u00a0]*#[\s\u00a0]*/ ) ) { |
| 402 |
state.tokenize = this.inLinkToSection.bind( this ); |
| 403 |
return this.makeLocalStyle( mwModeConfig.tags.link, state ); |
| 404 |
} |
| 405 |
if ( stream.match( /^[\s\u00a0]*\|[\s\u00a0]*/ ) ) { |
| 406 |
state.tokenize = this.eatLinkText(); |
| 407 |
return this.makeLocalStyle( mwModeConfig.tags.linkDelimiter, state ); |
| 408 |
} |
| 409 |
if ( stream.match( /^[\s\u00a0]*\]\]/ ) ) { |
| 410 |
state.tokenize = state.stack.pop(); |
| 411 |
return this.makeLocalStyle( mwModeConfig.tags.linkBracket, state, 'nLink' ); |
| 412 |
} |
| 413 |
if ( stream.match( /^[\s\u00a0]*[^\s\u00a0#|\]&~{]+/ ) || stream.eatSpace() ) { |
| 414 |
return this.makeStyle( |
| 415 |
`${ mwModeConfig.tags.linkPageName } ${ mwModeConfig.tags.pageName }`, |
| 416 |
state |
| 417 |
); |
| 418 |
} |
| 419 |
return this.eatWikiText( |
| 420 |
`${ mwModeConfig.tags.linkPageName } ${ mwModeConfig.tags.pageName }` |
| 421 |
)( stream, state ); |
| 422 |
} |
| 423 |
|
| 424 |
inLinkToSection( stream, state ) { |
| 425 |
if ( stream.sol() ) { |
| 426 |
// @todo error message |
| 427 |
state.nLink--; |
| 428 |
state.tokenize = state.stack.pop(); |
| 429 |
return; |
| 430 |
} |
| 431 |
// FIXME '{{' breaks links, example: [[z{{page]] |
| 432 |
if ( stream.match( /^[^|\]&~{}]+/ ) ) { |
| 433 |
return this.makeLocalStyle( mwModeConfig.tags.linkToSection, state ); |
| 434 |
} |
| 435 |
if ( stream.eat( '|' ) ) { |
| 436 |
state.tokenize = this.eatLinkText(); |
| 437 |
return this.makeLocalStyle( mwModeConfig.tags.linkDelimiter, state ); |
| 438 |
} |
| 439 |
if ( stream.match( ']]' ) ) { |
| 440 |
state.tokenize = state.stack.pop(); |
| 441 |
return this.makeLocalStyle( mwModeConfig.tags.linkBracket, state, 'nLink' ); |
| 442 |
} |
| 443 |
return this.eatWikiText( mwModeConfig.tags.linkToSection )( stream, state ); |
| 444 |
} |
| 445 |
|
| 446 |
eatLinkText() { |
| 447 |
let linkIsBold, linkIsItalic; |
| 448 |
return ( stream, state ) => { |
| 449 |
let tmpstyle; |
| 450 |
if ( stream.match( ']]' ) ) { |
| 451 |
state.tokenize = state.stack.pop(); |
| 452 |
return this.makeLocalStyle( mwModeConfig.tags.linkBracket, state, 'nLink' ); |
| 453 |
} |
| 454 |
if ( stream.match( '\'\'\'' ) ) { |
| 455 |
linkIsBold = !linkIsBold; |
| 456 |
return this.makeLocalStyle( |
| 457 |
`${ mwModeConfig.tags.linkText } ${ mwModeConfig.tags.apostrophes }`, |
| 458 |
state |
| 459 |
); |
| 460 |
} |
| 461 |
if ( stream.match( '\'\'' ) ) { |
| 462 |
linkIsItalic = !linkIsItalic; |
| 463 |
return this.makeLocalStyle( |
| 464 |
`${ mwModeConfig.tags.linkText } ${ mwModeConfig.tags.apostrophes }`, |
| 465 |
state |
| 466 |
); |
| 467 |
} |
| 468 |
tmpstyle = mwModeConfig.tags.linkText; |
| 469 |
if ( linkIsBold ) { |
| 470 |
tmpstyle += ' ' + mwModeConfig.tags.strong; |
| 471 |
} |
| 472 |
if ( linkIsItalic ) { |
| 473 |
tmpstyle += ' ' + mwModeConfig.tags.em; |
| 474 |
} |
| 475 |
if ( stream.match( /^[^'\]{&~<]+/ ) ) { |
| 476 |
return this.makeStyle( tmpstyle, state ); |
| 477 |
} |
| 478 |
return this.eatWikiText( tmpstyle )( stream, state ); |
| 479 |
}; |
| 480 |
} |
| 481 |
|
| 482 |
eatTagName( chars, isCloseTag, isHtmlTag ) { |
| 483 |
return ( stream, state ) => { |
| 484 |
let name = ''; |
| 485 |
while ( chars > 0 ) { |
| 486 |
chars--; |
| 487 |
name = name + stream.next(); |
| 488 |
} |
| 489 |
stream.eatSpace(); |
| 490 |
name = name.toLowerCase(); |
| 491 |
|
| 492 |
if ( isHtmlTag ) { |
| 493 |
if ( isCloseTag && !mwModeConfig.implicitlyClosedHtmlTags[ name ] ) { |
| 494 |
state.tokenize = this.eatChar( '>', mwModeConfig.tags.htmlTagBracket ); |
| 495 |
} else { |
| 496 |
state.tokenize = this.eatHtmlTagAttribute( name ); |
| 497 |
} |
| 498 |
return this.makeLocalStyle( mwModeConfig.tags.htmlTagName, state ); |
| 499 |
} |
| 500 |
// it is the extension tag |
| 501 |
if ( isCloseTag ) { |
| 502 |
state.tokenize = this.eatChar( |
| 503 |
'>', |
| 504 |
`${ mwModeConfig.tags.extTagBracket } mw-ext-${ name }` |
| 505 |
); |
| 506 |
} else { |
| 507 |
state.tokenize = this.eatExtTagAttribute( name ); |
| 508 |
} |
| 509 |
return this.makeLocalStyle( `${ mwModeConfig.tags.extTagName } mw-ext-${ name }`, state ); |
| 510 |
}; |
| 511 |
} |
| 512 |
|
| 513 |
eatHtmlTagAttribute( name ) { |
| 514 |
return ( stream, state ) => { |
| 515 |
|
| 516 |
if ( stream.match( /^(?:"[^<">]*"|'[^<'>]*'|[^>/<{&~])+/ ) ) { |
| 517 |
return this.makeLocalStyle( mwModeConfig.tags.htmlTagAttribute, state ); |
| 518 |
} |
| 519 |
if ( stream.eat( '>' ) ) { |
| 520 |
if ( !( name in mwModeConfig.implicitlyClosedHtmlTags ) ) { |
| 521 |
state.inHtmlTag.push( name ); |
| 522 |
} |
| 523 |
state.tokenize = state.stack.pop(); |
| 524 |
return this.makeLocalStyle( mwModeConfig.tags.htmlTagBracket, state ); |
| 525 |
} |
| 526 |
if ( stream.match( '/>' ) ) { |
| 527 |
state.tokenize = state.stack.pop(); |
| 528 |
return this.makeLocalStyle( mwModeConfig.tags.htmlTagBracket, state ); |
| 529 |
} |
| 530 |
return this.eatWikiText( mwModeConfig.tags.htmlTagAttribute )( stream, state ); |
| 531 |
}; |
| 532 |
} |
| 533 |
|
| 534 |
eatNowiki() { |
| 535 |
return ( stream ) => { |
| 536 |
if ( stream.match( /^[^&]+/ ) ) { |
| 537 |
return ''; |
| 538 |
} |
| 539 |
// eat & |
| 540 |
stream.next(); |
| 541 |
return this.eatHtmlEntity( stream, '' ); |
| 542 |
}; |
| 543 |
} |
| 544 |
|
| 545 |
eatExtTagAttribute( name ) { |
| 546 |
return ( stream, state ) => { |
| 547 |
|
| 548 |
if ( stream.match( /^(?:"[^">]*"|'[^'>]*'|[^>/<{&~])+/ ) ) { |
| 549 |
return this.makeLocalStyle( `${ mwModeConfig.tags.extTagAttribute } mw-ext-${ name }`, state ); |
| 550 |
} |
| 551 |
if ( stream.eat( '>' ) ) { |
| 552 |
state.extName = name; |
| 553 |
|
| 554 |
// FIXME: remove nowiki and pre from TagModes in extension.json after CM6 upgrade |
| 555 |
// leverage the tagModes system for <nowiki> and <pre> |
| 556 |
if ( name === 'nowiki' || name === 'pre' ) { |
| 557 |
// There's no actual processing within these tags (apart from HTML entities), |
| 558 |
// so startState and copyState can be no-ops. |
| 559 |
state.extMode = { |
| 560 |
startState: () => {}, |
| 561 |
copyState: () => {}, |
| 562 |
token: this.eatNowiki() |
| 563 |
}; |
| 564 |
} else if ( name in this.config.tagModes ) { |
| 565 |
const mode = this.config.tagModes[ name ]; |
| 566 |
if ( mode === 'mediawiki' || mode === 'text/mediawiki' ) { |
| 567 |
state.extMode = this.mediawiki; |
| 568 |
state.extState = state.extMode.startState(); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
state.tokenize = this.eatExtTagArea( name ); |
| 573 |
return this.makeLocalStyle( `${ mwModeConfig.tags.extTagBracket } mw-ext-${ name }`, state ); |
| 574 |
} |
| 575 |
if ( stream.match( '/>' ) ) { |
| 576 |
state.tokenize = state.stack.pop(); |
| 577 |
return this.makeLocalStyle( `${ mwModeConfig.tags.extTagBracket } mw-ext-${ name }`, state ); |
| 578 |
} |
| 579 |
return this.eatWikiText( `${ mwModeConfig.tags.extTagAttribute } mw-ext-${ name }` )( stream, state ); |
| 580 |
}; |
| 581 |
} |
| 582 |
|
| 583 |
eatExtTagArea( name ) { |
| 584 |
return ( stream, state ) => { |
| 585 |
const from = stream.pos, |
| 586 |
|
| 587 |
pattern = new RegExp( `</${ name }\\s*>`, 'i' ), |
| 588 |
m = pattern.exec( from ? stream.string.slice( from ) : stream.string ); |
| 589 |
let origString = false, |
| 590 |
to; |
| 591 |
|
| 592 |
if ( m ) { |
| 593 |
if ( m.index === 0 ) { |
| 594 |
state.tokenize = this.eatExtCloseTag( name ); |
| 595 |
state.extName = false; |
| 596 |
if ( state.extMode !== false ) { |
| 597 |
state.extMode = false; |
| 598 |
state.extState = false; |
| 599 |
} |
| 600 |
return state.tokenize( stream, state ); |
| 601 |
} |
| 602 |
to = m.index + from; |
| 603 |
origString = stream.string; |
| 604 |
stream.string = origString.slice( 0, to ); |
| 605 |
} |
| 606 |
|
| 607 |
state.stack.push( state.tokenize ); |
| 608 |
state.tokenize = this.eatExtTokens( origString ); |
| 609 |
return state.tokenize( stream, state ); |
| 610 |
}; |
| 611 |
} |
| 612 |
|
| 613 |
eatExtCloseTag( name ) { |
| 614 |
return ( stream, state ) => { |
| 615 |
stream.next(); // eat < |
| 616 |
stream.next(); // eat / |
| 617 |
state.tokenize = this.eatTagName( name.length, true, false ); |
| 618 |
return this.makeLocalStyle( `${ mwModeConfig.tags.extTagBracket } mw-ext-${ name }`, state ); |
| 619 |
}; |
| 620 |
} |
| 621 |
|
| 622 |
eatExtTokens( origString ) { |
| 623 |
return ( stream, state ) => { |
| 624 |
let ret; |
| 625 |
if ( state.extMode === false ) { |
| 626 |
ret = mwModeConfig.tags.extTag; |
| 627 |
stream.skipToEnd(); |
| 628 |
} else { |
| 629 |
ret = `mw-tag-${ state.extName } ` + |
| 630 |
state.extMode.token( stream, state.extState, origString === false ); |
| 631 |
} |
| 632 |
if ( stream.eol() ) { |
| 633 |
if ( origString !== false ) { |
| 634 |
stream.string = origString; |
| 635 |
} |
| 636 |
state.tokenize = state.stack.pop(); |
| 637 |
} |
| 638 |
return this.makeLocalStyle( ret, state ); |
| 639 |
}; |
| 640 |
} |
| 641 |
|
| 642 |
eatStartTable( stream, state ) { |
| 643 |
stream.match( '{|' ); |
| 644 |
stream.eatSpace(); |
| 645 |
state.tokenize = this.inTableDefinition.bind( this ); |
| 646 |
return mwModeConfig.tags.tableBracket; |
| 647 |
} |
| 648 |
|
| 649 |
inTableDefinition( stream, state ) { |
| 650 |
if ( stream.sol() ) { |
| 651 |
state.tokenize = this.inTable.bind( this ); |
| 652 |
return this.inTable( stream, state ); |
| 653 |
} |
| 654 |
return this.eatWikiText( mwModeConfig.tags.tableDefinition )( stream, state ); |
| 655 |
} |
| 656 |
|
| 657 |
inTable( stream, state ) { |
| 658 |
if ( stream.sol() ) { |
| 659 |
stream.eatSpace(); |
| 660 |
if ( stream.eat( '|' ) ) { |
| 661 |
if ( stream.eat( '-' ) ) { |
| 662 |
stream.eatSpace(); |
| 663 |
state.tokenize = this.inTableDefinition.bind( this ); |
| 664 |
return this.makeLocalStyle( mwModeConfig.tags.tableDelimiter, state ); |
| 665 |
} |
| 666 |
if ( stream.eat( '+' ) ) { |
| 667 |
stream.eatSpace(); |
| 668 |
state.tokenize = this.eatTableRow( true, false, true ); |
| 669 |
return this.makeLocalStyle( mwModeConfig.tags.tableDelimiter, state ); |
| 670 |
} |
| 671 |
if ( stream.eat( '}' ) ) { |
| 672 |
state.tokenize = state.stack.pop(); |
| 673 |
return this.makeLocalStyle( mwModeConfig.tags.tableBracket, state ); |
| 674 |
} |
| 675 |
stream.eatSpace(); |
| 676 |
state.tokenize = this.eatTableRow( true, false ); |
| 677 |
return this.makeLocalStyle( mwModeConfig.tags.tableDelimiter, state ); |
| 678 |
} |
| 679 |
if ( stream.eat( '!' ) ) { |
| 680 |
stream.eatSpace(); |
| 681 |
state.tokenize = this.eatTableRow( true, true ); |
| 682 |
return this.makeLocalStyle( mwModeConfig.tags.tableDelimiter, state ); |
| 683 |
} |
| 684 |
} |
| 685 |
return this.eatWikiText( '' )( stream, state ); |
| 686 |
} |
| 687 |
|
| 688 |
// isStart actually means whether there may be attributes */ |
| 689 |
eatTableRow( isStart, isHead, isCaption ) { |
| 690 |
let tag = ''; |
| 691 |
if ( isCaption ) { |
| 692 |
tag = mwModeConfig.tags.tableCaption; |
| 693 |
} else if ( isHead ) { |
| 694 |
tag = mwModeConfig.tags.strong; |
| 695 |
} |
| 696 |
return ( stream, state ) => { |
| 697 |
if ( stream.sol() ) { |
| 698 |
if ( stream.match( /^[\s\u00a0]*[|!]/, false ) ) { |
| 699 |
state.tokenize = this.inTable.bind( this ); |
| 700 |
return this.inTable( stream, state ); |
| 701 |
} |
| 702 |
} else { |
| 703 |
if ( stream.match( /^[^'|{[<&~!]+/ ) ) { |
| 704 |
return this.makeStyle( tag, state ); |
| 705 |
} |
| 706 |
if ( stream.match( '||' ) || ( isHead && stream.match( '!!' ) ) ) { |
| 707 |
this.isBold = false; |
| 708 |
this.isItalic = false; |
| 709 |
state.tokenize = this.eatTableRow( true, isHead, isCaption ); |
| 710 |
return this.makeLocalStyle( mwModeConfig.tags.tableDelimiter, state ); |
| 711 |
} |
| 712 |
if ( isStart && stream.eat( '|' ) ) { |
| 713 |
state.tokenize = this.eatTableRow( false, isHead, isCaption ); |
| 714 |
return this.makeLocalStyle( mwModeConfig.tags.tableDelimiter, state ); |
| 715 |
} |
| 716 |
} |
| 717 |
return this.eatWikiText( tag )( stream, state ); |
| 718 |
}; |
| 719 |
} |
| 720 |
|
| 721 |
eatFreeExternalLinkProtocol( stream, state ) { |
| 722 |
stream.match( this.urlProtocols ); |
| 723 |
state.tokenize = this.eatFreeExternalLink.bind( this ); |
| 724 |
return this.makeLocalStyle( mwModeConfig.tags.freeExtLinkProtocol, state ); |
| 725 |
} |
| 726 |
|
| 727 |
eatFreeExternalLink( stream, state ) { |
| 728 |
if ( stream.sol() ) { |
| 729 |
// @todo error message |
| 730 |
} else if ( stream.match( /^[^\s\u00a0{[\]<>~).,']*/ ) ) { |
| 731 |
if ( stream.peek() === '~' ) { |
| 732 |
if ( !stream.match( /^~~~+/, false ) ) { |
| 733 |
stream.match( /^~*/ ); |
| 734 |
return this.makeLocalStyle( mwModeConfig.tags.freeExtLink, state ); |
| 735 |
} |
| 736 |
} else if ( stream.peek() === '{' ) { |
| 737 |
if ( !stream.match( '{{', false ) ) { |
| 738 |
stream.next(); |
| 739 |
return this.makeLocalStyle( mwModeConfig.tags.freeExtLink, state ); |
| 740 |
} |
| 741 |
} else if ( stream.peek() === '\'' ) { |
| 742 |
if ( !stream.match( '\'\'', false ) ) { |
| 743 |
stream.next(); |
| 744 |
return this.makeLocalStyle( mwModeConfig.tags.freeExtLink, state ); |
| 745 |
} |
| 746 |
} else if ( stream.match( /^[).,]+(?=[^\s\u00a0{[\]<>~).,])/ ) ) { |
| 747 |
return this.makeLocalStyle( mwModeConfig.tags.freeExtLink, state ); |
| 748 |
} |
| 749 |
} |
| 750 |
state.tokenize = state.stack.pop(); |
| 751 |
return this.makeLocalStyle( mwModeConfig.tags.freeExtLink, state ); |
| 752 |
} |
| 753 |
|
| 754 |
eatList( stream, state ) { |
| 755 |
// Just consume all nested list and indention syntax when there is more |
| 756 |
const mt = stream.match( /^[*#;:]*/u ); |
| 757 |
if ( mt && !this.isNested( state ) && mt[ 0 ].includes( ';' ) ) { |
| 758 |
state.nDt += mt[ 0 ].split( ';' ).length - 1; |
| 759 |
} |
| 760 |
return this.makeLocalStyle( mwModeConfig.tags.list, state ); |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
* @param {string} style |
| 765 |
* @return {string|Function} |
| 766 |
* @private |
| 767 |
*/ |
| 768 |
eatWikiText( style ) { |
| 769 |
return ( stream, state ) => { |
| 770 |
let ch, tmp, mt, name, isCloseTag, tagname; |
| 771 |
const sol = stream.sol(); |
| 772 |
|
| 773 |
function chain( parser ) { |
| 774 |
state.stack.push( state.tokenize ); |
| 775 |
state.tokenize = parser; |
| 776 |
return parser( stream, state ); |
| 777 |
} |
| 778 |
|
| 779 |
if ( sol ) { |
| 780 |
// highlight free external links, see T108448 |
| 781 |
if ( !stream.match( '//', false ) && stream.match( this.urlProtocols ) ) { |
| 782 |
state.stack.push( state.tokenize ); |
| 783 |
state.tokenize = this.eatFreeExternalLink.bind( this ); |
| 784 |
return this.makeLocalStyle( mwModeConfig.tags.freeExtLinkProtocol, state ); |
| 785 |
} |
| 786 |
ch = stream.next(); |
| 787 |
switch ( ch ) { |
| 788 |
case '-': |
| 789 |
if ( stream.match( /^---+/ ) ) { |
| 790 |
return mwModeConfig.tags.hr; |
| 791 |
} |
| 792 |
break; |
| 793 |
case '=': |
| 794 |
|
| 795 |
tmp = stream.match( /^(={0,5})(.+?(=\1\s*)(<!--(?!.*-->.*\S).*?)?)$/ ); |
| 796 |
// Title |
| 797 |
if ( tmp ) { |
| 798 |
stream.backUp( tmp[ 2 ].length ); |
| 799 |
state.stack.push( state.tokenize ); |
| 800 |
state.tokenize = this.eatSectionHeader( tmp[ 3 ].length ); |
| 801 |
return mwModeConfig.tags.sectionHeader + ' ' + |
| 802 |
/** |
| 803 |
* Tokens used here include: |
| 804 |
* - cm-mw-section-1 |
| 805 |
* - cm-mw-section-2 |
| 806 |
* - cm-mw-section-3 |
| 807 |
* - cm-mw-section-4 |
| 808 |
* - cm-mw-section-5 |
| 809 |
* - cm-mw-section-6 |
| 810 |
*/ |
| 811 |
mwModeConfig.tags[ `sectionHeader${ tmp[ 1 ].length + 1 }` ]; |
| 812 |
} |
| 813 |
break; |
| 814 |
case ';': |
| 815 |
stream.backUp( 1 ); |
| 816 |
// fall through |
| 817 |
case '*': |
| 818 |
case '#': |
| 819 |
return this.eatList( stream, state ); |
| 820 |
case ':': |
| 821 |
// Highlight indented tables :{|, bug T108454 |
| 822 |
if ( stream.match( /^:*[\s\u00a0]*(?={\|)/ ) ) { |
| 823 |
state.stack.push( state.tokenize ); |
| 824 |
state.tokenize = this.eatStartTable.bind( this ); |
| 825 |
return mwModeConfig.tags.indenting; |
| 826 |
} |
| 827 |
return this.eatList( stream, state ); |
| 828 |
case ' ': |
| 829 |
// Leading spaces is valid syntax for tables, bug T108454 |
| 830 |
if ( stream.match( /^[\s\u00a0]*(?::+[\s\u00a0]*)?{\|/, false ) ) { |
| 831 |
stream.eatSpace(); |
| 832 |
if ( stream.match( /^:+/ ) ) { // ::{| |
| 833 |
stream.eatSpace(); |
| 834 |
state.stack.push( state.tokenize ); |
| 835 |
state.tokenize = this.eatStartTable.bind( this ); |
| 836 |
return mwModeConfig.tags.indenting; |
| 837 |
} |
| 838 |
stream.eat( '{' ); |
| 839 |
} else { |
| 840 |
return mwModeConfig.tags.skipFormatting; |
| 841 |
} |
| 842 |
// break is not necessary here |
| 843 |
// falls through |
| 844 |
case '{': |
| 845 |
if ( stream.eat( '|' ) ) { |
| 846 |
stream.eatSpace(); |
| 847 |
state.stack.push( state.tokenize ); |
| 848 |
state.tokenize = this.inTableDefinition.bind( this ); |
| 849 |
return mwModeConfig.tags.tableBracket; |
| 850 |
} |
| 851 |
} |
| 852 |
} else { |
| 853 |
ch = stream.next(); |
| 854 |
} |
| 855 |
|
| 856 |
switch ( ch ) { |
| 857 |
case '&': |
| 858 |
return this.makeStyle( |
| 859 |
this.eatHtmlEntity( stream, style ), |
| 860 |
state |
| 861 |
); |
| 862 |
case '\'': |
| 863 |
// skip the irrelevant apostrophes ( >5 or =4 ) |
| 864 |
if ( stream.match( /^'*(?=''''')/ ) || stream.match( /^'''(?!')/, false ) ) { |
| 865 |
break; |
| 866 |
} |
| 867 |
if ( stream.match( '\'\'' ) ) { // bold |
| 868 |
if ( !( this.firstSingleLetterWord || stream.match( '\'\'', false ) ) ) { |
| 869 |
this.prepareItalicForCorrection( stream ); |
| 870 |
} |
| 871 |
this.isBold = !this.isBold; |
| 872 |
return this.makeLocalStyle( mwModeConfig.tags.apostrophesBold, state ); |
| 873 |
} else if ( stream.eat( '\'' ) ) { // italic |
| 874 |
this.isItalic = !this.isItalic; |
| 875 |
return this.makeLocalStyle( mwModeConfig.tags.apostrophesItalic, state ); |
| 876 |
} |
| 877 |
break; |
| 878 |
case '[': |
| 879 |
if ( stream.eat( '[' ) ) { // Link Example: [[ Foo | Bar ]] |
| 880 |
stream.eatSpace(); |
| 881 |
if ( /[^\]|[]/.test( stream.peek() ) ) { |
| 882 |
state.nLink++; |
| 883 |
state.stack.push( state.tokenize ); |
| 884 |
state.tokenize = this.inLink.bind( this ); |
| 885 |
return this.makeLocalStyle( mwModeConfig.tags.linkBracket, state ); |
| 886 |
} |
| 887 |
} else { |
| 888 |
mt = stream.match( this.urlProtocols ); |
| 889 |
if ( mt ) { |
| 890 |
state.nLink++; |
| 891 |
stream.backUp( mt[ 0 ].length ); |
| 892 |
state.stack.push( state.tokenize ); |
| 893 |
state.tokenize = this.eatExternalLinkProtocol( mt[ 0 ].length ); |
| 894 |
return this.makeLocalStyle( mwModeConfig.tags.extLinkBracket, state ); |
| 895 |
} |
| 896 |
} |
| 897 |
break; |
| 898 |
case '{': |
| 899 |
// Can't be a variable when it starts with more than 3 brackets (T108450) or |
| 900 |
// a single { followed by a template. E.g. {{{!}} starts a table (T292967). |
| 901 |
if ( stream.match( /^{{(?!{|[^{}]*}}(?!}))/ ) ) { |
| 902 |
stream.eatSpace(); |
| 903 |
state.stack.push( state.tokenize ); |
| 904 |
state.tokenize = this.inVariable.bind( this ); |
| 905 |
return this.makeLocalStyle( |
| 906 |
mwModeConfig.tags.templateVariableBracket, |
| 907 |
state |
| 908 |
); |
| 909 |
} else if ( stream.match( /^{(?!{(?!{))[\s\u00a0]*/ ) ) { |
| 910 |
// Parser function |
| 911 |
if ( stream.peek() === '#' ) { |
| 912 |
state.nExt++; |
| 913 |
state.stack.push( state.tokenize ); |
| 914 |
state.tokenize = this.inParserFunctionName.bind( this ); |
| 915 |
return this.makeLocalStyle( |
| 916 |
mwModeConfig.tags.parserFunctionBracket, |
| 917 |
state |
| 918 |
); |
| 919 |
} |
| 920 |
// Check for parser function without '#' |
| 921 |
name = stream.match( /^([^}[\]<{|:]+)(.)?/, false ); |
| 922 |
if ( name ) { |
| 923 |
const [ , f, delimiter ] = name, |
| 924 |
ff = delimiter === ':' ? f : f.trim(), |
| 925 |
ffLower = ff.toLowerCase(), |
| 926 |
{ config: { functionSynonyms } } = this; |
| 927 |
if ( |
| 928 |
( !delimiter || delimiter === ':' || delimiter === '}' ) && |
| 929 |
( |
| 930 |
Object.prototype.hasOwnProperty.call( |
| 931 |
functionSynonyms[ 0 ], ffLower |
| 932 |
) || |
| 933 |
Object.prototype.hasOwnProperty.call( |
| 934 |
functionSynonyms[ 1 ], ff |
| 935 |
) |
| 936 |
) |
| 937 |
) { |
| 938 |
state.nExt++; |
| 939 |
state.stack.push( state.tokenize ); |
| 940 |
state.tokenize = this.inParserFunctionName.bind( this ); |
| 941 |
return this.makeLocalStyle( |
| 942 |
mwModeConfig.tags.parserFunctionBracket, |
| 943 |
state |
| 944 |
); |
| 945 |
} |
| 946 |
} |
| 947 |
// Template |
| 948 |
state.nTemplate++; |
| 949 |
state.stack.push( state.tokenize ); |
| 950 |
state.tokenize = this.eatTemplatePageName( false ); |
| 951 |
return this.makeLocalStyle( mwModeConfig.tags.templateBracket, state ); |
| 952 |
} |
| 953 |
break; |
| 954 |
case '<': |
| 955 |
if ( stream.match( '!--' ) ) { // comment |
| 956 |
return chain( this.eatBlock( mwModeConfig.tags.comment, '-->' ) ); |
| 957 |
} |
| 958 |
isCloseTag = !!stream.eat( '/' ); |
| 959 |
tagname = stream.match( /^[a-z][^>/\s\u00a0]*/i ); |
| 960 |
if ( tagname ) { |
| 961 |
tagname = tagname[ 0 ].toLowerCase(); |
| 962 |
if ( tagname in this.config.tags ) { |
| 963 |
// Parser function |
| 964 |
if ( isCloseTag === true ) { |
| 965 |
return mwModeConfig.tags.error; |
| 966 |
} |
| 967 |
stream.backUp( tagname.length ); |
| 968 |
state.stack.push( state.tokenize ); |
| 969 |
state.tokenize = this.eatTagName( tagname.length, isCloseTag, false ); |
| 970 |
return this.makeLocalStyle( `${ mwModeConfig.tags.extTagBracket } mw-ext-${ tagname }`, state ); |
| 971 |
} |
| 972 |
if ( tagname in mwModeConfig.permittedHtmlTags ) { |
| 973 |
// Html tag |
| 974 |
if ( isCloseTag === true && tagname !== state.inHtmlTag.pop() ) { |
| 975 |
// Increment position so that the closing '>' gets highlighted red. |
| 976 |
stream.pos++; |
| 977 |
return mwModeConfig.tags.error; |
| 978 |
} |
| 979 |
if ( |
| 980 |
isCloseTag === true && |
| 981 |
tagname in mwModeConfig.implicitlyClosedHtmlTags |
| 982 |
) { |
| 983 |
return mwModeConfig.tags.error; |
| 984 |
} |
| 985 |
stream.backUp( tagname.length ); |
| 986 |
state.stack.push( state.tokenize ); |
| 987 |
state.tokenize = this.eatTagName( |
| 988 |
tagname.length, |
| 989 |
// Opening void tags should also be treated as the closing tag. |
| 990 |
isCloseTag || |
| 991 |
( tagname in mwModeConfig.implicitlyClosedHtmlTags ), |
| 992 |
true |
| 993 |
); |
| 994 |
return this.makeLocalStyle( mwModeConfig.tags.htmlTagBracket, state ); |
| 995 |
} |
| 996 |
stream.backUp( tagname.length ); |
| 997 |
} |
| 998 |
break; |
| 999 |
case '~': |
| 1000 |
if ( stream.match( /^~{2,4}/ ) ) { |
| 1001 |
return mwModeConfig.tags.signature; |
| 1002 |
} |
| 1003 |
break; |
| 1004 |
// Maybe double underscored Magic Word such as __TOC__ |
| 1005 |
case '_': |
| 1006 |
tmp = 1; |
| 1007 |
// Optimize processing of many underscore symbols |
| 1008 |
while ( stream.eat( '_' ) ) { |
| 1009 |
tmp++; |
| 1010 |
} |
| 1011 |
// Many underscore symbols |
| 1012 |
if ( tmp > 2 ) { |
| 1013 |
if ( !stream.eol() ) { |
| 1014 |
// Leave last two underscore symbols for processing in next iteration |
| 1015 |
stream.backUp( 2 ); |
| 1016 |
} |
| 1017 |
// Optimization: skip regex function for EOL and backup-ed symbols |
| 1018 |
return this.makeStyle( style, state ); |
| 1019 |
// Check on double underscore Magic Word |
| 1020 |
} else if ( tmp === 2 ) { |
| 1021 |
// The same as the end of function except '_' inside and '__' at the end. |
| 1022 |
name = stream.match( /^([^\s\u00a0>}[\]<{'|&:~]+?)__/ ); |
| 1023 |
if ( name && name[ 0 ] ) { |
| 1024 |
if ( |
| 1025 |
'__' + name[ 0 ].toLowerCase() in this.config.doubleUnderscore[ 0 ] || |
| 1026 |
'__' + name[ 0 ] in this.config.doubleUnderscore[ 1 ] |
| 1027 |
) { |
| 1028 |
return mwModeConfig.tags.doubleUnderscore; |
| 1029 |
} |
| 1030 |
if ( !stream.eol() ) { |
| 1031 |
// Two underscore symbols at the end can be the |
| 1032 |
// beginning of another double underscored Magic Word |
| 1033 |
stream.backUp( 2 ); |
| 1034 |
} |
| 1035 |
// Optimization: skip regex for EOL and backup-ed symbols |
| 1036 |
return this.makeStyle( style, state ); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
break; |
| 1040 |
case ':': |
| 1041 |
if ( state.nDt > 0 && !this.isNested( state ) ) { |
| 1042 |
state.nDt--; |
| 1043 |
return mwModeConfig.tags.indenting; |
| 1044 |
} |
| 1045 |
break; |
| 1046 |
default: |
| 1047 |
if ( /[\s\u00a0]/.test( ch ) ) { |
| 1048 |
stream.eatSpace(); |
| 1049 |
// highlight free external links, bug T108448 |
| 1050 |
if ( stream.match( this.urlProtocols, false ) && !stream.match( '//' ) ) { |
| 1051 |
state.stack.push( state.tokenize ); |
| 1052 |
state.tokenize = this.eatFreeExternalLinkProtocol.bind( this ); |
| 1053 |
return this.makeStyle( style, state ); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
break; |
| 1057 |
} |
| 1058 |
stream.match( /^[^\s\u00a0_>}[\]<{'|&:~=]+/ ); |
| 1059 |
return this.makeStyle( style, state ); |
| 1060 |
}; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* Remembers position and status for rollbacking. |
| 1065 |
* It is needed for changing from bold to italic with apostrophes before it, if required. |
| 1066 |
* |
| 1067 |
* @see https://phabricator.wikimedia.org/T108455 |
| 1068 |
* |
| 1069 |
* @param {StringStream} stream |
| 1070 |
* @private |
| 1071 |
*/ |
| 1072 |
prepareItalicForCorrection( stream ) { |
| 1073 |
// See Parser::doQuotes() in MediaWiki Core, it works similarly. |
| 1074 |
// this.firstSingleLetterWord has maximum priority |
| 1075 |
// this.firstMultiLetterWord has medium priority |
| 1076 |
// this.firstSpace has low priority |
| 1077 |
const end = stream.pos, |
| 1078 |
str = stream.string.slice( 0, end - 3 ), |
| 1079 |
x1 = str.slice( -1 ), |
| 1080 |
x2 = str.slice( -2, -1 ); |
| 1081 |
|
| 1082 |
// this.firstSingleLetterWord always is undefined here |
| 1083 |
if ( x1 === ' ' ) { |
| 1084 |
if ( this.firstMultiLetterWord || this.firstSpace ) { |
| 1085 |
return; |
| 1086 |
} |
| 1087 |
this.firstSpace = end; |
| 1088 |
} else if ( x2 === ' ' ) { |
| 1089 |
this.firstSingleLetterWord = end; |
| 1090 |
} else if ( this.firstMultiLetterWord ) { |
| 1091 |
return; |
| 1092 |
} else { |
| 1093 |
this.firstMultiLetterWord = end; |
| 1094 |
} |
| 1095 |
// remember bold and italic state for later restoration |
| 1096 |
this.wasBold = this.isBold; |
| 1097 |
this.wasItalic = this.isItalic; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/** |
| 1101 |
* @see https://codemirror.net/docs/ref/#language.StreamParser |
| 1102 |
* @return {StreamParser} |
| 1103 |
* @private |
| 1104 |
*/ |
| 1105 |
get mediawiki() { |
| 1106 |
return { |
| 1107 |
name: 'mediawiki', |
| 1108 |
|
| 1109 |
/** |
| 1110 |
* Initial State for the parser. |
| 1111 |
* |
| 1112 |
* @return {Object} |
| 1113 |
* @private |
| 1114 |
*/ |
| 1115 |
startState: () => ( { |
| 1116 |
tokenize: this.eatWikiText( '' ), |
| 1117 |
stack: [], |
| 1118 |
inHtmlTag: [], |
| 1119 |
extName: false, |
| 1120 |
extMode: false, |
| 1121 |
extState: false, |
| 1122 |
nTemplate: 0, |
| 1123 |
nLink: 0, |
| 1124 |
nExt: 0, |
| 1125 |
nDt: 0 |
| 1126 |
} ), |
| 1127 |
|
| 1128 |
/** |
| 1129 |
* Copies the given state. |
| 1130 |
* |
| 1131 |
* @param {Object} state |
| 1132 |
* @return {Object} |
| 1133 |
* @private |
| 1134 |
*/ |
| 1135 |
copyState: ( state ) => ( { |
| 1136 |
tokenize: state.tokenize, |
| 1137 |
stack: state.stack.concat( [] ), |
| 1138 |
inHtmlTag: state.inHtmlTag.concat( [] ), |
| 1139 |
extName: state.extName, |
| 1140 |
extMode: state.extMode, |
| 1141 |
extState: state.extMode !== false && state.extMode.copyState( state.extState ), |
| 1142 |
nTemplate: state.nTemplate, |
| 1143 |
nLink: state.nLink, |
| 1144 |
nExt: state.nExt, |
| 1145 |
nDt: state.nDt |
| 1146 |
} ), |
| 1147 |
|
| 1148 |
/** |
| 1149 |
* Reads one token, advancing the stream past it, |
| 1150 |
* and returning a string indicating the token's style tag. |
| 1151 |
* |
| 1152 |
* @param {StringStream} stream |
| 1153 |
* @param {Object} state |
| 1154 |
* @return {string|null} |
| 1155 |
* @private |
| 1156 |
*/ |
| 1157 |
token: ( stream, state ) => { |
| 1158 |
let style, p, t, f, |
| 1159 |
readyTokens = [], |
| 1160 |
tmpTokens = []; |
| 1161 |
|
| 1162 |
if ( this.oldTokens.length > 0 ) { |
| 1163 |
// just send saved tokens till they exists |
| 1164 |
t = this.oldTokens.shift(); |
| 1165 |
stream.pos = t.pos; |
| 1166 |
state = t.state; |
| 1167 |
return t.style; |
| 1168 |
} |
| 1169 |
|
| 1170 |
if ( stream.sol() ) { |
| 1171 |
// reset bold and italic status in every new line |
| 1172 |
state.nDt = 0; |
| 1173 |
this.isBold = false; |
| 1174 |
this.isItalic = false; |
| 1175 |
this.firstSingleLetterWord = null; |
| 1176 |
this.firstMultiLetterWord = null; |
| 1177 |
this.firstSpace = null; |
| 1178 |
} |
| 1179 |
|
| 1180 |
do { |
| 1181 |
// get token style |
| 1182 |
style = state.tokenize( stream, state ); |
| 1183 |
f = this.firstSingleLetterWord || this.firstMultiLetterWord || this.firstSpace; |
| 1184 |
if ( f ) { |
| 1185 |
// rollback point exists |
| 1186 |
if ( f !== p ) { |
| 1187 |
// new rollback point |
| 1188 |
p = f; |
| 1189 |
// it's not first rollback point |
| 1190 |
if ( tmpTokens.length > 0 ) { |
| 1191 |
// save tokens |
| 1192 |
readyTokens = readyTokens.concat( tmpTokens ); |
| 1193 |
tmpTokens = []; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
// save token |
| 1197 |
tmpTokens.push( { |
| 1198 |
pos: stream.pos, |
| 1199 |
style, |
| 1200 |
state: ( state.extMode || this.mediawiki ).copyState( state ) |
| 1201 |
} ); |
| 1202 |
} else { |
| 1203 |
// rollback point does not exist |
| 1204 |
// remember style before possible rollback point |
| 1205 |
this.oldStyle = style; |
| 1206 |
// just return token style |
| 1207 |
return style; |
| 1208 |
} |
| 1209 |
} while ( !stream.eol() ); |
| 1210 |
|
| 1211 |
if ( this.isBold && this.isItalic ) { |
| 1212 |
// needs to rollback |
| 1213 |
// restore status |
| 1214 |
this.isItalic = this.wasItalic; |
| 1215 |
this.isBold = this.wasBold; |
| 1216 |
this.firstSingleLetterWord = null; |
| 1217 |
this.firstMultiLetterWord = null; |
| 1218 |
this.firstSpace = null; |
| 1219 |
if ( readyTokens.length > 0 ) { |
| 1220 |
// it contains tickets before the point of rollback |
| 1221 |
// add one apostrophe, next token will be italic (two apostrophes) |
| 1222 |
readyTokens[ readyTokens.length - 1 ].pos++; |
| 1223 |
// for sending tokens till the point of rollback |
| 1224 |
this.oldTokens = readyTokens; |
| 1225 |
} else { |
| 1226 |
// there are no tickets before the point of rollback |
| 1227 |
stream.pos = tmpTokens[ 0 ].pos - 2; // eat( '\'') |
| 1228 |
// send saved Style |
| 1229 |
return this.oldStyle; |
| 1230 |
} |
| 1231 |
} else { |
| 1232 |
// do not need to rollback |
| 1233 |
// send all saved tokens |
| 1234 |
this.oldTokens = readyTokens.concat( tmpTokens ); |
| 1235 |
} |
| 1236 |
// return first saved token |
| 1237 |
t = this.oldTokens.shift(); |
| 1238 |
stream.pos = t.pos; |
| 1239 |
state = t.state; |
| 1240 |
return t.style; |
| 1241 |
}, |
| 1242 |
|
| 1243 |
/** |
| 1244 |
* @param {Object} state |
| 1245 |
* @private |
| 1246 |
*/ |
| 1247 |
blankLine: ( state ) => { |
| 1248 |
if ( state.extMode && state.extMode.blankLine ) { |
| 1249 |
state.extMode.blankLine( state.extState ); |
| 1250 |
} |
| 1251 |
}, |
| 1252 |
|
| 1253 |
/** |
| 1254 |
* Extra tokens to use in this parser. |
| 1255 |
* |
| 1256 |
* @see CodeMirrorModeMediaWikiConfig.defaultTokenTable |
| 1257 |
* @return {Object<Tag>} |
| 1258 |
* @private |
| 1259 |
*/ |
| 1260 |
tokenTable: this.tokenTable |
| 1261 |
}; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
/** |
| 1266 |
* Gets a LanguageSupport instance for the MediaWiki mode. |
| 1267 |
* |
| 1268 |
* @member CodeMirrorModeMediaWiki |
| 1269 |
* @method |
| 1270 |
* @param {Object} [config] Configuration options for the MediaWiki mode. |
| 1271 |
* @param {boolean} [config.bidiIsolation=false] Enable bidi isolation around HTML tags. |
| 1272 |
* This should generally always be enabled on RTL pages, but it comes with a performance cost. |
| 1273 |
* @param {boolean} [config.templateFolding=true] Enable template folding. |
| 1274 |
* @param {Object|null} [mwConfig] Ignore; used only by unit tests. |
| 1275 |
* @return {LanguageSupport} |
| 1276 |
* @stable to call |
| 1277 |
*/ |
| 1278 |
const mediaWikiLang = ( config = { bidiIsolation: false }, mwConfig = null ) => { |
| 1279 |
mwConfig = mwConfig || mw.config.get( 'extCodeMirrorConfig' ); |
| 1280 |
const mode = new CodeMirrorModeMediaWiki( mwConfig ); |
| 1281 |
const parser = mode.mediawiki; |
| 1282 |
const lang = StreamLanguage.define( parser ); |
| 1283 |
const langExtension = [ syntaxHighlighting( |
| 1284 |
HighlightStyle.define( |
| 1285 |
mwModeConfig.getTagStyles( parser ) |
| 1286 |
) |
| 1287 |
) ]; |
| 1288 |
|
| 1289 |
// Set to [] to disable everywhere, or null to enable everywhere. |
| 1290 |
const templateFoldingNs = mwConfig.templateFoldingNamespaces; |
| 1291 |
const shouldUseFolding = !templateFoldingNs || templateFoldingNs.includes( mw.config.get( 'wgNamespaceNumber' ) ); |
| 1292 |
// Add template folding if in supported namespace. |
| 1293 |
if ( shouldUseFolding && ( config.templateFolding || config.templateFolding === undefined ) ) { |
| 1294 |
langExtension.push( templateFoldingExtension ); |
| 1295 |
} |
| 1296 |
|
| 1297 |
// Bundle the bidi isolation extension, as it's coded specifically for MediaWiki. |
| 1298 |
// This is behind a config option for performance reasons (we only use it on RTL pages). |
| 1299 |
if ( config.bidiIsolation ) { |
| 1300 |
langExtension.push( bidiIsolationExtension ); |
| 1301 |
} |
| 1302 |
|
| 1303 |
return new LanguageSupport( lang, langExtension ); |
| 1304 |
}; |
| 1305 |
|
| 1306 |
module.exports = mediaWikiLang; |