Debian Codegraph @

vendor/wikimedia/remex-html/src/TreeBuilder/ForeignAttributes.php

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