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 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@312: el.removeAttribute('_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@312: if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o]\s*\u00a0*/.test(val)) bgneal@312: type = 'ul'; bgneal@312: bgneal@312: // Detect ordered lists 1., a. or ixv. bgneal@312: if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0{2,}/.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@312: if (type == 'ul' && /^[\u2022\u00b7\u00a7\u00d8o]/.test(html)) bgneal@312: dom.remove(span); bgneal@312: else if (/^[\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@312: html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o]\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: * This method will split the current block parent and insert the contents inside the split position. bgneal@312: * This logic can be improved so text nodes at the start/end remain in the start/end block elements bgneal@312: */ bgneal@312: _insertBlockContent : function(ed, dom, content) { bgneal@312: var parentBlock, marker, sel = ed.selection, last, elm, vp, y, elmHeight, markerId = 'mce_marker'; bgneal@312: bgneal@312: function select(n) { bgneal@312: var r; bgneal@312: bgneal@312: if (tinymce.isIE) { bgneal@312: r = ed.getDoc().body.createTextRange(); bgneal@312: r.moveToElementText(n); bgneal@312: r.collapse(false); bgneal@312: r.select(); bgneal@312: } else { bgneal@312: sel.select(n, 1); bgneal@312: sel.collapse(false); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: // Insert a marker for the caret position bgneal@312: this._insert('', 1); bgneal@312: marker = dom.get(markerId); bgneal@312: parentBlock = dom.getParent(marker, 'p,h1,h2,h3,h4,h5,h6,ul,ol,th,td'); bgneal@312: bgneal@312: // If it's a parent block but not a table cell bgneal@312: if (parentBlock && !/TD|TH/.test(parentBlock.nodeName)) { bgneal@312: // Split parent block bgneal@312: marker = dom.split(parentBlock, marker); bgneal@312: bgneal@312: // Insert nodes before the marker bgneal@312: each(dom.create('div', 0, content).childNodes, function(n) { bgneal@312: last = marker.parentNode.insertBefore(n.cloneNode(true), marker); bgneal@312: }); bgneal@312: bgneal@312: // Move caret after marker bgneal@312: select(last); bgneal@312: } else { bgneal@312: dom.setOuterHTML(marker, content); bgneal@312: sel.select(ed.getBody(), 1); bgneal@312: sel.collapse(0); bgneal@312: } bgneal@312: bgneal@312: // Remove marker if it's left bgneal@312: while (elm = dom.get(markerId)) bgneal@312: dom.remove(elm); bgneal@312: bgneal@312: // Get element, position and height bgneal@312: elm = sel.getStart(); bgneal@312: vp = dom.getViewPort(ed.getWin()); bgneal@312: y = ed.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: ed.getDoc().body.scrollTop = y < vp.y ? y : y - vp.h + 25; 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@312: // It's better to use the insertHTML method on Gecko since it will combine paragraphs correctly before inserting the contents bgneal@312: ed.execCommand(tinymce.isGecko ? 'insertHTML' : '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 (!entities) bgneal@312: entities = ("34,quot,38,amp,39,apos,60,lt,62,gt," + ed.serializer.settings.entities).split(","); bgneal@312: 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: [/"],
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