]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/editor.js
WordPress 3.9.1
[autoinstalls/wordpress.git] / wp-admin / js / editor.js
1 /* global tinymce, tinyMCEPreInit, QTags, setUserSetting */
2
3 window.switchEditors = {
4
5         switchto: function( el ) {
6                 var aid = el.id,
7                         l = aid.length,
8                         id = aid.substr( 0, l - 5 ),
9                         mode = aid.substr( l - 4 );
10
11                 this.go( id, mode );
12         },
13
14         // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the 'Text' editor tab.
15         go: function( id, mode ) {
16                 var t = this, ed, wrap_id, txtarea_el, iframe, editorHeight, toolbarHeight,
17                         DOM = tinymce.DOM; //DOMUtils outside the editor iframe
18
19                 id = id || 'content';
20                 mode = mode || 'toggle';
21
22                 ed = tinymce.get( id );
23                 wrap_id = 'wp-' + id + '-wrap';
24                 txtarea_el = DOM.get( id );
25
26                 if ( 'toggle' === mode ) {
27                         if ( ed && ! ed.isHidden() ) {
28                                 mode = 'html';
29                         } else {
30                                 mode = 'tmce';
31                         }
32                 }
33
34                 function getToolbarHeight() {
35                         var node = DOM.select( '.mce-toolbar-grp', ed.getContainer() )[0],
36                                 height = node && node.clientHeight;
37
38                         if ( height && height > 10 && height < 200 ) {
39                                 return parseInt( height, 10 );
40                         }
41
42                         return 30;
43                 }
44
45                 if ( 'tmce' === mode || 'tinymce' === mode ) {
46                         if ( ed && ! ed.isHidden() ) {
47                                 return false;
48                         }
49
50                         if ( typeof( QTags ) !== 'undefined' ) {
51                                 QTags.closeAllTags( id );
52                         }
53
54                         editorHeight = txtarea_el ? parseInt( txtarea_el.style.height, 10 ) : 0;
55
56                         if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) {
57                                 txtarea_el.value = t.wpautop( txtarea_el.value );
58                         }
59
60                         if ( ed ) {
61                                 ed.show();
62
63                                 if ( editorHeight ) {
64                                         toolbarHeight = getToolbarHeight();
65                                         editorHeight = editorHeight - toolbarHeight + 14;
66
67                                         // height cannot be under 50 or over 5000
68                                         if ( editorHeight > 50 && editorHeight < 5000 ) {
69                                                 ed.theme.resizeTo( null, editorHeight );
70                                         }
71                                 }
72                         } else {
73                                 tinymce.init( tinyMCEPreInit.mceInit[id] );
74                         }
75
76                         DOM.removeClass( wrap_id, 'html-active' );
77                         DOM.addClass( wrap_id, 'tmce-active' );
78                         setUserSetting( 'editor', 'tinymce' );
79
80                 } else if ( 'html' === mode ) {
81
82                         if ( ed && ed.isHidden() ) {
83                                 return false;
84                         }
85
86                         if ( ed ) {
87                                 iframe = DOM.get( id + '_ifr' );
88                                 editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
89
90                                 if ( editorHeight ) {
91                                         toolbarHeight = getToolbarHeight();
92                                         editorHeight = editorHeight + toolbarHeight - 14;
93
94                                         // height cannot be under 50 or over 5000
95                                         if ( editorHeight > 50 && editorHeight < 5000 ) {
96                                                 txtarea_el.style.height = editorHeight + 'px';
97                                         }
98                                 }
99
100                                 ed.hide();
101                         } else {
102                                 // The TinyMCE instance doesn't exist, run the content through 'pre_wpautop()' and show the textarea
103                                 if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) {
104                                         txtarea_el.value = t.pre_wpautop( txtarea_el.value );
105                                 }
106
107                                 DOM.setStyles( txtarea_el, {'display': '', 'visibility': ''} );
108                         }
109
110                         DOM.removeClass( wrap_id, 'tmce-active' );
111                         DOM.addClass( wrap_id, 'html-active' );
112                         setUserSetting( 'editor', 'html' );
113                 }
114                 return false;
115         },
116
117         _wp_Nop: function( content ) {
118                 var blocklist1, blocklist2,
119                         preserve_linebreaks = false,
120                         preserve_br = false;
121
122                 // Protect pre|script tags
123                 if ( content.indexOf( '<pre' ) !== -1 || content.indexOf( '<script' ) !== -1 ) {
124                         preserve_linebreaks = true;
125                         content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
126                                 a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-line-break>' );
127                                 a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>' );
128                                 return a.replace( /\r?\n/g, '<wp-line-break>' );
129                         });
130                 }
131
132                 // keep <br> tags inside captions and remove line breaks
133                 if ( content.indexOf( '[caption' ) !== -1 ) {
134                         preserve_br = true;
135                         content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
136                                 return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
137                         });
138                 }
139
140                 // Pretty it up for the source editor
141                 blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset';
142                 content = content.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
143                 content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
144
145                 // Mark </p> if it has any attributes.
146                 content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
147
148                 // Separate <div> containing <p>
149                 content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
150
151                 // Remove <p> and <br />
152                 content = content.replace( /\s*<p>/gi, '' );
153                 content = content.replace( /\s*<\/p>\s*/gi, '\n\n' );
154                 content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
155                 content = content.replace( /\s*<br ?\/?>\s*/gi, '\n' );
156
157                 // Fix some block element newline issues
158                 content = content.replace( /\s*<div/g, '\n<div' );
159                 content = content.replace( /<\/div>\s*/g, '</div>\n' );
160                 content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
161                 content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
162
163                 blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset';
164                 content = content.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
165                 content = content.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
166                 content = content.replace( /<li([^>]*)>/g, '\t<li$1>' );
167
168                 if ( content.indexOf( '<hr' ) !== -1 ) {
169                         content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
170                 }
171
172                 if ( content.indexOf( '<object' ) !== -1 ) {
173                         content = content.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
174                                 return a.replace( /[\r\n]+/g, '' );
175                         });
176                 }
177
178                 // Unmark special paragraph closing tags
179                 content = content.replace( /<\/p#>/g, '</p>\n' );
180                 content = content.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
181
182                 // Trim whitespace
183                 content = content.replace( /^\s+/, '' );
184                 content = content.replace( /[\s\u00a0]+$/, '' );
185
186                 // put back the line breaks in pre|script
187                 if ( preserve_linebreaks ) {
188                         content = content.replace( /<wp-line-break>/g, '\n' );
189                 }
190
191                 // and the <br> tags in captions
192                 if ( preserve_br ) {
193                         content = content.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
194                 }
195
196                 return content;
197         },
198
199         _wp_Autop: function(pee) {
200                 var preserve_linebreaks = false,
201                         preserve_br = false,
202                         blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
203                                 '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|legend|section' +
204                                 '|article|aside|hgroup|header|footer|nav|figure|details|menu|summary';
205
206                 if ( pee.indexOf( '<object' ) !== -1 ) {
207                         pee = pee.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
208                                 return a.replace( /[\r\n]+/g, '' );
209                         });
210                 }
211
212                 pee = pee.replace( /<[^<>]+>/g, function( a ){
213                         return a.replace( /[\r\n]+/g, ' ' );
214                 });
215
216                 // Protect pre|script tags
217                 if ( pee.indexOf( '<pre' ) !== -1 || pee.indexOf( '<script' ) !== -1 ) {
218                         preserve_linebreaks = true;
219                         pee = pee.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
220                                 return a.replace( /(\r\n|\n)/g, '<wp-line-break>' );
221                         });
222                 }
223
224                 // keep <br> tags inside captions and convert line breaks
225                 if ( pee.indexOf( '[caption' ) !== -1 ) {
226                         preserve_br = true;
227                         pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
228                                 // keep existing <br>
229                                 a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
230                                 // no line breaks inside HTML tags
231                                 a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ) {
232                                         return b.replace( /[\r\n\t]+/, ' ' );
233                                 });
234                                 // convert remaining line breaks to <br>
235                                 return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
236                         });
237                 }
238
239                 pee = pee + '\n\n';
240                 pee = pee.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
241                 pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' );
242                 pee = pee.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
243                 pee = pee.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
244                 pee = pee.replace( /\r\n|\r/g, '\n' );
245                 pee = pee.replace( /\n\s*\n+/g, '\n\n' );
246                 pee = pee.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
247                 pee = pee.replace( /<p>\s*?<\/p>/gi, '');
248                 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
249                 pee = pee.replace( /<p>(<li.+?)<\/p>/gi, '$1');
250                 pee = pee.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
251                 pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
252                 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
253                 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
254                 pee = pee.replace( /\s*\n/gi, '<br />\n');
255                 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
256                 pee = pee.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
257                 pee = pee.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
258
259                 pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
260                         if ( c.match( /<p( [^>]*)?>/ ) ) {
261                                 return a;
262                         }
263
264                         return b + '<p>' + c + '</p>';
265                 });
266
267                 // put back the line breaks in pre|script
268                 if ( preserve_linebreaks ) {
269                         pee = pee.replace( /<wp-line-break>/g, '\n' );
270                 }
271
272                 if ( preserve_br ) {
273                         pee = pee.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
274                 }
275
276                 return pee;
277         },
278
279         pre_wpautop: function( content ) {
280                 var t = this, o = { o: t, data: content, unfiltered: content },
281                         q = typeof( jQuery ) !== 'undefined';
282
283                 if ( q ) {
284                         jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] );
285                 }
286
287                 o.data = t._wp_Nop( o.data );
288
289                 if ( q ) {
290                         jQuery('body').trigger('afterPreWpautop', [ o ] );
291                 }
292
293                 return o.data;
294         },
295
296         wpautop: function( pee ) {
297                 var t = this, o = { o: t, data: pee, unfiltered: pee },
298                         q = typeof( jQuery ) !== 'undefined';
299
300                 if ( q ) {
301                         jQuery( 'body' ).trigger('beforeWpautop', [ o ] );
302                 }
303
304                 o.data = t._wp_Autop( o.data );
305
306                 if ( q ) {
307                         jQuery( 'body' ).trigger('afterWpautop', [ o ] );
308                 }
309
310                 return o.data;
311         }
312 };