Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 305 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
| TokenStreamPatcher | |
0.00% |
0 / 305 |
|
0.00% |
0 / 14 |
13340 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| resetState | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| reset | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getResultTokens | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| onNewline | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| onEnd | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| clearSOL | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| reprocessTokens | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
20 | |||
| reprocessTrReparseBufViaOnAny | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| processTrReparseBuf | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
30 | |||
| convertNonHTMLTokenToString | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
552 | |||
| onAny | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| onAnyInternal | |
0.00% |
0 / 129 |
|
0.00% |
0 / 1 |
3782 | |||
| resetSrcOffset | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Parsoid\Wt2Html\TT; |
| 5 | |
| 6 | use Wikimedia\Parsoid\Core\SourceRange; |
| 7 | use Wikimedia\Parsoid\Core\SourceString; |
| 8 | use Wikimedia\Parsoid\Tokens\CommentTk; |
| 9 | use Wikimedia\Parsoid\Tokens\EmptyLineTk; |
| 10 | use Wikimedia\Parsoid\Tokens\EndTagTk; |
| 11 | use Wikimedia\Parsoid\Tokens\EOFTk; |
| 12 | use Wikimedia\Parsoid\Tokens\NlTk; |
| 13 | use Wikimedia\Parsoid\Tokens\SelfclosingTagTk; |
| 14 | use Wikimedia\Parsoid\Tokens\TagTk; |
| 15 | use Wikimedia\Parsoid\Tokens\Token; |
| 16 | use Wikimedia\Parsoid\Tokens\XMLTagTk; |
| 17 | use Wikimedia\Parsoid\Utils\PHPUtils; |
| 18 | use Wikimedia\Parsoid\Utils\PipelineUtils; |
| 19 | use Wikimedia\Parsoid\Utils\TokenUtils; |
| 20 | use Wikimedia\Parsoid\Wt2Html\TokenHandlerPipeline; |
| 21 | use Wikimedia\WikiPEG\SyntaxError; |
| 22 | |
| 23 | /** |
| 24 | * This class is an attempt to fixup the token stream to reparse strings |
| 25 | * as tokens that failed to parse in the tokenizer because of SOL or |
| 26 | * other constraints OR because tags were being constructed in pieces |
| 27 | * or whatever. |
| 28 | * |
| 29 | * This is a pure hack to improve compatibility with the core parser |
| 30 | * given that we dont have a preprocessor. This will be a grab-bag of |
| 31 | * heuristics and tricks to handle different scenarios. |
| 32 | */ |
| 33 | class TokenStreamPatcher extends LineBasedHandler { |
| 34 | private ?SourceRange $srcOffset; |
| 35 | private bool $sol; |
| 36 | /** |
| 37 | * This buffers whitespace, nls, and rendering-transparent tokens |
| 38 | * like category link tags. |
| 39 | */ |
| 40 | private array $nlWsMetaTokenBuf; |
| 41 | private int $wikiTableNesting; |
| 42 | /** True only for top-level & attribute value pipelines */ |
| 43 | private bool $inIndependentParse; |
| 44 | private ?array $tplInfo; |
| 45 | private ?array $trReparseBuf; |
| 46 | |
| 47 | public function __construct( TokenHandlerPipeline $manager, array $options ) { |
| 48 | $newOptions = [ 'tsp' => true ] + $options; |
| 49 | parent::__construct( $manager, $newOptions ); |
| 50 | $this->reset(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Resets any internal state for this token handler. |
| 55 | * |
| 56 | * @param array $options |
| 57 | */ |
| 58 | public function resetState( array $options ): void { |
| 59 | parent::resetState( $options ); |
| 60 | $this->inIndependentParse = $this->atTopLevel |
| 61 | // Attribute expansion is effectively its own document context |
| 62 | || isset( $this->options['attrExpansion'] ) |
| 63 | // Ext-tag processing is effectively its own document context |
| 64 | || isset( $this->options['extTag'] ); |
| 65 | } |
| 66 | |
| 67 | private function reset(): void { |
| 68 | // XXX: T405759 should initialize source better |
| 69 | $this->srcOffset = new SourceRange( 0, 0, $this->env->topFrame->getSource() ); |
| 70 | $this->sol = true; |
| 71 | $this->nlWsMetaTokenBuf = []; |
| 72 | $this->wikiTableNesting = 0; |
| 73 | $this->tplInfo = null; |
| 74 | $this->trReparseBuf = null; |
| 75 | } |
| 76 | |
| 77 | private function getResultTokens( array $ret ): array { |
| 78 | // Emit buffered newlines (and a transclusion meta-token, if any) |
| 79 | if ( count( $this->nlWsMetaTokenBuf ) > 0 ) { |
| 80 | $ret = array_merge( $this->nlWsMetaTokenBuf, $ret ); |
| 81 | $this->nlWsMetaTokenBuf = []; |
| 82 | } |
| 83 | return $ret; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @inheritDoc |
| 88 | */ |
| 89 | public function onNewline( NlTk $token ): ?array { |
| 90 | $this->env->trace( 'tsp', $this->pipelineId, |
| 91 | function () use ( $token ) { |
| 92 | return "(indep=" . ( $this->inIndependentParse ? "yes" : "no " ) . |
| 93 | ";sol=" . ( $this->sol ? "yes" : "no " ) . ') ' . |
| 94 | PHPUtils::jsonEncode( $token ); |
| 95 | } |
| 96 | ); |
| 97 | if ( $this->trReparseBuf ) { |
| 98 | $ret = $this->processTrReparseBuf( $token ); |
| 99 | $this->trReparseBuf = null; |
| 100 | } else { |
| 101 | $ret = []; |
| 102 | } |
| 103 | // These need to be after reprocessing to ensure |
| 104 | // we always end up in the correct state after. |
| 105 | $this->resetSrcOffset( $token->dataParsoid->tsr ?? null ); |
| 106 | $this->sol = true; |
| 107 | $this->nlWsMetaTokenBuf[] = $token; |
| 108 | $this->env->trace( 'tsp', $this->pipelineId, " ---> ", $ret ); |
| 109 | return $ret; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @inheritDoc |
| 114 | */ |
| 115 | public function onEnd( EOFTk $token ): ?array { |
| 116 | if ( $this->trReparseBuf ) { |
| 117 | $ret = $this->processTrReparseBuf(); |
| 118 | $this->trReparseBuf = null; |
| 119 | } else { |
| 120 | $ret = []; |
| 121 | } |
| 122 | PHPUtils::pushArray( $ret, $this->onAny( $token ) ); |
| 123 | $this->reset(); |
| 124 | $this->env->trace( 'tsp', $this->pipelineId, " ---> ", $ret ); |
| 125 | return $ret; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Clear start of line info |
| 130 | */ |
| 131 | private function clearSOL(): void { |
| 132 | // clear tsr and sol flag |
| 133 | $this->srcOffset = null; |
| 134 | $this->sol = false; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Reprocess source to tokens as they would be entering the |
| 139 | * TokenStreamPatcher, ie. expanded to the end of stage 2 |
| 140 | */ |
| 141 | private function reprocessTokens( |
| 142 | ?SourceRange $srcOffsets, string $str, bool $sol, ?string $startRule = null |
| 143 | ): array { |
| 144 | $missingSrcOffsets = ( $srcOffsets === null ); |
| 145 | // Use 'start' and 'source' of $srcOffsets but recompute the 'end' |
| 146 | $srcOffsets = new SourceRange( |
| 147 | ( $srcOffsets->start ?? 0 ), |
| 148 | ( $srcOffsets->start ?? 0 ) + strlen( $str ), |
| 149 | $srcOffsets->source ?? new SourceString( $str ) |
| 150 | ); |
| 151 | // This string effectively came from a template, so tag it as 'inTemplate' |
| 152 | $inTemplate = ( $this->tplInfo !== null ); |
| 153 | $pipelineOpts = [ |
| 154 | 'inTemplate' => $inTemplate, |
| 155 | 'expandTemplates' => !$inTemplate, |
| 156 | ]; |
| 157 | $toks = (array)PipelineUtils::processContentInPipeline( |
| 158 | $this->env, |
| 159 | $this->manager->getFrame(), |
| 160 | $str, |
| 161 | [ |
| 162 | 'pipelineType' => 'wikitext-to-expanded-tokens', |
| 163 | 'pipelineOpts' => $pipelineOpts, |
| 164 | 'sol' => $sol, |
| 165 | // processTrReparseBuf gets us here from a top-level content pipeline, |
| 166 | // but the content itself came from a template. In that situation, the |
| 167 | // pipeline isn't processing top-level content, and isn't at 'toplevel'. |
| 168 | 'toplevel' => $this->atTopLevel && !$inTemplate, |
| 169 | 'srcOffsets' => $srcOffsets, |
| 170 | ] + ( $startRule !== null ? [ 'startRule' => $startRule ] : [] ) |
| 171 | ); |
| 172 | TokenUtils::stripEOFTkFromTokens( $toks ); |
| 173 | if ( $missingSrcOffsets ) { |
| 174 | // Clear the TSR information from these tokens |
| 175 | TokenUtils::shiftTokenTSR( $toks, null ); |
| 176 | } |
| 177 | return $toks; |
| 178 | } |
| 179 | |
| 180 | private function reprocessTrReparseBufViaOnAny(): array { |
| 181 | // Reset state for reprocessing |
| 182 | $this->tplInfo = $this->trReparseBuf['trTplInfo']; |
| 183 | $this->clearSOL(); |
| 184 | // Pop tr & reset trReparseBuf -- we don't want to repopulate trReparseBuf! |
| 185 | $tokens = $this->trReparseBuf['tokens']; |
| 186 | $ret = $this->nlWsMetaTokenBuf; |
| 187 | $ret[] = array_shift( $tokens ); |
| 188 | $this->nlWsMetaTokenBuf = []; |
| 189 | $this->trReparseBuf = null; |
| 190 | |
| 191 | // Reprocess the rest |
| 192 | $this->env->trace( 'tsp', $this->pipelineId, "*** START REPROCESSING BUFFER VIA ONANY ***" ); |
| 193 | // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach |
| 194 | foreach ( $tokens as $tok ) { |
| 195 | $res = $this->onAnyInternal( $tok ); |
| 196 | if ( $res ) { |
| 197 | PHPUtils::pushArray( $ret, $res ); |
| 198 | } |
| 199 | } |
| 200 | $this->env->trace( 'tsp', $this->pipelineId, "*** END REPROCESSING BUFFER ***" ); |
| 201 | return $ret; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * If $nlTk is null, this is EOF scenario |
| 206 | */ |
| 207 | private function processTrReparseBuf( ?NlTk $nlTk = null ): array { |
| 208 | if ( !isset( $this->trReparseBuf['endMeta'] ) ) { |
| 209 | return $this->reprocessTrReparseBufViaOnAny(); |
| 210 | } |
| 211 | |
| 212 | // We have to parse these buffered tokens as attribute of the <tr> |
| 213 | if ( $this->tplInfo !== null ) { |
| 214 | // FIXME: Cannot reliably support this when we need to |
| 215 | // glue part of this unclosed template's content into |
| 216 | // the <tr>'s attributes that came from a different template. |
| 217 | // |
| 218 | // So, bail and let things be as is. |
| 219 | return $this->reprocessTrReparseBufViaOnAny(); |
| 220 | } |
| 221 | |
| 222 | $frameSrc = $this->manager->getFrame()->getSource(); |
| 223 | $srcOffsets = $this->manager->getSrcOffsets(); |
| 224 | |
| 225 | // Both these TSR properties will exist because they come from |
| 226 | // the top-level content and the tokenizer sets TSR offsets. |
| 227 | $tr = $this->trReparseBuf['tr']; |
| 228 | $endMeta = $this->trReparseBuf['endMeta']; |
| 229 | $source = $endMeta->dataParsoid->tsr->source ?? $srcOffsets->source ?? $frameSrc; |
| 230 | $metaEndTSR = $endMeta->dataParsoid->tsr->end; |
| 231 | $lineEndTSR = $nlTk ? $nlTk->dataParsoid->tsr->start : $srcOffsets->end; |
| 232 | |
| 233 | // Stitch new wikitext to include content found after template-end-meta |
| 234 | $extraAttrSrc = PHPUtils::safeSubstr( |
| 235 | $source->getSrcText(), |
| 236 | $metaEndTSR, $lineEndTSR - $metaEndTSR |
| 237 | ); |
| 238 | if ( !$extraAttrSrc ) { |
| 239 | return $this->reprocessTrReparseBufViaOnAny(); |
| 240 | } |
| 241 | |
| 242 | $toks = $this->trReparseBuf['tokens']; |
| 243 | $this->env->trace( 'tsp', $this->pipelineId, |
| 244 | static function () use ( $toks ) { |
| 245 | return "*** REPROCESSING TR WITH TOKENS ***" . |
| 246 | PHPUtils::jsonEncode( $toks ); |
| 247 | } |
| 248 | ); |
| 249 | |
| 250 | $freshSrc = ( $tr->dataParsoid->startTagSrc ?? '|-' ) . |
| 251 | ( $tr->dataParsoid->getTemp()->attrSrc ?? '' ) . |
| 252 | $extraAttrSrc; |
| 253 | |
| 254 | $newTRTokens = $this->reprocessTokens( |
| 255 | null, $freshSrc, true, "table_row_tag" |
| 256 | ); |
| 257 | // Remove mw:ExpandedAttributes info since this is already |
| 258 | // embedded inside an outer template wrapper. |
| 259 | $newTR = $newTRTokens[0]; |
| 260 | $newTR->removeAttribute( 'typeof' ); |
| 261 | $newTR->removeAttribute( 'about' ); |
| 262 | $newTR->dataMw = null; |
| 263 | |
| 264 | // Drop the original TR from the buffered tokens |
| 265 | array_shift( $this->trReparseBuf['tokens'] ); |
| 266 | |
| 267 | // * Tokens between tr & endMeta have to be trailing comments or WS |
| 268 | // because anything else would have been tokenized by the grammar |
| 269 | // as the tr's attributes. |
| 270 | // * All tokens after this have been absorbed into $tr's attributes |
| 271 | $newTRTokens[] = $endMeta; |
| 272 | |
| 273 | // Update endMeta TSR since it now wraps the entire line |
| 274 | $endMeta->dataParsoid->tsr->end = $lineEndTSR; |
| 275 | return $this->getResultTokens( $newTRTokens ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * @return array<string|Token> |
| 280 | */ |
| 281 | private function convertNonHTMLTokenToString( Token $token ): array { |
| 282 | $dp = $token->dataParsoid; |
| 283 | $tsr = $dp->tsr ?? null; |
| 284 | |
| 285 | if ( $tsr && $tsr->end > $tsr->start ) { |
| 286 | // > will only hold if these are valid numbers |
| 287 | $str = $tsr->substr( $this->manager->getFrame()->getSource() ); |
| 288 | // sol === false ensures that the pipe will not be parsed as a <td>/listItem again |
| 289 | return $this->reprocessTokens( $tsr, $str, false ); |
| 290 | } elseif ( !empty( $dp->autoInsertedStart ) && !empty( $dp->autoInsertedEnd ) ) { |
| 291 | return [ '' ]; |
| 292 | } else { |
| 293 | $tokenName = ( $token instanceof XMLTagTk ) ? $token->getName() : ''; |
| 294 | if ( $tokenName === 'listItem' ) { |
| 295 | return [ implode( '', $token->getAttributeV( 'bullets' ) ) ]; |
| 296 | } elseif ( !in_array( $tokenName, [ 'table', 'caption', 'tr', 'td', 'th' ], true ) ) { |
| 297 | return [ $token ]; |
| 298 | } |
| 299 | |
| 300 | // Only table tags from here on |
| 301 | $buf = $dp->startTagSrc ?? null; |
| 302 | if ( !$buf ) { |
| 303 | switch ( $tokenName ) { |
| 304 | case 'td': |
| 305 | $buf = ( $dp->stx ?? '' ) === 'row' ? '||' : '|'; |
| 306 | break; |
| 307 | case 'th': |
| 308 | $buf = ( $dp->stx ?? '' ) === 'row' ? '!!' : '!'; |
| 309 | break; |
| 310 | case 'tr': |
| 311 | $buf = '|-'; |
| 312 | break; |
| 313 | case 'caption': |
| 314 | $buf = $token instanceof TagTk ? '|+' : ''; |
| 315 | break; |
| 316 | case 'table': |
| 317 | if ( $token instanceof EndTagTk ) { |
| 318 | // Won't have attributes. Bail early! |
| 319 | return [ '|}' ]; |
| 320 | } |
| 321 | $buf = '{|'; |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // Extract attributes |
| 327 | $needsRetokenization = false; |
| 328 | $cellAttrSrc = $dp->getTemp()->attrSrc ?? null; |
| 329 | if ( $cellAttrSrc ) { |
| 330 | // Copied from TableFixups::convertAttribsToContent |
| 331 | if ( preg_match( "#['[{<]#", $cellAttrSrc ) ) { |
| 332 | $needsRetokenization = true; |
| 333 | } |
| 334 | $buf .= $cellAttrSrc; |
| 335 | if ( in_array( $tokenName, [ 'caption', 'td', 'th' ], true ) ) { |
| 336 | $buf .= $dp->attrSepSrc ?? '|'; |
| 337 | } |
| 338 | } else { |
| 339 | $buf .= $dp->attrSepSrc ?? ''; |
| 340 | } |
| 341 | |
| 342 | if ( $needsRetokenization ) { |
| 343 | // sol === false ensures that the pipe will not be parsed as td/th/tr/table/caption |
| 344 | $useTsr = $tsr?->start !== null; |
| 345 | return $this->reprocessTokens( |
| 346 | $useTsr ? $tsr : $this->srcOffset, |
| 347 | $buf, false ); |
| 348 | } else { |
| 349 | return [ $buf ]; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * @inheritDoc |
| 356 | */ |
| 357 | public function onAny( $token ): ?array { |
| 358 | try { |
| 359 | return $this->onAnyInternal( $token ); |
| 360 | } finally { |
| 361 | // Ensure we always clean up 'atStart' even |
| 362 | // in the presence of exceptions. |
| 363 | if ( $this->tplInfo && $this->tplInfo['startMeta'] !== $token ) { |
| 364 | $this->tplInfo['atStart'] = false; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @param string|Token $token |
| 371 | * @return ?array<string|Token> |
| 372 | */ |
| 373 | public function onAnyInternal( $token ): ?array { |
| 374 | $abortedTrReparse = false; |
| 375 | if ( $this->trReparseBuf !== null ) { |
| 376 | if ( $token instanceof XMLTagTk && |
| 377 | in_array( $token->getName(), [ 'td', 'th', 'tr', 'caption', 'table' ], true ) |
| 378 | ) { |
| 379 | // Abort token gluing if we encounter a table tag. This effectively |
| 380 | // implies that we are missing a newline in the token stream. |
| 381 | // This can happen because we lost a newline during preprocessing. |
| 382 | // Ex: {{1x|1=\nx\n}} strips the newlines. |
| 383 | $tokens = $this->reprocessTrReparseBufViaOnAny(); |
| 384 | $abortedTrReparse = true; |
| 385 | } elseif ( $token instanceof SelfclosingTagTk && |
| 386 | TokenUtils::hasTypeOf( $token, 'mw:Includes/IncludeOnly' ) |
| 387 | ) { |
| 388 | // includeonly directives should be entirely skipped and |
| 389 | // make everything messy if they include "\n" internally. |
| 390 | // So, we abort tr-reprocessing support if we encounter them. |
| 391 | $tokens = $this->reprocessTrReparseBufViaOnAny(); |
| 392 | $abortedTrReparse = true; |
| 393 | } else { |
| 394 | if ( $token instanceof SelfclosingTagTk ) { |
| 395 | if ( TokenUtils::hasTypeOf( $token, 'mw:Transclusion/End' ) ) { |
| 396 | if ( !isset( $this->trReparseBuf['endMeta'] ) ) { |
| 397 | $this->trReparseBuf['endMeta'] = $token; |
| 398 | } |
| 399 | $this->tplInfo = null; |
| 400 | } elseif ( TokenUtils::hasTypeOf( $token, 'mw:Transclusion' ) ) { |
| 401 | $this->tplInfo = [ 'startMeta' => $token, 'atStart' => true ]; |
| 402 | } |
| 403 | } |
| 404 | // Buffer and return. |
| 405 | // The buffer will be reprocessed when a NlTk is encountered. |
| 406 | $this->trReparseBuf['tokens'][] = $token; |
| 407 | return []; |
| 408 | } |
| 409 | } else { |
| 410 | $tokens = []; |
| 411 | } |
| 412 | |
| 413 | $this->env->trace( 'tsp', $this->pipelineId, |
| 414 | function () use ( $token ) { |
| 415 | return "(indep=" . ( $this->inIndependentParse ? "yes" : "no " ) . |
| 416 | ";sol=" . ( $this->sol ? "yes" : "no " ) . ') ' . |
| 417 | PHPUtils::jsonEncode( $token ); |
| 418 | } |
| 419 | ); |
| 420 | |
| 421 | $tokens[] = $token; |
| 422 | switch ( true ) { |
| 423 | case is_string( $token ): |
| 424 | // While we are buffering newlines to suppress them |
| 425 | // in case we see a category, buffer all intervening |
| 426 | // white-space as well. |
| 427 | if ( count( $this->nlWsMetaTokenBuf ) > 0 && preg_match( '/^\s*$/D', $token ) ) { |
| 428 | $this->nlWsMetaTokenBuf[] = $token; |
| 429 | return []; |
| 430 | } |
| 431 | |
| 432 | // This is only applicable where we use Parsoid's (broken) native preprocessor. |
| 433 | // This supports scenarios like "{{1x|*bar}}". When "{{{1}}}" is tokenized |
| 434 | // "*bar" isn't available and so won't become a list. |
| 435 | // FIXME: {{1x|1===foo==}} will still be broken. So, this fix below is somewhat |
| 436 | // independent of T2529 for our broken preprocessor but we are restricting the |
| 437 | // fix to T2529. |
| 438 | $T2529hack = false; |
| 439 | if ( $this->env->nativeTemplateExpansionEnabled() && |
| 440 | ( $this->tplInfo['atStart'] ?? false ) && |
| 441 | preg_match( '/^(?:{\\||[:;#*])/', $token ) |
| 442 | ) { |
| 443 | // Add a newline & force SOL |
| 444 | $T2529hack = true; |
| 445 | // Remove newline insertion in the core preprocessor |
| 446 | // only occurs if we weren't already at the start of |
| 447 | // the line (see discussion in ::onNewline() above). |
| 448 | if ( !$this->sol ) { |
| 449 | $this->nlWsMetaTokenBuf[] = new NlTk( null ); |
| 450 | $this->sol = true; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if ( $this->sol ) { |
| 455 | // Attempt to match "{|" after a newline and convert |
| 456 | // it to a table token. |
| 457 | if ( $this->inIndependentParse && str_starts_with( $token, '{|' ) ) { |
| 458 | // Reparse string with the 'table_start_tag' rule |
| 459 | // and fully reprocess them. |
| 460 | try { |
| 461 | $tokens = $this->reprocessTokens( |
| 462 | $this->srcOffset, $token, |
| 463 | true, 'table_start_tag' ); |
| 464 | $this->wikiTableNesting++; |
| 465 | } catch ( SyntaxError ) { |
| 466 | // XXX: The string begins with table start syntax, |
| 467 | // we really shouldn't be here. Anything else on the |
| 468 | // line would get swallowed up as attributes. |
| 469 | $this->env->log( 'error', 'Failed to tokenize table start tag.' ); |
| 470 | $this->clearSOL(); |
| 471 | } |
| 472 | } elseif ( $this->inIndependentParse && $T2529hack ) { // {| has been handled above |
| 473 | try { |
| 474 | $tokens = $this->reprocessTokens( |
| 475 | $this->srcOffset, $token, |
| 476 | true, 'list_item' ); |
| 477 | } catch ( SyntaxError ) { |
| 478 | $this->env->log( 'error', 'Failed to tokenize list item.' ); |
| 479 | $this->clearSOL(); |
| 480 | } |
| 481 | } elseif ( preg_match( '/^\s*$/D', $token ) ) { |
| 482 | // White-space doesn't change SOL state. Update srcOffset. |
| 483 | if ( $this->srcOffset !== null ) { |
| 484 | $this->srcOffset = $this->srcOffset->offset( strlen( $token ) ); |
| 485 | } |
| 486 | } else { |
| 487 | $this->clearSOL(); |
| 488 | } |
| 489 | } else { |
| 490 | $this->clearSOL(); |
| 491 | } |
| 492 | break; |
| 493 | |
| 494 | case $token instanceof CommentTk: |
| 495 | case $token instanceof EmptyLineTk: |
| 496 | // Comments / EmptyLines don't change SOL state |
| 497 | // Update srcOffset |
| 498 | $this->resetSrcOffset( $token->dataParsoid->tsr ?? null ); |
| 499 | break; |
| 500 | |
| 501 | case $token instanceof SelfclosingTagTk: |
| 502 | if ( $token->getName() === 'meta' && ( $token->dataParsoid->stx ?? '' ) !== 'html' ) { |
| 503 | $this->resetSrcOffset( $token->dataParsoid->tsr ?? null ); |
| 504 | if ( TokenUtils::hasTypeOf( $token, 'mw:Transclusion' ) ) { |
| 505 | $this->tplInfo = [ 'startMeta' => $token, 'atStart' => true ]; |
| 506 | if ( count( $this->nlWsMetaTokenBuf ) > 0 ) { |
| 507 | // If we have buffered newlines, we might very well encounter |
| 508 | // a category link, so continue buffering. |
| 509 | $this->nlWsMetaTokenBuf[] = $token; |
| 510 | return []; |
| 511 | } |
| 512 | } elseif ( TokenUtils::hasTypeOf( $token, 'mw:Transclusion/End' ) ) { |
| 513 | $this->tplInfo = null; |
| 514 | } |
| 515 | } elseif ( TokenUtils::isSolTransparentLinkTag( $token ) ) { |
| 516 | // Replace buffered newline & whitespace tokens with EmptyLineTk tokens. |
| 517 | // This tunnels them through the rest of the transformations |
| 518 | // without affecting them. During HTML building, they are expanded |
| 519 | // back to newlines / whitespace. |
| 520 | $n = count( $this->nlWsMetaTokenBuf ); |
| 521 | if ( $n > 0 ) { |
| 522 | $i = 0; |
| 523 | while ( $i < $n && |
| 524 | !( $this->nlWsMetaTokenBuf[$i] instanceof SelfclosingTagTk ) |
| 525 | ) { |
| 526 | $i++; |
| 527 | } |
| 528 | |
| 529 | $toks = [ |
| 530 | new EmptyLineTk( array_slice( $this->nlWsMetaTokenBuf, 0, $i ) ) |
| 531 | ]; |
| 532 | if ( $i < $n ) { |
| 533 | $toks[] = $this->nlWsMetaTokenBuf[$i]; |
| 534 | if ( $i + 1 < $n ) { |
| 535 | $toks[] = new EmptyLineTk( array_slice( $this->nlWsMetaTokenBuf, $i + 1 ) ); |
| 536 | } |
| 537 | } |
| 538 | $tokens = array_merge( $toks, $tokens ); |
| 539 | $this->nlWsMetaTokenBuf = []; |
| 540 | } |
| 541 | $this->clearSOL(); |
| 542 | } else { |
| 543 | $this->clearSOL(); |
| 544 | } |
| 545 | break; |
| 546 | |
| 547 | case $token instanceof TagTk: |
| 548 | if ( $this->inIndependentParse && !TokenUtils::isHTMLTag( $token ) ) { |
| 549 | $tokenName = $token->getName(); |
| 550 | if ( $tokenName === 'listItem' && isset( $this->options['attrExpansion'] ) ) { |
| 551 | // Convert list items back to bullet wikitext in attribute context |
| 552 | $tokens = $this->convertNonHTMLTokenToString( $token ); |
| 553 | } elseif ( $tokenName === 'table' ) { |
| 554 | $this->wikiTableNesting++; |
| 555 | } elseif ( in_array( $tokenName, [ 'td', 'th', 'tr', 'caption' ], true ) ) { |
| 556 | if ( $this->wikiTableNesting === 0 ) { |
| 557 | $tokens = $this->convertNonHTMLTokenToString( $token ); |
| 558 | } elseif ( $this->tplInfo && $tokenName === 'tr' ) { |
| 559 | // We may have to reparse attributes of this <tr>. |
| 560 | // So, track relevant info. |
| 561 | $this->trReparseBuf = [ |
| 562 | 'tr' => $token, |
| 563 | 'trTplInfo' => $this->tplInfo, |
| 564 | 'tokens' => [ $token ] |
| 565 | ]; |
| 566 | if ( $abortedTrReparse ) { |
| 567 | // If we have tokens from a previous trReparseBuf that was |
| 568 | // aborted and reprocessed above, we need to emit them now. |
| 569 | // So, dont return []. But, pop the $tr we are buffering above. |
| 570 | array_pop( $tokens ); |
| 571 | } else { |
| 572 | return []; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | $this->clearSOL(); |
| 578 | break; |
| 579 | |
| 580 | case $token instanceof EndTagTk: |
| 581 | if ( $this->inIndependentParse && !TokenUtils::isHTMLTag( $token ) ) { |
| 582 | if ( $this->wikiTableNesting > 0 ) { |
| 583 | if ( $token->getName() === 'table' ) { |
| 584 | $this->wikiTableNesting--; |
| 585 | } |
| 586 | } elseif ( $token->getName() === 'table' || $token->getName() === 'caption' ) { |
| 587 | $tokens = $this->convertNonHTMLTokenToString( $token ); |
| 588 | } |
| 589 | } |
| 590 | $this->clearSOL(); |
| 591 | break; |
| 592 | |
| 593 | default: |
| 594 | break; |
| 595 | } |
| 596 | |
| 597 | $tokens = $this->getResultTokens( $tokens ); |
| 598 | $this->env->trace( 'tsp', $this->pipelineId, " ---> ", $tokens ); |
| 599 | return $tokens; |
| 600 | } |
| 601 | |
| 602 | private function resetSrcOffset( ?SourceRange $tsr ): void { |
| 603 | if ( $tsr?->end === null ) { |
| 604 | $this->srcOffset = null; |
| 605 | } else { |
| 606 | $this->srcOffset = new SourceRange( $tsr->end, $tsr->end, $tsr->source ); |
| 607 | } |
| 608 | } |
| 609 | } |