1 /** |
1 /** |
2 * $Id: editor_plugin_src.js 919 2008-09-08 20:31:23Z spocke $ |
2 * $Id: editor_plugin_src.js 1225 2009-09-07 19:06:19Z spocke $ |
3 * |
3 * |
4 * @author Moxiecode |
4 * @author Moxiecode |
5 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved. |
5 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved. |
6 */ |
6 */ |
7 |
7 |
8 (function() { |
8 (function() { |
9 var Event = tinymce.dom.Event; |
9 var each = tinymce.each; |
10 |
10 |
11 tinymce.create('tinymce.plugins.PastePlugin', { |
11 tinymce.create('tinymce.plugins.PastePlugin', { |
12 init : function(ed, url) { |
12 init : function(ed, url) { |
13 var t = this; |
13 var t = this, cb; |
14 |
14 |
15 t.editor = ed; |
15 t.editor = ed; |
16 |
16 t.url = url; |
17 // Register commands |
17 |
18 ed.addCommand('mcePasteText', function(ui, v) { |
18 // Setup plugin events |
19 if (ui) { |
19 t.onPreProcess = new tinymce.util.Dispatcher(t); |
20 if ((ed.getParam('paste_use_dialog', true)) || (!tinymce.isIE)) { |
20 t.onPostProcess = new tinymce.util.Dispatcher(t); |
21 ed.windowManager.open({ |
21 |
22 file : url + '/pastetext.htm', |
22 // Register default handlers |
23 width : 450, |
23 t.onPreProcess.add(t._preProcess); |
24 height : 400, |
24 t.onPostProcess.add(t._postProcess); |
25 inline : 1 |
25 |
26 }, { |
26 // Register optional preprocess handler |
27 plugin_url : url |
27 t.onPreProcess.add(function(pl, o) { |
|
28 ed.execCallback('paste_preprocess', pl, o); |
|
29 }); |
|
30 |
|
31 // Register optional postprocess |
|
32 t.onPostProcess.add(function(pl, o) { |
|
33 ed.execCallback('paste_postprocess', pl, o); |
|
34 }); |
|
35 |
|
36 // This function executes the process handlers and inserts the contents |
|
37 function process(o) { |
|
38 var dom = ed.dom; |
|
39 |
|
40 // Execute pre process handlers |
|
41 t.onPreProcess.dispatch(t, o); |
|
42 |
|
43 // Create DOM structure |
|
44 o.node = dom.create('div', 0, o.content); |
|
45 |
|
46 // Execute post process handlers |
|
47 t.onPostProcess.dispatch(t, o); |
|
48 |
|
49 // Serialize content |
|
50 o.content = ed.serializer.serialize(o.node, {getInner : 1}); |
|
51 |
|
52 // Insert cleaned content. We need to handle insertion of contents containing block elements separately |
|
53 if (/<(p|h[1-6]|ul|ol)/.test(o.content)) |
|
54 t._insertBlockContent(ed, dom, o.content); |
|
55 else |
|
56 t._insert(o.content); |
|
57 }; |
|
58 |
|
59 // Add command for external usage |
|
60 ed.addCommand('mceInsertClipboardContent', function(u, o) { |
|
61 process(o); |
|
62 }); |
|
63 |
|
64 // This function grabs the contents from the clipboard by adding a |
|
65 // hidden div and placing the caret inside it and after the browser paste |
|
66 // is done it grabs that contents and processes that |
|
67 function grabContent(e) { |
|
68 var n, or, rng, sel = ed.selection, dom = ed.dom, body = ed.getBody(), posY; |
|
69 |
|
70 if (dom.get('_mcePaste')) |
|
71 return; |
|
72 |
|
73 // Create container to paste into |
|
74 n = dom.add(body, 'div', {id : '_mcePaste'}, '\uFEFF'); |
|
75 |
|
76 // If contentEditable mode we need to find out the position of the closest element |
|
77 if (body != ed.getDoc().body) |
|
78 posY = dom.getPos(ed.selection.getStart(), body).y; |
|
79 else |
|
80 posY = body.scrollTop; |
|
81 |
|
82 // Styles needs to be applied after the element is added to the document since WebKit will otherwise remove all styles |
|
83 dom.setStyles(n, { |
|
84 position : 'absolute', |
|
85 left : -10000, |
|
86 top : posY, |
|
87 width : 1, |
|
88 height : 1, |
|
89 overflow : 'hidden' |
|
90 }); |
|
91 |
|
92 if (tinymce.isIE) { |
|
93 // Select the container |
|
94 rng = dom.doc.body.createTextRange(); |
|
95 rng.moveToElementText(n); |
|
96 rng.execCommand('Paste'); |
|
97 |
|
98 // Remove container |
|
99 dom.remove(n); |
|
100 |
|
101 // Check if the contents was changed, if it wasn't then clipboard extraction failed probably due |
|
102 // to IE security settings so we pass the junk though better than nothing right |
|
103 if (n.innerHTML === '\uFEFF') { |
|
104 ed.execCommand('mcePasteWord'); |
|
105 e.preventDefault(); |
|
106 return; |
|
107 } |
|
108 |
|
109 // Process contents |
|
110 process({content : n.innerHTML}); |
|
111 |
|
112 // Block the real paste event |
|
113 return tinymce.dom.Event.cancel(e); |
|
114 } else { |
|
115 or = ed.selection.getRng(); |
|
116 |
|
117 // Move caret into hidden div |
|
118 n = n.firstChild; |
|
119 rng = ed.getDoc().createRange(); |
|
120 rng.setStart(n, 0); |
|
121 rng.setEnd(n, 1); |
|
122 sel.setRng(rng); |
|
123 |
|
124 // Wait a while and grab the pasted contents |
|
125 window.setTimeout(function() { |
|
126 var h = '', nl = dom.select('div[id=_mcePaste]'); |
|
127 |
|
128 // WebKit will split the div into multiple ones so this will loop through then all and join them to get the whole HTML string |
|
129 each(nl, function(n) { |
|
130 h += (dom.select('> span.Apple-style-span div', n)[0] || dom.select('> span.Apple-style-span', n)[0] || n).innerHTML; |
28 }); |
131 }); |
29 } else |
132 |
30 t._insertText(clipboardData.getData("Text"), true); |
133 // Remove the nodes |
31 } else |
134 each(nl, function(n) { |
32 t._insertText(v.html, v.linebreaks); |
135 dom.remove(n); |
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 }); |
136 }); |
46 } else |
137 |
47 t._insertText(t._clipboardHTML()); |
138 // Restore the old selection |
48 } else |
139 if (or) |
49 t._insertWordContent(v); |
140 sel.setRng(or); |
50 }); |
141 |
51 |
142 process({content : h}); |
52 ed.addCommand('mceSelectAll', function() { |
143 }, 0); |
53 ed.execCommand('selectall'); |
144 } |
54 }); |
145 }; |
55 |
146 |
56 // Register buttons |
147 // Check if we should use the new auto process method |
57 ed.addButton('pastetext', {title : 'paste.paste_text_desc', cmd : 'mcePasteText', ui : true}); |
148 if (ed.getParam('paste_auto_cleanup_on_paste', true)) { |
58 ed.addButton('pasteword', {title : 'paste.paste_word_desc', cmd : 'mcePasteWord', ui : true}); |
149 // Is it's Opera or older FF use key handler |
59 ed.addButton('selectall', {title : 'paste.selectall_desc', cmd : 'mceSelectAll'}); |
150 if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) { |
60 |
151 ed.onKeyDown.add(function(ed, e) { |
61 if (ed.getParam("paste_auto_cleanup_on_paste", false)) { |
152 if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45)) |
62 ed.onPaste.add(function(ed, e) { |
153 grabContent(e); |
63 return t._handlePasteEvent(e) |
154 }); |
64 }); |
155 } else { |
65 } |
156 // Grab contents on paste event on Gecko and WebKit |
66 |
157 ed.onPaste.addToTop(function(ed, e) { |
67 if (!tinymce.isIE && ed.getParam("paste_auto_cleanup_on_paste", false)) { |
158 return grabContent(e); |
68 // Force paste dialog if non IE browser |
159 }); |
69 ed.onKeyDown.add(function(ed, e) { |
160 } |
70 if (e.ctrlKey && e.keyCode == 86) { |
161 } |
71 window.setTimeout(function() { |
162 |
72 ed.execCommand("mcePasteText", true); |
163 // Block all drag/drop events |
73 }, 1); |
164 if (ed.getParam('paste_block_drop')) { |
74 |
165 ed.onInit.add(function() { |
75 Event.cancel(e); |
166 ed.dom.bind(ed.getBody(), ['dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag'], function(e) { |
76 } |
167 e.preventDefault(); |
77 }); |
168 e.stopPropagation(); |
78 } |
169 |
|
170 return false; |
|
171 }); |
|
172 }); |
|
173 } |
|
174 |
|
175 // Add legacy support |
|
176 t._legacySupport(); |
79 }, |
177 }, |
80 |
178 |
81 getInfo : function() { |
179 getInfo : function() { |
82 return { |
180 return { |
83 longname : 'Paste text/word', |
181 longname : 'Paste text/word', |
86 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste', |
184 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste', |
87 version : tinymce.majorVersion + "." + tinymce.minorVersion |
185 version : tinymce.majorVersion + "." + tinymce.minorVersion |
88 }; |
186 }; |
89 }, |
187 }, |
90 |
188 |
91 // Private methods |
189 _preProcess : function(pl, o) { |
92 |
190 var ed = this.editor, h = o.content, process, stripClass; |
93 _handlePasteEvent : function(e) { |
191 |
94 var html = this._clipboardHTML(), ed = this.editor, sel = ed.selection, r; |
192 //console.log('Before preprocess:' + o.content); |
95 |
193 |
96 // Removes italic, strong etc, the if was needed due to bug #1437114 |
194 function process(items) { |
97 if (ed && (r = sel.getRng()) && r.text.length > 0) |
195 each(items, function(v) { |
98 ed.execCommand('delete'); |
196 // Remove or replace |
99 |
197 if (v.constructor == RegExp) |
100 if (html && html.length > 0) |
198 h = h.replace(v, ''); |
101 ed.execCommand('mcePasteWord', false, html); |
199 else |
102 |
200 h = h.replace(v[0], v[1]); |
103 return Event.cancel(e); |
201 }); |
104 }, |
202 }; |
105 |
203 |
106 _insertText : function(content, bLinebreaks) { |
204 // Detect Word content and process it more aggressive |
107 content = this.editor.dom.encode(content); |
205 if (/(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/.test(h) || o.wordContent) { |
108 |
206 o.wordContent = true; // Mark the pasted contents as word specific content |
109 if (content && content.length > 0) { |
207 //console.log('Word contents detected.'); |
110 // Delete any highlighted text before pasting |
208 |
111 if (!this.editor.selection.isCollapsed()) |
209 // Process away some basic content |
112 this.editor.execCommand("Delete"); |
210 process([ |
113 |
211 /^\s*( )+/g, // nbsp entities at the start of contents |
114 if (bLinebreaks) { |
212 /( |<br[^>]*>)+\s*$/g // nbsp entities at the end of contents |
115 // Special paragraph treatment |
213 ]); |
116 if (this.editor.getParam("paste_create_paragraphs", true)) { |
214 |
117 var rl = this.editor.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(','); |
215 if (ed.getParam('paste_convert_middot_lists', true)) { |
118 for (var i=0; i<rl.length; i+=2) |
216 process([ |
119 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]); |
217 [/<!--\[if !supportLists\]-->/gi, '$&__MCE_ITEM__'], // Convert supportLists to a list item marker |
120 |
218 [/(<span[^>]+:\s*symbol[^>]+>)/gi, '$1__MCE_ITEM__'], // Convert symbol spans to list items |
121 content = content.replace(/\r\n\r\n/g, '</p><p>'); |
219 [/(<span[^>]+mso-list:[^>]+>)/gi, '$1__MCE_ITEM__'] // Convert mso-list to item marker |
122 content = content.replace(/\r\r/g, '</p><p>'); |
220 ]); |
123 content = content.replace(/\n\n/g, '</p><p>'); |
221 } |
124 |
222 |
125 // Has paragraphs |
223 process([ |
126 if ((pos = content.indexOf('</p><p>')) != -1) { |
224 /<!--[\s\S]+?-->/gi, // Word comments |
127 this.editor.execCommand("Delete"); |
225 /<\/?(img|font|meta|link|style|div|v:\w+)[^>]*>/gi, // Remove some tags including VML content |
128 |
226 /<\\?\?xml[^>]*>/gi, // XML namespace declarations |
129 var node = this.editor.selection.getNode(); |
227 /<\/?o:[^>]*>/gi, // MS namespaced elements <o:tag> |
130 |
228 / (id|name|language|type|on\w+|v:\w+)=\"([^\"]*)\"/gi, // on.., class, style and language attributes with quotes |
131 // Get list of elements to break |
229 / (id|name|language|type|on\w+|v:\w+)=(\w+)/gi, // on.., class, style and language attributes without quotes (IE) |
132 var breakElms = []; |
230 [/<(\/?)s>/gi, '<$1strike>'], // Convert <s> into <strike> for line-though |
133 |
231 /<script[^>]+>[\s\S]*?<\/script>/gi, // All scripts elements for msoShowComment for example |
134 do { |
232 [/ /g, '\u00a0'] // Replace nsbp entites to char since it's easier to handle |
135 if (node.nodeType == 1) { |
233 ]); |
136 // Don't break tables and break at body |
234 |
137 if (node.nodeName == "TD" || node.nodeName == "BODY") |
235 // Remove all spans if no styles is to be retained |
138 break; |
236 if (!ed.getParam('paste_retain_style_properties')) { |
139 |
237 process([ |
140 breakElms[breakElms.length] = node; |
238 /<\/?(span)[^>]*>/gi |
141 } |
239 ]); |
142 } while(node = node.parentNode); |
240 } |
143 |
241 } |
144 var before = "", after = "</p>"; |
242 |
145 before += content.substring(0, pos); |
243 // Allow for class names to be retained if desired; either all, or just the ones from Word |
146 |
244 // Note that the paste_strip_class_attributes: 'none, verify_css_classes: true is also a good variation. |
147 for (var i=0; i<breakElms.length; i++) { |
245 stripClass = ed.getParam('paste_strip_class_attributes'); |
148 before += "</" + breakElms[i].nodeName + ">"; |
246 if (stripClass != 'none') { |
149 after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">"; |
247 // Cleans everything but mceItem... classes |
150 } |
248 function cleanClasses(str, cls) { |
151 |
249 var i, out = ''; |
152 before += "<p>"; |
250 |
153 content = before + content.substring(pos+7) + after; |
251 // Remove all classes |
154 } |
252 if (stripClass == 'all') |
155 } |
253 return ''; |
156 |
254 |
157 if (this.editor.getParam("paste_create_linebreaks", true)) { |
255 cls = tinymce.explode(cls, ' '); |
158 content = content.replace(/\r\n/g, '<br />'); |
256 |
159 content = content.replace(/\r/g, '<br />'); |
257 for (i = cls.length - 1; i >= 0; i--) { |
160 content = content.replace(/\n/g, '<br />'); |
258 // Remove Mso classes |
|
259 if (!/^(Mso)/i.test(cls[i])) |
|
260 out += (!out ? '' : ' ') + cls[i]; |
161 } |
261 } |
162 } |
262 |
163 |
263 return ' class="' + out + '"'; |
164 this.editor.execCommand("mceInsertRawHTML", false, content); |
264 }; |
165 } |
265 |
166 }, |
266 process([ |
167 |
267 [/ class=\"([^\"]*)\"/gi, cleanClasses], // class attributes with quotes |
168 _insertWordContent : function(content) { |
268 [/ class=(\w+)/gi, cleanClasses] // class attributes without quotes (IE) |
|
269 ]); |
|
270 } |
|
271 |
|
272 // Remove spans option |
|
273 if (ed.getParam('paste_remove_spans')) { |
|
274 process([ |
|
275 /<\/?(span)[^>]*>/gi |
|
276 ]); |
|
277 } |
|
278 |
|
279 //console.log('After preprocess:' + h); |
|
280 |
|
281 o.content = h; |
|
282 }, |
|
283 |
|
284 /** |
|
285 * Various post process items. |
|
286 */ |
|
287 _postProcess : function(pl, o) { |
|
288 var t = this, ed = t.editor, dom = ed.dom, styleProps; |
|
289 |
|
290 if (o.wordContent) { |
|
291 // Remove named anchors or TOC links |
|
292 each(dom.select('a', o.node), function(a) { |
|
293 if (!a.href || a.href.indexOf('#_Toc') != -1) |
|
294 dom.remove(a, 1); |
|
295 }); |
|
296 |
|
297 if (t.editor.getParam('paste_convert_middot_lists', true)) |
|
298 t._convertLists(pl, o); |
|
299 |
|
300 // Process styles |
|
301 styleProps = ed.getParam('paste_retain_style_properties'); // retained properties |
|
302 |
|
303 // If string property then split it |
|
304 if (tinymce.is(styleProps, 'string')) |
|
305 styleProps = tinymce.explode(styleProps); |
|
306 |
|
307 // Retains some style properties |
|
308 each(dom.select('*', o.node), function(el) { |
|
309 var newStyle = {}, npc = 0, i, sp, sv; |
|
310 |
|
311 // Store a subset of the existing styles |
|
312 if (styleProps) { |
|
313 for (i = 0; i < styleProps.length; i++) { |
|
314 sp = styleProps[i]; |
|
315 sv = dom.getStyle(el, sp); |
|
316 |
|
317 if (sv) { |
|
318 newStyle[sp] = sv; |
|
319 npc++; |
|
320 } |
|
321 } |
|
322 } |
|
323 |
|
324 // Remove all of the existing styles |
|
325 dom.setAttrib(el, 'style', ''); |
|
326 |
|
327 if (styleProps && npc > 0) |
|
328 dom.setStyles(el, newStyle); // Add back the stored subset of styles |
|
329 else // Remove empty span tags that do not have class attributes |
|
330 if (el.nodeName == 'SPAN' && !el.className) |
|
331 dom.remove(el, true); |
|
332 }); |
|
333 } |
|
334 |
|
335 // Remove all style information or only specifically on WebKit to avoid the style bug on that browser |
|
336 if (ed.getParam("paste_remove_styles") || (ed.getParam("paste_remove_styles_if_webkit") && tinymce.isWebKit)) { |
|
337 each(dom.select('*[style]', o.node), function(el) { |
|
338 el.removeAttribute('style'); |
|
339 el.removeAttribute('mce_style'); |
|
340 }); |
|
341 } else { |
|
342 if (tinymce.isWebKit) { |
|
343 // We need to compress the styles on WebKit since if you paste <img border="0" /> it will become <img border="0" style="... lots of junk ..." /> |
|
344 // Removing the mce_style that contains the real value will force the Serializer engine to compress the styles |
|
345 each(dom.select('*', o.node), function(el) { |
|
346 el.removeAttribute('mce_style'); |
|
347 }); |
|
348 } |
|
349 } |
|
350 }, |
|
351 |
|
352 /** |
|
353 * Converts the most common bullet and number formats in Office into a real semantic UL/LI list. |
|
354 */ |
|
355 _convertLists : function(pl, o) { |
|
356 var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html; |
|
357 |
|
358 // Convert middot lists into real semantic lists |
|
359 each(dom.select('p', o.node), function(p) { |
|
360 var sib, val = '', type, html, idx, parents; |
|
361 |
|
362 // Get text node value at beginning of paragraph |
|
363 for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling) |
|
364 val += sib.nodeValue; |
|
365 |
|
366 val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/ /g, '\u00a0'); |
|
367 |
|
368 // Detect unordered lists look for bullets |
|
369 if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o]\s*\u00a0*/.test(val)) |
|
370 type = 'ul'; |
|
371 |
|
372 // Detect ordered lists 1., a. or ixv. |
|
373 if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0{2,}/.test(val)) |
|
374 type = 'ol'; |
|
375 |
|
376 // Check if node value matches the list pattern: o |
|
377 if (type) { |
|
378 margin = parseFloat(p.style.marginLeft || 0); |
|
379 |
|
380 if (margin > lastMargin) |
|
381 levels.push(margin); |
|
382 |
|
383 if (!listElm || type != lastType) { |
|
384 listElm = dom.create(type); |
|
385 dom.insertAfter(listElm, p); |
|
386 } else { |
|
387 // Nested list element |
|
388 if (margin > lastMargin) { |
|
389 listElm = li.appendChild(dom.create(type)); |
|
390 } else if (margin < lastMargin) { |
|
391 // Find parent level based on margin value |
|
392 idx = tinymce.inArray(levels, margin); |
|
393 parents = dom.getParents(listElm.parentNode, type); |
|
394 listElm = parents[parents.length - 1 - idx] || listElm; |
|
395 } |
|
396 } |
|
397 |
|
398 // Remove middot or number spans if they exists |
|
399 each(dom.select('span', p), function(span) { |
|
400 var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, ''); |
|
401 |
|
402 // Remove span with the middot or the number |
|
403 if (type == 'ul' && /^[\u2022\u00b7\u00a7\u00d8o]/.test(html)) |
|
404 dom.remove(span); |
|
405 else if (/^[\s\S]*\w+\.( |\u00a0)*\s*/.test(html)) |
|
406 dom.remove(span); |
|
407 }); |
|
408 |
|
409 html = p.innerHTML; |
|
410 |
|
411 // Remove middot/list items |
|
412 if (type == 'ul') |
|
413 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o]\s*( |\u00a0)+\s*/, ''); |
|
414 else |
|
415 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*\w+\.( |\u00a0)+\s*/, ''); |
|
416 |
|
417 // Create li and add paragraph data into the new li |
|
418 li = listElm.appendChild(dom.create('li', 0, html)); |
|
419 dom.remove(p); |
|
420 |
|
421 lastMargin = margin; |
|
422 lastType = type; |
|
423 } else |
|
424 listElm = lastMargin = 0; // End list element |
|
425 }); |
|
426 |
|
427 // Remove any left over makers |
|
428 html = o.node.innerHTML; |
|
429 if (html.indexOf('__MCE_ITEM__') != -1) |
|
430 o.node.innerHTML = html.replace(/__MCE_ITEM__/g, ''); |
|
431 }, |
|
432 |
|
433 /** |
|
434 * This method will split the current block parent and insert the contents inside the split position. |
|
435 * This logic can be improved so text nodes at the start/end remain in the start/end block elements |
|
436 */ |
|
437 _insertBlockContent : function(ed, dom, content) { |
|
438 var parentBlock, marker, sel = ed.selection, last, elm, vp, y, elmHeight; |
|
439 |
|
440 function select(n) { |
|
441 var r; |
|
442 |
|
443 if (tinymce.isIE) { |
|
444 r = ed.getDoc().body.createTextRange(); |
|
445 r.moveToElementText(n); |
|
446 r.collapse(false); |
|
447 r.select(); |
|
448 } else { |
|
449 sel.select(n, 1); |
|
450 sel.collapse(false); |
|
451 } |
|
452 }; |
|
453 |
|
454 // Insert a marker for the caret position |
|
455 this._insert('<span id="_marker"> </span>', 1); |
|
456 marker = dom.get('_marker'); |
|
457 parentBlock = dom.getParent(marker, 'p,h1,h2,h3,h4,h5,h6,ul,ol,th,td'); |
|
458 |
|
459 // If it's a parent block but not a table cell |
|
460 if (parentBlock && !/TD|TH/.test(parentBlock.nodeName)) { |
|
461 // Split parent block |
|
462 marker = dom.split(parentBlock, marker); |
|
463 |
|
464 // Insert nodes before the marker |
|
465 each(dom.create('div', 0, content).childNodes, function(n) { |
|
466 last = marker.parentNode.insertBefore(n.cloneNode(true), marker); |
|
467 }); |
|
468 |
|
469 // Move caret after marker |
|
470 select(last); |
|
471 } else { |
|
472 dom.setOuterHTML(marker, content); |
|
473 sel.select(ed.getBody(), 1); |
|
474 sel.collapse(0); |
|
475 } |
|
476 |
|
477 dom.remove('_marker'); // Remove marker if it's left |
|
478 |
|
479 // Get element, position and height |
|
480 elm = sel.getStart(); |
|
481 vp = dom.getViewPort(ed.getWin()); |
|
482 y = ed.dom.getPos(elm).y; |
|
483 elmHeight = elm.clientHeight; |
|
484 |
|
485 // Is element within viewport if not then scroll it into view |
|
486 if (y < vp.y || y + elmHeight > vp.y + vp.h) |
|
487 ed.getDoc().body.scrollTop = y < vp.y ? y : y - vp.h + 25; |
|
488 }, |
|
489 |
|
490 /** |
|
491 * Inserts the specified contents at the caret position. |
|
492 */ |
|
493 _insert : function(h, skip_undo) { |
|
494 var ed = this.editor; |
|
495 |
|
496 // First delete the contents seems to work better on WebKit |
|
497 if (!ed.selection.isCollapsed()) |
|
498 ed.getDoc().execCommand('Delete', false, null); |
|
499 |
|
500 // It's better to use the insertHTML method on Gecko since it will combine paragraphs correctly before inserting the contents |
|
501 ed.execCommand(tinymce.isGecko ? 'insertHTML' : 'mceInsertContent', false, h, {skip_undo : skip_undo}); |
|
502 }, |
|
503 |
|
504 /** |
|
505 * This method will open the old style paste dialogs. Some users might want the old behavior but still use the new cleanup engine. |
|
506 */ |
|
507 _legacySupport : function() { |
169 var t = this, ed = t.editor; |
508 var t = this, ed = t.editor; |
170 |
509 |
171 if (content && content.length > 0) { |
510 // Register commands for backwards compatibility |
172 // Cleanup Word content |
511 each(['mcePasteText', 'mcePasteWord'], function(cmd) { |
173 var bull = String.fromCharCode(8226); |
512 ed.addCommand(cmd, function() { |
174 var middot = String.fromCharCode(183); |
513 ed.windowManager.open({ |
175 |
514 file : t.url + (cmd == 'mcePasteText' ? '/pastetext.htm' : '/pasteword.htm'), |
176 if (ed.getParam('paste_insert_word_content_callback')) |
515 width : parseInt(ed.getParam("paste_dialog_width", "450")), |
177 content = ed.execCallback('paste_insert_word_content_callback', 'before', content); |
516 height : parseInt(ed.getParam("paste_dialog_height", "400")), |
178 |
517 inline : 1 |
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(','); |
518 }); |
180 for (var i=0; i<rl.length; i+=2) |
519 }); |
181 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]); |
520 }); |
182 |
521 |
183 if (this.editor.getParam("paste_convert_headers_to_strong", false)) { |
522 // Register buttons for backwards compatibility |
184 content = content.replace(new RegExp('<p class=MsoHeading.*?>(.*?)<\/p>', 'gi'), '<p><b>$1</b></p>'); |
523 ed.addButton('pastetext', {title : 'paste.paste_text_desc', cmd : 'mcePasteText'}); |
185 } |
524 ed.addButton('pasteword', {title : 'paste.paste_word_desc', cmd : 'mcePasteWord'}); |
186 |
525 ed.addButton('selectall', {title : 'paste.selectall_desc', cmd : 'selectall'}); |
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> <\/p>/gi, ""); // Remove pagebreaks |
|
219 content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks |
|
220 |
|
221 // content = content.replace(/\/? */gi, ""); |
|
222 // content = content.replace(/<p> <\/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]> <\/h[1-6]>/gi, '<p> </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> <\/b>/gi, '<b> </b>'); |
|
255 content = content.replace(/^( )*/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--| ', "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+| )').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--| ', "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 } |
526 } |
390 }); |
527 }); |
391 |
528 |
392 // Register plugin |
529 // Register plugin |
393 tinymce.PluginManager.add('paste', tinymce.plugins.PastePlugin); |
530 tinymce.PluginManager.add('paste', tinymce.plugins.PastePlugin); |