1 /* Import plugin specific language pack */
2 tinyMCE.importPluginLanguagePack('paste', 'en,sv,cs,zh_cn,fr_ca');
4 function TinyMCE_paste_getInfo() {
6 longname : 'Paste text/word',
7 author : 'Moxiecode Systems',
8 authorurl : 'http://tinymce.moxiecode.com',
9 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_paste.html',
10 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
14 function TinyMCE_paste_initInstance(inst) {
15 if (tinyMCE.isMSIE && tinyMCE.getParam("paste_auto_cleanup_on_paste", false))
16 tinyMCE.addEvent(inst.getBody(), "paste", TinyMCE_paste_handleEvent);
19 function TinyMCE_paste_handleEvent(e) {
22 var html = TinyMCE_paste__clipboardHTML();
24 // Removes italic, strong etc
25 tinyMCE.execCommand('delete');
27 if (html && html.length > 0)
28 tinyMCE.execCommand('mcePasteWord', false, html);
30 tinyMCE.cancelEvent(e);
37 function TinyMCE_paste_getControlHTML(control_name) {
38 switch (control_name) {
40 return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcePasteText\', true);" onmousedown="return false;"><img id="{$editor_id}pastetext" src="{$pluginurl}/images/pastetext.gif" title="{$lang_paste_text_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreClass(this);" /></a>';
43 return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcePasteWord\', true);" onmousedown="return false;"><img id="{$editor_id}pasteword" src="{$pluginurl}/images/pasteword.gif" title="{$lang_paste_word_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreClass(this);" /></a>';
46 return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceSelectAll\');" onmousedown="return false;"><img id="{$editor_id}selectall" src="{$pluginurl}/images/selectall.gif" title="{$lang_selectall_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreClass(this);" /></a>';
52 function TinyMCE_paste_execCommand(editor_id, element, command, user_interface, value) {
56 if (tinyMCE.isMSIE && !tinyMCE.getParam('paste_use_dialog', false))
57 TinyMCE_paste__insertText(clipboardData.getData("Text"), true);
59 var template = new Array();
60 template['file'] = '../../plugins/paste/pastetext.htm'; // Relative to theme
61 template['width'] = 450;
62 template['height'] = 400;
64 tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'});
67 TinyMCE_paste__insertText(value['html'], value['linebreaks']);
73 if (tinyMCE.isMSIE && !tinyMCE.getParam('paste_use_dialog', false)) {
74 var html = TinyMCE_paste__clipboardHTML();
76 if (html && html.length > 0)
77 TinyMCE_paste__insertWordContent(html);
79 var template = new Array();
80 template['file'] = '../../plugins/paste/pasteword.htm'; // Relative to theme
81 template['width'] = 450;
82 template['height'] = 400;
84 tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'});
87 TinyMCE_paste__insertWordContent(value);
92 tinyMCE.execInstanceCommand(editor_id, 'selectall');
97 // Pass to next handler in chain
101 function TinyMCE_paste__insertText(content, bLinebreaks) {
102 if (content && content.length > 0) {
104 // Special paragraph treatment
105 if (tinyMCE.getParam("plaintext_create_paragraphs", true)) {
106 content = tinyMCE.regexpReplace(content, "\r\n\r\n", "</p><p>", "gi");
107 content = tinyMCE.regexpReplace(content, "\r\r", "</p><p>", "gi");
108 content = tinyMCE.regexpReplace(content, "\n\n", "</p><p>", "gi");
111 if ((pos = content.indexOf('</p><p>')) != -1) {
112 tinyMCE.execCommand("Delete");
114 var node = tinyMCE.selectedInstance.getFocusElement();
116 // Get list of elements to break
117 var breakElms = new Array();
120 if (node.nodeType == 1) {
121 // Don't break tables and break at body
122 if (node.nodeName == "TD" || node.nodeName == "BODY")
125 breakElms[breakElms.length] = node;
127 } while(node = node.parentNode);
129 var before = "", after = "</p>";
130 before += content.substring(0, pos);
132 for (var i=0; i<breakElms.length; i++) {
133 before += "</" + breakElms[i].nodeName + ">";
134 after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">";
138 content = before + content.substring(pos+7) + after;
142 content = tinyMCE.regexpReplace(content, "\r\n", "<br />", "gi");
143 content = tinyMCE.regexpReplace(content, "\r", "<br />", "gi");
144 content = tinyMCE.regexpReplace(content, "\n", "<br />", "gi");
147 tinyMCE.execCommand("mceInsertRawHTML", false, content);
151 function TinyMCE_paste__insertWordContent(content) {
152 if (content && content.length > 0) {
153 // Cleanup Word content
154 var bull = String.fromCharCode(8226);
155 var middot = String.fromCharCode(183);
157 if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) {
158 content = content.replace(new RegExp('<p class=MsoHeading.*?>(.*?)<\/p>', 'gi'), '<p><b>$1</b></p>');
161 content = content.replace(new RegExp('tab-stops: list [0-9]+.0pt">', 'gi'), '">' + "--list--");
162 content = content.replace(new RegExp(bull + "(.*?)<BR>", "gi"), "<p>" + middot + "$1</p>");
163 content = content.replace(new RegExp('<SPAN style="mso-list: Ignore">', 'gi'), "<span>" + bull); // Covert to bull list
164 content = content.replace(/<o:p><\/o:p>/gi, "");
165 content = content.replace(new RegExp('<br style="page-break-before: always;.*>', 'gi'), '-- page break --'); // Replace pagebreaks
166 content = content.replace(new RegExp('<(!--)([^>]*)(--)>', 'g'), ""); // Word comments
167 content = content.replace(/<\/?span[^>]*>/gi, "");
168 content = content.replace(new RegExp('<(\w[^>]*) style="([^"]*)"([^>]*)', 'gi'), "<$1$3");
169 content = content.replace(/<\/?font[^>]*>/gi, "");
170 content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
171 content = content.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");
172 content = content.replace(/<\\?\?xml[^>]*>/gi, "");
173 content = content.replace(/<\/?\w+:[^>]*>/gi, "");
174 content = content.replace(/-- page break --\s*<p> <\/p>/gi, ""); // Remove pagebreaks
175 content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks
177 // content = content.replace(/\/? */gi, "");
178 // content = content.replace(/<p> <\/p>/gi, '');
180 if (!tinyMCE.settings['force_p_newlines']) {
181 content = content.replace('', '' ,'gi');
182 content = content.replace('</p>', '<br /><br />' ,'gi');
185 if (!tinyMCE.isMSIE && !tinyMCE.settings['force_p_newlines']) {
186 content = content.replace(/<\/?p[^>]*>/gi, "");
189 content = content.replace(/<\/?div[^>]*>/gi, "");
191 // Convert all middlot lists to UL lists
192 if (tinyMCE.getParam("paste_convert_middot_lists", true)) {
193 var div = document.createElement("div");
194 div.innerHTML = content;
196 // Convert all middot paragraphs to li elements
197 var className = tinyMCE.getParam("paste_unindented_list_class", "unIndentedList");
199 while (TinyMCE_paste_convertMiddots(div, "--list--")) ; // bull
200 while (TinyMCE_paste_convertMiddots(div, middot, className)) ; // Middot
201 while (TinyMCE_paste_convertMiddots(div, bull)) ; // bull
203 content = div.innerHTML;
206 // Replace all headers with strong and fix some other issues
207 if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) {
208 content = content.replace(/<h[1-6]> <\/h[1-6]>/gi, '<p> </p>');
209 content = content.replace(/<h[1-6]>/gi, '<p><b>');
210 content = content.replace(/<\/h[1-6]>/gi, '</b></p>');
211 content = content.replace(/<b> <\/b>/gi, '<b> </b>');
212 content = content.replace(/^( )*/gi, '');
215 content = content.replace(/--list--/gi, ""); // Remove --list--
217 // Insert cleaned content
218 tinyMCE.execCommand("mceInsertContent", false, content);
219 tinyMCE.execCommand("mceCleanup"); // Do normal cleanup
223 function TinyMCE_paste_convertMiddots(div, search, class_name) {
224 var mdot = String.fromCharCode(183);
225 var bull = String.fromCharCode(8226);
227 var nodes = div.getElementsByTagName("p");
228 for (var i=0; i<nodes.length; i++) {
232 if (p.innerHTML.indexOf(search) != -1) {
233 var ul = document.createElement("ul");
236 ul.className = class_name;
239 var li = document.createElement("li");
240 li.innerHTML = p.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--| ', "gi"), '');
244 var np = p.nextSibling;
246 // Not element or middot paragraph
247 if (np.nodeType != 1 || np.innerHTML.indexOf(search) == -1)
250 var cp = np.nextSibling;
251 var li = document.createElement("li");
252 li.innerHTML = np.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--| ', "gi"), '');
253 np.parentNode.removeChild(np);
258 p.parentNode.replaceChild(ul, p);
267 function TinyMCE_paste__clipboardHTML() {
268 var div = document.getElementById('_TinyMCE_clipboardHTML');
271 var div = document.createElement('DIV');
272 div.id = '_TinyMCE_clipboardHTML';
275 visibility = 'hidden';
277 position = 'absolute';
282 document.body.appendChild(div);
286 var rng = document.body.createTextRange();
287 rng.moveToElementText(div);
288 rng.execCommand('Paste');
289 var html = div.innerHTML;