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@217: ed.windowManager.alert(ed.translate('paste.plaintext_mode_sticky'));
bgneal@183: } else {
bgneal@217: ed.windowManager.alert(ed.translate('paste.plaintext_mode_sticky'));
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@217: // Check if browser supports direct plaintext access
bgneal@217: if (ed.pasteAsPlainText && (e.clipboardData || dom.doc.dataTransfer)) {
bgneal@217: e.preventDefault();
bgneal@217: process({content : (e.clipboardData || dom.doc.dataTransfer).getData('Text')}, true);
bgneal@217: return;
bgneal@217: }
bgneal@217:
bgneal@183: if (dom.get('_mcePaste'))
bgneal@183: return;
bgneal@183:
bgneal@183: // Create container to paste into
bgneal@247: 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@247: var child = n.firstChild;
bgneal@247:
bgneal@247: // WebKit inserts a DIV container with lots of odd styles
bgneal@247: if (child && child.nodeName == 'DIV' && child.style.marginTop && child.style.backgroundColor) {
bgneal@247: dom.remove(child, 1);
bgneal@247: }
bgneal@247:
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@217: // Remove apply style spans
bgneal@217: each(dom.select('span.Apple-style-span', n), function(n) {
bgneal@217: dom.remove(n, 1);
bgneal@217: });
bgneal@217:
bgneal@247: // Remove bogus br elements
bgneal@247: each(dom.select('br[_mce_bogus]', n), function(n) {
bgneal@247: dom.remove(n);
bgneal@247: });
bgneal@247:
bgneal@217: h += 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"], 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"],
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