]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TinyMCE/js/plugins/paste/editor_plugin_src.js
upgrade TinyMCE to 3.4.x
[quix0rs-gnu-social.git] / plugins / TinyMCE / js / 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(ed, dom, 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_sticky'));\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 : 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 plain text mode\r
271                                                         h = '<pre>' + dom.encode(textContent).replace(/\r?\n/g, '<br />') + '</pre>';\r
272                                                 }\r
273 \r
274                                                 // Remove the nodes\r
275                                                 each(dom.select('div.mcePaste'), function(n) {\r
276                                                         dom.remove(n);\r
277                                                 });\r
278 \r
279                                                 // Restore the old selection\r
280                                                 if (or)\r
281                                                         sel.setRng(or);\r
282 \r
283                                                 process({content : h});\r
284 \r
285                                                 // Unblock events ones we got the contents\r
286                                                 dom.unbind(ed.getDoc(), 'mousedown', block);\r
287                                                 dom.unbind(ed.getDoc(), 'keydown', block);\r
288                                         }, 0);\r
289                                 }\r
290                         }\r
291 \r
292                         // Check if we should use the new auto process method                   \r
293                         if (getParam(ed, "paste_auto_cleanup_on_paste")) {\r
294                                 // Is it's Opera or older FF use key handler\r
295                                 if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) {\r
296                                         ed.onKeyDown.addToTop(function(ed, e) {\r
297                                                 if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))\r
298                                                         grabContent(e);\r
299                                         });\r
300                                 } else {\r
301                                         // Grab contents on paste event on Gecko and WebKit\r
302                                         ed.onPaste.addToTop(function(ed, e) {\r
303                                                 return grabContent(e);\r
304                                         });\r
305                                 }\r
306                         }\r
307 \r
308                         ed.onInit.add(function() {\r
309                                 ed.controlManager.setActive("pastetext", ed.pasteAsPlainText);\r
310 \r
311                                 // Block all drag/drop events\r
312                                 if (getParam(ed, "paste_block_drop")) {\r
313                                         ed.dom.bind(ed.getBody(), ['dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag'], function(e) {\r
314                                                 e.preventDefault();\r
315                                                 e.stopPropagation();\r
316 \r
317                                                 return false;\r
318                                         });\r
319                                 }\r
320                         });\r
321 \r
322                         // Add legacy support\r
323                         t._legacySupport();\r
324                 },\r
325 \r
326                 getInfo : function() {\r
327                         return {\r
328                                 longname : 'Paste text/word',\r
329                                 author : 'Moxiecode Systems AB',\r
330                                 authorurl : 'http://tinymce.moxiecode.com',\r
331                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste',\r
332                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
333                         };\r
334                 },\r
335 \r
336                 _preProcess : function(pl, o) {\r
337                         var ed = this.editor,\r
338                                 h = o.content,\r
339                                 grep = tinymce.grep,\r
340                                 explode = tinymce.explode,\r
341                                 trim = tinymce.trim,\r
342                                 len, stripClass;\r
343 \r
344                         //console.log('Before preprocess:' + o.content);\r
345 \r
346                         function process(items) {\r
347                                 each(items, function(v) {\r
348                                         // Remove or replace\r
349                                         if (v.constructor == RegExp)\r
350                                                 h = h.replace(v, '');\r
351                                         else\r
352                                                 h = h.replace(v[0], v[1]);\r
353                                 });\r
354                         }\r
355                         \r
356                         if (ed.settings.paste_enable_default_filters == false) {\r
357                                 return;\r
358                         }\r
359 \r
360                         // IE9 adds BRs before/after block elements when contents is pasted from word or for example another browser\r
361                         if (tinymce.isIE && document.documentMode >= 9) {\r
362                                 // IE9 adds BRs before/after block elements when contents is pasted from word or for example another browser\r
363                                 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
364 \r
365                                 // IE9 also adds an extra BR element for each soft-linefeed and it also adds a BR for each word wrap break\r
366                                 process([\r
367                                         [/<br><br>/g, '<BR><BR>'], // Replace multiple BR elements with uppercase BR to keep them intact\r
368                                         [/<br>/g, ' '], // Replace single br elements with space since they are word wrap BR:s\r
369                                         [/<BR><BR>/g, '<br>'], // Replace back the double brs but into a single BR\r
370                                 ]);\r
371                         }\r
372 \r
373                         // Detect Word content and process it more aggressive\r
374                         if (/class="?Mso|style="[^"]*\bmso-|w:WordDocument/i.test(h) || o.wordContent) {\r
375                                 o.wordContent = true;                   // Mark the pasted contents as word specific content\r
376                                 //console.log('Word contents detected.');\r
377 \r
378                                 // Process away some basic content\r
379                                 process([\r
380                                         /^\s*(&nbsp;)+/gi,                              // &nbsp; entities at the start of contents\r
381                                         /(&nbsp;|<br[^>]*>)+\s*$/gi             // &nbsp; entities at the end of contents\r
382                                 ]);\r
383 \r
384                                 if (getParam(ed, "paste_convert_headers_to_strong")) {\r
385                                         h = h.replace(/<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>");\r
386                                 }\r
387 \r
388                                 if (getParam(ed, "paste_convert_middot_lists")) {\r
389                                         process([\r
390                                                 [/<!--\[if !supportLists\]-->/gi, '$&__MCE_ITEM__'],                                    // Convert supportLists to a list item marker\r
391                                                 [/(<span[^>]+(?:mso-list:|:\s*symbol)[^>]+>)/gi, '$1__MCE_ITEM__'],             // Convert mso-list and symbol spans to item markers\r
392                                                 [/(<p[^>]+(?:MsoListParagraph)[^>]+>)/gi, '$1__MCE_ITEM__']                             // Convert mso-list and symbol paragraphs to item markers (FF)\r
393                                         ]);\r
394                                 }\r
395 \r
396                                 process([\r
397                                         // Word comments like conditional comments etc\r
398                                         /<!--[\s\S]+?-->/gi,\r
399 \r
400                                         // Remove comments, scripts (e.g., msoShowComment), XML tag, VML content, MS Office namespaced tags, and a few other tags\r
401                                         /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|img|meta|link|style|\w:\w+)(?=[\s\/>]))[^>]*>/gi,\r
402 \r
403                                         // Convert <s> into <strike> for line-though\r
404                                         [/<(\/?)s>/gi, "<$1strike>"],\r
405 \r
406                                         // Replace nsbp entites to char since it's easier to handle\r
407                                         [/&nbsp;/gi, "\u00a0"]\r
408                                 ]);\r
409 \r
410                                 // Remove bad attributes, with or without quotes, ensuring that attribute text is really inside a tag.\r
411                                 // 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
412                                 do {\r
413                                         len = h.length;\r
414                                         h = h.replace(/(<[a-z][^>]*\s)(?:id|name|language|type|on\w+|\w+:\w+)=(?:"[^"]*"|\w+)\s?/gi, "$1");\r
415                                 } while (len != h.length);\r
416 \r
417                                 // Remove all spans if no styles is to be retained\r
418                                 if (getParam(ed, "paste_retain_style_properties").replace(/^none$/i, "").length == 0) {\r
419                                         h = h.replace(/<\/?span[^>]*>/gi, "");\r
420                                 } else {\r
421                                         // We're keeping styles, so at least clean them up.\r
422                                         // CSS Reference: http://msdn.microsoft.com/en-us/library/aa155477.aspx\r
423 \r
424                                         process([\r
425                                                 // Convert <span style="mso-spacerun:yes">___</span> to string of alternating breaking/non-breaking spaces of same length\r
426                                                 [/<span\s+style\s*=\s*"\s*mso-spacerun\s*:\s*yes\s*;?\s*"\s*>([\s\u00a0]*)<\/span>/gi,\r
427                                                         function(str, spaces) {\r
428                                                                 return (spaces.length > 0)? spaces.replace(/./, " ").slice(Math.floor(spaces.length/2)).split("").join("\u00a0") : "";\r
429                                                         }\r
430                                                 ],\r
431 \r
432                                                 // Examine all styles: delete junk, transform some, and keep the rest\r
433                                                 [/(<[a-z][^>]*)\sstyle="([^"]*)"/gi,\r
434                                                         function(str, tag, style) {\r
435                                                                 var n = [],\r
436                                                                         i = 0,\r
437                                                                         s = explode(trim(style).replace(/&quot;/gi, "'"), ";");\r
438 \r
439                                                                 // Examine each style definition within the tag's style attribute\r
440                                                                 each(s, function(v) {\r
441                                                                         var name, value,\r
442                                                                                 parts = explode(v, ":");\r
443 \r
444                                                                         function ensureUnits(v) {\r
445                                                                                 return v + ((v !== "0") && (/\d$/.test(v)))? "px" : "";\r
446                                                                         }\r
447 \r
448                                                                         if (parts.length == 2) {\r
449                                                                                 name = parts[0].toLowerCase();\r
450                                                                                 value = parts[1].toLowerCase();\r
451 \r
452                                                                                 // Translate certain MS Office styles into their CSS equivalents\r
453                                                                                 switch (name) {\r
454                                                                                         case "mso-padding-alt":\r
455                                                                                         case "mso-padding-top-alt":\r
456                                                                                         case "mso-padding-right-alt":\r
457                                                                                         case "mso-padding-bottom-alt":\r
458                                                                                         case "mso-padding-left-alt":\r
459                                                                                         case "mso-margin-alt":\r
460                                                                                         case "mso-margin-top-alt":\r
461                                                                                         case "mso-margin-right-alt":\r
462                                                                                         case "mso-margin-bottom-alt":\r
463                                                                                         case "mso-margin-left-alt":\r
464                                                                                         case "mso-table-layout-alt":\r
465                                                                                         case "mso-height":\r
466                                                                                         case "mso-width":\r
467                                                                                         case "mso-vertical-align-alt":\r
468                                                                                                 n[i++] = name.replace(/^mso-|-alt$/g, "") + ":" + ensureUnits(value);\r
469                                                                                                 return;\r
470 \r
471                                                                                         case "horiz-align":\r
472                                                                                                 n[i++] = "text-align:" + value;\r
473                                                                                                 return;\r
474 \r
475                                                                                         case "vert-align":\r
476                                                                                                 n[i++] = "vertical-align:" + value;\r
477                                                                                                 return;\r
478 \r
479                                                                                         case "font-color":\r
480                                                                                         case "mso-foreground":\r
481                                                                                                 n[i++] = "color:" + value;\r
482                                                                                                 return;\r
483 \r
484                                                                                         case "mso-background":\r
485                                                                                         case "mso-highlight":\r
486                                                                                                 n[i++] = "background:" + value;\r
487                                                                                                 return;\r
488 \r
489                                                                                         case "mso-default-height":\r
490                                                                                                 n[i++] = "min-height:" + ensureUnits(value);\r
491                                                                                                 return;\r
492 \r
493                                                                                         case "mso-default-width":\r
494                                                                                                 n[i++] = "min-width:" + ensureUnits(value);\r
495                                                                                                 return;\r
496 \r
497                                                                                         case "mso-padding-between-alt":\r
498                                                                                                 n[i++] = "border-collapse:separate;border-spacing:" + ensureUnits(value);\r
499                                                                                                 return;\r
500 \r
501                                                                                         case "text-line-through":\r
502                                                                                                 if ((value == "single") || (value == "double")) {\r
503                                                                                                         n[i++] = "text-decoration:line-through";\r
504                                                                                                 }\r
505                                                                                                 return;\r
506 \r
507                                                                                         case "mso-zero-height":\r
508                                                                                                 if (value == "yes") {\r
509                                                                                                         n[i++] = "display:none";\r
510                                                                                                 }\r
511                                                                                                 return;\r
512                                                                                 }\r
513 \r
514                                                                                 // Eliminate all MS Office style definitions that have no CSS equivalent by examining the first characters in the name\r
515                                                                                 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
516                                                                                         return;\r
517                                                                                 }\r
518 \r
519                                                                                 // If it reached this point, it must be a valid CSS style\r
520                                                                                 n[i++] = name + ":" + parts[1];         // Lower-case name, but keep value case\r
521                                                                         }\r
522                                                                 });\r
523 \r
524                                                                 // If style attribute contained any valid styles the re-write it; otherwise delete style attribute.\r
525                                                                 if (i > 0) {\r
526                                                                         return tag + ' style="' + n.join(';') + '"';\r
527                                                                 } else {\r
528                                                                         return tag;\r
529                                                                 }\r
530                                                         }\r
531                                                 ]\r
532                                         ]);\r
533                                 }\r
534                         }\r
535 \r
536                         // Replace headers with <strong>\r
537                         if (getParam(ed, "paste_convert_headers_to_strong")) {\r
538                                 process([\r
539                                         [/<h[1-6][^>]*>/gi, "<p><strong>"],\r
540                                         [/<\/h[1-6][^>]*>/gi, "</strong></p>"]\r
541                                 ]);\r
542                         }\r
543 \r
544                         process([\r
545                                 // Copy paste from Java like Open Office will produce this junk on FF\r
546                                 [/Version:[\d.]+\nStartHTML:\d+\nEndHTML:\d+\nStartFragment:\d+\nEndFragment:\d+/gi, '']\r
547                         ]);\r
548 \r
549                         // Class attribute options are: leave all as-is ("none"), remove all ("all"), or remove only those starting with mso ("mso").\r
550                         // Note:-  paste_strip_class_attributes: "none", verify_css_classes: true is also a good variation.\r
551                         stripClass = getParam(ed, "paste_strip_class_attributes");\r
552 \r
553                         if (stripClass !== "none") {\r
554                                 function removeClasses(match, g1) {\r
555                                                 if (stripClass === "all")\r
556                                                         return '';\r
557 \r
558                                                 var cls = grep(explode(g1.replace(/^(["'])(.*)\1$/, "$2"), " "),\r
559                                                         function(v) {\r
560                                                                 return (/^(?!mso)/i.test(v));\r
561                                                         }\r
562                                                 );\r
563 \r
564                                                 return cls.length ? ' class="' + cls.join(" ") + '"' : '';\r
565                                 };\r
566 \r
567                                 h = h.replace(/ class="([^"]+)"/gi, removeClasses);\r
568                                 h = h.replace(/ class=([\-\w]+)/gi, removeClasses);\r
569                         }\r
570 \r
571                         // Remove spans option\r
572                         if (getParam(ed, "paste_remove_spans")) {\r
573                                 h = h.replace(/<\/?span[^>]*>/gi, "");\r
574                         }\r
575 \r
576                         //console.log('After preprocess:' + h);\r
577 \r
578                         o.content = h;\r
579                 },\r
580 \r
581                 /**\r
582                  * Various post process items.\r
583                  */\r
584                 _postProcess : function(pl, o) {\r
585                         var t = this, ed = t.editor, dom = ed.dom, styleProps;\r
586 \r
587                         if (ed.settings.paste_enable_default_filters == false) {\r
588                                 return;\r
589                         }\r
590                         \r
591                         if (o.wordContent) {\r
592                                 // Remove named anchors or TOC links\r
593                                 each(dom.select('a', o.node), function(a) {\r
594                                         if (!a.href || a.href.indexOf('#_Toc') != -1)\r
595                                                 dom.remove(a, 1);\r
596                                 });\r
597 \r
598                                 if (getParam(ed, "paste_convert_middot_lists")) {\r
599                                         t._convertLists(pl, o);\r
600                                 }\r
601 \r
602                                 // Process styles\r
603                                 styleProps = getParam(ed, "paste_retain_style_properties"); // retained properties\r
604 \r
605                                 // Process only if a string was specified and not equal to "all" or "*"\r
606                                 if ((tinymce.is(styleProps, "string")) && (styleProps !== "all") && (styleProps !== "*")) {\r
607                                         styleProps = tinymce.explode(styleProps.replace(/^none$/i, ""));\r
608 \r
609                                         // Retains some style properties\r
610                                         each(dom.select('*', o.node), function(el) {\r
611                                                 var newStyle = {}, npc = 0, i, sp, sv;\r
612 \r
613                                                 // Store a subset of the existing styles\r
614                                                 if (styleProps) {\r
615                                                         for (i = 0; i < styleProps.length; i++) {\r
616                                                                 sp = styleProps[i];\r
617                                                                 sv = dom.getStyle(el, sp);\r
618 \r
619                                                                 if (sv) {\r
620                                                                         newStyle[sp] = sv;\r
621                                                                         npc++;\r
622                                                                 }\r
623                                                         }\r
624                                                 }\r
625 \r
626                                                 // Remove all of the existing styles\r
627                                                 dom.setAttrib(el, 'style', '');\r
628 \r
629                                                 if (styleProps && npc > 0)\r
630                                                         dom.setStyles(el, newStyle); // Add back the stored subset of styles\r
631                                                 else // Remove empty span tags that do not have class attributes\r
632                                                         if (el.nodeName == 'SPAN' && !el.className)\r
633                                                                 dom.remove(el, true);\r
634                                         });\r
635                                 }\r
636                         }\r
637 \r
638                         // Remove all style information or only specifically on WebKit to avoid the style bug on that browser\r
639                         if (getParam(ed, "paste_remove_styles") || (getParam(ed, "paste_remove_styles_if_webkit") && tinymce.isWebKit)) {\r
640                                 each(dom.select('*[style]', o.node), function(el) {\r
641                                         el.removeAttribute('style');\r
642                                         el.removeAttribute('data-mce-style');\r
643                                 });\r
644                         } else {\r
645                                 if (tinymce.isWebKit) {\r
646                                         // 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
647                                         // Removing the mce_style that contains the real value will force the Serializer engine to compress the styles\r
648                                         each(dom.select('*', o.node), function(el) {\r
649                                                 el.removeAttribute('data-mce-style');\r
650                                         });\r
651                                 }\r
652                         }\r
653                 },\r
654 \r
655                 /**\r
656                  * Converts the most common bullet and number formats in Office into a real semantic UL/LI list.\r
657                  */\r
658                 _convertLists : function(pl, o) {\r
659                         var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html;\r
660 \r
661                         // Convert middot lists into real semantic lists\r
662                         each(dom.select('p', o.node), function(p) {\r
663                                 var sib, val = '', type, html, idx, parents;\r
664 \r
665                                 // Get text node value at beginning of paragraph\r
666                                 for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling)\r
667                                         val += sib.nodeValue;\r
668 \r
669                                 val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/&nbsp;/g, '\u00a0');\r
670 \r
671                                 // Detect unordered lists look for bullets\r
672                                 if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*\u00a0*/.test(val))\r
673                                         type = 'ul';\r
674 \r
675                                 // Detect ordered lists 1., a. or ixv.\r
676                                 if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0+/.test(val))\r
677                                         type = 'ol';\r
678 \r
679                                 // Check if node value matches the list pattern: o&nbsp;&nbsp;\r
680                                 if (type) {\r
681                                         margin = parseFloat(p.style.marginLeft || 0);\r
682 \r
683                                         if (margin > lastMargin)\r
684                                                 levels.push(margin);\r
685 \r
686                                         if (!listElm || type != lastType) {\r
687                                                 listElm = dom.create(type);\r
688                                                 dom.insertAfter(listElm, p);\r
689                                         } else {\r
690                                                 // Nested list element\r
691                                                 if (margin > lastMargin) {\r
692                                                         listElm = li.appendChild(dom.create(type));\r
693                                                 } else if (margin < lastMargin) {\r
694                                                         // Find parent level based on margin value\r
695                                                         idx = tinymce.inArray(levels, margin);\r
696                                                         parents = dom.getParents(listElm.parentNode, type);\r
697                                                         listElm = parents[parents.length - 1 - idx] || listElm;\r
698                                                 }\r
699                                         }\r
700 \r
701                                         // Remove middot or number spans if they exists\r
702                                         each(dom.select('span', p), function(span) {\r
703                                                 var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, '');\r
704 \r
705                                                 // Remove span with the middot or the number\r
706                                                 if (type == 'ul' && /^__MCE_ITEM__[\u2022\u00b7\u00a7\u00d8o\u25CF]/.test(html))\r
707                                                         dom.remove(span);\r
708                                                 else if (/^__MCE_ITEM__[\s\S]*\w+\.(&nbsp;|\u00a0)*\s*/.test(html))\r
709                                                         dom.remove(span);\r
710                                         });\r
711 \r
712                                         html = p.innerHTML;\r
713 \r
714                                         // Remove middot/list items\r
715                                         if (type == 'ul')\r
716                                                 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*(&nbsp;|\u00a0)+\s*/, '');\r
717                                         else\r
718                                                 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*\w+\.(&nbsp;|\u00a0)+\s*/, '');\r
719 \r
720                                         // Create li and add paragraph data into the new li\r
721                                         li = listElm.appendChild(dom.create('li', 0, html));\r
722                                         dom.remove(p);\r
723 \r
724                                         lastMargin = margin;\r
725                                         lastType = type;\r
726                                 } else\r
727                                         listElm = lastMargin = 0; // End list element\r
728                         });\r
729 \r
730                         // Remove any left over makers\r
731                         html = o.node.innerHTML;\r
732                         if (html.indexOf('__MCE_ITEM__') != -1)\r
733                                 o.node.innerHTML = html.replace(/__MCE_ITEM__/g, '');\r
734                 },\r
735 \r
736                 /**\r
737                  * Inserts the specified contents at the caret position.\r
738                  */\r
739                 _insert : function(h, skip_undo) {\r
740                         var ed = this.editor, r = ed.selection.getRng();\r
741 \r
742                         // First delete the contents seems to work better on WebKit when the selection spans multiple list items or multiple table cells.\r
743                         if (!ed.selection.isCollapsed() && r.startContainer != r.endContainer)\r
744                                 ed.getDoc().execCommand('Delete', false, null);\r
745 \r
746                         ed.execCommand('mceInsertContent', false, h, {skip_undo : skip_undo});\r
747                 },\r
748 \r
749                 /**\r
750                  * Instead of the old plain text method which tried to re-create a paste operation, the\r
751                  * new approach adds a plain text mode toggle switch that changes the behavior of paste.\r
752                  * This function is passed the same input that the regular paste plugin produces.\r
753                  * It performs additional scrubbing and produces (and inserts) the plain text.\r
754                  * This approach leverages all of the great existing functionality in the paste\r
755                  * plugin, and requires minimal changes to add the new functionality.\r
756                  * Speednet - June 2009\r
757                  */\r
758                 _insertPlainText : function(ed, dom, h) {\r
759                         var i, len, pos, rpos, node, breakElms, before, after,\r
760                                 w = ed.getWin(),\r
761                                 d = ed.getDoc(),\r
762                                 sel = ed.selection,\r
763                                 is = tinymce.is,\r
764                                 inArray = tinymce.inArray,\r
765                                 linebr = getParam(ed, "paste_text_linebreaktype"),\r
766                                 rl = getParam(ed, "paste_text_replacements");\r
767 \r
768                         function process(items) {\r
769                                 each(items, function(v) {\r
770                                         if (v.constructor == RegExp)\r
771                                                 h = h.replace(v, "");\r
772                                         else\r
773                                                 h = h.replace(v[0], v[1]);\r
774                                 });\r
775                         };\r
776 \r
777                         if ((typeof(h) === "string") && (h.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(h)) {\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                                         [/\n{3,}/g, "\n\n"],                                                    // Max. 2 consecutive linebreaks\r
798                                         /^\s+|\s+$/g                                                                    // Trim the front & back\r
799                                 ]);\r
800 \r
801                                 h = dom.decode(tinymce.html.Entities.encodeRaw(h));\r
802 \r
803                                 // Delete any highlighted text before pasting\r
804                                 if (!sel.isCollapsed()) {\r
805                                         d.execCommand("Delete", false, null);\r
806                                 }\r
807 \r
808                                 // Perform default or custom replacements\r
809                                 if (is(rl, "array") || (is(rl, "array"))) {\r
810                                         process(rl);\r
811                                 }\r
812                                 else if (is(rl, "string")) {\r
813                                         process(new RegExp(rl, "gi"));\r
814                                 }\r
815 \r
816                                 // Treat paragraphs as specified in the config\r
817                                 if (linebr == "none") {\r
818                                         process([\r
819                                                 [/\n+/g, " "]\r
820                                         ]);\r
821                                 }\r
822                                 else if (linebr == "br") {\r
823                                         process([\r
824                                                 [/\n/g, "<br />"]\r
825                                         ]);\r
826                                 }\r
827                                 else {\r
828                                         process([\r
829                                                 /^\s+|\s+$/g,\r
830                                                 [/\n\n/g, "</p><p>"],\r
831                                                 [/\n/g, "<br />"]\r
832                                         ]);\r
833                                 }\r
834 \r
835                                 // This next piece of code handles the situation where we're pasting more than one paragraph of plain\r
836                                 // text, and we are pasting the content into the middle of a block node in the editor.  The block\r
837                                 // node gets split at the selection point into "Para A" and "Para B" (for the purposes of explaining).\r
838                                 // The first paragraph of the pasted text is appended to "Para A", and the last paragraph of the\r
839                                 // pasted text is prepended to "Para B".  Any other paragraphs of pasted text are placed between\r
840                                 // "Para A" and "Para B".  This code solves a host of problems with the original plain text plugin and\r
841                                 // now handles styles correctly.  (Pasting plain text into a styled paragraph is supposed to make the\r
842                                 // plain text take the same style as the existing paragraph.)\r
843                                 if ((pos = h.indexOf("</p><p>")) != -1) {\r
844                                         rpos = h.lastIndexOf("</p><p>");\r
845                                         node = sel.getNode(); \r
846                                         breakElms = [];         // Get list of elements to break \r
847 \r
848                                         do {\r
849                                                 if (node.nodeType == 1) {\r
850                                                         // Don't break tables and break at body\r
851                                                         if (node.nodeName == "TD" || node.nodeName == "BODY") {\r
852                                                                 break;\r
853                                                         }\r
854 \r
855                                                         breakElms[breakElms.length] = node;\r
856                                                 }\r
857                                         } while (node = node.parentNode);\r
858 \r
859                                         // Are we in the middle of a block node?\r
860                                         if (breakElms.length > 0) {\r
861                                                 before = h.substring(0, pos);\r
862                                                 after = "";\r
863 \r
864                                                 for (i=0, len=breakElms.length; i<len; i++) {\r
865                                                         before += "</" + breakElms[i].nodeName.toLowerCase() + ">";\r
866                                                         after += "<" + breakElms[breakElms.length-i-1].nodeName.toLowerCase() + ">";\r
867                                                 }\r
868 \r
869                                                 if (pos == rpos) {\r
870                                                         h = before + after + h.substring(pos+7);\r
871                                                 }\r
872                                                 else {\r
873                                                         h = before + h.substring(pos+4, rpos+4) + after + h.substring(rpos+7);\r
874                                                 }\r
875                                         }\r
876                                 }\r
877 \r
878                                 // Insert content at the caret, plus add a marker for repositioning the caret\r
879                                 ed.execCommand("mceInsertRawHTML", false, h + '<span id="_plain_text_marker">&nbsp;</span>');\r
880 \r
881                                 // Reposition the caret to the marker, which was placed immediately after the inserted content.\r
882                                 // Needs to be done asynchronously (in window.setTimeout) or else it doesn't work in all browsers.\r
883                                 // The second part of the code scrolls the content up if the caret is positioned off-screen.\r
884                                 // This is only necessary for WebKit browsers, but it doesn't hurt to use for all.\r
885                                 window.setTimeout(function() {\r
886                                         var marker = dom.get('_plain_text_marker'),\r
887                                                 elm, vp, y, elmHeight;\r
888 \r
889                                         sel.select(marker, false);\r
890                                         d.execCommand("Delete", false, null);\r
891                                         marker = null;\r
892 \r
893                                         // Get element, position and height\r
894                                         elm = sel.getStart();\r
895                                         vp = dom.getViewPort(w);\r
896                                         y = dom.getPos(elm).y;\r
897                                         elmHeight = elm.clientHeight;\r
898 \r
899                                         // Is element within viewport if not then scroll it into view\r
900                                         if ((y < vp.y) || (y + elmHeight > vp.y + vp.h)) {\r
901                                                 d.body.scrollTop = y < vp.y ? y : y - vp.h + 25;\r
902                                         }\r
903                                 }, 0);\r
904                         }\r
905                 },\r
906 \r
907                 /**\r
908                  * This method will open the old style paste dialogs. Some users might want the old behavior but still use the new cleanup engine.\r
909                  */\r
910                 _legacySupport : function() {\r
911                         var t = this, ed = t.editor;\r
912 \r
913                         // Register command(s) for backwards compatibility\r
914                         ed.addCommand("mcePasteWord", function() {\r
915                                 ed.windowManager.open({\r
916                                         file: t.url + "/pasteword.htm",\r
917                                         width: parseInt(getParam(ed, "paste_dialog_width")),\r
918                                         height: parseInt(getParam(ed, "paste_dialog_height")),\r
919                                         inline: 1\r
920                                 });\r
921                         });\r
922 \r
923                         if (getParam(ed, "paste_text_use_dialog")) {\r
924                                 ed.addCommand("mcePasteText", function() {\r
925                                         ed.windowManager.open({\r
926                                                 file : t.url + "/pastetext.htm",\r
927                                                 width: parseInt(getParam(ed, "paste_dialog_width")),\r
928                                                 height: parseInt(getParam(ed, "paste_dialog_height")),\r
929                                                 inline : 1\r
930                                         });\r
931                                 });\r
932                         }\r
933 \r
934                         // Register button for backwards compatibility\r
935                         ed.addButton("pasteword", {title : "paste.paste_word_desc", cmd : "mcePasteWord"});\r
936                 }\r
937         });\r
938 \r
939         // Register plugin\r
940         tinymce.PluginManager.add("paste", tinymce.plugins.PastePlugin);\r
941 })();\r