annotate media/js/tiny_mce/plugins/paste/editor_plugin_src.js @ 150:b43e1288ff80

Fix #33; use $.ajax instead of $.post so we can handle errors. Also, for some reason comparing objects in a template doesn't work now. Have to compare id fields.
author Brian Neal <bgneal@gmail.com>
date Thu, 17 Dec 2009 04:14:16 +0000
parents a5b4c5ce0658
children 149c3567fec1
rev   line source
bgneal@45 1 /**
bgneal@45 2 * $Id: editor_plugin_src.js 919 2008-09-08 20:31:23Z spocke $
bgneal@45 3 *
bgneal@45 4 * @author Moxiecode
bgneal@45 5 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
bgneal@45 6 */
bgneal@45 7
bgneal@45 8 (function() {
bgneal@45 9 var Event = tinymce.dom.Event;
bgneal@45 10
bgneal@45 11 tinymce.create('tinymce.plugins.PastePlugin', {
bgneal@45 12 init : function(ed, url) {
bgneal@45 13 var t = this;
bgneal@45 14
bgneal@45 15 t.editor = ed;
bgneal@45 16
bgneal@45 17 // Register commands
bgneal@45 18 ed.addCommand('mcePasteText', function(ui, v) {
bgneal@45 19 if (ui) {
bgneal@45 20 if ((ed.getParam('paste_use_dialog', true)) || (!tinymce.isIE)) {
bgneal@45 21 ed.windowManager.open({
bgneal@45 22 file : url + '/pastetext.htm',
bgneal@45 23 width : 450,
bgneal@45 24 height : 400,
bgneal@45 25 inline : 1
bgneal@45 26 }, {
bgneal@45 27 plugin_url : url
bgneal@45 28 });
bgneal@45 29 } else
bgneal@45 30 t._insertText(clipboardData.getData("Text"), true);
bgneal@45 31 } else
bgneal@45 32 t._insertText(v.html, v.linebreaks);
bgneal@45 33 });
bgneal@45 34
bgneal@45 35 ed.addCommand('mcePasteWord', function(ui, v) {
bgneal@45 36 if (ui) {
bgneal@45 37 if ((ed.getParam('paste_use_dialog', true)) || (!tinymce.isIE)) {
bgneal@45 38 ed.windowManager.open({
bgneal@45 39 file : url + '/pasteword.htm',
bgneal@45 40 width : 450,
bgneal@45 41 height : 400,
bgneal@45 42 inline : 1
bgneal@45 43 }, {
bgneal@45 44 plugin_url : url
bgneal@45 45 });
bgneal@45 46 } else
bgneal@45 47 t._insertText(t._clipboardHTML());
bgneal@45 48 } else
bgneal@45 49 t._insertWordContent(v);
bgneal@45 50 });
bgneal@45 51
bgneal@45 52 ed.addCommand('mceSelectAll', function() {
bgneal@45 53 ed.execCommand('selectall');
bgneal@45 54 });
bgneal@45 55
bgneal@45 56 // Register buttons
bgneal@45 57 ed.addButton('pastetext', {title : 'paste.paste_text_desc', cmd : 'mcePasteText', ui : true});
bgneal@45 58 ed.addButton('pasteword', {title : 'paste.paste_word_desc', cmd : 'mcePasteWord', ui : true});
bgneal@45 59 ed.addButton('selectall', {title : 'paste.selectall_desc', cmd : 'mceSelectAll'});
bgneal@45 60
bgneal@45 61 if (ed.getParam("paste_auto_cleanup_on_paste", false)) {
bgneal@45 62 ed.onPaste.add(function(ed, e) {
bgneal@45 63 return t._handlePasteEvent(e)
bgneal@45 64 });
bgneal@45 65 }
bgneal@45 66
bgneal@45 67 if (!tinymce.isIE && ed.getParam("paste_auto_cleanup_on_paste", false)) {
bgneal@45 68 // Force paste dialog if non IE browser
bgneal@45 69 ed.onKeyDown.add(function(ed, e) {
bgneal@45 70 if (e.ctrlKey && e.keyCode == 86) {
bgneal@45 71 window.setTimeout(function() {
bgneal@45 72 ed.execCommand("mcePasteText", true);
bgneal@45 73 }, 1);
bgneal@45 74
bgneal@45 75 Event.cancel(e);
bgneal@45 76 }
bgneal@45 77 });
bgneal@45 78 }
bgneal@45 79 },
bgneal@45 80
bgneal@45 81 getInfo : function() {
bgneal@45 82 return {
bgneal@45 83 longname : 'Paste text/word',
bgneal@45 84 author : 'Moxiecode Systems AB',
bgneal@45 85 authorurl : 'http://tinymce.moxiecode.com',
bgneal@45 86 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste',
bgneal@45 87 version : tinymce.majorVersion + "." + tinymce.minorVersion
bgneal@45 88 };
bgneal@45 89 },
bgneal@45 90
bgneal@45 91 // Private methods
bgneal@45 92
bgneal@45 93 _handlePasteEvent : function(e) {
bgneal@45 94 var html = this._clipboardHTML(), ed = this.editor, sel = ed.selection, r;
bgneal@45 95
bgneal@45 96 // Removes italic, strong etc, the if was needed due to bug #1437114
bgneal@45 97 if (ed && (r = sel.getRng()) && r.text.length > 0)
bgneal@45 98 ed.execCommand('delete');
bgneal@45 99
bgneal@45 100 if (html && html.length > 0)
bgneal@45 101 ed.execCommand('mcePasteWord', false, html);
bgneal@45 102
bgneal@45 103 return Event.cancel(e);
bgneal@45 104 },
bgneal@45 105
bgneal@45 106 _insertText : function(content, bLinebreaks) {
bgneal@45 107 content = this.editor.dom.encode(content);
bgneal@45 108
bgneal@45 109 if (content && content.length > 0) {
bgneal@45 110 // Delete any highlighted text before pasting
bgneal@45 111 if (!this.editor.selection.isCollapsed())
bgneal@45 112 this.editor.execCommand("Delete");
bgneal@45 113
bgneal@45 114 if (bLinebreaks) {
bgneal@45 115 // Special paragraph treatment
bgneal@45 116 if (this.editor.getParam("paste_create_paragraphs", true)) {
bgneal@45 117 var rl = this.editor.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(',');
bgneal@45 118 for (var i=0; i<rl.length; i+=2)
bgneal@45 119 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]);
bgneal@45 120
bgneal@45 121 content = content.replace(/\r\n\r\n/g, '</p><p>');
bgneal@45 122 content = content.replace(/\r\r/g, '</p><p>');
bgneal@45 123 content = content.replace(/\n\n/g, '</p><p>');
bgneal@45 124
bgneal@45 125 // Has paragraphs
bgneal@45 126 if ((pos = content.indexOf('</p><p>')) != -1) {
bgneal@45 127 this.editor.execCommand("Delete");
bgneal@45 128
bgneal@45 129 var node = this.editor.selection.getNode();
bgneal@45 130
bgneal@45 131 // Get list of elements to break
bgneal@45 132 var breakElms = [];
bgneal@45 133
bgneal@45 134 do {
bgneal@45 135 if (node.nodeType == 1) {
bgneal@45 136 // Don't break tables and break at body
bgneal@45 137 if (node.nodeName == "TD" || node.nodeName == "BODY")
bgneal@45 138 break;
bgneal@45 139
bgneal@45 140 breakElms[breakElms.length] = node;
bgneal@45 141 }
bgneal@45 142 } while(node = node.parentNode);
bgneal@45 143
bgneal@45 144 var before = "", after = "</p>";
bgneal@45 145 before += content.substring(0, pos);
bgneal@45 146
bgneal@45 147 for (var i=0; i<breakElms.length; i++) {
bgneal@45 148 before += "</" + breakElms[i].nodeName + ">";
bgneal@45 149 after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">";
bgneal@45 150 }
bgneal@45 151
bgneal@45 152 before += "<p>";
bgneal@45 153 content = before + content.substring(pos+7) + after;
bgneal@45 154 }
bgneal@45 155 }
bgneal@45 156
bgneal@45 157 if (this.editor.getParam("paste_create_linebreaks", true)) {
bgneal@45 158 content = content.replace(/\r\n/g, '<br />');
bgneal@45 159 content = content.replace(/\r/g, '<br />');
bgneal@45 160 content = content.replace(/\n/g, '<br />');
bgneal@45 161 }
bgneal@45 162 }
bgneal@45 163
bgneal@45 164 this.editor.execCommand("mceInsertRawHTML", false, content);
bgneal@45 165 }
bgneal@45 166 },
bgneal@45 167
bgneal@45 168 _insertWordContent : function(content) {
bgneal@45 169 var t = this, ed = t.editor;
bgneal@45 170
bgneal@45 171 if (content && content.length > 0) {
bgneal@45 172 // Cleanup Word content
bgneal@45 173 var bull = String.fromCharCode(8226);
bgneal@45 174 var middot = String.fromCharCode(183);
bgneal@45 175
bgneal@45 176 if (ed.getParam('paste_insert_word_content_callback'))
bgneal@45 177 content = ed.execCallback('paste_insert_word_content_callback', 'before', content);
bgneal@45 178
bgneal@45 179 var rl = ed.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\x93|\x94|\u201c|\u201d,",\x60|\x91|\x92|\u2018|\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(',');
bgneal@45 180 for (var i=0; i<rl.length; i+=2)
bgneal@45 181 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]);
bgneal@45 182
bgneal@45 183 if (this.editor.getParam("paste_convert_headers_to_strong", false)) {
bgneal@45 184 content = content.replace(new RegExp('<p class=MsoHeading.*?>(.*?)<\/p>', 'gi'), '<p><b>$1</b></p>');
bgneal@45 185 }
bgneal@45 186
bgneal@45 187 content = content.replace(new RegExp('tab-stops: list [0-9]+.0pt">', 'gi'), '">' + "--list--");
bgneal@45 188 content = content.replace(new RegExp(bull + "(.*?)<BR>", "gi"), "<p>" + middot + "$1</p>");
bgneal@45 189 content = content.replace(new RegExp('<SPAN style="mso-list: Ignore">', 'gi'), "<span>" + bull); // Covert to bull list
bgneal@45 190 content = content.replace(/<o:p><\/o:p>/gi, "");
bgneal@45 191 content = content.replace(new RegExp('<br style="page-break-before: always;.*>', 'gi'), '-- page break --'); // Replace pagebreaks
bgneal@45 192 content = content.replace(/<!--([\s\S]*?)-->|<style>[\s\S]*?<\/style>/g, ""); // Word comments
bgneal@45 193 content = content.replace(/<(meta|link)[^>]+>/g, ""); // Header elements
bgneal@45 194
bgneal@45 195 if (this.editor.getParam("paste_remove_spans", true))
bgneal@45 196 content = content.replace(/<\/?span[^>]*>/gi, "");
bgneal@45 197
bgneal@45 198 if (this.editor.getParam("paste_remove_styles", true))
bgneal@45 199 content = content.replace(new RegExp('<(\\w[^>]*) style="([^"]*)"([^>]*)', 'gi'), "<$1$3");
bgneal@45 200
bgneal@45 201 content = content.replace(/<\/?font[^>]*>/gi, "");
bgneal@45 202
bgneal@45 203 // Strips class attributes.
bgneal@45 204 switch (this.editor.getParam("paste_strip_class_attributes", "all")) {
bgneal@45 205 case "all":
bgneal@45 206 content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
bgneal@45 207 break;
bgneal@45 208
bgneal@45 209 case "mso":
bgneal@45 210 content = content.replace(new RegExp('<(\\w[^>]*) class="?mso([^ |>]*)([^>]*)', 'gi'), "<$1$3");
bgneal@45 211 break;
bgneal@45 212 }
bgneal@45 213
bgneal@45 214 content = content.replace(new RegExp('href="?' + this._reEscape("" + document.location) + '', 'gi'), 'href="' + this.editor.documentBaseURI.getURI());
bgneal@45 215 content = content.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");
bgneal@45 216 content = content.replace(/<\\?\?xml[^>]*>/gi, "");
bgneal@45 217 content = content.replace(/<\/?\w+:[^>]*>/gi, "");
bgneal@45 218 content = content.replace(/-- page break --\s*<p>&nbsp;<\/p>/gi, ""); // Remove pagebreaks
bgneal@45 219 content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks
bgneal@45 220
bgneal@45 221 // content = content.replace(/\/?&nbsp;*/gi, ""); &nbsp;
bgneal@45 222 // content = content.replace(/<p>&nbsp;<\/p>/gi, '');
bgneal@45 223
bgneal@45 224 if (!this.editor.getParam('force_p_newlines')) {
bgneal@45 225 content = content.replace('', '' ,'gi');
bgneal@45 226 content = content.replace('</p>', '<br /><br />' ,'gi');
bgneal@45 227 }
bgneal@45 228
bgneal@45 229 if (!tinymce.isIE && !this.editor.getParam('force_p_newlines')) {
bgneal@45 230 content = content.replace(/<\/?p[^>]*>/gi, "");
bgneal@45 231 }
bgneal@45 232
bgneal@45 233 content = content.replace(/<\/?div[^>]*>/gi, "");
bgneal@45 234
bgneal@45 235 // Convert all middlot lists to UL lists
bgneal@45 236 if (this.editor.getParam("paste_convert_middot_lists", true)) {
bgneal@45 237 var div = ed.dom.create("div", null, content);
bgneal@45 238
bgneal@45 239 // Convert all middot paragraphs to li elements
bgneal@45 240 var className = this.editor.getParam("paste_unindented_list_class", "unIndentedList");
bgneal@45 241
bgneal@45 242 while (this._convertMiddots(div, "--list--")) ; // bull
bgneal@45 243 while (this._convertMiddots(div, middot, className)) ; // Middot
bgneal@45 244 while (this._convertMiddots(div, bull)) ; // bull
bgneal@45 245
bgneal@45 246 content = div.innerHTML;
bgneal@45 247 }
bgneal@45 248
bgneal@45 249 // Replace all headers with strong and fix some other issues
bgneal@45 250 if (this.editor.getParam("paste_convert_headers_to_strong", false)) {
bgneal@45 251 content = content.replace(/<h[1-6]>&nbsp;<\/h[1-6]>/gi, '<p>&nbsp;&nbsp;</p>');
bgneal@45 252 content = content.replace(/<h[1-6]>/gi, '<p><b>');
bgneal@45 253 content = content.replace(/<\/h[1-6]>/gi, '</b></p>');
bgneal@45 254 content = content.replace(/<b>&nbsp;<\/b>/gi, '<b>&nbsp;&nbsp;</b>');
bgneal@45 255 content = content.replace(/^(&nbsp;)*/gi, '');
bgneal@45 256 }
bgneal@45 257
bgneal@45 258 content = content.replace(/--list--/gi, ""); // Remove --list--
bgneal@45 259
bgneal@45 260 if (ed.getParam('paste_insert_word_content_callback'))
bgneal@45 261 content = ed.execCallback('paste_insert_word_content_callback', 'after', content);
bgneal@45 262
bgneal@45 263 // Insert cleaned content
bgneal@45 264 this.editor.execCommand("mceInsertContent", false, content);
bgneal@45 265
bgneal@45 266 if (this.editor.getParam('paste_force_cleanup_wordpaste', true)) {
bgneal@45 267 var ed = this.editor;
bgneal@45 268
bgneal@45 269 window.setTimeout(function() {
bgneal@45 270 ed.execCommand("mceCleanup");
bgneal@45 271 }, 1); // Do normal cleanup detached from this thread
bgneal@45 272 }
bgneal@45 273 }
bgneal@45 274 },
bgneal@45 275
bgneal@45 276 _reEscape : function(s) {
bgneal@45 277 var l = "?.\\*[](){}+^$:";
bgneal@45 278 var o = "";
bgneal@45 279
bgneal@45 280 for (var i=0; i<s.length; i++) {
bgneal@45 281 var c = s.charAt(i);
bgneal@45 282
bgneal@45 283 if (l.indexOf(c) != -1)
bgneal@45 284 o += '\\' + c;
bgneal@45 285 else
bgneal@45 286 o += c;
bgneal@45 287 }
bgneal@45 288
bgneal@45 289 return o;
bgneal@45 290 },
bgneal@45 291
bgneal@45 292 _convertMiddots : function(div, search, class_name) {
bgneal@45 293 var ed = this.editor, mdot = String.fromCharCode(183), bull = String.fromCharCode(8226);
bgneal@45 294 var nodes, prevul, i, p, ul, li, np, cp, li;
bgneal@45 295
bgneal@45 296 nodes = div.getElementsByTagName("p");
bgneal@45 297 for (i=0; i<nodes.length; i++) {
bgneal@45 298 p = nodes[i];
bgneal@45 299
bgneal@45 300 // Is middot
bgneal@45 301 if (p.innerHTML.indexOf(search) == 0) {
bgneal@45 302 ul = ed.dom.create("ul");
bgneal@45 303
bgneal@45 304 if (class_name)
bgneal@45 305 ul.className = class_name;
bgneal@45 306
bgneal@45 307 // Add the first one
bgneal@45 308 li = ed.dom.create("li");
bgneal@45 309 li.innerHTML = p.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');
bgneal@45 310 ul.appendChild(li);
bgneal@45 311
bgneal@45 312 // Add the rest
bgneal@45 313 np = p.nextSibling;
bgneal@45 314 while (np) {
bgneal@45 315 // If the node is whitespace, then
bgneal@45 316 // ignore it and continue on.
bgneal@45 317 if (np.nodeType == 3 && new RegExp('^\\s$', 'm').test(np.nodeValue)) {
bgneal@45 318 np = np.nextSibling;
bgneal@45 319 continue;
bgneal@45 320 }
bgneal@45 321
bgneal@45 322 if (search == mdot) {
bgneal@45 323 if (np.nodeType == 1 && new RegExp('^o(\\s+|&nbsp;)').test(np.innerHTML)) {
bgneal@45 324 // Second level of nesting
bgneal@45 325 if (!prevul) {
bgneal@45 326 prevul = ul;
bgneal@45 327 ul = ed.dom.create("ul");
bgneal@45 328 prevul.appendChild(ul);
bgneal@45 329 }
bgneal@45 330 np.innerHTML = np.innerHTML.replace(/^o/, '');
bgneal@45 331 } else {
bgneal@45 332 // Pop the stack if we're going back up to the first level
bgneal@45 333 if (prevul) {
bgneal@45 334 ul = prevul;
bgneal@45 335 prevul = null;
bgneal@45 336 }
bgneal@45 337 // Not element or middot paragraph
bgneal@45 338 if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)
bgneal@45 339 break;
bgneal@45 340 }
bgneal@45 341 } else {
bgneal@45 342 // Not element or middot paragraph
bgneal@45 343 if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)
bgneal@45 344 break;
bgneal@45 345 }
bgneal@45 346
bgneal@45 347 cp = np.nextSibling;
bgneal@45 348 li = ed.dom.create("li");
bgneal@45 349 li.innerHTML = np.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');
bgneal@45 350 np.parentNode.removeChild(np);
bgneal@45 351 ul.appendChild(li);
bgneal@45 352 np = cp;
bgneal@45 353 }
bgneal@45 354
bgneal@45 355 p.parentNode.replaceChild(ul, p);
bgneal@45 356
bgneal@45 357 return true;
bgneal@45 358 }
bgneal@45 359 }
bgneal@45 360
bgneal@45 361 return false;
bgneal@45 362 },
bgneal@45 363
bgneal@45 364 _clipboardHTML : function() {
bgneal@45 365 var div = document.getElementById('_TinyMCE_clipboardHTML');
bgneal@45 366
bgneal@45 367 if (!div) {
bgneal@45 368 var div = document.createElement('DIV');
bgneal@45 369 div.id = '_TinyMCE_clipboardHTML';
bgneal@45 370
bgneal@45 371 with (div.style) {
bgneal@45 372 visibility = 'hidden';
bgneal@45 373 overflow = 'hidden';
bgneal@45 374 position = 'absolute';
bgneal@45 375 width = 1;
bgneal@45 376 height = 1;
bgneal@45 377 }
bgneal@45 378
bgneal@45 379 document.body.appendChild(div);
bgneal@45 380 }
bgneal@45 381
bgneal@45 382 div.innerHTML = '';
bgneal@45 383 var rng = document.body.createTextRange();
bgneal@45 384 rng.moveToElementText(div);
bgneal@45 385 rng.execCommand('Paste');
bgneal@45 386 var html = div.innerHTML;
bgneal@45 387 div.innerHTML = '';
bgneal@45 388 return html;
bgneal@45 389 }
bgneal@45 390 });
bgneal@45 391
bgneal@45 392 // Register plugin
bgneal@45 393 tinymce.PluginManager.add('paste', tinymce.plugins.PastePlugin);
bgneal@45 394 })();