]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js
Wordpress 2.3.3-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / editor_plugin.js
1 /* Import plugin specific language pack */
2 tinyMCE.importPluginLanguagePack('wordpress', 'en');
3
4 var TinyMCE_wordpressPlugin = {
5         getInfo : function() {
6                 return {
7                         longname : 'WordPress Plugin',
8                         author : 'WordPress',
9                         authorurl : 'http://wordpress.org',
10                         infourl : 'http://wordpress.org',
11                         version : '1'
12                 };
13         },
14
15         getControlHTML : function(control_name) {
16                 switch (control_name) {
17                         case "wp_more":
18                                 return tinyMCE.getButtonHTML(control_name, 'lang_wordpress_more_button', '{$pluginurl}/images/more.gif', 'wpMore');
19                         case "wp_page":
20                                 return tinyMCE.getButtonHTML(control_name, 'lang_wordpress_page_button', '{$pluginurl}/images/page.gif', 'wpPage');
21                         case "wp_help":
22                                 var buttons = tinyMCE.getButtonHTML(control_name, 'lang_help_button_title', '{$pluginurl}/images/help.gif', 'wpHelp');
23                                 var hiddenControls = '<div class="zerosize">'
24                                 + '<input type="button" accesskey="n" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceSpellCheck\',false);" />'
25                                 + '<input type="button" accesskey="k" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'Strikethrough\',false);" />'
26                                 + '<input type="button" accesskey="l" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'InsertUnorderedList\',false);" />'
27                                 + '<input type="button" accesskey="o" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'InsertOrderedList\',false);" />'
28                                 + '<input type="button" accesskey="w" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'Outdent\',false);" />'
29                                 + '<input type="button" accesskey="q" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'Indent\',false);" />'
30                                 + '<input type="button" accesskey="f" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'JustifyLeft\',false);" />'
31                                 + '<input type="button" accesskey="c" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'JustifyCenter\',false);" />'
32                                 + '<input type="button" accesskey="r" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'JustifyRight\',false);" />'
33                                 + '<input type="button" accesskey="j" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'JustifyFull\',false);" />'
34                                 + '<input type="button" accesskey="a" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceLink\',true);" />'
35                                 + '<input type="button" accesskey="s" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'unlink\',false);" />'
36                                 + '<input type="button" accesskey="m" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceImage\',true);" />'
37                                 + '<input type="button" accesskey="t" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'wpMore\');" />'
38                                 + '<input type="button" accesskey="g" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'wpPage\');" />'
39                                 + '<input type="button" accesskey="u" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'Undo\',false);" />'
40                                 + '<input type="button" accesskey="y" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'Redo\',false);" />'
41                                 + '<input type="button" accesskey="h" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'wpHelp\',false);" />'
42                                 + '<input type="button" accesskey="b" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'Bold\',false);" />'
43                                 + '<input type="button" accesskey="v" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'wpAdv\',false);" />'
44                                 + '</div>';
45                                 return buttons+hiddenControls;
46                         case "wp_adv":
47                                 return tinyMCE.getButtonHTML(control_name, 'lang_wordpress_adv_button', '{$pluginurl}/images/toolbars.gif', 'wpAdv');
48                         case "wp_adv_start":
49                                 return '<div id="wpadvbar" style="display:none;"><br />';
50                         case "wp_adv_end":
51                                 return '</div>';
52                 }
53                 return '';
54         },
55
56         execCommand : function(editor_id, element, command, user_interface, value) {
57                 var inst = tinyMCE.getInstanceById(editor_id);
58                 var focusElm = inst.getFocusElement();
59                 var doc = inst.getDoc();
60
61                 function getAttrib(elm, name) {
62                         return elm.getAttribute(name) ? elm.getAttribute(name) : "";
63                 }
64
65                 // Handle commands
66                 switch (command) {
67                         case "wpMore":
68                                 var flag = "";
69                                 var template = new Array();
70                                 var altMore = tinyMCE.getLang('lang_wordpress_more_alt');
71
72                                 // Is selection a image
73                                 if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {
74                                         flag = getAttrib(focusElm, 'class');
75
76                                         if (flag != 'mce_plugin_wordpress_more') // Not a wordpress
77                                                 return true;
78
79                                         action = "update";
80                                 }
81
82                                 html = ''
83                                         + '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" '
84                                         + ' width="100%" height="10px" '
85                                         + 'alt="'+altMore+'" title="'+altMore+'" class="mce_plugin_wordpress_more" name="mce_plugin_wordpress_more" />';
86                                 tinyMCE.execInstanceCommand(editor_id, 'mceInsertContent', false, html);
87                                 tinyMCE.selectedInstance.repaint();
88                                 return true;
89
90                         case "wpPage":
91                                 var flag = "";
92                                 var template = new Array();
93                                 var altPage = tinyMCE.getLang('lang_wordpress_more_alt');
94
95                                 // Is selection a image
96                                 if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {
97                                         flag = getAttrib(focusElm, 'name');
98
99                                         if (flag != 'mce_plugin_wordpress_page') // Not a wordpress
100                                                 return true;
101
102                                         action = "update";
103                                 }
104
105                                 html = ''
106                                         + '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" '
107                                         + ' width="100%" height="10px" '
108                                         + 'alt="'+altPage+'" title="'+altPage+'" class="mce_plugin_wordpress_page" name="mce_plugin_wordpress_page" />';
109                                 tinyMCE.execCommand("mceInsertContent",true,html);
110                                 tinyMCE.selectedInstance.repaint();
111                                 return true;
112
113                         case "wpHelp":
114                                 var template = new Array();
115
116                                 template['file']   = tinyMCE.baseURL + '/wp-mce-help.php';
117                                 template['width']  = 480;
118                                 template['height'] = 380;
119
120                                 args = {
121                                         resizable : 'yes',
122                                         scrollbars : 'yes'
123                                 };
124
125                                 tinyMCE.openWindow(template, args);
126                                 return true;
127                         case "wpAdv":
128                                 var adv = document.getElementById('wpadvbar');
129                                 if ( adv.style.display == 'none' ) {
130                                         adv.style.display = 'block';
131                                         tinyMCE.switchClass(editor_id + '_wp_adv', 'mceButtonSelected');
132                                 } else {
133                                         adv.style.display = 'none';
134                                         tinyMCE.switchClass(editor_id + '_wp_adv', 'mceButtonNormal');
135                                 }
136                                 return true;
137                 }
138
139                 // Pass to next handler in chain
140                 return false;
141         },
142
143         cleanup : function(type, content) {
144                 switch (type) {
145
146                         case "insert_to_editor":
147                                 var startPos = 0;
148                                 var altMore = tinyMCE.getLang('lang_wordpress_more_alt');
149                                 var altPage = tinyMCE.getLang('lang_wordpress_page_alt');
150
151                                 // Parse all <!--more--> tags and replace them with images
152                                 while ((startPos = content.indexOf('<!--more', startPos)) != -1) {
153                                         var endPos = content.indexOf('-->', startPos) + 3;
154                                         // Insert image
155                                         var moreText = content.substring(startPos + 8, endPos - 3);
156                                         var contentAfter = content.substring(endPos);
157                                         content = content.substring(0, startPos);
158                                         content += '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" ';
159                                         content += ' width="100%" height="10px" moretext="'+moreText+'" ';
160                                         content += 'alt="'+altMore+'" title="'+altMore+'" class="mce_plugin_wordpress_more" name="mce_plugin_wordpress_more" />';
161                                         content += contentAfter;
162
163                                         startPos++;
164                                 }
165                                 var startPos = 0;
166
167                                 // Parse all <!--page--> tags and replace them with images
168                                 while ((startPos = content.indexOf('<!--nextpage-->', startPos)) != -1) {
169                                         // Insert image
170                                         var contentAfter = content.substring(startPos + 15);
171                                         content = content.substring(0, startPos);
172                                         content += '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" ';
173                                         content += ' width="100%" height="10px" ';
174                                         content += 'alt="'+altPage+'" title="'+altPage+'" class="mce_plugin_wordpress_page" name="mce_plugin_wordpress_page" />';
175                                         content += contentAfter;
176
177                                         startPos++;
178                                 }
179
180                                 // Look for \n in <pre>, replace with <br>
181                                 var startPos = -1;
182                                 while ((startPos = content.indexOf('<pre', startPos+1)) != -1) {
183                                         var endPos = content.indexOf('</pre>', startPos+1);
184                                         var innerPos = content.indexOf('>', startPos+1);
185                                         var chunkBefore = content.substring(0, innerPos);
186                                         var chunkAfter = content.substring(endPos);
187                                         
188                                         var innards = content.substring(innerPos, endPos);
189                                         innards = innards.replace(/\n/g, '<br />');
190                                         content = chunkBefore + innards + chunkAfter;
191                                 }
192
193                                 break;
194
195                         case "get_from_editor":
196                                 // Parse all img tags and replace them with <!--more-->
197                                 var startPos = -1;
198                                 while ((startPos = content.indexOf('<img', startPos+1)) != -1) {
199                                         var endPos = content.indexOf('/>', startPos);
200                                         var attribs = this._parseAttributes(content.substring(startPos + 4, endPos));
201
202                                         if (attribs['class'] == "mce_plugin_wordpress_more" || attribs['name'] == "mce_plugin_wordpress_more") {
203                                                 endPos += 2;
204
205                                                 var moreText = attribs['moretext'] ? attribs['moretext'] : '';
206                                                 var embedHTML = '<!--more'+moreText+'-->';
207
208                                                 // Insert embed/object chunk
209                                                 chunkBefore = content.substring(0, startPos);
210                                                 chunkAfter = content.substring(endPos);
211                                                 content = chunkBefore + embedHTML + chunkAfter;
212                                         }
213                                         if (attribs['class'] == "mce_plugin_wordpress_page" || attribs['name'] == "mce_plugin_wordpress_page") {
214                                                 endPos += 2;
215
216                                                 var embedHTML = '<!--nextpage-->';
217
218                                                 // Insert embed/object chunk
219                                                 chunkBefore = content.substring(0, startPos);
220                                                 chunkAfter = content.substring(endPos);
221                                                 content = chunkBefore + embedHTML + chunkAfter;
222                                         }
223                                 }
224
225                                 // Remove normal line breaks
226                                 content = content.replace(/\n|\r/g, ' ');
227
228                                 // Look for <br> in <pre>, replace with \n
229                                 var startPos = -1;
230                                 while ((startPos = content.indexOf('<pre', startPos+1)) != -1) {
231                                         var endPos = content.indexOf('</pre>', startPos+1);
232                                         var innerPos = content.indexOf('>', startPos+1);
233                                         var chunkBefore = content.substring(0, innerPos);
234                                         var chunkAfter = content.substring(endPos);
235                                         
236                                         var innards = content.substring(innerPos, endPos);
237                                         innards = innards.replace(new RegExp('<br\\s?/?>', 'g'), '\n');
238                                         innards = innards.replace(new RegExp('\\s$', ''), '');
239                                         content = chunkBefore + innards + chunkAfter;
240                                 }
241
242                                 // Remove anonymous, empty paragraphs.
243                                 content = content.replace(new RegExp('<p>(\\s|&nbsp;)*</p>', 'mg'), '');
244
245                                 // Handle table badness.
246                                 content = content.replace(new RegExp('<(table( [^>]*)?)>.*?<((tr|thead)( [^>]*)?)>', 'mg'), '<$1><$3>');
247                                 content = content.replace(new RegExp('<(tr|thead|tfoot)>.*?<((td|th)( [^>]*)?)>', 'mg'), '<$1><$2>');
248                                 content = content.replace(new RegExp('</(td|th)>.*?<(td( [^>]*)?|th( [^>]*)?|/tr|/thead|/tfoot)>', 'mg'), '</$1><$2>');
249                                 content = content.replace(new RegExp('</tr>.*?<(tr|/table)>', 'mg'), '</tr><$1>');
250                                 content = content.replace(new RegExp('<(/?(table|tbody|tr|th|td)[^>]*)>(\\s*|(<br ?/?>)*)*', 'g'), '<$1>');
251
252                                 // Pretty it up for the source editor.
253                                 var blocklist = 'blockquote|ul|ol|li|table|thead|tr|th|td|div|h\\d|pre|p';
254                                 content = content.replace(new RegExp('\\s*</('+blocklist+')>\\s*', 'mg'), '</$1>\n');
255                                 content = content.replace(new RegExp('\\s*<(('+blocklist+')[^>]*)>', 'mg'), '\n<$1>');
256                                 content = content.replace(new RegExp('<((li|/?tr|/?thead|/?tfoot)( [^>]*)?)>', 'g'), '\t<$1>');
257                                 content = content.replace(new RegExp('<((td|th)( [^>]*)?)>', 'g'), '\t\t<$1>');
258                                 content = content.replace(new RegExp('\\s*<br ?/?>\\s*', 'mg'), '<br />\n');
259                                 content = content.replace(new RegExp('^\\s*', ''), '');
260                                 content = content.replace(new RegExp('\\s*$', ''), '');
261
262                                 break;
263                 }
264
265                 // Pass through to next handler in chain
266                 return content;
267         },
268
269         handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
270
271                 tinyMCE.switchClass(editor_id + '_wp_more', 'mceButtonNormal');
272                 tinyMCE.switchClass(editor_id + '_wp_page', 'mceButtonNormal');
273
274                 if (node == null)
275                         return;
276
277                 do {
278                         if (node.nodeName.toLowerCase() == "img" && tinyMCE.getAttrib(node, 'class').indexOf('mce_plugin_wordpress_more') == 0)
279                                 tinyMCE.switchClass(editor_id + '_wp_more', 'mceButtonSelected');
280                         if (node.nodeName.toLowerCase() == "img" && tinyMCE.getAttrib(node, 'class').indexOf('mce_plugin_wordpress_page') == 0)
281                                 tinyMCE.switchClass(editor_id + '_wp_page', 'mceButtonSelected');
282                 } while ((node = node.parentNode));
283
284                 return true;
285         },
286
287         saveCallback : function(el, content, body) {
288                 // We have a TON of cleanup to do.
289
290                 // Mark </p> if it has any attributes.
291                 content = content.replace(new RegExp('(<p[^>]+>.*?)</p>', 'mg'), '$1</p#>');
292
293                 // Decode the ampersands of time.
294                 // content = content.replace(new RegExp('&amp;', 'g'), '&');
295
296                 // Get it ready for wpautop.
297                 content = content.replace(new RegExp('\\s*<p>', 'mgi'), '');
298                 content = content.replace(new RegExp('\\s*</p>\\s*', 'mgi'), '\n\n');
299                 content = content.replace(new RegExp('\\n\\s*\\n', 'mgi'), '\n\n');
300                 content = content.replace(new RegExp('\\s*<br ?/?>\\s*', 'gi'), '\n');
301
302                 // Fix some block element newline issues
303                 var blocklist = 'blockquote|ul|ol|li|table|thead|tr|th|td|div|h\\d|pre';
304                 content = content.replace(new RegExp('\\s*<(('+blocklist+') ?[^>]*)\\s*>', 'mg'), '\n<$1>');
305                 content = content.replace(new RegExp('\\s*</('+blocklist+')>\\s*', 'mg'), '</$1>\n');
306                 content = content.replace(new RegExp('<li>', 'g'), '\t<li>');
307
308                 // Unmark special paragraph closing tags
309                 content = content.replace(new RegExp('</p#>', 'g'), '</p>\n');
310                 content = content.replace(new RegExp('\\s*(<p[^>]+>.*</p>)', 'mg'), '\n$1');
311
312                 // Trim trailing whitespace
313                 content = content.replace(new RegExp('\\s*$', ''), '');
314
315                 // Hope.
316                 return content;
317
318         },
319
320         _parseAttributes : function(attribute_string) {
321                 var attributeName = "";
322                 var attributeValue = "";
323                 var withInName;
324                 var withInValue;
325                 var attributes = new Array();
326                 var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');
327                 var titleText = tinyMCE.getLang('lang_wordpress_more');
328                 var titleTextPage = tinyMCE.getLang('lang_wordpress_page');
329
330                 if (attribute_string == null || attribute_string.length < 2)
331                         return null;
332
333                 withInName = withInValue = false;
334
335                 for (var i=0; i<attribute_string.length; i++) {
336                         var chr = attribute_string.charAt(i);
337
338                         if ((chr == '"' || chr == "'") && !withInValue)
339                                 withInValue = true;
340                         else if ((chr == '"' || chr == "'") && withInValue) {
341                                 withInValue = false;
342
343                                 var pos = attributeName.lastIndexOf(' ');
344                                 if (pos != -1)
345                                         attributeName = attributeName.substring(pos+1);
346
347                                 attributes[attributeName.toLowerCase()] = attributeValue.substring(1);
348
349                                 attributeName = "";
350                                 attributeValue = "";
351                         } else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)
352                                 withInName = true;
353
354                         if (chr == '=' && withInName)
355                                 withInName = false;
356
357                         if (withInName)
358                                 attributeName += chr;
359
360                         if (withInValue)
361                                 attributeValue += chr;
362                 }
363
364                 return attributes;
365         }
366 };
367
368 tinyMCE.addPlugin("wordpress", TinyMCE_wordpressPlugin);
369
370 /* This little hack protects our More and Page placeholders from the removeformat command */
371 tinyMCE.orgExecCommand = tinyMCE.execCommand;
372 tinyMCE.execCommand = function (command, user_interface, value) {
373         re = this.orgExecCommand(command, user_interface, value);
374
375         if ( command == 'removeformat' ) {
376                 var inst = tinyMCE.getInstanceById('mce_editor_0');
377                 doc = inst.getDoc();
378                 var imgs = doc.getElementsByTagName('img');
379                 for (i=0;img=imgs[i];i++)
380                         img.className = img.name;
381         }
382         return re;
383 };
384 wpInstTriggerSave = function (skip_cleanup, skip_callback) {
385         var e, nl = new Array(), i, s;
386
387         this.switchSettings();
388         s = tinyMCE.settings;
389
390         // Force hidden tabs visible while serializing
391         if (tinyMCE.isMSIE && !tinyMCE.isOpera) {
392                 e = this.iframeElement;
393
394                 do {
395                         if (e.style && e.style.display == 'none') {
396                                 e.style.display = 'block';
397                                 nl[nl.length] = {elm : e, type : 'style'};
398                         }
399
400                         if (e.style && s.hidden_tab_class.length > 0 && e.className.indexOf(s.hidden_tab_class) != -1) {
401                                 e.className = s.display_tab_class;
402                                 nl[nl.length] = {elm : e, type : 'class'};
403                         }
404                 } while ((e = e.parentNode) != null)
405         }
406
407         tinyMCE.settings['preformatted'] = false;
408
409         // Default to false
410         if (typeof(skip_cleanup) == "undefined")
411                 skip_cleanup = false;
412
413         // Default to false
414         if (typeof(skip_callback) == "undefined")
415                 skip_callback = false;
416
417 //      tinyMCE._setHTML(this.getDoc(), this.getBody().innerHTML);
418
419         // Remove visual aids when cleanup is disabled
420         if (this.settings['cleanup'] == false) {
421                 tinyMCE.handleVisualAid(this.getBody(), true, false, this);
422                 tinyMCE._setEventsEnabled(this.getBody(), true);
423         }
424
425         tinyMCE._customCleanup(this, "submit_content_dom", this.contentWindow.document.body);
426         tinyMCE.selectedInstance.getWin().oldfocus=tinyMCE.selectedInstance.getWin().focus;
427         tinyMCE.selectedInstance.getWin().focus=function() {};
428         var htm = tinyMCE._cleanupHTML(this, this.getDoc(), this.settings, this.getBody(), tinyMCE.visualAid, true, true);
429         tinyMCE.selectedInstance.getWin().focus=tinyMCE.selectedInstance.getWin().oldfocus;
430         htm = tinyMCE._customCleanup(this, "submit_content", htm);
431
432         if (!skip_callback && tinyMCE.settings['save_callback'] != "")
433                 var content = eval(tinyMCE.settings['save_callback'] + "(this.formTargetElementId,htm,this.getBody());");
434
435         // Use callback content if available
436         if ((typeof(content) != "undefined") && content != null)
437                 htm = content;
438
439         // Replace some weird entities (Bug: #1056343)
440         htm = tinyMCE.regexpReplace(htm, "&#40;", "(", "gi");
441         htm = tinyMCE.regexpReplace(htm, "&#41;", ")", "gi");
442         htm = tinyMCE.regexpReplace(htm, "&#59;", ";", "gi");
443         htm = tinyMCE.regexpReplace(htm, "&#34;", "&quot;", "gi");
444         htm = tinyMCE.regexpReplace(htm, "&#94;", "^", "gi");
445
446         if (this.formElement)
447                 this.formElement.value = htm;
448
449         if (tinyMCE.isSafari && this.formElement)
450                 this.formElement.innerText = htm;
451
452         // Hide them again (tabs in MSIE)
453         for (i=0; i<nl.length; i++) {
454                 if (nl[i].type == 'style')
455                         nl[i].elm.style.display = 'none';
456                 else
457                         nl[i].elm.className = s.hidden_tab_class;
458         }
459 }
460 tinyMCE.wpTriggerSave = function () {
461         var inst, n;
462         for (n in tinyMCE.instances) {
463                 inst = tinyMCE.instances[n];
464                 if (!tinyMCE.isInstance(inst))
465                         continue;
466                 inst.wpTriggerSave = wpInstTriggerSave;
467                 inst.wpTriggerSave(false, false);
468         }
469 }
470
471 function switchEditors(id) {
472         var inst = tinyMCE.getInstanceById(id);
473         var qt = document.getElementById('quicktags');
474         var H = document.getElementById('edButtonHTML');
475         var P = document.getElementById('edButtonPreview');
476         var ta = document.getElementById(id);
477         var pdr = ta.parentNode;
478
479         if ( inst ) {
480                 edToggle(H, P);
481
482                 if ( tinyMCE.isMSIE && !tinyMCE.isOpera ) {
483                         // IE rejects the later overflow assignment so we skip this step.
484                         // Alternate code might be nice. Until then, IE reflows.
485                 } else {
486                         // Lock the fieldset's height to prevent reflow/flicker
487                         pdr.style.height = pdr.clientHeight + 'px';
488                         pdr.style.overflow = 'hidden';
489                 }
490
491                 // Save the coords of the bottom right corner of the rich editor
492                 var table = document.getElementById(inst.editorId + '_parent').getElementsByTagName('table')[0];
493                 var y1 = table.offsetTop + table.offsetHeight;
494
495                 if ( TinyMCE_AdvancedTheme._getCookie("TinyMCE_" + inst.editorId + "_height") == null ) {
496                         var expires = new Date();
497                         expires.setTime(expires.getTime() + 3600000 * 24 * 30);
498                         var offset = tinyMCE.isMSIE ? 1 : 2;
499                         TinyMCE_AdvancedTheme._setCookie("TinyMCE_" + inst.editorId + "_height", "" + (table.offsetHeight - offset), expires);
500                 }
501
502                 // Unload the rich editor
503                 inst.triggerSave(false, false);
504                 htm = inst.formElement.value;
505                 tinyMCE.removeMCEControl(id);
506                 document.getElementById(id).value = htm;
507                 --tinyMCE.idCounter;
508
509                 // Reveal Quicktags and textarea
510                 qt.style.display = 'block';
511                 ta.style.display = 'inline';
512
513                 // Set the textarea height to match the rich editor
514                 y2 = ta.offsetTop + ta.offsetHeight;
515                 ta.style.height = (ta.clientHeight + y1 - y2) + 'px';
516
517                 // Tweak the widths
518                 ta.parentNode.style.paddingRight = '12px';
519
520                 if ( tinyMCE.isMSIE && !tinyMCE.isOpera ) {
521                 } else {
522                         // Unlock the fieldset's height
523                         pdr.style.height = 'auto';
524                         pdr.style.overflow = 'display';
525                 }
526         } else {
527                 edToggle(P, H);
528                 edCloseAllTags(); // :-(
529
530                 if ( tinyMCE.isMSIE && !tinyMCE.isOpera ) {
531                 } else {
532                         // Lock the fieldset's height
533                         pdr.style.height = pdr.clientHeight + 'px';
534                         pdr.style.overflow = 'hidden';
535                 }
536
537                 // Hide Quicktags and textarea
538                 qt.style.display = 'none';
539                 ta.style.display = 'none';
540
541                 // Tweak the widths
542                 ta.parentNode.style.paddingRight = '0px';
543
544                 // Load the rich editor with formatted html
545                 if ( tinyMCE.isMSIE ) {
546                         ta.value = wpautop(ta.value);
547                         tinyMCE.addMCEControl(ta, id);
548                 } else {
549                         htm = wpautop(ta.value);
550                         tinyMCE.addMCEControl(ta, id);
551                         tinyMCE.getInstanceById(id).execCommand('mceSetContent', null, htm);
552                 }
553
554                 if ( tinyMCE.isMSIE && !tinyMCE.isOpera ) {
555                 } else {
556                         // Unlock the fieldset's height
557                         pdr.style.height = 'auto';
558                         pdr.style.overflow = 'display';
559                 }
560         }
561 }
562
563 function edToggle(A, B) {
564         A.className = 'edButtonFore';
565         B.className = 'edButtonBack';
566
567         B.onclick = A.onclick;
568         A.onclick = null;
569 }
570
571 function wpautop(pee) {
572         pee = pee + "\n\n";
573         pee = pee.replace(new RegExp('<br />\\s*<br />', 'gi'), "\n\n");
574         pee = pee.replace(new RegExp('(<(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)', 'gi'), "\n$1"); 
575         pee = pee.replace(new RegExp('(</(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])>)', 'gi'), "$1\n\n");
576         pee = pee.replace(new RegExp("\\r\\n|\\r", 'g'), "\n");
577         pee = pee.replace(new RegExp("\\n\\s*\\n+", 'g'), "\n\n");
578         pee = pee.replace(new RegExp('([\\s\\S]+?)\\n\\n', 'mg'), "<p>$1</p>\n");
579         pee = pee.replace(new RegExp('<p>\\s*?</p>', 'gi'), '');
580         pee = pee.replace(new RegExp('<p>\\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\\s*</p>', 'gi'), "$1");
581         pee = pee.replace(new RegExp("<p>(<li.+?)</p>", 'gi'), "$1");
582         pee = pee.replace(new RegExp('<p><blockquote([^>]*)>', 'gi'), "<blockquote$1><p>");
583         pee = pee.replace(new RegExp('</blockquote></p>', 'gi'), '</p></blockquote>');
584         pee = pee.replace(new RegExp('<p>\\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)', 'gi'), "$1");
585         pee = pee.replace(new RegExp('(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\\s*</p>', 'gi'), "$1"); 
586         pee = pee.replace(new RegExp('\\s*\\n', 'gi'), "<br />\n");
587         pee = pee.replace(new RegExp('(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\\s*<br />', 'gi'), "$1");
588         pee = pee.replace(new RegExp('<br />(\\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)', 'gi'), '$1');
589         pee = pee.replace(new RegExp('^((?:&nbsp;)*)\\s', 'mg'), '$1&nbsp;');
590         //pee = pee.replace(new RegExp('(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' "); // Hmm...
591         return pee;
592 }