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