bgneal@45: /** bgneal@183: * editor_plugin_src.js bgneal@45: * bgneal@183: * Copyright 2009, Moxiecode Systems AB bgneal@183: * Released under LGPL License. bgneal@183: * bgneal@183: * License: http://tinymce.moxiecode.com/license bgneal@183: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@45: */ bgneal@45: bgneal@45: (function() { bgneal@183: var each = tinymce.each, bgneal@183: entities = null, bgneal@183: defs = { bgneal@183: paste_auto_cleanup_on_paste : true, bgneal@183: paste_block_drop : false, bgneal@183: paste_retain_style_properties : "none", bgneal@183: paste_strip_class_attributes : "mso", bgneal@183: paste_remove_spans : false, bgneal@183: paste_remove_styles : false, bgneal@183: paste_remove_styles_if_webkit : true, bgneal@183: paste_convert_middot_lists : true, bgneal@183: paste_convert_headers_to_strong : false, bgneal@183: paste_dialog_width : "450", bgneal@183: paste_dialog_height : "400", bgneal@183: paste_text_use_dialog : false, bgneal@183: paste_text_sticky : false, bgneal@183: paste_text_notifyalways : false, bgneal@183: paste_text_linebreaktype : "p", bgneal@183: paste_text_replacements : [ bgneal@183: [/\u2026/g, "..."], bgneal@183: [/[\x93\x94\u201c\u201d]/g, '"'], bgneal@183: [/[\x60\x91\x92\u2018\u2019]/g, "'"] bgneal@183: ] bgneal@183: }; bgneal@183: bgneal@183: function getParam(ed, name) { bgneal@183: return ed.getParam(name, defs[name]); bgneal@183: } bgneal@45: bgneal@45: tinymce.create('tinymce.plugins.PastePlugin', { bgneal@45: init : function(ed, url) { bgneal@45: var t = this; bgneal@45: bgneal@183: t.editor = ed; bgneal@183: t.url = url; bgneal@45: bgneal@183: // Setup plugin events bgneal@183: t.onPreProcess = new tinymce.util.Dispatcher(t); bgneal@183: t.onPostProcess = new tinymce.util.Dispatcher(t); bgneal@183: bgneal@183: // Register default handlers bgneal@183: t.onPreProcess.add(t._preProcess); bgneal@183: t.onPostProcess.add(t._postProcess); bgneal@183: bgneal@183: // Register optional preprocess handler bgneal@183: t.onPreProcess.add(function(pl, o) { bgneal@183: ed.execCallback('paste_preprocess', pl, o); bgneal@45: }); bgneal@45: bgneal@183: // Register optional postprocess bgneal@183: t.onPostProcess.add(function(pl, o) { bgneal@183: ed.execCallback('paste_postprocess', pl, o); bgneal@45: }); bgneal@45: bgneal@183: // Initialize plain text flag bgneal@183: ed.pasteAsPlainText = false; bgneal@183: bgneal@183: // This function executes the process handlers and inserts the contents bgneal@183: // force_rich overrides plain text mode set by user, important for pasting with execCommand bgneal@183: function process(o, force_rich) { bgneal@183: var dom = ed.dom; bgneal@183: bgneal@183: // Execute pre process handlers bgneal@183: t.onPreProcess.dispatch(t, o); bgneal@183: bgneal@183: // Create DOM structure bgneal@183: o.node = dom.create('div', 0, o.content); bgneal@183: bgneal@183: // Execute post process handlers bgneal@183: t.onPostProcess.dispatch(t, o); bgneal@183: bgneal@183: // Serialize content bgneal@183: o.content = ed.serializer.serialize(o.node, {getInner : 1}); bgneal@183: bgneal@183: // Plain text option active? bgneal@183: if ((!force_rich) && (ed.pasteAsPlainText)) { bgneal@183: t._insertPlainText(ed, dom, o.content); bgneal@183: bgneal@183: if (!getParam(ed, "paste_text_sticky")) { bgneal@183: ed.pasteAsPlainText = false; bgneal@183: ed.controlManager.setActive("pastetext", false); bgneal@183: } bgneal@183: } else if (/<(p|h[1-6]|ul|ol)/.test(o.content)) { bgneal@183: // Handle insertion of contents containing block elements separately bgneal@183: t._insertBlockContent(ed, dom, o.content); bgneal@183: } else { bgneal@183: t._insert(o.content); bgneal@183: } bgneal@183: } bgneal@183: bgneal@183: // Add command for external usage bgneal@183: ed.addCommand('mceInsertClipboardContent', function(u, o) { bgneal@183: process(o, true); bgneal@45: }); bgneal@45: bgneal@183: if (!getParam(ed, "paste_text_use_dialog")) { bgneal@183: ed.addCommand('mcePasteText', function(u, v) { bgneal@183: var cookie = tinymce.util.Cookie; bgneal@45: bgneal@183: ed.pasteAsPlainText = !ed.pasteAsPlainText; bgneal@183: ed.controlManager.setActive('pastetext', ed.pasteAsPlainText); bgneal@183: bgneal@183: if ((ed.pasteAsPlainText) && (!cookie.get("tinymcePasteText"))) { bgneal@183: if (getParam(ed, "paste_text_sticky")) { bgneal@183: ed.windowManager.alert("Paste is now in plain text mode. Click again to toggle back to regular paste mode. After you paste something you will be returned to regular paste mode."); bgneal@183: } else { bgneal@183: ed.windowManager.alert("Paste is now in plain text mode. Click again to toggle back to regular paste mode."); bgneal@183: } bgneal@183: bgneal@183: if (!getParam(ed, "paste_text_notifyalways")) { bgneal@183: cookie.set("tinymcePasteText", "1", new Date(new Date().getFullYear() + 1, 12, 31)) bgneal@183: } bgneal@183: } bgneal@45: }); bgneal@45: } bgneal@45: bgneal@183: ed.addButton('pastetext', {title: 'paste.paste_text_desc', cmd: 'mcePasteText'}); bgneal@183: ed.addButton('selectall', {title: 'paste.selectall_desc', cmd: 'selectall'}); bgneal@45: bgneal@183: // This function grabs the contents from the clipboard by adding a bgneal@183: // hidden div and placing the caret inside it and after the browser paste bgneal@183: // is done it grabs that contents and processes that bgneal@183: function grabContent(e) { bgneal@183: var n, or, rng, sel = ed.selection, dom = ed.dom, body = ed.getBody(), posY; bgneal@183: bgneal@183: if (dom.get('_mcePaste')) bgneal@183: return; bgneal@183: bgneal@183: // Create container to paste into bgneal@183: n = dom.add(body, 'div', {id : '_mcePaste', 'class' : 'mcePaste'}, '\uFEFF'); bgneal@183: bgneal@183: // If contentEditable mode we need to find out the position of the closest element bgneal@183: if (body != ed.getDoc().body) bgneal@183: posY = dom.getPos(ed.selection.getStart(), body).y; bgneal@183: else bgneal@183: posY = body.scrollTop; bgneal@183: bgneal@183: // Styles needs to be applied after the element is added to the document since WebKit will otherwise remove all styles bgneal@183: dom.setStyles(n, { bgneal@183: position : 'absolute', bgneal@183: left : -10000, bgneal@183: top : posY, bgneal@183: width : 1, bgneal@183: height : 1, bgneal@183: overflow : 'hidden' bgneal@183: }); bgneal@183: bgneal@183: if (tinymce.isIE) { bgneal@183: // Select the container bgneal@183: rng = dom.doc.body.createTextRange(); bgneal@183: rng.moveToElementText(n); bgneal@183: rng.execCommand('Paste'); bgneal@183: bgneal@183: // Remove container bgneal@183: dom.remove(n); bgneal@183: bgneal@183: // Check if the contents was changed, if it wasn't then clipboard extraction failed probably due bgneal@183: // to IE security settings so we pass the junk though better than nothing right bgneal@183: if (n.innerHTML === '\uFEFF') { bgneal@183: ed.execCommand('mcePasteWord'); bgneal@183: e.preventDefault(); bgneal@183: return; bgneal@45: } bgneal@183: bgneal@183: // Process contents bgneal@183: process({content : n.innerHTML}); bgneal@183: bgneal@183: // Block the real paste event bgneal@183: return tinymce.dom.Event.cancel(e); bgneal@183: } else { bgneal@183: function block(e) { bgneal@183: e.preventDefault(); bgneal@183: }; bgneal@183: bgneal@183: // Block mousedown and click to prevent selection change bgneal@183: dom.bind(ed.getDoc(), 'mousedown', block); bgneal@183: dom.bind(ed.getDoc(), 'keydown', block); bgneal@183: bgneal@183: or = ed.selection.getRng(); bgneal@183: bgneal@183: // Move caret into hidden div bgneal@183: n = n.firstChild; bgneal@183: rng = ed.getDoc().createRange(); bgneal@183: rng.setStart(n, 0); bgneal@183: rng.setEnd(n, 1); bgneal@183: sel.setRng(rng); bgneal@183: bgneal@183: // Wait a while and grab the pasted contents bgneal@183: window.setTimeout(function() { bgneal@183: var h = '', nl = dom.select('div.mcePaste'); bgneal@183: bgneal@183: // WebKit will split the div into multiple ones so this will loop through then all and join them to get the whole HTML string bgneal@183: each(nl, function(n) { bgneal@183: // WebKit duplicates the divs so we need to remove them bgneal@183: each(dom.select('div.mcePaste', n), function(n) { bgneal@183: dom.remove(n, 1); bgneal@183: }); bgneal@183: bgneal@183: // Contents in WebKit is sometimes wrapped in a apple style span so we need to grab it from that one bgneal@183: h += (dom.select('> span.Apple-style-span div', n)[0] || dom.select('> span.Apple-style-span', n)[0] || n).innerHTML; bgneal@183: }); bgneal@183: bgneal@183: // Remove the nodes bgneal@183: each(nl, function(n) { bgneal@183: dom.remove(n); bgneal@183: }); bgneal@183: bgneal@183: // Restore the old selection bgneal@183: if (or) bgneal@183: sel.setRng(or); bgneal@183: bgneal@183: process({content : h}); bgneal@183: bgneal@183: // Unblock events ones we got the contents bgneal@183: dom.unbind(ed.getDoc(), 'mousedown', block); bgneal@183: dom.unbind(ed.getDoc(), 'keydown', block); bgneal@183: }, 0); bgneal@183: } bgneal@183: } bgneal@183: bgneal@183: // Check if we should use the new auto process method bgneal@183: if (getParam(ed, "paste_auto_cleanup_on_paste")) { bgneal@183: // Is it's Opera or older FF use key handler bgneal@183: if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) { bgneal@183: ed.onKeyDown.add(function(ed, e) { bgneal@183: if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45)) bgneal@183: grabContent(e); bgneal@183: }); bgneal@183: } else { bgneal@183: // Grab contents on paste event on Gecko and WebKit bgneal@183: ed.onPaste.addToTop(function(ed, e) { bgneal@183: return grabContent(e); bgneal@183: }); bgneal@183: } bgneal@183: } bgneal@183: bgneal@183: // Block all drag/drop events bgneal@183: if (getParam(ed, "paste_block_drop")) { bgneal@183: ed.onInit.add(function() { bgneal@183: ed.dom.bind(ed.getBody(), ['dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag'], function(e) { bgneal@183: e.preventDefault(); bgneal@183: e.stopPropagation(); bgneal@183: bgneal@183: return false; bgneal@183: }); bgneal@45: }); bgneal@45: } bgneal@183: bgneal@183: // Add legacy support bgneal@183: t._legacySupport(); bgneal@45: }, bgneal@45: bgneal@45: getInfo : function() { bgneal@45: return { bgneal@45: longname : 'Paste text/word', bgneal@45: author : 'Moxiecode Systems AB', bgneal@45: authorurl : 'http://tinymce.moxiecode.com', bgneal@45: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste', bgneal@45: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@45: }; bgneal@45: }, bgneal@45: bgneal@183: _preProcess : function(pl, o) { bgneal@183: //console.log('Before preprocess:' + o.content); bgneal@45: bgneal@183: var ed = this.editor, bgneal@183: h = o.content, bgneal@183: grep = tinymce.grep, bgneal@183: explode = tinymce.explode, bgneal@183: trim = tinymce.trim, bgneal@183: len, stripClass; bgneal@45: bgneal@183: function process(items) { bgneal@183: each(items, function(v) { bgneal@183: // Remove or replace bgneal@183: if (v.constructor == RegExp) bgneal@183: h = h.replace(v, ''); bgneal@183: else bgneal@183: h = h.replace(v[0], v[1]); bgneal@183: }); bgneal@183: } bgneal@45: bgneal@183: // Detect Word content and process it more aggressive bgneal@183: if (/class="?Mso|style="[^"]*\bmso-|w:WordDocument/i.test(h) || o.wordContent) { bgneal@183: o.wordContent = true; // Mark the pasted contents as word specific content bgneal@183: //console.log('Word contents detected.'); bgneal@45: bgneal@183: // Process away some basic content bgneal@183: process([ bgneal@183: /^\s*( )+/gi, //   entities at the start of contents bgneal@183: /( |]*>)+\s*$/gi //   entities at the end of contents bgneal@183: ]); bgneal@183: bgneal@183: if (getParam(ed, "paste_convert_headers_to_strong")) { bgneal@183: h = h.replace(/

]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "

$1

"); bgneal@183: } bgneal@183: bgneal@183: if (getParam(ed, "paste_convert_middot_lists")) { bgneal@183: process([ bgneal@183: [//gi, '$&__MCE_ITEM__'], // Convert supportLists to a list item marker bgneal@183: [/(]+(?:mso-list:|:\s*symbol)[^>]+>)/gi, '$1__MCE_ITEM__'] // Convert mso-list and symbol spans to item markers bgneal@183: ]); bgneal@183: } bgneal@183: bgneal@183: process([ bgneal@183: // Word comments like conditional comments etc bgneal@183: //gi, bgneal@183: bgneal@183: // Remove comments, scripts (e.g., msoShowComment), XML tag, VML content, MS Office namespaced tags, and a few other tags bgneal@183: /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|img|meta|link|style|\w:\w+)(?=[\s\/>]))[^>]*>/gi, bgneal@183: bgneal@183: // Convert into for line-though bgneal@183: [/<(\/?)s>/gi, "<$1strike>"], bgneal@183: bgneal@183: // Replace nsbp entites to char since it's easier to handle bgneal@183: [/ /gi, "\u00a0"] bgneal@183: ]); bgneal@183: bgneal@183: // Remove bad attributes, with or without quotes, ensuring that attribute text is really inside a tag. bgneal@183: // 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. bgneal@183: do { bgneal@183: len = h.length; bgneal@183: h = h.replace(/(<[a-z][^>]*\s)(?:id|name|language|type|on\w+|\w+:\w+)=(?:"[^"]*"|\w+)\s?/gi, "$1"); bgneal@183: } while (len != h.length); bgneal@183: bgneal@183: // Remove all spans if no styles is to be retained bgneal@183: if (getParam(ed, "paste_retain_style_properties").replace(/^none$/i, "").length == 0) { bgneal@183: h = h.replace(/<\/?span[^>]*>/gi, ""); bgneal@183: } else { bgneal@183: // We're keeping styles, so at least clean them up. bgneal@183: // CSS Reference: http://msdn.microsoft.com/en-us/library/aa155477.aspx bgneal@183: bgneal@183: process([ bgneal@183: // Convert ___ to string of alternating breaking/non-breaking spaces of same length bgneal@183: [/([\s\u00a0]*)<\/span>/gi, bgneal@183: function(str, spaces) { bgneal@183: return (spaces.length > 0)? spaces.replace(/./, " ").slice(Math.floor(spaces.length/2)).split("").join("\u00a0") : ""; bgneal@183: } bgneal@183: ], bgneal@183: bgneal@183: // Examine all styles: delete junk, transform some, and keep the rest bgneal@183: [/(<[a-z][^>]*)\sstyle="([^"]*)"/gi, bgneal@183: function(str, tag, style) { bgneal@183: var n = [], bgneal@183: i = 0, bgneal@183: s = explode(trim(style).replace(/"/gi, "'"), ";"); bgneal@183: bgneal@183: // Examine each style definition within the tag's style attribute bgneal@183: each(s, function(v) { bgneal@183: var name, value, bgneal@183: parts = explode(v, ":"); bgneal@183: bgneal@183: function ensureUnits(v) { bgneal@183: return v + ((v !== "0") && (/\d$/.test(v)))? "px" : ""; bgneal@183: } bgneal@183: bgneal@183: if (parts.length == 2) { bgneal@183: name = parts[0].toLowerCase(); bgneal@183: value = parts[1].toLowerCase(); bgneal@183: bgneal@183: // Translate certain MS Office styles into their CSS equivalents bgneal@183: switch (name) { bgneal@183: case "mso-padding-alt": bgneal@183: case "mso-padding-top-alt": bgneal@183: case "mso-padding-right-alt": bgneal@183: case "mso-padding-bottom-alt": bgneal@183: case "mso-padding-left-alt": bgneal@183: case "mso-margin-alt": bgneal@183: case "mso-margin-top-alt": bgneal@183: case "mso-margin-right-alt": bgneal@183: case "mso-margin-bottom-alt": bgneal@183: case "mso-margin-left-alt": bgneal@183: case "mso-table-layout-alt": bgneal@183: case "mso-height": bgneal@183: case "mso-width": bgneal@183: case "mso-vertical-align-alt": bgneal@183: n[i++] = name.replace(/^mso-|-alt$/g, "") + ":" + ensureUnits(value); bgneal@183: return; bgneal@183: bgneal@183: case "horiz-align": bgneal@183: n[i++] = "text-align:" + value; bgneal@183: return; bgneal@183: bgneal@183: case "vert-align": bgneal@183: n[i++] = "vertical-align:" + value; bgneal@183: return; bgneal@183: bgneal@183: case "font-color": bgneal@183: case "mso-foreground": bgneal@183: n[i++] = "color:" + value; bgneal@183: return; bgneal@183: bgneal@183: case "mso-background": bgneal@183: case "mso-highlight": bgneal@183: n[i++] = "background:" + value; bgneal@183: return; bgneal@183: bgneal@183: case "mso-default-height": bgneal@183: n[i++] = "min-height:" + ensureUnits(value); bgneal@183: return; bgneal@183: bgneal@183: case "mso-default-width": bgneal@183: n[i++] = "min-width:" + ensureUnits(value); bgneal@183: return; bgneal@183: bgneal@183: case "mso-padding-between-alt": bgneal@183: n[i++] = "border-collapse:separate;border-spacing:" + ensureUnits(value); bgneal@183: return; bgneal@183: bgneal@183: case "text-line-through": bgneal@183: if ((value == "single") || (value == "double")) { bgneal@183: n[i++] = "text-decoration:line-through"; bgneal@183: } bgneal@183: return; bgneal@183: bgneal@183: case "mso-zero-height": bgneal@183: if (value == "yes") { bgneal@183: n[i++] = "display:none"; bgneal@183: } bgneal@183: return; bgneal@183: } bgneal@183: bgneal@183: // Eliminate all MS Office style definitions that have no CSS equivalent by examining the first characters in the name bgneal@183: 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)) { bgneal@183: return; bgneal@183: } bgneal@183: bgneal@183: // If it reached this point, it must be a valid CSS style bgneal@183: n[i++] = name + ":" + parts[1]; // Lower-case name, but keep value case bgneal@183: } bgneal@183: }); bgneal@183: bgneal@183: // If style attribute contained any valid styles the re-write it; otherwise delete style attribute. bgneal@183: if (i > 0) { bgneal@183: return tag + ' style="' + n.join(';') + '"'; bgneal@183: } else { bgneal@183: return tag; bgneal@183: } bgneal@183: } bgneal@183: ] bgneal@183: ]); bgneal@183: } bgneal@183: } bgneal@183: bgneal@183: // Replace headers with bgneal@183: if (getParam(ed, "paste_convert_headers_to_strong")) { bgneal@183: process([ bgneal@183: [/]*>/gi, "

"], bgneal@183: [/<\/h[1-6][^>]*>/gi, "

"] bgneal@183: ]); bgneal@183: } bgneal@183: bgneal@183: // Class attribute options are: leave all as-is ("none"), remove all ("all"), or remove only those starting with mso ("mso"). bgneal@183: // Note:- paste_strip_class_attributes: "none", verify_css_classes: true is also a good variation. bgneal@183: stripClass = getParam(ed, "paste_strip_class_attributes"); bgneal@183: bgneal@183: if (stripClass !== "none") { bgneal@183: function removeClasses(match, g1) { bgneal@183: if (stripClass === "all") bgneal@183: return ''; bgneal@183: bgneal@183: var cls = grep(explode(g1.replace(/^(["'])(.*)\1$/, "$2"), " "), bgneal@183: function(v) { bgneal@183: return (/^(?!mso)/i.test(v)); bgneal@183: } bgneal@183: ); bgneal@183: bgneal@183: return cls.length ? ' class="' + cls.join(" ") + '"' : ''; bgneal@183: }; bgneal@183: bgneal@183: h = h.replace(/ class="([^"]+)"/gi, removeClasses); bgneal@183: h = h.replace(/ class=(\w+)/gi, removeClasses); bgneal@183: } bgneal@183: bgneal@183: // Remove spans option bgneal@183: if (getParam(ed, "paste_remove_spans")) { bgneal@183: h = h.replace(/<\/?span[^>]*>/gi, ""); bgneal@183: } bgneal@183: bgneal@183: //console.log('After preprocess:' + h); bgneal@183: bgneal@183: o.content = h; bgneal@45: }, bgneal@45: bgneal@183: /** bgneal@183: * Various post process items. bgneal@183: */ bgneal@183: _postProcess : function(pl, o) { bgneal@183: var t = this, ed = t.editor, dom = ed.dom, styleProps; bgneal@45: bgneal@183: if (o.wordContent) { bgneal@183: // Remove named anchors or TOC links bgneal@183: each(dom.select('a', o.node), function(a) { bgneal@183: if (!a.href || a.href.indexOf('#_Toc') != -1) bgneal@183: dom.remove(a, 1); bgneal@183: }); bgneal@45: bgneal@183: if (getParam(ed, "paste_convert_middot_lists")) { bgneal@183: t._convertLists(pl, o); bgneal@45: } bgneal@45: bgneal@183: // Process styles bgneal@183: styleProps = getParam(ed, "paste_retain_style_properties"); // retained properties bgneal@45: bgneal@183: // Process only if a string was specified and not equal to "all" or "*" bgneal@183: if ((tinymce.is(styleProps, "string")) && (styleProps !== "all") && (styleProps !== "*")) { bgneal@183: styleProps = tinymce.explode(styleProps.replace(/^none$/i, "")); bgneal@45: bgneal@183: // Retains some style properties bgneal@183: each(dom.select('*', o.node), function(el) { bgneal@183: var newStyle = {}, npc = 0, i, sp, sv; bgneal@45: bgneal@183: // Store a subset of the existing styles bgneal@183: if (styleProps) { bgneal@183: for (i = 0; i < styleProps.length; i++) { bgneal@183: sp = styleProps[i]; bgneal@183: sv = dom.getStyle(el, sp); bgneal@45: bgneal@183: if (sv) { bgneal@183: newStyle[sp] = sv; bgneal@183: npc++; bgneal@183: } bgneal@183: } bgneal@183: } bgneal@45: bgneal@183: // Remove all of the existing styles bgneal@183: dom.setAttrib(el, 'style', ''); bgneal@183: bgneal@183: if (styleProps && npc > 0) bgneal@183: dom.setStyles(el, newStyle); // Add back the stored subset of styles bgneal@183: else // Remove empty span tags that do not have class attributes bgneal@183: if (el.nodeName == 'SPAN' && !el.className) bgneal@183: dom.remove(el, true); bgneal@183: }); bgneal@45: } bgneal@183: } bgneal@45: bgneal@183: // Remove all style information or only specifically on WebKit to avoid the style bug on that browser bgneal@183: if (getParam(ed, "paste_remove_styles") || (getParam(ed, "paste_remove_styles_if_webkit") && tinymce.isWebKit)) { bgneal@183: each(dom.select('*[style]', o.node), function(el) { bgneal@183: el.removeAttribute('style'); bgneal@183: el.removeAttribute('_mce_style'); bgneal@183: }); bgneal@183: } else { bgneal@183: if (tinymce.isWebKit) { bgneal@183: // We need to compress the styles on WebKit since if you paste it will become bgneal@183: // Removing the mce_style that contains the real value will force the Serializer engine to compress the styles bgneal@183: each(dom.select('*', o.node), function(el) { bgneal@183: el.removeAttribute('_mce_style'); bgneal@183: }); bgneal@45: } bgneal@45: } bgneal@45: }, bgneal@45: bgneal@183: /** bgneal@183: * Converts the most common bullet and number formats in Office into a real semantic UL/LI list. bgneal@183: */ bgneal@183: _convertLists : function(pl, o) { bgneal@183: var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html; bgneal@45: bgneal@183: // Convert middot lists into real semantic lists bgneal@183: each(dom.select('p', o.node), function(p) { bgneal@183: var sib, val = '', type, html, idx, parents; bgneal@45: bgneal@183: // Get text node value at beginning of paragraph bgneal@183: for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling) bgneal@183: val += sib.nodeValue; bgneal@45: bgneal@183: val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/ /g, '\u00a0'); bgneal@183: bgneal@183: // Detect unordered lists look for bullets bgneal@183: if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o]\s*\u00a0*/.test(val)) bgneal@183: type = 'ul'; bgneal@183: bgneal@183: // Detect ordered lists 1., a. or ixv. bgneal@183: if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0{2,}/.test(val)) bgneal@183: type = 'ol'; bgneal@183: bgneal@183: // Check if node value matches the list pattern: o   bgneal@183: if (type) { bgneal@183: margin = parseFloat(p.style.marginLeft || 0); bgneal@183: bgneal@183: if (margin > lastMargin) bgneal@183: levels.push(margin); bgneal@183: bgneal@183: if (!listElm || type != lastType) { bgneal@183: listElm = dom.create(type); bgneal@183: dom.insertAfter(listElm, p); bgneal@183: } else { bgneal@183: // Nested list element bgneal@183: if (margin > lastMargin) { bgneal@183: listElm = li.appendChild(dom.create(type)); bgneal@183: } else if (margin < lastMargin) { bgneal@183: // Find parent level based on margin value bgneal@183: idx = tinymce.inArray(levels, margin); bgneal@183: parents = dom.getParents(listElm.parentNode, type); bgneal@183: listElm = parents[parents.length - 1 - idx] || listElm; bgneal@183: } bgneal@183: } bgneal@183: bgneal@183: // Remove middot or number spans if they exists bgneal@183: each(dom.select('span', p), function(span) { bgneal@183: var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, ''); bgneal@183: bgneal@183: // Remove span with the middot or the number bgneal@183: if (type == 'ul' && /^[\u2022\u00b7\u00a7\u00d8o]/.test(html)) bgneal@183: dom.remove(span); bgneal@183: else if (/^[\s\S]*\w+\.( |\u00a0)*\s*/.test(html)) bgneal@183: dom.remove(span); bgneal@183: }); bgneal@183: bgneal@183: html = p.innerHTML; bgneal@183: bgneal@183: // Remove middot/list items bgneal@183: if (type == 'ul') bgneal@183: html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o]\s*( |\u00a0)+\s*/, ''); bgneal@183: else bgneal@183: html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*\w+\.( |\u00a0)+\s*/, ''); bgneal@183: bgneal@183: // Create li and add paragraph data into the new li bgneal@183: li = listElm.appendChild(dom.create('li', 0, html)); bgneal@183: dom.remove(p); bgneal@183: bgneal@183: lastMargin = margin; bgneal@183: lastType = type; bgneal@183: } else bgneal@183: listElm = lastMargin = 0; // End list element bgneal@183: }); bgneal@183: bgneal@183: // Remove any left over makers bgneal@183: html = o.node.innerHTML; bgneal@183: if (html.indexOf('__MCE_ITEM__') != -1) bgneal@183: o.node.innerHTML = html.replace(/__MCE_ITEM__/g, ''); bgneal@45: }, bgneal@45: bgneal@183: /** bgneal@183: * This method will split the current block parent and insert the contents inside the split position. bgneal@183: * This logic can be improved so text nodes at the start/end remain in the start/end block elements bgneal@183: */ bgneal@183: _insertBlockContent : function(ed, dom, content) { bgneal@183: var parentBlock, marker, sel = ed.selection, last, elm, vp, y, elmHeight, markerId = 'mce_marker'; bgneal@45: bgneal@183: function select(n) { bgneal@183: var r; bgneal@45: bgneal@183: if (tinymce.isIE) { bgneal@183: r = ed.getDoc().body.createTextRange(); bgneal@183: r.moveToElementText(n); bgneal@183: r.collapse(false); bgneal@183: r.select(); bgneal@183: } else { bgneal@183: sel.select(n, 1); bgneal@183: sel.collapse(false); bgneal@45: } bgneal@45: } bgneal@45: bgneal@183: // Insert a marker for the caret position bgneal@183: this._insert(' ', 1); bgneal@183: marker = dom.get(markerId); bgneal@183: parentBlock = dom.getParent(marker, 'p,h1,h2,h3,h4,h5,h6,ul,ol,th,td'); bgneal@183: bgneal@183: // If it's a parent block but not a table cell bgneal@183: if (parentBlock && !/TD|TH/.test(parentBlock.nodeName)) { bgneal@183: // Split parent block bgneal@183: marker = dom.split(parentBlock, marker); bgneal@183: bgneal@183: // Insert nodes before the marker bgneal@183: each(dom.create('div', 0, content).childNodes, function(n) { bgneal@183: last = marker.parentNode.insertBefore(n.cloneNode(true), marker); bgneal@183: }); bgneal@183: bgneal@183: // Move caret after marker bgneal@183: select(last); bgneal@183: } else { bgneal@183: dom.setOuterHTML(marker, content); bgneal@183: sel.select(ed.getBody(), 1); bgneal@183: sel.collapse(0); bgneal@183: } bgneal@183: bgneal@183: // Remove marker if it's left bgneal@183: while (elm = dom.get(markerId)) bgneal@183: dom.remove(elm); bgneal@183: bgneal@183: // Get element, position and height bgneal@183: elm = sel.getStart(); bgneal@183: vp = dom.getViewPort(ed.getWin()); bgneal@183: y = ed.dom.getPos(elm).y; bgneal@183: elmHeight = elm.clientHeight; bgneal@183: bgneal@183: // Is element within viewport if not then scroll it into view bgneal@183: if (y < vp.y || y + elmHeight > vp.y + vp.h) bgneal@183: ed.getDoc().body.scrollTop = y < vp.y ? y : y - vp.h + 25; bgneal@45: }, bgneal@45: bgneal@183: /** bgneal@183: * Inserts the specified contents at the caret position. bgneal@183: */ bgneal@183: _insert : function(h, skip_undo) { bgneal@183: var ed = this.editor; bgneal@45: bgneal@183: // First delete the contents seems to work better on WebKit bgneal@183: if (!ed.selection.isCollapsed()) bgneal@183: ed.getDoc().execCommand('Delete', false, null); bgneal@45: bgneal@183: // It's better to use the insertHTML method on Gecko since it will combine paragraphs correctly before inserting the contents bgneal@183: ed.execCommand(tinymce.isGecko ? 'insertHTML' : 'mceInsertContent', false, h, {skip_undo : skip_undo}); bgneal@183: }, bgneal@183: bgneal@183: /** bgneal@183: * Instead of the old plain text method which tried to re-create a paste operation, the bgneal@183: * new approach adds a plain text mode toggle switch that changes the behavior of paste. bgneal@183: * This function is passed the same input that the regular paste plugin produces. bgneal@183: * It performs additional scrubbing and produces (and inserts) the plain text. bgneal@183: * This approach leverages all of the great existing functionality in the paste bgneal@183: * plugin, and requires minimal changes to add the new functionality. bgneal@183: * Speednet - June 2009 bgneal@183: */ bgneal@183: _insertPlainText : function(ed, dom, h) { bgneal@183: var i, len, pos, rpos, node, breakElms, before, after, bgneal@183: w = ed.getWin(), bgneal@183: d = ed.getDoc(), bgneal@183: sel = ed.selection, bgneal@183: is = tinymce.is, bgneal@183: inArray = tinymce.inArray, bgneal@183: linebr = getParam(ed, "paste_text_linebreaktype"), bgneal@183: rl = getParam(ed, "paste_text_replacements"); bgneal@183: bgneal@183: function process(items) { bgneal@183: each(items, function(v) { bgneal@183: if (v.constructor == RegExp) bgneal@183: h = h.replace(v, ""); bgneal@183: else bgneal@183: h = h.replace(v[0], v[1]); bgneal@183: }); bgneal@183: }; bgneal@183: bgneal@183: if ((typeof(h) === "string") && (h.length > 0)) { bgneal@183: if (!entities) bgneal@183: entities = ("34,quot,38,amp,39,apos,60,lt,62,gt," + ed.serializer.settings.entities).split(","); bgneal@183: bgneal@183: // If HTML content with line-breaking tags, then remove all cr/lf chars because only tags will break a line bgneal@183: if (/<(?:p|br|h[1-6]|ul|ol|dl|table|t[rdh]|div|blockquote|fieldset|pre|address|center)[^>]*>/i.test(h)) { bgneal@183: process([ bgneal@183: /[\n\r]+/g bgneal@183: ]); bgneal@183: } else { bgneal@183: // Otherwise just get rid of carriage returns (only need linefeeds) bgneal@183: process([ bgneal@183: /\r+/g bgneal@183: ]); bgneal@45: } bgneal@45: bgneal@183: process([ bgneal@183: [/<\/(?: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 bgneal@183: [/]*>|<\/tr>/gi, "\n"], // Single linebreak for
tags and table rows bgneal@183: [/<\/t[dh]>\s*]*>/gi, "\t"], // Table cells get tabs betweem them bgneal@183: /<[a-z!\/?][^>]*>/gi, // Delete all remaining tags bgneal@183: [/ /gi, " "], // Convert non-break spaces to regular spaces (remember, *plain text*) bgneal@183: [ bgneal@183: // HTML entity bgneal@183: /&(#\d+|[a-z0-9]{1,10});/gi, bgneal@183: bgneal@183: // Replace with actual character bgneal@183: function(e, s) { bgneal@183: if (s.charAt(0) === "#") { bgneal@183: return String.fromCharCode(s.slice(1)); bgneal@183: } bgneal@183: else { bgneal@183: return ((e = inArray(entities, s)) > 0)? String.fromCharCode(entities[e-1]) : " "; bgneal@183: } bgneal@183: } bgneal@183: ], bgneal@183: [/(?:(?!\n)\s)*(\n+)(?:(?!\n)\s)*/gi, "$1"], // Cool little RegExp deletes whitespace around linebreak chars. bgneal@183: [/\n{3,}/g, "\n\n"], // Max. 2 consecutive linebreaks bgneal@183: /^\s+|\s+$/g // Trim the front & back bgneal@183: ]); bgneal@183: bgneal@183: h = dom.encode(h); bgneal@183: bgneal@183: // Delete any highlighted text before pasting bgneal@183: if (!sel.isCollapsed()) { bgneal@183: d.execCommand("Delete", false, null); bgneal@183: } bgneal@183: bgneal@183: // Perform default or custom replacements bgneal@183: if (is(rl, "array") || (is(rl, "array"))) { bgneal@183: process(rl); bgneal@183: } bgneal@183: else if (is(rl, "string")) { bgneal@183: process(new RegExp(rl, "gi")); bgneal@183: } bgneal@183: bgneal@183: // Treat paragraphs as specified in the config bgneal@183: if (linebr == "none") { bgneal@183: process([ bgneal@183: [/\n+/g, " "] bgneal@183: ]); bgneal@183: } bgneal@183: else if (linebr == "br") { bgneal@183: process([ bgneal@183: [/\n/g, "
"] bgneal@183: ]); bgneal@183: } bgneal@183: else { bgneal@183: process([ bgneal@183: /^\s+|\s+$/g, bgneal@183: [/\n\n/g, "

"], bgneal@183: [/\n/g, "
"] bgneal@183: ]); bgneal@183: } bgneal@183: bgneal@183: // This next piece of code handles the situation where we're pasting more than one paragraph of plain bgneal@183: // text, and we are pasting the content into the middle of a block node in the editor. The block bgneal@183: // node gets split at the selection point into "Para A" and "Para B" (for the purposes of explaining). bgneal@183: // The first paragraph of the pasted text is appended to "Para A", and the last paragraph of the bgneal@183: // pasted text is prepended to "Para B". Any other paragraphs of pasted text are placed between bgneal@183: // "Para A" and "Para B". This code solves a host of problems with the original plain text plugin and bgneal@183: // now handles styles correctly. (Pasting plain text into a styled paragraph is supposed to make the bgneal@183: // plain text take the same style as the existing paragraph.) bgneal@183: if ((pos = h.indexOf("

")) != -1) { bgneal@183: rpos = h.lastIndexOf("

"); bgneal@183: node = sel.getNode(); bgneal@183: breakElms = []; // Get list of elements to break bgneal@183: bgneal@183: do { bgneal@183: if (node.nodeType == 1) { bgneal@183: // Don't break tables and break at body bgneal@183: if (node.nodeName == "TD" || node.nodeName == "BODY") { bgneal@183: break; bgneal@183: } bgneal@183: bgneal@183: breakElms[breakElms.length] = node; bgneal@183: } bgneal@183: } while (node = node.parentNode); bgneal@183: bgneal@183: // Are we in the middle of a block node? bgneal@183: if (breakElms.length > 0) { bgneal@183: before = h.substring(0, pos); bgneal@183: after = ""; bgneal@183: bgneal@183: for (i=0, len=breakElms.length; i"; bgneal@183: after += "<" + breakElms[breakElms.length-i-1].nodeName.toLowerCase() + ">"; bgneal@183: } bgneal@183: bgneal@183: if (pos == rpos) { bgneal@183: h = before + after + h.substring(pos+7); bgneal@183: } bgneal@183: else { bgneal@183: h = before + h.substring(pos+4, rpos+4) + after + h.substring(rpos+7); bgneal@183: } bgneal@183: } bgneal@183: } bgneal@183: bgneal@183: // Insert content at the caret, plus add a marker for repositioning the caret bgneal@183: ed.execCommand("mceInsertRawHTML", false, h + ' '); bgneal@183: bgneal@183: // Reposition the caret to the marker, which was placed immediately after the inserted content. bgneal@183: // Needs to be done asynchronously (in window.setTimeout) or else it doesn't work in all browsers. bgneal@183: // The second part of the code scrolls the content up if the caret is positioned off-screen. bgneal@183: // This is only necessary for WebKit browsers, but it doesn't hurt to use for all. bgneal@183: window.setTimeout(function() { bgneal@183: var marker = dom.get('_plain_text_marker'), bgneal@183: elm, vp, y, elmHeight; bgneal@183: bgneal@183: sel.select(marker, false); bgneal@183: d.execCommand("Delete", false, null); bgneal@183: marker = null; bgneal@183: bgneal@183: // Get element, position and height bgneal@183: elm = sel.getStart(); bgneal@183: vp = dom.getViewPort(w); bgneal@183: y = dom.getPos(elm).y; bgneal@183: elmHeight = elm.clientHeight; bgneal@183: bgneal@183: // Is element within viewport if not then scroll it into view bgneal@183: if ((y < vp.y) || (y + elmHeight > vp.y + vp.h)) { bgneal@183: d.body.scrollTop = y < vp.y ? y : y - vp.h + 25; bgneal@183: } bgneal@183: }, 0); bgneal@183: } bgneal@183: }, bgneal@183: bgneal@183: /** bgneal@183: * This method will open the old style paste dialogs. Some users might want the old behavior but still use the new cleanup engine. bgneal@183: */ bgneal@183: _legacySupport : function() { bgneal@183: var t = this, ed = t.editor; bgneal@183: bgneal@183: // Register command(s) for backwards compatibility bgneal@183: ed.addCommand("mcePasteWord", function() { bgneal@183: ed.windowManager.open({ bgneal@183: file: t.url + "/pasteword.htm", bgneal@183: width: parseInt(getParam(ed, "paste_dialog_width")), bgneal@183: height: parseInt(getParam(ed, "paste_dialog_height")), bgneal@183: inline: 1 bgneal@183: }); bgneal@183: }); bgneal@183: bgneal@183: if (getParam(ed, "paste_text_use_dialog")) { bgneal@183: ed.addCommand("mcePasteText", function() { bgneal@183: ed.windowManager.open({ bgneal@183: file : t.url + "/pastetext.htm", bgneal@183: width: parseInt(getParam(ed, "paste_dialog_width")), bgneal@183: height: parseInt(getParam(ed, "paste_dialog_height")), bgneal@183: inline : 1 bgneal@183: }); bgneal@183: }); bgneal@45: } bgneal@45: bgneal@183: // Register button for backwards compatibility bgneal@183: ed.addButton("pasteword", {title : "paste.paste_word_desc", cmd : "mcePasteWord"}); bgneal@45: } bgneal@45: }); bgneal@45: bgneal@45: // Register plugin bgneal@183: tinymce.PluginManager.add("paste", tinymce.plugins.PastePlugin); bgneal@183: })();