bgneal@312: /** bgneal@312: * editor_plugin_src.js bgneal@312: * bgneal@312: * Copyright 2009, Moxiecode Systems AB bgneal@312: * Released under LGPL License. bgneal@312: * bgneal@312: * License: http://tinymce.moxiecode.com/license bgneal@312: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@312: */ bgneal@312: bgneal@312: (function() { bgneal@312: var each = tinymce.each, bgneal@312: defs = { bgneal@312: paste_auto_cleanup_on_paste : true, bgneal@442: paste_enable_default_filters : true, bgneal@312: paste_block_drop : false, bgneal@312: paste_retain_style_properties : "none", bgneal@312: paste_strip_class_attributes : "mso", bgneal@312: paste_remove_spans : false, bgneal@312: paste_remove_styles : false, bgneal@312: paste_remove_styles_if_webkit : true, bgneal@312: paste_convert_middot_lists : true, bgneal@312: paste_convert_headers_to_strong : false, bgneal@312: paste_dialog_width : "450", bgneal@312: paste_dialog_height : "400", bgneal@312: paste_text_use_dialog : false, bgneal@312: paste_text_sticky : false, bgneal@442: paste_text_sticky_default : false, bgneal@312: paste_text_notifyalways : false, bgneal@312: paste_text_linebreaktype : "p", bgneal@312: paste_text_replacements : [ bgneal@312: [/\u2026/g, "..."], bgneal@312: [/[\x93\x94\u201c\u201d]/g, '"'], bgneal@312: [/[\x60\x91\x92\u2018\u2019]/g, "'"] bgneal@312: ] bgneal@312: }; bgneal@312: bgneal@312: function getParam(ed, name) { bgneal@312: return ed.getParam(name, defs[name]); bgneal@312: } bgneal@312: bgneal@312: tinymce.create('tinymce.plugins.PastePlugin', { bgneal@312: init : function(ed, url) { bgneal@312: var t = this; bgneal@312: bgneal@312: t.editor = ed; bgneal@312: t.url = url; bgneal@312: bgneal@312: // Setup plugin events bgneal@312: t.onPreProcess = new tinymce.util.Dispatcher(t); bgneal@312: t.onPostProcess = new tinymce.util.Dispatcher(t); bgneal@312: bgneal@312: // Register default handlers bgneal@312: t.onPreProcess.add(t._preProcess); bgneal@312: t.onPostProcess.add(t._postProcess); bgneal@312: bgneal@312: // Register optional preprocess handler bgneal@312: t.onPreProcess.add(function(pl, o) { bgneal@312: ed.execCallback('paste_preprocess', pl, o); bgneal@312: }); bgneal@312: bgneal@312: // Register optional postprocess bgneal@312: t.onPostProcess.add(function(pl, o) { bgneal@312: ed.execCallback('paste_postprocess', pl, o); bgneal@312: }); bgneal@312: bgneal@442: ed.onKeyDown.addToTop(function(ed, e) { bgneal@442: // Block ctrl+v from adding an undo level since the default logic in tinymce.Editor will add that bgneal@442: if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45)) bgneal@442: return false; // Stop other listeners bgneal@442: }); bgneal@442: bgneal@312: // Initialize plain text flag bgneal@442: ed.pasteAsPlainText = getParam(ed, 'paste_text_sticky_default'); bgneal@312: bgneal@312: // This function executes the process handlers and inserts the contents bgneal@312: // force_rich overrides plain text mode set by user, important for pasting with execCommand bgneal@312: function process(o, force_rich) { bgneal@442: var dom = ed.dom, rng, nodes; bgneal@312: bgneal@312: // Execute pre process handlers bgneal@312: t.onPreProcess.dispatch(t, o); bgneal@312: bgneal@312: // Create DOM structure bgneal@312: o.node = dom.create('div', 0, o.content); bgneal@312: bgneal@442: // If pasting inside the same element and the contents is only one block bgneal@442: // remove the block and keep the text since Firefox will copy parts of pre and h1-h6 as a pre element bgneal@442: if (tinymce.isGecko) { bgneal@442: rng = ed.selection.getRng(true); bgneal@442: if (rng.startContainer == rng.endContainer && rng.startContainer.nodeType == 3) { bgneal@442: nodes = dom.select('p,h1,h2,h3,h4,h5,h6,pre', o.node); bgneal@442: bgneal@442: // Is only one block node and it doesn't contain word stuff bgneal@442: if (nodes.length == 1 && o.content.indexOf('__MCE_ITEM__') === -1) bgneal@442: dom.remove(nodes.reverse(), true); bgneal@442: } bgneal@442: } bgneal@442: bgneal@312: // Execute post process handlers bgneal@312: t.onPostProcess.dispatch(t, o); bgneal@312: bgneal@312: // Serialize content bgneal@312: o.content = ed.serializer.serialize(o.node, {getInner : 1}); bgneal@312: bgneal@312: // Plain text option active? bgneal@312: if ((!force_rich) && (ed.pasteAsPlainText)) { bgneal@312: t._insertPlainText(ed, dom, o.content); bgneal@312: bgneal@312: if (!getParam(ed, "paste_text_sticky")) { bgneal@312: ed.pasteAsPlainText = false; bgneal@312: ed.controlManager.setActive("pastetext", false); bgneal@312: } bgneal@312: } else { bgneal@312: t._insert(o.content); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: // Add command for external usage bgneal@312: ed.addCommand('mceInsertClipboardContent', function(u, o) { bgneal@312: process(o, true); bgneal@312: }); bgneal@312: bgneal@312: if (!getParam(ed, "paste_text_use_dialog")) { bgneal@312: ed.addCommand('mcePasteText', function(u, v) { bgneal@312: var cookie = tinymce.util.Cookie; bgneal@312: bgneal@312: ed.pasteAsPlainText = !ed.pasteAsPlainText; bgneal@312: ed.controlManager.setActive('pastetext', ed.pasteAsPlainText); bgneal@312: bgneal@312: if ((ed.pasteAsPlainText) && (!cookie.get("tinymcePasteText"))) { bgneal@312: if (getParam(ed, "paste_text_sticky")) { bgneal@312: ed.windowManager.alert(ed.translate('paste.plaintext_mode_sticky')); bgneal@312: } else { bgneal@312: ed.windowManager.alert(ed.translate('paste.plaintext_mode_sticky')); bgneal@312: } bgneal@312: bgneal@312: if (!getParam(ed, "paste_text_notifyalways")) { bgneal@312: cookie.set("tinymcePasteText", "1", new Date(new Date().getFullYear() + 1, 12, 31)) bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: bgneal@312: ed.addButton('pastetext', {title: 'paste.paste_text_desc', cmd: 'mcePasteText'}); bgneal@312: ed.addButton('selectall', {title: 'paste.selectall_desc', cmd: 'selectall'}); bgneal@312: bgneal@312: // This function grabs the contents from the clipboard by adding a bgneal@312: // hidden div and placing the caret inside it and after the browser paste bgneal@312: // is done it grabs that contents and processes that bgneal@312: function grabContent(e) { bgneal@442: var n, or, rng, oldRng, sel = ed.selection, dom = ed.dom, body = ed.getBody(), posY, textContent; bgneal@312: bgneal@312: // Check if browser supports direct plaintext access bgneal@442: if (e.clipboardData || dom.doc.dataTransfer) { bgneal@442: textContent = (e.clipboardData || dom.doc.dataTransfer).getData('Text'); bgneal@442: bgneal@442: if (ed.pasteAsPlainText) { bgneal@442: e.preventDefault(); bgneal@442: process({content : textContent.replace(/\r?\n/g, '
')}); bgneal@442: return; bgneal@442: } bgneal@312: } bgneal@312: bgneal@312: if (dom.get('_mcePaste')) bgneal@312: return; bgneal@312: bgneal@312: // Create container to paste into bgneal@442: n = dom.add(body, 'div', {id : '_mcePaste', 'class' : 'mcePaste', 'data-mce-bogus' : '1'}, '\uFEFF\uFEFF'); bgneal@312: bgneal@312: // If contentEditable mode we need to find out the position of the closest element bgneal@312: if (body != ed.getDoc().body) bgneal@312: posY = dom.getPos(ed.selection.getStart(), body).y; bgneal@312: else bgneal@442: posY = body.scrollTop + dom.getViewPort().y; bgneal@312: bgneal@312: // Styles needs to be applied after the element is added to the document since WebKit will otherwise remove all styles bgneal@312: dom.setStyles(n, { bgneal@312: position : 'absolute', bgneal@312: left : -10000, bgneal@312: top : posY, bgneal@312: width : 1, bgneal@312: height : 1, bgneal@312: overflow : 'hidden' bgneal@312: }); bgneal@312: bgneal@312: if (tinymce.isIE) { bgneal@442: // Store away the old range bgneal@442: oldRng = sel.getRng(); bgneal@442: bgneal@312: // Select the container bgneal@312: rng = dom.doc.body.createTextRange(); bgneal@312: rng.moveToElementText(n); bgneal@312: rng.execCommand('Paste'); bgneal@312: bgneal@312: // Remove container bgneal@312: dom.remove(n); bgneal@312: bgneal@312: // Check if the contents was changed, if it wasn't then clipboard extraction failed probably due bgneal@312: // to IE security settings so we pass the junk though better than nothing right bgneal@442: if (n.innerHTML === '\uFEFF\uFEFF') { bgneal@312: ed.execCommand('mcePasteWord'); bgneal@312: e.preventDefault(); bgneal@312: return; bgneal@312: } bgneal@312: bgneal@442: // Restore the old range and clear the contents before pasting bgneal@442: sel.setRng(oldRng); bgneal@442: sel.setContent(''); bgneal@442: bgneal@442: // For some odd reason we need to detach the the mceInsertContent call from the paste event bgneal@442: // It's like IE has a reference to the parent element that you paste in and the selection gets messed up bgneal@442: // when it tries to restore the selection bgneal@442: setTimeout(function() { bgneal@442: // Process contents bgneal@442: process({content : n.innerHTML}); bgneal@442: }, 0); bgneal@312: bgneal@312: // Block the real paste event bgneal@312: return tinymce.dom.Event.cancel(e); bgneal@312: } else { bgneal@312: function block(e) { bgneal@312: e.preventDefault(); bgneal@312: }; bgneal@312: bgneal@312: // Block mousedown and click to prevent selection change bgneal@312: dom.bind(ed.getDoc(), 'mousedown', block); bgneal@312: dom.bind(ed.getDoc(), 'keydown', block); bgneal@312: bgneal@312: or = ed.selection.getRng(); bgneal@312: bgneal@442: // Move select contents inside DIV bgneal@312: n = n.firstChild; bgneal@312: rng = ed.getDoc().createRange(); bgneal@312: rng.setStart(n, 0); bgneal@442: rng.setEnd(n, 2); bgneal@312: sel.setRng(rng); bgneal@312: bgneal@312: // Wait a while and grab the pasted contents bgneal@312: window.setTimeout(function() { bgneal@442: var h = '', nl; bgneal@312: bgneal@442: // Paste divs duplicated in paste divs seems to happen when you paste plain text so lets first look for that broken behavior in WebKit bgneal@442: if (!dom.select('div.mcePaste > div.mcePaste').length) { bgneal@442: nl = dom.select('div.mcePaste'); bgneal@312: bgneal@442: // 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@442: each(nl, function(n) { bgneal@442: var child = n.firstChild; bgneal@312: bgneal@442: // WebKit inserts a DIV container with lots of odd styles bgneal@442: if (child && child.nodeName == 'DIV' && child.style.marginTop && child.style.backgroundColor) { bgneal@442: dom.remove(child, 1); bgneal@442: } bgneal@442: bgneal@442: // Remove apply style spans bgneal@442: each(dom.select('span.Apple-style-span', n), function(n) { bgneal@442: dom.remove(n, 1); bgneal@442: }); bgneal@442: bgneal@442: // Remove bogus br elements bgneal@442: each(dom.select('br[data-mce-bogus]', n), function(n) { bgneal@442: dom.remove(n); bgneal@442: }); bgneal@442: bgneal@442: // WebKit will make a copy of the DIV for each line of plain text pasted and insert them into the DIV bgneal@442: if (n.parentNode.className != 'mcePaste') bgneal@442: h += n.innerHTML; bgneal@312: }); bgneal@442: } else { bgneal@442: // Found WebKit weirdness so force the content into plain text mode bgneal@442: h = '
' + dom.encode(textContent).replace(/\r?\n/g, '
') + '
'; bgneal@442: } bgneal@312: bgneal@312: // Remove the nodes bgneal@442: each(dom.select('div.mcePaste'), function(n) { bgneal@312: dom.remove(n); bgneal@312: }); bgneal@312: bgneal@312: // Restore the old selection bgneal@312: if (or) bgneal@312: sel.setRng(or); bgneal@312: bgneal@312: process({content : h}); bgneal@312: bgneal@312: // Unblock events ones we got the contents bgneal@312: dom.unbind(ed.getDoc(), 'mousedown', block); bgneal@312: dom.unbind(ed.getDoc(), 'keydown', block); bgneal@312: }, 0); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: // Check if we should use the new auto process method bgneal@312: if (getParam(ed, "paste_auto_cleanup_on_paste")) { bgneal@312: // Is it's Opera or older FF use key handler bgneal@312: if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) { bgneal@442: ed.onKeyDown.addToTop(function(ed, e) { bgneal@312: if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45)) bgneal@312: grabContent(e); bgneal@312: }); bgneal@312: } else { bgneal@312: // Grab contents on paste event on Gecko and WebKit bgneal@312: ed.onPaste.addToTop(function(ed, e) { bgneal@312: return grabContent(e); bgneal@312: }); bgneal@312: } bgneal@312: } bgneal@312: bgneal@442: ed.onInit.add(function() { bgneal@442: ed.controlManager.setActive("pastetext", ed.pasteAsPlainText); bgneal@442: bgneal@442: // Block all drag/drop events bgneal@442: if (getParam(ed, "paste_block_drop")) { bgneal@312: ed.dom.bind(ed.getBody(), ['dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag'], function(e) { bgneal@312: e.preventDefault(); bgneal@312: e.stopPropagation(); bgneal@312: bgneal@312: return false; bgneal@312: }); bgneal@442: } bgneal@442: }); bgneal@312: bgneal@312: // Add legacy support bgneal@312: t._legacySupport(); bgneal@312: }, bgneal@312: bgneal@312: getInfo : function() { bgneal@312: return { bgneal@312: longname : 'Paste text/word', bgneal@312: author : 'Moxiecode Systems AB', bgneal@312: authorurl : 'http://tinymce.moxiecode.com', bgneal@312: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste', bgneal@312: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@312: }; bgneal@312: }, bgneal@312: bgneal@312: _preProcess : function(pl, o) { bgneal@312: var ed = this.editor, bgneal@312: h = o.content, bgneal@312: grep = tinymce.grep, bgneal@312: explode = tinymce.explode, bgneal@312: trim = tinymce.trim, bgneal@312: len, stripClass; bgneal@312: bgneal@442: //console.log('Before preprocess:' + o.content); bgneal@442: bgneal@312: function process(items) { bgneal@312: each(items, function(v) { bgneal@312: // Remove or replace bgneal@312: if (v.constructor == RegExp) bgneal@312: h = h.replace(v, ''); bgneal@312: else bgneal@312: h = h.replace(v[0], v[1]); bgneal@312: }); bgneal@312: } bgneal@442: bgneal@442: if (ed.settings.paste_enable_default_filters == false) { bgneal@442: return; bgneal@442: } bgneal@442: bgneal@442: // IE9 adds BRs before/after block elements when contents is pasted from word or for example another browser bgneal@442: if (tinymce.isIE && document.documentMode >= 9) bgneal@442: process([[/(?:
 [\s\r\n]+|
)*(<\/?(h[1-6r]|p|div|address|pre|form|table|tbody|thead|tfoot|th|tr|td|li|ol|ul|caption|blockquote|center|dl|dt|dd|dir|fieldset)[^>]*>)(?:
 [\s\r\n]+|
)*/g, '$1']]); bgneal@312: bgneal@312: // Detect Word content and process it more aggressive bgneal@312: if (/class="?Mso|style="[^"]*\bmso-|w:WordDocument/i.test(h) || o.wordContent) { bgneal@312: o.wordContent = true; // Mark the pasted contents as word specific content bgneal@312: //console.log('Word contents detected.'); bgneal@312: bgneal@312: // Process away some basic content bgneal@312: process([ bgneal@312: /^\s*( )+/gi, //   entities at the start of contents bgneal@312: /( |]*>)+\s*$/gi //   entities at the end of contents bgneal@312: ]); bgneal@312: bgneal@312: if (getParam(ed, "paste_convert_headers_to_strong")) { bgneal@312: h = h.replace(/

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

$1

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

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

"] bgneal@312: ]); bgneal@312: } bgneal@312: bgneal@442: process([ bgneal@442: // Copy paste from Java like Open Office will produce this junk on FF bgneal@442: [/Version:[\d.]+\nStartHTML:\d+\nEndHTML:\d+\nStartFragment:\d+\nEndFragment:\d+/gi, ''] bgneal@442: ]); bgneal@442: bgneal@312: // Class attribute options are: leave all as-is ("none"), remove all ("all"), or remove only those starting with mso ("mso"). bgneal@312: // Note:- paste_strip_class_attributes: "none", verify_css_classes: true is also a good variation. bgneal@312: stripClass = getParam(ed, "paste_strip_class_attributes"); bgneal@312: bgneal@312: if (stripClass !== "none") { bgneal@312: function removeClasses(match, g1) { bgneal@312: if (stripClass === "all") bgneal@312: return ''; bgneal@312: bgneal@312: var cls = grep(explode(g1.replace(/^(["'])(.*)\1$/, "$2"), " "), bgneal@312: function(v) { bgneal@312: return (/^(?!mso)/i.test(v)); bgneal@312: } bgneal@312: ); bgneal@312: bgneal@312: return cls.length ? ' class="' + cls.join(" ") + '"' : ''; bgneal@312: }; bgneal@312: bgneal@312: h = h.replace(/ class="([^"]+)"/gi, removeClasses); bgneal@442: h = h.replace(/ class=([\-\w]+)/gi, removeClasses); bgneal@312: } bgneal@312: bgneal@312: // Remove spans option bgneal@312: if (getParam(ed, "paste_remove_spans")) { bgneal@312: h = h.replace(/<\/?span[^>]*>/gi, ""); bgneal@312: } bgneal@312: bgneal@312: //console.log('After preprocess:' + h); bgneal@312: bgneal@312: o.content = h; bgneal@312: }, bgneal@312: bgneal@312: /** bgneal@312: * Various post process items. bgneal@312: */ bgneal@312: _postProcess : function(pl, o) { bgneal@312: var t = this, ed = t.editor, dom = ed.dom, styleProps; bgneal@312: bgneal@442: if (ed.settings.paste_enable_default_filters == false) { bgneal@442: return; bgneal@442: } bgneal@442: bgneal@312: if (o.wordContent) { bgneal@312: // Remove named anchors or TOC links bgneal@312: each(dom.select('a', o.node), function(a) { bgneal@312: if (!a.href || a.href.indexOf('#_Toc') != -1) bgneal@312: dom.remove(a, 1); bgneal@312: }); bgneal@312: bgneal@312: if (getParam(ed, "paste_convert_middot_lists")) { bgneal@312: t._convertLists(pl, o); bgneal@312: } bgneal@312: bgneal@312: // Process styles bgneal@312: styleProps = getParam(ed, "paste_retain_style_properties"); // retained properties bgneal@312: bgneal@312: // Process only if a string was specified and not equal to "all" or "*" bgneal@312: if ((tinymce.is(styleProps, "string")) && (styleProps !== "all") && (styleProps !== "*")) { bgneal@312: styleProps = tinymce.explode(styleProps.replace(/^none$/i, "")); bgneal@312: bgneal@312: // Retains some style properties bgneal@312: each(dom.select('*', o.node), function(el) { bgneal@312: var newStyle = {}, npc = 0, i, sp, sv; bgneal@312: bgneal@312: // Store a subset of the existing styles bgneal@312: if (styleProps) { bgneal@312: for (i = 0; i < styleProps.length; i++) { bgneal@312: sp = styleProps[i]; bgneal@312: sv = dom.getStyle(el, sp); bgneal@312: bgneal@312: if (sv) { bgneal@312: newStyle[sp] = sv; bgneal@312: npc++; bgneal@312: } bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: // Remove all of the existing styles bgneal@312: dom.setAttrib(el, 'style', ''); bgneal@312: bgneal@312: if (styleProps && npc > 0) bgneal@312: dom.setStyles(el, newStyle); // Add back the stored subset of styles bgneal@312: else // Remove empty span tags that do not have class attributes bgneal@312: if (el.nodeName == 'SPAN' && !el.className) bgneal@312: dom.remove(el, true); bgneal@312: }); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: // Remove all style information or only specifically on WebKit to avoid the style bug on that browser bgneal@312: if (getParam(ed, "paste_remove_styles") || (getParam(ed, "paste_remove_styles_if_webkit") && tinymce.isWebKit)) { bgneal@312: each(dom.select('*[style]', o.node), function(el) { bgneal@312: el.removeAttribute('style'); bgneal@442: el.removeAttribute('data-mce-style'); bgneal@312: }); bgneal@312: } else { bgneal@312: if (tinymce.isWebKit) { bgneal@312: // We need to compress the styles on WebKit since if you paste it will become bgneal@312: // Removing the mce_style that contains the real value will force the Serializer engine to compress the styles bgneal@312: each(dom.select('*', o.node), function(el) { bgneal@442: el.removeAttribute('data-mce-style'); bgneal@312: }); bgneal@312: } bgneal@312: } bgneal@312: }, bgneal@312: bgneal@312: /** bgneal@312: * Converts the most common bullet and number formats in Office into a real semantic UL/LI list. bgneal@312: */ bgneal@312: _convertLists : function(pl, o) { bgneal@312: var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html; bgneal@312: bgneal@312: // Convert middot lists into real semantic lists bgneal@312: each(dom.select('p', o.node), function(p) { bgneal@312: var sib, val = '', type, html, idx, parents; bgneal@312: bgneal@312: // Get text node value at beginning of paragraph bgneal@312: for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling) bgneal@312: val += sib.nodeValue; bgneal@312: bgneal@312: val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/ /g, '\u00a0'); bgneal@312: bgneal@312: // Detect unordered lists look for bullets bgneal@442: if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*\u00a0*/.test(val)) bgneal@312: type = 'ul'; bgneal@312: bgneal@312: // Detect ordered lists 1., a. or ixv. bgneal@442: if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0+/.test(val)) bgneal@312: type = 'ol'; bgneal@312: bgneal@312: // Check if node value matches the list pattern: o   bgneal@312: if (type) { bgneal@312: margin = parseFloat(p.style.marginLeft || 0); bgneal@312: bgneal@312: if (margin > lastMargin) bgneal@312: levels.push(margin); bgneal@312: bgneal@312: if (!listElm || type != lastType) { bgneal@312: listElm = dom.create(type); bgneal@312: dom.insertAfter(listElm, p); bgneal@312: } else { bgneal@312: // Nested list element bgneal@312: if (margin > lastMargin) { bgneal@312: listElm = li.appendChild(dom.create(type)); bgneal@312: } else if (margin < lastMargin) { bgneal@312: // Find parent level based on margin value bgneal@312: idx = tinymce.inArray(levels, margin); bgneal@312: parents = dom.getParents(listElm.parentNode, type); bgneal@312: listElm = parents[parents.length - 1 - idx] || listElm; bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: // Remove middot or number spans if they exists bgneal@312: each(dom.select('span', p), function(span) { bgneal@312: var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, ''); bgneal@312: bgneal@312: // Remove span with the middot or the number bgneal@442: if (type == 'ul' && /^__MCE_ITEM__[\u2022\u00b7\u00a7\u00d8o\u25CF]/.test(html)) bgneal@312: dom.remove(span); bgneal@442: else if (/^__MCE_ITEM__[\s\S]*\w+\.( |\u00a0)*\s*/.test(html)) bgneal@312: dom.remove(span); bgneal@312: }); bgneal@312: bgneal@312: html = p.innerHTML; bgneal@312: bgneal@312: // Remove middot/list items bgneal@312: if (type == 'ul') bgneal@442: html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o\u25CF]\s*( |\u00a0)+\s*/, ''); bgneal@312: else bgneal@312: html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*\w+\.( |\u00a0)+\s*/, ''); bgneal@312: bgneal@312: // Create li and add paragraph data into the new li bgneal@312: li = listElm.appendChild(dom.create('li', 0, html)); bgneal@312: dom.remove(p); bgneal@312: bgneal@312: lastMargin = margin; bgneal@312: lastType = type; bgneal@312: } else bgneal@312: listElm = lastMargin = 0; // End list element bgneal@312: }); bgneal@312: bgneal@312: // Remove any left over makers bgneal@312: html = o.node.innerHTML; bgneal@312: if (html.indexOf('__MCE_ITEM__') != -1) bgneal@312: o.node.innerHTML = html.replace(/__MCE_ITEM__/g, ''); bgneal@312: }, bgneal@312: bgneal@312: /** bgneal@312: * Inserts the specified contents at the caret position. bgneal@312: */ bgneal@312: _insert : function(h, skip_undo) { bgneal@312: var ed = this.editor, r = ed.selection.getRng(); bgneal@312: bgneal@312: // First delete the contents seems to work better on WebKit when the selection spans multiple list items or multiple table cells. bgneal@312: if (!ed.selection.isCollapsed() && r.startContainer != r.endContainer) bgneal@312: ed.getDoc().execCommand('Delete', false, null); bgneal@312: bgneal@442: ed.execCommand('mceInsertContent', false, h, {skip_undo : skip_undo}); bgneal@312: }, bgneal@312: bgneal@312: /** bgneal@312: * Instead of the old plain text method which tried to re-create a paste operation, the bgneal@312: * new approach adds a plain text mode toggle switch that changes the behavior of paste. bgneal@312: * This function is passed the same input that the regular paste plugin produces. bgneal@312: * It performs additional scrubbing and produces (and inserts) the plain text. bgneal@312: * This approach leverages all of the great existing functionality in the paste bgneal@312: * plugin, and requires minimal changes to add the new functionality. bgneal@312: * Speednet - June 2009 bgneal@312: */ bgneal@312: _insertPlainText : function(ed, dom, h) { bgneal@312: var i, len, pos, rpos, node, breakElms, before, after, bgneal@312: w = ed.getWin(), bgneal@312: d = ed.getDoc(), bgneal@312: sel = ed.selection, bgneal@312: is = tinymce.is, bgneal@312: inArray = tinymce.inArray, bgneal@312: linebr = getParam(ed, "paste_text_linebreaktype"), bgneal@312: rl = getParam(ed, "paste_text_replacements"); bgneal@312: bgneal@312: function process(items) { bgneal@312: each(items, function(v) { bgneal@312: if (v.constructor == RegExp) bgneal@312: h = h.replace(v, ""); bgneal@312: else bgneal@312: h = h.replace(v[0], v[1]); bgneal@312: }); bgneal@312: }; bgneal@312: bgneal@312: if ((typeof(h) === "string") && (h.length > 0)) { bgneal@312: // If HTML content with line-breaking tags, then remove all cr/lf chars because only tags will break a line bgneal@312: if (/<(?:p|br|h[1-6]|ul|ol|dl|table|t[rdh]|div|blockquote|fieldset|pre|address|center)[^>]*>/i.test(h)) { bgneal@312: process([ bgneal@312: /[\n\r]+/g bgneal@312: ]); bgneal@312: } else { bgneal@312: // Otherwise just get rid of carriage returns (only need linefeeds) bgneal@312: process([ bgneal@312: /\r+/g bgneal@312: ]); bgneal@312: } bgneal@312: bgneal@312: process([ bgneal@312: [/<\/(?: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@312: [/]*>|<\/tr>/gi, "\n"], // Single linebreak for
tags and table rows bgneal@312: [/<\/t[dh]>\s*]*>/gi, "\t"], // Table cells get tabs betweem them bgneal@312: /<[a-z!\/?][^>]*>/gi, // Delete all remaining tags bgneal@312: [/ /gi, " "], // Convert non-break spaces to regular spaces (remember, *plain text*) bgneal@312: [/(?:(?!\n)\s)*(\n+)(?:(?!\n)\s)*/gi, "$1"], // Cool little RegExp deletes whitespace around linebreak chars. bgneal@312: [/\n{3,}/g, "\n\n"], // Max. 2 consecutive linebreaks bgneal@312: /^\s+|\s+$/g // Trim the front & back bgneal@312: ]); bgneal@312: bgneal@442: h = dom.decode(tinymce.html.Entities.encodeRaw(h)); bgneal@312: bgneal@312: // Delete any highlighted text before pasting bgneal@312: if (!sel.isCollapsed()) { bgneal@312: d.execCommand("Delete", false, null); bgneal@312: } bgneal@312: bgneal@312: // Perform default or custom replacements bgneal@312: if (is(rl, "array") || (is(rl, "array"))) { bgneal@312: process(rl); bgneal@312: } bgneal@312: else if (is(rl, "string")) { bgneal@312: process(new RegExp(rl, "gi")); bgneal@312: } bgneal@312: bgneal@312: // Treat paragraphs as specified in the config bgneal@312: if (linebr == "none") { bgneal@312: process([ bgneal@312: [/\n+/g, " "] bgneal@312: ]); bgneal@312: } bgneal@312: else if (linebr == "br") { bgneal@312: process([ bgneal@312: [/\n/g, "
"] bgneal@312: ]); bgneal@312: } bgneal@312: else { bgneal@312: process([ bgneal@312: /^\s+|\s+$/g, bgneal@312: [/\n\n/g, "

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

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

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