Parser
3 years ago
Serializer
3 years ago
Elements.php
3 years ago
Entities.php
3 years ago
Exception.php
3 years ago
InstructionProcessor.php
3 years ago
Elements.php
521 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Provide general element functions. |
| 5 | */ |
| 6 | namespace IAWP\Masterminds\HTML5; |
| 7 | |
| 8 | /** |
| 9 | * This class provides general information about HTML5 elements, |
| 10 | * including syntactic and semantic issues. |
| 11 | * Parsers and serializers can |
| 12 | * use this class as a reference point for information about the rules |
| 13 | * of various HTML5 elements. |
| 14 | * |
| 15 | * @todo consider using a bitmask table lookup. There is enough overlap in |
| 16 | * naming that this could significantly shrink the size and maybe make it |
| 17 | * faster. See the Go teams implementation at https://code.google.com/p/go/source/browse/html/atom. |
| 18 | */ |
| 19 | class Elements |
| 20 | { |
| 21 | /** |
| 22 | * Indicates an element is described in the specification. |
| 23 | */ |
| 24 | const KNOWN_ELEMENT = 1; |
| 25 | // From section 8.1.2: "script", "style" |
| 26 | // From 8.2.5.4.7 ("in body" insertion mode): "noembed" |
| 27 | // From 8.4 "style", "xmp", "iframe", "noembed", "noframes" |
| 28 | /** |
| 29 | * Indicates the contained text should be processed as raw text. |
| 30 | */ |
| 31 | const TEXT_RAW = 2; |
| 32 | // From section 8.1.2: "textarea", "title" |
| 33 | /** |
| 34 | * Indicates the contained text should be processed as RCDATA. |
| 35 | */ |
| 36 | const TEXT_RCDATA = 4; |
| 37 | /** |
| 38 | * Indicates the tag cannot have content. |
| 39 | */ |
| 40 | const VOID_TAG = 8; |
| 41 | // "address", "article", "aside", "blockquote", "center", "details", "dialog", "dir", "div", "dl", |
| 42 | // "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "menu", |
| 43 | // "nav", "ol", "p", "section", "summary", "ul" |
| 44 | // "h1", "h2", "h3", "h4", "h5", "h6" |
| 45 | // "pre", "listing" |
| 46 | // "form" |
| 47 | // "plaintext" |
| 48 | /** |
| 49 | * Indicates that if a previous event is for a P tag, that element |
| 50 | * should be considered closed. |
| 51 | */ |
| 52 | const AUTOCLOSE_P = 16; |
| 53 | /** |
| 54 | * Indicates that the text inside is plaintext (pre). |
| 55 | */ |
| 56 | const TEXT_PLAINTEXT = 32; |
| 57 | // See https://developer.mozilla.org/en-US/docs/HTML/Block-level_elements |
| 58 | /** |
| 59 | * Indicates that the tag is a block. |
| 60 | */ |
| 61 | const BLOCK_TAG = 64; |
| 62 | /** |
| 63 | * Indicates that the tag allows only inline elements as child nodes. |
| 64 | */ |
| 65 | const BLOCK_ONLY_INLINE = 128; |
| 66 | /** |
| 67 | * The HTML5 elements as defined in http://dev.w3.org/html5/markup/elements.html. |
| 68 | * |
| 69 | * @var array |
| 70 | */ |
| 71 | public static $html5 = array( |
| 72 | 'a' => 1, |
| 73 | 'abbr' => 1, |
| 74 | 'address' => 65, |
| 75 | // NORMAL | BLOCK_TAG |
| 76 | 'area' => 9, |
| 77 | // NORMAL | VOID_TAG |
| 78 | 'article' => 81, |
| 79 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 80 | 'aside' => 81, |
| 81 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 82 | 'audio' => 1, |
| 83 | // NORMAL |
| 84 | 'b' => 1, |
| 85 | 'base' => 9, |
| 86 | // NORMAL | VOID_TAG |
| 87 | 'bdi' => 1, |
| 88 | 'bdo' => 1, |
| 89 | 'blockquote' => 81, |
| 90 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 91 | 'body' => 1, |
| 92 | 'br' => 9, |
| 93 | // NORMAL | VOID_TAG |
| 94 | 'button' => 1, |
| 95 | 'canvas' => 65, |
| 96 | // NORMAL | BLOCK_TAG |
| 97 | 'caption' => 1, |
| 98 | 'cite' => 1, |
| 99 | 'code' => 1, |
| 100 | 'col' => 9, |
| 101 | // NORMAL | VOID_TAG |
| 102 | 'colgroup' => 1, |
| 103 | 'command' => 9, |
| 104 | // NORMAL | VOID_TAG |
| 105 | // "data" => 1, // This is highly experimental and only part of the whatwg spec (not w3c). See https://developer.mozilla.org/en-US/docs/HTML/Element/data |
| 106 | 'datalist' => 1, |
| 107 | 'dd' => 65, |
| 108 | // NORMAL | BLOCK_TAG |
| 109 | 'del' => 1, |
| 110 | 'details' => 17, |
| 111 | // NORMAL | AUTOCLOSE_P, |
| 112 | 'dfn' => 1, |
| 113 | 'dialog' => 17, |
| 114 | // NORMAL | AUTOCLOSE_P, |
| 115 | 'div' => 81, |
| 116 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 117 | 'dl' => 81, |
| 118 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 119 | 'dt' => 1, |
| 120 | 'em' => 1, |
| 121 | 'embed' => 9, |
| 122 | // NORMAL | VOID_TAG |
| 123 | 'fieldset' => 81, |
| 124 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 125 | 'figcaption' => 81, |
| 126 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 127 | 'figure' => 81, |
| 128 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 129 | 'footer' => 81, |
| 130 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 131 | 'form' => 81, |
| 132 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 133 | 'h1' => 81, |
| 134 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 135 | 'h2' => 81, |
| 136 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 137 | 'h3' => 81, |
| 138 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 139 | 'h4' => 81, |
| 140 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 141 | 'h5' => 81, |
| 142 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 143 | 'h6' => 81, |
| 144 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 145 | 'head' => 1, |
| 146 | 'header' => 81, |
| 147 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 148 | 'hgroup' => 81, |
| 149 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 150 | 'hr' => 73, |
| 151 | // NORMAL | VOID_TAG |
| 152 | 'html' => 1, |
| 153 | 'i' => 1, |
| 154 | 'iframe' => 3, |
| 155 | // NORMAL | TEXT_RAW |
| 156 | 'img' => 9, |
| 157 | // NORMAL | VOID_TAG |
| 158 | 'input' => 9, |
| 159 | // NORMAL | VOID_TAG |
| 160 | 'kbd' => 1, |
| 161 | 'ins' => 1, |
| 162 | 'keygen' => 9, |
| 163 | // NORMAL | VOID_TAG |
| 164 | 'label' => 1, |
| 165 | 'legend' => 1, |
| 166 | 'li' => 1, |
| 167 | 'link' => 9, |
| 168 | // NORMAL | VOID_TAG |
| 169 | 'map' => 1, |
| 170 | 'mark' => 1, |
| 171 | 'menu' => 17, |
| 172 | // NORMAL | AUTOCLOSE_P, |
| 173 | 'meta' => 9, |
| 174 | // NORMAL | VOID_TAG |
| 175 | 'meter' => 1, |
| 176 | 'nav' => 17, |
| 177 | // NORMAL | AUTOCLOSE_P, |
| 178 | 'noscript' => 65, |
| 179 | // NORMAL | BLOCK_TAG |
| 180 | 'object' => 1, |
| 181 | 'ol' => 81, |
| 182 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 183 | 'optgroup' => 1, |
| 184 | 'option' => 1, |
| 185 | 'output' => 65, |
| 186 | // NORMAL | BLOCK_TAG |
| 187 | 'p' => 209, |
| 188 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG | BLOCK_ONLY_INLINE |
| 189 | 'param' => 9, |
| 190 | // NORMAL | VOID_TAG |
| 191 | 'pre' => 81, |
| 192 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 193 | 'progress' => 1, |
| 194 | 'q' => 1, |
| 195 | 'rp' => 1, |
| 196 | 'rt' => 1, |
| 197 | 'ruby' => 1, |
| 198 | 's' => 1, |
| 199 | 'samp' => 1, |
| 200 | 'script' => 3, |
| 201 | // NORMAL | TEXT_RAW |
| 202 | 'section' => 81, |
| 203 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 204 | 'select' => 1, |
| 205 | 'small' => 1, |
| 206 | 'source' => 9, |
| 207 | // NORMAL | VOID_TAG |
| 208 | 'span' => 1, |
| 209 | 'strong' => 1, |
| 210 | 'style' => 3, |
| 211 | // NORMAL | TEXT_RAW |
| 212 | 'sub' => 1, |
| 213 | 'summary' => 17, |
| 214 | // NORMAL | AUTOCLOSE_P, |
| 215 | 'sup' => 1, |
| 216 | 'table' => 65, |
| 217 | // NORMAL | BLOCK_TAG |
| 218 | 'tbody' => 1, |
| 219 | 'td' => 1, |
| 220 | 'textarea' => 5, |
| 221 | // NORMAL | TEXT_RCDATA |
| 222 | 'tfoot' => 65, |
| 223 | // NORMAL | BLOCK_TAG |
| 224 | 'th' => 1, |
| 225 | 'thead' => 1, |
| 226 | 'time' => 1, |
| 227 | 'title' => 5, |
| 228 | // NORMAL | TEXT_RCDATA |
| 229 | 'tr' => 1, |
| 230 | 'track' => 9, |
| 231 | // NORMAL | VOID_TAG |
| 232 | 'u' => 1, |
| 233 | 'ul' => 81, |
| 234 | // NORMAL | AUTOCLOSE_P | BLOCK_TAG |
| 235 | 'var' => 1, |
| 236 | 'video' => 65, |
| 237 | // NORMAL | BLOCK_TAG |
| 238 | 'wbr' => 9, |
| 239 | // NORMAL | VOID_TAG |
| 240 | // Legacy? |
| 241 | 'basefont' => 8, |
| 242 | // VOID_TAG |
| 243 | 'bgsound' => 8, |
| 244 | // VOID_TAG |
| 245 | 'noframes' => 2, |
| 246 | // RAW_TEXT |
| 247 | 'frame' => 9, |
| 248 | // NORMAL | VOID_TAG |
| 249 | 'frameset' => 1, |
| 250 | 'center' => 16, |
| 251 | 'dir' => 16, |
| 252 | 'listing' => 16, |
| 253 | // AUTOCLOSE_P |
| 254 | 'plaintext' => 48, |
| 255 | // AUTOCLOSE_P | TEXT_PLAINTEXT |
| 256 | 'applet' => 0, |
| 257 | 'marquee' => 0, |
| 258 | 'isindex' => 8, |
| 259 | // VOID_TAG |
| 260 | 'xmp' => 20, |
| 261 | // AUTOCLOSE_P | VOID_TAG | RAW_TEXT |
| 262 | 'noembed' => 2, |
| 263 | ); |
| 264 | /** |
| 265 | * The MathML elements. |
| 266 | * See http://www.w3.org/wiki/MathML/Elements. |
| 267 | * |
| 268 | * In our case we are only concerned with presentation MathML and not content |
| 269 | * MathML. There is a nice list of this subset at https://developer.mozilla.org/en-US/docs/MathML/Element. |
| 270 | * |
| 271 | * @var array |
| 272 | */ |
| 273 | public static $mathml = array('maction' => 1, 'maligngroup' => 1, 'malignmark' => 1, 'math' => 1, 'menclose' => 1, 'merror' => 1, 'mfenced' => 1, 'mfrac' => 1, 'mglyph' => 1, 'mi' => 1, 'mlabeledtr' => 1, 'mlongdiv' => 1, 'mmultiscripts' => 1, 'mn' => 1, 'mo' => 1, 'mover' => 1, 'mpadded' => 1, 'mphantom' => 1, 'mroot' => 1, 'mrow' => 1, 'ms' => 1, 'mscarries' => 1, 'mscarry' => 1, 'msgroup' => 1, 'msline' => 1, 'mspace' => 1, 'msqrt' => 1, 'msrow' => 1, 'mstack' => 1, 'mstyle' => 1, 'msub' => 1, 'msup' => 1, 'msubsup' => 1, 'mtable' => 1, 'mtd' => 1, 'mtext' => 1, 'mtr' => 1, 'munder' => 1, 'munderover' => 1); |
| 274 | /** |
| 275 | * The svg elements. |
| 276 | * |
| 277 | * The Mozilla documentation has a good list at https://developer.mozilla.org/en-US/docs/SVG/Element. |
| 278 | * The w3c list appears to be lacking in some areas like filter effect elements. |
| 279 | * That list can be found at http://www.w3.org/wiki/SVG/Elements. |
| 280 | * |
| 281 | * Note, FireFox appears to do a better job rendering filter effects than chrome. |
| 282 | * While they are in the spec I'm not sure how widely implemented they are. |
| 283 | * |
| 284 | * @var array |
| 285 | */ |
| 286 | public static $svg = array( |
| 287 | 'a' => 1, |
| 288 | 'altGlyph' => 1, |
| 289 | 'altGlyphDef' => 1, |
| 290 | 'altGlyphItem' => 1, |
| 291 | 'animate' => 1, |
| 292 | 'animateColor' => 1, |
| 293 | 'animateMotion' => 1, |
| 294 | 'animateTransform' => 1, |
| 295 | 'circle' => 1, |
| 296 | 'clipPath' => 1, |
| 297 | 'color-profile' => 1, |
| 298 | 'cursor' => 1, |
| 299 | 'defs' => 1, |
| 300 | 'desc' => 1, |
| 301 | 'ellipse' => 1, |
| 302 | 'feBlend' => 1, |
| 303 | 'feColorMatrix' => 1, |
| 304 | 'feComponentTransfer' => 1, |
| 305 | 'feComposite' => 1, |
| 306 | 'feConvolveMatrix' => 1, |
| 307 | 'feDiffuseLighting' => 1, |
| 308 | 'feDisplacementMap' => 1, |
| 309 | 'feDistantLight' => 1, |
| 310 | 'feFlood' => 1, |
| 311 | 'feFuncA' => 1, |
| 312 | 'feFuncB' => 1, |
| 313 | 'feFuncG' => 1, |
| 314 | 'feFuncR' => 1, |
| 315 | 'feGaussianBlur' => 1, |
| 316 | 'feImage' => 1, |
| 317 | 'feMerge' => 1, |
| 318 | 'feMergeNode' => 1, |
| 319 | 'feMorphology' => 1, |
| 320 | 'feOffset' => 1, |
| 321 | 'fePointLight' => 1, |
| 322 | 'feSpecularLighting' => 1, |
| 323 | 'feSpotLight' => 1, |
| 324 | 'feTile' => 1, |
| 325 | 'feTurbulence' => 1, |
| 326 | 'filter' => 1, |
| 327 | 'font' => 1, |
| 328 | 'font-face' => 1, |
| 329 | 'font-face-format' => 1, |
| 330 | 'font-face-name' => 1, |
| 331 | 'font-face-src' => 1, |
| 332 | 'font-face-uri' => 1, |
| 333 | 'foreignObject' => 1, |
| 334 | 'g' => 1, |
| 335 | 'glyph' => 1, |
| 336 | 'glyphRef' => 1, |
| 337 | 'hkern' => 1, |
| 338 | 'image' => 1, |
| 339 | 'line' => 1, |
| 340 | 'linearGradient' => 1, |
| 341 | 'marker' => 1, |
| 342 | 'mask' => 1, |
| 343 | 'metadata' => 1, |
| 344 | 'missing-glyph' => 1, |
| 345 | 'mpath' => 1, |
| 346 | 'path' => 1, |
| 347 | 'pattern' => 1, |
| 348 | 'polygon' => 1, |
| 349 | 'polyline' => 1, |
| 350 | 'radialGradient' => 1, |
| 351 | 'rect' => 1, |
| 352 | 'script' => 3, |
| 353 | // NORMAL | RAW_TEXT |
| 354 | 'set' => 1, |
| 355 | 'stop' => 1, |
| 356 | 'style' => 3, |
| 357 | // NORMAL | RAW_TEXT |
| 358 | 'svg' => 1, |
| 359 | 'switch' => 1, |
| 360 | 'symbol' => 1, |
| 361 | 'text' => 1, |
| 362 | 'textPath' => 1, |
| 363 | 'title' => 1, |
| 364 | 'tref' => 1, |
| 365 | 'tspan' => 1, |
| 366 | 'use' => 1, |
| 367 | 'view' => 1, |
| 368 | 'vkern' => 1, |
| 369 | ); |
| 370 | /** |
| 371 | * Some attributes in SVG are case sensitive. |
| 372 | * |
| 373 | * This map contains key/value pairs with the key as the lowercase attribute |
| 374 | * name and the value with the correct casing. |
| 375 | */ |
| 376 | public static $svgCaseSensitiveAttributeMap = array('attributename' => 'attributeName', 'attributetype' => 'attributeType', 'basefrequency' => 'baseFrequency', 'baseprofile' => 'baseProfile', 'calcmode' => 'calcMode', 'clippathunits' => 'clipPathUnits', 'contentscripttype' => 'contentScriptType', 'contentstyletype' => 'contentStyleType', 'diffuseconstant' => 'diffuseConstant', 'edgemode' => 'edgeMode', 'externalresourcesrequired' => 'externalResourcesRequired', 'filterres' => 'filterRes', 'filterunits' => 'filterUnits', 'glyphref' => 'glyphRef', 'gradienttransform' => 'gradientTransform', 'gradientunits' => 'gradientUnits', 'kernelmatrix' => 'kernelMatrix', 'kernelunitlength' => 'kernelUnitLength', 'keypoints' => 'keyPoints', 'keysplines' => 'keySplines', 'keytimes' => 'keyTimes', 'lengthadjust' => 'lengthAdjust', 'limitingconeangle' => 'limitingConeAngle', 'markerheight' => 'markerHeight', 'markerunits' => 'markerUnits', 'markerwidth' => 'markerWidth', 'maskcontentunits' => 'maskContentUnits', 'maskunits' => 'maskUnits', 'numoctaves' => 'numOctaves', 'pathlength' => 'pathLength', 'patterncontentunits' => 'patternContentUnits', 'patterntransform' => 'patternTransform', 'patternunits' => 'patternUnits', 'pointsatx' => 'pointsAtX', 'pointsaty' => 'pointsAtY', 'pointsatz' => 'pointsAtZ', 'preservealpha' => 'preserveAlpha', 'preserveaspectratio' => 'preserveAspectRatio', 'primitiveunits' => 'primitiveUnits', 'refx' => 'refX', 'refy' => 'refY', 'repeatcount' => 'repeatCount', 'repeatdur' => 'repeatDur', 'requiredextensions' => 'requiredExtensions', 'requiredfeatures' => 'requiredFeatures', 'specularconstant' => 'specularConstant', 'specularexponent' => 'specularExponent', 'spreadmethod' => 'spreadMethod', 'startoffset' => 'startOffset', 'stddeviation' => 'stdDeviation', 'stitchtiles' => 'stitchTiles', 'surfacescale' => 'surfaceScale', 'systemlanguage' => 'systemLanguage', 'tablevalues' => 'tableValues', 'targetx' => 'targetX', 'targety' => 'targetY', 'textlength' => 'textLength', 'viewbox' => 'viewBox', 'viewtarget' => 'viewTarget', 'xchannelselector' => 'xChannelSelector', 'ychannelselector' => 'yChannelSelector', 'zoomandpan' => 'zoomAndPan'); |
| 377 | /** |
| 378 | * Some SVG elements are case sensitive. |
| 379 | * This map contains these. |
| 380 | * |
| 381 | * The map contains key/value store of the name is lowercase as the keys and |
| 382 | * the correct casing as the value. |
| 383 | */ |
| 384 | public static $svgCaseSensitiveElementMap = array('altglyph' => 'altGlyph', 'altglyphdef' => 'altGlyphDef', 'altglyphitem' => 'altGlyphItem', 'animatecolor' => 'animateColor', 'animatemotion' => 'animateMotion', 'animatetransform' => 'animateTransform', 'clippath' => 'clipPath', 'feblend' => 'feBlend', 'fecolormatrix' => 'feColorMatrix', 'fecomponenttransfer' => 'feComponentTransfer', 'fecomposite' => 'feComposite', 'feconvolvematrix' => 'feConvolveMatrix', 'fediffuselighting' => 'feDiffuseLighting', 'fedisplacementmap' => 'feDisplacementMap', 'fedistantlight' => 'feDistantLight', 'feflood' => 'feFlood', 'fefunca' => 'feFuncA', 'fefuncb' => 'feFuncB', 'fefuncg' => 'feFuncG', 'fefuncr' => 'feFuncR', 'fegaussianblur' => 'feGaussianBlur', 'feimage' => 'feImage', 'femerge' => 'feMerge', 'femergenode' => 'feMergeNode', 'femorphology' => 'feMorphology', 'feoffset' => 'feOffset', 'fepointlight' => 'fePointLight', 'fespecularlighting' => 'feSpecularLighting', 'fespotlight' => 'feSpotLight', 'fetile' => 'feTile', 'feturbulence' => 'feTurbulence', 'foreignobject' => 'foreignObject', 'glyphref' => 'glyphRef', 'lineargradient' => 'linearGradient', 'radialgradient' => 'radialGradient', 'textpath' => 'textPath'); |
| 385 | /** |
| 386 | * Check whether the given element meets the given criterion. |
| 387 | * |
| 388 | * Example: |
| 389 | * |
| 390 | * Elements::isA('script', Elements::TEXT_RAW); // Returns true. |
| 391 | * |
| 392 | * Elements::isA('script', Elements::TEXT_RCDATA); // Returns false. |
| 393 | * |
| 394 | * @param string $name The element name. |
| 395 | * @param int $mask One of the constants on this class. |
| 396 | * |
| 397 | * @return bool true if the element matches the mask, false otherwise. |
| 398 | */ |
| 399 | public static function isA($name, $mask) |
| 400 | { |
| 401 | return (static::element($name) & $mask) === $mask; |
| 402 | } |
| 403 | /** |
| 404 | * Test if an element is a valid html5 element. |
| 405 | * |
| 406 | * @param string $name The name of the element. |
| 407 | * |
| 408 | * @return bool true if a html5 element and false otherwise. |
| 409 | */ |
| 410 | public static function isHtml5Element($name) |
| 411 | { |
| 412 | // html5 element names are case insensitive. Forcing lowercase for the check. |
| 413 | // Do we need this check or will all data passed here already be lowercase? |
| 414 | return isset(static::$html5[\strtolower($name)]); |
| 415 | } |
| 416 | /** |
| 417 | * Test if an element name is a valid MathML presentation element. |
| 418 | * |
| 419 | * @param string $name The name of the element. |
| 420 | * |
| 421 | * @return bool true if a MathML name and false otherwise. |
| 422 | */ |
| 423 | public static function isMathMLElement($name) |
| 424 | { |
| 425 | // MathML is case-sensitive unlike html5 elements. |
| 426 | return isset(static::$mathml[$name]); |
| 427 | } |
| 428 | /** |
| 429 | * Test if an element is a valid SVG element. |
| 430 | * |
| 431 | * @param string $name The name of the element. |
| 432 | * |
| 433 | * @return bool true if a SVG element and false otherise. |
| 434 | */ |
| 435 | public static function isSvgElement($name) |
| 436 | { |
| 437 | // SVG is case-sensitive unlike html5 elements. |
| 438 | return isset(static::$svg[$name]); |
| 439 | } |
| 440 | /** |
| 441 | * Is an element name valid in an html5 document. |
| 442 | * This includes html5 elements along with other allowed embedded content |
| 443 | * such as svg and mathml. |
| 444 | * |
| 445 | * @param string $name The name of the element. |
| 446 | * |
| 447 | * @return bool true if valid and false otherwise. |
| 448 | */ |
| 449 | public static function isElement($name) |
| 450 | { |
| 451 | return static::isHtml5Element($name) || static::isMathMLElement($name) || static::isSvgElement($name); |
| 452 | } |
| 453 | /** |
| 454 | * Get the element mask for the given element name. |
| 455 | * |
| 456 | * @param string $name The name of the element. |
| 457 | * |
| 458 | * @return int the element mask. |
| 459 | */ |
| 460 | public static function element($name) |
| 461 | { |
| 462 | if (isset(static::$html5[$name])) { |
| 463 | return static::$html5[$name]; |
| 464 | } |
| 465 | if (isset(static::$svg[$name])) { |
| 466 | return static::$svg[$name]; |
| 467 | } |
| 468 | if (isset(static::$mathml[$name])) { |
| 469 | return static::$mathml[$name]; |
| 470 | } |
| 471 | return 0; |
| 472 | } |
| 473 | /** |
| 474 | * Normalize a SVG element name to its proper case and form. |
| 475 | * |
| 476 | * @param string $name The name of the element. |
| 477 | * |
| 478 | * @return string the normalized form of the element name. |
| 479 | */ |
| 480 | public static function normalizeSvgElement($name) |
| 481 | { |
| 482 | $name = \strtolower($name); |
| 483 | if (isset(static::$svgCaseSensitiveElementMap[$name])) { |
| 484 | $name = static::$svgCaseSensitiveElementMap[$name]; |
| 485 | } |
| 486 | return $name; |
| 487 | } |
| 488 | /** |
| 489 | * Normalize a SVG attribute name to its proper case and form. |
| 490 | * |
| 491 | * @param string $name The name of the attribute. |
| 492 | * |
| 493 | * @return string The normalized form of the attribute name. |
| 494 | */ |
| 495 | public static function normalizeSvgAttribute($name) |
| 496 | { |
| 497 | $name = \strtolower($name); |
| 498 | if (isset(static::$svgCaseSensitiveAttributeMap[$name])) { |
| 499 | $name = static::$svgCaseSensitiveAttributeMap[$name]; |
| 500 | } |
| 501 | return $name; |
| 502 | } |
| 503 | /** |
| 504 | * Normalize a MathML attribute name to its proper case and form. |
| 505 | * Note, all MathML element names are lowercase. |
| 506 | * |
| 507 | * @param string $name The name of the attribute. |
| 508 | * |
| 509 | * @return string The normalized form of the attribute name. |
| 510 | */ |
| 511 | public static function normalizeMathMlAttribute($name) |
| 512 | { |
| 513 | $name = \strtolower($name); |
| 514 | // Only one attribute has a mixed case form for MathML. |
| 515 | if ('definitionurl' === $name) { |
| 516 | $name = 'definitionURL'; |
| 517 | } |
| 518 | return $name; |
| 519 | } |
| 520 | } |
| 521 |