comparison media/js/tiny_mce/plugins/paste/editor_plugin_src.js @ 45:a5b4c5ce0658

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