html5-schema.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore'
  7. ], function (_) {
  8. 'use strict';
  9. /* eslint-disable max-len */
  10. var schema = {
  11. blockContent: [
  12. 'address', 'article', 'aside', 'blockquote', 'details', 'dialog', 'div', 'dl', 'fieldset',
  13. 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr',
  14. 'menu', 'nav', 'ol', 'p', 'pre', 'section', 'table', 'ul'
  15. ],
  16. phrasingContent: [
  17. '#comment', '#text', 'a', 'abbr', 'audio', 'b', 'bdi', 'bdo', 'br', 'button', 'canvas',
  18. 'cite','code', 'command', 'datalist', 'del', 'dfn', 'em', 'embed', 'i', 'iframe', 'img',
  19. 'input', 'ins', 'kbd', 'keygen', 'label', 'map', 'mark', 'meter', 'noscript', 'object',
  20. 'output', 'picture', 'progress', 'q', 'ruby', 's', 'samp', 'script', 'select', 'small',
  21. 'span', 'strong', 'sub', 'sup', 'textarea', 'time', 'u', 'var', 'video', 'wbr'
  22. ],
  23. blockElements: [
  24. 'address', 'article', 'aside', 'blockquote', 'caption', 'center', 'datalist', 'dd', 'dir', 'div',
  25. 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
  26. 'header', 'hgroup', 'hr', 'isindex', 'li', 'menu', 'nav', 'noscript', 'ol', 'optgroup', 'option',
  27. 'p', 'pre', 'section', 'select', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul'
  28. ],
  29. boolAttrs: [
  30. 'autoplay', 'checked', 'compact', 'controls', 'declare', 'defer', 'disabled', 'ismap', 'loop',
  31. 'multiple', 'nohref', 'noresize', 'noshade', 'nowrap', 'readonly', 'selected'
  32. ],
  33. shortEnded: [
  34. 'area', 'base', 'basefont', 'br', 'col', 'embed', 'frame', 'hr', 'img', 'input', 'isindex',
  35. 'link', 'meta', 'param', 'source', 'track', 'wbr'
  36. ],
  37. whiteSpace: [
  38. 'audio', 'iframe', 'noscript', 'object', 'pre', 'script', 'style', 'textarea', 'video'
  39. ],
  40. selfClosing: [
  41. 'colgroup', 'dd', 'dt', 'li', 'option', 'p', 'td', 'tfoot', 'th', 'thead', 'tr'
  42. ]
  43. };
  44. schema.flowContent = schema.blockContent.concat(schema.phrasingContent, ['style']);
  45. schema.nonEmpty = ['td', 'th', 'iframe', 'video', 'audio', 'object', 'script', 'i', 'em', 'span'].concat(schema.shortEnded);
  46. _.extend(schema, (function (phrasingContent, flowContent) {
  47. var validElements = [],
  48. validChildren = [],
  49. compiled = {},
  50. globalAttrs,
  51. rawData;
  52. globalAttrs = [
  53. 'id', 'dir', 'lang', 'class', 'style', 'title', 'hidden', 'onclick', 'onkeyup',
  54. 'tabindex', 'dropzone', 'accesskey', 'draggable', 'translate', 'onmouseup',
  55. 'onkeydown', 'spellcheck', 'ondblclick', 'onmouseout', 'onkeypress', 'contextmenu',
  56. 'onmousedown', 'onmouseover', 'onmousemove', 'contenteditable'
  57. ];
  58. rawData = [
  59. ['html', 'manifest', 'head body'],
  60. ['head', '', 'base command link meta noscript script style title'],
  61. ['title hr noscript br'],
  62. ['base', 'href target'],
  63. ['link', 'href rel media hreflang type sizes hreflang'],
  64. ['meta', 'name http-equiv content charset'],
  65. ['style', 'media type scoped'],
  66. ['script', 'src async defer type charset'],
  67. ['body', 'onafterprint onbeforeprint onbeforeunload onblur onerror onfocus ' +
  68. 'onhashchange onload onmessage onoffline ononline onpagehide onpageshow ' +
  69. 'onpopstate onresize onscroll onstorage onunload background bgcolor text link vlink alink', flowContent
  70. ],
  71. ['caption', '', _.without(flowContent, 'table')],
  72. ['address dt dd div', '', flowContent],
  73. ['h1 h2 h3 h4 h5 h6 pre p abbr code var samp kbd sub sup i b u bdo span legend em strong small s cite dfn', '', phrasingContent],
  74. ['blockquote', 'cite', flowContent],
  75. ['ol', 'reversed start type', 'li'],
  76. ['ul', 'type compact', 'li'],
  77. ['li', 'value type', flowContent],
  78. ['dl', '', 'dt dd'],
  79. ['a', 'href target rel media hreflang type charset name rev shape coords download', phrasingContent],
  80. ['q', 'cite', phrasingContent],
  81. ['ins del', 'cite datetime', flowContent],
  82. ['img', 'src sizes srcset alt usemap ismap width height name longdesc align border hspace vspace'],
  83. ['iframe', 'src name width height longdesc frameborder marginwidth marginheight scrolling align sandbox seamless allowfullscreen', flowContent],
  84. ['embed', 'src type width height'],
  85. ['object', 'data type typemustmatch name usemap form width height declare classid code codebase codetype archive standby align border hspace vspace', flowContent.concat(['param'])],
  86. ['param', 'name value valuetype type'],
  87. ['map', 'name', flowContent.concat(['area'])],
  88. ['area', 'alt coords shape href target rel media hreflang type nohref'],
  89. ['table', 'border summary width frame rules cellspacing cellpadding align bgcolor', 'caption colgroup thead tfoot tbody tr col'],
  90. ['colgroup', 'span width align char charoff valign', 'col'],
  91. ['col', 'span'],
  92. ['tbody thead tfoot', 'align char charoff valign', 'tr'],
  93. ['tr', 'align char charoff valign bgcolor', 'td th'],
  94. ['td', 'colspan rowspan headers abbr axis scope align char charoff valign nowrap bgcolor width height', flowContent],
  95. ['th', 'colspan rowspan headers scope abbr axis align char charoff valign nowrap bgcolor width height accept', flowContent],
  96. ['form', 'accept-charset action autocomplete enctype method name novalidate target onsubmit onreset', flowContent],
  97. ['fieldset', 'disabled form name', flowContent.concat(['legend'])],
  98. ['label', 'form for', phrasingContent],
  99. ['input', 'accept alt autocomplete checked dirname disabled form formaction formenctype formmethod formnovalidate ' +
  100. 'formtarget height list max maxlength min multiple name pattern readonly required size src step type value width usemap align'
  101. ],
  102. ['button', 'disabled form formaction formenctype formmethod formnovalidate formtarget name type value', phrasingContent],
  103. ['select', 'disabled form multiple name required size onfocus onblur onchange', 'option optgroup'],
  104. ['optgroup', 'disabled label', 'option'],
  105. ['option', 'disabled label selected value'],
  106. ['textarea', 'cols dirname disabled form maxlength name readonly required rows wrap'],
  107. ['menu', 'type label', flowContent.concat(['li'])],
  108. ['noscript', '', flowContent],
  109. ['wbr'],
  110. ['ruby', '', phrasingContent.concat(['rt', 'rp'])],
  111. ['figcaption', '', flowContent],
  112. ['mark rt rp summary bdi', '', phrasingContent],
  113. ['canvas', 'width height', flowContent],
  114. ['video', 'src crossorigin poster preload autoplay mediagroup loop muted controls width height buffered', flowContent.concat(['track', 'source'])],
  115. ['audio', 'src crossorigin preload autoplay mediagroup loop muted controls buffered volume', flowContent.concat(['track', 'source'])],
  116. ['picture', '', 'img source'],
  117. ['source', 'src srcset type media sizes'],
  118. ['track', 'kind src srclang label default'],
  119. ['datalist', '', phrasingContent.concat(['option'])],
  120. ['article section nav aside header footer', '', flowContent],
  121. ['hgroup', '', 'h1 h2 h3 h4 h5 h6'],
  122. ['figure', '', flowContent.concat(['figcaption'])],
  123. ['time', 'datetime', phrasingContent],
  124. ['dialog', 'open', flowContent],
  125. ['command', 'type label icon disabled checked radiogroup command'],
  126. ['output', 'for form name', phrasingContent],
  127. ['progress', 'value max', phrasingContent],
  128. ['meter', 'value min max low high optimum', phrasingContent],
  129. ['details', 'open', flowContent.concat(['summary'])],
  130. ['keygen', 'autofocus challenge disabled form keytype name'],
  131. ['script', 'language xml:space'],
  132. ['style', 'xml:space'],
  133. ['embed', 'align name hspace vspace'],
  134. ['br', 'clear'],
  135. ['applet', 'codebase archive code object alt name width height align hspace vspace'],
  136. ['font basefont', 'size color face'],
  137. ['h1 h2 h3 h4 h5 h6 div p legend caption', 'align'],
  138. ['ol dl menu dir', 'compact'],
  139. ['pre', 'width xml:space'],
  140. ['hr', 'align noshade size width'],
  141. ['isindex', 'prompt'],
  142. ['col', 'width align char charoff valign'],
  143. ['input button select textarea', 'autofocus'],
  144. ['input textarea', 'placeholder onselect onchange onfocus onblur'],
  145. ['link script img', 'crossorigin']
  146. ];
  147. rawData.forEach(function (data) {
  148. var nodes = data[0].split(' '),
  149. attributes = data[1] || [],
  150. children = data[2] || [],
  151. ni = nodes.length,
  152. nodeName,
  153. schemaData;
  154. if (typeof attributes === 'string') {
  155. attributes = attributes.split(' ');
  156. }
  157. if (typeof children === 'string') {
  158. children = children.split(' ');
  159. }
  160. while (ni--) {
  161. nodeName = nodes[ni];
  162. schemaData = compiled[nodeName] || {};
  163. compiled[nodeName] = {
  164. attributes: _.union(schemaData.attributes, globalAttrs, attributes),
  165. children: _.union(schemaData.children, children)
  166. };
  167. }
  168. });
  169. ['a', 'dfn', 'form', 'meter', 'progress'].forEach(function (nodeName) {
  170. var node = compiled[nodeName];
  171. node.children = _.without(node.children, nodeName);
  172. });
  173. _.each(compiled, function (node, nodeName) {
  174. var filteredAttributes = [];
  175. _.each(node.attributes, function (attribute) { //eslint-disable-line max-nested-callbacks
  176. // Disallowing usage of 'on*' attributes.
  177. if (!/^on/.test(attribute)) {
  178. filteredAttributes.push(attribute);
  179. }
  180. });
  181. node.attributes = filteredAttributes;
  182. validElements.push(nodeName + '[' + node.attributes.join('|') + ']');
  183. validChildren.push(nodeName + '[' + node.children.join('|') + ']');
  184. });
  185. return {
  186. nodes: compiled,
  187. validElements: validElements,
  188. validChildren: validChildren
  189. };
  190. })(schema.phrasingContent, schema.flowContent));
  191. return schema;
  192. });