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: entities = null,
bgneal@312: defs = {
bgneal@312: paste_auto_cleanup_on_paste : 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@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@312: // Initialize plain text flag
bgneal@312: ed.pasteAsPlainText = false;
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@312: var dom = ed.dom;
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@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 if (/<(p|h[1-6]|ul|ol)/.test(o.content)) {
bgneal@312: // Handle insertion of contents containing block elements separately
bgneal@312: t._insertBlockContent(ed, dom, o.content);
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@312: var n, or, rng, sel = ed.selection, dom = ed.dom, body = ed.getBody(), posY;
bgneal@312:
bgneal@312: // Check if browser supports direct plaintext access
bgneal@312: if (ed.pasteAsPlainText && (e.clipboardData || dom.doc.dataTransfer)) {
bgneal@312: e.preventDefault();
bgneal@312: process({content : (e.clipboardData || dom.doc.dataTransfer).getData('Text')}, true);
bgneal@312: return;
bgneal@312: }
bgneal@312:
bgneal@312: if (dom.get('_mcePaste'))
bgneal@312: return;
bgneal@312:
bgneal@312: // Create container to paste into
bgneal@312: n = dom.add(body, 'div', {id : '_mcePaste', 'class' : 'mcePaste'}, '\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@312: posY = body.scrollTop;
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@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@312: if (n.innerHTML === '\uFEFF') {
bgneal@312: ed.execCommand('mcePasteWord');
bgneal@312: e.preventDefault();
bgneal@312: return;
bgneal@312: }
bgneal@312:
bgneal@312: // Process contents
bgneal@312: process({content : n.innerHTML});
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@312: // Move caret into hidden div
bgneal@312: n = n.firstChild;
bgneal@312: rng = ed.getDoc().createRange();
bgneal@312: rng.setStart(n, 0);
bgneal@312: rng.setEnd(n, 1);
bgneal@312: sel.setRng(rng);
bgneal@312:
bgneal@312: // Wait a while and grab the pasted contents
bgneal@312: window.setTimeout(function() {
bgneal@312: var h = '', nl = dom.select('div.mcePaste');
bgneal@312:
bgneal@312: // 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@312: each(nl, function(n) {
bgneal@312: var child = n.firstChild;
bgneal@312:
bgneal@312: // WebKit inserts a DIV container with lots of odd styles
bgneal@312: if (child && child.nodeName == 'DIV' && child.style.marginTop && child.style.backgroundColor) {
bgneal@312: dom.remove(child, 1);
bgneal@312: }
bgneal@312:
bgneal@312: // WebKit duplicates the divs so we need to remove them
bgneal@312: each(dom.select('div.mcePaste', n), function(n) {
bgneal@312: dom.remove(n, 1);
bgneal@312: });
bgneal@312:
bgneal@312: // Remove apply style spans
bgneal@312: each(dom.select('span.Apple-style-span', n), function(n) {
bgneal@312: dom.remove(n, 1);
bgneal@312: });
bgneal@312:
bgneal@312: // Remove bogus br elements
bgneal@312: each(dom.select('br[_mce_bogus]', n), function(n) {
bgneal@312: dom.remove(n);
bgneal@312: });
bgneal@312:
bgneal@312: h += n.innerHTML;
bgneal@312: });
bgneal@312:
bgneal@312: // Remove the nodes
bgneal@312: each(nl, 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@312: ed.onKeyDown.add(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@312: // Block all drag/drop events
bgneal@312: if (getParam(ed, "paste_block_drop")) {
bgneal@312: ed.onInit.add(function() {
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@312: });
bgneal@312: }
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: //console.log('Before preprocess:' + o.content);
bgneal@312:
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@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@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@312: [/(]+(?:mso-list:|:\s*symbol)[^>]+>)/gi, '$1__MCE_ITEM__'] // Convert mso-list and symbol spans to item markers 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"], bgneal@312: [/<\/h[1-6][^>]*>/gi, "
"] bgneal@312: ]); bgneal@312: } bgneal@312: 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@312: 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@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@312: el.removeAttribute('_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"],
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