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