ElementsTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. namespace Masterminds\HTML5\Tests;
  3. use Masterminds\HTML5\Elements;
  4. class ElementsTest extends TestCase
  5. {
  6. public $html5Elements = array(
  7. 'a',
  8. 'abbr',
  9. 'address',
  10. 'area',
  11. 'article',
  12. 'aside',
  13. 'audio',
  14. 'b',
  15. 'base',
  16. 'bdi',
  17. 'bdo',
  18. 'blockquote',
  19. 'body',
  20. 'br',
  21. 'button',
  22. 'canvas',
  23. 'caption',
  24. 'cite',
  25. 'code',
  26. 'col',
  27. 'colgroup',
  28. 'command',
  29. // "data",
  30. 'datalist',
  31. 'dd',
  32. 'del',
  33. 'details',
  34. 'dfn',
  35. 'dialog',
  36. 'div',
  37. 'dl',
  38. 'dt',
  39. 'em',
  40. 'embed',
  41. 'fieldset',
  42. 'figcaption',
  43. 'figure',
  44. 'footer',
  45. 'form',
  46. 'h1',
  47. 'h2',
  48. 'h3',
  49. 'h4',
  50. 'h5',
  51. 'h6',
  52. 'head',
  53. 'header',
  54. 'hgroup',
  55. 'hr',
  56. 'html',
  57. 'i',
  58. 'iframe',
  59. 'img',
  60. 'input',
  61. 'ins',
  62. 'kbd',
  63. 'keygen',
  64. 'label',
  65. 'legend',
  66. 'li',
  67. 'link',
  68. 'map',
  69. 'mark',
  70. 'menu',
  71. 'meta',
  72. 'meter',
  73. 'nav',
  74. 'noscript',
  75. 'object',
  76. 'ol',
  77. 'optgroup',
  78. 'option',
  79. 'output',
  80. 'p',
  81. 'param',
  82. 'pre',
  83. 'progress',
  84. 'q',
  85. 'rp',
  86. 'rt',
  87. 'ruby',
  88. 's',
  89. 'samp',
  90. 'script',
  91. 'section',
  92. 'select',
  93. 'small',
  94. 'source',
  95. 'span',
  96. 'strong',
  97. 'style',
  98. 'sub',
  99. 'summary',
  100. 'sup',
  101. 'table',
  102. 'tbody',
  103. 'td',
  104. 'textarea',
  105. 'tfoot',
  106. 'th',
  107. 'thead',
  108. 'time',
  109. 'title',
  110. 'tr',
  111. 'track',
  112. 'u',
  113. 'ul',
  114. 'var',
  115. 'video',
  116. 'wbr',
  117. );
  118. public $mathmlElements = array(
  119. 'maction',
  120. 'maligngroup',
  121. 'malignmark',
  122. 'math',
  123. 'menclose',
  124. 'merror',
  125. 'mfenced',
  126. 'mfrac',
  127. 'mglyph',
  128. 'mi',
  129. 'mlabeledtr',
  130. 'mlongdiv',
  131. 'mmultiscripts',
  132. 'mn',
  133. 'mo',
  134. 'mover',
  135. 'mpadded',
  136. 'mphantom',
  137. 'mroot',
  138. 'mrow',
  139. 'ms',
  140. 'mscarries',
  141. 'mscarry',
  142. 'msgroup',
  143. 'msline',
  144. 'mspace',
  145. 'msqrt',
  146. 'msrow',
  147. 'mstack',
  148. 'mstyle',
  149. 'msub',
  150. 'msup',
  151. 'msubsup',
  152. 'mtable',
  153. 'mtd',
  154. 'mtext',
  155. 'mtr',
  156. 'munder',
  157. 'munderover',
  158. );
  159. public $svgElements = array(
  160. 'a',
  161. 'altGlyph',
  162. 'altGlyphDef',
  163. 'altGlyphItem',
  164. 'animate',
  165. 'animateColor',
  166. 'animateMotion',
  167. 'animateTransform',
  168. 'circle',
  169. 'clipPath',
  170. 'color-profile',
  171. 'cursor',
  172. 'defs',
  173. 'desc',
  174. 'ellipse',
  175. 'feBlend',
  176. 'feColorMatrix',
  177. 'feComponentTransfer',
  178. 'feComposite',
  179. 'feConvolveMatrix',
  180. 'feDiffuseLighting',
  181. 'feDisplacementMap',
  182. 'feDistantLight',
  183. 'feFlood',
  184. 'feFuncA',
  185. 'feFuncB',
  186. 'feFuncG',
  187. 'feFuncR',
  188. 'feGaussianBlur',
  189. 'feImage',
  190. 'feMerge',
  191. 'feMergeNode',
  192. 'feMorphology',
  193. 'feOffset',
  194. 'fePointLight',
  195. 'feSpecularLighting',
  196. 'feSpotLight',
  197. 'feTile',
  198. 'feTurbulence',
  199. 'filter',
  200. 'font',
  201. 'font-face',
  202. 'font-face-format',
  203. 'font-face-name',
  204. 'font-face-src',
  205. 'font-face-uri',
  206. 'foreignObject',
  207. 'g',
  208. 'glyph',
  209. 'glyphRef',
  210. 'hkern',
  211. 'image',
  212. 'line',
  213. 'linearGradient',
  214. 'marker',
  215. 'mask',
  216. 'metadata',
  217. 'missing-glyph',
  218. 'mpath',
  219. 'path',
  220. 'pattern',
  221. 'polygon',
  222. 'polyline',
  223. 'radialGradient',
  224. 'rect',
  225. 'script',
  226. 'set',
  227. 'stop',
  228. 'style',
  229. 'svg',
  230. 'switch',
  231. 'symbol',
  232. 'text',
  233. 'textPath',
  234. 'title',
  235. 'tref',
  236. 'tspan',
  237. 'use',
  238. 'view',
  239. 'vkern',
  240. );
  241. public function testIsHtml5Element()
  242. {
  243. foreach ($this->html5Elements as $element) {
  244. $this->assertTrue(Elements::isHtml5Element($element), 'html5 element test failed on: ' . $element);
  245. $this->assertTrue(Elements::isHtml5Element(strtoupper($element)), 'html5 element test failed on: ' . strtoupper($element));
  246. }
  247. $nonhtml5 = array(
  248. 'foo',
  249. 'bar',
  250. 'baz',
  251. );
  252. foreach ($nonhtml5 as $element) {
  253. $this->assertFalse(Elements::isHtml5Element($element), 'html5 element test failed on: ' . $element);
  254. $this->assertFalse(Elements::isHtml5Element(strtoupper($element)), 'html5 element test failed on: ' . strtoupper($element));
  255. }
  256. }
  257. public function testIsMathMLElement()
  258. {
  259. foreach ($this->mathmlElements as $element) {
  260. $this->assertTrue(Elements::isMathMLElement($element), 'MathML element test failed on: ' . $element);
  261. // MathML is case sensitive so these should all fail.
  262. $this->assertFalse(Elements::isMathMLElement(strtoupper($element)), 'MathML element test failed on: ' . strtoupper($element));
  263. }
  264. $nonMathML = array(
  265. 'foo',
  266. 'bar',
  267. 'baz',
  268. );
  269. foreach ($nonMathML as $element) {
  270. $this->assertFalse(Elements::isMathMLElement($element), 'MathML element test failed on: ' . $element);
  271. }
  272. }
  273. public function testIsSvgElement()
  274. {
  275. foreach ($this->svgElements as $element) {
  276. $this->assertTrue(Elements::isSvgElement($element), 'SVG element test failed on: ' . $element);
  277. // SVG is case sensitive so these should all fail.
  278. $this->assertFalse(Elements::isSvgElement(strtoupper($element)), 'SVG element test failed on: ' . strtoupper($element));
  279. }
  280. $nonSVG = array(
  281. 'foo',
  282. 'bar',
  283. 'baz',
  284. );
  285. foreach ($nonSVG as $element) {
  286. $this->assertFalse(Elements::isSvgElement($element), 'SVG element test failed on: ' . $element);
  287. }
  288. }
  289. public function testIsElement()
  290. {
  291. foreach ($this->html5Elements as $element) {
  292. $this->assertTrue(Elements::isElement($element), 'html5 element test failed on: ' . $element);
  293. $this->assertTrue(Elements::isElement(strtoupper($element)), 'html5 element test failed on: ' . strtoupper($element));
  294. }
  295. foreach ($this->mathmlElements as $element) {
  296. $this->assertTrue(Elements::isElement($element), 'MathML element test failed on: ' . $element);
  297. // MathML is case sensitive so these should all fail.
  298. $this->assertFalse(Elements::isElement(strtoupper($element)), 'MathML element test failed on: ' . strtoupper($element));
  299. }
  300. foreach ($this->svgElements as $element) {
  301. $this->assertTrue(Elements::isElement($element), 'SVG element test failed on: ' . $element);
  302. // SVG is case sensitive so these should all fail. But, there is duplication
  303. // html5 and SVG. Since html5 is case insensitive we need to make sure
  304. // it's not a html5 element first.
  305. if (!in_array($element, $this->html5Elements)) {
  306. $this->assertFalse(Elements::isElement(strtoupper($element)), 'SVG element test failed on: ' . strtoupper($element));
  307. }
  308. }
  309. $nonhtml5 = array(
  310. 'foo',
  311. 'bar',
  312. 'baz',
  313. );
  314. foreach ($nonhtml5 as $element) {
  315. $this->assertFalse(Elements::isElement($element), 'html5 element test failed on: ' . $element);
  316. $this->assertFalse(Elements::isElement(strtoupper($element)), 'html5 element test failed on: ' . strtoupper($element));
  317. }
  318. }
  319. public function testElement()
  320. {
  321. foreach ($this->html5Elements as $element) {
  322. $this->assertGreaterThan(0, Elements::element($element));
  323. }
  324. $nonhtml5 = array(
  325. 'foo',
  326. 'bar',
  327. 'baz',
  328. );
  329. foreach ($nonhtml5 as $element) {
  330. $this->assertEquals(0, Elements::element($element));
  331. }
  332. }
  333. public function testIsA()
  334. {
  335. $this->assertTrue(Elements::isA('script', Elements::KNOWN_ELEMENT));
  336. $this->assertFalse(Elements::isA('scriptypoo', Elements::KNOWN_ELEMENT));
  337. $this->assertTrue(Elements::isA('script', Elements::TEXT_RAW));
  338. $this->assertFalse(Elements::isA('script', Elements::TEXT_RCDATA));
  339. $voidElements = array(
  340. 'area',
  341. 'base',
  342. 'basefont',
  343. 'bgsound',
  344. 'br',
  345. 'col',
  346. 'command',
  347. 'embed',
  348. 'frame',
  349. 'hr',
  350. 'img',
  351. );
  352. foreach ($voidElements as $element) {
  353. $this->assertTrue(Elements::isA($element, Elements::VOID_TAG), 'Void element test failed on: ' . $element);
  354. }
  355. $nonVoid = array(
  356. 'span',
  357. 'a',
  358. 'div',
  359. );
  360. foreach ($nonVoid as $tag) {
  361. $this->assertFalse(Elements::isA($tag, Elements::VOID_TAG), 'Void element test failed on: ' . $tag);
  362. }
  363. $blockTags = array(
  364. 'address',
  365. 'article',
  366. 'aside',
  367. 'blockquote',
  368. 'canvas',
  369. 'dd',
  370. 'div',
  371. 'dl',
  372. 'fieldset',
  373. 'figcaption',
  374. 'figure',
  375. 'footer',
  376. 'form',
  377. 'h1',
  378. 'h2',
  379. 'h3',
  380. 'h4',
  381. 'h5',
  382. 'h6',
  383. 'header',
  384. 'hgroup',
  385. 'hr',
  386. 'noscript',
  387. 'ol',
  388. 'output',
  389. 'p',
  390. 'pre',
  391. 'section',
  392. 'table',
  393. 'tfoot',
  394. 'ul',
  395. 'video',
  396. );
  397. foreach ($blockTags as $tag) {
  398. $this->assertTrue(Elements::isA($tag, Elements::BLOCK_TAG), 'Block tag test failed on: ' . $tag);
  399. }
  400. $nonBlockTags = array(
  401. 'span',
  402. 'img',
  403. 'label',
  404. );
  405. foreach ($nonBlockTags as $tag) {
  406. $this->assertFalse(Elements::isA($tag, Elements::BLOCK_TAG), 'Block tag test failed on: ' . $tag);
  407. }
  408. }
  409. public function testNormalizeSvgElement()
  410. {
  411. $tests = array(
  412. 'foo' => 'foo',
  413. 'altglyph' => 'altGlyph',
  414. 'BAR' => 'bar',
  415. 'fespecularlighting' => 'feSpecularLighting',
  416. 'bAz' => 'baz',
  417. 'foreignobject' => 'foreignObject',
  418. );
  419. foreach ($tests as $input => $expected) {
  420. $this->assertEquals($expected, Elements::normalizeSvgElement($input));
  421. }
  422. }
  423. public function testNormalizeSvgAttribute()
  424. {
  425. $tests = array(
  426. 'foo' => 'foo',
  427. 'attributename' => 'attributeName',
  428. 'BAR' => 'bar',
  429. 'limitingconeangle' => 'limitingConeAngle',
  430. 'bAz' => 'baz',
  431. 'patterncontentunits' => 'patternContentUnits',
  432. );
  433. foreach ($tests as $input => $expected) {
  434. $this->assertEquals($expected, Elements::normalizeSvgAttribute($input));
  435. }
  436. }
  437. public function testNormalizeMathMlAttribute()
  438. {
  439. $tests = array(
  440. 'foo' => 'foo',
  441. 'definitionurl' => 'definitionURL',
  442. 'BAR' => 'bar',
  443. );
  444. foreach ($tests as $input => $expected) {
  445. $this->assertEquals($expected, Elements::normalizeMathMlAttribute($input));
  446. }
  447. }
  448. }