2 * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $
\r
5 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
\r
8 /* Import plugin specific language pack */
\r
9 tinyMCE.importPluginLanguagePack('paste');
\r
11 var TinyMCE_PastePlugin = {
\r
12 getInfo : function() {
\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
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
27 getControlHTML : function(cn) {
\r
30 return tinyMCE.getButtonHTML(cn, 'lang_paste_text_desc', '{$pluginurl}/images/pastetext.gif', 'mcePasteText', true);
\r
33 return tinyMCE.getButtonHTML(cn, 'lang_paste_word_desc', '{$pluginurl}/images/pasteword.gif', 'mcePasteWord', true);
\r
36 return tinyMCE.getButtonHTML(cn, 'lang_selectall_desc', '{$pluginurl}/images/selectall.gif', 'mceSelectAll', true);
\r
42 execCommand : function(editor_id, element, command, user_interface, value) {
\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
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
57 TinyMCE_PastePlugin._insertText(value['html'], value['linebreaks']);
\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
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
74 TinyMCE_PastePlugin._insertWordContent(value);
\r
78 case "mceSelectAll":
\r
79 tinyMCE.execInstanceCommand(editor_id, 'selectall');
\r
84 // Pass to next handler in chain
\r
88 // Private plugin internal methods
\r
90 _handlePasteEvent : function(e) {
\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
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
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
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
138 breakElms[breakElms.length] = node;
\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
151 content = before + content.substring(pos+7) + after;
\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
162 tinyMCE.execCommand("mceInsertRawHTML", false, content);
\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
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
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
202 content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
\r
206 content = content.replace(new RegExp('<(\\w[^>]*) class="?mso([^ |>]*)([^>]*)', 'gi'), "<$1$3");
\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> <\/p>/gi, ""); // Remove pagebreaks
\r
215 content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks
\r
217 // content = content.replace(/\/? */gi, "");
\r
218 // content = content.replace(/<p> <\/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
225 if (!tinyMCE.isMSIE && !tinyMCE.settings['force_p_newlines']) {
\r
226 content = content.replace(/<\/?p[^>]*>/gi, "");
\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
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]> <\/h[1-6]>/gi, '<p> </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> <\/b>/gi, '<b> </b>');
\r
252 content = content.replace(/^( )*/gi, '');
\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
268 _reEscape : function(s) {
\r
269 var l = "?.\\*[](){}+^$:";
\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
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
290 for (var i=0; i<nodes.length; i++) {
\r
294 if (p.innerHTML.indexOf(search) == 0) {
\r
295 var ul = document.createElement("ul");
\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--| ', "gi"), '');
\r
303 ul.appendChild(li);
\r
306 var np = p.nextSibling;
\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
315 if (search == mdot) {
\r
316 if (np.nodeType == 1 && new RegExp('^o(\\s+| )').test(np.innerHTML)) {
\r
317 // Second level of nesting
\r
320 ul = document.createElement("ul");
\r
321 prevul.appendChild(ul);
\r
323 np.innerHTML = np.innerHTML.replace(/^o/, '');
\r
325 // Pop the stack if we're going back up to the first level
\r
330 // Not element or middot paragraph
\r
331 if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)
\r
335 // Not element or middot paragraph
\r
336 if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)
\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--| ', "gi"), '');
\r
343 np.parentNode.removeChild(np);
\r
344 ul.appendChild(li);
\r
348 p.parentNode.replaceChild(ul, p);
\r
357 _clipboardHTML : function() {
\r
358 var div = document.getElementById('_TinyMCE_clipboardHTML');
\r
361 var div = document.createElement('DIV');
\r
362 div.id = '_TinyMCE_clipboardHTML';
\r
365 visibility = 'hidden';
\r
366 overflow = 'hidden';
\r
367 position = 'absolute';
\r
372 document.body.appendChild(div);
\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
385 tinyMCE.addPlugin("paste", TinyMCE_PastePlugin);
\r