| 1 | /** |
| 2 | * Schema.js |
| 3 | * |
| 4 | * Copyright, Moxiecode Systems AB |
| 5 | * Released under LGPL License. |
| 6 | * |
| 7 | * License: http://www.tinymce.com/license |
| 8 | * Contributing: http://www.tinymce.com/contributing |
| 9 | */ |
| 10 | |
| 11 | (function(tinymce) { |
| 12 | var mapCache = {}, makeMap = tinymce.makeMap, each = tinymce.each; |
| 13 | |
| 14 | function split(str, delim) { |
| 15 | return str.split(delim || ','); |
| 16 | }; |
| 17 | |
| 18 | /** |
| 19 | * Unpacks the specified lookup and string data it will also parse it into an object |
| 20 | * map with sub object for it's children. This will later also include the attributes. |
| 21 | */ |
| 22 | function unpack(lookup, data) { |
| 23 | var key, elements = {}; |
| 24 | |
| 25 | function replace(value) { |
| 26 | return value.replace(/[A-Z]+/g, function(key) { |
| 27 | return replace(lookup[key]); |
| 28 | }); |
| 29 | }; |
| 30 | |
| 31 | // Unpack lookup |
| 32 | for (key in lookup) { |
| 33 | if (lookup.hasOwnProperty(key)) |
| 34 | lookup[key] = replace(lookup[key]); |
| 35 | } |
| 36 | |
| 37 | // Unpack and parse data into object map |
| 38 | replace(data).replace(/#/g, '#text').replace(/(\w+)\[([^\]]+)\]\[([^\]]*)\]/g, function(str, name, attributes, children) { |
| 39 | attributes = split(attributes, '|'); |
| 40 | |
| 41 | elements[name] = { |
| 42 | attributes : makeMap(attributes), |
| 43 | attributesOrder : attributes, |
| 44 | children : makeMap(children, '|', {'#comment' : {}}) |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | return elements; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Returns the HTML5 schema and caches it in the mapCache. |
| 53 | */ |
| 54 | function getHTML5() { |
| 55 | var html5 = mapCache.html5; |
| 56 | |
| 57 | if (!html5) { |
| 58 | html5 = mapCache.html5 = unpack({ |
| 59 | A : 'id|accesskey|class|dir|draggable|item|hidden|itemprop|role|spellcheck|style|subject|title|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup', |
| 60 | B : '#|a|abbr|area|audio|b|bdo|br|button|canvas|cite|code|command|datalist|del|dfn|em|embed|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|meta|' + |
| 61 | 'meter|noscript|object|output|progress|q|ruby|samp|script|select|small|span|strong|sub|sup|svg|textarea|time|var|video|wbr', |
| 62 | C : '#|a|abbr|area|address|article|aside|audio|b|bdo|blockquote|br|button|canvas|cite|code|command|datalist|del|details|dfn|dialog|div|dl|em|embed|fieldset|' + |
| 63 | 'figure|footer|form|h1|h2|h3|h4|h5|h6|header|hgroup|hr|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|menu|meta|meter|nav|noscript|ol|object|output|' + |
| 64 | 'p|pre|progress|q|ruby|samp|script|section|select|small|span|strong|style|sub|sup|svg|table|textarea|time|ul|var|video' |
| 65 | }, 'html[A|manifest][body|head]' + |
| 66 | 'head[A][base|command|link|meta|noscript|script|style|title]' + |
| 67 | 'title[A][#]' + |
| 68 | 'base[A|href|target][]' + |
| 69 | 'link[A|href|rel|media|type|sizes][]' + |
| 70 | 'meta[A|http-equiv|name|content|charset][]' + |
| 71 | 'style[A|type|media|scoped][#]' + |
| 72 | 'script[A|charset|type|src|defer|async][#]' + |
| 73 | 'noscript[A][C]' + |
| 74 | 'body[A][C]' + |
| 75 | 'section[A][C]' + |
| 76 | 'nav[A][C]' + |
| 77 | 'article[A][C]' + |
| 78 | 'aside[A][C]' + |
| 79 | 'h1[A][B]' + |
| 80 | 'h2[A][B]' + |
| 81 | 'h3[A][B]' + |
| 82 | 'h4[A][B]' + |
| 83 | 'h5[A][B]' + |
| 84 | 'h6[A][B]' + |
| 85 | 'hgroup[A][h1|h2|h3|h4|h5|h6]' + |
| 86 | 'header[A][C]' + |
| 87 | 'footer[A][C]' + |
| 88 | 'address[A][C]' + |
| 89 | 'p[A][B]' + |
| 90 | 'br[A][]' + |
| 91 | 'pre[A][B]' + |
| 92 | 'dialog[A][dd|dt]' + |
| 93 | 'blockquote[A|cite][C]' + |
| 94 | 'ol[A|start|reversed][li]' + |
| 95 | 'ul[A][li]' + |
| 96 | 'li[A|value][C]' + |
| 97 | 'dl[A][dd|dt]' + |
| 98 | 'dt[A][B]' + |
| 99 | 'dd[A][C]' + |
| 100 | 'a[A|href|target|ping|rel|media|type][B]' + |
| 101 | 'em[A][B]' + |
| 102 | 'strong[A][B]' + |
| 103 | 'small[A][B]' + |
| 104 | 'cite[A][B]' + |
| 105 | 'q[A|cite][B]' + |
| 106 | 'dfn[A][B]' + |
| 107 | 'abbr[A][B]' + |
| 108 | 'code[A][B]' + |
| 109 | 'var[A][B]' + |
| 110 | 'samp[A][B]' + |
| 111 | 'kbd[A][B]' + |
| 112 | 'sub[A][B]' + |
| 113 | 'sup[A][B]' + |
| 114 | 'i[A][B]' + |
| 115 | 'b[A][B]' + |
| 116 | 'mark[A][B]' + |
| 117 | 'progress[A|value|max][B]' + |
| 118 | 'meter[A|value|min|max|low|high|optimum][B]' + |
| 119 | 'time[A|datetime][B]' + |
| 120 | 'ruby[A][B|rt|rp]' + |
| 121 | 'rt[A][B]' + |
| 122 | 'rp[A][B]' + |
| 123 | 'bdo[A][B]' + |
| 124 | 'span[A][B]' + |
| 125 | 'ins[A|cite|datetime][B]' + |
| 126 | 'del[A|cite|datetime][B]' + |
| 127 | 'figure[A][C|legend|figcaption]' + |
| 128 | 'figcaption[A][C]' + |
| 129 | 'img[A|alt|src|height|width|usemap|ismap][]' + |
| 130 | 'iframe[A|name|src|height|width|sandbox|seamless][]' + |
| 131 | 'embed[A|src|height|width|type][]' + |
| 132 | 'object[A|data|type|height|width|usemap|name|form|classid][param]' + |
| 133 | 'param[A|name|value][]' + |
| 134 | 'details[A|open][C|legend]' + |
| 135 | 'command[A|type|label|icon|disabled|checked|radiogroup][]' + |
| 136 | 'menu[A|type|label][C|li]' + |
| 137 | 'legend[A][C|B]' + |
| 138 | 'div[A][C]' + |
| 139 | 'source[A|src|type|media][]' + |
| 140 | 'audio[A|src|autobuffer|autoplay|loop|controls][source]' + |
| 141 | 'video[A|src|autobuffer|autoplay|loop|controls|width|height|poster][source]' + |
| 142 | 'hr[A][]' + |
| 143 | 'form[A|accept-charset|action|autocomplete|enctype|method|name|novalidate|target][C]' + |
| 144 | 'fieldset[A|disabled|form|name][C|legend]' + |
| 145 | 'label[A|form|for][B]' + |
| 146 | 'input[A|type|accept|alt|autocomplete|autofocus|checked|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|height|list|max|maxlength|min|' + |
| 147 | 'multiple|pattern|placeholder|readonly|required|size|src|step|width|files|value|name][]' + |
| 148 | 'button[A|autofocus|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|name|value|type][B]' + |
| 149 | 'select[A|autofocus|disabled|form|multiple|name|size][option|optgroup]' + |
| 150 | 'datalist[A][B|option]' + |
| 151 | 'optgroup[A|disabled|label][option]' + |
| 152 | 'option[A|disabled|selected|label|value][]' + |
| 153 | 'textarea[A|autofocus|disabled|form|maxlength|name|placeholder|readonly|required|rows|cols|wrap][]' + |
| 154 | 'keygen[A|autofocus|challenge|disabled|form|keytype|name][]' + |
| 155 | 'output[A|for|form|name][B]' + |
| 156 | 'canvas[A|width|height][]' + |
| 157 | 'map[A|name][B|C]' + |
| 158 | 'area[A|shape|coords|href|alt|target|media|rel|ping|type][]' + |
| 159 | 'mathml[A][]' + |
| 160 | 'svg[A][]' + |
| 161 | 'table[A|border][caption|colgroup|thead|tfoot|tbody|tr]' + |
| 162 | 'caption[A][C]' + |
| 163 | 'colgroup[A|span][col]' + |
| 164 | 'col[A|span][]' + |
| 165 | 'thead[A][tr]' + |
| 166 | 'tfoot[A][tr]' + |
| 167 | 'tbody[A][tr]' + |
| 168 | 'tr[A][th|td]' + |
| 169 | 'th[A|headers|rowspan|colspan|scope][B]' + |
| 170 | 'td[A|headers|rowspan|colspan][C]' + |
| 171 | 'wbr[A][]' |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | return html5; |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * Returns the HTML4 schema and caches it in the mapCache. |
| 180 | */ |
| 181 | function getHTML4() { |
| 182 | var html4 = mapCache.html4; |
| 183 | |
| 184 | if (!html4) { |
| 185 | // This is the XHTML 1.0 transitional elements with it's attributes and children packed to reduce it's size |
| 186 | html4 = mapCache.html4 = unpack({ |
| 187 | Z : 'H|K|N|O|P', |
| 188 | Y : 'X|form|R|Q', |
| 189 | ZG : 'E|span|width|align|char|charoff|valign', |
| 190 | X : 'p|T|div|U|W|isindex|fieldset|table', |
| 191 | ZF : 'E|align|char|charoff|valign', |
| 192 | W : 'pre|hr|blockquote|address|center|noframes', |
| 193 | ZE : 'abbr|axis|headers|scope|rowspan|colspan|align|char|charoff|valign|nowrap|bgcolor|width|height', |
| 194 | ZD : '[E][S]', |
| 195 | U : 'ul|ol|dl|menu|dir', |
| 196 | ZC : 'p|Y|div|U|W|table|br|span|bdo|object|applet|img|map|K|N|Q', |
| 197 | T : 'h1|h2|h3|h4|h5|h6', |
| 198 | ZB : 'X|S|Q', |
| 199 | S : 'R|P', |
| 200 | ZA : 'a|G|J|M|O|P', |
| 201 | R : 'a|H|K|N|O', |
| 202 | Q : 'noscript|P', |
| 203 | P : 'ins|del|script', |
| 204 | O : 'input|select|textarea|label|button', |
| 205 | N : 'M|L', |
| 206 | M : 'em|strong|dfn|code|q|samp|kbd|var|cite|abbr|acronym', |
| 207 | L : 'sub|sup', |
| 208 | K : 'J|I', |
| 209 | J : 'tt|i|b|u|s|strike', |
| 210 | I : 'big|small|font|basefont', |
| 211 | H : 'G|F', |
| 212 | G : 'br|span|bdo', |
| 213 | F : 'object|applet|img|map|iframe', |
| 214 | E : 'A|B|C', |
| 215 | D : 'accesskey|tabindex|onfocus|onblur', |
| 216 | C : 'onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup', |
| 217 | B : 'lang|xml:lang|dir', |
| 218 | A : 'id|class|style|title' |
| 219 | }, 'script[id|charset|type|language|src|defer|xml:space][]' + |
| 220 | 'style[B|id|type|media|title|xml:space][]' + |
| 221 | 'object[E|declare|classid|codebase|data|type|codetype|archive|standby|width|height|usemap|name|tabindex|align|border|hspace|vspace][#|param|Y]' + |
| 222 | 'param[id|name|value|valuetype|type][]' + |
| 223 | 'p[E|align][#|S]' + |
| 224 | 'a[E|D|charset|type|name|href|hreflang|rel|rev|shape|coords|target][#|Z]' + |
| 225 | 'br[A|clear][]' + |
| 226 | 'span[E][#|S]' + |
| 227 | 'bdo[A|C|B][#|S]' + |
| 228 | 'applet[A|codebase|archive|code|object|alt|name|width|height|align|hspace|vspace][#|param|Y]' + |
| 229 | 'h1[E|align][#|S]' + |
| 230 | 'img[E|src|alt|name|longdesc|width|height|usemap|ismap|align|border|hspace|vspace][]' + |
| 231 | 'map[B|C|A|name][X|form|Q|area]' + |
| 232 | 'h2[E|align][#|S]' + |
| 233 | 'iframe[A|longdesc|name|src|frameborder|marginwidth|marginheight|scrolling|align|width|height][#|Y]' + |
| 234 | 'h3[E|align][#|S]' + |
| 235 | 'tt[E][#|S]' + |
| 236 | 'i[E][#|S]' + |
| 237 | 'b[E][#|S]' + |
| 238 | 'u[E][#|S]' + |
| 239 | 's[E][#|S]' + |
| 240 | 'strike[E][#|S]' + |
| 241 | 'big[E][#|S]' + |
| 242 | 'small[E][#|S]' + |
| 243 | 'font[A|B|size|color|face][#|S]' + |
| 244 | 'basefont[id|size|color|face][]' + |
| 245 | 'em[E][#|S]' + |
| 246 | 'strong[E][#|S]' + |
| 247 | 'dfn[E][#|S]' + |
| 248 | 'code[E][#|S]' + |
| 249 | 'q[E|cite][#|S]' + |
| 250 | 'samp[E][#|S]' + |
| 251 | 'kbd[E][#|S]' + |
| 252 | 'var[E][#|S]' + |
| 253 | 'cite[E][#|S]' + |
| 254 | 'abbr[E][#|S]' + |
| 255 | 'acronym[E][#|S]' + |
| 256 | 'sub[E][#|S]' + |
| 257 | 'sup[E][#|S]' + |
| 258 | 'input[E|D|type|name|value|checked|disabled|readonly|size|maxlength|src|alt|usemap|onselect|onchange|accept|align][]' + |
| 259 | 'select[E|name|size|multiple|disabled|tabindex|onfocus|onblur|onchange][optgroup|option]' + |
| 260 | 'optgroup[E|disabled|label][option]' + |
| 261 | 'option[E|selected|disabled|label|value][]' + |
| 262 | 'textarea[E|D|name|rows|cols|disabled|readonly|onselect|onchange][]' + |
| 263 | 'label[E|for|accesskey|onfocus|onblur][#|S]' + |
| 264 | 'button[E|D|name|value|type|disabled][#|p|T|div|U|W|table|G|object|applet|img|map|K|N|Q]' + |
| 265 | 'h4[E|align][#|S]' + |
| 266 | 'ins[E|cite|datetime][#|Y]' + |
| 267 | 'h5[E|align][#|S]' + |
| 268 | 'del[E|cite|datetime][#|Y]' + |
| 269 | 'h6[E|align][#|S]' + |
| 270 | 'div[E|align][#|Y]' + |
| 271 | 'ul[E|type|compact][li]' + |
| 272 | 'li[E|type|value][#|Y]' + |
| 273 | 'ol[E|type|compact|start][li]' + |
| 274 | 'dl[E|compact][dt|dd]' + |
| 275 | 'dt[E][#|S]' + |
| 276 | 'dd[E][#|Y]' + |
| 277 | 'menu[E|compact][li]' + |
| 278 | 'dir[E|compact][li]' + |
| 279 | 'pre[E|width|xml:space][#|ZA]' + |
| 280 | 'hr[E|align|noshade|size|width][]' + |
| 281 | 'blockquote[E|cite][#|Y]' + |
| 282 | 'address[E][#|S|p]' + |
| 283 | 'center[E][#|Y]' + |
| 284 | 'noframes[E][#|Y]' + |
| 285 | 'isindex[A|B|prompt][]' + |
| 286 | 'fieldset[E][#|legend|Y]' + |
| 287 | 'legend[E|accesskey|align][#|S]' + |
| 288 | 'table[E|summary|width|border|frame|rules|cellspacing|cellpadding|align|bgcolor][caption|col|colgroup|thead|tfoot|tbody|tr]' + |
| 289 | 'caption[E|align][#|S]' + |
| 290 | 'col[ZG][]' + |
| 291 | 'colgroup[ZG][col]' + |
| 292 | 'thead[ZF][tr]' + |
| 293 | 'tr[ZF|bgcolor][th|td]' + |
| 294 | 'th[E|ZE][#|Y]' + |
| 295 | 'form[E|action|method|name|enctype|onsubmit|onreset|accept|accept-charset|target][#|X|R|Q]' + |
| 296 | 'noscript[E][#|Y]' + |
| 297 | 'td[E|ZE][#|Y]' + |
| 298 | 'tfoot[ZF][tr]' + |
| 299 | 'tbody[ZF][tr]' + |
| 300 | 'area[E|D|shape|coords|href|nohref|alt|target][]' + |
| 301 | 'base[id|href|target][]' + |
| 302 | 'body[E|onload|onunload|background|bgcolor|text|link|vlink|alink][#|Y]' |
| 303 | ); |
| 304 | } |
| 305 | |
| 306 | return html4; |
| 307 | }; |
| 308 | |
| 309 | /** |
| 310 | * WordPress Core |
| 311 | * |
| 312 | * Returns a schema that is a superset of TinyMCE's HTML5 and HTML4 schemas. |
| 313 | */ |
| 314 | function getSaneSchema() { |
| 315 | var sane = mapCache.sane; |
| 316 | |
| 317 | if ( ! sane ) { |
| 318 | sane = mapCache.sane = unpack({ |
| 319 | A : 'id|accesskey|class|dir|draggable|item|hidden|itemprop|role|spellcheck|style|subject|title|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup|lang|xml:lang|dir', |
| 320 | B : '#|a|abbr|area|audio|b|bdo|br|button|canvas|cite|code|command|datalist|del|dfn|em|embed|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|meta|' + |
| 321 | 'meter|noscript|object|output|progress|q|ruby|samp|script|select|small|span|strong|sub|sup|svg|textarea|time|var|video|wbr|acronym|basefont|font|big|strike|s|u|tt|applet', |
| 322 | C : '#|a|abbr|area|address|article|aside|audio|b|bdo|blockquote|br|button|canvas|cite|code|command|datalist|del|details|dfn|dialog|div|dl|em|embed|fieldset|' + |
| 323 | 'figure|footer|form|h1|h2|h3|h4|h5|h6|header|hgroup|hr|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|menu|meta|meter|nav|noscript|ol|object|output|' + |
| 324 | 'p|pre|progress|q|ruby|samp|script|section|select|small|span|strong|style|sub|sup|svg|table|textarea|time|ul|var|video|acronym|basefont|font|big|strike|s|u|tt|applet|isindex|noframes|center|dir|wbr' |
| 325 | }, 'html[A|manifest][body|head]' + |
| 326 | 'head[A][base|command|link|meta|noscript|script|style|title]' + |
| 327 | 'title[A][#]' + |
| 328 | 'base[A|href|target][]' + |
| 329 | 'link[A|href|rel|media|type|sizes][]' + |
| 330 | 'meta[A|http-equiv|name|content|charset][]' + |
| 331 | 'style[A|type|media|scoped|xml:space][#]' + |
| 332 | 'script[A|charset|type|src|defer|async|language|xml:space][#]' + |
| 333 | 'noscript[A][C]' + |
| 334 | 'body[A|onload|onunload|background|bgcolor|text|link|vlink|alink][C]' + |
| 335 | 'section[A][C]' + |
| 336 | 'nav[A][C]' + |
| 337 | 'article[A][C]' + |
| 338 | 'aside[A][C]' + |
| 339 | 'h1[A|align][B]' + |
| 340 | 'h2[A|align][B]' + |
| 341 | 'h3[A|align][B]' + |
| 342 | 'h4[A|align][B]' + |
| 343 | 'h5[A|align][B]' + |
| 344 | 'h6[A|align][B]' + |
| 345 | 'hgroup[A][h1|h2|h3|h4|h5|h6]' + |
| 346 | 'header[A][C]' + |
| 347 | 'footer[A][C]' + |
| 348 | 'address[A][C]' + |
| 349 | 'p[A|align][B]' + |
| 350 | 'br[A|clear][]' + |
| 351 | 'pre[A|width|xml:space][B]' + |
| 352 | 'dialog[A][dd|dt]' + |
| 353 | 'blockquote[A|cite][C]' + |
| 354 | 'ol[A|start|reversed|type|compact][li]' + |
| 355 | 'ul[A|type|compact][li]' + |
| 356 | 'li[A|value|type][C]' + |
| 357 | 'dl[A|compact][dd|dt]' + |
| 358 | 'dt[A][B]' + |
| 359 | 'dd[A][C]' + |
| 360 | 'a[A|href|target|ping|rel|media|type|tabindex|onfocus|onblur|charset|name|hreflang|rev|shape|coords][B]' + |
| 361 | 'em[A][B]' + |
| 362 | 'strong[A][B]' + |
| 363 | 'small[A][B]' + |
| 364 | 'cite[A][B]' + |
| 365 | 'q[A|cite][B]' + |
| 366 | 'dfn[A][B]' + |
| 367 | 'abbr[A][B]' + |
| 368 | 'code[A][B]' + |
| 369 | 'var[A][B]' + |
| 370 | 'samp[A][B]' + |
| 371 | 'kbd[A][B]' + |
| 372 | 'sub[A][B]' + |
| 373 | 'sup[A][B]' + |
| 374 | 'i[A][B]' + |
| 375 | 'b[A][B]' + |
| 376 | 'mark[A][B]' + |
| 377 | 'progress[A|value|max][B]' + |
| 378 | 'meter[A|value|min|max|low|high|optimum][B]' + |
| 379 | 'time[A|datetime][B]' + |
| 380 | 'ruby[A][B|rt|rp]' + |
| 381 | 'rt[A][B]' + |
| 382 | 'rp[A][B]' + |
| 383 | 'bdo[A][B]' + |
| 384 | 'span[A][B]' + |
| 385 | 'ins[A|cite|datetime][C]' + |
| 386 | 'del[A|cite|datetime][C]' + |
| 387 | 'figure[A][C|legend|figcaption]' + |
| 388 | 'figcaption[A][C]' + |
| 389 | 'img[A|alt|src|height|width|usemap|ismap|name|longdesc|align|border|hspace|vspace][]' + |
| 390 | 'iframe[A|name|src|height|width|sandbox|seamless|longdesc|frameborder|marginwidth|marginheight|scrolling|align][]' + |
| 391 | 'embed[A|src|height|width|type][]' + |
| 392 | 'object[*][]' + |
| 393 | 'param[A|name|value|valuetype|type][]' + |
| 394 | 'details[A|open][C|legend]' + |
| 395 | 'command[A|type|label|icon|disabled|checked|radiogroup][]' + |
| 396 | 'menu[A|type|label|compact][C|li]' + |
| 397 | 'legend[A|align][C|B]' + |
| 398 | 'div[A|align][C]' + |
| 399 | 'source[A|src|type|media][]' + |
| 400 | 'audio[A|src|autobuffer|autoplay|loop|controls][source]' + |
| 401 | 'video[A|src|autobuffer|autoplay|loop|controls|width|height|poster][source]' + |
| 402 | 'hr[A|align|noshade|size|width][]' + |
| 403 | 'form[A|accept-charset|action|autocomplete|enctype|method|name|novalidate|target|onsubmit|onreset|accept][C]' + |
| 404 | 'fieldset[A|disabled|form|name][C|legend]' + |
| 405 | 'label[A|form|for|onfocus|onblur][B]' + |
| 406 | 'input[A|type|accept|alt|autocomplete|autofocus|checked|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|height|list|max|maxlength|min|' + |
| 407 | 'multiple|pattern|placeholder|readonly|required|size|src|step|width|files|value|name|tabindex|onfocus|onblur|usemap|onselect|onchange|align][]' + |
| 408 | 'button[A|autofocus|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|name|value|type|tabindex|onfocus|onblur][C]' + |
| 409 | 'select[A|autofocus|disabled|form|multiple|name|size|tabindex|onfocus|onblur|onchange][option|optgroup]' + |
| 410 | 'datalist[A][B|option]' + |
| 411 | 'optgroup[A|disabled|label][option]' + |
| 412 | 'option[A|disabled|selected|label|value][]' + |
| 413 | 'textarea[A|autofocus|disabled|form|maxlength|name|placeholder|readonly|required|rows|cols|wrap|tabindex|onfocus|onblur|onselect|onchange][]' + |
| 414 | 'keygen[A|autofocus|challenge|disabled|form|keytype|name][]' + |
| 415 | 'output[A|for|form|name][B]' + |
| 416 | 'canvas[A|width|height][]' + |
| 417 | 'map[A|name][B|C]' + |
| 418 | 'area[A|shape|coords|href|alt|target|media|rel|ping|type|tabindex|onfocus|onblur|nohref][]' + |
| 419 | 'mathml[A][]' + |
| 420 | 'svg[A][]' + |
| 421 | 'table[A|border|summary|width|frame|rules|cellspacing|cellpadding|align|bgcolor][caption|colgroup|thead|tfoot|tbody|tr|col]' + |
| 422 | 'caption[A|align][C]' + |
| 423 | 'colgroup[A|span|width|align|char|charoff|valign][col]' + |
| 424 | 'col[A|span|width|align|char|charoff|valign][]' + |
| 425 | 'thead[A|align|char|charoff|valign][tr]' + |
| 426 | 'tfoot[A|align|char|charoff|valign][tr]' + |
| 427 | 'tbody[A|align|char|charoff|valign][tr]' + |
| 428 | 'tr[A|align|char|charoff|valign|bgcolor][th|td]' + |
| 429 | 'th[A|headers|rowspan|colspan|scope|abbr|axis|align|char|charoff|valign|nowrap|bgcolor|width|height][C]' + |
| 430 | 'td[A|headers|rowspan|colspan|abbr|axis|scope|align|char|charoff|valign|nowrap|bgcolor|width|height][C]' + |
| 431 | 'wbr[A][]' + |
| 432 | 'applet[id|class|style|title|codebase|archive|code|object|alt|name|width|height|align|hspace|vspace][C|param]' + |
| 433 | 'tt[A][B]' + |
| 434 | 'u[A][B]' + |
| 435 | 's[A][B]' + |
| 436 | 'strike[A][B]' + |
| 437 | 'big[A][B]' + |
| 438 | 'font[id|class|style|title|lang|xml:lang|dir|size|color|face][B]' + |
| 439 | 'basefont[id|size|color|face][]' + |
| 440 | 'acronym[A][C]' + |
| 441 | 'dir[A|compact][li]' + |
| 442 | 'center[A][C]' + |
| 443 | 'noframes[A][C]' + |
| 444 | 'isindex[id|class|style|title|lang|xml:lang|dir|prompt][]' |
| 445 | ); |
| 446 | } |
| 447 | |
| 448 | return sane; |
| 449 | }; |
| 450 | |
| 451 | /** |
| 452 | * Schema validator class. |
| 453 | * |
| 454 | * @class tinymce.html.Schema |
| 455 | * @example |
| 456 | * if (tinymce.activeEditor.schema.isValidChild('p', 'span')) |
| 457 | * alert('span is valid child of p.'); |
| 458 | * |
| 459 | * if (tinymce.activeEditor.schema.getElementRule('p')) |
| 460 | * alert('P is a valid element.'); |
| 461 | * |
| 462 | * @class tinymce.html.Schema |
| 463 | * @version 3.4 |
| 464 | */ |
| 465 | |
| 466 | /** |
| 467 | * Constructs a new Schema instance. |
| 468 | * |
| 469 | * @constructor |
| 470 | * @method Schema |
| 471 | * @param {Object} settings Name/value settings object. |
| 472 | */ |
| 473 | tinymce.html.Schema = function(settings) { |
| 474 | var self = this, elements = {}, children = {}, patternElements = [], validStyles, schemaItems; |
| 475 | var whiteSpaceElementsMap, selfClosingElementsMap, shortEndedElementsMap, boolAttrMap, blockElementsMap, nonEmptyElementsMap, customElementsMap = {}; |
| 476 | |
| 477 | // Creates an lookup table map object for the specified option or the default value |
| 478 | function createLookupTable(option, default_value, extend) { |
| 479 | var value = settings[option]; |
| 480 | |
| 481 | if (!value) { |
| 482 | // Get cached default map or make it if needed |
| 483 | value = mapCache[option]; |
| 484 | |
| 485 | if (!value) { |
| 486 | value = makeMap(default_value, ' ', makeMap(default_value.toUpperCase(), ' ')); |
| 487 | value = tinymce.extend(value, extend); |
| 488 | |
| 489 | mapCache[option] = value; |
| 490 | } |
| 491 | } else { |
| 492 | // Create custom map |
| 493 | value = makeMap(value, ',', makeMap(value.toUpperCase(), ' ')); |
| 494 | } |
| 495 | |
| 496 | return value; |
| 497 | }; |
| 498 | |
| 499 | settings = settings || {}; |
| 500 | |
| 501 | /** |
| 502 | * WordPress core uses a sane schema in place of the default "HTML5" schema. |
| 503 | */ |
| 504 | schemaItems = settings.schema == "html5" ? getSaneSchema() : getHTML4(); |
| 505 | |
| 506 | // Allow all elements and attributes if verify_html is set to false |
| 507 | if (settings.verify_html === false) |
| 508 | settings.valid_elements = '*[*]'; |
| 509 | |
| 510 | // Build styles list |
| 511 | if (settings.valid_styles) { |
| 512 | validStyles = {}; |
| 513 | |
| 514 | // Convert styles into a rule list |
| 515 | each(settings.valid_styles, function(value, key) { |
| 516 | validStyles[key] = tinymce.explode(value); |
| 517 | }); |
| 518 | } |
| 519 | |
| 520 | // Setup map objects |
| 521 | whiteSpaceElementsMap = createLookupTable('whitespace_elements', 'pre script noscript style textarea'); |
| 522 | selfClosingElementsMap = createLookupTable('self_closing_elements', 'colgroup dd dt li option p td tfoot th thead tr'); |
| 523 | shortEndedElementsMap = createLookupTable('short_ended_elements', 'area base basefont br col frame hr img input isindex link meta param embed source wbr'); |
| 524 | boolAttrMap = createLookupTable('boolean_attributes', 'checked compact declare defer disabled ismap multiple nohref noresize noshade nowrap readonly selected autoplay loop controls'); |
| 525 | nonEmptyElementsMap = createLookupTable('non_empty_elements', 'td th iframe video audio object', shortEndedElementsMap); |
| 526 | textBlockElementsMap = createLookupTable('text_block_elements', 'h1 h2 h3 h4 h5 h6 p div address pre form ' + |
| 527 | 'blockquote center dir fieldset header footer article section hgroup aside nav figure'); |
| 528 | blockElementsMap = createLookupTable('block_elements', 'hr table tbody thead tfoot ' + |
| 529 | 'th tr td li ol ul caption dl dt dd noscript menu isindex samp option datalist select optgroup', textBlockElementsMap); |
| 530 | |
| 531 | // Converts a wildcard expression string to a regexp for example *a will become /.*a/. |
| 532 | function patternToRegExp(str) { |
| 533 | return new RegExp('^' + str.replace(/([?+*])/g, '.$1') + '$'); |
| 534 | }; |
| 535 | |
| 536 | // Parses the specified valid_elements string and adds to the current rules |
| 537 | // This function is a bit hard to read since it's heavily optimized for speed |
| 538 | function addValidElements(valid_elements) { |
| 539 | var ei, el, ai, al, yl, matches, element, attr, attrData, elementName, attrName, attrType, attributes, attributesOrder, |
| 540 | prefix, outputName, globalAttributes, globalAttributesOrder, transElement, key, childKey, value, |
| 541 | elementRuleRegExp = /^([#+\-])?([^\[\/]+)(?:\/([^\[]+))?(?:\[([^\]]+)\])?$/, |
| 542 | attrRuleRegExp = /^([!\-])?(\w+::\w+|[^=:<]+)?(?:([=:<])(.*))?$/, |
| 543 | hasPatternsRegExp = /[*?+]/; |
| 544 | |
| 545 | if (valid_elements) { |
| 546 | // Split valid elements into an array with rules |
| 547 | valid_elements = split(valid_elements); |
| 548 | |
| 549 | if (elements['@']) { |
| 550 | globalAttributes = elements['@'].attributes; |
| 551 | globalAttributesOrder = elements['@'].attributesOrder; |
| 552 | } |
| 553 | |
| 554 | // Loop all rules |
| 555 | for (ei = 0, el = valid_elements.length; ei < el; ei++) { |
| 556 | // Parse element rule |
| 557 | matches = elementRuleRegExp.exec(valid_elements[ei]); |
| 558 | if (matches) { |
| 559 | // Setup local names for matches |
| 560 | prefix = matches[1]; |
| 561 | elementName = matches[2]; |
| 562 | outputName = matches[3]; |
| 563 | attrData = matches[4]; |
| 564 | |
| 565 | // Create new attributes and attributesOrder |
| 566 | attributes = {}; |
| 567 | attributesOrder = []; |
| 568 | |
| 569 | // Create the new element |
| 570 | element = { |
| 571 | attributes : attributes, |
| 572 | attributesOrder : attributesOrder |
| 573 | }; |
| 574 | |
| 575 | // Padd empty elements prefix |
| 576 | if (prefix === '#') |
| 577 | element.paddEmpty = true; |
| 578 | |
| 579 | // Remove empty elements prefix |
| 580 | if (prefix === '-') |
| 581 | element.removeEmpty = true; |
| 582 | |
| 583 | // Copy attributes from global rule into current rule |
| 584 | if (globalAttributes) { |
| 585 | for (key in globalAttributes) |
| 586 | attributes[key] = globalAttributes[key]; |
| 587 | |
| 588 | attributesOrder.push.apply(attributesOrder, globalAttributesOrder); |
| 589 | } |
| 590 | |
| 591 | // Attributes defined |
| 592 | if (attrData) { |
| 593 | attrData = split(attrData, '|'); |
| 594 | for (ai = 0, al = attrData.length; ai < al; ai++) { |
| 595 | matches = attrRuleRegExp.exec(attrData[ai]); |
| 596 | if (matches) { |
| 597 | attr = {}; |
| 598 | attrType = matches[1]; |
| 599 | attrName = matches[2].replace(/::/g, ':'); |
| 600 | prefix = matches[3]; |
| 601 | value = matches[4]; |
| 602 | |
| 603 | // Required |
| 604 | if (attrType === '!') { |
| 605 | element.attributesRequired = element.attributesRequired || []; |
| 606 | element.attributesRequired.push(attrName); |
| 607 | attr.required = true; |
| 608 | } |
| 609 | |
| 610 | // Denied from global |
| 611 | if (attrType === '-') { |
| 612 | delete attributes[attrName]; |
| 613 | attributesOrder.splice(tinymce.inArray(attributesOrder, attrName), 1); |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | // Default value |
| 618 | if (prefix) { |
| 619 | // Default value |
| 620 | if (prefix === '=') { |
| 621 | element.attributesDefault = element.attributesDefault || []; |
| 622 | element.attributesDefault.push({name: attrName, value: value}); |
| 623 | attr.defaultValue = value; |
| 624 | } |
| 625 | |
| 626 | // Forced value |
| 627 | if (prefix === ':') { |
| 628 | element.attributesForced = element.attributesForced || []; |
| 629 | element.attributesForced.push({name: attrName, value: value}); |
| 630 | attr.forcedValue = value; |
| 631 | } |
| 632 | |
| 633 | // Required values |
| 634 | if (prefix === '<') |
| 635 | attr.validValues = makeMap(value, '?'); |
| 636 | } |
| 637 | |
| 638 | // Check for attribute patterns |
| 639 | if (hasPatternsRegExp.test(attrName)) { |
| 640 | element.attributePatterns = element.attributePatterns || []; |
| 641 | attr.pattern = patternToRegExp(attrName); |
| 642 | element.attributePatterns.push(attr); |
| 643 | } else { |
| 644 | // Add attribute to order list if it doesn't already exist |
| 645 | if (!attributes[attrName]) |
| 646 | attributesOrder.push(attrName); |
| 647 | |
| 648 | attributes[attrName] = attr; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // Global rule, store away these for later usage |
| 655 | if (!globalAttributes && elementName == '@') { |
| 656 | globalAttributes = attributes; |
| 657 | globalAttributesOrder = attributesOrder; |
| 658 | } |
| 659 | |
| 660 | // Handle substitute elements such as b/strong |
| 661 | if (outputName) { |
| 662 | element.outputName = elementName; |
| 663 | elements[outputName] = element; |
| 664 | } |
| 665 | |
| 666 | // Add pattern or exact element |
| 667 | if (hasPatternsRegExp.test(elementName)) { |
| 668 | element.pattern = patternToRegExp(elementName); |
| 669 | patternElements.push(element); |
| 670 | } else |
| 671 | elements[elementName] = element; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | }; |
| 676 | |
| 677 | function setValidElements(valid_elements) { |
| 678 | elements = {}; |
| 679 | patternElements = []; |
| 680 | |
| 681 | addValidElements(valid_elements); |
| 682 | |
| 683 | each(schemaItems, function(element, name) { |
| 684 | children[name] = element.children; |
| 685 | }); |
| 686 | }; |
| 687 | |
| 688 | // Adds custom non HTML elements to the schema |
| 689 | function addCustomElements(custom_elements) { |
| 690 | var customElementRegExp = /^(~)?(.+)$/; |
| 691 | |
| 692 | if (custom_elements) { |
| 693 | each(split(custom_elements), function(rule) { |
| 694 | var matches = customElementRegExp.exec(rule), |
| 695 | inline = matches[1] === '~', |
| 696 | cloneName = inline ? 'span' : 'div', |
| 697 | name = matches[2]; |
| 698 | |
| 699 | children[name] = children[cloneName]; |
| 700 | customElementsMap[name] = cloneName; |
| 701 | |
| 702 | // If it's not marked as inline then add it to valid block elements |
| 703 | if (!inline) { |
| 704 | blockElementsMap[name.toUpperCase()] = {}; |
| 705 | blockElementsMap[name] = {}; |
| 706 | } |
| 707 | |
| 708 | // Add elements clone if needed |
| 709 | if (!elements[name]) { |
| 710 | elements[name] = elements[cloneName]; |
| 711 | } |
| 712 | |
| 713 | // Add custom elements at span/div positions |
| 714 | each(children, function(element, child) { |
| 715 | if (element[cloneName]) |
| 716 | element[name] = element[cloneName]; |
| 717 | }); |
| 718 | }); |
| 719 | } |
| 720 | }; |
| 721 | |
| 722 | // Adds valid children to the schema object |
| 723 | function addValidChildren(valid_children) { |
| 724 | var childRuleRegExp = /^([+\-]?)(\w+)\[([^\]]+)\]$/; |
| 725 | |
| 726 | if (valid_children) { |
| 727 | each(split(valid_children), function(rule) { |
| 728 | var matches = childRuleRegExp.exec(rule), parent, prefix; |
| 729 | |
| 730 | if (matches) { |
| 731 | prefix = matches[1]; |
| 732 | |
| 733 | // Add/remove items from default |
| 734 | if (prefix) |
| 735 | parent = children[matches[2]]; |
| 736 | else |
| 737 | parent = children[matches[2]] = {'#comment' : {}}; |
| 738 | |
| 739 | parent = children[matches[2]]; |
| 740 | |
| 741 | each(split(matches[3], '|'), function(child) { |
| 742 | if (prefix === '-') |
| 743 | delete parent[child]; |
| 744 | else |
| 745 | parent[child] = {}; |
| 746 | }); |
| 747 | } |
| 748 | }); |
| 749 | } |
| 750 | }; |
| 751 | |
| 752 | function getElementRule(name) { |
| 753 | var element = elements[name], i; |
| 754 | |
| 755 | // Exact match found |
| 756 | if (element) |
| 757 | return element; |
| 758 | |
| 759 | // No exact match then try the patterns |
| 760 | i = patternElements.length; |
| 761 | while (i--) { |
| 762 | element = patternElements[i]; |
| 763 | |
| 764 | if (element.pattern.test(name)) |
| 765 | return element; |
| 766 | } |
| 767 | }; |
| 768 | |
| 769 | if (!settings.valid_elements) { |
| 770 | // No valid elements defined then clone the elements from the schema spec |
| 771 | each(schemaItems, function(element, name) { |
| 772 | elements[name] = { |
| 773 | attributes : element.attributes, |
| 774 | attributesOrder : element.attributesOrder |
| 775 | }; |
| 776 | |
| 777 | children[name] = element.children; |
| 778 | }); |
| 779 | |
| 780 | // Switch these on HTML4 |
| 781 | if (settings.schema != "html5") { |
| 782 | each(split('strong/b,em/i'), function(item) { |
| 783 | item = split(item, '/'); |
| 784 | elements[item[1]].outputName = item[0]; |
| 785 | }); |
| 786 | } |
| 787 | |
| 788 | // Add default alt attribute for images |
| 789 | elements.img.attributesDefault = [{name: 'alt', value: ''}]; |
| 790 | |
| 791 | // Remove these if they are empty by default |
| 792 | each(split('ol,ul,sub,sup,blockquote,span,font,a,table,tbody,tr,strong,em,b,i'), function(name) { |
| 793 | if (elements[name]) { |
| 794 | elements[name].removeEmpty = true; |
| 795 | } |
| 796 | }); |
| 797 | |
| 798 | // Padd these by default |
| 799 | each(split('p,h1,h2,h3,h4,h5,h6,th,td,pre,div,address,caption'), function(name) { |
| 800 | elements[name].paddEmpty = true; |
| 801 | }); |
| 802 | } else |
| 803 | setValidElements(settings.valid_elements); |
| 804 | |
| 805 | addCustomElements(settings.custom_elements); |
| 806 | addValidChildren(settings.valid_children); |
| 807 | addValidElements(settings.extended_valid_elements); |
| 808 | |
| 809 | // Todo: Remove this when we fix list handling to be valid |
| 810 | addValidChildren('+ol[ul|ol],+ul[ul|ol]'); |
| 811 | |
| 812 | // Delete invalid elements |
| 813 | if (settings.invalid_elements) { |
| 814 | tinymce.each(tinymce.explode(settings.invalid_elements), function(item) { |
| 815 | if (elements[item]) |
| 816 | delete elements[item]; |
| 817 | }); |
| 818 | } |
| 819 | |
| 820 | // If the user didn't allow span only allow internal spans |
| 821 | if (!getElementRule('span')) |
| 822 | addValidElements('span[!data-mce-type|*]'); |
| 823 | |
| 824 | /** |
| 825 | * Name/value map object with valid parents and children to those parents. |
| 826 | * |
| 827 | * @example |
| 828 | * children = { |
| 829 | * div:{p:{}, h1:{}} |
| 830 | * }; |
| 831 | * @field children |
| 832 | * @type {Object} |
| 833 | */ |
| 834 | self.children = children; |
| 835 | |
| 836 | /** |
| 837 | * Name/value map object with valid styles for each element. |
| 838 | * |
| 839 | * @field styles |
| 840 | * @type {Object} |
| 841 | */ |
| 842 | self.styles = validStyles; |
| 843 | |
| 844 | /** |
| 845 | * Returns a map with boolean attributes. |
| 846 | * |
| 847 | * @method getBoolAttrs |
| 848 | * @return {Object} Name/value lookup map for boolean attributes. |
| 849 | */ |
| 850 | self.getBoolAttrs = function() { |
| 851 | return boolAttrMap; |
| 852 | }; |
| 853 | |
| 854 | /** |
| 855 | * Returns a map with block elements. |
| 856 | * |
| 857 | * @method getBlockElements |
| 858 | * @return {Object} Name/value lookup map for block elements. |
| 859 | */ |
| 860 | self.getBlockElements = function() { |
| 861 | return blockElementsMap; |
| 862 | }; |
| 863 | |
| 864 | /** |
| 865 | * Returns a map with text block elements. Such as: p,h1-h6,div,address |
| 866 | * |
| 867 | * @method getTextBlockElements |
| 868 | * @return {Object} Name/value lookup map for block elements. |
| 869 | */ |
| 870 | self.getTextBlockElements = function() { |
| 871 | return textBlockElementsMap; |
| 872 | }; |
| 873 | |
| 874 | /** |
| 875 | * Returns a map with short ended elements such as BR or IMG. |
| 876 | * |
| 877 | * @method getShortEndedElements |
| 878 | * @return {Object} Name/value lookup map for short ended elements. |
| 879 | */ |
| 880 | self.getShortEndedElements = function() { |
| 881 | return shortEndedElementsMap; |
| 882 | }; |
| 883 | |
| 884 | /** |
| 885 | * Returns a map with self closing tags such as <li>. |
| 886 | * |
| 887 | * @method getSelfClosingElements |
| 888 | * @return {Object} Name/value lookup map for self closing tags elements. |
| 889 | */ |
| 890 | self.getSelfClosingElements = function() { |
| 891 | return selfClosingElementsMap; |
| 892 | }; |
| 893 | |
| 894 | /** |
| 895 | * Returns a map with elements that should be treated as contents regardless if it has text |
| 896 | * content in them or not such as TD, VIDEO or IMG. |
| 897 | * |
| 898 | * @method getNonEmptyElements |
| 899 | * @return {Object} Name/value lookup map for non empty elements. |
| 900 | */ |
| 901 | self.getNonEmptyElements = function() { |
| 902 | return nonEmptyElementsMap; |
| 903 | }; |
| 904 | |
| 905 | /** |
| 906 | * Returns a map with elements where white space is to be preserved like PRE or SCRIPT. |
| 907 | * |
| 908 | * @method getWhiteSpaceElements |
| 909 | * @return {Object} Name/value lookup map for white space elements. |
| 910 | */ |
| 911 | self.getWhiteSpaceElements = function() { |
| 912 | return whiteSpaceElementsMap; |
| 913 | }; |
| 914 | |
| 915 | /** |
| 916 | * Returns true/false if the specified element and it's child is valid or not |
| 917 | * according to the schema. |
| 918 | * |
| 919 | * @method isValidChild |
| 920 | * @param {String} name Element name to check for. |
| 921 | * @param {String} child Element child to verify. |
| 922 | * @return {Boolean} True/false if the element is a valid child of the specified parent. |
| 923 | */ |
| 924 | self.isValidChild = function(name, child) { |
| 925 | var parent = children[name]; |
| 926 | |
| 927 | return !!(parent && parent[child]); |
| 928 | }; |
| 929 | |
| 930 | /** |
| 931 | * Returns true/false if the specified element name and optional attribute is |
| 932 | * valid according to the schema. |
| 933 | * |
| 934 | * @method isValid |
| 935 | * @param {String} name Name of element to check. |
| 936 | * @param {String} attr Optional attribute name to check for. |
| 937 | * @return {Boolean} True/false if the element and attribute is valid. |
| 938 | */ |
| 939 | self.isValid = function(name, attr) { |
| 940 | var attrPatterns, i, rule = getElementRule(name); |
| 941 | |
| 942 | // Check if it's a valid element |
| 943 | if (rule) { |
| 944 | if (attr) { |
| 945 | // Check if attribute name exists |
| 946 | if (rule.attributes[attr]) { |
| 947 | return true; |
| 948 | } |
| 949 | |
| 950 | // Check if attribute matches a regexp pattern |
| 951 | attrPatterns = rule.attributePatterns; |
| 952 | if (attrPatterns) { |
| 953 | i = attrPatterns.length; |
| 954 | while (i--) { |
| 955 | if (attrPatterns[i].pattern.test(name)) { |
| 956 | return true; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } else { |
| 961 | return true; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // No match |
| 966 | return false; |
| 967 | }; |
| 968 | |
| 969 | /** |
| 970 | * Returns true/false if the specified element is valid or not |
| 971 | * according to the schema. |
| 972 | * |
| 973 | * @method getElementRule |
| 974 | * @param {String} name Element name to check for. |
| 975 | * @return {Object} Element object or undefined if the element isn't valid. |
| 976 | */ |
| 977 | self.getElementRule = getElementRule; |
| 978 | |
| 979 | /** |
| 980 | * Returns an map object of all custom elements. |
| 981 | * |
| 982 | * @method getCustomElements |
| 983 | * @return {Object} Name/value map object of all custom elements. |
| 984 | */ |
| 985 | self.getCustomElements = function() { |
| 986 | return customElementsMap; |
| 987 | }; |
| 988 | |
| 989 | /** |
| 990 | * Parses a valid elements string and adds it to the schema. The valid elements format is for example "element[attr=default|otherattr]". |
| 991 | * Existing rules will be replaced with the ones specified, so this extends the schema. |
| 992 | * |
| 993 | * @method addValidElements |
| 994 | * @param {String} valid_elements String in the valid elements format to be parsed. |
| 995 | */ |
| 996 | self.addValidElements = addValidElements; |
| 997 | |
| 998 | /** |
| 999 | * Parses a valid elements string and sets it to the schema. The valid elements format is for example "element[attr=default|otherattr]". |
| 1000 | * Existing rules will be replaced with the ones specified, so this extends the schema. |
| 1001 | * |
| 1002 | * @method setValidElements |
| 1003 | * @param {String} valid_elements String in the valid elements format to be parsed. |
| 1004 | */ |
| 1005 | self.setValidElements = setValidElements; |
| 1006 | |
| 1007 | /** |
| 1008 | * Adds custom non HTML elements to the schema. |
| 1009 | * |
| 1010 | * @method addCustomElements |
| 1011 | * @param {String} custom_elements Comma separated list of custom elements to add. |
| 1012 | */ |
| 1013 | self.addCustomElements = addCustomElements; |
| 1014 | |
| 1015 | /** |
| 1016 | * Parses a valid children string and adds them to the schema structure. The valid children format is for example: "element[child1|child2]". |
| 1017 | * |
| 1018 | * @method addValidChildren |
| 1019 | * @param {String} valid_children Valid children elements string to parse |
| 1020 | */ |
| 1021 | self.addValidChildren = addValidChildren; |
| 1022 | |
| 1023 | self.elements = elements; |
| 1024 | }; |
| 1025 | })(tinymce); |
| 1026 | /** |
| 1027 | * Schema.js |
| 1028 | * |
| 1029 | * Copyright, Moxiecode Systems AB |
| 1030 | * Released under LGPL License. |
| 1031 | * |
| 1032 | * License: http://www.tinymce.com/license |
| 1033 | * Contributing: http://www.tinymce.com/contributing |
| 1034 | */ |
| 1035 | |
| 1036 | (function(tinymce) { |
| 1037 | var mapCache = {}, makeMap = tinymce.makeMap, each = tinymce.each; |
| 1038 | |
| 1039 | function split(str, delim) { |
| 1040 | return str.split(delim || ','); |
| 1041 | }; |
| 1042 | |
| 1043 | /** |
| 1044 | * Unpacks the specified lookup and string data it will also parse it into an object |
| 1045 | * map with sub object for it's children. This will later also include the attributes. |
| 1046 | */ |
| 1047 | function unpack(lookup, data) { |
| 1048 | var key, elements = {}; |
| 1049 | |
| 1050 | function replace(value) { |
| 1051 | return value.replace(/[A-Z]+/g, function(key) { |
| 1052 | return replace(lookup[key]); |
| 1053 | }); |
| 1054 | }; |
| 1055 | |
| 1056 | // Unpack lookup |
| 1057 | for (key in lookup) { |
| 1058 | if (lookup.hasOwnProperty(key)) |
| 1059 | lookup[key] = replace(lookup[key]); |
| 1060 | } |
| 1061 | |
| 1062 | // Unpack and parse data into object map |
| 1063 | replace(data).replace(/#/g, '#text').replace(/(\w+)\[([^\]]+)\]\[([^\]]*)\]/g, function(str, name, attributes, children) { |
| 1064 | attributes = split(attributes, '|'); |
| 1065 | |
| 1066 | elements[name] = { |
| 1067 | attributes : makeMap(attributes), |
| 1068 | attributesOrder : attributes, |
| 1069 | children : makeMap(children, '|', {'#comment' : {}}) |
| 1070 | } |
| 1071 | }); |
| 1072 | |
| 1073 | return elements; |
| 1074 | }; |
| 1075 | |
| 1076 | /** |
| 1077 | * Returns the HTML5 schema and caches it in the mapCache. |
| 1078 | */ |
| 1079 | function getHTML5() { |
| 1080 | var html5 = mapCache.html5; |
| 1081 | |
| 1082 | if (!html5) { |
| 1083 | html5 = mapCache.html5 = unpack({ |
| 1084 | A : 'id|accesskey|class|dir|draggable|item|hidden|itemprop|role|spellcheck|style|subject|title|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup', |
| 1085 | B : '#|a|abbr|area|audio|b|bdo|br|button|canvas|cite|code|command|datalist|del|dfn|em|embed|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|meta|' + |
| 1086 | 'meter|noscript|object|output|progress|q|ruby|samp|script|select|small|span|strong|sub|sup|svg|textarea|time|var|video|wbr', |
| 1087 | C : '#|a|abbr|area|address|article|aside|audio|b|bdo|blockquote|br|button|canvas|cite|code|command|datalist|del|details|dfn|dialog|div|dl|em|embed|fieldset|' + |
| 1088 | 'figure|footer|form|h1|h2|h3|h4|h5|h6|header|hgroup|hr|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|menu|meta|meter|nav|noscript|ol|object|output|' + |
| 1089 | 'p|pre|progress|q|ruby|samp|script|section|select|small|span|strong|style|sub|sup|svg|table|textarea|time|ul|var|video' |
| 1090 | }, 'html[A|manifest][body|head]' + |
| 1091 | 'head[A][base|command|link|meta|noscript|script|style|title]' + |
| 1092 | 'title[A][#]' + |
| 1093 | 'base[A|href|target][]' + |
| 1094 | 'link[A|href|rel|media|type|sizes][]' + |
| 1095 | 'meta[A|http-equiv|name|content|charset][]' + |
| 1096 | 'style[A|type|media|scoped][#]' + |
| 1097 | 'script[A|charset|type|src|defer|async][#]' + |
| 1098 | 'noscript[A][C]' + |
| 1099 | 'body[A][C]' + |
| 1100 | 'section[A][C]' + |
| 1101 | 'nav[A][C]' + |
| 1102 | 'article[A][C]' + |
| 1103 | 'aside[A][C]' + |
| 1104 | 'h1[A][B]' + |
| 1105 | 'h2[A][B]' + |
| 1106 | 'h3[A][B]' + |
| 1107 | 'h4[A][B]' + |
| 1108 | 'h5[A][B]' + |
| 1109 | 'h6[A][B]' + |
| 1110 | 'hgroup[A][h1|h2|h3|h4|h5|h6]' + |
| 1111 | 'header[A][C]' + |
| 1112 | 'footer[A][C]' + |
| 1113 | 'address[A][C]' + |
| 1114 | 'p[A][B]' + |
| 1115 | 'br[A][]' + |
| 1116 | 'pre[A][B]' + |
| 1117 | 'dialog[A][dd|dt]' + |
| 1118 | 'blockquote[A|cite][C]' + |
| 1119 | 'ol[A|start|reversed][li]' + |
| 1120 | 'ul[A][li]' + |
| 1121 | 'li[A|value][C]' + |
| 1122 | 'dl[A][dd|dt]' + |
| 1123 | 'dt[A][B]' + |
| 1124 | 'dd[A][C]' + |
| 1125 | 'a[A|href|target|ping|rel|media|type][B]' + |
| 1126 | 'em[A][B]' + |
| 1127 | 'strong[A][B]' + |
| 1128 | 'small[A][B]' + |
| 1129 | 'cite[A][B]' + |
| 1130 | 'q[A|cite][B]' + |
| 1131 | 'dfn[A][B]' + |
| 1132 | 'abbr[A][B]' + |
| 1133 | 'code[A][B]' + |
| 1134 | 'var[A][B]' + |
| 1135 | 'samp[A][B]' + |
| 1136 | 'kbd[A][B]' + |
| 1137 | 'sub[A][B]' + |
| 1138 | 'sup[A][B]' + |
| 1139 | 'i[A][B]' + |
| 1140 | 'b[A][B]' + |
| 1141 | 'mark[A][B]' + |
| 1142 | 'progress[A|value|max][B]' + |
| 1143 | 'meter[A|value|min|max|low|high|optimum][B]' + |
| 1144 | 'time[A|datetime][B]' + |
| 1145 | 'ruby[A][B|rt|rp]' + |
| 1146 | 'rt[A][B]' + |
| 1147 | 'rp[A][B]' + |
| 1148 | 'bdo[A][B]' + |
| 1149 | 'span[A][B]' + |
| 1150 | 'ins[A|cite|datetime][B]' + |
| 1151 | 'del[A|cite|datetime][B]' + |
| 1152 | 'figure[A][C|legend|figcaption]' + |
| 1153 | 'figcaption[A][C]' + |
| 1154 | 'img[A|alt|src|height|width|usemap|ismap][]' + |
| 1155 | 'iframe[A|name|src|height|width|sandbox|seamless][]' + |
| 1156 | 'embed[A|src|height|width|type][]' + |
| 1157 | 'object[A|data|type|height|width|usemap|name|form|classid][param]' + |
| 1158 | 'param[A|name|value][]' + |
| 1159 | 'details[A|open][C|legend]' + |
| 1160 | 'command[A|type|label|icon|disabled|checked|radiogroup][]' + |
| 1161 | 'menu[A|type|label][C|li]' + |
| 1162 | 'legend[A][C|B]' + |
| 1163 | 'div[A][C]' + |
| 1164 | 'source[A|src|type|media][]' + |
| 1165 | 'audio[A|src|autobuffer|autoplay|loop|controls][source]' + |
| 1166 | 'video[A|src|autobuffer|autoplay|loop|controls|width|height|poster][source]' + |
| 1167 | 'hr[A][]' + |
| 1168 | 'form[A|accept-charset|action|autocomplete|enctype|method|name|novalidate|target][C]' + |
| 1169 | 'fieldset[A|disabled|form|name][C|legend]' + |
| 1170 | 'label[A|form|for][B]' + |
| 1171 | 'input[A|type|accept|alt|autocomplete|autofocus|checked|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|height|list|max|maxlength|min|' + |
| 1172 | 'multiple|pattern|placeholder|readonly|required|size|src|step|width|files|value|name][]' + |
| 1173 | 'button[A|autofocus|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|name|value|type][B]' + |
| 1174 | 'select[A|autofocus|disabled|form|multiple|name|size][option|optgroup]' + |
| 1175 | 'datalist[A][B|option]' + |
| 1176 | 'optgroup[A|disabled|label][option]' + |
| 1177 | 'option[A|disabled|selected|label|value][]' + |
| 1178 | 'textarea[A|autofocus|disabled|form|maxlength|name|placeholder|readonly|required|rows|cols|wrap][]' + |
| 1179 | 'keygen[A|autofocus|challenge|disabled|form|keytype|name][]' + |
| 1180 | 'output[A|for|form|name][B]' + |
| 1181 | 'canvas[A|width|height][]' + |
| 1182 | 'map[A|name][B|C]' + |
| 1183 | 'area[A|shape|coords|href|alt|target|media|rel|ping|type][]' + |
| 1184 | 'mathml[A][]' + |
| 1185 | 'svg[A][]' + |
| 1186 | 'table[A|border][caption|colgroup|thead|tfoot|tbody|tr]' + |
| 1187 | 'caption[A][C]' + |
| 1188 | 'colgroup[A|span][col]' + |
| 1189 | 'col[A|span][]' + |
| 1190 | 'thead[A][tr]' + |
| 1191 | 'tfoot[A][tr]' + |
| 1192 | 'tbody[A][tr]' + |
| 1193 | 'tr[A][th|td]' + |
| 1194 | 'th[A|headers|rowspan|colspan|scope][B]' + |
| 1195 | 'td[A|headers|rowspan|colspan][C]' + |
| 1196 | 'wbr[A][]' |
| 1197 | ); |
| 1198 | } |
| 1199 | |
| 1200 | return html5; |
| 1201 | }; |
| 1202 | |
| 1203 | /** |
| 1204 | * Returns the HTML4 schema and caches it in the mapCache. |
| 1205 | */ |
| 1206 | function getHTML4() { |
| 1207 | var html4 = mapCache.html4; |
| 1208 | |
| 1209 | if (!html4) { |
| 1210 | // This is the XHTML 1.0 transitional elements with it's attributes and children packed to reduce it's size |
| 1211 | html4 = mapCache.html4 = unpack({ |
| 1212 | Z : 'H|K|N|O|P', |
| 1213 | Y : 'X|form|R|Q', |
| 1214 | ZG : 'E|span|width|align|char|charoff|valign', |
| 1215 | X : 'p|T|div|U|W|isindex|fieldset|table', |
| 1216 | ZF : 'E|align|char|charoff|valign', |
| 1217 | W : 'pre|hr|blockquote|address|center|noframes', |
| 1218 | ZE : 'abbr|axis|headers|scope|rowspan|colspan|align|char|charoff|valign|nowrap|bgcolor|width|height', |
| 1219 | ZD : '[E][S]', |
| 1220 | U : 'ul|ol|dl|menu|dir', |
| 1221 | ZC : 'p|Y|div|U|W|table|br|span|bdo|object|applet|img|map|K|N|Q', |
| 1222 | T : 'h1|h2|h3|h4|h5|h6', |
| 1223 | ZB : 'X|S|Q', |
| 1224 | S : 'R|P', |
| 1225 | ZA : 'a|G|J|M|O|P', |
| 1226 | R : 'a|H|K|N|O', |
| 1227 | Q : 'noscript|P', |
| 1228 | P : 'ins|del|script', |
| 1229 | O : 'input|select|textarea|label|button', |
| 1230 | N : 'M|L', |
| 1231 | M : 'em|strong|dfn|code|q|samp|kbd|var|cite|abbr|acronym', |
| 1232 | L : 'sub|sup', |
| 1233 | K : 'J|I', |
| 1234 | J : 'tt|i|b|u|s|strike', |
| 1235 | I : 'big|small|font|basefont', |
| 1236 | H : 'G|F', |
| 1237 | G : 'br|span|bdo', |
| 1238 | F : 'object|applet|img|map|iframe', |
| 1239 | E : 'A|B|C', |
| 1240 | D : 'accesskey|tabindex|onfocus|onblur', |
| 1241 | C : 'onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup', |
| 1242 | B : 'lang|xml:lang|dir', |
| 1243 | A : 'id|class|style|title' |
| 1244 | }, 'script[id|charset|type|language|src|defer|xml:space][]' + |
| 1245 | 'style[B|id|type|media|title|xml:space][]' + |
| 1246 | 'object[E|declare|classid|codebase|data|type|codetype|archive|standby|width|height|usemap|name|tabindex|align|border|hspace|vspace][#|param|Y]' + |
| 1247 | 'param[id|name|value|valuetype|type][]' + |
| 1248 | 'p[E|align][#|S]' + |
| 1249 | 'a[E|D|charset|type|name|href|hreflang|rel|rev|shape|coords|target][#|Z]' + |
| 1250 | 'br[A|clear][]' + |
| 1251 | 'span[E][#|S]' + |
| 1252 | 'bdo[A|C|B][#|S]' + |
| 1253 | 'applet[A|codebase|archive|code|object|alt|name|width|height|align|hspace|vspace][#|param|Y]' + |
| 1254 | 'h1[E|align][#|S]' + |
| 1255 | 'img[E|src|alt|name|longdesc|width|height|usemap|ismap|align|border|hspace|vspace][]' + |
| 1256 | 'map[B|C|A|name][X|form|Q|area]' + |
| 1257 | 'h2[E|align][#|S]' + |
| 1258 | 'iframe[A|longdesc|name|src|frameborder|marginwidth|marginheight|scrolling|align|width|height][#|Y]' + |
| 1259 | 'h3[E|align][#|S]' + |
| 1260 | 'tt[E][#|S]' + |
| 1261 | 'i[E][#|S]' + |
| 1262 | 'b[E][#|S]' + |
| 1263 | 'u[E][#|S]' + |
| 1264 | 's[E][#|S]' + |
| 1265 | 'strike[E][#|S]' + |
| 1266 | 'big[E][#|S]' + |
| 1267 | 'small[E][#|S]' + |
| 1268 | 'font[A|B|size|color|face][#|S]' + |
| 1269 | 'basefont[id|size|color|face][]' + |
| 1270 | 'em[E][#|S]' + |
| 1271 | 'strong[E][#|S]' + |
| 1272 | 'dfn[E][#|S]' + |
| 1273 | 'code[E][#|S]' + |
| 1274 | 'q[E|cite][#|S]' + |
| 1275 | 'samp[E][#|S]' + |
| 1276 | 'kbd[E][#|S]' + |
| 1277 | 'var[E][#|S]' + |
| 1278 | 'cite[E][#|S]' + |
| 1279 | 'abbr[E][#|S]' + |
| 1280 | 'acronym[E][#|S]' + |
| 1281 | 'sub[E][#|S]' + |
| 1282 | 'sup[E][#|S]' + |
| 1283 | 'input[E|D|type|name|value|checked|disabled|readonly|size|maxlength|src|alt|usemap|onselect|onchange|accept|align][]' + |
| 1284 | 'select[E|name|size|multiple|disabled|tabindex|onfocus|onblur|onchange][optgroup|option]' + |
| 1285 | 'optgroup[E|disabled|label][option]' + |
| 1286 | 'option[E|selected|disabled|label|value][]' + |
| 1287 | 'textarea[E|D|name|rows|cols|disabled|readonly|onselect|onchange][]' + |
| 1288 | 'label[E|for|accesskey|onfocus|onblur][#|S]' + |
| 1289 | 'button[E|D|name|value|type|disabled][#|p|T|div|U|W|table|G|object|applet|img|map|K|N|Q]' + |
| 1290 | 'h4[E|align][#|S]' + |
| 1291 | 'ins[E|cite|datetime][#|Y]' + |
| 1292 | 'h5[E|align][#|S]' + |
| 1293 | 'del[E|cite|datetime][#|Y]' + |
| 1294 | 'h6[E|align][#|S]' + |
| 1295 | 'div[E|align][#|Y]' + |
| 1296 | 'ul[E|type|compact][li]' + |
| 1297 | 'li[E|type|value][#|Y]' + |
| 1298 | 'ol[E|type|compact|start][li]' + |
| 1299 | 'dl[E|compact][dt|dd]' + |
| 1300 | 'dt[E][#|S]' + |
| 1301 | 'dd[E][#|Y]' + |
| 1302 | 'menu[E|compact][li]' + |
| 1303 | 'dir[E|compact][li]' + |
| 1304 | 'pre[E|width|xml:space][#|ZA]' + |
| 1305 | 'hr[E|align|noshade|size|width][]' + |
| 1306 | 'blockquote[E|cite][#|Y]' + |
| 1307 | 'address[E][#|S|p]' + |
| 1308 | 'center[E][#|Y]' + |
| 1309 | 'noframes[E][#|Y]' + |
| 1310 | 'isindex[A|B|prompt][]' + |
| 1311 | 'fieldset[E][#|legend|Y]' + |
| 1312 | 'legend[E|accesskey|align][#|S]' + |
| 1313 | 'table[E|summary|width|border|frame|rules|cellspacing|cellpadding|align|bgcolor][caption|col|colgroup|thead|tfoot|tbody|tr]' + |
| 1314 | 'caption[E|align][#|S]' + |
| 1315 | 'col[ZG][]' + |
| 1316 | 'colgroup[ZG][col]' + |
| 1317 | 'thead[ZF][tr]' + |
| 1318 | 'tr[ZF|bgcolor][th|td]' + |
| 1319 | 'th[E|ZE][#|Y]' + |
| 1320 | 'form[E|action|method|name|enctype|onsubmit|onreset|accept|accept-charset|target][#|X|R|Q]' + |
| 1321 | 'noscript[E][#|Y]' + |
| 1322 | 'td[E|ZE][#|Y]' + |
| 1323 | 'tfoot[ZF][tr]' + |
| 1324 | 'tbody[ZF][tr]' + |
| 1325 | 'area[E|D|shape|coords|href|nohref|alt|target][]' + |
| 1326 | 'base[id|href|target][]' + |
| 1327 | 'body[E|onload|onunload|background|bgcolor|text|link|vlink|alink][#|Y]' |
| 1328 | ); |
| 1329 | } |
| 1330 | |
| 1331 | return html4; |
| 1332 | }; |
| 1333 | |
| 1334 | /** |
| 1335 | * Schema validator class. |
| 1336 | * |
| 1337 | * @class tinymce.html.Schema |
| 1338 | * @example |
| 1339 | * if (tinymce.activeEditor.schema.isValidChild('p', 'span')) |
| 1340 | * alert('span is valid child of p.'); |
| 1341 | * |
| 1342 | * if (tinymce.activeEditor.schema.getElementRule('p')) |
| 1343 | * alert('P is a valid element.'); |
| 1344 | * |
| 1345 | * @class tinymce.html.Schema |
| 1346 | * @version 3.4 |
| 1347 | */ |
| 1348 | |
| 1349 | /** |
| 1350 | * Constructs a new Schema instance. |
| 1351 | * |
| 1352 | * @constructor |
| 1353 | * @method Schema |
| 1354 | * @param {Object} settings Name/value settings object. |
| 1355 | */ |
| 1356 | tinymce.html.Schema = function(settings) { |
| 1357 | var self = this, elements = {}, children = {}, patternElements = [], validStyles, schemaItems; |
| 1358 | var whiteSpaceElementsMap, selfClosingElementsMap, shortEndedElementsMap, boolAttrMap, blockElementsMap, nonEmptyElementsMap, customElementsMap = {}; |
| 1359 | |
| 1360 | // Creates an lookup table map object for the specified option or the default value |
| 1361 | function createLookupTable(option, default_value, extend) { |
| 1362 | var value = settings[option]; |
| 1363 | |
| 1364 | if (!value) { |
| 1365 | // Get cached default map or make it if needed |
| 1366 | value = mapCache[option]; |
| 1367 | |
| 1368 | if (!value) { |
| 1369 | value = makeMap(default_value, ' ', makeMap(default_value.toUpperCase(), ' ')); |
| 1370 | value = tinymce.extend(value, extend); |
| 1371 | |
| 1372 | mapCache[option] = value; |
| 1373 | } |
| 1374 | } else { |
| 1375 | // Create custom map |
| 1376 | value = makeMap(value, ',', makeMap(value.toUpperCase(), ' ')); |
| 1377 | } |
| 1378 | |
| 1379 | return value; |
| 1380 | }; |
| 1381 | |
| 1382 | settings = settings || {}; |
| 1383 | schemaItems = settings.schema == "html5" ? getHTML5() : getHTML4(); |
| 1384 | |
| 1385 | // Koop |
| 1386 | window.schema = { |
| 1387 | html4: getHTML4(), |
| 1388 | html5: getHTML5(), |
| 1389 | sane: (function() { |
| 1390 | var cachedMapCache = mapCache, |
| 1391 | html5, html4; |
| 1392 | |
| 1393 | // Bust the mapCache so we're not dealing with the other schema objects. |
| 1394 | mapCache = {}; |
| 1395 | html5 = getHTML5(); |
| 1396 | html4 = getHTML4(); |
| 1397 | mapCache = cachedMapCache; |
| 1398 | |
| 1399 | |
| 1400 | each( html4, function( html4settings, tag ) { |
| 1401 | var html5settings = html5[ tag ], |
| 1402 | difference = []; |
| 1403 | |
| 1404 | // Merge tags missing in HTML5 mode. |
| 1405 | if ( ! html5settings ) { |
| 1406 | html5[ tag ] = html4settings; |
| 1407 | return; |
| 1408 | } |
| 1409 | |
| 1410 | // Merge attributes missing from this HTML5 tag. |
| 1411 | each( html4settings.attributes, function( attribute, key ) { |
| 1412 | if ( ! html5settings.attributes[ key ] ) |
| 1413 | html5settings.attributes[ key ] = attribute; |
| 1414 | }); |
| 1415 | |
| 1416 | // Merge any missing attributes into the atttributes order. |
| 1417 | each( html4settings.attributesOrder, function( key ) { |
| 1418 | if ( -1 === tinymce.inArray( html5settings.attributesOrder, key ) ) |
| 1419 | difference.push( key ); |
| 1420 | }); |
| 1421 | |
| 1422 | html5settings.attributesOrder = html5settings.attributesOrder.concat( difference ); |
| 1423 | |
| 1424 | // Merge children missing from this HTML5 tag. |
| 1425 | each( html4settings.children, function( child, key ) { |
| 1426 | if ( ! html5settings.children[ key ] ) |
| 1427 | html5settings.children[ key ] = child; |
| 1428 | }); |
| 1429 | }); |
| 1430 | |
| 1431 | return html5; |
| 1432 | }()) |
| 1433 | }; |
| 1434 | // Koop |
| 1435 | |
| 1436 | // Allow all elements and attributes if verify_html is set to false |
| 1437 | if (settings.verify_html === false) |
| 1438 | settings.valid_elements = '*[*]'; |
| 1439 | |
| 1440 | // Build styles list |
| 1441 | if (settings.valid_styles) { |
| 1442 | validStyles = {}; |
| 1443 | |
| 1444 | // Convert styles into a rule list |
| 1445 | each(settings.valid_styles, function(value, key) { |
| 1446 | validStyles[key] = tinymce.explode(value); |
| 1447 | }); |
| 1448 | } |
| 1449 | |
| 1450 | // Setup map objects |
| 1451 | whiteSpaceElementsMap = createLookupTable('whitespace_elements', 'pre script noscript style textarea'); |
| 1452 | selfClosingElementsMap = createLookupTable('self_closing_elements', 'colgroup dd dt li option p td tfoot th thead tr'); |
| 1453 | shortEndedElementsMap = createLookupTable('short_ended_elements', 'area base basefont br col frame hr img input isindex link meta param embed source wbr'); |
| 1454 | boolAttrMap = createLookupTable('boolean_attributes', 'checked compact declare defer disabled ismap multiple nohref noresize noshade nowrap readonly selected autoplay loop controls'); |
| 1455 | nonEmptyElementsMap = createLookupTable('non_empty_elements', 'td th iframe video audio object', shortEndedElementsMap); |
| 1456 | textBlockElementsMap = createLookupTable('text_block_elements', 'h1 h2 h3 h4 h5 h6 p div address pre form ' + |
| 1457 | 'blockquote center dir fieldset header footer article section hgroup aside nav figure'); |
| 1458 | blockElementsMap = createLookupTable('block_elements', 'hr table tbody thead tfoot ' + |
| 1459 | 'th tr td li ol ul caption dl dt dd noscript menu isindex samp option datalist select optgroup', textBlockElementsMap); |
| 1460 | |
| 1461 | // Converts a wildcard expression string to a regexp for example *a will become /.*a/. |
| 1462 | function patternToRegExp(str) { |
| 1463 | return new RegExp('^' + str.replace(/([?+*])/g, '.$1') + '$'); |
| 1464 | }; |
| 1465 | |
| 1466 | // Parses the specified valid_elements string and adds to the current rules |
| 1467 | // This function is a bit hard to read since it's heavily optimized for speed |
| 1468 | function addValidElements(valid_elements) { |
| 1469 | var ei, el, ai, al, yl, matches, element, attr, attrData, elementName, attrName, attrType, attributes, attributesOrder, |
| 1470 | prefix, outputName, globalAttributes, globalAttributesOrder, transElement, key, childKey, value, |
| 1471 | elementRuleRegExp = /^([#+\-])?([^\[\/]+)(?:\/([^\[]+))?(?:\[([^\]]+)\])?$/, |
| 1472 | attrRuleRegExp = /^([!\-])?(\w+::\w+|[^=:<]+)?(?:([=:<])(.*))?$/, |
| 1473 | hasPatternsRegExp = /[*?+]/; |
| 1474 | |
| 1475 | if (valid_elements) { |
| 1476 | // Split valid elements into an array with rules |
| 1477 | valid_elements = split(valid_elements); |
| 1478 | |
| 1479 | if (elements['@']) { |
| 1480 | globalAttributes = elements['@'].attributes; |
| 1481 | globalAttributesOrder = elements['@'].attributesOrder; |
| 1482 | } |
| 1483 | |
| 1484 | // Loop all rules |
| 1485 | for (ei = 0, el = valid_elements.length; ei < el; ei++) { |
| 1486 | // Parse element rule |
| 1487 | matches = elementRuleRegExp.exec(valid_elements[ei]); |
| 1488 | if (matches) { |
| 1489 | // Setup local names for matches |
| 1490 | prefix = matches[1]; |
| 1491 | elementName = matches[2]; |
| 1492 | outputName = matches[3]; |
| 1493 | attrData = matches[4]; |
| 1494 | |
| 1495 | // Create new attributes and attributesOrder |
| 1496 | attributes = {}; |
| 1497 | attributesOrder = []; |
| 1498 | |
| 1499 | // Create the new element |
| 1500 | element = { |
| 1501 | attributes : attributes, |
| 1502 | attributesOrder : attributesOrder |
| 1503 | }; |
| 1504 | |
| 1505 | // Padd empty elements prefix |
| 1506 | if (prefix === '#') |
| 1507 | element.paddEmpty = true; |
| 1508 | |
| 1509 | // Remove empty elements prefix |
| 1510 | if (prefix === '-') |
| 1511 | element.removeEmpty = true; |
| 1512 | |
| 1513 | // Copy attributes from global rule into current rule |
| 1514 | if (globalAttributes) { |
| 1515 | for (key in globalAttributes) |
| 1516 | attributes[key] = globalAttributes[key]; |
| 1517 | |
| 1518 | attributesOrder.push.apply(attributesOrder, globalAttributesOrder); |
| 1519 | } |
| 1520 | |
| 1521 | // Attributes defined |
| 1522 | if (attrData) { |
| 1523 | attrData = split(attrData, '|'); |
| 1524 | for (ai = 0, al = attrData.length; ai < al; ai++) { |
| 1525 | matches = attrRuleRegExp.exec(attrData[ai]); |
| 1526 | if (matches) { |
| 1527 | attr = {}; |
| 1528 | attrType = matches[1]; |
| 1529 | attrName = matches[2].replace(/::/g, ':'); |
| 1530 | prefix = matches[3]; |
| 1531 | value = matches[4]; |
| 1532 | |
| 1533 | // Required |
| 1534 | if (attrType === '!') { |
| 1535 | element.attributesRequired = element.attributesRequired || []; |
| 1536 | element.attributesRequired.push(attrName); |
| 1537 | attr.required = true; |
| 1538 | } |
| 1539 | |
| 1540 | // Denied from global |
| 1541 | if (attrType === '-') { |
| 1542 | delete attributes[attrName]; |
| 1543 | attributesOrder.splice(tinymce.inArray(attributesOrder, attrName), 1); |
| 1544 | continue; |
| 1545 | } |
| 1546 | |
| 1547 | // Default value |
| 1548 | if (prefix) { |
| 1549 | // Default value |
| 1550 | if (prefix === '=') { |
| 1551 | element.attributesDefault = element.attributesDefault || []; |
| 1552 | element.attributesDefault.push({name: attrName, value: value}); |
| 1553 | attr.defaultValue = value; |
| 1554 | } |
| 1555 | |
| 1556 | // Forced value |
| 1557 | if (prefix === ':') { |
| 1558 | element.attributesForced = element.attributesForced || []; |
| 1559 | element.attributesForced.push({name: attrName, value: value}); |
| 1560 | attr.forcedValue = value; |
| 1561 | } |
| 1562 | |
| 1563 | // Required values |
| 1564 | if (prefix === '<') |
| 1565 | attr.validValues = makeMap(value, '?'); |
| 1566 | } |
| 1567 | |
| 1568 | // Check for attribute patterns |
| 1569 | if (hasPatternsRegExp.test(attrName)) { |
| 1570 | element.attributePatterns = element.attributePatterns || []; |
| 1571 | attr.pattern = patternToRegExp(attrName); |
| 1572 | element.attributePatterns.push(attr); |
| 1573 | } else { |
| 1574 | // Add attribute to order list if it doesn't already exist |
| 1575 | if (!attributes[attrName]) |
| 1576 | attributesOrder.push(attrName); |
| 1577 | |
| 1578 | attributes[attrName] = attr; |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | // Global rule, store away these for later usage |
| 1585 | if (!globalAttributes && elementName == '@') { |
| 1586 | globalAttributes = attributes; |
| 1587 | globalAttributesOrder = attributesOrder; |
| 1588 | } |
| 1589 | |
| 1590 | // Handle substitute elements such as b/strong |
| 1591 | if (outputName) { |
| 1592 | element.outputName = elementName; |
| 1593 | elements[outputName] = element; |
| 1594 | } |
| 1595 | |
| 1596 | // Add pattern or exact element |
| 1597 | if (hasPatternsRegExp.test(elementName)) { |
| 1598 | element.pattern = patternToRegExp(elementName); |
| 1599 | patternElements.push(element); |
| 1600 | } else |
| 1601 | elements[elementName] = element; |
| 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | }; |
| 1606 | |
| 1607 | function setValidElements(valid_elements) { |
| 1608 | elements = {}; |
| 1609 | patternElements = []; |
| 1610 | |
| 1611 | addValidElements(valid_elements); |
| 1612 | |
| 1613 | each(schemaItems, function(element, name) { |
| 1614 | children[name] = element.children; |
| 1615 | }); |
| 1616 | }; |
| 1617 | |
| 1618 | // Adds custom non HTML elements to the schema |
| 1619 | function addCustomElements(custom_elements) { |
| 1620 | var customElementRegExp = /^(~)?(.+)$/; |
| 1621 | |
| 1622 | if (custom_elements) { |
| 1623 | each(split(custom_elements), function(rule) { |
| 1624 | var matches = customElementRegExp.exec(rule), |
| 1625 | inline = matches[1] === '~', |
| 1626 | cloneName = inline ? 'span' : 'div', |
| 1627 | name = matches[2]; |
| 1628 | |
| 1629 | children[name] = children[cloneName]; |
| 1630 | customElementsMap[name] = cloneName; |
| 1631 | |
| 1632 | // If it's not marked as inline then add it to valid block elements |
| 1633 | if (!inline) { |
| 1634 | blockElementsMap[name.toUpperCase()] = {}; |
| 1635 | blockElementsMap[name] = {}; |
| 1636 | } |
| 1637 | |
| 1638 | // Add elements clone if needed |
| 1639 | if (!elements[name]) { |
| 1640 | elements[name] = elements[cloneName]; |
| 1641 | } |
| 1642 | |
| 1643 | // Add custom elements at span/div positions |
| 1644 | each(children, function(element, child) { |
| 1645 | if (element[cloneName]) |
| 1646 | element[name] = element[cloneName]; |
| 1647 | }); |
| 1648 | }); |
| 1649 | } |
| 1650 | }; |
| 1651 | |
| 1652 | // Adds valid children to the schema object |
| 1653 | function addValidChildren(valid_children) { |
| 1654 | var childRuleRegExp = /^([+\-]?)(\w+)\[([^\]]+)\]$/; |
| 1655 | |
| 1656 | if (valid_children) { |
| 1657 | each(split(valid_children), function(rule) { |
| 1658 | var matches = childRuleRegExp.exec(rule), parent, prefix; |
| 1659 | |
| 1660 | if (matches) { |
| 1661 | prefix = matches[1]; |
| 1662 | |
| 1663 | // Add/remove items from default |
| 1664 | if (prefix) |
| 1665 | parent = children[matches[2]]; |
| 1666 | else |
| 1667 | parent = children[matches[2]] = {'#comment' : {}}; |
| 1668 | |
| 1669 | parent = children[matches[2]]; |
| 1670 | |
| 1671 | each(split(matches[3], '|'), function(child) { |
| 1672 | if (prefix === '-') |
| 1673 | delete parent[child]; |
| 1674 | else |
| 1675 | parent[child] = {}; |
| 1676 | }); |
| 1677 | } |
| 1678 | }); |
| 1679 | } |
| 1680 | }; |
| 1681 | |
| 1682 | function getElementRule(name) { |
| 1683 | var element = elements[name], i; |
| 1684 | |
| 1685 | // Exact match found |
| 1686 | if (element) |
| 1687 | return element; |
| 1688 | |
| 1689 | // No exact match then try the patterns |
| 1690 | i = patternElements.length; |
| 1691 | while (i--) { |
| 1692 | element = patternElements[i]; |
| 1693 | |
| 1694 | if (element.pattern.test(name)) |
| 1695 | return element; |
| 1696 | } |
| 1697 | }; |
| 1698 | |
| 1699 | if (!settings.valid_elements) { |
| 1700 | // No valid elements defined then clone the elements from the schema spec |
| 1701 | each(schemaItems, function(element, name) { |
| 1702 | elements[name] = { |
| 1703 | attributes : element.attributes, |
| 1704 | attributesOrder : element.attributesOrder |
| 1705 | }; |
| 1706 | |
| 1707 | children[name] = element.children; |
| 1708 | }); |
| 1709 | |
| 1710 | // Switch these on HTML4 |
| 1711 | if (settings.schema != "html5") { |
| 1712 | each(split('strong/b,em/i'), function(item) { |
| 1713 | item = split(item, '/'); |
| 1714 | elements[item[1]].outputName = item[0]; |
| 1715 | }); |
| 1716 | } |
| 1717 | |
| 1718 | // Add default alt attribute for images |
| 1719 | elements.img.attributesDefault = [{name: 'alt', value: ''}]; |
| 1720 | |
| 1721 | // Remove these if they are empty by default |
| 1722 | each(split('ol,ul,sub,sup,blockquote,span,font,a,table,tbody,tr,strong,em,b,i'), function(name) { |
| 1723 | if (elements[name]) { |
| 1724 | elements[name].removeEmpty = true; |
| 1725 | } |
| 1726 | }); |
| 1727 | |
| 1728 | // Padd these by default |
| 1729 | each(split('p,h1,h2,h3,h4,h5,h6,th,td,pre,div,address,caption'), function(name) { |
| 1730 | elements[name].paddEmpty = true; |
| 1731 | }); |
| 1732 | } else |
| 1733 | setValidElements(settings.valid_elements); |
| 1734 | |
| 1735 | addCustomElements(settings.custom_elements); |
| 1736 | addValidChildren(settings.valid_children); |
| 1737 | addValidElements(settings.extended_valid_elements); |
| 1738 | |
| 1739 | // Todo: Remove this when we fix list handling to be valid |
| 1740 | addValidChildren('+ol[ul|ol],+ul[ul|ol]'); |
| 1741 | |
| 1742 | // Delete invalid elements |
| 1743 | if (settings.invalid_elements) { |
| 1744 | tinymce.each(tinymce.explode(settings.invalid_elements), function(item) { |
| 1745 | if (elements[item]) |
| 1746 | delete elements[item]; |
| 1747 | }); |
| 1748 | } |
| 1749 | |
| 1750 | // If the user didn't allow span only allow internal spans |
| 1751 | if (!getElementRule('span')) |
| 1752 | addValidElements('span[!data-mce-type|*]'); |
| 1753 | |
| 1754 | /** |
| 1755 | * Name/value map object with valid parents and children to those parents. |
| 1756 | * |
| 1757 | * @example |
| 1758 | * children = { |
| 1759 | * div:{p:{}, h1:{}} |
| 1760 | * }; |
| 1761 | * @field children |
| 1762 | * @type {Object} |
| 1763 | */ |
| 1764 | self.children = children; |
| 1765 | |
| 1766 | /** |
| 1767 | * Name/value map object with valid styles for each element. |
| 1768 | * |
| 1769 | * @field styles |
| 1770 | * @type {Object} |
| 1771 | */ |
| 1772 | self.styles = validStyles; |
| 1773 | |
| 1774 | /** |
| 1775 | * Returns a map with boolean attributes. |
| 1776 | * |
| 1777 | * @method getBoolAttrs |
| 1778 | * @return {Object} Name/value lookup map for boolean attributes. |
| 1779 | */ |
| 1780 | self.getBoolAttrs = function() { |
| 1781 | return boolAttrMap; |
| 1782 | }; |
| 1783 | |
| 1784 | /** |
| 1785 | * Returns a map with block elements. |
| 1786 | * |
| 1787 | * @method getBlockElements |
| 1788 | * @return {Object} Name/value lookup map for block elements. |
| 1789 | */ |
| 1790 | self.getBlockElements = function() { |
| 1791 | return blockElementsMap; |
| 1792 | }; |
| 1793 | |
| 1794 | /** |
| 1795 | * Returns a map with text block elements. Such as: p,h1-h6,div,address |
| 1796 | * |
| 1797 | * @method getTextBlockElements |
| 1798 | * @return {Object} Name/value lookup map for block elements. |
| 1799 | */ |
| 1800 | self.getTextBlockElements = function() { |
| 1801 | return textBlockElementsMap; |
| 1802 | }; |
| 1803 | |
| 1804 | /** |
| 1805 | * Returns a map with short ended elements such as BR or IMG. |
| 1806 | * |
| 1807 | * @method getShortEndedElements |
| 1808 | * @return {Object} Name/value lookup map for short ended elements. |
| 1809 | */ |
| 1810 | self.getShortEndedElements = function() { |
| 1811 | return shortEndedElementsMap; |
| 1812 | }; |
| 1813 | |
| 1814 | /** |
| 1815 | * Returns a map with self closing tags such as <li>. |
| 1816 | * |
| 1817 | * @method getSelfClosingElements |
| 1818 | * @return {Object} Name/value lookup map for self closing tags elements. |
| 1819 | */ |
| 1820 | self.getSelfClosingElements = function() { |
| 1821 | return selfClosingElementsMap; |
| 1822 | }; |
| 1823 | |
| 1824 | /** |
| 1825 | * Returns a map with elements that should be treated as contents regardless if it has text |
| 1826 | * content in them or not such as TD, VIDEO or IMG. |
| 1827 | * |
| 1828 | * @method getNonEmptyElements |
| 1829 | * @return {Object} Name/value lookup map for non empty elements. |
| 1830 | */ |
| 1831 | self.getNonEmptyElements = function() { |
| 1832 | return nonEmptyElementsMap; |
| 1833 | }; |
| 1834 | |
| 1835 | /** |
| 1836 | * Returns a map with elements where white space is to be preserved like PRE or SCRIPT. |
| 1837 | * |
| 1838 | * @method getWhiteSpaceElements |
| 1839 | * @return {Object} Name/value lookup map for white space elements. |
| 1840 | */ |
| 1841 | self.getWhiteSpaceElements = function() { |
| 1842 | return whiteSpaceElementsMap; |
| 1843 | }; |
| 1844 | |
| 1845 | /** |
| 1846 | * Returns true/false if the specified element and it's child is valid or not |
| 1847 | * according to the schema. |
| 1848 | * |
| 1849 | * @method isValidChild |
| 1850 | * @param {String} name Element name to check for. |
| 1851 | * @param {String} child Element child to verify. |
| 1852 | * @return {Boolean} True/false if the element is a valid child of the specified parent. |
| 1853 | */ |
| 1854 | self.isValidChild = function(name, child) { |
| 1855 | var parent = children[name]; |
| 1856 | |
| 1857 | return !!(parent && parent[child]); |
| 1858 | }; |
| 1859 | |
| 1860 | /** |
| 1861 | * Returns true/false if the specified element name and optional attribute is |
| 1862 | * valid according to the schema. |
| 1863 | * |
| 1864 | * @method isValid |
| 1865 | * @param {String} name Name of element to check. |
| 1866 | * @param {String} attr Optional attribute name to check for. |
| 1867 | * @return {Boolean} True/false if the element and attribute is valid. |
| 1868 | */ |
| 1869 | self.isValid = function(name, attr) { |
| 1870 | var attrPatterns, i, rule = getElementRule(name); |
| 1871 | |
| 1872 | // Check if it's a valid element |
| 1873 | if (rule) { |
| 1874 | if (attr) { |
| 1875 | // Check if attribute name exists |
| 1876 | if (rule.attributes[attr]) { |
| 1877 | return true; |
| 1878 | } |
| 1879 | |
| 1880 | // Check if attribute matches a regexp pattern |
| 1881 | attrPatterns = rule.attributePatterns; |
| 1882 | if (attrPatterns) { |
| 1883 | i = attrPatterns.length; |
| 1884 | while (i--) { |
| 1885 | if (attrPatterns[i].pattern.test(name)) { |
| 1886 | return true; |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | } else { |
| 1891 | return true; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | // No match |
| 1896 | return false; |
| 1897 | }; |
| 1898 | |
| 1899 | /** |
| 1900 | * Returns true/false if the specified element is valid or not |
| 1901 | * according to the schema. |
| 1902 | * |
| 1903 | * @method getElementRule |
| 1904 | * @param {String} name Element name to check for. |
| 1905 | * @return {Object} Element object or undefined if the element isn't valid. |
| 1906 | */ |
| 1907 | self.getElementRule = getElementRule; |
| 1908 | |
| 1909 | /** |
| 1910 | * Returns an map object of all custom elements. |
| 1911 | * |
| 1912 | * @method getCustomElements |
| 1913 | * @return {Object} Name/value map object of all custom elements. |
| 1914 | */ |
| 1915 | self.getCustomElements = function() { |
| 1916 | return customElementsMap; |
| 1917 | }; |
| 1918 | |
| 1919 | /** |
| 1920 | * Parses a valid elements string and adds it to the schema. The valid elements format is for example "element[attr=default|otherattr]". |
| 1921 | * Existing rules will be replaced with the ones specified, so this extends the schema. |
| 1922 | * |
| 1923 | * @method addValidElements |
| 1924 | * @param {String} valid_elements String in the valid elements format to be parsed. |
| 1925 | */ |
| 1926 | self.addValidElements = addValidElements; |
| 1927 | |
| 1928 | /** |
| 1929 | * Parses a valid elements string and sets it to the schema. The valid elements format is for example "element[attr=default|otherattr]". |
| 1930 | * Existing rules will be replaced with the ones specified, so this extends the schema. |
| 1931 | * |
| 1932 | * @method setValidElements |
| 1933 | * @param {String} valid_elements String in the valid elements format to be parsed. |
| 1934 | */ |
| 1935 | self.setValidElements = setValidElements; |
| 1936 | |
| 1937 | /** |
| 1938 | * Adds custom non HTML elements to the schema. |
| 1939 | * |
| 1940 | * @method addCustomElements |
| 1941 | * @param {String} custom_elements Comma separated list of custom elements to add. |
| 1942 | */ |
| 1943 | self.addCustomElements = addCustomElements; |
| 1944 | |
| 1945 | /** |
| 1946 | * Parses a valid children string and adds them to the schema structure. The valid children format is for example: "element[child1|child2]". |
| 1947 | * |
| 1948 | * @method addValidChildren |
| 1949 | * @param {String} valid_children Valid children elements string to parse |
| 1950 | */ |
| 1951 | self.addValidChildren = addValidChildren; |
| 1952 | |
| 1953 | self.elements = elements; |
| 1954 | }; |
| 1955 | })(tinymce); |