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