]> git.mxchange.org Git - friendica.git/blob - library/tinymce/jscripts/tiny_mce/plugins/paste/editor_plugin_src.js
updated tinymce to 3.5.11
[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                                         // Don't remove the type attribute for lists so that non-default list types display correctly.\r
417                                         h = h.replace(/(<?!(ol|ul)[^>]*\s)(?:id|name|language|type|on\w+|\w+:\w+)=(?:"[^"]*"|\w+)\s?/gi, "$1");\r
418                                         h = h.replace(/(<(ol|ul)[^>]*\s)(?:id|name|language|on\w+|\w+:\w+)=(?:"[^"]*"|\w+)\s?/gi, "$1");\r
419                                 } while (len != h.length);\r
420 \r
421                                 // Remove all spans if no styles is to be retained\r
422                                 if (getParam(ed, "paste_retain_style_properties").replace(/^none$/i, "").length == 0) {\r
423                                         h = h.replace(/<\/?span[^>]*>/gi, "");\r
424                                 } else {\r
425                                         // We're keeping styles, so at least clean them up.\r
426                                         // CSS Reference: http://msdn.microsoft.com/en-us/library/aa155477.aspx\r
427 \r
428                                         process([\r
429                                                 // Convert <span style="mso-spacerun:yes">___</span> to string of alternating breaking/non-breaking spaces of same length\r
430                                                 [/<span\s+style\s*=\s*"\s*mso-spacerun\s*:\s*yes\s*;?\s*"\s*>([\s\u00a0]*)<\/span>/gi,\r
431                                                         function(str, spaces) {\r
432                                                                 return (spaces.length > 0)? spaces.replace(/./, " ").slice(Math.floor(spaces.length/2)).split("").join("\u00a0") : "";\r
433                                                         }\r
434                                                 ],\r
435 \r
436                                                 // Examine all styles: delete junk, transform some, and keep the rest\r
437                                                 [/(<[a-z][^>]*)\sstyle="([^"]*)"/gi,\r
438                                                         function(str, tag, style) {\r
439                                                                 var n = [],\r
440                                                                         i = 0,\r
441                                                                         s = explode(trim(style).replace(/&quot;/gi, "'"), ";");\r
442 \r
443                                                                 // Examine each style definition within the tag's style attribute\r
444                                                                 each(s, function(v) {\r
445                                                                         var name, value,\r
446                                                                                 parts = explode(v, ":");\r
447 \r
448                                                                         function ensureUnits(v) {\r
449                                                                                 return v + ((v !== "0") && (/\d$/.test(v)))? "px" : "";\r
450                                                                         }\r
451 \r
452                                                                         if (parts.length == 2) {\r
453                                                                                 name = parts[0].toLowerCase();\r
454                                                                                 value = parts[1].toLowerCase();\r
455 \r
456                                                                                 // Translate certain MS Office styles into their CSS equivalents\r
457                                                                                 switch (name) {\r
458                                                                                         case "mso-padding-alt":\r
459                                                                                         case "mso-padding-top-alt":\r
460                                                                                         case "mso-padding-right-alt":\r
461                                                                                         case "mso-padding-bottom-alt":\r
462                                                                                         case "mso-padding-left-alt":\r
463                                                                                         case "mso-margin-alt":\r
464                                                                                         case "mso-margin-top-alt":\r
465                                                                                         case "mso-margin-right-alt":\r
466                                                                                         case "mso-margin-bottom-alt":\r
467                                                                                         case "mso-margin-left-alt":\r
468                                                                                         case "mso-table-layout-alt":\r
469                                                                                         case "mso-height":\r
470                                                                                         case "mso-width":\r
471                                                                                         case "mso-vertical-align-alt":\r
472                                                                                                 n[i++] = name.replace(/^mso-|-alt$/g, "") + ":" + ensureUnits(value);\r
473                                                                                                 return;\r
474 \r
475                                                                                         case "horiz-align":\r
476                                                                                                 n[i++] = "text-align:" + value;\r
477                                                                                                 return;\r
478 \r
479                                                                                         case "vert-align":\r
480                                                                                                 n[i++] = "vertical-align:" + value;\r
481                                                                                                 return;\r
482 \r
483                                                                                         case "font-color":\r
484                                                                                         case "mso-foreground":\r
485                                                                                                 n[i++] = "color:" + value;\r
486                                                                                                 return;\r
487 \r
488                                                                                         case "mso-background":\r
489                                                                                         case "mso-highlight":\r
490                                                                                                 n[i++] = "background:" + value;\r
491                                                                                                 return;\r
492 \r
493                                                                                         case "mso-default-height":\r
494                                                                                                 n[i++] = "min-height:" + ensureUnits(value);\r
495                                                                                                 return;\r
496 \r
497                                                                                         case "mso-default-width":\r
498                                                                                                 n[i++] = "min-width:" + ensureUnits(value);\r
499                                                                                                 return;\r
500 \r
501                                                                                         case "mso-padding-between-alt":\r
502                                                                                                 n[i++] = "border-collapse:separate;border-spacing:" + ensureUnits(value);\r
503                                                                                                 return;\r
504 \r
505                                                                                         case "text-line-through":\r
506                                                                                                 if ((value == "single") || (value == "double")) {\r
507                                                                                                         n[i++] = "text-decoration:line-through";\r
508                                                                                                 }\r
509                                                                                                 return;\r
510 \r
511                                                                                         case "mso-zero-height":\r
512                                                                                                 if (value == "yes") {\r
513                                                                                                         n[i++] = "display:none";\r
514                                                                                                 }\r
515                                                                                                 return;\r
516                                                                                 }\r
517 \r
518                                                                                 // Eliminate all MS Office style definitions that have no CSS equivalent by examining the first characters in the name\r
519                                                                                 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
520                                                                                         return;\r
521                                                                                 }\r
522 \r
523                                                                                 // If it reached this point, it must be a valid CSS style\r
524                                                                                 n[i++] = name + ":" + parts[1];         // Lower-case name, but keep value case\r
525                                                                         }\r
526                                                                 });\r
527 \r
528                                                                 // If style attribute contained any valid styles the re-write it; otherwise delete style attribute.\r
529                                                                 if (i > 0) {\r
530                                                                         return tag + ' style="' + n.join(';') + '"';\r
531                                                                 } else {\r
532                                                                         return tag;\r
533                                                                 }\r
534                                                         }\r
535                                                 ]\r
536                                         ]);\r
537                                 }\r
538                         }\r
539 \r
540                         // Replace headers with <strong>\r
541                         if (getParam(ed, "paste_convert_headers_to_strong")) {\r
542                                 process([\r
543                                         [/<h[1-6][^>]*>/gi, "<p><strong>"],\r
544                                         [/<\/h[1-6][^>]*>/gi, "</strong></p>"]\r
545                                 ]);\r
546                         }\r
547 \r
548                         process([\r
549                                 // Copy paste from Java like Open Office will produce this junk on FF\r
550                                 [/Version:[\d.]+\nStartHTML:\d+\nEndHTML:\d+\nStartFragment:\d+\nEndFragment:\d+/gi, '']\r
551                         ]);\r
552 \r
553                         // Class attribute options are: leave all as-is ("none"), remove all ("all"), or remove only those starting with mso ("mso").\r
554                         // Note:-  paste_strip_class_attributes: "none", verify_css_classes: true is also a good variation.\r
555                         stripClass = getParam(ed, "paste_strip_class_attributes");\r
556 \r
557                         if (stripClass !== "none") {\r
558                                 function removeClasses(match, g1) {\r
559                                                 if (stripClass === "all")\r
560                                                         return '';\r
561 \r
562                                                 var cls = grep(explode(g1.replace(/^(["'])(.*)\1$/, "$2"), " "),\r
563                                                         function(v) {\r
564                                                                 return (/^(?!mso)/i.test(v));\r
565                                                         }\r
566                                                 );\r
567 \r
568                                                 return cls.length ? ' class="' + cls.join(" ") + '"' : '';\r
569                                 };\r
570 \r
571                                 h = h.replace(/ class="([^"]+)"/gi, removeClasses);\r
572                                 h = h.replace(/ class=([\-\w]+)/gi, removeClasses);\r
573                         }\r
574 \r
575                         // Remove spans option\r
576                         if (getParam(ed, "paste_remove_spans")) {\r
577                                 h = h.replace(/<\/?span[^>]*>/gi, "");\r
578                         }\r
579 \r
580                         //console.log('After preprocess:' + h);\r
581 \r
582                         o.content = h;\r
583                 },\r
584 \r
585                 /**\r
586                  * Various post process items.\r
587                  */\r
588                 _postProcess : function(pl, o) {\r
589                         var t = this, ed = t.editor, dom = ed.dom, styleProps;\r
590 \r
591                         if (ed.settings.paste_enable_default_filters == false) {\r
592                                 return;\r
593                         }\r
594 \r
595                         if (o.wordContent) {\r
596                                 // Remove named anchors or TOC links\r
597                                 each(dom.select('a', o.node), function(a) {\r
598                                         if (!a.href || a.href.indexOf('#_Toc') != -1)\r
599                                                 dom.remove(a, 1);\r
600                                 });\r
601 \r
602                                 if (getParam(ed, "paste_convert_middot_lists")) {\r
603                                         t._convertLists(pl, o);\r
604                                 }\r
605 \r
606                                 // Process styles\r
607                                 styleProps = getParam(ed, "paste_retain_style_properties"); // retained properties\r
608 \r
609                                 // Process only if a string was specified and not equal to "all" or "*"\r
610                                 if ((tinymce.is(styleProps, "string")) && (styleProps !== "all") && (styleProps !== "*")) {\r
611                                         styleProps = tinymce.explode(styleProps.replace(/^none$/i, ""));\r
612 \r
613                                         // Retains some style properties\r
614                                         each(dom.select('*', o.node), function(el) {\r
615                                                 var newStyle = {}, npc = 0, i, sp, sv;\r
616 \r
617                                                 // Store a subset of the existing styles\r
618                                                 if (styleProps) {\r
619                                                         for (i = 0; i < styleProps.length; i++) {\r
620                                                                 sp = styleProps[i];\r
621                                                                 sv = dom.getStyle(el, sp);\r
622 \r
623                                                                 if (sv) {\r
624                                                                         newStyle[sp] = sv;\r
625                                                                         npc++;\r
626                                                                 }\r
627                                                         }\r
628                                                 }\r
629 \r
630                                                 // Remove all of the existing styles\r
631                                                 dom.setAttrib(el, 'style', '');\r
632 \r
633                                                 if (styleProps && npc > 0)\r
634                                                         dom.setStyles(el, newStyle); // Add back the stored subset of styles\r
635                                                 else // Remove empty span tags that do not have class attributes\r
636                                                         if (el.nodeName == 'SPAN' && !el.className)\r
637                                                                 dom.remove(el, true);\r
638                                         });\r
639                                 }\r
640                         }\r
641 \r
642                         // Remove all style information or only specifically on WebKit to avoid the style bug on that browser\r
643                         if (getParam(ed, "paste_remove_styles") || (getParam(ed, "paste_remove_styles_if_webkit") && tinymce.isWebKit)) {\r
644                                 each(dom.select('*[style]', o.node), function(el) {\r
645                                         el.removeAttribute('style');\r
646                                         el.removeAttribute('data-mce-style');\r
647                                 });\r
648                         } else {\r
649                                 if (tinymce.isWebKit) {\r
650                                         // 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
651                                         // Removing the mce_style that contains the real value will force the Serializer engine to compress the styles\r
652                                         each(dom.select('*', o.node), function(el) {\r
653                                                 el.removeAttribute('data-mce-style');\r
654                                         });\r
655                                 }\r
656                         }\r
657                 },\r
658 \r
659                 /**\r
660                  * Converts the most common bullet and number formats in Office into a real semantic UL/LI list.\r
661                  */\r
662                 _convertLists : function(pl, o) {\r
663                         var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html;\r
664 \r
665                         // Convert middot lists into real semantic lists\r
666                         each(dom.select('p', o.node), function(p) {\r
667                                 var sib, val = '', type, html, idx, parents;\r
668 \r
669                                 // Get text node value at beginning of paragraph\r
670                                 for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling)\r
671                                         val += sib.nodeValue;\r
672 \r
673                                 val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/&nbsp;/g, '\u00a0');\r
674 \r
675                                 // Detect unordered lists look for bullets\r
676                                 if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*\u00a0*/.test(val))\r
677                                         type = 'ul';\r
678 \r
679                                 // Detect ordered lists 1., a. or ixv.\r
680                                 if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0+/.test(val))\r
681                                         type = 'ol';\r
682 \r
683                                 // Check if node value matches the list pattern: o&nbsp;&nbsp;\r
684                                 if (type) {\r
685                                         margin = parseFloat(p.style.marginLeft || 0);\r
686 \r
687                                         if (margin > lastMargin)\r
688                                                 levels.push(margin);\r
689 \r
690                                         if (!listElm || type != lastType) {\r
691                                                 listElm = dom.create(type);\r
692                                                 dom.insertAfter(listElm, p);\r
693                                         } else {\r
694                                                 // Nested list element\r
695                                                 if (margin > lastMargin) {\r
696                                                         listElm = li.appendChild(dom.create(type));\r
697                                                 } else if (margin < lastMargin) {\r
698                                                         // Find parent level based on margin value\r
699                                                         idx = tinymce.inArray(levels, margin);\r
700                                                         parents = dom.getParents(listElm.parentNode, type);\r
701                                                         listElm = parents[parents.length - 1 - idx] || listElm;\r
702                                                 }\r
703                                         }\r
704 \r
705                                         // Remove middot or number spans if they exists\r
706                                         each(dom.select('span', p), function(span) {\r
707                                                 var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, '');\r
708 \r
709                                                 // Remove span with the middot or the number\r
710                                                 if (type == 'ul' && /^__MCE_ITEM__[\u2022\u00b7\u00a7\u00d8o\u25CF]/.test(html))\r
711                                                         dom.remove(span);\r
712                                                 else if (/^__MCE_ITEM__[\s\S]*\w+\.(&nbsp;|\u00a0)*\s*/.test(html))\r
713                                                         dom.remove(span);\r
714                                         });\r
715 \r
716                                         html = p.innerHTML;\r
717 \r
718                                         // Remove middot/list items\r
719                                         if (type == 'ul')\r
720                                                 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*(&nbsp;|\u00a0)+\s*/, '');\r
721                                         else\r
722                                                 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*[\w|'<'|'>']+\.(&nbsp;|\u00a0)+\s*/, '');;\r
723 \r
724                                         // Create li and add paragraph data into the new li\r
725                                         li = listElm.appendChild(dom.create('li', 0, html));\r
726                                         dom.remove(p);\r
727 \r
728                                         lastMargin = margin;\r
729                                         lastType = type;\r
730                                 } else\r
731                                         listElm = lastMargin = 0; // End list element\r
732                         });\r
733 \r
734                         // Remove any left over makers\r
735                         html = o.node.innerHTML;\r
736                         if (html.indexOf('__MCE_ITEM__') != -1)\r
737                                 o.node.innerHTML = html.replace(/__MCE_ITEM__/g, '');\r
738                 },\r
739 \r
740                 /**\r
741                  * Inserts the specified contents at the caret position.\r
742                  */\r
743                 _insert : function(h, skip_undo) {\r
744                         var ed = this.editor, r = ed.selection.getRng();\r
745 \r
746                         // First delete the contents seems to work better on WebKit when the selection spans multiple list items or multiple table cells.\r
747                         if (!ed.selection.isCollapsed() && r.startContainer != r.endContainer)\r
748                                 ed.getDoc().execCommand('Delete', false, null);\r
749 \r
750                         ed.execCommand('mceInsertContent', false, h, {skip_undo : skip_undo});\r
751                 },\r
752 \r
753                 /**\r
754                  * Instead of the old plain text method which tried to re-create a paste operation, the\r
755                  * new approach adds a plain text mode toggle switch that changes the behavior of paste.\r
756                  * This function is passed the same input that the regular paste plugin produces.\r
757                  * It performs additional scrubbing and produces (and inserts) the plain text.\r
758                  * This approach leverages all of the great existing functionality in the paste\r
759                  * plugin, and requires minimal changes to add the new functionality.\r
760                  * Speednet - June 2009\r
761                  */\r
762                 _insertPlainText : function(content) {\r
763                         var ed = this.editor,\r
764                                 linebr = getParam(ed, "paste_text_linebreaktype"),\r
765                                 rl = getParam(ed, "paste_text_replacements"),\r
766                                 is = tinymce.is;\r
767 \r
768                         function process(items) {\r
769                                 each(items, function(v) {\r
770                                         if (v.constructor == RegExp)\r
771                                                 content = content.replace(v, "");\r
772                                         else\r
773                                                 content = content.replace(v[0], v[1]);\r
774                                 });\r
775                         };\r
776 \r
777                         if ((typeof(content) === "string") && (content.length > 0)) {\r
778                                 // If HTML content with line-breaking tags, then remove all cr/lf chars because only tags will break a line\r
779                                 if (/<(?:p|br|h[1-6]|ul|ol|dl|table|t[rdh]|div|blockquote|fieldset|pre|address|center)[^>]*>/i.test(content)) {\r
780                                         process([\r
781                                                 /[\n\r]+/g\r
782                                         ]);\r
783                                 } else {\r
784                                         // Otherwise just get rid of carriage returns (only need linefeeds)\r
785                                         process([\r
786                                                 /\r+/g\r
787                                         ]);\r
788                                 }\r
789 \r
790                                 process([\r
791                                         [/<\/(?: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
792                                         [/<br[^>]*>|<\/tr>/gi, "\n"],                           // Single linebreak for <br /> tags and table rows\r
793                                         [/<\/t[dh]>\s*<t[dh][^>]*>/gi, "\t"],           // Table cells get tabs betweem them\r
794                                         /<[a-z!\/?][^>]*>/gi,                                           // Delete all remaining tags\r
795                                         [/&nbsp;/gi, " "],                                                      // Convert non-break spaces to regular spaces (remember, *plain text*)\r
796                                         [/(?:(?!\n)\s)*(\n+)(?:(?!\n)\s)*/gi, "$1"] // Cool little RegExp deletes whitespace around linebreak chars.\r
797                                 ]);\r
798 \r
799                                 var maxLinebreaks = Number(getParam(ed, "paste_max_consecutive_linebreaks"));\r
800                                 if (maxLinebreaks > -1) {\r
801                                         var maxLinebreaksRegex = new RegExp("\n{" + (maxLinebreaks + 1) + ",}", "g");\r
802                                         var linebreakReplacement = "";\r
803 \r
804                                         while (linebreakReplacement.length < maxLinebreaks) {\r
805                                                 linebreakReplacement += "\n";\r
806                                         }\r
807 \r
808                                         process([\r
809                                                 [maxLinebreaksRegex, linebreakReplacement] // Limit max consecutive linebreaks\r
810                                         ]);\r
811                                 }\r
812 \r
813                                 content = ed.dom.decode(tinymce.html.Entities.encodeRaw(content));\r
814 \r
815                                 // Perform default or custom replacements\r
816                                 if (is(rl, "array")) {\r
817                                         process(rl);\r
818                                 } else if (is(rl, "string")) {\r
819                                         process(new RegExp(rl, "gi"));\r
820                                 }\r
821 \r
822                                 // Treat paragraphs as specified in the config\r
823                                 if (linebr == "none") {\r
824                                         // Convert all line breaks to space\r
825                                         process([\r
826                                                 [/\n+/g, " "]\r
827                                         ]);\r
828                                 } else if (linebr == "br") {\r
829                                         // Convert all line breaks to <br />\r
830                                         process([\r
831                                                 [/\n/g, "<br />"]\r
832                                         ]);\r
833                                 } else if (linebr == "p") {\r
834                                         // Convert all line breaks to <p>...</p>\r
835                                         process([\r
836                                                 [/\n+/g, "</p><p>"],\r
837                                                 [/^(.*<\/p>)(<p>)$/, '<p>$1']\r
838                                         ]);\r
839                                 } else {\r
840                                         // defaults to "combined"\r
841                                         // Convert single line breaks to <br /> and double line breaks to <p>...</p>\r
842                                         process([\r
843                                                 [/\n\n/g, "</p><p>"],\r
844                                                 [/^(.*<\/p>)(<p>)$/, '<p>$1'],\r
845                                                 [/\n/g, "<br />"]\r
846                                         ]);\r
847                                 }\r
848 \r
849                                 ed.execCommand('mceInsertContent', false, content);\r
850                         }\r
851                 },\r
852 \r
853                 /**\r
854                  * This method will open the old style paste dialogs. Some users might want the old behavior but still use the new cleanup engine.\r
855                  */\r
856                 _legacySupport : function() {\r
857                         var t = this, ed = t.editor;\r
858 \r
859                         // Register command(s) for backwards compatibility\r
860                         ed.addCommand("mcePasteWord", function() {\r
861                                 ed.windowManager.open({\r
862                                         file: t.url + "/pasteword.htm",\r
863                                         width: parseInt(getParam(ed, "paste_dialog_width")),\r
864                                         height: parseInt(getParam(ed, "paste_dialog_height")),\r
865                                         inline: 1\r
866                                 });\r
867                         });\r
868 \r
869                         if (getParam(ed, "paste_text_use_dialog")) {\r
870                                 ed.addCommand("mcePasteText", function() {\r
871                                         ed.windowManager.open({\r
872                                                 file : t.url + "/pastetext.htm",\r
873                                                 width: parseInt(getParam(ed, "paste_dialog_width")),\r
874                                                 height: parseInt(getParam(ed, "paste_dialog_height")),\r
875                                                 inline : 1\r
876                                         });\r
877                                 });\r
878                         }\r
879 \r
880                         // Register button for backwards compatibility\r
881                         ed.addButton("pasteword", {title : "paste.paste_word_desc", cmd : "mcePasteWord"});\r
882                 }\r
883         });\r
884 \r
885         // Register plugin\r
886         tinymce.PluginManager.add("paste", tinymce.plugins.PastePlugin);\r
887 })();\r