Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AttributeTransformManager
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
272
0.00% covered (danger)
0.00%
0 / 1
 process
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
272
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Wt2Html\TT;
5
6use Wikimedia\Parsoid\Tokens\KV;
7use Wikimedia\Parsoid\Wt2Html\Frame;
8
9/**
10 * Utility transformation manager for expanding attributes
11 * whose keys and/or values are not plain strings.
12 */
13class AttributeTransformManager {
14    /**
15     * Expand both key and values of all key/value pairs. Used for generic
16     * (non-template) tokens in the AttributeExpander handler, which runs after
17     * templates are already expanded.
18     *
19     * @param Frame $frame
20     * @param array $options
21     *  - bool inTemplate (reqd) Is this being invoked while processing a template?
22     *  - bool expandTemplates (reqd) Should we expand templates encountered here?
23     * @param KV[] $attributes
24     * @return ?KV[] expanded attributes
25     */
26    public static function process( Frame $frame, array $options, array $attributes ): ?array {
27        $expanded = false;
28
29        // Transform each argument (key and value).
30        foreach ( $attributes as &$cur ) {
31            $k = $cur->k;
32            $v = $cur->v;
33            if ( $cur->v === null ) {
34                $cur->v = $v = '';
35            }
36
37            // fast path for string-only attributes
38            if ( is_string( $k ) && is_string( $v ) ) {
39                continue;
40            }
41
42            $expandV = false;
43            if ( is_array( $v ) ) {
44                foreach ( $v as $vv ) {
45                    if ( !is_string( $vv ) ) {
46                        $expandV = true;
47                        break;
48                    }
49                }
50
51                if ( $expandV ) {
52                    // transform the value
53                    $v = $frame->expand( $v, [
54                        'attrExpansion' => true,
55                        'expandTemplates' => $options['expandTemplates'],
56                        'inTemplate' => $options['inTemplate'],
57                        'srcOffsets' => $cur->srcOffsets->value,
58                    ] );
59                }
60            }
61
62            $expandK = false;
63            if ( is_array( $k ) ) {
64                foreach ( $k as $kk ) {
65                    if ( !is_string( $kk ) ) {
66                        $expandK = true;
67                        break;
68                    }
69                }
70
71                if ( $expandK ) {
72                    // transform the key
73                    $k = $frame->expand( $k, [
74                        'attrExpansion' => true,
75                        'expandTemplates' => $options['expandTemplates'],
76                        'inTemplate' => $options['inTemplate'],
77                        'srcOffsets' => $cur->srcOffsets->key,
78                    ] );
79                }
80            }
81
82            if ( $expandK || $expandV ) {
83                $expanded = true;
84                $cur = new KV( $k, $v, $cur->srcOffsets );
85            }
86        }
87
88        return $expanded ? $attributes : null;
89    }
90}