| | 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 the result of a deep merge between the HTML5 |
| | 313 | * and HTML4 schemas. |
| | 314 | */ |
| | 315 | function getSaneSchema() { |
| | 316 | var cachedMapCache = mapCache, |
| | 317 | html5, html4; |
| | 318 | |
| | 319 | if ( mapCache.sane ) |
| | 320 | return mapCache.sane; |
| | 321 | |
| | 322 | // Bust the mapCache so we're not dealing with the other schema objects. |
| | 323 | mapCache = {}; |
| | 324 | html5 = getHTML5(); |
| | 325 | html4 = getHTML4(); |
| | 326 | mapCache = cachedMapCache; |
| | 327 | |
| | 328 | each( html4, function( html4settings, tag ) { |
| | 329 | var html5settings = html5[ tag ], |
| | 330 | difference = []; |
| | 331 | |
| | 332 | // Merge tags missing in HTML5 mode. |
| | 333 | if ( ! html5settings ) { |
| | 334 | html5[ tag ] = html4settings; |
| | 335 | return; |
| | 336 | } |
| | 337 | |
| | 338 | // Merge attributes missing from this HTML5 tag. |
| | 339 | each( html4settings.attributes, function( attribute, key ) { |
| | 340 | if ( ! html5settings.attributes[ key ] ) |
| | 341 | html5settings.attributes[ key ] = attribute; |
| | 342 | }); |
| | 343 | |
| | 344 | // Merge any missing attributes into the attributes order. |
| | 345 | each( html4settings.attributesOrder, function( key ) { |
| | 346 | if ( -1 === tinymce.inArray( html5settings.attributesOrder, key ) ) |
| | 347 | difference.push( key ); |
| | 348 | }); |
| | 349 | |
| | 350 | html5settings.attributesOrder = html5settings.attributesOrder.concat( difference ); |
| | 351 | |
| | 352 | // Merge children missing from this HTML5 tag. |
| | 353 | each( html4settings.children, function( child, key ) { |
| | 354 | if ( ! html5settings.children[ key ] ) |
| | 355 | html5settings.children[ key ] = child; |
| | 356 | }); |
| | 357 | }); |
| | 358 | |
| | 359 | return mapCache.sane = html5; |
| | 360 | } |
| | 361 | |
| | 362 | /** |
| | 363 | * Schema validator class. |
| | 364 | * |
| | 365 | * @class tinymce.html.Schema |
| | 366 | * @example |
| | 367 | * if (tinymce.activeEditor.schema.isValidChild('p', 'span')) |
| | 368 | * alert('span is valid child of p.'); |
| | 369 | * |
| | 370 | * if (tinymce.activeEditor.schema.getElementRule('p')) |
| | 371 | * alert('P is a valid element.'); |
| | 372 | * |
| | 373 | * @class tinymce.html.Schema |
| | 374 | * @version 3.4 |
| | 375 | */ |
| | 376 | |
| | 377 | /** |
| | 378 | * Constructs a new Schema instance. |
| | 379 | * |
| | 380 | * @constructor |
| | 381 | * @method Schema |
| | 382 | * @param {Object} settings Name/value settings object. |
| | 383 | */ |
| | 384 | tinymce.html.Schema = function(settings) { |
| | 385 | var self = this, elements = {}, children = {}, patternElements = [], validStyles, schemaItems; |
| | 386 | var whiteSpaceElementsMap, selfClosingElementsMap, shortEndedElementsMap, boolAttrMap, blockElementsMap, nonEmptyElementsMap, customElementsMap = {}; |
| | 387 | |
| | 388 | // Creates an lookup table map object for the specified option or the default value |
| | 389 | function createLookupTable(option, default_value, extend) { |
| | 390 | var value = settings[option]; |
| | 391 | |
| | 392 | if (!value) { |
| | 393 | // Get cached default map or make it if needed |
| | 394 | value = mapCache[option]; |
| | 395 | |
| | 396 | if (!value) { |
| | 397 | value = makeMap(default_value, ' ', makeMap(default_value.toUpperCase(), ' ')); |
| | 398 | value = tinymce.extend(value, extend); |
| | 399 | |
| | 400 | mapCache[option] = value; |
| | 401 | } |
| | 402 | } else { |
| | 403 | // Create custom map |
| | 404 | value = makeMap(value, ',', makeMap(value.toUpperCase(), ' ')); |
| | 405 | } |
| | 406 | |
| | 407 | return value; |
| | 408 | }; |
| | 409 | |
| | 410 | settings = settings || {}; |
| | 411 | |
| | 412 | /** |
| | 413 | * WordPress core uses a sane schema in place of the default "HTML5" schema. |
| | 414 | */ |
| | 415 | schemaItems = settings.schema == "html5" ? getSaneSchema() : getHTML4(); |
| | 416 | |
| | 417 | // Allow all elements and attributes if verify_html is set to false |
| | 418 | if (settings.verify_html === false) |
| | 419 | settings.valid_elements = '*[*]'; |
| | 420 | |
| | 421 | // Build styles list |
| | 422 | if (settings.valid_styles) { |
| | 423 | validStyles = {}; |
| | 424 | |
| | 425 | // Convert styles into a rule list |
| | 426 | each(settings.valid_styles, function(value, key) { |
| | 427 | validStyles[key] = tinymce.explode(value); |
| | 428 | }); |
| | 429 | } |
| | 430 | |
| | 431 | // Setup map objects |
| | 432 | whiteSpaceElementsMap = createLookupTable('whitespace_elements', 'pre script noscript style textarea'); |
| | 433 | selfClosingElementsMap = createLookupTable('self_closing_elements', 'colgroup dd dt li option p td tfoot th thead tr'); |
| | 434 | shortEndedElementsMap = createLookupTable('short_ended_elements', 'area base basefont br col frame hr img input isindex link meta param embed source wbr'); |
| | 435 | boolAttrMap = createLookupTable('boolean_attributes', 'checked compact declare defer disabled ismap multiple nohref noresize noshade nowrap readonly selected autoplay loop controls'); |
| | 436 | nonEmptyElementsMap = createLookupTable('non_empty_elements', 'td th iframe video audio object', shortEndedElementsMap); |
| | 437 | textBlockElementsMap = createLookupTable('text_block_elements', 'h1 h2 h3 h4 h5 h6 p div address pre form ' + |
| | 438 | 'blockquote center dir fieldset header footer article section hgroup aside nav figure'); |
| | 439 | blockElementsMap = createLookupTable('block_elements', 'hr table tbody thead tfoot ' + |
| | 440 | 'th tr td li ol ul caption dl dt dd noscript menu isindex samp option datalist select optgroup', textBlockElementsMap); |
| | 441 | |
| | 442 | // Converts a wildcard expression string to a regexp for example *a will become /.*a/. |
| | 443 | function patternToRegExp(str) { |
| | 444 | return new RegExp('^' + str.replace(/([?+*])/g, '.$1') + '$'); |
| | 445 | }; |
| | 446 | |
| | 447 | // Parses the specified valid_elements string and adds to the current rules |
| | 448 | // This function is a bit hard to read since it's heavily optimized for speed |
| | 449 | function addValidElements(valid_elements) { |
| | 450 | var ei, el, ai, al, yl, matches, element, attr, attrData, elementName, attrName, attrType, attributes, attributesOrder, |
| | 451 | prefix, outputName, globalAttributes, globalAttributesOrder, transElement, key, childKey, value, |
| | 452 | elementRuleRegExp = /^([#+\-])?([^\[\/]+)(?:\/([^\[]+))?(?:\[([^\]]+)\])?$/, |
| | 453 | attrRuleRegExp = /^([!\-])?(\w+::\w+|[^=:<]+)?(?:([=:<])(.*))?$/, |
| | 454 | hasPatternsRegExp = /[*?+]/; |
| | 455 | |
| | 456 | if (valid_elements) { |
| | 457 | // Split valid elements into an array with rules |
| | 458 | valid_elements = split(valid_elements); |
| | 459 | |
| | 460 | if (elements['@']) { |
| | 461 | globalAttributes = elements['@'].attributes; |
| | 462 | globalAttributesOrder = elements['@'].attributesOrder; |
| | 463 | } |
| | 464 | |
| | 465 | // Loop all rules |
| | 466 | for (ei = 0, el = valid_elements.length; ei < el; ei++) { |
| | 467 | // Parse element rule |
| | 468 | matches = elementRuleRegExp.exec(valid_elements[ei]); |
| | 469 | if (matches) { |
| | 470 | // Setup local names for matches |
| | 471 | prefix = matches[1]; |
| | 472 | elementName = matches[2]; |
| | 473 | outputName = matches[3]; |
| | 474 | attrData = matches[4]; |
| | 475 | |
| | 476 | // Create new attributes and attributesOrder |
| | 477 | attributes = {}; |
| | 478 | attributesOrder = []; |
| | 479 | |
| | 480 | // Create the new element |
| | 481 | element = { |
| | 482 | attributes : attributes, |
| | 483 | attributesOrder : attributesOrder |
| | 484 | }; |
| | 485 | |
| | 486 | // Padd empty elements prefix |
| | 487 | if (prefix === '#') |
| | 488 | element.paddEmpty = true; |
| | 489 | |
| | 490 | // Remove empty elements prefix |
| | 491 | if (prefix === '-') |
| | 492 | element.removeEmpty = true; |
| | 493 | |
| | 494 | // Copy attributes from global rule into current rule |
| | 495 | if (globalAttributes) { |
| | 496 | for (key in globalAttributes) |
| | 497 | attributes[key] = globalAttributes[key]; |
| | 498 | |
| | 499 | attributesOrder.push.apply(attributesOrder, globalAttributesOrder); |
| | 500 | } |
| | 501 | |
| | 502 | // Attributes defined |
| | 503 | if (attrData) { |
| | 504 | attrData = split(attrData, '|'); |
| | 505 | for (ai = 0, al = attrData.length; ai < al; ai++) { |
| | 506 | matches = attrRuleRegExp.exec(attrData[ai]); |
| | 507 | if (matches) { |
| | 508 | attr = {}; |
| | 509 | attrType = matches[1]; |
| | 510 | attrName = matches[2].replace(/::/g, ':'); |
| | 511 | prefix = matches[3]; |
| | 512 | value = matches[4]; |
| | 513 | |
| | 514 | // Required |
| | 515 | if (attrType === '!') { |
| | 516 | element.attributesRequired = element.attributesRequired || []; |
| | 517 | element.attributesRequired.push(attrName); |
| | 518 | attr.required = true; |
| | 519 | } |
| | 520 | |
| | 521 | // Denied from global |
| | 522 | if (attrType === '-') { |
| | 523 | delete attributes[attrName]; |
| | 524 | attributesOrder.splice(tinymce.inArray(attributesOrder, attrName), 1); |
| | 525 | continue; |
| | 526 | } |
| | 527 | |
| | 528 | // Default value |
| | 529 | if (prefix) { |
| | 530 | // Default value |
| | 531 | if (prefix === '=') { |
| | 532 | element.attributesDefault = element.attributesDefault || []; |
| | 533 | element.attributesDefault.push({name: attrName, value: value}); |
| | 534 | attr.defaultValue = value; |
| | 535 | } |
| | 536 | |
| | 537 | // Forced value |
| | 538 | if (prefix === ':') { |
| | 539 | element.attributesForced = element.attributesForced || []; |
| | 540 | element.attributesForced.push({name: attrName, value: value}); |
| | 541 | attr.forcedValue = value; |
| | 542 | } |
| | 543 | |
| | 544 | // Required values |
| | 545 | if (prefix === '<') |
| | 546 | attr.validValues = makeMap(value, '?'); |
| | 547 | } |
| | 548 | |
| | 549 | // Check for attribute patterns |
| | 550 | if (hasPatternsRegExp.test(attrName)) { |
| | 551 | element.attributePatterns = element.attributePatterns || []; |
| | 552 | attr.pattern = patternToRegExp(attrName); |
| | 553 | element.attributePatterns.push(attr); |
| | 554 | } else { |
| | 555 | // Add attribute to order list if it doesn't already exist |
| | 556 | if (!attributes[attrName]) |
| | 557 | attributesOrder.push(attrName); |
| | 558 | |
| | 559 | attributes[attrName] = attr; |
| | 560 | } |
| | 561 | } |
| | 562 | } |
| | 563 | } |
| | 564 | |
| | 565 | // Global rule, store away these for later usage |
| | 566 | if (!globalAttributes && elementName == '@') { |
| | 567 | globalAttributes = attributes; |
| | 568 | globalAttributesOrder = attributesOrder; |
| | 569 | } |
| | 570 | |
| | 571 | // Handle substitute elements such as b/strong |
| | 572 | if (outputName) { |
| | 573 | element.outputName = elementName; |
| | 574 | elements[outputName] = element; |
| | 575 | } |
| | 576 | |
| | 577 | // Add pattern or exact element |
| | 578 | if (hasPatternsRegExp.test(elementName)) { |
| | 579 | element.pattern = patternToRegExp(elementName); |
| | 580 | patternElements.push(element); |
| | 581 | } else |
| | 582 | elements[elementName] = element; |
| | 583 | } |
| | 584 | } |
| | 585 | } |
| | 586 | }; |
| | 587 | |
| | 588 | function setValidElements(valid_elements) { |
| | 589 | elements = {}; |
| | 590 | patternElements = []; |
| | 591 | |
| | 592 | addValidElements(valid_elements); |
| | 593 | |
| | 594 | each(schemaItems, function(element, name) { |
| | 595 | children[name] = element.children; |
| | 596 | }); |
| | 597 | }; |
| | 598 | |
| | 599 | // Adds custom non HTML elements to the schema |
| | 600 | function addCustomElements(custom_elements) { |
| | 601 | var customElementRegExp = /^(~)?(.+)$/; |
| | 602 | |
| | 603 | if (custom_elements) { |
| | 604 | each(split(custom_elements), function(rule) { |
| | 605 | var matches = customElementRegExp.exec(rule), |
| | 606 | inline = matches[1] === '~', |
| | 607 | cloneName = inline ? 'span' : 'div', |
| | 608 | name = matches[2]; |
| | 609 | |
| | 610 | children[name] = children[cloneName]; |
| | 611 | customElementsMap[name] = cloneName; |
| | 612 | |
| | 613 | // If it's not marked as inline then add it to valid block elements |
| | 614 | if (!inline) { |
| | 615 | blockElementsMap[name.toUpperCase()] = {}; |
| | 616 | blockElementsMap[name] = {}; |
| | 617 | } |
| | 618 | |
| | 619 | // Add elements clone if needed |
| | 620 | if (!elements[name]) { |
| | 621 | elements[name] = elements[cloneName]; |
| | 622 | } |
| | 623 | |
| | 624 | // Add custom elements at span/div positions |
| | 625 | each(children, function(element, child) { |
| | 626 | if (element[cloneName]) |
| | 627 | element[name] = element[cloneName]; |
| | 628 | }); |
| | 629 | }); |
| | 630 | } |
| | 631 | }; |
| | 632 | |
| | 633 | // Adds valid children to the schema object |
| | 634 | function addValidChildren(valid_children) { |
| | 635 | var childRuleRegExp = /^([+\-]?)(\w+)\[([^\]]+)\]$/; |
| | 636 | |
| | 637 | if (valid_children) { |
| | 638 | each(split(valid_children), function(rule) { |
| | 639 | var matches = childRuleRegExp.exec(rule), parent, prefix; |
| | 640 | |
| | 641 | if (matches) { |
| | 642 | prefix = matches[1]; |
| | 643 | |
| | 644 | // Add/remove items from default |
| | 645 | if (prefix) |
| | 646 | parent = children[matches[2]]; |
| | 647 | else |
| | 648 | parent = children[matches[2]] = {'#comment' : {}}; |
| | 649 | |
| | 650 | parent = children[matches[2]]; |
| | 651 | |
| | 652 | each(split(matches[3], '|'), function(child) { |
| | 653 | if (prefix === '-') |
| | 654 | delete parent[child]; |
| | 655 | else |
| | 656 | parent[child] = {}; |
| | 657 | }); |
| | 658 | } |
| | 659 | }); |
| | 660 | } |
| | 661 | }; |
| | 662 | |
| | 663 | function getElementRule(name) { |
| | 664 | var element = elements[name], i; |
| | 665 | |
| | 666 | // Exact match found |
| | 667 | if (element) |
| | 668 | return element; |
| | 669 | |
| | 670 | // No exact match then try the patterns |
| | 671 | i = patternElements.length; |
| | 672 | while (i--) { |
| | 673 | element = patternElements[i]; |
| | 674 | |
| | 675 | if (element.pattern.test(name)) |
| | 676 | return element; |
| | 677 | } |
| | 678 | }; |
| | 679 | |
| | 680 | if (!settings.valid_elements) { |
| | 681 | // No valid elements defined then clone the elements from the schema spec |
| | 682 | each(schemaItems, function(element, name) { |
| | 683 | elements[name] = { |
| | 684 | attributes : element.attributes, |
| | 685 | attributesOrder : element.attributesOrder |
| | 686 | }; |
| | 687 | |
| | 688 | children[name] = element.children; |
| | 689 | }); |
| | 690 | |
| | 691 | // Switch these on HTML4 |
| | 692 | if (settings.schema != "html5") { |
| | 693 | each(split('strong/b,em/i'), function(item) { |
| | 694 | item = split(item, '/'); |
| | 695 | elements[item[1]].outputName = item[0]; |
| | 696 | }); |
| | 697 | } |
| | 698 | |
| | 699 | // Add default alt attribute for images |
| | 700 | elements.img.attributesDefault = [{name: 'alt', value: ''}]; |
| | 701 | |
| | 702 | // Remove these if they are empty by default |
| | 703 | each(split('ol,ul,sub,sup,blockquote,span,font,a,table,tbody,tr,strong,em,b,i'), function(name) { |
| | 704 | if (elements[name]) { |
| | 705 | elements[name].removeEmpty = true; |
| | 706 | } |
| | 707 | }); |
| | 708 | |
| | 709 | // Padd these by default |
| | 710 | each(split('p,h1,h2,h3,h4,h5,h6,th,td,pre,div,address,caption'), function(name) { |
| | 711 | elements[name].paddEmpty = true; |
| | 712 | }); |
| | 713 | } else |
| | 714 | setValidElements(settings.valid_elements); |
| | 715 | |
| | 716 | addCustomElements(settings.custom_elements); |
| | 717 | addValidChildren(settings.valid_children); |
| | 718 | addValidElements(settings.extended_valid_elements); |
| | 719 | |
| | 720 | // Todo: Remove this when we fix list handling to be valid |
| | 721 | addValidChildren('+ol[ul|ol],+ul[ul|ol]'); |
| | 722 | |
| | 723 | // Delete invalid elements |
| | 724 | if (settings.invalid_elements) { |
| | 725 | tinymce.each(tinymce.explode(settings.invalid_elements), function(item) { |
| | 726 | if (elements[item]) |
| | 727 | delete elements[item]; |
| | 728 | }); |
| | 729 | } |
| | 730 | |
| | 731 | // If the user didn't allow span only allow internal spans |
| | 732 | if (!getElementRule('span')) |
| | 733 | addValidElements('span[!data-mce-type|*]'); |
| | 734 | |
| | 735 | /** |
| | 736 | * Name/value map object with valid parents and children to those parents. |
| | 737 | * |
| | 738 | * @example |
| | 739 | * children = { |
| | 740 | * div:{p:{}, h1:{}} |
| | 741 | * }; |
| | 742 | * @field children |
| | 743 | * @type {Object} |
| | 744 | */ |
| | 745 | self.children = children; |
| | 746 | |
| | 747 | /** |
| | 748 | * Name/value map object with valid styles for each element. |
| | 749 | * |
| | 750 | * @field styles |
| | 751 | * @type {Object} |
| | 752 | */ |
| | 753 | self.styles = validStyles; |
| | 754 | |
| | 755 | /** |
| | 756 | * Returns a map with boolean attributes. |
| | 757 | * |
| | 758 | * @method getBoolAttrs |
| | 759 | * @return {Object} Name/value lookup map for boolean attributes. |
| | 760 | */ |
| | 761 | self.getBoolAttrs = function() { |
| | 762 | return boolAttrMap; |
| | 763 | }; |
| | 764 | |
| | 765 | /** |
| | 766 | * Returns a map with block elements. |
| | 767 | * |
| | 768 | * @method getBlockElements |
| | 769 | * @return {Object} Name/value lookup map for block elements. |
| | 770 | */ |
| | 771 | self.getBlockElements = function() { |
| | 772 | return blockElementsMap; |
| | 773 | }; |
| | 774 | |
| | 775 | /** |
| | 776 | * Returns a map with text block elements. Such as: p,h1-h6,div,address |
| | 777 | * |
| | 778 | * @method getTextBlockElements |
| | 779 | * @return {Object} Name/value lookup map for block elements. |
| | 780 | */ |
| | 781 | self.getTextBlockElements = function() { |
| | 782 | return textBlockElementsMap; |
| | 783 | }; |
| | 784 | |
| | 785 | /** |
| | 786 | * Returns a map with short ended elements such as BR or IMG. |
| | 787 | * |
| | 788 | * @method getShortEndedElements |
| | 789 | * @return {Object} Name/value lookup map for short ended elements. |
| | 790 | */ |
| | 791 | self.getShortEndedElements = function() { |
| | 792 | return shortEndedElementsMap; |
| | 793 | }; |
| | 794 | |
| | 795 | /** |
| | 796 | * Returns a map with self closing tags such as <li>. |
| | 797 | * |
| | 798 | * @method getSelfClosingElements |
| | 799 | * @return {Object} Name/value lookup map for self closing tags elements. |
| | 800 | */ |
| | 801 | self.getSelfClosingElements = function() { |
| | 802 | return selfClosingElementsMap; |
| | 803 | }; |
| | 804 | |
| | 805 | /** |
| | 806 | * Returns a map with elements that should be treated as contents regardless if it has text |
| | 807 | * content in them or not such as TD, VIDEO or IMG. |
| | 808 | * |
| | 809 | * @method getNonEmptyElements |
| | 810 | * @return {Object} Name/value lookup map for non empty elements. |
| | 811 | */ |
| | 812 | self.getNonEmptyElements = function() { |
| | 813 | return nonEmptyElementsMap; |
| | 814 | }; |
| | 815 | |
| | 816 | /** |
| | 817 | * Returns a map with elements where white space is to be preserved like PRE or SCRIPT. |
| | 818 | * |
| | 819 | * @method getWhiteSpaceElements |
| | 820 | * @return {Object} Name/value lookup map for white space elements. |
| | 821 | */ |
| | 822 | self.getWhiteSpaceElements = function() { |
| | 823 | return whiteSpaceElementsMap; |
| | 824 | }; |
| | 825 | |
| | 826 | /** |
| | 827 | * Returns true/false if the specified element and it's child is valid or not |
| | 828 | * according to the schema. |
| | 829 | * |
| | 830 | * @method isValidChild |
| | 831 | * @param {String} name Element name to check for. |
| | 832 | * @param {String} child Element child to verify. |
| | 833 | * @return {Boolean} True/false if the element is a valid child of the specified parent. |
| | 834 | */ |
| | 835 | self.isValidChild = function(name, child) { |
| | 836 | var parent = children[name]; |
| | 837 | |
| | 838 | return !!(parent && parent[child]); |
| | 839 | }; |
| | 840 | |
| | 841 | /** |
| | 842 | * Returns true/false if the specified element name and optional attribute is |
| | 843 | * valid according to the schema. |
| | 844 | * |
| | 845 | * @method isValid |
| | 846 | * @param {String} name Name of element to check. |
| | 847 | * @param {String} attr Optional attribute name to check for. |
| | 848 | * @return {Boolean} True/false if the element and attribute is valid. |
| | 849 | */ |
| | 850 | self.isValid = function(name, attr) { |
| | 851 | var attrPatterns, i, rule = getElementRule(name); |
| | 852 | |
| | 853 | // Check if it's a valid element |
| | 854 | if (rule) { |
| | 855 | if (attr) { |
| | 856 | // Check if attribute name exists |
| | 857 | if (rule.attributes[attr]) { |
| | 858 | return true; |
| | 859 | } |
| | 860 | |
| | 861 | // Check if attribute matches a regexp pattern |
| | 862 | attrPatterns = rule.attributePatterns; |
| | 863 | if (attrPatterns) { |
| | 864 | i = attrPatterns.length; |
| | 865 | while (i--) { |
| | 866 | if (attrPatterns[i].pattern.test(name)) { |
| | 867 | return true; |
| | 868 | } |
| | 869 | } |
| | 870 | } |
| | 871 | } else { |
| | 872 | return true; |
| | 873 | } |
| | 874 | } |
| | 875 | |
| | 876 | // No match |
| | 877 | return false; |
| | 878 | }; |
| | 879 | |
| | 880 | /** |
| | 881 | * Returns true/false if the specified element is valid or not |
| | 882 | * according to the schema. |
| | 883 | * |
| | 884 | * @method getElementRule |
| | 885 | * @param {String} name Element name to check for. |
| | 886 | * @return {Object} Element object or undefined if the element isn't valid. |
| | 887 | */ |
| | 888 | self.getElementRule = getElementRule; |
| | 889 | |
| | 890 | /** |
| | 891 | * Returns an map object of all custom elements. |
| | 892 | * |
| | 893 | * @method getCustomElements |
| | 894 | * @return {Object} Name/value map object of all custom elements. |
| | 895 | */ |
| | 896 | self.getCustomElements = function() { |
| | 897 | return customElementsMap; |
| | 898 | }; |
| | 899 | |
| | 900 | /** |
| | 901 | * Parses a valid elements string and adds it to the schema. The valid elements format is for example "element[attr=default|otherattr]". |
| | 902 | * Existing rules will be replaced with the ones specified, so this extends the schema. |
| | 903 | * |
| | 904 | * @method addValidElements |
| | 905 | * @param {String} valid_elements String in the valid elements format to be parsed. |
| | 906 | */ |
| | 907 | self.addValidElements = addValidElements; |
| | 908 | |
| | 909 | /** |
| | 910 | * Parses a valid elements string and sets it to the schema. The valid elements format is for example "element[attr=default|otherattr]". |
| | 911 | * Existing rules will be replaced with the ones specified, so this extends the schema. |
| | 912 | * |
| | 913 | * @method setValidElements |
| | 914 | * @param {String} valid_elements String in the valid elements format to be parsed. |
| | 915 | */ |
| | 916 | self.setValidElements = setValidElements; |
| | 917 | |
| | 918 | /** |
| | 919 | * Adds custom non HTML elements to the schema. |
| | 920 | * |
| | 921 | * @method addCustomElements |
| | 922 | * @param {String} custom_elements Comma separated list of custom elements to add. |
| | 923 | */ |
| | 924 | self.addCustomElements = addCustomElements; |
| | 925 | |
| | 926 | /** |
| | 927 | * Parses a valid children string and adds them to the schema structure. The valid children format is for example: "element[child1|child2]". |
| | 928 | * |
| | 929 | * @method addValidChildren |
| | 930 | * @param {String} valid_children Valid children elements string to parse |
| | 931 | */ |
| | 932 | self.addValidChildren = addValidChildren; |
| | 933 | |
| | 934 | self.elements = elements; |
| | 935 | }; |
| | 936 | })(tinymce); |
| | 937 | No newline at end of file |