Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
ForeignAttributes
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 11
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 offsetExists
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 offsetGet
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 offsetSet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 offsetUnset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getValues
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIterator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getObjects
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 merge
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 clone
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\RemexHtml\TreeBuilder;
5
6use Wikimedia\RemexHtml\HTMLData;
7use Wikimedia\RemexHtml\Tokenizer\Attribute;
8use Wikimedia\RemexHtml\Tokenizer\Attributes;
9
10/**
11 * An Attributes class for storing attributes on foreign elements, which may
12 * have namespaces. Features lazy adjustment of attribute name case.
13 */
14class ForeignAttributes implements Attributes {
15    protected Attributes $unadjusted;
16
17    /** @var array The map of lowercase attribute name to correct attribute name */
18    protected $table;
19
20    /** @var Attribute[]|null */
21    protected $attrObjects;
22
23    /**
24     * Adjustment tables for the case of attributes on MathML and SVG elements
25     */
26    private const ADJUSTMENT_TABLES = [
27        'math' => [
28            'definitionurl' => 'definitionURL',
29        ],
30        'svg' => [
31            'attributename' => 'attributeName',
32            'attributetype' => 'attributeType',
33            'basefrequency' => 'baseFrequency',
34            'baseprofile' => 'baseProfile',
35            'calcmode' => 'calcMode',
36            'clippathunits' => 'clipPathUnits',
37            'diffuseconstant' => 'diffuseConstant',
38            'edgemode' => 'edgeMode',
39            'filterunits' => 'filterUnits',
40            'glyphref' => 'glyphRef',
41            'gradienttransform' => 'gradientTransform',
42            'gradientunits' => 'gradientUnits',
43            'kernelmatrix' => 'kernelMatrix',
44            'kernelunitlength' => 'kernelUnitLength',
45            'keypoints' => 'keyPoints',
46            'keysplines' => 'keySplines',
47            'keytimes' => 'keyTimes',
48            'lengthadjust' => 'lengthAdjust',
49            'limitingconeangle' => 'limitingConeAngle',
50            'markerheight' => 'markerHeight',
51            'markerunits' => 'markerUnits',
52            'markerwidth' => 'markerWidth',
53            'maskcontentunits' => 'maskContentUnits',
54            'maskunits' => 'maskUnits',
55            'numoctaves' => 'numOctaves',
56            'pathlength' => 'pathLength',
57            'patterncontentunits' => 'patternContentUnits',
58            'patterntransform' => 'patternTransform',
59            'patternunits' => 'patternUnits',
60            'pointsatx' => 'pointsAtX',
61            'pointsaty' => 'pointsAtY',
62            'pointsatz' => 'pointsAtZ',
63            'preservealpha' => 'preserveAlpha',
64            'preserveaspectratio' => 'preserveAspectRatio',
65            'primitiveunits' => 'primitiveUnits',
66            'refx' => 'refX',
67            'refy' => 'refY',
68            'repeatcount' => 'repeatCount',
69            'repeatdur' => 'repeatDur',
70            'requiredextensions' => 'requiredExtensions',
71            'requiredfeatures' => 'requiredFeatures',
72            'specularconstant' => 'specularConstant',
73            'specularexponent' => 'specularExponent',
74            'spreadmethod' => 'spreadMethod',
75            'startoffset' => 'startOffset',
76            'stddeviation' => 'stdDeviation',
77            'stitchtiles' => 'stitchTiles',
78            'surfacescale' => 'surfaceScale',
79            'systemlanguage' => 'systemLanguage',
80            'tablevalues' => 'tableValues',
81            'targetx' => 'targetX',
82            'targety' => 'targetY',
83            'textlength' => 'textLength',
84            'viewbox' => 'viewBox',
85            'viewtarget' => 'viewTarget',
86            'xchannelselector' => 'xChannelSelector',
87            'ychannelselector' => 'yChannelSelector',
88            'zoomandpan' => 'zoomAndPan',
89        ],
90        'other' => [],
91    ];
92
93    /**
94     * The potentially namespaced attributes, and the namespaces they belong to.
95     * Excepting xmlns since it is very special.
96     */
97    private const NAMESPACE_MAP = [
98        'xlink:actuate' => HTMLData::NS_XLINK,
99        'xlink:arcrole' => HTMLData::NS_XLINK,
100        'xlink:href' => HTMLData::NS_XLINK,
101        'xlink:role' => HTMLData::NS_XLINK,
102        'xlink:show' => HTMLData::NS_XLINK,
103        'xlink:title' => HTMLData::NS_XLINK,
104        'xlink:type' => HTMLData::NS_XLINK,
105        'xml:lang' => HTMLData::NS_XML,
106        'xml:space' => HTMLData::NS_XML,
107        'xmlns:xlink' => HTMLData::NS_XMLNS,
108    ];
109
110    /**
111     * @param Attributes $unadjusted The unadjusted attributes from the Tokenizer
112     * @param string $type The element type, which may be "math", "svg" or "other".
113     */
114    public function __construct( Attributes $unadjusted, $type ) {
115        $this->unadjusted = $unadjusted;
116        $this->table = self::ADJUSTMENT_TABLES[$type];
117    }
118
119    /** @inheritDoc */
120    public function offsetExists( $offset ): bool {
121        $offset = $this->table[$offset] ?? $offset;
122        return $this->unadjusted->offsetExists( $offset );
123    }
124
125    /** @inheritDoc */
126    public function &offsetGet( $offset ): string {
127        $offset = $this->table[$offset] ?? $offset;
128        $value = &$this->unadjusted->offsetGet( $offset );
129        return $value;
130    }
131
132    /** @inheritDoc */
133    public function offsetSet( $offset, $value ): void {
134        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
135        throw new TreeBuilderError( "Setting foreign attributes is not supported" );
136    }
137
138    /** @inheritDoc */
139    public function offsetUnset( $offset ): void {
140        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
141        throw new TreeBuilderError( "Setting foreign attributes is not supported" );
142    }
143
144    /** @inheritDoc */
145    public function getValues() {
146        $result = [];
147        foreach ( $this->unadjusted->getValues() as $name => $value ) {
148            $name = $this->table[$name] ?? $name;
149            $result[$name] = $value;
150        }
151        return $result;
152    }
153
154    /** @inheritDoc */
155    public function count(): int {
156        return $this->unadjusted->count();
157    }
158
159    public function getIterator(): \ArrayIterator {
160        return new \ArrayIterator( $this->getValues() );
161    }
162
163    /** @inheritDoc */
164    public function getObjects() {
165        if ( $this->attrObjects === null ) {
166            $result = [];
167            foreach ( $this->unadjusted->getValues() as $name => $value ) {
168                if ( isset( $this->table[$name] ) ) {
169                    $name = $this->table[$name];
170                }
171                if ( $name === 'xmlns' ) {
172                    $prefix = null;
173                    $namespace = HTMLData::NS_XMLNS;
174                    $localName = $name;
175                } elseif ( isset( self::NAMESPACE_MAP[$name] ) ) {
176                    $namespace = self::NAMESPACE_MAP[$name];
177                    [ $prefix, $localName ] = explode( ':', $name, 2 );
178                } else {
179                    $prefix = null;
180                    $namespace = null;
181                    $localName = $name;
182                }
183                $result[$name] = new Attribute( $name, $namespace, $prefix, $localName, $value );
184            }
185            $this->attrObjects = $result;
186        }
187        return $this->attrObjects;
188    }
189
190    /** @inheritDoc */
191    public function merge( Attributes $other ) {
192        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
193        throw new TreeBuilderError( __METHOD__ . ': unimplemented' );
194    }
195
196    /** @inheritDoc */
197    public function clone() {
198        return $this;
199    }
200}