]> git.mxchange.org Git - friendica.git/blob - library/tinymce/jscripts/tiny_mce/plugins/paste/editor_plugin_src.js
Merge pull request #592 from fermionic/20130126-richtext-feature-fixes
[friendica.git] / library / tinymce / jscripts / tiny_mce / plugins / paste / editor_plugin_src.js
1 /**\r
2  * editor_plugin_src.js\r
3  *\r
4  * Copyright 2009, Moxiecode Systems AB\r
5  * Released under LGPL License.\r
6  *\r
7  * License: http://tinymce.moxiecode.com/license\r
8  * Contributing: http://tinymce.moxiecode.com/contributing\r
9  */\r
10 \r
11 (function() {\r
12         var each = tinymce.each,\r
13                 defs = {\r
14                         paste_auto_cleanup_on_paste : true,\r
15                         paste_enable_default_filters : true,\r
16                         paste_block_drop : false,\r
17                         paste_retain_style_properties : "none",\r
18                         paste_strip_class_attributes : "mso",\r
19                         paste_remove_spans : false,\r
20                         paste_remove_styles : false,\r
21                         paste_remove_styles_if_webkit : true,\r
22                         paste_convert_middot_lists : true,\r
23                         paste_convert_headers_to_strong : false,\r
24                         paste_dialog_width : "450",\r
25                         paste_dialog_height : "400",\r
26                         paste_max_consecutive_linebreaks: 2,\r
27                         paste_text_use_dialog : false,\r
28                         paste_text_sticky : false,\r
29                         paste_text_sticky_default : false,\r
30                         paste_text_notifyalways : false,\r
31                         paste_text_linebreaktype : "combined",\r
32                         paste_text_replacements : [\r
33                                 [/\u2026/g, "..."],\r
34                                 [/[\x93\x94\u201c\u201d]/g, '"'],\r
35                                 [/[\x60\x91\x92\u2018\u2019]/g, "'"]\r
36                         ]\r
37                 };\r
38 \r
39         function getParam(ed, name) {\r
40                 return ed.getParam(name, defs[name]);\r
41         }\r
42 \r
43         tinymce.create('tinymce.plugins.PastePlugin', {\r
44                 init : function(ed, url) {\r
45                         var t = this;\r
46 \r
47                         t.editor = ed;\r
48                         t.url = url;\r
49 \r
50                         // Setup plugin events\r
51                         t.onPreProcess = new tinymce.util.Dispatcher(t);\r
52                         t.onPostProcess = new tinymce.util.Dispatcher(t);\r
53 \r
54                         // Register default handlers\r
55                         t.onPreProcess.add(t._preProcess);\r
56                         t.onPostProcess.add(t._postProcess);\r
57 \r
58                         // Register optional preprocess handler\r
59                         t.onPreProcess.add(function(pl, o) {\r
60                                 ed.execCallback('paste_preprocess', pl, o);\r
61                         });\r
62 \r
63                         // Register optional postprocess\r
64                         t.onPostProcess.add(function(pl, o) {\r
65                                 ed.execCallback('paste_postprocess', pl, o);\r
66                         });\r
67 \r
68                         ed.onKeyDown.addToTop(function(ed, e) {\r
69                                 // Block ctrl+v from adding an undo level since the default logic in tinymce.Editor will add that\r
70                                 if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))\r
71                                         return false; // Stop other listeners\r
72                         });\r
73 \r
74                         // Initialize plain text flag\r
75                         ed.pasteAsPlainText = getParam(ed, 'paste_text_sticky_default');\r
76 \r
77                         // This function executes the process handlers and inserts the contents\r
78                         // force_rich overrides plain text mode set by user, important for pasting with execCommand\r
79                         function process(o, force_rich) {\r
80                                 var dom = ed.dom, rng;\r
81 \r
82                                 // Execute pre process handlers\r
83                                 t.onPreProcess.dispatch(t, o);\r
84 \r
85                                 // Create DOM structure\r
86                                 o.node = dom.create('div', 0, o.content);\r
87 \r
88                                 // If pasting inside the same element and the contents is only one block\r
89                                 // remove the block and keep the text since Firefox will copy parts of pre and h1-h6 as a pre element\r
90                                 if (tinymce.isGecko) {\r
91                                         rng = ed.selection.getRng(true);\r
92                                         if (rng.startContainer == rng.endContainer && rng.startContainer.nodeType == 3) {\r
93                                                 // Is only one block node and it doesn't contain word stuff\r
94                                                 if (o.node.childNodes.length === 1 && /^(p|h[1-6]|pre)$/i.test(o.node.firstChild.nodeName) && o.content.indexOf('__MCE_ITEM__') === -1)\r
95                                                         dom.remove(o.node.firstChild, true);\r
96                                         }\r
97                                 }\r
98 \r
99                                 // Execute post process handlers\r
100                                 t.onPostProcess.dispatch(t, o);\r
101 \r
102                                 // Serialize content\r
103                                 o.content = ed.serializer.serialize(o.node, {getInner : 1, forced_root_block : ''});\r
104 \r
105                                 // Plain text option active?\r
106                                 if ((!force_rich) && (ed.pasteAsPlainText)) {\r
107                                         t._insertPlainText(o.content);\r
108 \r
109                                         if (!getParam(ed, "paste_text_sticky")) {\r
110                                                 ed.pasteAsPlainText = false;\r
111                                                 ed.controlManager.setActive("pastetext", false);\r
112                                         }\r
113                                 } else {\r
114                                         t._insert(o.content);\r
115                                 }\r
116                         }\r
117 \r
118                         // Add command for external usage\r
119                         ed.addCommand('mceInsertClipboardContent', function(u, o) {\r
120                                 process(o, true);\r
121                         });\r
122 \r
123                         if (!getParam(ed, "paste_text_use_dialog")) {\r
124                                 ed.addCommand('mcePasteText', function(u, v) {\r
125                                         var cookie = tinymce.util.Cookie;\r
126 \r
127                                         ed.pasteAsPlainText = !ed.pasteAsPlainText;\r
128                                         ed.controlManager.setActive('pastetext', ed.pasteAsPlainText);\r
129 \r
130                                         if ((ed.pasteAsPlainText) && (!cookie.get("tinymcePasteText"))) {\r
131                                                 if (getParam(ed, "paste_text_sticky")) {\r
132                                                         ed.windowManager.alert(ed.translate('paste.plaintext_mode_sticky'));\r
133                                                 } else {\r
134                                                         ed.windowManager.alert(ed.translate('paste.plaintext_mode'));\r
135                                                 }\r
136 \r
137                                                 if (!getParam(ed, "paste_text_notifyalways")) {\r
138                                                         cookie.set("tinymcePasteText", "1", new Date(new Date().getFullYear() + 1, 12, 31))\r
139                                                 }\r
140                                         }\r
141                                 });\r
142                         }\r
143 \r
144                         ed.addButton('pastetext', {title: 'paste.paste_text_desc', cmd: 'mcePasteText'});\r
145                         ed.addButton('selectall', {title: 'paste.selectall_desc', cmd: 'selectall'});\r
146 \r
147                         // This function grabs the contents from the clipboard by adding a\r
148                         // hidden div and placing the caret inside it and after the browser paste\r
149                         // is done it grabs that contents and processes that\r
150                         function grabContent(e) {\r
151                                 var n, or, rng, oldRng, sel = ed.selection, dom = ed.dom, body = ed.getBody(), posY, textContent;\r
152 \r
153                                 // Check if browser supports direct plaintext access\r
154                                 if (e.clipboardData || dom.doc.dataTransfer) {\r
155                                         textContent = (e.clipboardData || dom.doc.dataTransfer).getData('Text');\r
156 \r
157                                         if (ed.pasteAsPlainText) {\r
158                                                 e.preventDefault();\r
159                                                 process({content : dom.encode(textContent).replace(/\r?\n/g, '<br />')});\r
160                                                 return;\r
161                                         }\r
162                                 }\r
163 \r
164                                 if (dom.get('_mcePaste'))\r
165                                         return;\r
166 \r
167                                 // Create container to paste into\r
168                                 n = dom.add(body, 'div', {id : '_mcePaste', 'class' : 'mcePaste', 'data-mce-bogus' : '1'}, '\uFEFF\uFEFF');\r
169 \r
170                                 // If contentEditable mode we need to find out the position of the closest element\r
171                                 if (body != ed.getDoc().body)\r
172                                         posY = dom.getPos(ed.selection.getStart(), body).y;\r
173                                 else\r
174                                         posY = body.scrollTop + dom.getViewPort(ed.getWin()).y;\r
175 \r
176                                 // Styles needs to be applied after the element is added to the document since WebKit will otherwise remove all styles\r
177                                 // If also needs to be in view on IE or the paste would fail\r
178                                 dom.setStyles(n, {\r
179                                         position : 'absolute',\r
180                                         left : tinymce.isGecko ? -40 : 0, // Need to move it out of site on Gecko since it will othewise display a ghost resize rect for the div\r
181                                         top : posY - 25,\r
182                                         width : 1,\r
183                                         height : 1,\r
184                                         overflow : 'hidden'\r
185                                 });\r
186 \r
187                                 if (tinymce.isIE) {\r
188                                         // Store away the old range\r
189                                         oldRng = sel.getRng();\r
190 \r
191                                         // Select the container\r
192                                         rng = dom.doc.body.createTextRange();\r
193                                         rng.moveToElementText(n);\r
194                                         rng.execCommand('Paste');\r
195 \r
196                                         // Remove container\r
197                                         dom.remove(n);\r
198 \r
199                                         // Check if the contents was changed, if it wasn't then clipboard extraction failed probably due\r
200                                         // to IE security settings so we pass the junk though better than nothing right\r
201                                         if (n.innerHTML === '\uFEFF\uFEFF') {\r
202                                                 ed.execCommand('mcePasteWord');\r
203                                                 e.preventDefault();\r
204                                                 return;\r
205                                         }\r
206 \r
207                                         // Restore the old range and clear the contents before pasting\r
208                                         sel.setRng(oldRng);\r
209                                         sel.setContent('');\r
210 \r
211                                         // For some odd reason we need to detach the the mceInsertContent call from the paste event\r
212                                         // It's like IE has a reference to the parent element that you paste in and the selection gets messed up\r
213                                         // when it tries to restore the selection\r
214                                         setTimeout(function() {\r
215                                                 // Process contents\r
216                                                 process({content : n.innerHTML});\r
217                                         }, 0);\r
218 \r
219                                         // Block the real paste event\r
220                                         return tinymce.dom.Event.cancel(e);\r
221                                 } else {\r
222                                         function block(e) {\r
223                                                 e.preventDefault();\r
224                                         };\r
225 \r
226                                         // Block mousedown and click to prevent selection change\r
227                                         dom.bind(ed.getDoc(), 'mousedown', block);\r
228                                         dom.bind(ed.getDoc(), 'keydown', block);\r
229 \r
230                                         or = ed.selection.getRng();\r
231 \r
232                                         // Move select contents inside DIV\r
233                                         n = n.firstChild;\r
234                                         rng = ed.getDoc().createRange();\r
235                                         rng.setStart(n, 0);\r
236                                         rng.setEnd(n, 2);\r
237                                         sel.setRng(rng);\r
238 \r
239                                         // Wait a while and grab the pasted contents\r
240                                         window.setTimeout(function() {\r
241                                                 var h = '', nl;\r
242 \r
243                                                 // Paste divs duplicated in paste divs seems to happen when you paste plain text so lets first look for that broken behavior in WebKit\r
244                                                 if (!dom.select('div.mcePaste > div.mcePaste').length) {\r
245                                                         nl = dom.select('div.mcePaste');\r
246 \r
247                                                         // WebKit will split the div into multiple ones so this will loop through then all and join them to get the whole HTML string\r
248                                                         each(nl, function(n) {\r
249                                                                 var child = n.firstChild;\r
250 \r
251                                                                 // WebKit inserts a DIV container with lots of odd styles\r
252                                                                 if (child && child.nodeName == 'DIV' && child.style.marginTop && child.style.backgroundColor) {\r
253                                                                         dom.remove(child, 1);\r
254                                                                 }\r
255 \r
256                                                                 // Remove apply style spans\r
257                                                                 each(dom.select('span.Apple-style-span', n), function(n) {\r
258                                                                         dom.remove(n, 1);\r
259                                                                 });\r
260 \r
261                                                                 // Remove bogus br elements\r
262                                                                 each(dom.select('br[data-mce-bogus]', n), function(n) {\r
263                                                                         dom.remove(n);\r
264                                                                 });\r
265 \r
266                                                                 // WebKit will make a copy of the DIV for each line of plain text pasted and insert them into the DIV\r
267                                                                 if (n.parentNode.className != 'mcePaste')\r
268                                                                         h += n.innerHTML;\r
269                                                         });\r
270                                                 } else {\r
271                                                         // Found WebKit weirdness so force the content into paragraphs this seems to happen when you paste plain text from Nodepad etc\r
272                                                         // So this logic will replace double enter with paragraphs and single enter with br so it kind of looks the same\r
273                                                         h = '<p>' + dom.encode(textContent).replace(/\r?\n\r?\n/g, '</p><p>').replace(/\r?\n/g, '<br />') + '</p>';\r
274                                                 }\r
275 \r
276                                                 // Remove the nodes\r
277                                                 each(dom.select('div.mcePaste'), function(n) {\r
278                                                         dom.remove(n);\r
279                                                 });\r
280 \r
281                                                 // Restore the old selection\r
282                                                 if (or)\r
283                                                         sel.setRng(or);\r
284 \r
285                                                 process({content : h});\r
286 \r
287                                                 // Unblock events ones we got the contents\r
288                                                 dom.unbind(ed.getDoc(), 'mousedown', block);\r
289                                                 dom.unbind(ed.getDoc(), 'keydown', block);\r
290                                         }, 0);\r
291                                 }\r
292                         }\r
293 \r
294                         // Check if we should use the new auto process method                   \r
295                         if (getParam(ed, "paste_auto_cleanup_on_paste")) {\r
296                                 // Is it's Opera or older FF use key handler\r
297                                 if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) {\r
298                                         ed.onKeyDown.addToTop(function(ed, e) {\r
299                                                 if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))\r
300                                                         grabContent(e);\r
301                                         });\r
302                                 } else {\r
303                                         // Grab contents on paste event on Gecko and WebKit\r
304                                         ed.onPaste.addToTop(function(ed, e) {\r
305                                                 return grabContent(e);\r
306                                         });\r
307                                 }\r
308                         }\r
309 \r
310                         ed.onInit.add(function() {\r
311                                 ed.controlManager.setActive("pastetext", ed.pasteAsPlainText);\r
312 \r
313                                 // Block all drag/drop events\r
314                                 if (getParam(ed, "paste_block_drop")) {\r
315                                         ed.dom.bind(ed.getBody(), ['dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag'], function(e) {\r
316                                                 e.preventDefault();\r
317                                                 e.stopPropagation();\r
318 \r
319                                                 return false;\r
320                                         });\r
321                                 }\r
322                         });\r
323 \r
324                         // Add legacy support\r
325                         t._legacySupport();\r
326                 },\r
327 \r
328                 getInfo : function() {\r
329                         return {\r
330                                 longname : 'Paste text/word',\r
331                                 author : 'Moxiecode Systems AB',\r
332                                 authorurl : 'http://tinymce.moxiecode.com',\r
333                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste',\r
334                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
335                         };\r
336                 },\r
337 \r
338                 _preProcess : function(pl, o) {\r
339                         var ed = this.editor,\r
340                                 h = o.content,\r
341                                 grep = tinymce.grep,\r
342                                 explode = tinymce.explode,\r
343                                 trim = tinymce.trim,\r
344                                 len, stripClass;\r
345 \r
346                         //console.log('Before preprocess:' + o.content);\r
347 \r
348                         function process(items) {\r
349                                 each(items, function(v) {\r
350                                         // Remove or replace\r
351                                         if (v.constructor == RegExp)\r
352                                                 h = h.replace(v, '');\r
353                                         else\r
354                                                 h = h.replace(v[0], v[1]);\r
355                                 });\r
356                         }\r
357                         \r
358                         if (ed.settings.paste_enable_default_filters == false) {\r
359                                 return;\r
360                         }\r
361 \r
362                         // IE9 adds BRs before/after block elements when contents is pasted from word or for example another browser\r
363                         if (tinymce.isIE && document.documentMode >= 9 && /<(h[1-6r]|p|div|address|pre|form|table|tbody|thead|tfoot|th|tr|td|li|ol|ul|caption|blockquote|center|dl|dt|dd|dir|fieldset)/.test(o.content)) {\r
364                                 // IE9 adds BRs before/after block elements when contents is pasted from word or for example another browser\r
365                                 process([[/(?:<br>&nbsp;[\s\r\n]+|<br>)*(<\/?(h[1-6r]|p|div|address|pre|form|table|tbody|thead|tfoot|th|tr|td|li|ol|ul|caption|blockquote|center|dl|dt|dd|dir|fieldset)[^>]*>)(?:<br>&nbsp;[\s\r\n]+|<br>)*/g, '$1']]);\r
366 \r
367                                 // IE9 also adds an extra BR element for each soft-linefeed and it also adds a BR for each word wrap break\r
368                                 process([\r
369                                         [/<br><br>/g, '<BR><BR>'], // Replace multiple BR elements with uppercase BR to keep them intact\r
370                                         [/<br>/g, ' '], // Replace single br elements with space since they are word wrap BR:s\r
371                                         [/<BR><BR>/g, '<br>'] // Replace back the double brs but into a single BR\r
372                                 ]);\r
373                         }\r
374 \r
375                         // Detect Word content and process it more aggressive\r
376                         if (/class="?Mso|style="[^"]*\bmso-|w:WordDocument/i.test(h) || o.wordContent) {\r
377                                 o.wordContent = true;                   // Mark the pasted contents as word specific content\r
378                                 //console.log('Word contents detected.');\r
379 \r
380                                 // Process away some basic content\r
381                                 process([\r
382                                         /^\s*(&nbsp;)+/gi,                              // &nbsp; entities at the start of contents\r
383                                         /(&nbsp;|<br[^>]*>)+\s*$/gi             // &nbsp; entities at the end of contents\r
384                                 ]);\r
385 \r
386                                 if (getParam(ed, "paste_convert_headers_to_strong")) {\r
387                                         h = h.replace(/<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>");\r
388                                 }\r
389 \r
390                                 if (getParam(ed, "paste_convert_middot_lists")) {\r
391                                         process([\r
392                                                 [/<!--\[if !supportLists\]-->/gi, '$&__MCE_ITEM__'],                                    // Convert supportLists to a list item marker\r
393                                                 [/(<span[^>]+(?:mso-list:|:\s*symbol)[^>]+>)/gi, '$1__MCE_ITEM__'],             // Convert mso-list and symbol spans to item markers\r
394                                                 [/(<p[^>]+(?:MsoListParagraph)[^>]+>)/gi, '$1__MCE_ITEM__']                             // Convert mso-list and symbol paragraphs to item markers (FF)\r
395                                         ]);\r
396                                 }\r
397 \r
398                                 process([\r
399                                         // Word comments like conditional comments etc\r
400                                         /<!--[\s\S]+?-->/gi,\r
401 \r
402                                         // Remove comments, scripts (e.g., msoShowComment), XML tag, VML content, MS Office namespaced tags, and a few other tags\r
403                                         /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|img|meta|link|style|\w:\w+)(?=[\s\/>]))[^>]*>/gi,\r
404 \r
405                                         // Convert <s> into <strike> for line-though\r
406                                         [/<(\/?)s>/gi, "<$1strike>"],\r
407 \r
408                                         // Replace nsbp entites to char since it's easier to handle\r
409                                         [/&nbsp;/gi, "\u00a0"]\r
410                                 ]);\r
411 \r
412                                 // Remove bad attributes, with or without quotes, ensuring that attribute text is really inside a tag.\r
413                                 // If JavaScript had a RegExp look-behind, we could have integrated this with the last process() array and got rid of the loop. But alas, it does not, so we cannot.\r
414                                 do {\r
415                                         len = h.length;\r
416                                         h = h.replace(/(<[a-z][^>]*\s)(?:id|name|language|type|on\w+|\w+:\w+)=(?:"[^"]*"|\w+)\s?/gi, "$1");\r
417                                 } while (len != h.length);\r
418 \r
419                                 // Remove all spans if no styles is to be retained\r
420                                 if (getParam(ed, "paste_retain_style_properties").replace(/^none$/i, "").length == 0) {\r
421                                         h = h.replace(/<\/?span[^>]*>/gi, "");\r
422                                 } else {\r
423                                         // We're keeping styles, so at least clean them up.\r
424                                         // CSS Reference: http://msdn.microsoft.com/en-us/library/aa155477.aspx\r
425 \r
426                                         process([\r
427                                                 // Convert <span style="mso-spacerun:yes">___</span> to string of alternating breaking/non-breaking spaces of same length\r
428                                                 [/<span\s+style\s*=\s*"\s*mso-spacerun\s*:\s*yes\s*;?\s*"\s*>([\s\u00a0]*)<\/span>/gi,\r
429                                                         function(str, spaces) {\r
430                                                                 return (spaces.length > 0)? spaces.replace(/./, " ").slice(Math.floor(spaces.length/2)).split("").join("\u00a0") : "";\r
431                                                         }\r
432                                                 ],\r
433 \r
434                                                 // Examine all styles: delete junk, transform some, and keep the rest\r
435                                                 [/(<[a-z][^>]*)\sstyle="([^"]*)"/gi,\r
436                                                         function(str, tag, style) {\r
437                                                                 var n = [],\r
438                                                                         i = 0,\r
439                                                                         s = explode(trim(style).replace(/&quot;/gi, "'"), ";");\r
440 \r
441                                                                 // Examine each style definition within the tag's style attribute\r
442                                                                 each(s, function(v) {\r
443                                                                         var name, value,\r
444                                                                                 parts = explode(v, ":");\r
445 \r
446                                                                         function ensureUnits(v) {\r
447                                                                                 return v + ((v !== "0") && (/\d$/.test(v)))? "px" : "";\r
448                                                                         }\r
449 \r
450                                                                         if (parts.length == 2) {\r
451                                                                                 name = parts[0].toLowerCase();\r
452                                                                                 value = parts[1].toLowerCase();\r
453 \r
454                                                                                 // Translate certain MS Office styles into their CSS equivalents\r
455                                                                                 switch (name) {\r
456                                                                                         case "mso-padding-alt":\r
457                                                                                         case "mso-padding-top-alt":\r
458                                                                                         case "mso-padding-right-alt":\r
459                                                                                         case "mso-padding-bottom-alt":\r
460                                                                                         case "mso-padding-left-alt":\r
461                                                                                         case "mso-margin-alt":\r
462                                                                                         case "mso-margin-top-alt":\r
463                                                                                         case "mso-margin-right-alt":\r
464                                                                                         case "mso-margin-bottom-alt":\r
465                                                                                         case "mso-margin-left-alt":\r
466                                                                                         case "mso-table-layout-alt":\r
467                                                                                         case "mso-height":\r
468                                                                                         case "mso-width":\r
469                                                                                         case "mso-vertical-align-alt":\r
470                                                                                                 n[i++] = name.replace(/^mso-|-alt$/g, "") + ":" + ensureUnits(value);\r
471                                                                                                 return;\r
472 \r
473                                                                                         case "horiz-align":\r
474                                                                                                 n[i++] = "text-align:" + value;\r
475                                                                                                 return;\r
476 \r
477                                                                                         case "vert-align":\r
478                                                                                                 n[i++] = "vertical-align:" + value;\r
479                                                                                                 return;\r
480 \r
481                                                                                         case "font-color":\r
482                                                                                         case "mso-foreground":\r
483                                                                                                 n[i++] = "color:" + value;\r
484                                                                                                 return;\r
485 \r
486                                                                                         case "mso-background":\r
487                                                                                         case "mso-highlight":\r
488                                                                                                 n[i++] = "background:" + value;\r
489                                                                                                 return;\r
490 \r
491                                                                                         case "mso-default-height":\r
492                                                                                                 n[i++] = "min-height:" + ensureUnits(value);\r
493                                                                                                 return;\r
494 \r
495                                                                                         case "mso-default-width":\r
496                                                                                                 n[i++] = "min-width:" + ensureUnits(value);\r
497                                                                                                 return;\r
498 \r
499                                                                                         case "mso-padding-between-alt":\r
500                                                                                                 n[i++] = "border-collapse:separate;border-spacing:" + ensureUnits(value);\r
501                                                                                                 return;\r
502 \r
503                                                                                         case "text-line-through":\r
504                                                                                                 if ((value == "single") || (value == "double")) {\r
505                                                                                                         n[i++] = "text-decoration:line-through";\r
506                                                                                                 }\r
507                                                                                                 return;\r
508 \r
509                                                                                         case "mso-zero-height":\r
510                                                                                                 if (value == "yes") {\r
511                                                                                                         n[i++] = "display:none";\r
512                                                                                                 }\r
513                                                                                                 return;\r
514                                                                                 }\r
515 \r
516                                                                                 // Eliminate all MS Office style definitions that have no CSS equivalent by examining the first characters in the name\r
517                                                                                 if (/^(mso|column|font-emph|lang|layout|line-break|list-image|nav|panose|punct|row|ruby|sep|size|src|tab-|table-border|text-(?!align|decor|indent|trans)|top-bar|version|vnd|word-break)/.test(name)) {\r
518                                                                                         return;\r
519                                                                                 }\r
520 \r
521                                                                                 // If it reached this point, it must be a valid CSS style\r
522                                                                                 n[i++] = name + ":" + parts[1];         // Lower-case name, but keep value case\r
523                                                                         }\r
524                                                                 });\r
525 \r
526                                                                 // If style attribute contained any valid styles the re-write it; otherwise delete style attribute.\r
527                                                                 if (i > 0) {\r
528                                                                         return tag + ' style="' + n.join(';') + '"';\r
529                                                                 } else {\r
530                                                                         return tag;\r
531                                                                 }\r
532                                                         }\r
533                                                 ]\r
534                                         ]);\r
535                                 }\r
536                         }\r
537 \r
538                         // Replace headers with <strong>\r
539                         if (getParam(ed, "paste_convert_headers_to_strong")) {\r
540                                 process([\r
541                                         [/<h[1-6][^>]*>/gi, "<p><strong>"],\r
542                                         [/<\/h[1-6][^>]*>/gi, "</strong></p>"]\r
543                                 ]);\r
544                         }\r
545 \r
546                         process([\r
547                                 // Copy paste from Java like Open Office will produce this junk on FF\r
548                                 [/Version:[\d.]+\nStartHTML:\d+\nEndHTML:\d+\nStartFragment:\d+\nEndFragment:\d+/gi, '']\r
549                         ]);\r
550 \r
551                         // Class attribute options are: leave all as-is ("none"), remove all ("all"), or remove only those starting with mso ("mso").\r
552                         // Note:-  paste_strip_class_attributes: "none", verify_css_classes: true is also a good variation.\r
553                         stripClass = getParam(ed, "paste_strip_class_attributes");\r
554 \r
555                         if (stripClass !== "none") {\r
556                                 function removeClasses(match, g1) {\r
557                                                 if (stripClass === "all")\r
558                                                         return '';\r
559 \r
560                                                 var cls = grep(explode(g1.replace(/^(["'])(.*)\1$/, "$2"), " "),\r
561                                                         function(v) {\r
562                                                                 return (/^(?!mso)/i.test(v));\r
563                                                         }\r
564                                                 );\r
565 \r
566                                                 return cls.length ? ' class="' + cls.join(" ") + '"' : '';\r
567                                 };\r
568 \r
569                                 h = h.replace(/ class="([^"]+)"/gi, removeClasses);\r
570                                 h = h.replace(/ class=([\-\w]+)/gi, removeClasses);\r
571                         }\r
572 \r
573                         // Remove spans option\r
574                         if (getParam(ed, "paste_remove_spans")) {\r
575                                 h = h.replace(/<\/?span[^>]*>/gi, "");\r
576                         }\r
577 \r
578                         //console.log('After preprocess:' + h);\r
579 \r
580                         o.content = h;\r
581                 },\r
582 \r
583                 /**\r
584                  * Various post process items.\r
585                  */\r
586                 _postProcess : function(pl, o) {\r
587                         var t = this, ed = t.editor, dom = ed.dom, styleProps;\r
588 \r
589                         if (ed.settings.paste_enable_default_filters == false) {\r
590                                 return;\r
591                         }\r
592                         \r
593                         if (o.wordContent) {\r
594                                 // Remove named anchors or TOC links\r
595                                 each(dom.select('a', o.node), function(a) {\r
596                                         if (!a.href || a.href.indexOf('#_Toc') != -1)\r
597                                                 dom.remove(a, 1);\r
598                                 });\r
599 \r
600                                 if (getParam(ed, "paste_convert_middot_lists")) {\r
601                                         t._convertLists(pl, o);\r
602                                 }\r
603 \r
604                                 // Process styles\r
605                                 styleProps = getParam(ed, "paste_retain_style_properties"); // retained properties\r
606 \r
607                                 // Process only if a string was specified and not equal to "all" or "*"\r
608                                 if ((tinymce.is(styleProps, "string")) && (styleProps !== "all") && (styleProps !== "*")) {\r
609                                         styleProps = tinymce.explode(styleProps.replace(/^none$/i, ""));\r
610 \r
611                                         // Retains some style properties\r
612                                         each(dom.select('*', o.node), function(el) {\r
613                                                 var newStyle = {}, npc = 0, i, sp, sv;\r
614 \r
615                                                 // Store a subset of the existing styles\r
616                                                 if (styleProps) {\r
617                                                         for (i = 0; i < styleProps.length; i++) {\r
618                                                                 sp = styleProps[i];\r
619                                                                 sv = dom.getStyle(el, sp);\r
620 \r
621                                                                 if (sv) {\r
622                                                                         newStyle[sp] = sv;\r
623                                                                         npc++;\r
624                                                                 }\r
625                                                         }\r
626                                                 }\r
627 \r
628                                                 // Remove all of the existing styles\r
629                                                 dom.setAttrib(el, 'style', '');\r
630 \r
631                                                 if (styleProps && npc > 0)\r
632                                                         dom.setStyles(el, newStyle); // Add back the stored subset of styles\r
633                                                 else // Remove empty span tags that do not have class attributes\r
634                                                         if (el.nodeName == 'SPAN' && !el.className)\r
635                                                                 dom.remove(el, true);\r
636                                         });\r
637                                 }\r
638                         }\r
639 \r
640                         // Remove all style information or only specifically on WebKit to avoid the style bug on that browser\r
641                         if (getParam(ed, "paste_remove_styles") || (getParam(ed, "paste_remove_styles_if_webkit") && tinymce.isWebKit)) {\r
642                                 each(dom.select('*[style]', o.node), function(el) {\r
643                                         el.removeAttribute('style');\r
644                                         el.removeAttribute('data-mce-style');\r
645                                 });\r
646                         } else {\r
647                                 if (tinymce.isWebKit) {\r
648                                         // 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 ..." />\r
649                                         // Removing the mce_style that contains the real value will force the Serializer engine to compress the styles\r
650                                         each(dom.select('*', o.node), function(el) {\r
651                                                 el.removeAttribute('data-mce-style');\r
652                                         });\r
653                                 }\r
654                         }\r
655                 },\r
656 \r
657                 /**\r
658                  * Converts the most common bullet and number formats in Office into a real semantic UL/LI list.\r
659                  */\r
660                 _convertLists : function(pl, o) {\r
661                         var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html;\r
662 \r
663                         // Convert middot lists into real semantic lists\r
664                         each(dom.select('p', o.node), function(p) {\r
665                                 var sib, val = '', type, html, idx, parents;\r
666 \r
667                                 // Get text node value at beginning of paragraph\r
668                                 for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling)\r
669                                         val += sib.nodeValue;\r
670 \r
671                                 val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/&nbsp;/g, '\u00a0');\r
672 \r
673                                 // Detect unordered lists look for bullets\r
674                                 if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*\u00a0*/.test(val))\r
675                                         type = 'ul';\r
676 \r
677                                 // Detect ordered lists 1., a. or ixv.\r
678                                 if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0+/.test(val))\r
679                                         type = 'ol';\r
680 \r
681                                 // Check if node value matches the list pattern: o&nbsp;&nbsp;\r
682                                 if (type) {\r
683                                         margin = parseFloat(p.style.marginLeft || 0);\r
684 \r
685                                         if (margin > lastMargin)\r
686                                                 levels.push(margin);\r
687 \r
688                                         if (!listElm || type != lastType) {\r
689                                                 listElm = dom.create(type);\r
690                                                 dom.insertAfter(listElm, p);\r
691                                         } else {\r
692                                                 // Nested list element\r
693                                                 if (margin > lastMargin) {\r
694                                                         listElm = li.appendChild(dom.create(type));\r
695                                                 } else if (margin < lastMargin) {\r
696                                                         // Find parent level based on margin value\r
697                                                         idx = tinymce.inArray(levels, margin);\r
698                                                         parents = dom.getParents(listElm.parentNode, type);\r
699                                                         listElm = parents[parents.length - 1 - idx] || listElm;\r
700                                                 }\r
701                                         }\r
702 \r
703                                         // Remove middot or number spans if they exists\r
704                                         each(dom.select('span', p), function(span) {\r
705                                                 var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, '');\r
706 \r
707                                                 // Remove span with the middot or the number\r
708                                                 if (type == 'ul' && /^__MCE_ITEM__[\u2022\u00b7\u00a7\u00d8o\u25CF]/.test(html))\r
709                                                         dom.remove(span);\r
710                                                 else if (/^__MCE_ITEM__[\s\S]*\w+\.(&nbsp;|\u00a0)*\s*/.test(html))\r
711                                                         dom.remove(span);\r
712                                         });\r
713 \r
714                                         html = p.innerHTML;\r
715 \r
716                                         // Remove middot/list items\r
717                                         if (type == 'ul')\r
718                                                 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*(&nbsp;|\u00a0)+\s*/, '');\r
719                                         else\r
720                                                 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*\w+\.(&nbsp;|\u00a0)+\s*/, '');\r
721 \r
722                                         // Create li and add paragraph data into the new li\r
723                                         li = listElm.appendChild(dom.create('li', 0, html));\r
724                                         dom.remove(p);\r
725 \r
726                                         lastMargin = margin;\r
727                                         lastType = type;\r
728                                 } else\r
729                                         listElm = lastMargin = 0; // End list element\r
730                         });\r
731 \r
732                         // Remove any left over makers\r
733                         html = o.node.innerHTML;\r
734                         if (html.indexOf('__MCE_ITEM__') != -1)\r
735                                 o.node.innerHTML = html.replace(/__MCE_ITEM__/g, '');\r
736                 },\r
737 \r
738                 /**\r
739                  * Inserts the specified contents at the caret position.\r
740                  */\r
741                 _insert : function(h, skip_undo) {\r
742                         var ed = this.editor, r = ed.selection.getRng();\r
743 \r
744                         // First delete the contents seems to work better on WebKit when the selection spans multiple list items or multiple table cells.\r
745                         if (!ed.selection.isCollapsed() && r.startContainer != r.endContainer)\r
746                                 ed.getDoc().execCommand('Delete', false, null);\r
747 \r
748                         ed.execCommand('mceInsertContent', false, h, {skip_undo : skip_undo});\r
749                 },\r
750 \r
751                 /**\r
752                  * Instead of the old plain text method which tried to re-create a paste operation, the\r
753                  * new approach adds a plain text mode toggle switch that changes the behavior of paste.\r
754                  * This function is passed the same input that the regular paste plugin produces.\r
755                  * It performs additional scrubbing and produces (and inserts) the plain text.\r
756                  * This approach leverages all of the great existing functionality in the paste\r
757                  * plugin, and requires minimal changes to add the new functionality.\r
758                  * Speednet - June 2009\r
759                  */\r
760                 _insertPlainText : function(content) {\r
761                         var ed = this.editor,\r
762                                 linebr = getParam(ed, "paste_text_linebreaktype"),\r
763                                 rl = getParam(ed, "paste_text_replacements"),\r
764                                 is = tinymce.is;\r
765 \r
766                         function process(items) {\r
767                                 each(items, function(v) {\r
768                                         if (v.constructor == RegExp)\r
769                                                 content = content.replace(v, "");\r
770                                         else\r
771                                                 content = content.replace(v[0], v[1]);\r
772                                 });\r
773                         };\r
774 \r
775                         if ((typeof(content) === "string") && (content.length > 0)) {\r
776                                 // If HTML content with line-breaking tags, then remove all cr/lf chars because only tags will break a line\r
777                                 if (/<(?:p|br|h[1-6]|ul|ol|dl|table|t[rdh]|div|blockquote|fieldset|pre|address|center)[^>]*>/i.test(content)) {\r
778                                         process([\r
779                                                 /[\n\r]+/g\r
780                                         ]);\r
781                                 } else {\r
782                                         // Otherwise just get rid of carriage returns (only need linefeeds)\r
783                                         process([\r
784                                                 /\r+/g\r
785                                         ]);\r
786                                 }\r
787 \r
788                                 process([\r
789                                         [/<\/(?:p|h[1-6]|ul|ol|dl|table|div|blockquote|fieldset|pre|address|center)>/gi, "\n\n"],               // Block tags get a blank line after them\r
790                                         [/<br[^>]*>|<\/tr>/gi, "\n"],                           // Single linebreak for <br /> tags and table rows\r
791                                         [/<\/t[dh]>\s*<t[dh][^>]*>/gi, "\t"],           // Table cells get tabs betweem them\r
792                                         /<[a-z!\/?][^>]*>/gi,                                           // Delete all remaining tags\r
793                                         [/&nbsp;/gi, " "],                                                      // Convert non-break spaces to regular spaces (remember, *plain text*)\r
794                                         [/(?:(?!\n)\s)*(\n+)(?:(?!\n)\s)*/gi, "$1"] // Cool little RegExp deletes whitespace around linebreak chars.\r
795                                 ]);\r
796 \r
797                                 var maxLinebreaks = Number(getParam(ed, "paste_max_consecutive_linebreaks"));\r
798                                 if (maxLinebreaks > -1) {\r
799                                         var maxLinebreaksRegex = new RegExp("\n{" + (maxLinebreaks + 1) + ",}", "g");\r
800                                         var linebreakReplacement = "";\r
801 \r
802                                         while (linebreakReplacement.length < maxLinebreaks) {\r
803                                                 linebreakReplacement += "\n";\r
804                                         }\r
805 \r
806                                         process([\r
807                                                 [maxLinebreaksRegex, linebreakReplacement] // Limit max consecutive linebreaks\r
808                                         ]);\r
809                                 }\r
810 \r
811                                 content = ed.dom.decode(tinymce.html.Entities.encodeRaw(content));\r
812 \r
813                                 // Perform default or custom replacements\r
814                                 if (is(rl, "array")) {\r
815                                         process(rl);\r
816                                 } else if (is(rl, "string")) {\r
817                                         process(new RegExp(rl, "gi"));\r
818                                 }\r
819 \r
820                                 // Treat paragraphs as specified in the config\r
821                                 if (linebr == "none") {\r
822                                         // Convert all line breaks to space\r
823                                         process([\r
824                                                 [/\n+/g, " "]\r
825                                         ]);\r
826                                 } else if (linebr == "br") {\r
827                                         // Convert all line breaks to <br />\r
828                                         process([\r
829                                                 [/\n/g, "<br />"]\r
830                                         ]);\r
831                                 } else if (linebr == "p") {\r
832                                         // Convert all line breaks to <p>...</p>\r
833                                         process([\r
834                                                 [/\n+/g, "</p><p>"],\r
835                                                 [/^(.*<\/p>)(<p>)$/, '<p>$1']\r
836                                         ]);\r
837                                 } else {\r
838                                         // defaults to "combined"\r
839                                         // Convert single line breaks to <br /> and double line breaks to <p>...</p>\r
840                                         process([\r
841                                                 [/\n\n/g, "</p><p>"],\r
842                                                 [/^(.*<\/p>)(<p>)$/, '<p>$1'],\r
843                                                 [/\n/g, "<br />"]\r
844                                         ]);\r
845                                 }\r
846 \r
847                                 ed.execCommand('mceInsertContent', false, content);\r
848                         }\r
849                 },\r
850 \r
851                 /**\r
852                  * This method will open the old style paste dialogs. Some users might want the old behavior but still use the new cleanup engine.\r
853                  */\r
854                 _legacySupport : function() {\r
855                         var t = this, ed = t.editor;\r
856 \r
857                         // Register command(s) for backwards compatibility\r
858                         ed.addCommand("mcePasteWord", function() {\r
859                                 ed.windowManager.open({\r
860                                         file: t.url + "/pasteword.htm",\r
861                                         width: parseInt(getParam(ed, "paste_dialog_width")),\r
862                                         height: parseInt(getParam(ed, "paste_dialog_height")),\r
863                                         inline: 1\r
864                                 });\r
865                         });\r
866 \r
867                         if (getParam(ed, "paste_text_use_dialog")) {\r
868                                 ed.addCommand("mcePasteText", function() {\r
869                                         ed.windowManager.open({\r
870                                                 file : t.url + "/pastetext.htm",\r
871                                                 width: parseInt(getParam(ed, "paste_dialog_width")),\r
872                                                 height: parseInt(getParam(ed, "paste_dialog_height")),\r
873                                                 inline : 1\r
874                                         });\r
875                                 });\r
876                         }\r
877 \r
878                         // Register button for backwards compatibility\r
879                         ed.addButton("pasteword", {title : "paste.paste_word_desc", cmd : "mcePasteWord"});\r
880                 }\r
881         });\r
882 \r
883         // Register plugin\r
884         tinymce.PluginManager.add("paste", tinymce.plugins.PastePlugin);\r
885 })();\r