| 1 |
/** |
| 2 |
* Combined Wiki (MediaWiki) and HTML tokenizer based on pegjs. Emits several |
| 3 |
* chunks of tokens (one chunk per top-level block matched) and eventually an |
| 4 |
* end event. Tokens map to HTML tags as far as possible, with custom tokens |
| 5 |
* used where further processing on the token stream is needed. |
| 6 |
*/ |
| 7 |
{ |
| 8 |
use Wikimedia\Assert\UnreachableException; |
| 9 |
use Wikimedia\JsonCodec\JsonCodec; |
| 10 |
use Wikimedia\Parsoid\Config\Env; |
| 11 |
use Wikimedia\Parsoid\Config\SiteConfig; |
| 12 |
use Wikimedia\Parsoid\Core\DomSourceRange; |
| 13 |
use Wikimedia\Parsoid\NodeData\DataMw; |
| 14 |
use Wikimedia\Parsoid\NodeData\DataParsoid; |
| 15 |
use Wikimedia\Parsoid\NodeData\TempData; |
| 16 |
use Wikimedia\Parsoid\Tokens\CommentTk; |
| 17 |
use Wikimedia\Parsoid\Tokens\EOFTk; |
| 18 |
use Wikimedia\Parsoid\Tokens\EndTagTk; |
| 19 |
use Wikimedia\Parsoid\Tokens\KV; |
| 20 |
use Wikimedia\Parsoid\Tokens\KVSourceRange; |
| 21 |
use Wikimedia\Parsoid\Tokens\NlTk; |
| 22 |
use Wikimedia\Parsoid\Tokens\SelfclosingTagTk; |
| 23 |
use Wikimedia\Parsoid\Tokens\SourceRange; |
| 24 |
use Wikimedia\Parsoid\Tokens\TagTk; |
| 25 |
use Wikimedia\Parsoid\Tokens\Token; |
| 26 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 27 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 28 |
use Wikimedia\Parsoid\Utils\TokenUtils; |
| 29 |
use Wikimedia\Parsoid\Utils\Utils; |
| 30 |
use Wikimedia\Parsoid\Utils\WTUtils; |
| 31 |
use Wikimedia\Parsoid\Wikitext\Consts; |
| 32 |
} |
| 33 |
{ |
| 34 |
/** @var Env */ |
| 35 |
private $env; |
| 36 |
|
| 37 |
/** @var SiteConfig */ |
| 38 |
private $siteConfig; |
| 39 |
|
| 40 |
/** @var array */ |
| 41 |
private $pipelineOpts; |
| 42 |
|
| 43 |
/** @var int */ |
| 44 |
private $pipelineOffset; |
| 45 |
|
| 46 |
private $extTags; |
| 47 |
|
| 48 |
private $startTime; |
| 49 |
|
| 50 |
/** @var string */ |
| 51 |
private $reUrltextLookahead; |
| 52 |
|
| 53 |
/** @var string */ |
| 54 |
private $urltextPlainSegment = ''; |
| 55 |
|
| 56 |
/** @var bool */ |
| 57 |
private $urltextFoundAutolink = false; |
| 58 |
|
| 59 |
protected function initialize() { |
| 60 |
$this->env = $this->options['env']; |
| 61 |
$this->siteConfig = $this->env->getSiteConfig(); |
| 62 |
|
| 63 |
$tokenizer = $this->options['pegTokenizer']; |
| 64 |
$this->pipelineOpts = $tokenizer->getOptions(); |
| 65 |
// FIXME: inTemplate option may not always be set in |
| 66 |
// standalone tokenizers user by some pipelines handlers. |
| 67 |
$this->pipelineOffset = $this->options['pipelineOffset'] ?? 0; |
| 68 |
$this->extTags = $this->siteConfig->getExtensionTagNameMap(); |
| 69 |
|
| 70 |
// Non-greedy text_char sequence: stop at ampersand, double-underscore, |
| 71 |
// magic link prefix or protocol |
| 72 |
$this->reUrltextLookahead = '!(?:' . |
| 73 |
'([^-\'<[{\n\r:;\]}|\!=&]*?)' . |
| 74 |
'(?:__|$|[-\'<[{\n\r:;\]}|\!=&]|(RFC|PMID|ISBN|' . |
| 75 |
'(?i)' . $this->siteConfig->getProtocolsRegex( true ) . |
| 76 |
')))!A'; |
| 77 |
} |
| 78 |
|
| 79 |
private $prevOffset = 0; |
| 80 |
private $headingIndex = 0; |
| 81 |
|
| 82 |
public function resetState() { |
| 83 |
$this->prevOffset = 0; |
| 84 |
$this->headingIndex = 0; |
| 85 |
} |
| 86 |
|
| 87 |
private function assert( $condition, $text ) { |
| 88 |
if ( !$condition ) { |
| 89 |
throw new \RuntimeException( "Grammar.pegphp assertion failure: $text" ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private function unreachable() { |
| 94 |
throw new UnreachableException( "Grammar.pegphp: this should be unreachable" ); |
| 95 |
} |
| 96 |
|
| 97 |
// Some shorthands for legibility |
| 98 |
private function startOffset() { |
| 99 |
return $this->savedPos; |
| 100 |
} |
| 101 |
|
| 102 |
private function endOffset() { |
| 103 |
return $this->currPos; |
| 104 |
} |
| 105 |
|
| 106 |
private function tsrOffsets( $flag = 'default' ): SourceRange { |
| 107 |
switch ( $flag ) { |
| 108 |
case 'start': |
| 109 |
return new SourceRange( $this->savedPos, $this->savedPos ); |
| 110 |
case 'end': |
| 111 |
return new SourceRange( $this->currPos, $this->currPos ); |
| 112 |
default: |
| 113 |
return new SourceRange( $this->savedPos, $this->currPos ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* Emit a chunk of tokens to our consumers. Once this has been done, the |
| 119 |
* current expression can return an empty list (true). |
| 120 |
*/ |
| 121 |
private function emitChunk( $tokens ) { |
| 122 |
// FIXME: We don't expect nulls here, but looks like |
| 123 |
// hack from I1c695ab6cdd3655e98877c175ddbabdee9dc44b7 |
| 124 |
// introduces them. Work around it for now! |
| 125 |
if ( !$tokens ) { |
| 126 |
return []; |
| 127 |
} |
| 128 |
|
| 129 |
// Shift tsr of all tokens by the pipeline offset |
| 130 |
TokenUtils::shiftTokenTSR( $tokens, $this->pipelineOffset ); |
| 131 |
$this->env->log( 'trace/peg', $this->options['pipelineId'] ?? '0', '----> ', $tokens ); |
| 132 |
|
| 133 |
$i = null; |
| 134 |
$n = count( $tokens ); |
| 135 |
|
| 136 |
// Enforce parsing resource limits |
| 137 |
for ( $i = 0; $i < $n; $i++ ) { |
| 138 |
TokenizerUtils::enforceParserResourceLimits( $this->env, $tokens[ $i ] ); |
| 139 |
} |
| 140 |
|
| 141 |
return $tokens; |
| 142 |
} |
| 143 |
|
| 144 |
/* ------------------------------------------------------------------------ |
| 145 |
* Extension tags should be parsed with higher priority than anything else. |
| 146 |
* |
| 147 |
* The trick we use is to strip out the content inside a matching tag-pair |
| 148 |
* and not tokenize it. The content, if it needs to parsed (for example, |
| 149 |
* for <ref>, <*include*> tags), is parsed in a fresh tokenizer context |
| 150 |
* which means any error correction that needs to happen is restricted to |
| 151 |
* the scope of the extension content and doesn't spill over to the higher |
| 152 |
* level. Ex: <math><!--foo</math>. |
| 153 |
* |
| 154 |
* IGNORE: {{ this just balances the blocks in this comment for pegjs |
| 155 |
* |
| 156 |
* This trick also lets us prevent extension content (that don't accept WT) |
| 157 |
* from being parsed as wikitext (Ex: <math>\frac{foo\frac{bar}}</math>) |
| 158 |
* We don't want the "}}" being treated as a template closing tag and |
| 159 |
* closing outer templates. |
| 160 |
* --------------------------------------------------------------------- */ |
| 161 |
|
| 162 |
private function isXMLTag( string $name ): bool { |
| 163 |
$lName = mb_strtolower( $name ); |
| 164 |
return isset( Consts::$HTML['HTML5Tags'][$lName] ) || |
| 165 |
isset( Consts::$HTML['OlderHTMLTags'][$lName] ); |
| 166 |
} |
| 167 |
|
| 168 |
private function maybeAnnotationOrExtensionTag( Token $t, ?bool $end, array $attribs, SourceRange $tsr ) { |
| 169 |
$tagName = mb_strtolower( $t->getName() ); |
| 170 |
|
| 171 |
$isAnnotationTag = $this->siteConfig->isAnnotationTag( $tagName ); |
| 172 |
if ( !$isAnnotationTag ) { |
| 173 |
$pipepos = strpos( $tagName, '|' ); |
| 174 |
if ( $pipepos ) { |
| 175 |
$strBeforePipe = substr( $tagName, 0, $pipepos ); |
| 176 |
$isAnnotationTag = $this->siteConfig->isAnnotationTag( $strBeforePipe ); |
| 177 |
if ( $isAnnotationTag ) { |
| 178 |
$attribs = [ new KV( "name", substr( $tagName, $pipepos + 1, strlen( $tagName ) - $pipepos - 1 ) ) ]; |
| 179 |
$tagName = $strBeforePipe; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if ( $isAnnotationTag ) { |
| 185 |
$metaAttrs = [ new KV( 'typeof', 'mw:Annotation/' . $tagName . ($end ? '/End' : '') ) ]; |
| 186 |
$datamw = null; |
| 187 |
if ( count( $attribs ) > 0 ) { |
| 188 |
$attrMap = []; |
| 189 |
foreach ( $attribs as $attr ) { |
| 190 |
// If the key or the value is not a string, we replace it by the thing that generated it and |
| 191 |
// consider that wikitext as a raw string instead. |
| 192 |
$k = is_string( $attr->k ) ? $attr->k : $attr->ksrc; |
| 193 |
$v = is_string( $attr->v ) ? $attr->v : $attr->vsrc; |
| 194 |
$attrMap[$k] = $v; |
| 195 |
} |
| 196 |
// Possible follow-up in T295168 for attribute sanitation |
| 197 |
// T367616: 'attrs' should be renamed to 'extAttrs' |
| 198 |
$datamw = new DataMw( [ 'attrs' => (object)$attrMap ] ); |
| 199 |
} |
| 200 |
$dp = new DataParsoid(); |
| 201 |
$dp->tsr = $tsr; |
| 202 |
$this->env->hasAnnotations = true; |
| 203 |
// FIXME: Suppress annotation meta tokens from template pipelines |
| 204 |
// since they may not have TSR values and won't get recognized as |
| 205 |
// annotation ranges. Without TSR, they might end up stuck in |
| 206 |
// fosterable positions and cause havoc on edits by breaking selser. |
| 207 |
if ( empty( $this->pipelineOpts['inTemplate'] ) ) { |
| 208 |
return [ new SelfclosingTagTk ( 'meta', $metaAttrs, $dp, $datamw ) ]; |
| 209 |
} else { |
| 210 |
return []; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
$isInstalledExt = isset( $this->extTags[$tagName] ); |
| 215 |
$isIncludeTag = WTUtils::isIncludeTag( $tagName ); |
| 216 |
|
| 217 |
// Extensions have higher precedence when they shadow html tags. |
| 218 |
if ( !( $isInstalledExt || $isIncludeTag ) ) { |
| 219 |
return $t; |
| 220 |
} |
| 221 |
|
| 222 |
$dp = $t->dataParsoid; |
| 223 |
$skipPos = $this->currPos; |
| 224 |
|
| 225 |
switch ( get_class( $t ) ) { |
| 226 |
case EndTagTk::class: |
| 227 |
if ( $isIncludeTag ) { |
| 228 |
return $t; |
| 229 |
} |
| 230 |
// Similar to TagTk, we rely on the sanitizer to convert to text |
| 231 |
// where necessary and emit tokens to ease the wikitext escaping |
| 232 |
// code. However, extension tags that shadow html tags will see |
| 233 |
// their unmatched end tags dropped while tree building, since |
| 234 |
// the sanitizer will let them through. |
| 235 |
return $t; // not text() |
| 236 |
|
| 237 |
case SelfclosingTagTk::class: |
| 238 |
$dp->src = $dp->tsr->substr( $this->input ); |
| 239 |
$dp->extTagOffsets = new DomSourceRange( |
| 240 |
$dp->tsr->start, $dp->tsr->end, |
| 241 |
$dp->tsr->length(), 0 |
| 242 |
); |
| 243 |
if ( $isIncludeTag ) { |
| 244 |
return $t; |
| 245 |
} |
| 246 |
break; |
| 247 |
|
| 248 |
case TagTk::class: |
| 249 |
$endTagRE = '~.*?(</' . preg_quote( $tagName, '~' ) . '\s*>)~iusA'; |
| 250 |
$tagContentFound = preg_match( $endTagRE, $this->input, $tagContent, 0, $dp->tsr->start ); |
| 251 |
|
| 252 |
if ( !$tagContentFound ) { |
| 253 |
$dp->src = $dp->tsr->substr( $this->input ); |
| 254 |
$dp->extTagOffsets = new DomSourceRange( |
| 255 |
$dp->tsr->start, $dp->tsr->end, |
| 256 |
$dp->tsr->length(), 0 |
| 257 |
); |
| 258 |
if ( $isIncludeTag ) { |
| 259 |
return $t; |
| 260 |
} else { |
| 261 |
// This is undefined behaviour. The old parser currently |
| 262 |
// returns text here (see core commit 674e8388cba), |
| 263 |
// whereas this results in unclosed |
| 264 |
// extension tags that shadow html tags falling back to |
| 265 |
// their html equivalent. The sanitizer will take care |
| 266 |
// of converting to text where necessary. We do this to |
| 267 |
// simplify `hasWikitextTokens` when escaping wikitext, |
| 268 |
// which wants these as tokens because it's otherwise |
| 269 |
// lacking in context. |
| 270 |
return $t; // not text() |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
$extSrc = $tagContent[0]; |
| 275 |
$extEndOffset = $dp->tsr->start + strlen( $extSrc ); |
| 276 |
$extEndTagWidth = strlen( $tagContent[1] ); |
| 277 |
|
| 278 |
if ( !empty( $this->pipelineOpts['inTemplate'] ) ) { |
| 279 |
// Support nesting in extensions tags while tokenizing in templates |
| 280 |
// to support the #tag parser function. |
| 281 |
// |
| 282 |
// It's necessary to permit this broadly in templates because |
| 283 |
// there's no way to distinguish whether the nesting happened |
| 284 |
// while expanding the #tag parser function, or just a general |
| 285 |
// syntax errors. In other words, |
| 286 |
// |
| 287 |
// hi<ref>ho<ref>hi</ref>ho</ref> |
| 288 |
// |
| 289 |
// and |
| 290 |
// |
| 291 |
// hi{{#tag:ref|ho<ref>hi</ref>ho}} |
| 292 |
// |
| 293 |
// found in template are returned indistinguishably after a |
| 294 |
// preprocessing request, though the old parser renders them |
| 295 |
// differently. #tag in template is probably a common enough |
| 296 |
// use case that we want to accept these false positives, |
| 297 |
// though another approach could be to drop this code here, and |
| 298 |
// invoke a native #tag handler and forgo those in templates. |
| 299 |
// |
| 300 |
// Expand `extSrc` as long as there is a <tagName> found in the |
| 301 |
// extension source body. |
| 302 |
$startTagRE = '~<' . preg_quote( $tagName, '~' ) . '(?:[^/>]|/(?!>))*>~i'; |
| 303 |
$s = substr( $extSrc, $dp->tsr->end - $dp->tsr->start ); |
| 304 |
$openTags = 0; |
| 305 |
while ( true ) { |
| 306 |
if ( preg_match_all( $startTagRE, $s, $matches ) ) { |
| 307 |
$openTags += count( $matches[0] ); |
| 308 |
} |
| 309 |
if ( !$openTags ) { |
| 310 |
break; |
| 311 |
} |
| 312 |
if ( !preg_match( $endTagRE, $this->input, $tagContent, 0, $extEndOffset ) ) { |
| 313 |
break; |
| 314 |
} |
| 315 |
$openTags -= 1; |
| 316 |
$s = $tagContent[0]; |
| 317 |
$extEndOffset += strlen( $s ); |
| 318 |
$extEndTagWidth = strlen( $tagContent[1] ); |
| 319 |
$extSrc .= $s; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// Extension content source |
| 324 |
$dp->src = $extSrc; |
| 325 |
$dp->extTagOffsets = new DomSourceRange( |
| 326 |
$dp->tsr->start, $extEndOffset, |
| 327 |
$dp->tsr->length(), $extEndTagWidth |
| 328 |
); |
| 329 |
|
| 330 |
$skipPos = $dp->extTagOffsets->innerEnd(); |
| 331 |
|
| 332 |
// If the xml-tag is a known installed (not native) extension, |
| 333 |
// skip the end-tag as well. |
| 334 |
if ( $isInstalledExt ) { |
| 335 |
$skipPos = $dp->extTagOffsets->end; |
| 336 |
} |
| 337 |
break; |
| 338 |
|
| 339 |
default: |
| 340 |
$this->unreachable(); |
| 341 |
} |
| 342 |
|
| 343 |
$this->currPos = $skipPos; |
| 344 |
|
| 345 |
if ( $isInstalledExt ) { |
| 346 |
// update tsr->end to span the start and end tags. |
| 347 |
$dp->tsr->end = $this->endOffset(); // was just modified above |
| 348 |
return new SelfclosingTagTk( 'extension', [ |
| 349 |
new KV( 'typeof', 'mw:Extension' ), |
| 350 |
new KV( 'name', $tagName ), |
| 351 |
new KV( 'about', $this->env->newAboutId() ), |
| 352 |
new KV( 'source', $dp->src ), |
| 353 |
new KV( 'options', $t->attribs ) |
| 354 |
], $dp |
| 355 |
); |
| 356 |
} elseif ( $isIncludeTag ) { |
| 357 |
// Parse ext-content, strip eof, and shift tsr |
| 358 |
$extContent = $dp->extTagOffsets->stripTags( $dp->src ); |
| 359 |
$tokenizer = new PegTokenizer( $this->env ); |
| 360 |
$tokenizer->setSourceOffsets( new SourceRange( $dp->extTagOffsets->innerStart(), $dp->extTagOffsets->innerEnd() ) ); |
| 361 |
$extContentToks = $tokenizer->tokenizeSync( $extContent, [ 'sol' => true ] ); |
| 362 |
if ( $dp->extTagOffsets->closeWidth > 0 ) { |
| 363 |
TokenUtils::stripEOFTkFromTokens( $extContentToks ); |
| 364 |
} |
| 365 |
array_unshift( $extContentToks, $t ); |
| 366 |
return $extContentToks; |
| 367 |
} else { |
| 368 |
$this->unreachable(); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/* ******************************************************** |
| 374 |
* "start" is the top-level start rule |
| 375 |
* |
| 376 |
* Other allowed start rules: |
| 377 |
* - table_start_tag |
| 378 |
* - url |
| 379 |
* - row_syntax_table_args |
| 380 |
* - table_attributes |
| 381 |
* - generic_newline_attributes |
| 382 |
* - tplarg_or_template_or_bust |
| 383 |
* - extlink |
| 384 |
* - list_item |
| 385 |
* |
| 386 |
* These start rules are listed in lib/wt2html/tokenizer.js |
| 387 |
* and is used during PEG grammar compilation. |
| 388 |
* ******************************************************** */ |
| 389 |
|
| 390 |
start "start" = |
| 391 |
t:tlb* n:newlineToken* { |
| 392 |
if ( count( $t ) ) { |
| 393 |
$ret = TokenizerUtils::flattenIfArray( $t ); |
| 394 |
} else { |
| 395 |
$ret = []; |
| 396 |
} |
| 397 |
if ( count( $n ) ) { |
| 398 |
PHPUtils::pushArray($ret, $n); |
| 399 |
} |
| 400 |
$ret[] = new EOFTk(); |
| 401 |
return $ret; |
| 402 |
} |
| 403 |
|
| 404 |
generic_newline_attributes "generic_newline_attributes" = generic_newline_attribute* |
| 405 |
|
| 406 |
table_attributes "table_attributes" = |
| 407 |
(table_attribute / optionalSpaceToken b:broken_table_attribute_name_char { return $b; })* |
| 408 |
|
| 409 |
/* |
| 410 |
* This rule exists to support tokenizing the document in chunks. |
| 411 |
* The parser's streaming interface will stop tokenization after each iteration |
| 412 |
* of the starred subexpression, and yield to the node.js event-loop to |
| 413 |
* schedule other pending event handlers. |
| 414 |
*/ |
| 415 |
start_async = |
| 416 |
( |
| 417 |
( |
| 418 |
& { |
| 419 |
$this->startTime = null; |
| 420 |
if ( $this->env->profiling() ) { |
| 421 |
$profile = $this->env->getCurrentProfile(); |
| 422 |
$this->startTime = microtime( true ); |
| 423 |
} |
| 424 |
return true; |
| 425 |
} |
| 426 |
t:tlb |
| 427 |
& { |
| 428 |
if ( $this->env->profiling() ) { |
| 429 |
$profile = $this->env->getCurrentProfile(); |
| 430 |
$profile->bumpTimeUse( |
| 431 |
'PEG', 1000 * ( microtime( true ) - $this->startTime ), 'PEG' ); |
| 432 |
} |
| 433 |
return true; |
| 434 |
} |
| 435 |
) { return $t; } |
| 436 |
/ newlineToken* &{ |
| 437 |
// "tlb" matches "block" matches "sol" matches "newlineToken" |
| 438 |
// But, "tlb" is prefixed with a !eof clause, so, we should only |
| 439 |
// get here on eof. So, safe to unconditionally terminate the |
| 440 |
// generator loop here. |
| 441 |
return false; |
| 442 |
} |
| 443 |
)* |
| 444 |
|
| 445 |
/* |
| 446 |
* A document (start rule) is a sequence of toplevelblocks. Tokens are |
| 447 |
* emitted in chunks per toplevelblock to avoid buffering the full document. |
| 448 |
*/ |
| 449 |
tlb "tlb" = |
| 450 |
!eof b:block { |
| 451 |
// Clear the tokenizer's backtracking cache after matching each |
| 452 |
// toplevelblock. There won't be any backtracking as a document is just a |
| 453 |
// sequence of toplevelblocks, so the cache for previous toplevelblocks |
| 454 |
// will never be needed. |
| 455 |
$end = $this->startOffset(); |
| 456 |
for ( ; $this->prevOffset < $end; $this->prevOffset++ ) { |
| 457 |
unset( $this->cache[$this->prevOffset] ); |
| 458 |
} |
| 459 |
|
| 460 |
$tokens = null; |
| 461 |
if ( is_array( $b ) && count( $b ) ) { |
| 462 |
$tokens = TokenizerUtils::flattenIfArray( $b ); |
| 463 |
} elseif ( is_string( $b ) ) { |
| 464 |
$tokens = [ $b ]; |
| 465 |
} |
| 466 |
|
| 467 |
// Emit tokens for this toplevelblock. This feeds a chunk to the parser pipeline. |
| 468 |
return $this->emitChunk( $tokens ); |
| 469 |
} |
| 470 |
|
| 471 |
/* |
| 472 |
* Redirects can only occur as the first thing in a document. See |
| 473 |
* WikitextContent::getRedirectTarget() |
| 474 |
*/ |
| 475 |
redirect = |
| 476 |
rw:redirect_word |
| 477 |
sp:$space_or_newline* |
| 478 |
c:$(":" space_or_newline*)? |
| 479 |
wl:wikilink |
| 480 |
& { |
| 481 |
return count( $wl ) === 1 && $wl[0] instanceof Token; |
| 482 |
} |
| 483 |
{ |
| 484 |
$link = $wl[0]; |
| 485 |
if ( $sp ) { |
| 486 |
$rw .= $sp; |
| 487 |
} |
| 488 |
if ( $c ) { |
| 489 |
$rw .= $c; |
| 490 |
} |
| 491 |
// Build a redirect token |
| 492 |
$dp = new DataParsoid; |
| 493 |
$dp->src = $rw; |
| 494 |
$dp->tsr = $this->tsrOffsets(); |
| 495 |
$dp->linkTk = $link; |
| 496 |
$redirect = new SelfclosingTagTk( 'mw:redirect', |
| 497 |
// Put 'href' into attributes so it gets template-expanded |
| 498 |
[ $link->getAttributeKV( 'href' ) ], |
| 499 |
$dp |
| 500 |
); |
| 501 |
return $redirect; |
| 502 |
} |
| 503 |
|
| 504 |
/* The 'redirect' magic word. |
| 505 |
* The leading whitespace allowed is due to the PHP trim() function. |
| 506 |
*/ |
| 507 |
redirect_word = |
| 508 |
$( |
| 509 |
[ \t\n\r\0\x0b]* |
| 510 |
rw:$(!space_or_newline ![:\[] .)+ |
| 511 |
& { |
| 512 |
return preg_match( $this->env->getSiteConfig()->getMagicWordMatcher( 'redirect' ), $rw ); |
| 513 |
} |
| 514 |
) |
| 515 |
|
| 516 |
/* |
| 517 |
* The actual contents of each block. |
| 518 |
*/ |
| 519 |
block = |
| 520 |
// Redirect has to be the first alternative; otherwise it gets parsed as a <ol> |
| 521 |
// In practice, WikiContent::getRedirectTargetAndText() in MediaWiki strips |
| 522 |
// the redirect from the source so it never sees it. |
| 523 |
&sof r:redirect cil:sol_transparent* bl:block_line? { |
| 524 |
return array_merge( [ $r ], $cil, $bl ?: [] ); |
| 525 |
} |
| 526 |
/ block_lines |
| 527 |
// Inlineline includes generic tags; wrapped into paragraphs in token |
| 528 |
// transform and DOM postprocessor |
| 529 |
/ inlineline |
| 530 |
/ s:sol !sof !inline_breaks { return $s; } |
| 531 |
|
| 532 |
/* |
| 533 |
* A block nested in other constructs. Avoid eating end delimiters for other |
| 534 |
* constructs by checking against inline_breaks first. |
| 535 |
*/ |
| 536 |
nested_block = !inline_breaks b:block { return $b; } |
| 537 |
|
| 538 |
/* |
| 539 |
* The same, but suitable for use inside a table construct. |
| 540 |
* Doesn't match table_heading_tag, table_row_tag, table_data_tag, |
| 541 |
* table_caption tag, or table_end_tag, although it does allow |
| 542 |
* table_start_tag (for nested tables). |
| 543 |
*/ |
| 544 |
nested_block_in_table = |
| 545 |
// XXX: don't rely on a lame look-ahead like this; use syntax stops |
| 546 |
// instead, so that multi-line th content followed by a line prefixed with |
| 547 |
// a comment is also handled. Alternatively, implement a sol look-behind |
| 548 |
// assertion accepting spaces and comments. |
| 549 |
!(sol (space* sol)? space* (pipe / "!")) |
| 550 |
|
| 551 |
// avoid recursion via nested_block_in_table, as that can lead to stack |
| 552 |
// overflow in large tables |
| 553 |
// See https://phabricator.wikimedia.org/T59670 |
| 554 |
b:nested_block<tableDataBlock> { |
| 555 |
return $b; |
| 556 |
} |
| 557 |
|
| 558 |
/* |
| 559 |
* Line-based block constructs. |
| 560 |
*/ |
| 561 |
block_lines = |
| 562 |
s:sol |
| 563 |
// eat an empty line before the block |
| 564 |
s2:(os:optionalSpaceToken so:sol { return array_merge( $os, $so ); })? |
| 565 |
bl:block_line { |
| 566 |
return array_merge( $s, $s2 ?: [], $bl ); |
| 567 |
} |
| 568 |
|
| 569 |
// Horizontal rules |
| 570 |
hr = |
| 571 |
"----" d:$"-"* |
| 572 |
// Check if a newline or content follows |
| 573 |
lineContent:( &sol "" { return null; } / "" { return true; } ) { |
| 574 |
$dataParsoid = new DataParsoid; |
| 575 |
$dataParsoid->tsr = $this->tsrOffsets(); |
| 576 |
if ( $lineContent !== null ) { |
| 577 |
$dataParsoid->lineContent = $lineContent; |
| 578 |
} |
| 579 |
if ( strlen( $d ) > 0 ) { |
| 580 |
$dataParsoid->extra_dashes = strlen( $d ); |
| 581 |
} |
| 582 |
return [new SelfclosingTagTk( 'hr', [], $dataParsoid )]; |
| 583 |
} |
| 584 |
|
| 585 |
/* |
| 586 |
* Block structures with start-of-line wiki syntax |
| 587 |
*/ |
| 588 |
block_line = |
| 589 |
heading |
| 590 |
/ list_item |
| 591 |
/ hr |
| 592 |
/ st:optionalSpaceToken &[ <{}|!] tl:table_line { |
| 593 |
return array_merge( $st, $tl ); |
| 594 |
} |
| 595 |
|
| 596 |
br = |
| 597 |
s:optionalSpaceToken &newline { |
| 598 |
$dp = new DataParsoid; |
| 599 |
$dp->tsr = $this->tsrOffsets(); |
| 600 |
return array_merge( $s, [ |
| 601 |
new SelfclosingTagTk( 'br', [], $dp ) |
| 602 |
] ); |
| 603 |
} |
| 604 |
|
| 605 |
inline_breaks = |
| 606 |
& [=|!{}:;\r\n[\]\-] |
| 607 |
( |
| 608 |
annOrExtTag: <annOrExtTag> |
| 609 |
h: <h> |
| 610 |
extlink: <extlink> |
| 611 |
intemplate: <intemplate> |
| 612 |
preproc: <preproc> |
| 613 |
equal: <equal> |
| 614 |
table: <table> |
| 615 |
templateArg: <templateArg> |
| 616 |
tableCellArg: <tableCellArg> |
| 617 |
semicolon: <semicolon> |
| 618 |
arrow: <arrow> |
| 619 |
linkdesc: <linkdesc> |
| 620 |
colon: <colon> |
| 621 |
th: <th> |
| 622 |
& { |
| 623 |
return TokenizerUtils::inlineBreaks( $this->input, $this->endOffset(), [ |
| 624 |
'annOrExtTag' => $annOrExtTag, |
| 625 |
'h' => $h, |
| 626 |
'extlink' => $extlink, |
| 627 |
'intemplate' => $intemplate, |
| 628 |
'preproc' => $preproc, |
| 629 |
'equal' => $equal, |
| 630 |
'table' => $table, |
| 631 |
'templateArg' => $templateArg, |
| 632 |
'tableCellArg' => $tableCellArg, |
| 633 |
'semicolon' => $semicolon, |
| 634 |
'arrow' => $arrow, |
| 635 |
'linkdesc' => $linkdesc, |
| 636 |
'colon' => $colon, |
| 637 |
'th' => $th |
| 638 |
], $this->env ); |
| 639 |
} |
| 640 |
) |
| 641 |
|
| 642 |
inlineline = |
| 643 |
c:( |
| 644 |
urltext |
| 645 |
/ !inline_breaks |
| 646 |
r:(inline_element / !newline s:. { return $s; }) { return $r; } |
| 647 |
)+ { |
| 648 |
return TokenizerUtils::flattenStringlist( $c ); |
| 649 |
} |
| 650 |
|
| 651 |
inline_xmlish_tag = |
| 652 |
"<" tag:(xmlish_tag<annOrExtTag> / xmlish_tag<annOrExtTag=false> / tvar_old_syntax_closing_HACK ) |
| 653 |
{ return $tag; } |
| 654 |
|
| 655 |
inline_element = |
| 656 |
& '<' r:( inline_xmlish_tag / comment ) { return $r; } |
| 657 |
/ & '{' r:tplarg_or_template { return $r; } |
| 658 |
/ & "-{" r:lang_variant_or_tpl { return $r; } |
| 659 |
// FIXME: The old parser's handleInternalLinks2 splits on [[, resulting |
| 660 |
// in sequences with odd number of brackets parsing as text, and sequences |
| 661 |
// with even number of brackets having its innermost pair parse as a |
| 662 |
// wikilink. For now, we faithfully reproduce what's found there but |
| 663 |
// wikitext, the language, shouldn't be defined by odd tokenizing behaviour |
| 664 |
// in the old parser. Flagging this for a future cleanup. |
| 665 |
/ $('[[' &'[')+ |
| 666 |
/ & '[' r:( wikilink / extlink ) { return $r; } |
| 667 |
/ & "'" r:quote { return $r; } |
| 668 |
|
| 669 |
/* Headings */ |
| 670 |
|
| 671 |
heading = |
| 672 |
& "=" // guard, to make sure '='+ will match. |
| 673 |
// XXX: Also check to end to avoid inline parsing? |
| 674 |
r:( |
| 675 |
s:$'='+ // moved in here to make s accessible to inner action |
| 676 |
ce:( |
| 677 |
(ill:inlineline<h>? { return $ill ?: []; }) |
| 678 |
$'='+ |
| 679 |
)? |
| 680 |
& { return $ce || strlen( $s ) > 2; } |
| 681 |
endTPos:("" { return $this->endOffset(); }) |
| 682 |
spc:( space / sol_transparent )* |
| 683 |
&eolf |
| 684 |
{ |
| 685 |
$c = null; |
| 686 |
$e = null; |
| 687 |
$level = null; |
| 688 |
if ( $ce ) { |
| 689 |
$c = $ce[0]; |
| 690 |
$e = $ce[1]; |
| 691 |
$level = min( strlen( $s ), strlen( $e ) ); |
| 692 |
} else { |
| 693 |
// split up equal signs into two equal parts, with at least |
| 694 |
// one character in the middle. |
| 695 |
$level = (int)floor( ( strlen( $s ) - 1 ) / 2 ); |
| 696 |
$c = [ str_repeat( '=', strlen( $s ) - 2 * $level ) ]; |
| 697 |
$s = $e = str_repeat( '=', $level ); |
| 698 |
} |
| 699 |
$level = min( 6, $level ); |
| 700 |
// convert surplus equals into text |
| 701 |
if ( strlen( $s ) > $level ) { |
| 702 |
$extras1 = substr( $s, 0, strlen( $s ) - $level ); |
| 703 |
if ( is_string( $c[0] ) ) { |
| 704 |
$c[0] = $extras1 . $c[0]; |
| 705 |
} else { |
| 706 |
array_unshift( $c, $extras1 ); |
| 707 |
} |
| 708 |
} |
| 709 |
if ( strlen( $e ) > $level ) { |
| 710 |
$extras2 = substr( $e, 0, strlen( $e ) - $level ); |
| 711 |
$lastElem = PHPUtils::lastItem( $c ); |
| 712 |
if ( is_string( $lastElem ) ) { |
| 713 |
$c[count( $c ) - 1] .= $extras2; |
| 714 |
} else { |
| 715 |
$c[] = $extras2; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
$tagDP = new DataParsoid; |
| 720 |
$tagDP->tsr = $this->tsrOffsets( 'start' ); |
| 721 |
$tagDP->tsr->end += $level; |
| 722 |
// Match the old parser's behavior by |
| 723 |
// (a) making headingIndex part of tokenizer state |
| 724 |
// (don't reuse pipeline! see $this->resetState above) |
| 725 |
// (b) assigning the index when ==*== is tokenized, |
| 726 |
// even if we're inside a template argument |
| 727 |
// or other context which won't end up putting the heading |
| 728 |
// on the output page. T213468/T214538 |
| 729 |
$this->headingIndex++; |
| 730 |
$tagDP->getTemp()->headingIndex = $this->headingIndex; |
| 731 |
$res = [ new TagTk( 'h' . $level, [], $tagDP ) ]; |
| 732 |
|
| 733 |
PHPUtils::pushArray( $res, $c ); |
| 734 |
|
| 735 |
$endTagDP = new DataParsoid; |
| 736 |
$endTagDP->tsr = new SourceRange( $endTPos - $level, $endTPos ); |
| 737 |
$res[] = new EndTagTk( 'h' . $level, [], $endTagDP ); |
| 738 |
$res[] = $spc; |
| 739 |
return $res; |
| 740 |
} |
| 741 |
) { return $r; } |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
// Behavior switches. See: |
| 746 |
// https://www.mediawiki.org/wiki/Help:Magic_words#Behavior_switches |
| 747 |
behavior_switch = |
| 748 |
bs:$('__' behavior_text '__') { |
| 749 |
if ( $this->siteConfig->isBehaviorSwitch( $bs ) ) { |
| 750 |
$dp = new DataParsoid; |
| 751 |
$dp->tsr = $this->tsrOffsets(); |
| 752 |
$dp->src = $bs; |
| 753 |
$dp->magicSrc = $bs; |
| 754 |
return [ |
| 755 |
new SelfclosingTagTk( 'behavior-switch', [ new KV( 'word', $bs ) ], $dp ) |
| 756 |
]; |
| 757 |
} else { |
| 758 |
return [ $bs ]; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
// Instead of defining a charset, the old parser's doDoubleUnderscore concats a |
| 763 |
// regexp of all the language specific aliases of the behavior switches and |
| 764 |
// then does a match and replace. Just be as permissive as possible and let the |
| 765 |
// BehaviorSwitchPreprocessor back out of any overreach. |
| 766 |
behavior_text = $( !'__' ( text_char / "-" ) )+ |
| 767 |
|
| 768 |
|
| 769 |
/************************************************************** |
| 770 |
* External (bracketed and autolinked) links |
| 771 |
**************************************************************/ |
| 772 |
|
| 773 |
autolink = |
| 774 |
! <extlink> |
| 775 |
// this must be a word boundary, so previous character must be non-word |
| 776 |
! { return Utils::isUniWord(Utils::lastUniChar( $this->input, $this->endOffset() ) ); } |
| 777 |
r:( |
| 778 |
autourl |
| 779 |
/ autoref |
| 780 |
/ isbn |
| 781 |
) { return $r; } |
| 782 |
|
| 783 |
extlink "extlink" = |
| 784 |
! <extlink> // extlink cannot be nested |
| 785 |
r:( |
| 786 |
"[" |
| 787 |
p0:( "" { return $this->endOffset(); }) |
| 788 |
flat:( |
| 789 |
addr:(url_protocol ipv6urladdr / "") |
| 790 |
target:(extlink_nonipv6url<extlink> / "") |
| 791 |
{ return TokenizerUtils::flattenString( [ $addr, $target ] ); } |
| 792 |
) |
| 793 |
& { |
| 794 |
// Protocol must be valid and there ought to be at least one |
| 795 |
// post-protocol character. So strip last char off target |
| 796 |
// before testing protocol. |
| 797 |
if ( is_array( $flat ) ) { |
| 798 |
// There are templates present, alas. |
| 799 |
return count( $flat ) > 0; |
| 800 |
} |
| 801 |
return Utils::isProtocolValid( substr( $flat, 0, -1 ), $this->env ); |
| 802 |
} |
| 803 |
p1:( "" { return $this->endOffset(); }) |
| 804 |
sp:$( space / unispace )* |
| 805 |
p2:( "" { return $this->endOffset(); }) |
| 806 |
content:inlineline<extlink>? |
| 807 |
p3:( "" { return $this->endOffset(); }) |
| 808 |
"]" { |
| 809 |
$tsr1 = new SourceRange( $p0, $p1 ); |
| 810 |
$tsr2 = new SourceRange( $p2, $p3 ); |
| 811 |
$dp = new DataParsoid; |
| 812 |
$dp->tsr = $this->tsrOffsets(); |
| 813 |
$dp->getTemp()->extLinkContentOffsets = $tsr2; |
| 814 |
return [ |
| 815 |
new SelfclosingTagTk( |
| 816 |
'extlink', |
| 817 |
[ |
| 818 |
new KV( 'href', $flat, $tsr1->expandTsrV() ), |
| 819 |
new KV( 'mw:content', $content ?? '', $tsr2->expandTsrV() ), |
| 820 |
new KV( 'spaces', $sp ) |
| 821 |
], |
| 822 |
$dp |
| 823 |
) |
| 824 |
]; } |
| 825 |
) { return $r; } |
| 826 |
|
| 827 |
RFC = &{ return $this->siteConfig->magicLinkEnabled("RFC"); } 'RFC' { |
| 828 |
return 'RFC'; |
| 829 |
} |
| 830 |
PMID = &{ return $this->siteConfig->magicLinkEnabled("PMID"); } 'PMID' { |
| 831 |
return 'PMID'; |
| 832 |
} |
| 833 |
|
| 834 |
autoref = |
| 835 |
ref:(RFC / PMID) sp:space_or_nbsp+ identifier:$[0-9]+ end_of_word |
| 836 |
{ |
| 837 |
$base_urls = [ |
| 838 |
'RFC' => 'https://datatracker.ietf.org/doc/html/rfc%s', |
| 839 |
'PMID' => '//www.ncbi.nlm.nih.gov/pubmed/%s?dopt=Abstract' |
| 840 |
]; |
| 841 |
$tsr = $this->tsrOffsets(); |
| 842 |
$dp = new DataParsoid; |
| 843 |
$dp->tsr = $tsr; |
| 844 |
$dp->stx = 'magiclink'; |
| 845 |
$this->env->getDataAccess()->addTrackingCategory( |
| 846 |
$this->env->getPageConfig(), |
| 847 |
$this->env->getMetadata(), |
| 848 |
'magiclink-tracking-' . strtolower($ref) |
| 849 |
); |
| 850 |
return [ |
| 851 |
new SelfclosingTagTk( 'extlink', [ |
| 852 |
new KV( 'href', sprintf( $base_urls[$ref], $identifier ) ), |
| 853 |
new KV( 'mw:content', TokenizerUtils::flattenString( [ $ref, $sp, $identifier ] ), $tsr->expandTsrV() ), |
| 854 |
new KV( 'typeof', 'mw:ExtLink/' . $ref ) |
| 855 |
], |
| 856 |
$dp |
| 857 |
) |
| 858 |
]; |
| 859 |
} |
| 860 |
|
| 861 |
isbn = |
| 862 |
&{ return $this->siteConfig->magicLinkEnabled("ISBN"); } |
| 863 |
'ISBN' sp:space_or_nbsp+ isbn:( |
| 864 |
[0-9] |
| 865 |
((space_or_nbsp_or_dash / "") [0-9])+ |
| 866 |
((space_or_nbsp_or_dash / "") [xX] / "") |
| 867 |
) |
| 868 |
isbncode:( |
| 869 |
end_of_word |
| 870 |
{ |
| 871 |
// Convert isbn token-and-entity array to stripped string. |
| 872 |
$stripped = ''; |
| 873 |
foreach ( TokenizerUtils::flattenStringlist( $isbn ) as $part ) { |
| 874 |
if ( is_string( $part ) ) { |
| 875 |
$stripped .= $part; |
| 876 |
} |
| 877 |
} |
| 878 |
return strtoupper( preg_replace( '/[^\dX]/i', '', $stripped ) ); |
| 879 |
} |
| 880 |
) |
| 881 |
&{ |
| 882 |
// ISBNs can only be 10 or 13 digits long (with a specific format) |
| 883 |
return strlen( $isbncode ) === 10 |
| 884 |
|| ( strlen( $isbncode ) === 13 && preg_match( '/^97[89]/', $isbncode ) ); |
| 885 |
} |
| 886 |
{ |
| 887 |
$tsr = $this->tsrOffsets(); |
| 888 |
$dp = new DataParsoid; |
| 889 |
$dp->stx = 'magiclink'; |
| 890 |
$dp->tsr = $tsr; |
| 891 |
$this->env->getDataAccess()->addTrackingCategory( |
| 892 |
$this->env->getPageConfig(), |
| 893 |
$this->env->getMetadata(), |
| 894 |
'magiclink-tracking-isbn' |
| 895 |
); |
| 896 |
return [ |
| 897 |
new SelfclosingTagTk( 'extlink', [ |
| 898 |
new KV( 'href', 'Special:BookSources/' . $isbncode ), |
| 899 |
new KV( 'mw:content', TokenizerUtils::flattenString( [ 'ISBN', $sp, $isbn ] ), $tsr->expandTsrV() ), |
| 900 |
new KV( 'typeof', 'mw:WikiLink/ISBN' ) |
| 901 |
], |
| 902 |
$dp |
| 903 |
) |
| 904 |
]; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
/* Default URL protocols in MediaWiki (see DefaultSettings). Normally |
| 909 |
* these can be configured dynamically. */ |
| 910 |
|
| 911 |
url_protocol = |
| 912 |
p:$( '//' / [A-Za-z] [-A-Za-z0-9+.]* ':' '//'? ) |
| 913 |
& { return Utils::isProtocolValid( $p, $this->env ); } |
| 914 |
{ return $p; } |
| 915 |
|
| 916 |
// no punctuation, and '{<' to trigger directives |
| 917 |
no_punctuation_char = [^ \]\[\r\n"'<>\x00-\x20\x7f&\u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000{] |
| 918 |
|
| 919 |
// this is the general url rule |
| 920 |
// on the PHP side, the path part matches EXT_LINK_URL_CLASS |
| 921 |
// which is '[^][<>"\\x00-\\x20\\x7F\p{Zs}]' |
| 922 |
url = |
| 923 |
proto:url_protocol |
| 924 |
addr:(ipv6urladdr / "") |
| 925 |
path:( |
| 926 |
!inline_breaks c:( |
| 927 |
no_punctuation_char |
| 928 |
/ comment |
| 929 |
/ tplarg_or_template |
| 930 |
/ ['{] |
| 931 |
/ ! ( "&" ( [lL][tT] / [gG][tT] ) ";" ) |
| 932 |
r:( |
| 933 |
& "&" he:htmlentity { return $he; } |
| 934 |
/ "&" |
| 935 |
) { return $r; } |
| 936 |
) { return $c; } |
| 937 |
)* |
| 938 |
// Must be at least one character after the protocol |
| 939 |
& { return $addr !== '' || count( $path ) > 0; } |
| 940 |
{ |
| 941 |
return TokenizerUtils::flattenString( array_merge( [ $proto, $addr ], $path ) ); |
| 942 |
} |
| 943 |
|
| 944 |
// this is the somewhat-restricted rule used in autolinks |
| 945 |
// See Parser::doMagicLinks and Parser.php::makeFreeExternalLink. |
| 946 |
// The `path` portion matches EXT_LINK_URL_CLASS, as in the general |
| 947 |
// url rule. As in PHP, we do some fancy fixup to yank out |
| 948 |
// trailing punctuation, perhaps including parentheses. |
| 949 |
autourl = |
| 950 |
! '//' // protocol-relative autolinks not allowed (T32269) |
| 951 |
r:( |
| 952 |
proto:url_protocol |
| 953 |
addr:(ipv6urladdr / "") |
| 954 |
path:( |
| 955 |
!inline_breaks c:( |
| 956 |
no_punctuation_char |
| 957 |
/ comment |
| 958 |
/ tplarg_or_template |
| 959 |
/ $("'" !"'") // single quotes are ok, double quotes are bad |
| 960 |
/ "{" |
| 961 |
/ ! ( rhe:raw_htmlentity &{ return $rhe === '<' || $rhe === '>' || $rhe === "\u{A0}"; } ) |
| 962 |
r:( |
| 963 |
& "&" he:htmlentity { return $he; } |
| 964 |
/ "&" |
| 965 |
) { return $r; } |
| 966 |
) { return $c; } |
| 967 |
)* |
| 968 |
{ |
| 969 |
// as in Parser.php::makeFreeExternalLink, we're going to |
| 970 |
// yank trailing punctuation out of this match. |
| 971 |
$url = TokenizerUtils::flattenStringlist( array_merge( [ $proto, $addr ], $path ) ); |
| 972 |
// only need to look at last element; HTML entities are strip-proof. |
| 973 |
$last = PHPUtils::lastItem( $url ); |
| 974 |
$trim = 0; |
| 975 |
if ( is_string( $last ) ) { |
| 976 |
$strip = TokenizerUtils::getAutoUrlTerminatingChars( in_array( '(', $path, true ) ); |
| 977 |
$trim = strspn( strrev( $last ), $strip ); |
| 978 |
$url[ count( $url ) - 1 ] = substr( $last, 0, strlen( $last ) - $trim ); |
| 979 |
} |
| 980 |
$url = TokenizerUtils::flattenStringlist( $url ); |
| 981 |
if ( count( $url ) === 1 && is_string( $url[0] ) && strlen( $url[0] ) <= strlen( $proto ) ) { |
| 982 |
return null; // ensure we haven't stripped everything: T106945 |
| 983 |
} |
| 984 |
$this->currPos -= $trim; |
| 985 |
return $url; |
| 986 |
} |
| 987 |
) |
| 988 |
&{ return $r !== null; } |
| 989 |
{ |
| 990 |
$tsr = $this->tsrOffsets(); |
| 991 |
$dp = new DataParsoid; |
| 992 |
$dp->tsr = $tsr; |
| 993 |
$res = [ new SelfclosingTagTk( 'urllink', [ new KV( 'href', $r, $tsr->expandTsrV() ) ], $dp ) ]; |
| 994 |
return $res; |
| 995 |
} |
| 996 |
|
| 997 |
// This is extracted from EXT_LINK_ADDR in Parser.php: a simplified |
| 998 |
// expression to match an IPv6 address. The IPv4 address and "at least |
| 999 |
// one character of a host name" portions are punted to the `path` |
| 1000 |
// component of the `autourl` and `url` productions |
| 1001 |
ipv6urladdr = |
| 1002 |
$( "[" [0-9A-Fa-f:.]+ "]" ) |
| 1003 |
|
| 1004 |
/************************************************************** |
| 1005 |
* Templates, -arguments and wikilinks |
| 1006 |
**************************************************************/ |
| 1007 |
|
| 1008 |
/* |
| 1009 |
* Precedence: template arguments win over templates. See |
| 1010 |
* http://www.mediawiki.org/wiki/Preprocessor_ABNF#Ideal_precedence |
| 1011 |
* 4: {{{{·}}}} → {·{{{·}}}·} |
| 1012 |
* 5: {{{{{·}}}}} → {{·{{{·}}}·}} |
| 1013 |
* 6: {{{{{{·}}}}}} → {{{·{{{·}}}·}}} |
| 1014 |
* 7: {{{{{{{·}}}}}}} → {·{{{·{{{·}}}·}}}·} |
| 1015 |
* This is only if close has > 3 braces; otherwise we just match open |
| 1016 |
* and close as we find them. |
| 1017 |
*/ |
| 1018 |
tplarg_or_template = |
| 1019 |
&'{{' |
| 1020 |
t:tplarg_or_template_guarded<intemplate=true> { |
| 1021 |
return $t; |
| 1022 |
} |
| 1023 |
|
| 1024 |
tplarg_or_template_guarded = |
| 1025 |
&('{{' &('{{{'+ !'{') tplarg) a:(template/broken_template) { return $a; } |
| 1026 |
/ a:$('{' &('{{{'+ !'{'))? b:tplarg { return [ $a, $b ]; } |
| 1027 |
/ a:$('{' &('{{' !'{'))? b:template { return [ $a, $b ]; } |
| 1028 |
/ broken_template |
| 1029 |
|
| 1030 |
tplarg_or_template_or_bust = |
| 1031 |
r:(tplarg_or_template / .)+ { return TokenizerUtils::flattenIfArray( $r ); } |
| 1032 |
|
| 1033 |
template = |
| 1034 |
template_preproc<&preproc="}}"> |
| 1035 |
|
| 1036 |
// The old preprocessor maintains a single stack of "closing token we |
| 1037 |
// are currently looking for", with no backtracking. This means that |
| 1038 |
// once you see `[[ {{` you are looking only for `}}` -- if that template |
| 1039 |
// turns out to be broken you will never pop the `}}` and there is no way |
| 1040 |
// to close the `[[`. Since the PEG tokenizer in Parsoid uses backtracking |
| 1041 |
// and parses in a single pass (instead of PHP's split preprocessor/parser) |
| 1042 |
// we have to be a little more careful when we emulate this behavior. |
| 1043 |
// If we use a rule like: |
| 1044 |
// template = "{{" tplname tplargs* "}}"? |
| 1045 |
// Then we end up having to reinterpret `tplname tplargs*` as a tlb if it |
| 1046 |
// turns out we never find the `}}`, which involves a lot of tedious gluing |
| 1047 |
// tokens back together with fingers crossed we haven't discarded any |
| 1048 |
// significant newlines/whitespace/etc. An alternative would be a rule like: |
| 1049 |
// broken_template = "{{" tlb |
| 1050 |
// but again, `template` is used in many different contexts; `tlb` isn't |
| 1051 |
// necessarily the right one to recursively invoke. Instead we get the |
| 1052 |
// broken template off of the PEGjs production stack by returning immediately |
| 1053 |
// after `{{`, but we set the "preproc" reference parameter to false (the |
| 1054 |
// reference parameter feature having been introduced for this sole purpose) |
| 1055 |
// to indicate to the parent rule that we're "still in" the {{ context and |
| 1056 |
// shouldn't ever inlineBreak for any closing tokens above this one. For |
| 1057 |
// example: |
| 1058 |
// [[Foo{{Bar]] |
| 1059 |
// This will match as: |
| 1060 |
// wikilink->text,template->text --> FAILS looking for }} |
| 1061 |
// backtracks, popping "bracket_bracket" and "brace_brace" off preproc stack |
| 1062 |
// wikilink->text,broken_template,text --> FAILS looking for ]] |
| 1063 |
// backtracks, popping "bracket_bracket" and false off preproc stack |
| 1064 |
// broken_wikilink,text,broken_template,text --> OK |
| 1065 |
// with [false, false] left on the preproc stack |
| 1066 |
|
| 1067 |
broken_template = |
| 1068 |
preproc:<&preproc> |
| 1069 |
t:"{{" { |
| 1070 |
$preproc = null; |
| 1071 |
return $t; |
| 1072 |
} |
| 1073 |
|
| 1074 |
template_preproc = |
| 1075 |
"{{" leadWS:$( nl_comment_space* ) |
| 1076 |
target:template_param_value |
| 1077 |
params:( |
| 1078 |
nl_comment_space* "|" |
| 1079 |
r:( |
| 1080 |
p0:("" { return $this->endOffset(); }) |
| 1081 |
v:nl_comment_space* |
| 1082 |
p1:("" { return $this->endOffset(); }) |
| 1083 |
&("|" / "}}") |
| 1084 |
{ |
| 1085 |
// empty argument |
| 1086 |
$tsr0 = new SourceRange( $p0, $p1 ); |
| 1087 |
return new KV( '', TokenizerUtils::flattenIfArray( $v ), $tsr0->expandTsrV() ); |
| 1088 |
} |
| 1089 |
/ template_param |
| 1090 |
) { return $r; } |
| 1091 |
)* |
| 1092 |
trailWS:$( nl_comment_space* ) |
| 1093 |
inline_breaks "}}" |
| 1094 |
{ |
| 1095 |
// Insert target as first positional attribute, so that it can be |
| 1096 |
// generically expanded. The TemplateHandler then needs to shift it out |
| 1097 |
// again. |
| 1098 |
array_unshift( $params, new KV( TokenizerUtils::flattenIfArray( $target['tokens'] ), '', $target['srcOffsets']->expandTsrK() ) ); |
| 1099 |
$dp = new DataParsoid; |
| 1100 |
$dp->tsr = $this->tsrOffsets(); |
| 1101 |
$dp->src = $this->text(); |
| 1102 |
$tmp = $dp->getTemp(); |
| 1103 |
$tmp->leadWS = $leadWS; |
| 1104 |
$tmp->trailWS = $trailWS; |
| 1105 |
$obj = new SelfclosingTagTk( 'template', $params, $dp ); |
| 1106 |
return $obj; |
| 1107 |
} |
| 1108 |
/ $('{{' space_or_newline* '}}') |
| 1109 |
|
| 1110 |
tplarg = |
| 1111 |
tplarg_preproc<&preproc="}}"> |
| 1112 |
|
| 1113 |
tplarg_preproc = |
| 1114 |
"{{{" |
| 1115 |
p:("" { return $this->endOffset(); }) |
| 1116 |
target:template_param_value? |
| 1117 |
params:( |
| 1118 |
nl_comment_space* "|" |
| 1119 |
r:( |
| 1120 |
p0:("" { return $this->endOffset(); }) |
| 1121 |
v:nl_comment_space* |
| 1122 |
p1:("" { return $this->endOffset(); }) |
| 1123 |
&("|" / "}}}") |
| 1124 |
{ |
| 1125 |
// empty argument |
| 1126 |
return [ 'tokens' => $v, 'srcOffsets' => new SourceRange( $p0, $p1 ) ]; |
| 1127 |
} |
| 1128 |
/ template_param_value |
| 1129 |
) { return $r; } |
| 1130 |
)* |
| 1131 |
nl_comment_space* |
| 1132 |
inline_breaks "}}}" |
| 1133 |
{ |
| 1134 |
$kvs = []; |
| 1135 |
|
| 1136 |
if ( $target === null ) { |
| 1137 |
$target = [ 'tokens' => '', 'srcOffsets' => new SourceRange( $p, $p ) ]; |
| 1138 |
} |
| 1139 |
// Insert target as first positional attribute, so that it can be |
| 1140 |
// generically expanded. The TemplateHandler then needs to shift it out |
| 1141 |
// again. |
| 1142 |
$kvs[] = new KV( TokenizerUtils::flattenIfArray( $target['tokens'] ), '', $target['srcOffsets']->expandTsrK() ); |
| 1143 |
|
| 1144 |
foreach ( $params as $o ) { |
| 1145 |
$s = $o['srcOffsets']; |
| 1146 |
$kvs[] = new KV( '', TokenizerUtils::flattenIfArray( $o['tokens'] ), $s->expandTsrV() ); |
| 1147 |
} |
| 1148 |
|
| 1149 |
$dp = new DataParsoid; |
| 1150 |
$dp->tsr = $this->tsrOffsets(); |
| 1151 |
$dp->src = $this->text(); |
| 1152 |
$obj = new SelfclosingTagTk( 'templatearg', $kvs, $dp ); |
| 1153 |
return $obj; |
| 1154 |
} |
| 1155 |
|
| 1156 |
template_param = |
| 1157 |
name:template_param_name |
| 1158 |
val:( |
| 1159 |
kEndPos:("" { return $this->endOffset(); }) |
| 1160 |
// no optionalSpaceToken here, it's eaten by template_param_name |
| 1161 |
"=" |
| 1162 |
vStartPos:("" { return $this->endOffset(); }) |
| 1163 |
optSp:optionalSpaceToken |
| 1164 |
tpv:template_param_value? { |
| 1165 |
return [ |
| 1166 |
'kEndPos' => $kEndPos, |
| 1167 |
'vStartPos' => $vStartPos, |
| 1168 |
'value' => TokenizerUtils::flattenString( [ $optSp, $tpv['tokens'] ?? [] ] ), |
| 1169 |
]; |
| 1170 |
} |
| 1171 |
)? { |
| 1172 |
if ( $val !== null ) { |
| 1173 |
if ( $val['value'] !== null ) { |
| 1174 |
$so = new KVSourceRange( |
| 1175 |
$this->startOffset(), $val['kEndPos'], |
| 1176 |
$val['vStartPos'], $this->endOffset() |
| 1177 |
); |
| 1178 |
return new KV( |
| 1179 |
$name, |
| 1180 |
TokenizerUtils::flattenIfArray( $val['value'] ), |
| 1181 |
$so |
| 1182 |
); |
| 1183 |
} else { |
| 1184 |
return new KV( |
| 1185 |
TokenizerUtils::flattenIfArray( $name ), |
| 1186 |
'', |
| 1187 |
$so |
| 1188 |
); |
| 1189 |
} |
| 1190 |
} else { |
| 1191 |
$so = new SourceRange( $this->startOffset(), $this->endOffset() ); |
| 1192 |
return new KV( |
| 1193 |
'', |
| 1194 |
TokenizerUtils::flattenIfArray( $name ), |
| 1195 |
$so->expandTsrV() |
| 1196 |
); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
// empty parameter |
| 1200 |
/ & [|}] { |
| 1201 |
$so = new SourceRange( $this->startOffset(), $this->endOffset() ); |
| 1202 |
return new KV( '', '', $so->expandTsrV() ); |
| 1203 |
} |
| 1204 |
|
| 1205 |
template_param_name = |
| 1206 |
template_param_text<equal> / (&'=' { return ''; }) |
| 1207 |
|
| 1208 |
template_param_value = |
| 1209 |
tpt:template_param_text<equal=false> |
| 1210 |
{ |
| 1211 |
return [ 'tokens' => $tpt, 'srcOffsets' => $this->tsrOffsets() ]; |
| 1212 |
} |
| 1213 |
|
| 1214 |
template_param_text = |
| 1215 |
il:(nested_block<table=false, extlink=false, templateArg=true, tableCellArg=false> / newlineToken)+ |
| 1216 |
{ |
| 1217 |
// il is guaranteed to be an array -- so, tu.flattenIfArray will |
| 1218 |
// always return an array |
| 1219 |
$r = TokenizerUtils::flattenIfArray( $il ); |
| 1220 |
if ( count( $r ) === 1 && is_string( $r[0] ) ) { |
| 1221 |
$r = $r[0]; |
| 1222 |
} |
| 1223 |
return $r; |
| 1224 |
} |
| 1225 |
|
| 1226 |
//// Language converter block markup of language variants: -{ ... }- |
| 1227 |
|
| 1228 |
// Note that "rightmost opening" precedence rule (see |
| 1229 |
// https://www.mediawiki.org/wiki/Preprocessor_ABNF ) means |
| 1230 |
// that neither -{{ nor -{{{ are parsed as a -{ token, although |
| 1231 |
// -{{{{ is (since {{{ has precedence over {{). |
| 1232 |
|
| 1233 |
lang_variant_or_tpl = |
| 1234 |
&('-{' &('{{{'+ !'{') tplarg) a:lang_variant { return $a; } |
| 1235 |
/ a:$('-' &('{{{'+ !'{')) b:tplarg { return [ $a, $b ]; } |
| 1236 |
/ a:$('-' &('{{' '{{{'* !'{')) b:template { return [ $a, $b ]; } |
| 1237 |
/ &'-{' a:lang_variant { return $a; } |
| 1238 |
|
| 1239 |
broken_lang_variant = |
| 1240 |
r:"-{" |
| 1241 |
preproc:<&preproc> |
| 1242 |
{ |
| 1243 |
$preproc = null; |
| 1244 |
return $r; |
| 1245 |
} |
| 1246 |
|
| 1247 |
lang_variant = |
| 1248 |
// FIXME: Maybe this should suppress "table" and "tableCellArg" like 'template_param_text' d too |
| 1249 |
lang_variant_preproc<&preproc="}-", extlink=false> |
| 1250 |
/ broken_lang_variant |
| 1251 |
|
| 1252 |
lang_variant_preproc = |
| 1253 |
lv0:("-{" { return $this->startOffset(); }) |
| 1254 |
f:( |
| 1255 |
&{ return $this->env->langConverterEnabled(); } |
| 1256 |
ff:opt_lang_variant_flags { |
| 1257 |
// if flags contains 'R', then don't treat ; or : specially inside. |
| 1258 |
if ( isset( $ff['flags'] ) ) { |
| 1259 |
$ff['raw'] = isset( $ff['flags']['R'] ) || isset( $ff['flags']['N'] ); |
| 1260 |
} elseif ( isset( $ff['variants'] ) ) { |
| 1261 |
$ff['raw'] = true; |
| 1262 |
} |
| 1263 |
return $ff; |
| 1264 |
} / |
| 1265 |
&{ return !$this->env->langConverterEnabled(); } |
| 1266 |
"" { |
| 1267 |
// if language converter not enabled, don't try to parse inside. |
| 1268 |
return [ 'raw' => true ]; |
| 1269 |
} |
| 1270 |
) |
| 1271 |
ts:( |
| 1272 |
&{ return $f['raw']; } |
| 1273 |
lv:lang_variant_text { return [ [ 'text' => $lv ] ]; } |
| 1274 |
/ |
| 1275 |
&{ return !$f['raw']; } |
| 1276 |
lv:lang_variant_option_list { return $lv; } |
| 1277 |
) |
| 1278 |
inline_breaks |
| 1279 |
lv1:("}-" { return $this->endOffset(); }) |
| 1280 |
{ |
| 1281 |
if ( !$this->env->langConverterEnabled() ) { |
| 1282 |
return [ '-{', $ts[0]['text']['tokens'], '}-' ]; |
| 1283 |
} |
| 1284 |
$lvsrc = substr( $this->input, $lv0, $lv1 - $lv0 ); |
| 1285 |
$attribs = []; |
| 1286 |
|
| 1287 |
foreach ( $ts as &$t ) { |
| 1288 |
// move token strings into KV attributes so that they are |
| 1289 |
// properly expanded by early stages of the token pipeline |
| 1290 |
foreach ( [ 'text', 'from', 'to' ] as $fld ) { |
| 1291 |
if ( !isset( $t[$fld] ) ) { |
| 1292 |
continue; |
| 1293 |
} |
| 1294 |
$name = 'mw:lv' . count( $attribs ); |
| 1295 |
// Note that AttributeExpander will expect the tokens array to be |
| 1296 |
// flattened. We do that in lang_variant_text / lang_variant_nowiki |
| 1297 |
$attribs[] = new KV( $name, $t[$fld]['tokens'], $t[$fld]['srcOffsets']->expandTsrV() ); |
| 1298 |
$t[$fld] = $name; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
unset( $t ); |
| 1302 |
|
| 1303 |
$flags = isset( $f['flags'] ) ? array_keys( $f['flags'] ) : []; |
| 1304 |
sort( $flags ); |
| 1305 |
$variants = isset( $f['variants'] ) ? array_keys( $f['variants'] ) : []; |
| 1306 |
sort( $variants ); |
| 1307 |
|
| 1308 |
$dp = new DataParsoid; |
| 1309 |
$dp->tsr = new SourceRange( $lv0, $lv1 ); |
| 1310 |
$dp->src = $lvsrc; |
| 1311 |
$dp->flags = $flags; |
| 1312 |
$dp->variants = $variants; |
| 1313 |
$dp->original = $f['original']; |
| 1314 |
$dp->flagSp = $f['sp']; |
| 1315 |
$dp->texts = $ts; |
| 1316 |
|
| 1317 |
return [ |
| 1318 |
new SelfclosingTagTk( |
| 1319 |
'language-variant', |
| 1320 |
$attribs, |
| 1321 |
$dp |
| 1322 |
) |
| 1323 |
]; |
| 1324 |
} |
| 1325 |
|
| 1326 |
opt_lang_variant_flags = |
| 1327 |
f:( ff:lang_variant_flags "|" { return $ff; } )? |
| 1328 |
{ |
| 1329 |
// Collect & separate flags and variants into a hashtable (by key) and ordered list |
| 1330 |
$flags = []; |
| 1331 |
$variants = []; |
| 1332 |
$flagList = []; |
| 1333 |
$flagSpace = []; |
| 1334 |
$variantList = []; |
| 1335 |
$variantSpace = []; |
| 1336 |
$useVariants = false; |
| 1337 |
if ( $f !== null ) { |
| 1338 |
// lang_variant_flags returns arrays in reverse order. |
| 1339 |
$spPtr = count( $f['sp'] ) - 1; |
| 1340 |
for ( $i = count( $f['flags'] ) - 1; $i >= 0; $i--) { |
| 1341 |
$item = $f['flags'][$i]; |
| 1342 |
if ( isset( $item['flag'] ) ) { |
| 1343 |
$flagSpace[] = $f['sp'][$spPtr--]; |
| 1344 |
$flags[$item['flag']] = true; |
| 1345 |
$flagList[] = $item['flag']; |
| 1346 |
$flagSpace[] = $f['sp'][$spPtr--]; |
| 1347 |
} |
| 1348 |
if ( isset( $item['variant'] ) ) { |
| 1349 |
$variantSpace[] = $f['sp'][$spPtr--]; |
| 1350 |
$variants[$item['variant']] = true; |
| 1351 |
$variantList[] = $item['variant']; |
| 1352 |
$variantSpace[] = $f['sp'][$spPtr--]; |
| 1353 |
} |
| 1354 |
} |
| 1355 |
if ( $spPtr >= 0 ) { |
| 1356 |
// handle space after a trailing semicolon |
| 1357 |
$flagSpace[] = $f['sp'][$spPtr]; |
| 1358 |
$variantSpace[] = $f['sp'][$spPtr]; |
| 1359 |
} |
| 1360 |
} |
| 1361 |
// Parse flags (this logic is from core/languages/ConverterRule.php |
| 1362 |
// in the parseFlags() function) |
| 1363 |
if ( count( $flags ) === 0 && count( $variants ) === 0 ) { |
| 1364 |
$flags['$S'] = true; |
| 1365 |
} elseif ( isset( $flags['R'] ) ) { |
| 1366 |
$flags = [ 'R' => true ]; // remove other flags |
| 1367 |
} elseif ( isset( $flags['N'] ) ) { |
| 1368 |
$flags = [ 'N' => true ]; // remove other flags |
| 1369 |
} elseif ( isset( $flags['-'] ) ) { |
| 1370 |
$flags = [ '-' => true ]; // remove other flags |
| 1371 |
} elseif ( isset( $flags['T'] ) && count( $flags ) === 1 ) { |
| 1372 |
$flags['H'] = true; |
| 1373 |
} elseif ( isset( $flags['H'] ) ) { |
| 1374 |
// Replace A flag, and remove other flags except T and D |
| 1375 |
$nf = [ '$+' => true, 'H' => true ]; |
| 1376 |
if ( isset( $flags['T'] ) ) { $nf['T'] = true; } |
| 1377 |
if ( isset( $flags['D'] ) ) { $nf['D'] = true; } |
| 1378 |
$flags = $nf; |
| 1379 |
} elseif ( count( $variants ) > 0 ) { |
| 1380 |
$useVariants = true; |
| 1381 |
} else { |
| 1382 |
if ( isset( $flags['A'] ) ) { |
| 1383 |
$flags['$+'] = true; |
| 1384 |
$flags['$S'] = true; |
| 1385 |
} |
| 1386 |
if ( isset( $flags['D'] ) ) { |
| 1387 |
unset( $flags['$S'] ); |
| 1388 |
} |
| 1389 |
} |
| 1390 |
if ( $useVariants ) { |
| 1391 |
return [ 'variants' => $variants, 'original' => $variantList, 'sp' => $variantSpace ]; |
| 1392 |
} else { |
| 1393 |
return [ 'flags' => $flags, 'original' => $flagList, 'sp' => $flagSpace ]; |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
lang_variant_flags = |
| 1398 |
sp1:$(space_or_newline*) f:lang_variant_flag sp2:$(space_or_newline*) |
| 1399 |
more:( ";" lang_variant_flags? )? |
| 1400 |
{ |
| 1401 |
$r = ( $more && $more[1] ) ? $more[1] : [ 'sp' => [], 'flags' => [] ]; |
| 1402 |
// Note that sp and flags are in reverse order, since we're using |
| 1403 |
// right recursion and want to push instead of unshift. |
| 1404 |
$r['sp'][] = $sp2; |
| 1405 |
$r['sp'][] = $sp1; |
| 1406 |
$r['flags'][] = $f; |
| 1407 |
return $r; |
| 1408 |
} |
| 1409 |
/ sp:$(space_or_newline*) { |
| 1410 |
return [ 'sp' => [ $sp ], 'flags' => [] ]; |
| 1411 |
} |
| 1412 |
|
| 1413 |
lang_variant_flag = |
| 1414 |
f:[-+A-Z] { return [ 'flag' => $f ]; } |
| 1415 |
/ v:lang_variant_name { return [ 'variant' => $v ]; } |
| 1416 |
/ b:$(!space_or_newline !nowiki [^{}|;])+ { return [ 'bogus' => $b ]; /* bad flag */} |
| 1417 |
|
| 1418 |
// language variant name, like zh, zh-cn, etc. |
| 1419 |
lang_variant_name = |
| 1420 |
$([a-z] [-a-zA-Z]+) |
| 1421 |
// Escaped otherwise-unrepresentable language names |
| 1422 |
// Primarily for supporting html2html round trips; PHP doesn't support |
| 1423 |
// using nowikis here (yet!) |
| 1424 |
/ nowiki_text |
| 1425 |
|
| 1426 |
lang_variant_option_list = |
| 1427 |
o:lang_variant_option rest:( ";" oo:lang_variant_option { return $oo; })* |
| 1428 |
tr:( ";" $bogus_lang_variant_option )* // optional trailing crap |
| 1429 |
{ |
| 1430 |
array_unshift( $rest, $o ); |
| 1431 |
// if the last bogus option is just spaces, keep them; otherwise |
| 1432 |
// drop all this bogus stuff on the ground |
| 1433 |
if ( count($tr) > 0 ) { |
| 1434 |
$last = $tr[count($tr)-1]; |
| 1435 |
if (preg_match('/^\s*$/Du', $last[1])) { |
| 1436 |
$rest[] = [ 'semi' => true, 'sp' => $last[1] ]; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
return $rest; |
| 1440 |
} |
| 1441 |
/ lvtext:lang_variant_text { return [ [ 'text' => $lvtext ] ]; } |
| 1442 |
|
| 1443 |
bogus_lang_variant_option = |
| 1444 |
lang_variant_text? |
| 1445 |
|
| 1446 |
lang_variant_option = |
| 1447 |
sp1:$(space_or_newline*) lang:lang_variant_name |
| 1448 |
sp2:$(space_or_newline*) ":" |
| 1449 |
sp3:$(space_or_newline*) |
| 1450 |
lvtext:(lang_variant_nowiki / lang_variant_text_no_semi) |
| 1451 |
{ |
| 1452 |
return [ |
| 1453 |
'twoway' => true, |
| 1454 |
'lang' => $lang, |
| 1455 |
'text' => $lvtext, |
| 1456 |
'sp' => [ $sp1, $sp2, $sp3 ] |
| 1457 |
]; |
| 1458 |
} |
| 1459 |
/ sp1:$(space_or_newline*) |
| 1460 |
from:(lang_variant_nowiki / lang_variant_text_no_semi_or_arrow) |
| 1461 |
"=>" |
| 1462 |
sp2:$(space_or_newline*) lang:lang_variant_name |
| 1463 |
sp3:$(space_or_newline*) ":" |
| 1464 |
sp4:$(space_or_newline*) |
| 1465 |
to:(lang_variant_nowiki / lang_variant_text_no_semi) |
| 1466 |
{ |
| 1467 |
return [ |
| 1468 |
'oneway' => true, |
| 1469 |
'from' => $from, |
| 1470 |
'lang' => $lang, |
| 1471 |
'to' => $to, |
| 1472 |
'sp' => [ $sp1, $sp2, $sp3, $sp4 ] |
| 1473 |
]; |
| 1474 |
} |
| 1475 |
|
| 1476 |
// html2wt support: If a language name or conversion string can't be |
| 1477 |
// represented w/o breaking wikitext, just wrap it in a <nowiki>. |
| 1478 |
// PHP doesn't support this (yet), but Parsoid does. |
| 1479 |
lang_variant_nowiki = |
| 1480 |
n:nowiki_text |
| 1481 |
sp:$space_or_newline* { |
| 1482 |
$tsr = $this->tsrOffsets(); |
| 1483 |
$tsr->end -= strlen( $sp ); |
| 1484 |
return [ |
| 1485 |
'tokens' => [ $n ], |
| 1486 |
'srcOffsets' => $tsr, |
| 1487 |
]; |
| 1488 |
} |
| 1489 |
|
| 1490 |
lang_variant_text = |
| 1491 |
tokens:(inlineline / "|" )* |
| 1492 |
{ |
| 1493 |
return [ |
| 1494 |
'tokens' => TokenizerUtils::flattenStringlist( $tokens ), |
| 1495 |
'srcOffsets' => $this->tsrOffsets(), |
| 1496 |
]; |
| 1497 |
} |
| 1498 |
|
| 1499 |
lang_variant_text_no_semi = |
| 1500 |
lang_variant_text<semicolon> |
| 1501 |
|
| 1502 |
lang_variant_text_no_semi_or_arrow = |
| 1503 |
lang_variant_text_no_semi<arrow> |
| 1504 |
|
| 1505 |
wikilink_content = |
| 1506 |
( |
| 1507 |
pipe |
| 1508 |
startPos:("" { return $this->endOffset(); }) |
| 1509 |
lt:link_text? { |
| 1510 |
$tsr = new SourceRange( $startPos, $this->endOffset() ); |
| 1511 |
$maybeContent = new KV( 'mw:maybeContent', $lt ?? [], $tsr->expandTsrV() ); |
| 1512 |
$maybeContent->vsrc = substr( $this->input, $startPos, $this->endOffset() - $startPos ); |
| 1513 |
return $maybeContent; |
| 1514 |
} |
| 1515 |
)* |
| 1516 |
|
| 1517 |
wikilink = |
| 1518 |
wikilink_preproc<&preproc="]]"> |
| 1519 |
/ broken_wikilink |
| 1520 |
|
| 1521 |
// `broken-link` (see [[:mw:Preprocessor_ABNF]]), but careful because the |
| 1522 |
// second bracket could start an extlink. Set preproc to false as a reference |
| 1523 |
// parameter in the parent since we haven't seen a double-close bracket. |
| 1524 |
// (See full explanation above broken_template production.) |
| 1525 |
broken_wikilink = |
| 1526 |
&"[[" |
| 1527 |
preproc:<&preproc> |
| 1528 |
&{ $preproc = null; return true; } |
| 1529 |
a:("[" (extlink / "[")) { |
| 1530 |
return $a; |
| 1531 |
} |
| 1532 |
|
| 1533 |
wikilink_preproc = |
| 1534 |
"[[" |
| 1535 |
spos:("" { return $this->endOffset(); }) |
| 1536 |
target:wikilink_preprocessor_text? |
| 1537 |
tpos:("" { return $this->endOffset(); }) |
| 1538 |
lcs:wikilink_content |
| 1539 |
inline_breaks "]]" |
| 1540 |
{ |
| 1541 |
$pipeTrick = count( $lcs ) === 1 && count( $lcs[0]->v ) === 0; |
| 1542 |
$textTokens = []; |
| 1543 |
if ( $target === null || $pipeTrick ) { |
| 1544 |
$textTokens[] = '[['; |
| 1545 |
if ( $target ) { |
| 1546 |
$textTokens[] = $target; |
| 1547 |
} |
| 1548 |
foreach ( $lcs as $a ) { |
| 1549 |
// a is a mw:maybeContent attribute |
| 1550 |
$textTokens[] = '|'; |
| 1551 |
if ( count( $a->v ) > 0 ) { |
| 1552 |
$textTokens[] = $a->v; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
$textTokens[] = ']]'; |
| 1556 |
return $textTokens; |
| 1557 |
} |
| 1558 |
$obj = new SelfclosingTagTk( 'wikilink' ); |
| 1559 |
$tsr = new SourceRange( $spos, $tpos ); |
| 1560 |
$hrefKV = new KV( 'href', $target, $tsr->expandTsrV() ); |
| 1561 |
$hrefKV->vsrc = $tsr->substr( $this->input ); |
| 1562 |
// XXX: Point to object with path, revision and input information |
| 1563 |
// obj.source = input; |
| 1564 |
$obj->attribs[] = $hrefKV; |
| 1565 |
$obj->attribs = array_merge( $obj->attribs, $lcs ); |
| 1566 |
$dp = new DataParsoid; |
| 1567 |
$dp->tsr = $this->tsrOffsets(); |
| 1568 |
$dp->src = $this->text(); |
| 1569 |
$obj->dataParsoid = $dp; |
| 1570 |
return [ $obj ]; |
| 1571 |
} |
| 1572 |
|
| 1573 |
// Tables are allowed inside image captions. |
| 1574 |
// Suppress the equal flag temporarily in this rule to consume the '=' here. |
| 1575 |
link_text = link_text_parameterized<equal = false, linkdesc = true> |
| 1576 |
|
| 1577 |
link_text_parameterized = |
| 1578 |
c:( |
| 1579 |
// This group is similar to "block_line" but "list_item" |
| 1580 |
// is omitted since `doBlockLevels` happens after |
| 1581 |
// `handleInternalLinks2`, where newlines are stripped. |
| 1582 |
(sol (heading / hr / full_table_in_link_caption)) |
| 1583 |
/ urltext |
| 1584 |
/ ( |
| 1585 |
!inline_breaks |
| 1586 |
r:( inline_element / '[' text_char+ ']' $(&(!']' / ']]')) / . ) { return $r; } |
| 1587 |
) |
| 1588 |
)+ { |
| 1589 |
return TokenizerUtils::flattenStringlist( $c ); |
| 1590 |
} |
| 1591 |
|
| 1592 |
/* Generic quote rule for italic and bold, further processed in a token |
| 1593 |
* stream transformation in doQuotes. Relies on NlTk tokens being emitted |
| 1594 |
* for each line of text to balance quotes per line. |
| 1595 |
* |
| 1596 |
* We are not using a simple pair rule here as we need to support mis-nested |
| 1597 |
* bolds/italics and MediaWiki's special heuristics for apostrophes, which are |
| 1598 |
* all not context free. */ |
| 1599 |
quote = |
| 1600 |
quotes:$("''" "'"*) { |
| 1601 |
// sequences of four or more than five quotes are assumed to start |
| 1602 |
// with some number of plain-text apostrophes. |
| 1603 |
$plainticks = 0; |
| 1604 |
$result = []; |
| 1605 |
if ( strlen( $quotes ) === 4 ) { |
| 1606 |
$plainticks = 1; |
| 1607 |
} elseif ( strlen( $quotes ) > 5 ) { |
| 1608 |
$plainticks = strlen( $quotes ) - 5; |
| 1609 |
} |
| 1610 |
if ( $plainticks > 0 ) { |
| 1611 |
$result[] = substr( $quotes, 0, $plainticks ); |
| 1612 |
} |
| 1613 |
// mw-quote token will be consumed in token transforms |
| 1614 |
$tsr = $this->tsrOffsets(); |
| 1615 |
$tsr->start += $plainticks; |
| 1616 |
$dp = new DataParsoid; |
| 1617 |
$dp->tsr = $tsr; |
| 1618 |
$mwq = new SelfclosingTagTk( 'mw-quote', |
| 1619 |
[ new KV( 'value', substr( $quotes, $plainticks ) ) ], |
| 1620 |
$dp ); |
| 1621 |
if ( strlen( $quotes ) > 2 ) { |
| 1622 |
$mwq->addAttribute( 'isSpace_1', $tsr->start > 0 && substr( $this->input, $tsr->start - 1, 1 ) === ' '); |
| 1623 |
$mwq->addAttribute( 'isSpace_2', $tsr->start > 1 && substr( $this->input, $tsr->start - 2, 1 ) === ' '); |
| 1624 |
} |
| 1625 |
$result[] = $mwq; |
| 1626 |
return $result; |
| 1627 |
} |
| 1628 |
|
| 1629 |
|
| 1630 |
/*********************************************************** |
| 1631 |
* Xmlish tags |
| 1632 |
***********************************************************/ |
| 1633 |
|
| 1634 |
// FIXME: Temporary (?) hack to let us not horribly break on old tvar syntax |
| 1635 |
// In coordination with language team, get rid of this hack once all old uses |
| 1636 |
// are migrated to new syntax (T274881). |
| 1637 |
tvar_old_syntax_closing_HACK = |
| 1638 |
"/>" |
| 1639 |
& { return $this->env->hasAnnotations && $this->siteConfig->isAnnotationTag( 'tvar' ); } |
| 1640 |
{ |
| 1641 |
$metaAttrs = [ new KV( 'typeof', 'mw:Annotation/tvar/End' ) ]; |
| 1642 |
$dp = new DataParsoid(); |
| 1643 |
$dp->tsr = $this->tsrOffsets(); |
| 1644 |
$dp->tsr->start--; // For "<" matched at the start of xmlish_tag rule |
| 1645 |
if ( empty( $this->pipelineOpts['inTemplate'] ) ) { |
| 1646 |
return [ new SelfclosingTagTk ( 'meta', $metaAttrs, $dp ) ]; |
| 1647 |
} else { |
| 1648 |
// suppress meta tags from pipeline output |
| 1649 |
return []; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
|
| 1653 |
annotation_tag = |
| 1654 |
annToken:extension_annotation_tag |
| 1655 |
&{ |
| 1656 |
return ( $annToken instanceof Token && $annToken->getName() !== 'extension' ); |
| 1657 |
} |
| 1658 |
{ return $annToken; } |
| 1659 |
|
| 1660 |
extension_annotation_tag = |
| 1661 |
!<annOrExtTag> |
| 1662 |
"<" tag:( |
| 1663 |
extToken:xmlish_tag<annOrExtTag> |
| 1664 |
// Account for `maybeAnnotationOrExtensionTag` returning unmatched start / end tags |
| 1665 |
&{ return !$extToken || $extToken[0]->getName() === 'extension' || |
| 1666 |
($extToken[0]->getName() === 'meta' && preg_match( WTUtils::ANNOTATION_META_TYPE_REGEXP, $extToken[0]->getAttributeV( 'typeof' ) ?? '' ) > 0); } |
| 1667 |
{ return !$extToken ? '' : $extToken[0]; } |
| 1668 |
/ tvar_old_syntax_closing_HACK ) { return $tag; } |
| 1669 |
|
| 1670 |
nowiki = |
| 1671 |
& ("<" "/"? "nowiki"i ) |
| 1672 |
extToken:extension_annotation_tag |
| 1673 |
{ return $extToken; } |
| 1674 |
|
| 1675 |
// Used by lang_variant productions to protect special language names or |
| 1676 |
// conversion strings. |
| 1677 |
nowiki_text = |
| 1678 |
extToken:nowiki |
| 1679 |
{ |
| 1680 |
$txt = Utils::extractExtBody( $extToken ); |
| 1681 |
return Utils::decodeWtEntities( $txt ); |
| 1682 |
} |
| 1683 |
|
| 1684 |
/* Generic XML-like tags |
| 1685 |
* |
| 1686 |
* These also cover extensions (including Cite), which will hook into the |
| 1687 |
* token stream for further processing. The content of extension tags is |
| 1688 |
* parsed as regular inline, but the source positions of the tag are added |
| 1689 |
* to allow reconstructing the unparsed text from the input. */ |
| 1690 |
|
| 1691 |
// See http://www.w3.org/TR/html5/syntax.html#tag-open-state and the following |
| 1692 |
// paragraphs. Note that we don't enforce ascii alpha for the first character |
| 1693 |
// here because we need to be more permissive for extension tag names. That |
| 1694 |
// happens in xmlish_tag below. |
| 1695 |
tag_name = $[^\t\n\v />\0]+ |
| 1696 |
|
| 1697 |
// This rule is used in carefully crafted places of xmlish tag tokenizing with |
| 1698 |
// the inclusion of solidus to match where the spec would ignore those |
| 1699 |
// characters. In particular, it does not belong in between attribute name |
| 1700 |
// and value. |
| 1701 |
space_or_newline_or_solidus = space_or_newline / (s:"/" !">" { return $s; }) |
| 1702 |
|
| 1703 |
xmlish_tag = |
| 1704 |
& { |
| 1705 |
$this->assert( |
| 1706 |
$this->input[$this->currPos - 1] === '<', |
| 1707 |
'Failed to open xmlish_tag before entering.' |
| 1708 |
); |
| 1709 |
return true; |
| 1710 |
} |
| 1711 |
end:"/"? |
| 1712 |
name: tag_name |
| 1713 |
annOrExtTag: <annOrExtTag> |
| 1714 |
& { |
| 1715 |
if ( $annOrExtTag ) { |
| 1716 |
return WTUtils::isAnnOrExtTag( $this->env, $name ); |
| 1717 |
} else { |
| 1718 |
// Only enforce ascii alpha first char for non-extension tags. |
| 1719 |
// See tag_name above for the details. |
| 1720 |
return preg_match( '/^[A-Za-z]/', $name ) && $this->isXMLTag( $name ); |
| 1721 |
} |
| 1722 |
} |
| 1723 |
// By the time we get to `doTableStuff` in the old parser, we've already |
| 1724 |
// safely encoded element attributes. See 55313f4e in core. |
| 1725 |
attribs:generic_newline_attributes<table=false, tableCellArg=false> |
| 1726 |
space_or_newline_or_solidus* // No need to preserve this -- canonicalize on RT via dirty diff |
| 1727 |
selfclose:"/"? |
| 1728 |
space* // not preserved - canonicalized on RT via dirty diff |
| 1729 |
">" |
| 1730 |
{ |
| 1731 |
$lcName = mb_strtolower( $name ); |
| 1732 |
|
| 1733 |
// Extension tags don't necessarily have the same semantics as html tags, |
| 1734 |
// so don't treat them as void elements. |
| 1735 |
$isVoidElt = Utils::isVoidElement( $lcName ) && !$annOrExtTag; |
| 1736 |
|
| 1737 |
// Support </br> |
| 1738 |
if ( $lcName === 'br' && $end ) { |
| 1739 |
$end = null; |
| 1740 |
} |
| 1741 |
|
| 1742 |
$tsr = $this->tsrOffsets(); |
| 1743 |
$tsr->start--; // For "<" matched at the start of xmlish_tag rule |
| 1744 |
$res = TokenizerUtils::buildXMLTag( $name, $lcName, $attribs, $end, !!$selfclose || $isVoidElt, $tsr ); |
| 1745 |
|
| 1746 |
// change up data-attribs in one scenario |
| 1747 |
// void-elts that aren't self-closed ==> useful for accurate RT-ing |
| 1748 |
if ( !$selfclose && $isVoidElt ) { |
| 1749 |
unset( $res->dataParsoid->selfClose ); |
| 1750 |
$res->dataParsoid->noClose = true; |
| 1751 |
} |
| 1752 |
|
| 1753 |
$met = $this->maybeAnnotationOrExtensionTag( $res, $end, $attribs, $tsr ); |
| 1754 |
return is_array( $met ) ? $met : [ $met ]; |
| 1755 |
} |
| 1756 |
|
| 1757 |
// A generic attribute that can span multiple lines. |
| 1758 |
generic_newline_attribute = |
| 1759 |
space_or_newline_or_solidus* |
| 1760 |
namePos0:("" { return $this->endOffset(); }) |
| 1761 |
name:generic_attribute_name |
| 1762 |
namePos1:("" { return $this->endOffset(); }) |
| 1763 |
vd:(space_or_newline* "=" v:generic_att_value? { return $v; })? |
| 1764 |
{ |
| 1765 |
// NB: Keep in sync w/ table_attibute |
| 1766 |
$res = null; |
| 1767 |
// Encapsulate protected attributes. |
| 1768 |
if ( is_string( $name ) ) { |
| 1769 |
$name = TokenizerUtils::protectAttrs( $name ); |
| 1770 |
} |
| 1771 |
$nameSO = new SourceRange( $namePos0, $namePos1 ); |
| 1772 |
if ( $vd !== null ) { |
| 1773 |
$res = new KV( $name, $vd['value'], $nameSO->join( $vd['srcOffsets'] ) ); |
| 1774 |
$res->vsrc = $vd['srcOffsets']->substr( $this->input ); |
| 1775 |
} else { |
| 1776 |
$res = new KV( $name, '', $nameSO->expandTsrK() ); |
| 1777 |
} |
| 1778 |
if ( is_array( $name ) ) { |
| 1779 |
$res->ksrc = $nameSO->substr( $this->input ); |
| 1780 |
} |
| 1781 |
return $res; |
| 1782 |
} |
| 1783 |
|
| 1784 |
// A single-line attribute. |
| 1785 |
table_attribute = |
| 1786 |
s:optionalSpaceToken |
| 1787 |
namePos0:("" { return $this->endOffset(); }) |
| 1788 |
name:table_attribute_name |
| 1789 |
namePos1:("" { return $this->endOffset(); }) |
| 1790 |
vd:(optionalSpaceToken "=" v:table_att_value? { return $v; })? |
| 1791 |
{ |
| 1792 |
// NB: Keep in sync w/ generic_newline_attribute |
| 1793 |
$res = null; |
| 1794 |
// Encapsulate protected attributes. |
| 1795 |
if ( gettype( $name ) === 'string' ) { |
| 1796 |
$name = TokenizerUtils::protectAttrs( $name ); |
| 1797 |
} |
| 1798 |
$nameSO = new SourceRange( $namePos0, $namePos1 ); |
| 1799 |
if ( $vd !== null ) { |
| 1800 |
$res = new KV( $name, $vd['value'], $nameSO->join( $vd['srcOffsets'] ) ); |
| 1801 |
$res->vsrc = $vd['srcOffsets']->substr( $this->input ); |
| 1802 |
} else { |
| 1803 |
$res = new KV( $name, '', $nameSO->expandTsrK() ); |
| 1804 |
} |
| 1805 |
if ( is_array( $name ) ) { |
| 1806 |
$res->ksrc = $nameSO->substr( $this->input ); |
| 1807 |
} |
| 1808 |
return $res; |
| 1809 |
} |
| 1810 |
|
| 1811 |
// The old parser's Sanitizer::removeHTMLtags explodes on < so that it can't |
| 1812 |
// be found anywhere in xmlish tags. This is a divergence from html5 tokenizing |
| 1813 |
// which happily permits it in attribute positions. Extension tags being the |
| 1814 |
// exception, since they're stripped beforehand. |
| 1815 |
less_than = |
| 1816 |
$( |
| 1817 |
&<annOrExtTag> |
| 1818 |
"<" |
| 1819 |
) |
| 1820 |
|
| 1821 |
// The arrangement of chars is to emphasize the split between what's disallowed |
| 1822 |
// by html5 and what's necessary to give directive a chance. |
| 1823 |
// See: http://www.w3.org/TR/html5/syntax.html#attributes-0 |
| 1824 |
generic_attribute_name = |
| 1825 |
q:$(["'=]?) // From #before-attribute-name-state, < is omitted for directive |
| 1826 |
r:( |
| 1827 |
$[^ \t\r\n\0/=><&{}\-!|]+ |
| 1828 |
/ !inline_breaks |
| 1829 |
// \0/=> is the html5 attribute name set we do not want. |
| 1830 |
t:( |
| 1831 |
directive |
| 1832 |
/ less_than |
| 1833 |
/ $( !( space_or_newline / [\0/=><] ) . ) |
| 1834 |
) { return $t; } |
| 1835 |
)* |
| 1836 |
& { return count( $r ) > 0 || $q !== ''; } |
| 1837 |
{ |
| 1838 |
array_unshift( $r, $q ); |
| 1839 |
return TokenizerUtils::flattenString( $r ); |
| 1840 |
} |
| 1841 |
|
| 1842 |
// Also accept these chars in a wikitext table or tr attribute name position. |
| 1843 |
// They are normally not matched by the table_attribute_name. |
| 1844 |
broken_table_attribute_name_char = c:[\0/=>] { return new KV( $c, '' ); } |
| 1845 |
|
| 1846 |
// Same as generic_attribute_name, except for accepting tags and wikilinks. |
| 1847 |
// (That doesn't make sense (ie. match the old parser) in the generic case.) |
| 1848 |
// We also give a chance to break on \[ (see T2553). |
| 1849 |
table_attribute_name = |
| 1850 |
q:$(["'=]?) // From #before-attribute-name-state, < is omitted for directive |
| 1851 |
r:( |
| 1852 |
$[^ \t\r\n\0/=><&{}\-!|\[]+ |
| 1853 |
/ !inline_breaks |
| 1854 |
// \0/=> is the html5 attribute name set we do not want. |
| 1855 |
t:( |
| 1856 |
$wikilink |
| 1857 |
/ directive |
| 1858 |
// Accept tags-inside-attributes as attribute names. |
| 1859 |
// The sanitizer will strip and shadow them for roundtripping. |
| 1860 |
// Example: <hiddentext>generated with.. </hiddentext> |
| 1861 |
/ x:inline_xmlish_tag ill:inlineline? { return array_merge( $x, $ill ?: [] ); } |
| 1862 |
/ $( !( space_or_newline / [\0/=>] ) . ) |
| 1863 |
) { return $t; } |
| 1864 |
)* |
| 1865 |
& { return count( $r ) > 0 || $q !== ''; } |
| 1866 |
{ |
| 1867 |
array_unshift( $r, $q ); |
| 1868 |
return TokenizerUtils::flattenString( $r ); |
| 1869 |
} |
| 1870 |
|
| 1871 |
// Attribute value, quoted variants can span multiple lines. |
| 1872 |
// Missing end quote: accept /> look-ahead as heuristic. |
| 1873 |
// These need to be kept in sync with the attribute_preprocessor_text_* |
| 1874 |
generic_att_value = |
| 1875 |
s:$(space_or_newline* "'") t:attribute_preprocessor_text_single? q:$("'" / &('/'? '>')) { |
| 1876 |
return TokenizerUtils::getAttrVal( $t, $this->startOffset() + strlen( $s ), $this->endOffset() - strlen( $q ) ); |
| 1877 |
} |
| 1878 |
/ s:$(space_or_newline* '"') t:attribute_preprocessor_text_double? q:$('"' / &('/'? '>')) { |
| 1879 |
return TokenizerUtils::getAttrVal( $t, $this->startOffset() + strlen( $s ), $this->endOffset() - strlen( $q ) ); |
| 1880 |
} |
| 1881 |
/ s:$space_or_newline* t:attribute_preprocessor_text &(space_or_newline / eof / '/'? '>') { |
| 1882 |
return TokenizerUtils::getAttrVal( $t, $this->startOffset() + strlen( $s ), $this->endOffset() ); |
| 1883 |
} |
| 1884 |
|
| 1885 |
// Attribute value, restricted to a single line. |
| 1886 |
// Missing end quote: accept |, !!, \r, and \n look-ahead as heuristic. |
| 1887 |
// These need to be kept in sync with the table_attribute_preprocessor_text_* |
| 1888 |
table_att_value = |
| 1889 |
s:$(space* "'") t:table_attribute_preprocessor_text_single? q:$("'" / &('!!' / [|\r\n])) { |
| 1890 |
return TokenizerUtils::getAttrVal( $t, $this->startOffset() + strlen( $s ), $this->endOffset() - strlen( $q ) ); |
| 1891 |
} |
| 1892 |
/ s:$(space* '"') t:table_attribute_preprocessor_text_double? q:$('"' / &('!!' / [|\r\n])) { |
| 1893 |
return TokenizerUtils::getAttrVal( $t, $this->startOffset() + strlen( $s ), $this->endOffset() - strlen( $q ) ); |
| 1894 |
} |
| 1895 |
/ s:$space* t:table_attribute_preprocessor_text &(space_or_newline/ eof / '!!' / '|') { |
| 1896 |
return TokenizerUtils::getAttrVal( $t, $this->startOffset() + strlen( $s ), $this->endOffset() ); |
| 1897 |
} |
| 1898 |
|
| 1899 |
/********************************************************* |
| 1900 |
* Lists |
| 1901 |
*********************************************************/ |
| 1902 |
list_item = dtdd / hacky_dl_uses / li |
| 1903 |
|
| 1904 |
li = |
| 1905 |
bullets:list_char+ |
| 1906 |
c:inlineline? |
| 1907 |
// The inline_break is to check if we've hit a template end delimiter. |
| 1908 |
&(eolf / inline_breaks) |
| 1909 |
{ |
| 1910 |
// Leave bullets as an array -- list handler expects this |
| 1911 |
$tsr = $this->tsrOffsets( 'start' ); |
| 1912 |
$tsr->end += count( $bullets ); |
| 1913 |
$dp = new DataParsoid; |
| 1914 |
$dp->tsr = $tsr; |
| 1915 |
$li = new TagTk( 'listItem', [ new KV( 'bullets', $bullets, $tsr->expandTsrV() ) ], $dp ); |
| 1916 |
return array_merge( [ $li ], $c ?: [] ); |
| 1917 |
} |
| 1918 |
|
| 1919 |
/* |
| 1920 |
* This rule is required to support wikitext of this form |
| 1921 |
* ::{|border="1"|foo|bar|baz|} |
| 1922 |
* where the leading colons are used to indent the entire table. |
| 1923 |
* This hack was added back in 2006 in commit |
| 1924 |
* a0746946312b0f1eda30a2c793f5f7052e8e5f3a based on a patch by Carl |
| 1925 |
* Fürstenberg. |
| 1926 |
*/ |
| 1927 |
hacky_dl_uses = |
| 1928 |
bullets:":"+ |
| 1929 |
tbl:(table_line (sol+ table_line)*) |
| 1930 |
line:inlineline? |
| 1931 |
&((space / comment)* eolf) |
| 1932 |
{ |
| 1933 |
// Leave bullets as an array -- list handler expects this |
| 1934 |
$tsr = $this->tsrOffsets( 'start' ); |
| 1935 |
$tsr->end += count( $bullets ); |
| 1936 |
$dp = new DataParsoid; |
| 1937 |
$dp->tsr = $tsr; |
| 1938 |
$li = new TagTk( 'listItem', [ new KV( 'bullets', $bullets, $tsr->expandTsrV() ) ], $dp ); |
| 1939 |
return TokenizerUtils::flattenIfArray( [ $li, $tbl, $line ?: [] ] ); |
| 1940 |
} |
| 1941 |
|
| 1942 |
dtdd_colon = c:inlineline_break_on_colon? cpos:(":" { return $this->endOffset(); }) |
| 1943 |
{ |
| 1944 |
return [ $c, $cpos ]; |
| 1945 |
} |
| 1946 |
|
| 1947 |
dtdd = |
| 1948 |
bullets:(!(";" !list_char) lc:list_char { return $lc; })* |
| 1949 |
";" |
| 1950 |
colons:dtdd_colon* |
| 1951 |
d:inlineline? |
| 1952 |
&eolf { |
| 1953 |
$bulletToks = []; |
| 1954 |
// Leave bullets as an array -- list handler expects this |
| 1955 |
// TSR: +1 for the leading ";" |
| 1956 |
$numBullets = count( $bullets ) + 1; |
| 1957 |
$tsr = $this->tsrOffsets( 'start' ); |
| 1958 |
$tsr->end += $numBullets; |
| 1959 |
$li1Bullets = $bullets; |
| 1960 |
$li1Bullets[] = ';'; |
| 1961 |
$dp = new DataParsoid; |
| 1962 |
$dp->tsr = $tsr; |
| 1963 |
$bulletToks[] = new TagTk( 'listItem', [ new KV( 'bullets', $li1Bullets, $tsr->expandTsrV() ) ], $dp ); |
| 1964 |
foreach ( $colons as $colon) { |
| 1965 |
if ( $colon[0] ) { // can be null because of "?" in dtdd_colon |
| 1966 |
$bulletToks[] = $colon[0]; |
| 1967 |
} |
| 1968 |
$cpos = $colon[1]; |
| 1969 |
// TSR: -1 for the intermediate ":" |
| 1970 |
$li2Bullets = $bullets; |
| 1971 |
$li2Bullets[] = ':'; |
| 1972 |
$tsr2 = new SourceRange( $cpos - 1, $cpos ); |
| 1973 |
$dp2 = new DataParsoid; |
| 1974 |
$dp2->tsr = $tsr2; |
| 1975 |
$dp2->stx = 'row'; |
| 1976 |
$bulletToks[] = new TagTk( 'listItem', [ new KV( 'bullets', $li2Bullets, $tsr2->expandTsrV() ) ], $dp2 ); |
| 1977 |
} |
| 1978 |
|
| 1979 |
if ( $d ) { |
| 1980 |
$bulletToks = array_merge( $bulletToks, $d ); |
| 1981 |
} |
| 1982 |
return $bulletToks; |
| 1983 |
} |
| 1984 |
|
| 1985 |
list_char = [*#:;] |
| 1986 |
|
| 1987 |
inlineline_break_on_colon = |
| 1988 |
inlineline<colon> |
| 1989 |
|
| 1990 |
/****************************************************************************** |
| 1991 |
* Tables |
| 1992 |
* ------ |
| 1993 |
* Table rules are geared to support independent parsing of fragments in |
| 1994 |
* templates (the common table start / row / table end use case). The tokens |
| 1995 |
* produced by these fragments then match up to a table while building the |
| 1996 |
* DOM tree. For similar reasons, table rows do not emit explicit end tag |
| 1997 |
* tokens. |
| 1998 |
* |
| 1999 |
* The separate table_line rule is faster than moving those rules |
| 2000 |
* directly to block_lines. |
| 2001 |
* |
| 2002 |
* Notes about the full_table_in_link_caption rule |
| 2003 |
* ----------------------------------------------------- |
| 2004 |
* However, for link-tables, we have introduced a stricter parse wherein |
| 2005 |
* we require table-start and table-end tags to not come from a template. |
| 2006 |
* In addition, this new rule doesn't accept fosterable-content in |
| 2007 |
* the table unlike the more lax (sol table_line)+ rule. |
| 2008 |
* |
| 2009 |
* This is the best we can do at this time since we cannot distinguish |
| 2010 |
* between table rows and image options entirely in the tokenizer. |
| 2011 |
* |
| 2012 |
* Consider the following examples: |
| 2013 |
* |
| 2014 |
* Example 1: |
| 2015 |
* |
| 2016 |
* [[Image:Foo.jpg|left|30px|Example 1 |
| 2017 |
* {{This-template-returns-a-table-start-tag}} |
| 2018 |
* |foo |
| 2019 |
* {{This-template-returns-a-table-end-tag}} |
| 2020 |
* ]] |
| 2021 |
* |
| 2022 |
* Example 2: |
| 2023 |
* |
| 2024 |
* [[Image:Foo.jpg|left|30px|Example 1 |
| 2025 |
* {{1x|a}} |
| 2026 |
* |foo |
| 2027 |
* {{1x|b}} |
| 2028 |
* ]] |
| 2029 |
* |
| 2030 |
* So, we cannot know a priori (without preprocessing or fully expanding |
| 2031 |
* all templates) if "|foo" in the two examples is a table cell or an image |
| 2032 |
* option. This is a limitation of our tokenizer-based approach compared to |
| 2033 |
* the preprocessing-based approach of the old parser. |
| 2034 |
* |
| 2035 |
* Given this limitation, we are okay forcing a full-table context in |
| 2036 |
* link captions (if necessary, we can relax the fosterable-content requirement |
| 2037 |
* but that is broken wikitext anyway, so we can force that edge-case wikitext |
| 2038 |
* to get fixed by rejecting it). |
| 2039 |
******************************************************************************/ |
| 2040 |
|
| 2041 |
full_table_in_link_caption = |
| 2042 |
!inline_breaks |
| 2043 |
// Note that "linkdesc" is suppressed here to provide a nested parsing |
| 2044 |
// context in which to parse the table. Otherwise, we may break on |
| 2045 |
// on pipes in the `table_start_tag` and `table_row_tag` attributes. |
| 2046 |
// However, as a result, this can be more permissive than the old |
| 2047 |
// implementation (legacy parser?), but likelier to match the users intent. |
| 2048 |
// Suppress the recursion protection from tableDataBlock since we're trying |
| 2049 |
// to parse a full table and if the link is itself nested in a table this |
| 2050 |
// will always stop. Hopefully, this won't result in any overflows. |
| 2051 |
r: full_table_in_link_caption_parameterized<linkdesc=false, table, tableDataBlock=false> { |
| 2052 |
return $r; |
| 2053 |
} |
| 2054 |
|
| 2055 |
full_table_in_link_caption_parameterized = |
| 2056 |
table_start_tag |
| 2057 |
// Accept multiple end tags since a nested table may have been |
| 2058 |
// opened in the table content line. |
| 2059 |
( |
| 2060 |
(sol+ (table_content_line / tplarg_or_template))* |
| 2061 |
sol+ table_end_tag |
| 2062 |
)+ |
| 2063 |
|
| 2064 |
// This rule assumes start-of-line position! |
| 2065 |
table_line = |
| 2066 |
(! inline_breaks / & '{{!}}' ) |
| 2067 |
tl:( |
| 2068 |
table_start_tag |
| 2069 |
/ table_content_line<table> |
| 2070 |
/ table_end_tag |
| 2071 |
) { |
| 2072 |
return $tl; |
| 2073 |
} |
| 2074 |
|
| 2075 |
table_content_line = |
| 2076 |
(space / comment)* ( |
| 2077 |
table_heading_tags |
| 2078 |
/ table_row_tag |
| 2079 |
/ table_data_tags |
| 2080 |
/ table_caption_tag |
| 2081 |
) |
| 2082 |
|
| 2083 |
table_start_tag "table_start_tag" = |
| 2084 |
sc:(space / comment)* |
| 2085 |
startPos:("" { return $this->endOffset(); }) |
| 2086 |
b:"{" p:pipe |
| 2087 |
// ok to normalize away stray |} on rt (see T59360) |
| 2088 |
ta:(table_attributes<table=false> / &{ $this->unreachable(); }) |
| 2089 |
tsEndPos:("" { return $this->endOffset(); }) |
| 2090 |
s2:space* |
| 2091 |
{ |
| 2092 |
$coms = TokenizerUtils::popComments( $ta ); |
| 2093 |
if ( $coms ) { |
| 2094 |
$tsEndPos = $coms['commentStartPos']; |
| 2095 |
} |
| 2096 |
|
| 2097 |
$dp = new DataParsoid; |
| 2098 |
$dp->tsr = new SourceRange( $startPos, $tsEndPos ); |
| 2099 |
if ( $p !== '|' ) { |
| 2100 |
// Variation from default |
| 2101 |
$dp->startTagSrc = $b . $p; |
| 2102 |
} |
| 2103 |
|
| 2104 |
return array_merge( $sc, |
| 2105 |
[ new TagTk( 'table', $ta, $dp ) ], |
| 2106 |
$coms ? $coms['buf'] : [], |
| 2107 |
$s2 ); |
| 2108 |
} |
| 2109 |
|
| 2110 |
// FIXME: Not sure if we want to support it, but this should allow columns. |
| 2111 |
table_caption_tag = |
| 2112 |
// avoid recursion via nested_block_in_table |
| 2113 |
! <tableDataBlock> |
| 2114 |
p:pipe "+" |
| 2115 |
args:row_syntax_table_args? |
| 2116 |
tagEndPos:("" { return $this->endOffset(); }) |
| 2117 |
c:nested_block_in_table* { |
| 2118 |
$tsr = new SourceRange( $this->startOffset(), $tagEndPos ); |
| 2119 |
return TokenizerUtils::buildTableTokens( |
| 2120 |
'caption', '|+', $args, $tsr, $this->endOffset(), $c, true ); |
| 2121 |
} |
| 2122 |
|
| 2123 |
table_row_tag = |
| 2124 |
// avoid recursion via nested_block_in_table |
| 2125 |
! <tableDataBlock> |
| 2126 |
p:pipe dashes:$"-"+ |
| 2127 |
a:(table_attributes<table=false> / &{ $this->unreachable(); }) |
| 2128 |
tagEndPos:("" { return $this->endOffset(); }) |
| 2129 |
s2:space* |
| 2130 |
{ |
| 2131 |
$coms = TokenizerUtils::popComments( $a ); |
| 2132 |
if ( $coms ) { |
| 2133 |
$tagEndPos = $coms['commentStartPos']; |
| 2134 |
} |
| 2135 |
|
| 2136 |
$da = new DataParsoid; |
| 2137 |
$da->tsr = new SourceRange( $this->startOffset(), $tagEndPos ); |
| 2138 |
$da->startTagSrc = $p . $dashes; |
| 2139 |
|
| 2140 |
// We rely on our tree builder to close the row as needed. This is |
| 2141 |
// needed to support building tables from fragment templates with |
| 2142 |
// individual cells or rows. |
| 2143 |
$trToken = new TagTk( 'tr', $a, $da ); |
| 2144 |
|
| 2145 |
return array_merge( [ $trToken ], $coms ? $coms['buf'] : [], $s2 ); |
| 2146 |
} |
| 2147 |
|
| 2148 |
table_heading_tags = table_heading_tags_parameterized<&th> |
| 2149 |
|
| 2150 |
table_heading_tags_parameterized = |
| 2151 |
"!" |
| 2152 |
thTag:table_heading_tag |
| 2153 |
thTags:ths { |
| 2154 |
// Avoid modifying a cached result |
| 2155 |
$thTag[0] = clone $thTag[0]; |
| 2156 |
$da = $thTag[0]->dataParsoid = clone $thTag[0]->dataParsoid; |
| 2157 |
$da->tsr = clone $da->tsr; |
| 2158 |
$da->tsr->start--; // include "!" |
| 2159 |
array_unshift( $thTags, $thTag ); |
| 2160 |
return $thTags; |
| 2161 |
} |
| 2162 |
|
| 2163 |
table_heading_tag = |
| 2164 |
arg:row_syntax_table_args? |
| 2165 |
tagEndPos:("" { return $this->endOffset(); }) |
| 2166 |
c:( |
| 2167 |
th:<&th> |
| 2168 |
d:nested_block_in_table { |
| 2169 |
// Ignore newlines found in transclusions! |
| 2170 |
// This is not perfect (since {{..}} may not always tokenize to transclusions). |
| 2171 |
if ( $th !== false && strpos( preg_replace( "/{{[\s\S]+?}}/", "", $this->text() ), "\n" ) !== false ) { |
| 2172 |
// There's been a newline. Remove the break and continue |
| 2173 |
// tokenizing nested_block_in_tables. |
| 2174 |
$th = false; |
| 2175 |
} |
| 2176 |
return $d; |
| 2177 |
} |
| 2178 |
)* { |
| 2179 |
$tsr = new SourceRange( $this->startOffset(), $tagEndPos ); |
| 2180 |
return TokenizerUtils::buildTableTokens( 'th', '!', $arg, |
| 2181 |
$tsr, $this->endOffset(), $c ); |
| 2182 |
} |
| 2183 |
|
| 2184 |
ths = ( |
| 2185 |
pp:("!!" / pipe_pipe) |
| 2186 |
tht:table_heading_tag { |
| 2187 |
// Avoid modifying cached dataParsoid object |
| 2188 |
$tht[0] = clone $tht[0]; |
| 2189 |
$da = $tht[0]->dataParsoid = clone $tht[0]->dataParsoid; |
| 2190 |
$da->tsr = clone $da->tsr; |
| 2191 |
$da->stx = 'row'; |
| 2192 |
$da->setTempFlag( TempData::NON_MERGEABLE_TABLE_CELL ); |
| 2193 |
$da->tsr->start -= strlen( $pp ); // include "!!" or "||" |
| 2194 |
if ( $pp !== '!!' || ( isset( $da->startTagSrc ) && $da->startTagSrc !== $pp ) ) { |
| 2195 |
// Variation from default |
| 2196 |
$da->startTagSrc = $pp . ( isset( $da->startTagSrc ) ? substr( $da->startTagSrc, 1 ) : '' ); |
| 2197 |
} |
| 2198 |
return $tht; |
| 2199 |
} |
| 2200 |
)* |
| 2201 |
|
| 2202 |
table_data_tags = |
| 2203 |
// avoid recursion via nested_block_in_table |
| 2204 |
! <tableDataBlock> |
| 2205 |
p:pipe |
| 2206 |
![+-] td:table_data_tag |
| 2207 |
tds:tds { |
| 2208 |
// Avoid modifying a cached result |
| 2209 |
$td[0] = clone $td[0]; |
| 2210 |
$da = $td[0]->dataParsoid = clone $td[0]->dataParsoid; |
| 2211 |
$da->tsr = clone $da->tsr; |
| 2212 |
$da->tsr->start -= strlen( $p ); // include "|" |
| 2213 |
if ( $p !== '|' ) { |
| 2214 |
// Variation from default |
| 2215 |
$da->startTagSrc = $p; |
| 2216 |
} |
| 2217 |
return array_merge( $td, $tds ); |
| 2218 |
} |
| 2219 |
|
| 2220 |
table_data_tag = |
| 2221 |
! "}" |
| 2222 |
arg:row_syntax_table_args? |
| 2223 |
// use inline_breaks to break on tr etc |
| 2224 |
tagEndPos:("" { return $this->endOffset(); }) |
| 2225 |
td:nested_block_in_table* |
| 2226 |
{ |
| 2227 |
$tsr = new SourceRange( $this->startOffset(), $tagEndPos ); |
| 2228 |
return TokenizerUtils::buildTableTokens( 'td', '|', $arg, |
| 2229 |
$tsr, $this->endOffset(), $td ); |
| 2230 |
} |
| 2231 |
|
| 2232 |
tds = ( |
| 2233 |
pp:pipe_pipe |
| 2234 |
tdt:table_data_tag { |
| 2235 |
// Avoid modifying cached dataParsoid object |
| 2236 |
$tdt[0] = clone $tdt[0]; |
| 2237 |
$da = $tdt[0]->dataParsoid = clone $tdt[0]->dataParsoid; |
| 2238 |
$da->tsr = clone $da->tsr; |
| 2239 |
$da->stx = 'row'; |
| 2240 |
$da->setTempFlag( TempData::NON_MERGEABLE_TABLE_CELL ); |
| 2241 |
$da->tsr->start -= strlen( $pp ); // include "||" |
| 2242 |
if ( $pp !== '||' || ( isset( $da->startTagSrc ) && $da->startTagSrc !== $pp ) ) { |
| 2243 |
// Variation from default |
| 2244 |
$da->startTagSrc = $pp . ( isset( $da->startTagSrc ) ? substr( $da->startTagSrc, 1 ) : '' ); |
| 2245 |
} |
| 2246 |
return $tdt; |
| 2247 |
} |
| 2248 |
)* |
| 2249 |
|
| 2250 |
table_end_tag = |
| 2251 |
sc:(space / comment)* |
| 2252 |
startPos:("" { return $this->endOffset(); }) |
| 2253 |
p:pipe |
| 2254 |
b:"}" |
| 2255 |
{ |
| 2256 |
$dp = new DataParsoid; |
| 2257 |
$dp->tsr = new SourceRange( $startPos, $this->endOffset() ); |
| 2258 |
$tblEnd = new EndTagTk( 'table', [], $dp ); |
| 2259 |
if ( $p !== '|' ) { |
| 2260 |
// p+"<brace-char>" is triggering some bug in pegJS |
| 2261 |
// I cannot even use that expression in the comment! |
| 2262 |
$tblEnd->dataParsoid->endTagSrc = $p . $b; |
| 2263 |
} |
| 2264 |
array_push( $sc, $tblEnd ); |
| 2265 |
return $sc; |
| 2266 |
} |
| 2267 |
|
| 2268 |
/** |
| 2269 |
* Table parameters separated from the content by a single pipe. Does *not* |
| 2270 |
* match if followed by double pipe (row-based syntax). |
| 2271 |
*/ |
| 2272 |
row_syntax_table_args = |
| 2273 |
as:table_attributes<tableCellArg> s:optional_spaces p:pipe !pipe { |
| 2274 |
return [ $as, $s, $p ]; |
| 2275 |
} |
| 2276 |
|
| 2277 |
|
| 2278 |
/******************************************************************* |
| 2279 |
* Text variants and other general rules |
| 2280 |
*******************************************************************/ |
| 2281 |
|
| 2282 |
/* All chars that cannot start syntactic structures in the middle of a line |
| 2283 |
* XXX: ] and other end delimiters should probably only be activated inside |
| 2284 |
* structures to avoid unnecessarily leaving the text rule on plain |
| 2285 |
* content. |
| 2286 |
* |
| 2287 |
* TODO: Much of this is should really be context-dependent (syntactic |
| 2288 |
* flags). The wikilink_preprocessor_text rule is an example where |
| 2289 |
* text_char is not quite right and had to be augmented. Try to minimize / |
| 2290 |
* clarify this carefully! |
| 2291 |
* |
| 2292 |
* This character class is inlined into $this->reUrltextLookahead. Changes |
| 2293 |
* here may also need to be reflected there. |
| 2294 |
*/ |
| 2295 |
|
| 2296 |
text_char = [^-'<[{\n\r:;\]}|!=] |
| 2297 |
|
| 2298 |
/* Legend |
| 2299 |
* ' quotes (italic/bold) |
| 2300 |
* < start of xmlish_tag |
| 2301 |
* [ start of links |
| 2302 |
* { start of parser functions, transclusion and template args |
| 2303 |
* \n all sort of block-level markup at start of line |
| 2304 |
* \r ditto |
| 2305 |
* A-Za-z autolinks (http(s), nttp(s), mailto, ISBN, PMID, RFC) |
| 2306 |
* |
| 2307 |
* _ behavior switches (e.g., '__NOTOC__') (XXX: not URL related) |
| 2308 |
* ! and | table cell delimiters, might be better to specialize those |
| 2309 |
* = headings - also specialize those! |
| 2310 |
* |
| 2311 |
* The following chars are also included for now, but only apply in some |
| 2312 |
* contexts and should probably be enabled only in those: |
| 2313 |
* : separate definition in ; term : definition |
| 2314 |
* ] end of link |
| 2315 |
* } end of parser func/transclusion/template arg |
| 2316 |
* - start of lang_variant -{ ... }- |
| 2317 |
* ; separator in lang_variant |
| 2318 |
*/ |
| 2319 |
|
| 2320 |
urltext = |
| 2321 |
( |
| 2322 |
/* Very special performance hack: |
| 2323 |
* Look for a plain text sequence, and if found, pretend to match the |
| 2324 |
* empty string, but then advance currPos in the action and return the |
| 2325 |
* whole plain text segment as a single result. |
| 2326 |
*/ |
| 2327 |
& { |
| 2328 |
if ( preg_match( $this->reUrltextLookahead, $this->input, $m, 0, $this->currPos ) ) { |
| 2329 |
$plain = $m[1]; |
| 2330 |
$this->urltextPlainSegment = $plain; |
| 2331 |
$this->urltextFoundAutolink = ( $m[2] ?? '' ) !== ''; |
| 2332 |
return (bool)strlen( $plain ); |
| 2333 |
} else { |
| 2334 |
$this->urltextFoundAutolink = false; |
| 2335 |
return false; |
| 2336 |
} |
| 2337 |
} |
| 2338 |
'' { |
| 2339 |
$this->currPos += strlen( $this->urltextPlainSegment ); |
| 2340 |
return $this->urltextPlainSegment; |
| 2341 |
} |
| 2342 |
/ & { return $this->urltextFoundAutolink; } al:autolink { return $al; } |
| 2343 |
/ & "&" he:htmlentity { return $he; } |
| 2344 |
/ & ('__') bs:behavior_switch { return $bs; } |
| 2345 |
/ text_char |
| 2346 |
)+ |
| 2347 |
|
| 2348 |
raw_htmlentity = |
| 2349 |
m:$("&" [#0-9a-zA-Zרלמرلم]+ ";") { |
| 2350 |
return Utils::decodeWtEntities( $m ); |
| 2351 |
} |
| 2352 |
|
| 2353 |
htmlentity = |
| 2354 |
cc:raw_htmlentity { |
| 2355 |
// if this is an invalid entity, don't tag it with 'mw:Entity' |
| 2356 |
// note that some entities (like ∾̳) decode to 2 codepoints! |
| 2357 |
if ( mb_strlen( $cc ) > 2 /* decoded entity would be 1-2 codepoints */ ) { |
| 2358 |
return $cc; |
| 2359 |
} |
| 2360 |
$dpStart = new DataParsoid; |
| 2361 |
$dpStart->src = $this->text(); |
| 2362 |
$dpStart->srcContent = $cc; |
| 2363 |
$dpStart->tsr = $this->tsrOffsets( 'start' ); |
| 2364 |
$dpEnd = new DataParsoid; |
| 2365 |
$dpEnd->tsr = $this->tsrOffsets( 'end' ); |
| 2366 |
return [ |
| 2367 |
// If this changes, the nowiki extension's toDOM will need to follow suit |
| 2368 |
new TagTk( 'span', [ new KV( 'typeof', 'mw:Entity' ) ], $dpStart ), |
| 2369 |
$cc, |
| 2370 |
new EndTagTk( 'span', [], $dpEnd ) |
| 2371 |
]; |
| 2372 |
} |
| 2373 |
|
| 2374 |
/** |
| 2375 |
* noinclude / includeonly / onlyinclude rules. These are normally |
| 2376 |
* handled by the xmlish_tag rule, except where generic tags are not |
| 2377 |
* allowed- for example in directives, which are allowed in various attribute |
| 2378 |
* names and -values. |
| 2379 |
* |
| 2380 |
* Example test case: |
| 2381 |
* {| |
| 2382 |
* |-<includeonly> |
| 2383 |
* foo |
| 2384 |
* </includeonly> |
| 2385 |
* |Hello |
| 2386 |
* |} |
| 2387 |
*/ |
| 2388 |
|
| 2389 |
include_limits = |
| 2390 |
& ("<" "/"? ( "includeonly"i / "noinclude"i / "onlyinclude"i ) ) |
| 2391 |
"<" il:xmlish_tag<annOrExtTag> |
| 2392 |
sol_il: <sol_il> |
| 2393 |
& { |
| 2394 |
$il = $il[0]; |
| 2395 |
$lname = mb_strtolower( $il->getName() ); |
| 2396 |
if ( !WTUtils::isIncludeTag( $lname ) ) { return false; } |
| 2397 |
// Preserve SOL where necessary (for onlyinclude and noinclude) |
| 2398 |
// Note that this only works because we encounter <*include*> tags in |
| 2399 |
// the toplevel content and we rely on the php preprocessor to expand |
| 2400 |
// templates, so we shouldn't ever be tokenizing inInclude. |
| 2401 |
// Last line should be empty (except for comments) |
| 2402 |
if ( $lname !== 'includeonly' && $sol_il && $il instanceof TagTk ) { |
| 2403 |
$dp = $il->dataParsoid; |
| 2404 |
$inclContent = $dp->extTagOffsets->stripTags( $dp->src ); |
| 2405 |
$nlpos = strrpos( $inclContent, "\n" ); |
| 2406 |
$last = $nlpos === false ? $inclContent : substr( $inclContent, $nlpos + 1 ); |
| 2407 |
if ( !preg_match( '/^(<!--([^-]|-(?!->))*-->)*$/D', $last ) ) { |
| 2408 |
return false; |
| 2409 |
} |
| 2410 |
} |
| 2411 |
return true; |
| 2412 |
} |
| 2413 |
{ |
| 2414 |
return $il; |
| 2415 |
} |
| 2416 |
|
| 2417 |
// 'Preprocessor' directive- higher-level things that can occur in otherwise |
| 2418 |
// plain-text content. |
| 2419 |
directive = |
| 2420 |
comment |
| 2421 |
/ extension_annotation_tag |
| 2422 |
/ tplarg_or_template |
| 2423 |
/ & "-{" v:lang_variant_or_tpl { return $v; } |
| 2424 |
/ & "&" e:htmlentity { return $e; } |
| 2425 |
/ include_limits |
| 2426 |
|
| 2427 |
wikilink_preprocessor_text = |
| 2428 |
r:( |
| 2429 |
t:$[^<[{\n\r\t|!\]}{ &\-]+ |
| 2430 |
// XXX gwicke: any more chars we need to allow here? |
| 2431 |
/ !inline_breaks wr:( directive / $( !"]]" ( text_char / [!<\-\}\]\n\r] ) ) ) |
| 2432 |
{ return $wr; } |
| 2433 |
)+ { |
| 2434 |
return TokenizerUtils::flattenStringlist( $r ); |
| 2435 |
} |
| 2436 |
|
| 2437 |
// added special separator character class inline: separates url from |
| 2438 |
// description / text |
| 2439 |
extlink_nonipv6url = |
| 2440 |
// Prevent breaking on pipes when we're in a link description. |
| 2441 |
// See the test, 'Images with the "|" character in the comment'. |
| 2442 |
extlink_nonipv6url_parameterized<linkdesc=false> |
| 2443 |
|
| 2444 |
extlink_nonipv6url_parameterized = |
| 2445 |
r:( |
| 2446 |
$[^<[{\n\r|!\]}\-\t&="' \u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]+ |
| 2447 |
/ !inline_breaks s:( directive / [&|{\-!}=] ) { return $s; } |
| 2448 |
/ $(['] ![']) // single quotes are ok, double quotes are bad |
| 2449 |
)+ { |
| 2450 |
return TokenizerUtils::flattenString( $r ); |
| 2451 |
} |
| 2452 |
|
| 2453 |
// Attribute values with preprocessor support |
| 2454 |
|
| 2455 |
// n.b. / is a permissible char in the three rules below. |
| 2456 |
// We only break on />, enforced by the negated expression. |
| 2457 |
// Hence, it isn't included in the stop set. |
| 2458 |
|
| 2459 |
// The stop set is space_or_newline and > which matches generic_att_value. |
| 2460 |
attribute_preprocessor_text = |
| 2461 |
r:( |
| 2462 |
$[^{}&<\-|/ \t\n\r\x0c>]+ |
| 2463 |
/ !inline_breaks |
| 2464 |
!'/>' |
| 2465 |
s:( directive / less_than / [{}&\-|/] ) { return $s; } |
| 2466 |
)+ { |
| 2467 |
return TokenizerUtils::flattenString( $r ); |
| 2468 |
} |
| 2469 |
|
| 2470 |
// The stop set is '> which matches generic_att_value. |
| 2471 |
attribute_preprocessor_text_single = |
| 2472 |
r:( |
| 2473 |
$[^{}&<\-|/'>]+ |
| 2474 |
/ !inline_breaks |
| 2475 |
!'/>' |
| 2476 |
s:( directive / less_than / [{}&\-|/] ) { return $s; } |
| 2477 |
)* { |
| 2478 |
return TokenizerUtils::flattenString( $r ); |
| 2479 |
} |
| 2480 |
|
| 2481 |
// The stop set is "> which matches generic_att_value. |
| 2482 |
attribute_preprocessor_text_double = |
| 2483 |
r:( |
| 2484 |
$[^{}&<\-|/">]+ |
| 2485 |
/ !inline_breaks |
| 2486 |
!'/>' |
| 2487 |
s:( directive / less_than / [{}&\-|/] ) { return $s; } |
| 2488 |
)* { |
| 2489 |
return TokenizerUtils::flattenString( $r ); |
| 2490 |
} |
| 2491 |
|
| 2492 |
// Variants with the entire attribute on a single line |
| 2493 |
|
| 2494 |
// n.b. ! is a permissible char in the three rules below. |
| 2495 |
// We only break on !! in th, enforced by the inline break. |
| 2496 |
// Hence, it isn't included in the stop set. |
| 2497 |
// [ is also permissible but we give a chance to break |
| 2498 |
// for the [[ special case in the old parser's doTableStuff (See T2553). |
| 2499 |
|
| 2500 |
// The stop set is space_or_newline and | which matches table_att_value. |
| 2501 |
table_attribute_preprocessor_text = |
| 2502 |
r:( |
| 2503 |
$[^{}&<\-!\[ \t\n\r\x0c|]+ |
| 2504 |
/ !inline_breaks s:( directive / [{}&<\-!\[] ) { return $s; } |
| 2505 |
)+ { |
| 2506 |
return TokenizerUtils::flattenString( $r ); |
| 2507 |
} |
| 2508 |
|
| 2509 |
// The stop set is '\r\n| which matches table_att_value. |
| 2510 |
table_attribute_preprocessor_text_single = |
| 2511 |
r:( |
| 2512 |
$[^{}&<\-!\['\r\n|]+ |
| 2513 |
/ !inline_breaks s:( directive / [{}&<\-!\[] ) { return $s; } |
| 2514 |
)* { |
| 2515 |
return TokenizerUtils::flattenString( $r ); |
| 2516 |
} |
| 2517 |
|
| 2518 |
// The stop set is "\r\n| which matches table_att_value. |
| 2519 |
table_attribute_preprocessor_text_double = |
| 2520 |
r:( |
| 2521 |
$[^{}&<\-!\["\r\n|]+ |
| 2522 |
/ !inline_breaks s:( directive / [{}&<\-!\[] ) { return $s; } |
| 2523 |
)* { |
| 2524 |
return TokenizerUtils::flattenString( $r ); |
| 2525 |
} |
| 2526 |
|
| 2527 |
// Special-case support for those pipe templates |
| 2528 |
pipe = "|" / "{{!}}" |
| 2529 |
|
| 2530 |
// SSS FIXME: what about |{{!}} and {{!}}| |
| 2531 |
pipe_pipe = "||" / "{{!}}{{!}}" |
| 2532 |
|
| 2533 |
space = [ \t] |
| 2534 |
|
| 2535 |
optional_spaces = $[ \t]* |
| 2536 |
|
| 2537 |
// Start of file |
| 2538 |
sof = & { return $this->endOffset() === 0 && !$this->pipelineOffset; } |
| 2539 |
|
| 2540 |
// End of file |
| 2541 |
eof = & { return $this->endOffset() === $this->inputLength; } |
| 2542 |
|
| 2543 |
newline = '\n' / '\r\n' |
| 2544 |
|
| 2545 |
newlineToken = newline { return [ new NlTk( $this->tsrOffsets() ) ]; } |
| 2546 |
|
| 2547 |
eolf = newline / eof |
| 2548 |
|
| 2549 |
// The old parser does a straight str.replace(/<!--((?!-->).)*-->/g, "") |
| 2550 |
// but, as always, things around here are a little more complicated. |
| 2551 |
// |
| 2552 |
// We accept the same comments, but because we emit them as HTML comments |
| 2553 |
// instead of deleting them, we have to encode the data to ensure that |
| 2554 |
// we always emit a valid HTML5 comment. See the encodeComment helper |
| 2555 |
// for further details. |
| 2556 |
comment = |
| 2557 |
'<!--' c:$(!"-->" .)* cEnd:$('-->' / eof) { |
| 2558 |
$data = WTUtils::encodeComment( $c ); |
| 2559 |
$dp = new DataParsoid; |
| 2560 |
$dp->tsr = $this->tsrOffsets(); |
| 2561 |
if ( $cEnd !== '-->' ) { |
| 2562 |
$dp->unclosedComment = true; |
| 2563 |
} |
| 2564 |
return [ new CommentTk( $data, $dp ) ]; |
| 2565 |
} |
| 2566 |
|
| 2567 |
nl_comment_space = newlineToken / space / comment |
| 2568 |
|
| 2569 |
optionalSpaceToken = |
| 2570 |
s:optional_spaces { |
| 2571 |
if ( $s !== '' ) { |
| 2572 |
return [ $s ]; |
| 2573 |
} else { |
| 2574 |
return []; |
| 2575 |
} |
| 2576 |
} |
| 2577 |
|
| 2578 |
/* This rule corresponds to \s in the PHP preg_* functions, |
| 2579 |
* which is used frequently in the old parser. The inclusion of |
| 2580 |
* form feed (but not other whitespace, like vertical tab) is a quirk |
| 2581 |
* of Perl, which PHP inherited via the PCRE (Perl-Compatible Regular |
| 2582 |
* Expressions) library. |
| 2583 |
*/ |
| 2584 |
space_or_newline = [ \t\n\r\x0c] |
| 2585 |
|
| 2586 |
/* This rule corresponds to \b in the PHP preg_* functions, |
| 2587 |
* after a word character. That is, it's a zero-width lookahead that |
| 2588 |
* the next character is not a word character. |
| 2589 |
*/ |
| 2590 |
end_of_word = eof / ![A-Za-z0-9_] |
| 2591 |
|
| 2592 |
// Unicode "separator, space" category. It covers the \u0020 space as well |
| 2593 |
// as \u3000 IDEOGRAPHIC SPACE (see bug 19052). In PHP this is \p{Zs}. |
| 2594 |
// Keep this up-to-date with the characters tagged ;Zs; in |
| 2595 |
// http://www.unicode.org/Public/UNIDATA/UnicodeData.txt |
| 2596 |
unispace = [ \u00A0\u1680\u2000-\u200A\u202F\u205F\u3000] |
| 2597 |
|
| 2598 |
// Non-newline whitespace, including non-breaking spaces. Used for magic links. |
| 2599 |
space_or_nbsp = |
| 2600 |
space // includes \t |
| 2601 |
/ unispace |
| 2602 |
/ & "&" he:htmlentity &{ return is_array( $he ) && $he[ 1 ] === "\u{A0}"; } |
| 2603 |
{ return $he; } |
| 2604 |
|
| 2605 |
// Used within ISBN magic links |
| 2606 |
space_or_nbsp_or_dash = |
| 2607 |
space_or_nbsp / "-" |
| 2608 |
|
| 2609 |
// Elements that do not break beginning or end of line for blocks (headers for instance) |
| 2610 |
sol_transparent = comment / include_limits<sol_il> / annotation_tag / behavior_switch |
| 2611 |
|
| 2612 |
sol = (empty_line_with_comments / sol_prefix) sol_transparent* |
| 2613 |
|
| 2614 |
sol_prefix = |
| 2615 |
newlineToken |
| 2616 |
/ & { |
| 2617 |
// Use the sol flag only at the start of the input |
| 2618 |
// Flag should always be an actual boolean (not falsy or undefined) |
| 2619 |
$this->assert( is_bool( $this->options['sol'] ), 'sol should be boolean' ); |
| 2620 |
return $this->endOffset() === 0 && $this->options['sol']; |
| 2621 |
} { |
| 2622 |
return []; |
| 2623 |
} |
| 2624 |
|
| 2625 |
// This rule requires at least one comment to be matched |
| 2626 |
empty_line_with_comments = |
| 2627 |
sp:sol_prefix p:("" { return $this->endOffset(); }) c:(space* comment (space / comment)* newline)+ { |
| 2628 |
$dp = new DataParsoid; |
| 2629 |
$dp->tsr = new SourceRange( $p, $this->endOffset() ); |
| 2630 |
$dp->tokens = TokenizerUtils::flattenIfArray( $c ); |
| 2631 |
return [ |
| 2632 |
$sp, |
| 2633 |
new SelfclosingTagTk( 'meta', [ new KV( 'typeof', 'mw:EmptyLine' ) ], $dp ) |
| 2634 |
]; |
| 2635 |
} |