]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js
Wordpress 2.0.11
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / editor_plugin.js
1 /* Import plugin specific language pack */\r
2 tinyMCE.importPluginLanguagePack('wordpress', '');\r
3 \r
4 function TinyMCE_wordpress_initInstance(inst) {\r
5         if (!tinyMCE.settings['wordpress_skip_plugin_css'])\r
6                 tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/wordpress/wordpress.css");\r
7 }\r
8 \r
9 function TinyMCE_wordpress_getControlHTML(control_name) {\r
10         switch (control_name) {\r
11                 case "wordpress":\r
12                         var titleMore = tinyMCE.getLang('lang_wordpress_more_button');\r
13                         var titlePage = tinyMCE.getLang('lang_wordpress_page_button');\r
14                         var titleHelp = tinyMCE.getLang('lang_wordpress_help_button');\r
15                         var buttons = '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcewordpressmore\')" target="_self" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcewordpressmore\');return false;"><img id="{$editor_id}_wordpress_more" src="{$pluginurl}/images/more.gif" title="'+titleMore+'" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" /></a>';\r
16                         // Add this to the buttons var to put the Page button into the toolbar.\r
17                         // '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcewordpresspage\')" target="_self" onclick="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcewordpresspage\');return false;"><img id="{$editor_id}_wordpress_page" src="{$pluginurl}/images/page.gif" title="'+titlePage+'" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" /></a>';\r
18                         return buttons;\r
19         }\r
20 \r
21         return '';\r
22 }\r
23 \r
24 function TinyMCE_wordpress_parseAttributes(attribute_string) {\r
25         var attributeName = "";\r
26         var attributeValue = "";\r
27         var withInName;\r
28         var withInValue;\r
29         var attributes = new Array();\r
30         var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');\r
31         var titleText = tinyMCE.getLang('lang_wordpress_more');\r
32         var titleTextPage = tinyMCE.getLang('lang_wordpress_page');\r
33 \r
34         if (attribute_string == null || attribute_string.length < 2)\r
35                 return null;\r
36 \r
37         withInName = withInValue = false;\r
38 \r
39         for (var i=0; i<attribute_string.length; i++) {\r
40                 var chr = attribute_string.charAt(i);\r
41 \r
42                 if ((chr == '"' || chr == "'") && !withInValue)\r
43                         withInValue = true;\r
44                 else if ((chr == '"' || chr == "'") && withInValue) {\r
45                         withInValue = false;\r
46 \r
47                         var pos = attributeName.lastIndexOf(' ');\r
48                         if (pos != -1)\r
49                                 attributeName = attributeName.substring(pos+1);\r
50 \r
51                         attributes[attributeName.toLowerCase()] = attributeValue.substring(1).toLowerCase();\r
52 \r
53                         attributeName = "";\r
54                         attributeValue = "";\r
55                 } else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)\r
56                         withInName = true;\r
57 \r
58                 if (chr == '=' && withInName)\r
59                         withInName = false;\r
60 \r
61                 if (withInName)\r
62                         attributeName += chr;\r
63 \r
64                 if (withInValue)\r
65                         attributeValue += chr;\r
66         }\r
67 \r
68         return attributes;\r
69 }\r
70 \r
71 function TinyMCE_wordpress_execCommand(editor_id, element, command, user_interface, value) {\r
72         var inst = tinyMCE.getInstanceById(editor_id);\r
73         var focusElm = inst.getFocusElement();\r
74         var doc = inst.getDoc();\r
75 \r
76         function getAttrib(elm, name) {\r
77                 return elm.getAttribute(name) ? elm.getAttribute(name) : "";\r
78         }\r
79 \r
80         // Handle commands\r
81         switch (command) {\r
82                         case "mcewordpressmore":\r
83                                 var flag = "";\r
84                                 var template = new Array();\r
85                                 var altMore = tinyMCE.getLang('lang_wordpress_more_alt');\r
86 \r
87                                 // Is selection a image\r
88                                 if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {\r
89                                         flag = getAttrib(focusElm, 'class');\r
90         \r
91                                         if (flag != 'mce_plugin_wordpress_more') // Not a wordpress\r
92                                                 return true;\r
93         \r
94                                         action = "update";\r
95                                 }\r
96         \r
97                                 html = ''\r
98                                         + '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" '\r
99                                         + ' width="100%" height="10px" '\r
100                                         + 'alt="'+altMore+'" title="'+altMore+'" class="mce_plugin_wordpress_more" name="mce_plugin_wordpress_more" />';\r
101                                 tinyMCE.execCommand("mceInsertContent",true,html);\r
102                                 tinyMCE.selectedInstance.repaint();\r
103                                 return true;\r
104                         case "mcewordpresspage":\r
105                                 var flag = "";\r
106                                 var template = new Array();\r
107                                 var altPage = tinyMCE.getLang('lang_wordpress_more_alt');\r
108         \r
109                                 // Is selection a image\r
110                                 if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {\r
111                                         flag = getAttrib(focusElm, 'name');\r
112         \r
113                                         if (flag != 'mce_plugin_wordpress_page') // Not a wordpress\r
114                                                 return true;\r
115         \r
116                                         action = "update";\r
117                                 }\r
118         \r
119                                 html = ''\r
120                                         + '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" '\r
121                                         + ' width="100%" height="10px" '\r
122                                         + 'alt="'+altPage+'" title="'+altPage+'" class="mce_plugin_wordpress_page" name="mce_plugin_wordpress_page" />';\r
123                                 tinyMCE.execCommand("mceInsertContent",true,html);\r
124                                 tinyMCE.selectedInstance.repaint();\r
125                                 return true;\r
126         }\r
127 \r
128         // Pass to next handler in chain\r
129         return false;\r
130 }\r
131 \r
132 function TinyMCE_wordpress_cleanup(type, content) {\r
133         switch (type) {\r
134         \r
135                 case "insert_to_editor":\r
136                         var startPos = 0;\r
137                         var altMore = tinyMCE.getLang('lang_wordpress_more_alt');\r
138                         var altPage = tinyMCE.getLang('lang_wordpress_page_alt');\r
139 \r
140                         // Parse all <!--more--> tags and replace them with images\r
141                         while ((startPos = content.indexOf('<!--more-->', startPos)) != -1) {\r
142                                 // Insert image\r
143                                 var contentAfter = content.substring(startPos + 11);\r
144                                 content = content.substring(0, startPos);\r
145                                 content += '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" ';\r
146                                 content += ' width="100%" height="10px" ';\r
147                                 content += 'alt="'+altMore+'" title="'+altMore+'" class="mce_plugin_wordpress_more" />';\r
148                                 content += contentAfter;\r
149 \r
150                                 startPos++;\r
151                         }\r
152                         var startPos = 0;\r
153 \r
154                         // Parse all <!--page--> tags and replace them with images\r
155                         while ((startPos = content.indexOf('<!--nextpage-->', startPos)) != -1) {\r
156                                 // Insert image\r
157                                 var contentAfter = content.substring(startPos + 15);\r
158                                 content = content.substring(0, startPos);\r
159                                 content += '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" ';\r
160                                 content += ' width="100%" height="10px" ';\r
161                                 content += 'alt="'+altPage+'" title="'+altPage+'" class="mce_plugin_wordpress_page" />';\r
162                                 content += contentAfter;\r
163 \r
164                                 startPos++;\r
165                         }\r
166 \r
167                         // It's supposed to be WYSIWYG, right?\r
168                         content = content.replace(new RegExp('&', 'g'), '&amp;');\r
169 \r
170                         break;\r
171 \r
172                 case "get_from_editor":\r
173                         // Parse all img tags and replace them with <!--more-->\r
174                         var startPos = -1;\r
175                         while ((startPos = content.indexOf('<img', startPos+1)) != -1) {\r
176                                 var endPos = content.indexOf('/>', startPos);\r
177                                 var attribs = TinyMCE_wordpress_parseAttributes(content.substring(startPos + 4, endPos));\r
178 \r
179                                 if (attribs['class'] == "mce_plugin_wordpress_more") {\r
180                                         endPos += 2;\r
181         \r
182                                         var embedHTML = '<!--more-->';\r
183         \r
184                                         // Insert embed/object chunk\r
185                                         chunkBefore = content.substring(0, startPos);\r
186                                         chunkAfter = content.substring(endPos);\r
187                                         content = chunkBefore + embedHTML + chunkAfter;\r
188                                 }\r
189                                 if (attribs['class'] == "mce_plugin_wordpress_page") {\r
190                                         endPos += 2;\r
191         \r
192                                         var embedHTML = '<!--nextpage-->';\r
193         \r
194                                         // Insert embed/object chunk\r
195                                         chunkBefore = content.substring(0, startPos);\r
196                                         chunkAfter = content.substring(endPos);\r
197                                         content = chunkBefore + embedHTML + chunkAfter;\r
198                                 }\r
199                         }\r
200 \r
201                         // If it says & in the WYSIWYG editor, it should say &amp; in the html.\r
202                         content = content.replace(new RegExp('&', 'g'), '&amp;');\r
203                         content = content.replace(new RegExp('&amp;nbsp;', 'g'), '&nbsp;');\r
204 \r
205                         // Remove anonymous, empty paragraphs.\r
206                         content = content.replace(new RegExp('<p>(\\s|&nbsp;)*</p>', 'mg'), '');\r
207 \r
208                         // Handle table badness.\r
209                         content = content.replace(new RegExp('<(table( [^>]*)?)>.*?<((tr|thead)( [^>]*)?)>', 'mg'), '<$1><$3>');\r
210                         content = content.replace(new RegExp('<(tr|thead|tfoot)>.*?<((td|th)( [^>]*)?)>', 'mg'), '<$1><$2>');\r
211                         content = content.replace(new RegExp('</(td|th)>.*?<(td( [^>]*)?|th( [^>]*)?|/tr|/thead|/tfoot)>', 'mg'), '</$1><$2>');\r
212                         content = content.replace(new RegExp('</tr>.*?<(tr|/table)>', 'mg'), '</tr><$1>');\r
213                         content = content.replace(new RegExp('<(/?(table|tbody|tr|th|td)[^>]*)>(\\s*|(<br ?/?>)*)*', 'g'), '<$1>');\r
214 \r
215                         // Pretty it up for the source editor.\r
216                         var blocklist = 'blockquote|ul|ol|li|table|thead|tr|th|td|div|h\\d|pre|p';\r
217                         content = content.replace(new RegExp('\\s*</('+blocklist+')>\\s*', 'mg'), '</$1>\n');\r
218                         content = content.replace(new RegExp('\\s*<(('+blocklist+')[^>]*)>\\s*', 'mg'), '\n<$1>');\r
219                         content = content.replace(new RegExp('<((li|/?tr|/?thead|/?tfoot)( [^>]*)?)>', 'g'), '\t<$1>');\r
220                         content = content.replace(new RegExp('<((td|th)( [^>]*)?)>', 'g'), '\t\t<$1>');\r
221                         content = content.replace(new RegExp('\\s*<br ?/?>\\s*', 'mg'), '<br />\n');\r
222                         content = content.replace(new RegExp('^\\s*', ''), '');\r
223                         content = content.replace(new RegExp('\\s*$', ''), '');\r
224                         \r
225                         break;\r
226         }\r
227 \r
228         // Pass through to next handler in chain\r
229         return content;\r
230 }\r
231 \r
232 function TinyMCE_wordpress_handleNodeChange(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {\r
233         function getAttrib(elm, name) {\r
234                 return elm.getAttribute(name) ? elm.getAttribute(name) : "";\r
235         }\r
236 \r
237         tinyMCE.switchClassSticky(editor_id + '_wordpress_more', 'mceButtonNormal');\r
238         tinyMCE.switchClassSticky(editor_id + '_wordpress_page', 'mceButtonNormal');\r
239 \r
240         if (node == null)\r
241                 return;\r
242 \r
243         do {\r
244                 if (node.nodeName.toLowerCase() == "img" && getAttrib(node, 'class').indexOf('mce_plugin_wordpress_more') == 0)\r
245                         tinyMCE.switchClassSticky(editor_id + '_wordpress_more', 'mceButtonSelected');\r
246                 if (node.nodeName.toLowerCase() == "img" && getAttrib(node, 'class').indexOf('mce_plugin_wordpress_page') == 0)\r
247                         tinyMCE.switchClassSticky(editor_id + '_wordpress_page', 'mceButtonSelected');\r
248         } while ((node = node.parentNode));\r
249 \r
250         return true;\r
251 }\r
252 \r
253 function wp_save_callback(el, content, body) {\r
254         // We have a TON of cleanup to do.\r
255 \r
256         // Mark </p> if it has any attributes.\r
257         content = content.replace(new RegExp('(<p[^>]+>.*?)</p>', 'mg'), '$1</p#>');\r
258 \r
259         // Decode the ampersands of time.\r
260         content = content.replace(new RegExp('&amp;', 'g'), '&');\r
261 \r
262         // Get it ready for wpautop.\r
263         content = content.replace(new RegExp('[\\s]*<p>[\\s]*', 'mgi'), '');\r
264         content = content.replace(new RegExp('[\\s]*</p>[\\s]*', 'mgi'), '\n\n');\r
265         content = content.replace(new RegExp('\\n\\s*\\n\\s*\\n*', 'mgi'), '\n\n');\r
266         content = content.replace(new RegExp('\\s*<br ?/?>\\s*', 'gi'), '\n');\r
267 \r
268         // Fix some block element newline issues\r
269         var blocklist = 'blockquote|ul|ol|li|table|thead|tr|th|td|div|h\\d|pre';\r
270         content = content.replace(new RegExp('\\s*<(('+blocklist+') ?[^>]*)\\s*>', 'mg'), '\n<$1>');\r
271         content = content.replace(new RegExp('\\s*</('+blocklist+')>\\s*', 'mg'), '</$1>\n');\r
272         content = content.replace(new RegExp('<li>', 'g'), '\t<li>');\r
273 \r
274         // Unmark special paragraph closing tags\r
275         content = content.replace(new RegExp('</p#>', 'g'), '</p>\n');\r
276         content = content.replace(new RegExp('\\s*(<p[^>]+>.*</p>)', 'mg'), '\n$1');\r
277 \r
278         // Trim any whitespace\r
279         content = content.replace(new RegExp('^\\s*', ''), '');\r
280         content = content.replace(new RegExp('\\s*$', ''), '');\r
281 \r
282         // Hope.\r
283         return content;\r
284 \r
285 }\r