Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UniversalTokenHandler | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| onCompoundTk | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| onAny | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| process | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Parsoid\Wt2Html\TT; |
| 5 | |
| 6 | use Wikimedia\Assert\UnreachableException; |
| 7 | use Wikimedia\Parsoid\Tokens\CompoundTk; |
| 8 | use Wikimedia\Parsoid\Tokens\Token; |
| 9 | |
| 10 | /** |
| 11 | * These handlers process *all* tags or *all* tokens even. |
| 12 | * |
| 13 | * Ex: OnlyInclude, AttributeExpander, SanitizerHandler |
| 14 | * |
| 15 | * They only need to support onAny handlers. |
| 16 | */ |
| 17 | abstract class UniversalTokenHandler extends TokenHandler { |
| 18 | /** |
| 19 | * Process the compound token in a handler-specific way. |
| 20 | * - Some handlers might ignore it and pass it through |
| 21 | * - Some handlers might process its nested tokens and update it |
| 22 | * - Some handlers might process its nested tokens and flatten it |
| 23 | * |
| 24 | * To ensure that handlers that encounter compound tokens always |
| 25 | * have explicit handling for them, the default implementation |
| 26 | * here will throw an exception! |
| 27 | * |
| 28 | * NOTE: The only reason we are processing a handler here is because |
| 29 | * of needing to support profiling. For the profiling use case, |
| 30 | * we will be passing a TraceProxy instead of the handler itself. |
| 31 | * |
| 32 | * @return ?array<string|Token> |
| 33 | */ |
| 34 | public function onCompoundTk( CompoundTk $ctk, TokenHandler $tokensHandler ): ?array { |
| 35 | throw new UnreachableException( |
| 36 | get_class( $this ) . ": Unsupported compound token." |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * This handler is called for *all* tokens in the token stream. |
| 42 | * |
| 43 | * @param Token|string $token Token to be processed |
| 44 | * @return ?array<string|Token> |
| 45 | * - null indicates that the token was untransformed |
| 46 | * and it will be added to this handler's output array. |
| 47 | * - an array indicates the token was transformed |
| 48 | * and the contents of the array will be added to this |
| 49 | * handler's output array. |
| 50 | */ |
| 51 | public function onAny( $token ): ?array { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | /** @inheritDoc */ |
| 56 | public function process( array $tokens ): array { |
| 57 | $accum = []; |
| 58 | foreach ( $tokens as $token ) { |
| 59 | if ( $token instanceof CompoundTk ) { |
| 60 | $res = $this->onCompoundTk( $token, $this ); |
| 61 | } else { |
| 62 | $res = $this->onAny( $token ); |
| 63 | } |
| 64 | if ( $res === null ) { |
| 65 | $accum[] = $token; |
| 66 | } else { |
| 67 | // Avoid array_merge() -- see https://w.wiki/3zvE |
| 68 | foreach ( $res as $t ) { |
| 69 | $accum[] = $t; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return $accum; |
| 74 | } |
| 75 | } |