]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/wp-tinymce-schema.js
Wordpress 3.6
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / wp-tinymce-schema.js
1 /**
2  * TinyMCE Schema.js
3  *
4  * Duck-punched by WordPress core to support a sane schema superset.
5  *
6  * Copyright, Moxiecode Systems AB
7  * Released under LGPL
8  *
9  * License: http://www.tinymce.com/license
10  * Contributing: http://www.tinymce.com/contributing
11  */
12
13 (function(tinymce) {
14         var mapCache = {}, makeMap = tinymce.makeMap, each = tinymce.each;
15
16         function split(str, delim) {
17                 return str.split(delim || ',');
18         };
19
20         /**
21          * Unpacks the specified lookup and string data it will also parse it into an object
22          * map with sub object for it's children. This will later also include the attributes.
23          */
24         function unpack(lookup, data) {
25                 var key, elements = {};
26
27                 function replace(value) {
28                         return value.replace(/[A-Z]+/g, function(key) {
29                                 return replace(lookup[key]);
30                         });
31                 };
32
33                 // Unpack lookup
34                 for (key in lookup) {
35                         if (lookup.hasOwnProperty(key))
36                                 lookup[key] = replace(lookup[key]);
37                 }
38
39                 // Unpack and parse data into object map
40                 replace(data).replace(/#/g, '#text').replace(/(\w+)\[([^\]]+)\]\[([^\]]*)\]/g, function(str, name, attributes, children) {
41                         attributes = split(attributes, '|');
42
43                         elements[name] = {
44                                 attributes : makeMap(attributes),
45                                 attributesOrder : attributes,
46                                 children : makeMap(children, '|', {'#comment' : {}})
47                         }
48                 });
49
50                 return elements;
51         };
52
53         /**
54          * Returns the HTML5 schema and caches it in the mapCache.
55          */
56         function getHTML5() {
57                 var html5 = mapCache.html5;
58
59                 if (!html5) {
60                         html5 = mapCache.html5 = unpack({
61                                         A : 'accesskey|class|contextmenu|dir|draggable|dropzone|hidden|id|inert|itemid|itemprop|itemref|itemscope|itemtype|lang|spellcheck|style|tabindex|title|translate|item|role|subject|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup',
62                                         B : '#|a|abbr|area|audio|b|bdi|bdo|br|button|canvas|cite|code|command|data|datalist|del|dfn|em|embed|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|math|meta|meter|noscript|object|output|progress|q|ruby|s|samp|script|select|small|span|strong|sub|sup|svg|textarea|time|u|var|video|wbr',
63                                         C : '#|a|abbr|area|address|article|aside|audio|b|bdi|bdo|blockquote|br|button|canvas|cite|code|command|data|datalist|del|details|dfn|dialog|div|dl|em|embed|fieldset|figure|footer|form|h1|h2|h3|h4|h5|h6|header|hgroup|hr|i|iframe|img|input|ins|kbd|keygen|label|link|map|mark|math|menu|meta|meter|nav|noscript|ol|object|output|p|pre|progress|q|ruby|s|samp|script|section|select|small|span|strong|style|sub|sup|svg|table|textarea|time|u|ul|var|video|wbr'
64                                 }, 'html[A|manifest][body|head]' +
65                                         'head[A][base|command|link|meta|noscript|script|style|title]' +
66                                         'title[A][#]' +
67                                         'base[A|href|target][]' +
68                                         'link[A|href|rel|media|type|sizes|crossorigin|hreflang][]' +
69                                         'meta[A|http-equiv|name|content|charset][]' +
70                                         'style[A|type|media|scoped][#]' +
71                                         'script[A|charset|type|src|defer|async|crossorigin][#]' +
72                                         'noscript[A][C]' +
73                                         'body[A|onafterprint|onbeforeprint|onbeforeunload|onblur|onerror|onfocus|onfullscreenchange|onfullscreenerror|onhashchange|onload|onmessage|onoffline|ononline|onpagehide|onpageshow|onpopstate|onresize|onscroll|onstorage|onunload][C]' +
74                                         'section[A][C]' +
75                                         'nav[A][C]' +
76                                         'article[A][C]' +
77                                         'aside[A][C]' +
78                                         'h1[A][B]' +
79                                         'h2[A][B]' +
80                                         'h3[A][B]' +
81                                         'h4[A][B]' +
82                                         'h5[A][B]' +
83                                         'h6[A][B]' +
84                                         'hgroup[A][h1|h2|h3|h4|h5|h6]' +
85                                         'header[A][C]' +
86                                         'footer[A][C]' +
87                                         'address[A][C]' +
88                                         'p[A][B]' +
89                                         'br[A][]' +
90                                         'pre[A][B]' +
91                                         'dialog[A|open][C|dd|dt]' +
92                                         'blockquote[A|cite][C]' +
93                                         'ol[A|start|reversed][li]' +
94                                         'ul[A][li]' +
95                                         'li[A|value][C]' +
96                                         'dl[A][dd|dt]' +
97                                         'dt[A][C|B]' +
98                                         'dd[A][C]' +
99                                         'a[A|href|target|download|ping|rel|media|type][B]' +
100                                         'em[A][B]' +
101                                         'strong[A][B]' +
102                                         'small[A][B]' +
103                                         's[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                                         'u[A][B]' +
117                                         'mark[A][B]' +
118                                         'progress[A|value|max][B]' +
119                                         'meter[A|value|min|max|low|high|optimum][B]' +
120                                         'time[A|datetime][B]' +
121                                         'ruby[A][B|rt|rp]' +
122                                         'rt[A][B]' +
123                                         'rp[A][B]' +
124                                         'bdi[A][B]' +
125                                         'bdo[A][B]' +
126                                         'span[A][B]' +
127                                         'ins[A|cite|datetime][C|B]' +
128                                         'del[A|cite|datetime][C|B]' +
129                                         'figure[A][C|legend|figcaption]' +
130                                         'figcaption[A][C]' +
131                                         'img[A|alt|src|srcset|crossorigin|usemap|ismap|width|height][]' +
132                                         'iframe[A|name|src|srcdoc|height|width|sandbox|seamless|allowfullscreen][C|B]' +
133                                         'embed[A|src|height|width|type][]' +
134                                         'object[A|data|type|typemustmatch|name|usemap|form|width|height][C|B|param]' +
135                                         'param[A|name|value][]' +
136                                         'summary[A][B]' +
137                                         'details[A|open][C|legend|summary]' +
138                                         'command[A|type|label|icon|disabled|checked|radiogroup|command][]' +
139                                         'menu[A|type|label][C|li]' +
140                                         'legend[A][C|B]' +
141                                         'div[A][C]' +
142                                         'source[A|src|type|media][]' +
143                                         'track[A|kind|src|srclang|label|default][]' +
144                                         'audio[A|src|autobuffer|autoplay|loop|controls|crossorigin|preload|mediagroup|muted][C|source|track]' +
145                                         'video[A|src|autobuffer|autoplay|loop|controls|width|height|poster|crossorigin|preload|mediagroup|muted][C|source|track]' +
146                                         'hr[A][]' +
147                                         'form[A|accept-charset|action|autocomplete|enctype|method|name|novalidate|target][C]' +
148                                         'fieldset[A|disabled|form|name][C|legend]' +
149                                         'label[A|form|for][B]' +
150                                         'input[A|type|accept|alt|autocomplete|autofocus|checked|dirname|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|height|inputmode|list|max|maxlength|min|multiple|name|pattern|placeholder|readonly|required|size|src|step|value|width|files][]' +
151                                         'button[A|autofocus|disabled|form|formaction|formenctype|formmethod|formnovalidate|formtarget|name|type|value][B]' +
152                                         'select[A|autofocus|disabled|form|multiple|name|required|size][option|optgroup]' +
153                                         'data[A|value][B]' +
154                                         'datalist[A][B|option]' +
155                                         'optgroup[A|disabled|label][option]' +
156                                         'option[A|disabled|selected|label|value][#]' +
157                                         'textarea[A|autocomplete|autofocus|cols|dirname|disabled|form|inputmode|maxlength|name|placeholder|readonly|required|rows|wrap][#]' +
158                                         'keygen[A|autofocus|challenge|disabled|form|keytype|name][]' +
159                                         'output[A|for|form|name][B]' +
160                                         'canvas[A|width|height][a|button|input]' +
161                                         'map[A|name][C|B]' +
162                                         'area[A|alt|coords|shape|href|target|download|ping|rel|media|hreflang|type][]' +
163                                         'math[A][]' +
164                                         'svg[A][]' +
165                                         'table[A][caption|colgroup|thead|tfoot|tbody|tr]' +
166                                         'caption[A][C]' +
167                                         'colgroup[A|span][col]' +
168                                         'col[A|span][]' +
169                                         'thead[A][tr]' +
170                                         'tfoot[A][tr]' +
171                                         'tbody[A][tr]' +
172                                         'tr[A][th|td]' +
173                                         'th[A|headers|rowspan|colspan|scope][C]' +
174                                         'td[A|headers|rowspan|colspan][C]' +
175                                         'wbr[A][]'
176                         );
177                 }
178
179                 return html5;
180         };
181
182         /**
183          * Returns the HTML4 schema and caches it in the mapCache.
184          */
185         function getHTML4() {
186                 var html4 = mapCache.html4;
187
188                 if (!html4) {
189                         // This is the XHTML 1.0 transitional elements with it's attributes and children packed to reduce it's size
190                         html4 = mapCache.html4 = unpack({
191                                 Z : 'H|K|N|O|P',
192                                 Y : 'X|form|R|Q',
193                                 ZG : 'E|span|width|align|char|charoff|valign',
194                                 X : 'p|T|div|U|W|isindex|fieldset|table',
195                                 ZF : 'E|align|char|charoff|valign',
196                                 W : 'pre|hr|blockquote|address|center|noframes',
197                                 ZE : 'abbr|axis|headers|scope|rowspan|colspan|align|char|charoff|valign|nowrap|bgcolor|width|height',
198                                 ZD : '[E][S]',
199                                 U : 'ul|ol|dl|menu|dir',
200                                 ZC : 'p|Y|div|U|W|table|br|span|bdo|object|applet|img|map|K|N|Q',
201                                 T : 'h1|h2|h3|h4|h5|h6',
202                                 ZB : 'X|S|Q',
203                                 S : 'R|P',
204                                 ZA : 'a|G|J|M|O|P',
205                                 R : 'a|H|K|N|O',
206                                 Q : 'noscript|P',
207                                 P : 'ins|del|script',
208                                 O : 'input|select|textarea|label|button',
209                                 N : 'M|L',
210                                 M : 'em|strong|dfn|code|q|samp|kbd|var|cite|abbr|acronym',
211                                 L : 'sub|sup',
212                                 K : 'J|I',
213                                 J : 'tt|i|b|u|s|strike',
214                                 I : 'big|small|font|basefont',
215                                 H : 'G|F',
216                                 G : 'br|span|bdo',
217                                 F : 'object|applet|img|map|iframe',
218                                 E : 'A|B|C',
219                                 D : 'accesskey|tabindex|onfocus|onblur',
220                                 C : 'onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup',
221                                 B : 'lang|xml:lang|dir',
222                                 A : 'id|class|style|title'
223                         }, 'script[id|charset|type|language|src|defer|xml:space][]' +
224                                 'style[B|id|type|media|title|xml:space][]' +
225                                 'object[E|declare|classid|codebase|data|type|codetype|archive|standby|width|height|usemap|name|tabindex|align|border|hspace|vspace][#|param|Y]' +
226                                 'param[id|name|value|valuetype|type][]' +
227                                 'p[E|align][#|S]' +
228                                 'a[E|D|charset|type|name|href|hreflang|rel|rev|shape|coords|target][#|Z]' +
229                                 'br[A|clear][]' +
230                                 'span[E][#|S]' +
231                                 'bdo[A|C|B][#|S]' +
232                                 'applet[A|codebase|archive|code|object|alt|name|width|height|align|hspace|vspace][#|param|Y]' +
233                                 'h1[E|align][#|S]' +
234                                 'img[E|src|alt|name|longdesc|width|height|usemap|ismap|align|border|hspace|vspace][]' +
235                                 'map[B|C|A|name][X|form|Q|area]' +
236                                 'h2[E|align][#|S]' +
237                                 'iframe[A|longdesc|name|src|frameborder|marginwidth|marginheight|scrolling|align|width|height][#|Y]' +
238                                 'h3[E|align][#|S]' +
239                                 'tt[E][#|S]' +
240                                 'i[E][#|S]' +
241                                 'b[E][#|S]' +
242                                 'u[E][#|S]' +
243                                 's[E][#|S]' +
244                                 'strike[E][#|S]' +
245                                 'big[E][#|S]' +
246                                 'small[E][#|S]' +
247                                 'font[A|B|size|color|face][#|S]' +
248                                 'basefont[id|size|color|face][]' +
249                                 'em[E][#|S]' +
250                                 'strong[E][#|S]' +
251                                 'dfn[E][#|S]' +
252                                 'code[E][#|S]' +
253                                 'q[E|cite][#|S]' +
254                                 'samp[E][#|S]' +
255                                 'kbd[E][#|S]' +
256                                 'var[E][#|S]' +
257                                 'cite[E][#|S]' +
258                                 'abbr[E][#|S]' +
259                                 'acronym[E][#|S]' +
260                                 'sub[E][#|S]' +
261                                 'sup[E][#|S]' +
262                                 'input[E|D|type|name|value|checked|disabled|readonly|size|maxlength|src|alt|usemap|onselect|onchange|accept|align][]' +
263                                 'select[E|name|size|multiple|disabled|tabindex|onfocus|onblur|onchange][optgroup|option]' +
264                                 'optgroup[E|disabled|label][option]' +
265                                 'option[E|selected|disabled|label|value][]' +
266                                 'textarea[E|D|name|rows|cols|disabled|readonly|onselect|onchange][]' +
267                                 'label[E|for|accesskey|onfocus|onblur][#|S]' +
268                                 'button[E|D|name|value|type|disabled][#|p|T|div|U|W|table|G|object|applet|img|map|K|N|Q]' +
269                                 'h4[E|align][#|S]' +
270                                 'ins[E|cite|datetime][#|Y]' +
271                                 'h5[E|align][#|S]' +
272                                 'del[E|cite|datetime][#|Y]' +
273                                 'h6[E|align][#|S]' +
274                                 'div[E|align][#|Y]' +
275                                 'ul[E|type|compact][li]' +
276                                 'li[E|type|value][#|Y]' +
277                                 'ol[E|type|compact|start][li]' +
278                                 'dl[E|compact][dt|dd]' +
279                                 'dt[E][#|S]' +
280                                 'dd[E][#|Y]' +
281                                 'menu[E|compact][li]' +
282                                 'dir[E|compact][li]' +
283                                 'pre[E|width|xml:space][#|ZA]' +
284                                 'hr[E|align|noshade|size|width][]' +
285                                 'blockquote[E|cite][#|Y]' +
286                                 'address[E][#|S|p]' +
287                                 'center[E][#|Y]' +
288                                 'noframes[E][#|Y]' +
289                                 'isindex[A|B|prompt][]' +
290                                 'fieldset[E][#|legend|Y]' +
291                                 'legend[E|accesskey|align][#|S]' +
292                                 'table[E|summary|width|border|frame|rules|cellspacing|cellpadding|align|bgcolor][caption|col|colgroup|thead|tfoot|tbody|tr]' +
293                                 'caption[E|align][#|S]' +
294                                 'col[ZG][]' +
295                                 'colgroup[ZG][col]' +
296                                 'thead[ZF][tr]' +
297                                 'tr[ZF|bgcolor][th|td]' +
298                                 'th[E|ZE][#|Y]' +
299                                 'form[E|action|method|name|enctype|onsubmit|onreset|accept|accept-charset|target][#|X|R|Q]' +
300                                 'noscript[E][#|Y]' +
301                                 'td[E|ZE][#|Y]' +
302                                 'tfoot[ZF][tr]' +
303                                 'tbody[ZF][tr]' +
304                                 'area[E|D|shape|coords|href|nohref|alt|target][]' +
305                                 'base[id|href|target][]' +
306                                 'body[E|onload|onunload|background|bgcolor|text|link|vlink|alink][#|Y]'
307                         );
308                 }
309
310                 return html4;
311         };
312
313         /**
314          * WordPress Core
315          *
316          * Returns a schema that is the result of a deep merge between the HTML5
317          * and HTML4 schemas.
318          */
319         function getSaneSchema() {
320                 var cachedMapCache = mapCache,
321                         html5, html4;
322
323                 if ( mapCache.sane )
324                         return mapCache.sane;
325
326                 // Bust the mapCache so we're not dealing with the other schema objects.
327                 mapCache = {};
328                 html5 = getHTML5();
329                 html4 = getHTML4();
330                 mapCache = cachedMapCache;
331
332                 each( html4, function( html4settings, tag ) {
333                         var html5settings = html5[ tag ],
334                                 difference = [];
335
336                         // Merge tags missing in HTML5 mode.
337                         if ( ! html5settings ) {
338                                 html5[ tag ] = html4settings;
339                                 return;
340                         }
341
342                         // Merge attributes missing from this HTML5 tag.
343                         each( html4settings.attributes, function( attribute, key ) {
344                                 if ( ! html5settings.attributes[ key ] )
345                                         html5settings.attributes[ key ] = attribute;
346                         });
347
348                         // Merge any missing attributes into the attributes order.
349                         each( html4settings.attributesOrder, function( key ) {
350                                 if ( -1 === tinymce.inArray( html5settings.attributesOrder, key ) )
351                                         difference.push( key );
352                         });
353
354                         html5settings.attributesOrder = html5settings.attributesOrder.concat( difference );
355
356                         // Merge children missing from this HTML5 tag.
357                         each( html4settings.children, function( child, key ) {
358                                 if ( ! html5settings.children[ key ] )
359                                         html5settings.children[ key ] = child;
360                         });
361                 });
362
363                 return mapCache.sane = html5;
364         }
365
366         /**
367          * Schema validator class.
368          *
369          * @class tinymce.html.Schema
370          * @example
371          *  if (tinymce.activeEditor.schema.isValidChild('p', 'span'))
372          *    alert('span is valid child of p.');
373          *
374          *  if (tinymce.activeEditor.schema.getElementRule('p'))
375          *    alert('P is a valid element.');
376          *
377          * @class tinymce.html.Schema
378          * @version 3.4
379          */
380
381         /**
382          * Constructs a new Schema instance.
383          *
384          * @constructor
385          * @method Schema
386          * @param {Object} settings Name/value settings object.
387          */
388         tinymce.html.Schema = function(settings) {
389                 var self = this, elements = {}, children = {}, patternElements = [], validStyles, schemaItems;
390                 var whiteSpaceElementsMap, selfClosingElementsMap, shortEndedElementsMap, boolAttrMap, blockElementsMap, nonEmptyElementsMap, customElementsMap = {};
391
392                 // Creates an lookup table map object for the specified option or the default value
393                 function createLookupTable(option, default_value, extend) {
394                         var value = settings[option];
395
396                         if (!value) {
397                                 // Get cached default map or make it if needed
398                                 value = mapCache[option];
399
400                                 if (!value) {
401                                         value = makeMap(default_value, ' ', makeMap(default_value.toUpperCase(), ' '));
402                                         value = tinymce.extend(value, extend);
403
404                                         mapCache[option] = value;
405                                 }
406                         } else {
407                                 // Create custom map
408                                 value = makeMap(value, ',', makeMap(value.toUpperCase(), ' '));
409                         }
410
411                         return value;
412                 };
413
414                 settings = settings || {};
415
416                 /**
417                  * WordPress core uses a sane schema in place of the default "HTML5" schema.
418                  */
419                 schemaItems = settings.schema == "html5" ? getSaneSchema() : getHTML4();
420
421                 // Allow all elements and attributes if verify_html is set to false
422                 if (settings.verify_html === false)
423                         settings.valid_elements = '*[*]';
424
425                 // Build styles list
426                 if (settings.valid_styles) {
427                         validStyles = {};
428
429                         // Convert styles into a rule list
430                         each(settings.valid_styles, function(value, key) {
431                                 validStyles[key] = tinymce.explode(value);
432                         });
433                 }
434
435                 // Setup map objects
436                 whiteSpaceElementsMap = createLookupTable('whitespace_elements', 'pre script noscript style textarea');
437                 selfClosingElementsMap = createLookupTable('self_closing_elements', 'colgroup dd dt li option p td tfoot th thead tr');
438                 shortEndedElementsMap = createLookupTable('short_ended_elements', 'area base basefont br col frame hr img input isindex link meta param embed source wbr');
439                 boolAttrMap = createLookupTable('boolean_attributes', 'checked compact declare defer disabled ismap multiple nohref noresize noshade nowrap readonly selected autoplay loop controls');
440                 nonEmptyElementsMap = createLookupTable('non_empty_elements', 'td th iframe video audio object', shortEndedElementsMap);
441                 textBlockElementsMap = createLookupTable('text_block_elements', 'h1 h2 h3 h4 h5 h6 p div address pre form ' +
442                                                 'blockquote center dir fieldset header footer article section hgroup aside nav figure');
443                 blockElementsMap = createLookupTable('block_elements', 'hr table tbody thead tfoot ' +
444                                                 'th tr td li ol ul caption dl dt dd noscript menu isindex option datalist select optgroup', textBlockElementsMap);
445
446                 // Converts a wildcard expression string to a regexp for example *a will become /.*a/.
447                 function patternToRegExp(str) {
448                         return new RegExp('^' + str.replace(/([?+*])/g, '.$1') + '$');
449                 };
450
451                 // Parses the specified valid_elements string and adds to the current rules
452                 // This function is a bit hard to read since it's heavily optimized for speed
453                 function addValidElements(valid_elements) {
454                         var ei, el, ai, al, yl, matches, element, attr, attrData, elementName, attrName, attrType, attributes, attributesOrder,
455                                 prefix, outputName, globalAttributes, globalAttributesOrder, transElement, key, childKey, value,
456                                 elementRuleRegExp = /^([#+\-])?([^\[\/]+)(?:\/([^\[]+))?(?:\[([^\]]+)\])?$/,
457                                 attrRuleRegExp = /^([!\-])?(\w+::\w+|[^=:<]+)?(?:([=:<])(.*))?$/,
458                                 hasPatternsRegExp = /[*?+]/;
459
460                         if (valid_elements) {
461                                 // Split valid elements into an array with rules
462                                 valid_elements = split(valid_elements);
463
464                                 if (elements['@']) {
465                                         globalAttributes = elements['@'].attributes;
466                                         globalAttributesOrder = elements['@'].attributesOrder;
467                                 }
468
469                                 // Loop all rules
470                                 for (ei = 0, el = valid_elements.length; ei < el; ei++) {
471                                         // Parse element rule
472                                         matches = elementRuleRegExp.exec(valid_elements[ei]);
473                                         if (matches) {
474                                                 // Setup local names for matches
475                                                 prefix = matches[1];
476                                                 elementName = matches[2];
477                                                 outputName = matches[3];
478                                                 attrData = matches[4];
479
480                                                 // Create new attributes and attributesOrder
481                                                 attributes = {};
482                                                 attributesOrder = [];
483
484                                                 // Create the new element
485                                                 element = {
486                                                         attributes : attributes,
487                                                         attributesOrder : attributesOrder
488                                                 };
489
490                                                 // Padd empty elements prefix
491                                                 if (prefix === '#')
492                                                         element.paddEmpty = true;
493
494                                                 // Remove empty elements prefix
495                                                 if (prefix === '-')
496                                                         element.removeEmpty = true;
497
498                                                 // Copy attributes from global rule into current rule
499                                                 if (globalAttributes) {
500                                                         for (key in globalAttributes)
501                                                                 attributes[key] = globalAttributes[key];
502
503                                                         attributesOrder.push.apply(attributesOrder, globalAttributesOrder);
504                                                 }
505
506                                                 // Attributes defined
507                                                 if (attrData) {
508                                                         attrData = split(attrData, '|');
509                                                         for (ai = 0, al = attrData.length; ai < al; ai++) {
510                                                                 matches = attrRuleRegExp.exec(attrData[ai]);
511                                                                 if (matches) {
512                                                                         attr = {};
513                                                                         attrType = matches[1];
514                                                                         attrName = matches[2].replace(/::/g, ':');
515                                                                         prefix = matches[3];
516                                                                         value = matches[4];
517
518                                                                         // Required
519                                                                         if (attrType === '!') {
520                                                                                 element.attributesRequired = element.attributesRequired || [];
521                                                                                 element.attributesRequired.push(attrName);
522                                                                                 attr.required = true;
523                                                                         }
524
525                                                                         // Denied from global
526                                                                         if (attrType === '-') {
527                                                                                 delete attributes[attrName];
528                                                                                 attributesOrder.splice(tinymce.inArray(attributesOrder, attrName), 1);
529                                                                                 continue;
530                                                                         }
531
532                                                                         // Default value
533                                                                         if (prefix) {
534                                                                                 // Default value
535                                                                                 if (prefix === '=') {
536                                                                                         element.attributesDefault = element.attributesDefault || [];
537                                                                                         element.attributesDefault.push({name: attrName, value: value});
538                                                                                         attr.defaultValue = value;
539                                                                                 }
540
541                                                                                 // Forced value
542                                                                                 if (prefix === ':') {
543                                                                                         element.attributesForced = element.attributesForced || [];
544                                                                                         element.attributesForced.push({name: attrName, value: value});
545                                                                                         attr.forcedValue = value;
546                                                                                 }
547
548                                                                                 // Required values
549                                                                                 if (prefix === '<')
550                                                                                         attr.validValues = makeMap(value, '?');
551                                                                         }
552
553                                                                         // Check for attribute patterns
554                                                                         if (hasPatternsRegExp.test(attrName)) {
555                                                                                 element.attributePatterns = element.attributePatterns || [];
556                                                                                 attr.pattern = patternToRegExp(attrName);
557                                                                                 element.attributePatterns.push(attr);
558                                                                         } else {
559                                                                                 // Add attribute to order list if it doesn't already exist
560                                                                                 if (!attributes[attrName])
561                                                                                         attributesOrder.push(attrName);
562
563                                                                                 attributes[attrName] = attr;
564                                                                         }
565                                                                 }
566                                                         }
567                                                 }
568
569                                                 // Global rule, store away these for later usage
570                                                 if (!globalAttributes && elementName == '@') {
571                                                         globalAttributes = attributes;
572                                                         globalAttributesOrder = attributesOrder;
573                                                 }
574
575                                                 // Handle substitute elements such as b/strong
576                                                 if (outputName) {
577                                                         element.outputName = elementName;
578                                                         elements[outputName] = element;
579                                                 }
580
581                                                 // Add pattern or exact element
582                                                 if (hasPatternsRegExp.test(elementName)) {
583                                                         element.pattern = patternToRegExp(elementName);
584                                                         patternElements.push(element);
585                                                 } else
586                                                         elements[elementName] = element;
587                                         }
588                                 }
589                         }
590                 };
591
592                 function setValidElements(valid_elements) {
593                         elements = {};
594                         patternElements = [];
595
596                         addValidElements(valid_elements);
597
598                         each(schemaItems, function(element, name) {
599                                 children[name] = element.children;
600                         });
601                 };
602
603                 // Adds custom non HTML elements to the schema
604                 function addCustomElements(custom_elements) {
605                         var customElementRegExp = /^(~)?(.+)$/;
606
607                         if (custom_elements) {
608                                 each(split(custom_elements), function(rule) {
609                                         var matches = customElementRegExp.exec(rule),
610                                                 inline = matches[1] === '~',
611                                                 cloneName = inline ? 'span' : 'div',
612                                                 name = matches[2];
613
614                                         children[name] = children[cloneName];
615                                         customElementsMap[name] = cloneName;
616
617                                         // If it's not marked as inline then add it to valid block elements
618                                         if (!inline) {
619                                                 blockElementsMap[name.toUpperCase()] = {};
620                                                 blockElementsMap[name] = {};
621                                         }
622
623                                         // Add elements clone if needed
624                                         if (!elements[name]) {
625                                                 elements[name] = elements[cloneName];
626                                         }
627
628                                         // Add custom elements at span/div positions
629                                         each(children, function(element, child) {
630                                                 if (element[cloneName])
631                                                         element[name] = element[cloneName];
632                                         });
633                                 });
634                         }
635                 };
636
637                 // Adds valid children to the schema object
638                 function addValidChildren(valid_children) {
639                         var childRuleRegExp = /^([+\-]?)(\w+)\[([^\]]+)\]$/;
640
641                         if (valid_children) {
642                                 each(split(valid_children), function(rule) {
643                                         var matches = childRuleRegExp.exec(rule), parent, prefix;
644
645                                         if (matches) {
646                                                 prefix = matches[1];
647
648                                                 // Add/remove items from default
649                                                 if (prefix)
650                                                         parent = children[matches[2]];
651                                                 else
652                                                         parent = children[matches[2]] = {'#comment' : {}};
653
654                                                 parent = children[matches[2]];
655
656                                                 each(split(matches[3], '|'), function(child) {
657                                                         if (prefix === '-')
658                                                                 delete parent[child];
659                                                         else
660                                                                 parent[child] = {};
661                                                 });
662                                         }
663                                 });
664                         }
665                 };
666
667                 function getElementRule(name) {
668                         var element = elements[name], i;
669
670                         // Exact match found
671                         if (element)
672                                 return element;
673
674                         // No exact match then try the patterns
675                         i = patternElements.length;
676                         while (i--) {
677                                 element = patternElements[i];
678
679                                 if (element.pattern.test(name))
680                                         return element;
681                         }
682                 };
683
684                 if (!settings.valid_elements) {
685                         // No valid elements defined then clone the elements from the schema spec
686                         each(schemaItems, function(element, name) {
687                                 elements[name] = {
688                                         attributes : element.attributes,
689                                         attributesOrder : element.attributesOrder
690                                 };
691
692                                 children[name] = element.children;
693                         });
694
695                         // Switch these on HTML4
696                         if (settings.schema != "html5") {
697                                 each(split('strong/b,em/i'), function(item) {
698                                         item = split(item, '/');
699                                         elements[item[1]].outputName = item[0];
700                                 });
701                         }
702
703                         // Add default alt attribute for images
704                         elements.img.attributesDefault = [{name: 'alt', value: ''}];
705
706                         // Remove these if they are empty by default
707                         each(split('ol,ul,sub,sup,blockquote,span,font,a,table,tbody,tr'), function(name) {
708                                 if (elements[name]) {
709                                         elements[name].removeEmpty = true;
710                                 }
711                         });
712
713                         // Padd these by default
714                         each(split('p,h1,h2,h3,h4,h5,h6,th,td,pre,div,address,caption'), function(name) {
715                                 elements[name].paddEmpty = true;
716                         });
717                 } else
718                         setValidElements(settings.valid_elements);
719
720                 addCustomElements(settings.custom_elements);
721                 addValidChildren(settings.valid_children);
722                 addValidElements(settings.extended_valid_elements);
723
724                 // Todo: Remove this when we fix list handling to be valid
725                 addValidChildren('+ol[ul|ol],+ul[ul|ol]');
726
727                 // Delete invalid elements
728                 if (settings.invalid_elements) {
729                         tinymce.each(tinymce.explode(settings.invalid_elements), function(item) {
730                                 if (elements[item])
731                                         delete elements[item];
732                         });
733                 }
734
735                 // If the user didn't allow span only allow internal spans
736                 if (!getElementRule('span'))
737                         addValidElements('span[!data-mce-type|*]');
738
739                 /**
740                  * Name/value map object with valid parents and children to those parents.
741                  *
742                  * @example
743                  * children = {
744                  *    div:{p:{}, h1:{}}
745                  * };
746                  * @field children
747                  * @type {Object}
748                  */
749                 self.children = children;
750
751                 /**
752                  * Name/value map object with valid styles for each element.
753                  *
754                  * @field styles
755                  * @type {Object}
756                  */
757                 self.styles = validStyles;
758
759                 /**
760                  * Returns a map with boolean attributes.
761                  *
762                  * @method getBoolAttrs
763                  * @return {Object} Name/value lookup map for boolean attributes.
764                  */
765                 self.getBoolAttrs = function() {
766                         return boolAttrMap;
767                 };
768
769                 /**
770                  * Returns a map with block elements.
771                  *
772                  * @method getBlockElements
773                  * @return {Object} Name/value lookup map for block elements.
774                  */
775                 self.getBlockElements = function() {
776                         return blockElementsMap;
777                 };
778
779                 /**
780                  * Returns a map with text block elements. Such as: p,h1-h6,div,address
781                  *
782                  * @method getTextBlockElements
783                  * @return {Object} Name/value lookup map for block elements.
784                  */
785                 self.getTextBlockElements = function() {
786                         return textBlockElementsMap;
787                 };
788
789                 /**
790                  * Returns a map with short ended elements such as BR or IMG.
791                  *
792                  * @method getShortEndedElements
793                  * @return {Object} Name/value lookup map for short ended elements.
794                  */
795                 self.getShortEndedElements = function() {
796                         return shortEndedElementsMap;
797                 };
798
799                 /**
800                  * Returns a map with self closing tags such as <li>.
801                  *
802                  * @method getSelfClosingElements
803                  * @return {Object} Name/value lookup map for self closing tags elements.
804                  */
805                 self.getSelfClosingElements = function() {
806                         return selfClosingElementsMap;
807                 };
808
809                 /**
810                  * Returns a map with elements that should be treated as contents regardless if it has text
811                  * content in them or not such as TD, VIDEO or IMG.
812                  *
813                  * @method getNonEmptyElements
814                  * @return {Object} Name/value lookup map for non empty elements.
815                  */
816                 self.getNonEmptyElements = function() {
817                         return nonEmptyElementsMap;
818                 };
819
820                 /**
821                  * Returns a map with elements where white space is to be preserved like PRE or SCRIPT.
822                  *
823                  * @method getWhiteSpaceElements
824                  * @return {Object} Name/value lookup map for white space elements.
825                  */
826                 self.getWhiteSpaceElements = function() {
827                         return whiteSpaceElementsMap;
828                 };
829
830                 /**
831                  * Returns true/false if the specified element and it's child is valid or not
832                  * according to the schema.
833                  *
834                  * @method isValidChild
835                  * @param {String} name Element name to check for.
836                  * @param {String} child Element child to verify.
837                  * @return {Boolean} True/false if the element is a valid child of the specified parent.
838                  */
839                 self.isValidChild = function(name, child) {
840                         var parent = children[name];
841
842                         return !!(parent && parent[child]);
843                 };
844
845                 /**
846                  * Returns true/false if the specified element name and optional attribute is
847                  * valid according to the schema.
848                  *
849                  * @method isValid
850                  * @param {String} name Name of element to check.
851                  * @param {String} attr Optional attribute name to check for.
852                  * @return {Boolean} True/false if the element and attribute is valid.
853                  */
854                 self.isValid = function(name, attr) {
855                         var attrPatterns, i, rule = getElementRule(name);
856
857                         // Check if it's a valid element
858                         if (rule) {
859                                 if (attr) {
860                                         // Check if attribute name exists
861                                         if (rule.attributes[attr]) {
862                                                 return true;
863                                         }
864
865                                         // Check if attribute matches a regexp pattern
866                                         attrPatterns = rule.attributePatterns;
867                                         if (attrPatterns) {
868                                                 i = attrPatterns.length;
869                                                 while (i--) {
870                                                         if (attrPatterns[i].pattern.test(name)) {
871                                                                 return true;
872                                                         }
873                                                 }
874                                         }
875                                 } else {
876                                         return true;
877                                 }
878                         }
879
880                         // No match
881                         return false;
882                 };
883
884                 /**
885                  * Returns true/false if the specified element is valid or not
886                  * according to the schema.
887                  *
888                  * @method getElementRule
889                  * @param {String} name Element name to check for.
890                  * @return {Object} Element object or undefined if the element isn't valid.
891                  */
892                 self.getElementRule = getElementRule;
893
894                 /**
895                  * Returns an map object of all custom elements.
896                  *
897                  * @method getCustomElements
898                  * @return {Object} Name/value map object of all custom elements.
899                  */
900                 self.getCustomElements = function() {
901                         return customElementsMap;
902                 };
903
904                 /**
905                  * Parses a valid elements string and adds it to the schema. The valid elements format is for example "element[attr=default|otherattr]".
906                  * Existing rules will be replaced with the ones specified, so this extends the schema.
907                  *
908                  * @method addValidElements
909                  * @param {String} valid_elements String in the valid elements format to be parsed.
910                  */
911                 self.addValidElements = addValidElements;
912
913                 /**
914                  * Parses a valid elements string and sets it to the schema. The valid elements format is for example "element[attr=default|otherattr]".
915                  * Existing rules will be replaced with the ones specified, so this extends the schema.
916                  *
917                  * @method setValidElements
918                  * @param {String} valid_elements String in the valid elements format to be parsed.
919                  */
920                 self.setValidElements = setValidElements;
921
922                 /**
923                  * Adds custom non HTML elements to the schema.
924                  *
925                  * @method addCustomElements
926                  * @param {String} custom_elements Comma separated list of custom elements to add.
927                  */
928                 self.addCustomElements = addCustomElements;
929
930                 /**
931                  * Parses a valid children string and adds them to the schema structure. The valid children format is for example: "element[child1|child2]".
932                  *
933                  * @method addValidChildren
934                  * @param {String} valid_children Valid children elements string to parse
935                  */
936                 self.addValidChildren = addValidChildren;
937
938                 self.elements = elements;
939         };
940 })(tinymce);