applied my changes - initial import
[boxroom-stian.git] / public / javascripts / tiny_mce / plugins / paste / editor_plugin_src.js
blob1e2583678582896160087d1225b583b3c6623f43
1 /**\r
2  * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $\r
3  *\r
4  * @author Moxiecode\r
5  * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.\r
6  */\r
7 \r
8 /* Import plugin specific language pack */ \r
9 tinyMCE.importPluginLanguagePack('paste');\r
11 var TinyMCE_PastePlugin = {\r
12         getInfo : function() {\r
13                 return {\r
14                         longname : 'Paste text/word',\r
15                         author : 'Moxiecode Systems AB',\r
16                         authorurl : 'http://tinymce.moxiecode.com',\r
17                         infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_paste.html',\r
18                         version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion\r
19                 };\r
20         },\r
22         initInstance : function(inst) {\r
23                 if (tinyMCE.isMSIE && tinyMCE.getParam("paste_auto_cleanup_on_paste", false))\r
24                         tinyMCE.addEvent(inst.getBody(), "paste", TinyMCE_PastePlugin._handlePasteEvent);\r
25         },\r
27         getControlHTML : function(cn) { \r
28                 switch (cn) { \r
29                         case "pastetext":\r
30                                 return tinyMCE.getButtonHTML(cn, 'lang_paste_text_desc', '{$pluginurl}/images/pastetext.gif', 'mcePasteText', true);\r
32                         case "pasteword":\r
33                                 return tinyMCE.getButtonHTML(cn, 'lang_paste_word_desc', '{$pluginurl}/images/pasteword.gif', 'mcePasteWord', true);\r
35                         case "selectall":\r
36                                 return tinyMCE.getButtonHTML(cn, 'lang_selectall_desc', '{$pluginurl}/images/selectall.gif', 'mceSelectAll', true);\r
37                 } \r
39                 return ''; \r
40         },\r
42         execCommand : function(editor_id, element, command, user_interface, value) { \r
43                 switch (command) { \r
44                         case "mcePasteText": \r
45                                 if (user_interface) {\r
46                                         if ((tinyMCE.isMSIE && !tinyMCE.isOpera) && !tinyMCE.getParam('paste_use_dialog', false))\r
47                                                 TinyMCE_PastePlugin._insertText(clipboardData.getData("Text"), true); \r
48                                         else { \r
49                                                 var template = new Array(); \r
50                                                 template['file']        = '../../plugins/paste/pastetext.htm'; // Relative to theme \r
51                                                 template['width']  = 450; \r
52                                                 template['height'] = 400; \r
53                                                 var plain_text = ""; \r
54                                                 tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'}); \r
55                                         }\r
56                                 } else\r
57                                         TinyMCE_PastePlugin._insertText(value['html'], value['linebreaks']);\r
59                                 return true;\r
61                         case "mcePasteWord": \r
62                                 if (user_interface) {\r
63                                         if ((tinyMCE.isMSIE && !tinyMCE.isOpera) && !tinyMCE.getParam('paste_use_dialog', false)) {\r
64                                                 TinyMCE_PastePlugin._insertWordContent(TinyMCE_PastePlugin._clipboardHTML());\r
65                                         } else { \r
66                                                 var template = new Array(); \r
67                                                 template['file']        = '../../plugins/paste/pasteword.htm'; // Relative to theme \r
68                                                 template['width']  = 450; \r
69                                                 template['height'] = 400; \r
70                                                 var plain_text = ""; \r
71                                                 tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'});\r
72                                         }\r
73                                 } else\r
74                                         TinyMCE_PastePlugin._insertWordContent(value);\r
76                                 return true;\r
78                         case "mceSelectAll":\r
79                                 tinyMCE.execInstanceCommand(editor_id, 'selectall'); \r
80                                 return true; \r
82                 } \r
84                 // Pass to next handler in chain \r
85                 return false; \r
86         },\r
88         // Private plugin internal methods\r
90         _handlePasteEvent : function(e) {\r
91                 switch (e.type) {\r
92                         case "paste":\r
93                                 var html = TinyMCE_PastePlugin._clipboardHTML();\r
94                                 var r, inst = tinyMCE.selectedInstance;\r
96                                 // Removes italic, strong etc, the if was needed due to bug #1437114\r
97                                 if (inst && (r = inst.getRng()) && r.text.length > 0)\r
98                                         tinyMCE.execCommand('delete');\r
100                                 if (html && html.length > 0)\r
101                                         tinyMCE.execCommand('mcePasteWord', false, html);\r
103                                 tinyMCE.cancelEvent(e);\r
104                                 return false;\r
105                 }\r
107                 return true;\r
108         },\r
110         _insertText : function(content, bLinebreaks) { \r
111                 if (content && content.length > 0) {\r
112                         if (bLinebreaks) { \r
113                                 // Special paragraph treatment \r
114                                 if (tinyMCE.getParam("paste_create_paragraphs", true)) {\r
115                                         var rl = tinyMCE.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(',');\r
116                                         for (var i=0; i<rl.length; i+=2)\r
117                                                 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]);\r
119                                         content = tinyMCE.regexpReplace(content, "\r\n\r\n", "</p><p>", "gi"); \r
120                                         content = tinyMCE.regexpReplace(content, "\r\r", "</p><p>", "gi"); \r
121                                         content = tinyMCE.regexpReplace(content, "\n\n", "</p><p>", "gi"); \r
123                                         // Has paragraphs \r
124                                         if ((pos = content.indexOf('</p><p>')) != -1) { \r
125                                                 tinyMCE.execCommand("Delete"); \r
127                                                 var node = tinyMCE.selectedInstance.getFocusElement(); \r
129                                                 // Get list of elements to break \r
130                                                 var breakElms = new Array(); \r
132                                                 do { \r
133                                                         if (node.nodeType == 1) { \r
134                                                                 // Don't break tables and break at body \r
135                                                                 if (node.nodeName == "TD" || node.nodeName == "BODY") \r
136                                                                         break; \r
137                 \r
138                                                                 breakElms[breakElms.length] = node; \r
139                                                         } \r
140                                                 } while(node = node.parentNode); \r
142                                                 var before = "", after = "</p>"; \r
143                                                 before += content.substring(0, pos); \r
145                                                 for (var i=0; i<breakElms.length; i++) { \r
146                                                         before += "</" + breakElms[i].nodeName + ">"; \r
147                                                         after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">"; \r
148                                                 } \r
150                                                 before += "<p>"; \r
151                                                 content = before + content.substring(pos+7) + after; \r
152                                         } \r
153                                 } \r
155                                 if (tinyMCE.getParam("paste_create_linebreaks", true)) {\r
156                                         content = tinyMCE.regexpReplace(content, "\r\n", "<br />", "gi"); \r
157                                         content = tinyMCE.regexpReplace(content, "\r", "<br />", "gi"); \r
158                                         content = tinyMCE.regexpReplace(content, "\n", "<br />", "gi"); \r
159                                 }\r
160                         } \r
161                 \r
162                         tinyMCE.execCommand("mceInsertRawHTML", false, content); \r
163                 }\r
164         },\r
166         _insertWordContent : function(content) { \r
167                 if (content && content.length > 0) {\r
168                         // Cleanup Word content\r
169                         var bull = String.fromCharCode(8226);\r
170                         var middot = String.fromCharCode(183);\r
171                         var cb;\r
173                         if ((cb = tinyMCE.getParam("paste_insert_word_content_callback", "")) != "")\r
174                                 content = eval(cb + "('before', content)");\r
176                         var rl = tinyMCE.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(',');\r
177                         for (var i=0; i<rl.length; i+=2)\r
178                                 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]);\r
180                         if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) {\r
181                                 content = content.replace(new RegExp('<p class=MsoHeading.*?>(.*?)<\/p>', 'gi'), '<p><b>$1</b></p>');\r
182                         }\r
184                         content = content.replace(new RegExp('tab-stops: list [0-9]+.0pt">', 'gi'), '">' + "--list--");\r
185                         content = content.replace(new RegExp(bull + "(.*?)<BR>", "gi"), "<p>" + middot + "$1</p>");\r
186                         content = content.replace(new RegExp('<SPAN style="mso-list: Ignore">', 'gi'), "<span>" + bull); // Covert to bull list\r
187                         content = content.replace(/<o:p><\/o:p>/gi, "");\r
188                         content = content.replace(new RegExp('<br style="page-break-before: always;.*>', 'gi'), '-- page break --'); // Replace pagebreaks\r
189                         content = content.replace(new RegExp('<(!--)([^>]*)(--)>', 'g'), "");  // Word comments\r
191                         if (tinyMCE.getParam("paste_remove_spans", true))\r
192                                 content = content.replace(/<\/?span[^>]*>/gi, "");\r
194                         if (tinyMCE.getParam("paste_remove_styles", true))\r
195                                 content = content.replace(new RegExp('<(\\w[^>]*) style="([^"]*)"([^>]*)', 'gi'), "<$1$3");\r
197                         content = content.replace(/<\/?font[^>]*>/gi, "");\r
199                         // Strips class attributes.\r
200                         switch (tinyMCE.getParam("paste_strip_class_attributes", "all")) {\r
201                                 case "all":\r
202                                         content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");\r
203                                         break;\r
205                                 case "mso":\r
206                                         content = content.replace(new RegExp('<(\\w[^>]*) class="?mso([^ |>]*)([^>]*)', 'gi'), "<$1$3");\r
207                                         break;\r
208                         }\r
210                         content = content.replace(new RegExp('href="?' + TinyMCE_PastePlugin._reEscape("" + document.location) + '', 'gi'), 'href="' + tinyMCE.settings['document_base_url']);\r
211                         content = content.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");\r
212                         content = content.replace(/<\\?\?xml[^>]*>/gi, "");\r
213                         content = content.replace(/<\/?\w+:[^>]*>/gi, "");\r
214                         content = content.replace(/-- page break --\s*<p>&nbsp;<\/p>/gi, ""); // Remove pagebreaks\r
215                         content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks\r
217         //              content = content.replace(/\/?&nbsp;*/gi, ""); &nbsp;\r
218         //              content = content.replace(/<p>&nbsp;<\/p>/gi, '');\r
220                         if (!tinyMCE.settings['force_p_newlines']) {\r
221                                 content = content.replace('', '' ,'gi');\r
222                                 content = content.replace('</p>', '<br /><br />' ,'gi');\r
223                         }\r
225                         if (!tinyMCE.isMSIE && !tinyMCE.settings['force_p_newlines']) {\r
226                                 content = content.replace(/<\/?p[^>]*>/gi, "");\r
227                         }\r
229                         content = content.replace(/<\/?div[^>]*>/gi, "");\r
231                         // Convert all middlot lists to UL lists\r
232                         if (tinyMCE.getParam("paste_convert_middot_lists", true)) {\r
233                                 var div = document.createElement("div");\r
234                                 div.innerHTML = content;\r
236                                 // Convert all middot paragraphs to li elements\r
237                                 var className = tinyMCE.getParam("paste_unindented_list_class", "unIndentedList");\r
239                                 while (TinyMCE_PastePlugin._convertMiddots(div, "--list--")) ; // bull\r
240                                 while (TinyMCE_PastePlugin._convertMiddots(div, middot, className)) ; // Middot\r
241                                 while (TinyMCE_PastePlugin._convertMiddots(div, bull)) ; // bull\r
243                                 content = div.innerHTML;\r
244                         }\r
246                         // Replace all headers with strong and fix some other issues\r
247                         if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) {\r
248                                 content = content.replace(/<h[1-6]>&nbsp;<\/h[1-6]>/gi, '<p>&nbsp;&nbsp;</p>');\r
249                                 content = content.replace(/<h[1-6]>/gi, '<p><b>');\r
250                                 content = content.replace(/<\/h[1-6]>/gi, '</b></p>');\r
251                                 content = content.replace(/<b>&nbsp;<\/b>/gi, '<b>&nbsp;&nbsp;</b>');\r
252                                 content = content.replace(/^(&nbsp;)*/gi, '');\r
253                         }\r
255                         content = content.replace(/--list--/gi, ""); // Remove --list--\r
257                         if ((cb = tinyMCE.getParam("paste_insert_word_content_callback", "")) != "")\r
258                                 content = eval(cb + "('after', content)");\r
260                         // Insert cleaned content\r
261                         tinyMCE.execCommand("mceInsertContent", false, content);\r
263                         if (tinyMCE.getParam('paste_force_cleanup_wordpaste', true))\r
264                                 window.setTimeout('tinyMCE.execCommand("mceCleanup");', 1); // Do normal cleanup detached from this thread\r
265                 }\r
266         },\r
268         _reEscape : function(s) {\r
269                 var l = "?.\\*[](){}+^$:";\r
270                 var o = "";\r
272                 for (var i=0; i<s.length; i++) {\r
273                         var c = s.charAt(i);\r
275                         if (l.indexOf(c) != -1)\r
276                                 o += '\\' + c;\r
277                         else\r
278                                 o += c;\r
279                 }\r
281                 return o;\r
282         },\r
284         _convertMiddots : function(div, search, class_name) {\r
285                 var mdot = String.fromCharCode(183);\r
286                 var bull = String.fromCharCode(8226);\r
288                 var nodes = div.getElementsByTagName("p");\r
289                 var prevul;\r
290                 for (var i=0; i<nodes.length; i++) {\r
291                         var p = nodes[i];\r
293                         // Is middot\r
294                         if (p.innerHTML.indexOf(search) == 0) {\r
295                                 var ul = document.createElement("ul");\r
297                                 if (class_name)\r
298                                         ul.className = class_name;\r
300                                 // Add the first one\r
301                                 var li = document.createElement("li");\r
302                                 li.innerHTML = p.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');\r
303                                 ul.appendChild(li);\r
305                                 // Add the rest\r
306                                 var np = p.nextSibling;\r
307                                 while (np) {\r
308                                 // If the node is whitespace, then\r
309                                 // ignore it and continue on.\r
310                                 if (np.nodeType == 3 && new RegExp('^\\s$', 'm').test(np.nodeValue)) {\r
311                                         np = np.nextSibling;\r
312                                         continue;\r
313                                 }\r
315                                         if (search == mdot) {\r
316                                                 if (np.nodeType == 1 && new RegExp('^o(\\s+|&nbsp;)').test(np.innerHTML)) {\r
317                                                         // Second level of nesting\r
318                                                         if (!prevul) {\r
319                                                                 prevul = ul;\r
320                                                                 ul = document.createElement("ul");\r
321                                                                 prevul.appendChild(ul);\r
322                                                         }\r
323                                                         np.innerHTML = np.innerHTML.replace(/^o/, '');\r
324                                                 } else {\r
325                                                         // Pop the stack if we're going back up to the first level\r
326                                                         if (prevul) {\r
327                                                                 ul = prevul;\r
328                                                                 prevul = null;\r
329                                                         }\r
330                                                         // Not element or middot paragraph\r
331                                                         if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)\r
332                                                                 break;\r
333                                                 }\r
334                                         } else {\r
335                                                 // Not element or middot paragraph\r
336                                                 if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)\r
337                                                         break;\r
338                                         }\r
340                                         var cp = np.nextSibling;\r
341                                         var li = document.createElement("li");\r
342                                         li.innerHTML = np.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');\r
343                                         np.parentNode.removeChild(np);\r
344                                         ul.appendChild(li);\r
345                                         np = cp;\r
346                                 }\r
348                                 p.parentNode.replaceChild(ul, p);\r
350                                 return true;\r
351                         }\r
352                 }\r
354                 return false;\r
355         },\r
357         _clipboardHTML : function() {\r
358                 var div = document.getElementById('_TinyMCE_clipboardHTML');\r
360                 if (!div) {\r
361                         var div = document.createElement('DIV');\r
362                         div.id = '_TinyMCE_clipboardHTML';\r
364                         with (div.style) {\r
365                                 visibility = 'hidden';\r
366                                 overflow = 'hidden';\r
367                                 position = 'absolute';\r
368                                 width = 1;\r
369                                 height = 1;\r
370                         }\r
372                         document.body.appendChild(div);\r
373                 }\r
375                 div.innerHTML = '';\r
376                 var rng = document.body.createTextRange();\r
377                 rng.moveToElementText(div);\r
378                 rng.execCommand('Paste');\r
379                 var html = div.innerHTML;\r
380                 div.innerHTML = '';\r
381                 return html;\r
382         }\r
383 };\r
385 tinyMCE.addPlugin("paste", TinyMCE_PastePlugin);\r