includes/clientside/tinymce/plugins/paste/editor_plugin_src.js
changeset 1193 e3b94bd055dc
parent 778 57ce13805b6f
--- a/includes/clientside/tinymce/plugins/paste/editor_plugin_src.js	Mon Dec 21 15:41:05 2009 -0500
+++ b/includes/clientside/tinymce/plugins/paste/editor_plugin_src.js	Tue Dec 22 13:09:59 2009 -0500
@@ -1,81 +1,179 @@
 /**
- * $Id: editor_plugin_src.js 919 2008-09-08 20:31:23Z spocke $
+ * $Id: editor_plugin_src.js 1225 2009-09-07 19:06:19Z spocke $
  *
  * @author Moxiecode
  * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
  */
 
 (function() {
-	var Event = tinymce.dom.Event;
+	var each = tinymce.each;
 
 	tinymce.create('tinymce.plugins.PastePlugin', {
 		init : function(ed, url) {
-			var t = this;
+			var t = this, cb;
+
+			t.editor = ed;
+			t.url = url;
+
+			// Setup plugin events
+			t.onPreProcess = new tinymce.util.Dispatcher(t);
+			t.onPostProcess = new tinymce.util.Dispatcher(t);
 
-			t.editor = ed; 
+			// Register default handlers
+			t.onPreProcess.add(t._preProcess);
+			t.onPostProcess.add(t._postProcess);
+
+			// Register optional preprocess handler
+			t.onPreProcess.add(function(pl, o) {
+				ed.execCallback('paste_preprocess', pl, o);
+			});
+
+			// Register optional postprocess
+			t.onPostProcess.add(function(pl, o) {
+				ed.execCallback('paste_postprocess', pl, o);
+			});
 
-			// Register commands
-			ed.addCommand('mcePasteText', function(ui, v) {
-				if (ui) {
-					if ((ed.getParam('paste_use_dialog', true)) || (!tinymce.isIE)) {
-						ed.windowManager.open({
-							file : url + '/pastetext.htm',
-							width : 450,
-							height : 400,
-							inline : 1
-						}, {
-							plugin_url : url
-						});
-					} else
-						t._insertText(clipboardData.getData("Text"), true);
-				} else
-					t._insertText(v.html, v.linebreaks);
+			// This function executes the process handlers and inserts the contents
+			function process(o) {
+				var dom = ed.dom;
+
+				// Execute pre process handlers
+				t.onPreProcess.dispatch(t, o);
+
+				// Create DOM structure
+				o.node = dom.create('div', 0, o.content);
+
+				// Execute post process handlers
+				t.onPostProcess.dispatch(t, o);
+
+				// Serialize content
+				o.content = ed.serializer.serialize(o.node, {getInner : 1});
+
+				//  Insert cleaned content. We need to handle insertion of contents containing block elements separately
+				if (/<(p|h[1-6]|ul|ol)/.test(o.content))
+					t._insertBlockContent(ed, dom, o.content);
+				else
+					t._insert(o.content);
+			};
+
+			// Add command for external usage
+			ed.addCommand('mceInsertClipboardContent', function(u, o) {
+				process(o);
 			});
 
-			ed.addCommand('mcePasteWord', function(ui, v) {
-				if (ui) {
-					if ((ed.getParam('paste_use_dialog', true)) || (!tinymce.isIE)) {
-						ed.windowManager.open({
-							file : url + '/pasteword.htm',
-							width : 450,
-							height : 400,
-							inline : 1
-						}, {
-							plugin_url : url
+			// This function grabs the contents from the clipboard by adding a
+			// hidden div and placing the caret inside it and after the browser paste
+			// is done it grabs that contents and processes that
+			function grabContent(e) {
+				var n, or, rng, sel = ed.selection, dom = ed.dom, body = ed.getBody(), posY;
+
+				if (dom.get('_mcePaste'))
+					return;
+
+				// Create container to paste into
+				n = dom.add(body, 'div', {id : '_mcePaste'}, '\uFEFF');
+
+				// If contentEditable mode we need to find out the position of the closest element
+				if (body != ed.getDoc().body)
+					posY = dom.getPos(ed.selection.getStart(), body).y;
+				else
+					posY = body.scrollTop;
+
+				// Styles needs to be applied after the element is added to the document since WebKit will otherwise remove all styles
+				dom.setStyles(n, {
+					position : 'absolute',
+					left : -10000,
+					top : posY,
+					width : 1,
+					height : 1,
+					overflow : 'hidden'
+				});
+
+				if (tinymce.isIE) {
+					// Select the container
+					rng = dom.doc.body.createTextRange();
+					rng.moveToElementText(n);
+					rng.execCommand('Paste');
+
+					// Remove container
+					dom.remove(n);
+
+					// Check if the contents was changed, if it wasn't then clipboard extraction failed probably due
+					// to IE security settings so we pass the junk though better than nothing right
+					if (n.innerHTML === '\uFEFF') {
+						ed.execCommand('mcePasteWord');
+						e.preventDefault();
+						return;
+					}
+
+					// Process contents
+					process({content : n.innerHTML});
+
+					// Block the real paste event
+					return tinymce.dom.Event.cancel(e);
+				} else {
+					or = ed.selection.getRng();
+
+					// Move caret into hidden div
+					n = n.firstChild;
+					rng = ed.getDoc().createRange();
+					rng.setStart(n, 0);
+					rng.setEnd(n, 1);
+					sel.setRng(rng);
+
+					// Wait a while and grab the pasted contents
+					window.setTimeout(function() {
+						var h = '', nl = dom.select('div[id=_mcePaste]');
+
+						// WebKit will split the div into multiple ones so this will loop through then all and join them to get the whole HTML string
+						each(nl, function(n) {
+							h += (dom.select('> span.Apple-style-span div', n)[0] || dom.select('> span.Apple-style-span', n)[0] || n).innerHTML;
 						});
-					} else
-						t._insertText(t._clipboardHTML());
-				} else
-					t._insertWordContent(v);
-			});
+
+						// Remove the nodes
+						each(nl, function(n) {
+							dom.remove(n);
+						});
+
+						// Restore the old selection
+						if (or)
+							sel.setRng(or);
+
+						process({content : h});
+					}, 0);
+				}
+			};
 
-			ed.addCommand('mceSelectAll', function() {
-				ed.execCommand('selectall'); 
-			});
+			// Check if we should use the new auto process method			
+			if (ed.getParam('paste_auto_cleanup_on_paste', true)) {
+				// Is it's Opera or older FF use key handler
+				if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) {
+					ed.onKeyDown.add(function(ed, e) {
+						if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))
+							grabContent(e);
+					});
+				} else {
+					// Grab contents on paste event on Gecko and WebKit
+					ed.onPaste.addToTop(function(ed, e) {
+						return grabContent(e);
+					});
+				}
+			}
 
-			// Register buttons
-			ed.addButton('pastetext', {title : 'paste.paste_text_desc', cmd : 'mcePasteText', ui : true});
-			ed.addButton('pasteword', {title : 'paste.paste_word_desc', cmd : 'mcePasteWord', ui : true});
-			ed.addButton('selectall', {title : 'paste.selectall_desc', cmd : 'mceSelectAll'});
+			// Block all drag/drop events
+			if (ed.getParam('paste_block_drop')) {
+				ed.onInit.add(function() {
+					ed.dom.bind(ed.getBody(), ['dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag'], function(e) {
+						e.preventDefault();
+						e.stopPropagation();
 
-			if (ed.getParam("paste_auto_cleanup_on_paste", false)) {
-				ed.onPaste.add(function(ed, e) {
-					return t._handlePasteEvent(e)
+						return false;
+					});
 				});
 			}
 
-			if (!tinymce.isIE && ed.getParam("paste_auto_cleanup_on_paste", false)) {
-				// Force paste dialog if non IE browser
-				ed.onKeyDown.add(function(ed, e) {
-					if (e.ctrlKey && e.keyCode == 86) {
-						window.setTimeout(function() {
-							ed.execCommand("mcePasteText", true);
-						}, 1);
-
-						Event.cancel(e);
-					}
-				});
-			}
+			// Add legacy support
+			t._legacySupport();
 		},
 
 		getInfo : function() {
@@ -88,304 +186,343 @@
 			};
 		},
 
-		// Private methods
+		_preProcess : function(pl, o) {
+			var ed = this.editor, h = o.content, process, stripClass;
 
-		_handlePasteEvent : function(e) {
-			var html = this._clipboardHTML(), ed = this.editor, sel = ed.selection, r;
-
-			// Removes italic, strong etc, the if was needed due to bug #1437114
-			if (ed && (r = sel.getRng()) && r.text.length > 0)
-				ed.execCommand('delete');
+			//console.log('Before preprocess:' + o.content);
 
-			if (html && html.length > 0)
-				ed.execCommand('mcePasteWord', false, html);
+			function process(items) {
+				each(items, function(v) {
+					// Remove or replace
+					if (v.constructor == RegExp)
+						h = h.replace(v, '');
+					else
+						h = h.replace(v[0], v[1]);
+				});
+			};
 
-			return Event.cancel(e);
-		},
-
-		_insertText : function(content, bLinebreaks) {
-			content = this.editor.dom.encode(content);
+			// Detect Word content and process it more aggressive
+			if (/(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/.test(h) || o.wordContent) {
+				o.wordContent = true; // Mark the pasted contents as word specific content
+				//console.log('Word contents detected.');
 
-			if (content && content.length > 0) {
-				// Delete any highlighted text before pasting
-				if (!this.editor.selection.isCollapsed())
-					this.editor.execCommand("Delete"); 
+				// Process away some basic content
+				process([
+					/^\s*(&nbsp;)+/g,											// nbsp entities at the start of contents
+					/(&nbsp;|<br[^>]*>)+\s*$/g									// nbsp entities at the end of contents
+				]);
 
-				if (bLinebreaks) { 
-					// Special paragraph treatment 
-					if (this.editor.getParam("paste_create_paragraphs", true)) {
-						var rl = this.editor.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(',');
-						for (var i=0; i<rl.length; i+=2)
-							content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]);
+				if (ed.getParam('paste_convert_middot_lists', true)) {
+					process([
+						[/<!--\[if !supportLists\]-->/gi, '$&__MCE_ITEM__'],			// Convert supportLists to a list item marker
+						[/(<span[^>]+:\s*symbol[^>]+>)/gi, '$1__MCE_ITEM__'],				// Convert symbol spans to list items
+						[/(<span[^>]+mso-list:[^>]+>)/gi, '$1__MCE_ITEM__']				// Convert mso-list to item marker
+					]);
+				}
 
-						content = content.replace(/\r\n\r\n/g, '</p><p>');
-						content = content.replace(/\r\r/g, '</p><p>');
-						content = content.replace(/\n\n/g, '</p><p>');
-
-						// Has paragraphs 
-						if ((pos = content.indexOf('</p><p>')) != -1) { 
-							this.editor.execCommand("Delete"); 
+				process([
+					/<!--[\s\S]+?-->/gi,												// Word comments
+					/<\/?(img|font|meta|link|style|div|v:\w+)[^>]*>/gi,					// Remove some tags including VML content
+					/<\\?\?xml[^>]*>/gi,												// XML namespace declarations
+					/<\/?o:[^>]*>/gi,													// MS namespaced elements <o:tag>
+					/ (id|name|language|type|on\w+|v:\w+)=\"([^\"]*)\"/gi,				// on.., class, style and language attributes with quotes
+					/ (id|name|language|type|on\w+|v:\w+)=(\w+)/gi,						// on.., class, style and language attributes without quotes (IE)
+					[/<(\/?)s>/gi, '<$1strike>'],										// Convert <s> into <strike> for line-though
+					/<script[^>]+>[\s\S]*?<\/script>/gi,								// All scripts elements for msoShowComment for example
+					[/&nbsp;/g, '\u00a0']												// Replace nsbp entites to char since it's easier to handle
+				]);
 
-							var node = this.editor.selection.getNode(); 
-
-							// Get list of elements to break 
-							var breakElms = [];
+				// Remove all spans if no styles is to be retained
+				if (!ed.getParam('paste_retain_style_properties')) {
+					process([
+						/<\/?(span)[^>]*>/gi
+					]);
+				}
+			}
 
-							do { 
-								if (node.nodeType == 1) { 
-									// Don't break tables and break at body 
-									if (node.nodeName == "TD" || node.nodeName == "BODY") 
-										break; 
-			
-									breakElms[breakElms.length] = node; 
-								} 
-							} while(node = node.parentNode); 
+			// Allow for class names to be retained if desired; either all, or just the ones from Word
+			// Note that the paste_strip_class_attributes: 'none, verify_css_classes: true is also a good variation.
+			stripClass = ed.getParam('paste_strip_class_attributes');
+			if (stripClass != 'none') {
+				// Cleans everything but mceItem... classes
+				function cleanClasses(str, cls) {
+					var i, out = '';
 
-							var before = "", after = "</p>"; 
-							before += content.substring(0, pos); 
+					// Remove all classes
+					if (stripClass == 'all')
+						return '';
+
+					cls = tinymce.explode(cls, ' ');
 
-							for (var i=0; i<breakElms.length; i++) { 
-								before += "</" + breakElms[i].nodeName + ">"; 
-								after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">"; 
-							} 
+					for (i = cls.length - 1; i >= 0; i--) {
+						// Remove Mso classes
+						if (!/^(Mso)/i.test(cls[i]))
+							out += (!out ? '' : ' ') + cls[i];
+					}
 
-							before += "<p>"; 
-							content = before + content.substring(pos+7) + after; 
-						} 
-					} 
+					return ' class="' + out + '"';
+				};
 
-					if (this.editor.getParam("paste_create_linebreaks", true)) {
-						content = content.replace(/\r\n/g, '<br />');
-						content = content.replace(/\r/g, '<br />');
-						content = content.replace(/\n/g, '<br />');
-					}
-				} 
+				process([
+					[/ class=\"([^\"]*)\"/gi, cleanClasses],	// class attributes with quotes
+					[/ class=(\w+)/gi, cleanClasses]			// class attributes without quotes (IE)
+				]);
+			}
 
-				this.editor.execCommand("mceInsertRawHTML", false, content); 
+			// Remove spans option
+			if (ed.getParam('paste_remove_spans')) {
+				process([
+					/<\/?(span)[^>]*>/gi
+				]);
 			}
+
+			//console.log('After preprocess:' + h);
+
+			o.content = h;
 		},
 
-		_insertWordContent : function(content) { 
-			var t = this, ed = t.editor;
-
-			if (content && content.length > 0) {
-				// Cleanup Word content
-				var bull = String.fromCharCode(8226);
-				var middot = String.fromCharCode(183);
+		/**
+		 * Various post process items.
+		 */
+		_postProcess : function(pl, o) {
+			var t = this, ed = t.editor, dom = ed.dom, styleProps;
 
-				if (ed.getParam('paste_insert_word_content_callback'))
-					content = ed.execCallback('paste_insert_word_content_callback', 'before', content);
-
-				var rl = ed.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\x93|\x94|\u201c|\u201d,",\x60|\x91|\x92|\u2018|\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(',');
-				for (var i=0; i<rl.length; i+=2)
-					content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]);
-
-				if (this.editor.getParam("paste_convert_headers_to_strong", false)) {
-					content = content.replace(new RegExp('<p class=MsoHeading.*?>(.*?)<\/p>', 'gi'), '<p><b>$1</b></p>');
-				}
+			if (o.wordContent) {
+				// Remove named anchors or TOC links
+				each(dom.select('a', o.node), function(a) {
+					if (!a.href || a.href.indexOf('#_Toc') != -1)
+						dom.remove(a, 1);
+				});
 
-				content = content.replace(new RegExp('tab-stops: list [0-9]+.0pt">', 'gi'), '">' + "--list--");
-				content = content.replace(new RegExp(bull + "(.*?)<BR>", "gi"), "<p>" + middot + "$1</p>");
-				content = content.replace(new RegExp('<SPAN style="mso-list: Ignore">', 'gi'), "<span>" + bull); // Covert to bull list
-				content = content.replace(/<o:p><\/o:p>/gi, "");
-				content = content.replace(new RegExp('<br style="page-break-before: always;.*>', 'gi'), '-- page break --'); // Replace pagebreaks
-				content = content.replace(/<!--([\s\S]*?)-->|<style>[\s\S]*?<\/style>/g, "");  // Word comments
-				content = content.replace(/<(meta|link)[^>]+>/g, ""); // Header elements
+				if (t.editor.getParam('paste_convert_middot_lists', true))
+					t._convertLists(pl, o);
 
-				if (this.editor.getParam("paste_remove_spans", true))
-					content = content.replace(/<\/?span[^>]*>/gi, "");
+				// Process styles
+				styleProps = ed.getParam('paste_retain_style_properties'); // retained properties
 
-				if (this.editor.getParam("paste_remove_styles", true))
-					content = content.replace(new RegExp('<(\\w[^>]*) style="([^"]*)"([^>]*)', 'gi'), "<$1$3");
-
-				content = content.replace(/<\/?font[^>]*>/gi, "");
+				// If string property then split it
+				if (tinymce.is(styleProps, 'string'))
+					styleProps = tinymce.explode(styleProps);
 
-				// Strips class attributes.
-				switch (this.editor.getParam("paste_strip_class_attributes", "all")) {
-					case "all":
-						content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
-						break;
-
-					case "mso":
-						content = content.replace(new RegExp('<(\\w[^>]*) class="?mso([^ |>]*)([^>]*)', 'gi'), "<$1$3");
-						break;
-				}
+				// Retains some style properties
+				each(dom.select('*', o.node), function(el) {
+					var newStyle = {}, npc = 0, i, sp, sv;
 
-				content = content.replace(new RegExp('href="?' + this._reEscape("" + document.location) + '', 'gi'), 'href="' + this.editor.documentBaseURI.getURI());
-				content = content.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");
-				content = content.replace(/<\\?\?xml[^>]*>/gi, "");
-				content = content.replace(/<\/?\w+:[^>]*>/gi, "");
-				content = content.replace(/-- page break --\s*<p>&nbsp;<\/p>/gi, ""); // Remove pagebreaks
-				content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks
-
-		//		content = content.replace(/\/?&nbsp;*/gi, ""); &nbsp;
-		//		content = content.replace(/<p>&nbsp;<\/p>/gi, '');
+					// Store a subset of the existing styles
+					if (styleProps) {
+						for (i = 0; i < styleProps.length; i++) {
+							sp = styleProps[i];
+							sv = dom.getStyle(el, sp);
 
-				if (!this.editor.getParam('force_p_newlines')) {
-					content = content.replace('', '' ,'gi');
-					content = content.replace('</p>', '<br /><br />' ,'gi');
-				}
-
-				if (!tinymce.isIE && !this.editor.getParam('force_p_newlines')) {
-					content = content.replace(/<\/?p[^>]*>/gi, "");
-				}
+							if (sv) {
+								newStyle[sp] = sv;
+								npc++;
+							}
+						}
+					}
 
-				content = content.replace(/<\/?div[^>]*>/gi, "");
-
-				// Convert all middlot lists to UL lists
-				if (this.editor.getParam("paste_convert_middot_lists", true)) {
-					var div = ed.dom.create("div", null, content);
-
-					// Convert all middot paragraphs to li elements
-					var className = this.editor.getParam("paste_unindented_list_class", "unIndentedList");
+					// Remove all of the existing styles
+					dom.setAttrib(el, 'style', '');
 
-					while (this._convertMiddots(div, "--list--")) ; // bull
-					while (this._convertMiddots(div, middot, className)) ; // Middot
-					while (this._convertMiddots(div, bull)) ; // bull
-
-					content = div.innerHTML;
-				}
+					if (styleProps && npc > 0)
+						dom.setStyles(el, newStyle); // Add back the stored subset of styles
+					else // Remove empty span tags that do not have class attributes
+						if (el.nodeName == 'SPAN' && !el.className)
+							dom.remove(el, true);
+				});
+			}
 
-				// Replace all headers with strong and fix some other issues
-				if (this.editor.getParam("paste_convert_headers_to_strong", false)) {
-					content = content.replace(/<h[1-6]>&nbsp;<\/h[1-6]>/gi, '<p>&nbsp;&nbsp;</p>');
-					content = content.replace(/<h[1-6]>/gi, '<p><b>');
-					content = content.replace(/<\/h[1-6]>/gi, '</b></p>');
-					content = content.replace(/<b>&nbsp;<\/b>/gi, '<b>&nbsp;&nbsp;</b>');
-					content = content.replace(/^(&nbsp;)*/gi, '');
-				}
-
-				content = content.replace(/--list--/gi, ""); // Remove --list--
-
-				if (ed.getParam('paste_insert_word_content_callback'))
-					content = ed.execCallback('paste_insert_word_content_callback', 'after', content);
-
-				// Insert cleaned content
-				this.editor.execCommand("mceInsertContent", false, content);
-
-				if (this.editor.getParam('paste_force_cleanup_wordpaste', true)) {
-					var ed = this.editor;
-
-					window.setTimeout(function() {
-						ed.execCommand("mceCleanup");
-					}, 1); // Do normal cleanup detached from this thread
+			// Remove all style information or only specifically on WebKit to avoid the style bug on that browser
+			if (ed.getParam("paste_remove_styles") || (ed.getParam("paste_remove_styles_if_webkit") && tinymce.isWebKit)) {
+				each(dom.select('*[style]', o.node), function(el) {
+					el.removeAttribute('style');
+					el.removeAttribute('mce_style');
+				});
+			} else {
+				if (tinymce.isWebKit) {
+					// We need to compress the styles on WebKit since if you paste <img border="0" /> it will become <img border="0" style="... lots of junk ..." />
+					// Removing the mce_style that contains the real value will force the Serializer engine to compress the styles
+					each(dom.select('*', o.node), function(el) {
+						el.removeAttribute('mce_style');
+					});
 				}
 			}
 		},
 
-		_reEscape : function(s) {
-			var l = "?.\\*[](){}+^$:";
-			var o = "";
+		/**
+		 * Converts the most common bullet and number formats in Office into a real semantic UL/LI list.
+		 */
+		_convertLists : function(pl, o) {
+			var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html;
+
+			// Convert middot lists into real semantic lists
+			each(dom.select('p', o.node), function(p) {
+				var sib, val = '', type, html, idx, parents;
+
+				// Get text node value at beginning of paragraph
+				for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling)
+					val += sib.nodeValue;
 
-			for (var i=0; i<s.length; i++) {
-				var c = s.charAt(i);
+				val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/&nbsp;/g, '\u00a0');
+
+				// Detect unordered lists look for bullets
+				if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o]\s*\u00a0*/.test(val))
+					type = 'ul';
+
+				// Detect ordered lists 1., a. or ixv.
+				if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0{2,}/.test(val))
+					type = 'ol';
+
+				// Check if node value matches the list pattern: o&nbsp;&nbsp;
+				if (type) {
+					margin = parseFloat(p.style.marginLeft || 0);
+
+					if (margin > lastMargin)
+						levels.push(margin);
 
-				if (l.indexOf(c) != -1)
-					o += '\\' + c;
-				else
-					o += c;
-			}
+					if (!listElm || type != lastType) {
+						listElm = dom.create(type);
+						dom.insertAfter(listElm, p);
+					} else {
+						// Nested list element
+						if (margin > lastMargin) {
+							listElm = li.appendChild(dom.create(type));
+						} else if (margin < lastMargin) {
+							// Find parent level based on margin value
+							idx = tinymce.inArray(levels, margin);
+							parents = dom.getParents(listElm.parentNode, type);
+							listElm = parents[parents.length - 1 - idx] || listElm;
+						}
+					}
+
+					// Remove middot or number spans if they exists
+					each(dom.select('span', p), function(span) {
+						var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, '');
 
-			return o;
+						// Remove span with the middot or the number
+						if (type == 'ul' && /^[\u2022\u00b7\u00a7\u00d8o]/.test(html))
+							dom.remove(span);
+						else if (/^[\s\S]*\w+\.(&nbsp;|\u00a0)*\s*/.test(html))
+							dom.remove(span);
+					});
+
+					html = p.innerHTML;
+
+					// Remove middot/list items
+					if (type == 'ul')
+						html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o]\s*(&nbsp;|\u00a0)+\s*/, '');
+					else
+						html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*\w+\.(&nbsp;|\u00a0)+\s*/, '');
+
+					// Create li and add paragraph data into the new li
+					li = listElm.appendChild(dom.create('li', 0, html));
+					dom.remove(p);
+
+					lastMargin = margin;
+					lastType = type;
+				} else
+					listElm = lastMargin = 0; // End list element
+			});
+
+			// Remove any left over makers
+			html = o.node.innerHTML;
+			if (html.indexOf('__MCE_ITEM__') != -1)
+				o.node.innerHTML = html.replace(/__MCE_ITEM__/g, '');
 		},
 
-		_convertMiddots : function(div, search, class_name) {
-			var ed = this.editor, mdot = String.fromCharCode(183), bull = String.fromCharCode(8226);
-			var nodes, prevul, i, p, ul, li, np, cp, li;
+		/**
+		 * This method will split the current block parent and insert the contents inside the split position.
+		 * This logic can be improved so text nodes at the start/end remain in the start/end block elements
+		 */
+		_insertBlockContent : function(ed, dom, content) {
+			var parentBlock, marker, sel = ed.selection, last, elm, vp, y, elmHeight;
 
-			nodes = div.getElementsByTagName("p");
-			for (i=0; i<nodes.length; i++) {
-				p = nodes[i];
-
-				// Is middot
-				if (p.innerHTML.indexOf(search) == 0) {
-					ul = ed.dom.create("ul");
-
-					if (class_name)
-						ul.className = class_name;
+			function select(n) {
+				var r;
 
-					// Add the first one
-					li = ed.dom.create("li");
-					li.innerHTML = p.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');
-					ul.appendChild(li);
-
-					// Add the rest
-					np = p.nextSibling;
-					while (np) {
-						// If the node is whitespace, then
-						// ignore it and continue on.
-						if (np.nodeType == 3 && new RegExp('^\\s$', 'm').test(np.nodeValue)) {
-								np = np.nextSibling;
-								continue;
-						}
+				if (tinymce.isIE) {
+					r = ed.getDoc().body.createTextRange();
+					r.moveToElementText(n);
+					r.collapse(false);
+					r.select();
+				} else {
+					sel.select(n, 1);
+					sel.collapse(false);
+				}
+			};
 
-						if (search == mdot) {
-								if (np.nodeType == 1 && new RegExp('^o(\\s+|&nbsp;)').test(np.innerHTML)) {
-										// Second level of nesting
-										if (!prevul) {
-												prevul = ul;
-												ul = ed.dom.create("ul");
-												prevul.appendChild(ul);
-										}
-										np.innerHTML = np.innerHTML.replace(/^o/, '');
-								} else {
-										// Pop the stack if we're going back up to the first level
-										if (prevul) {
-												ul = prevul;
-												prevul = null;
-										}
-										// Not element or middot paragraph
-										if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)
-												break;
-								}
-						} else {
-								// Not element or middot paragraph
-								if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0)
-										break;
-							}
+			// Insert a marker for the caret position
+			this._insert('<span id="_marker">&nbsp;</span>', 1);
+			marker = dom.get('_marker');
+			parentBlock = dom.getParent(marker, 'p,h1,h2,h3,h4,h5,h6,ul,ol,th,td');
+
+			// If it's a parent block but not a table cell
+			if (parentBlock && !/TD|TH/.test(parentBlock.nodeName)) {
+				// Split parent block
+				marker = dom.split(parentBlock, marker);
 
-						cp = np.nextSibling;
-						li = ed.dom.create("li");
-						li.innerHTML = np.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');
-						np.parentNode.removeChild(np);
-						ul.appendChild(li);
-						np = cp;
-					}
+				// Insert nodes before the marker
+				each(dom.create('div', 0, content).childNodes, function(n) {
+					last = marker.parentNode.insertBefore(n.cloneNode(true), marker);
+				});
 
-					p.parentNode.replaceChild(ul, p);
-
-					return true;
-				}
+				// Move caret after marker
+				select(last);
+			} else {
+				dom.setOuterHTML(marker, content);
+				sel.select(ed.getBody(), 1);
+				sel.collapse(0);
 			}
 
-			return false;
+			dom.remove('_marker'); // Remove marker if it's left
+
+			// Get element, position and height
+			elm = sel.getStart();
+			vp = dom.getViewPort(ed.getWin());
+			y = ed.dom.getPos(elm).y;
+			elmHeight = elm.clientHeight;
+
+			// Is element within viewport if not then scroll it into view
+			if (y < vp.y || y + elmHeight > vp.y + vp.h)
+				ed.getDoc().body.scrollTop = y < vp.y ? y : y - vp.h + 25;
 		},
 
-		_clipboardHTML : function() {
-			var div = document.getElementById('_TinyMCE_clipboardHTML');
+		/**
+		 * Inserts the specified contents at the caret position.
+		 */
+		_insert : function(h, skip_undo) {
+			var ed = this.editor;
 
-			if (!div) {
-				var div = document.createElement('DIV');
-				div.id = '_TinyMCE_clipboardHTML';
+			// First delete the contents seems to work better on WebKit
+			if (!ed.selection.isCollapsed())
+				ed.getDoc().execCommand('Delete', false, null);
+
+			// It's better to use the insertHTML method on Gecko since it will combine paragraphs correctly before inserting the contents
+			ed.execCommand(tinymce.isGecko ? 'insertHTML' : 'mceInsertContent', false, h, {skip_undo : skip_undo});
+		},
 
-				with (div.style) {
-					visibility = 'hidden';
-					overflow = 'hidden';
-					position = 'absolute';
-					width = 1;
-					height = 1;
-				}
+		/**
+		 * This method will open the old style paste dialogs. Some users might want the old behavior but still use the new cleanup engine.
+		 */
+		_legacySupport : function() {
+			var t = this, ed = t.editor;
 
-				document.body.appendChild(div);
-			}
+			// Register commands for backwards compatibility
+			each(['mcePasteText', 'mcePasteWord'], function(cmd) {
+				ed.addCommand(cmd, function() {
+					ed.windowManager.open({
+						file : t.url + (cmd == 'mcePasteText' ? '/pastetext.htm' : '/pasteword.htm'),
+						width : parseInt(ed.getParam("paste_dialog_width", "450")),
+						height : parseInt(ed.getParam("paste_dialog_height", "400")),
+						inline : 1
+					});
+				});
+			});
 
-			div.innerHTML = '';
-			var rng = document.body.createTextRange();
-			rng.moveToElementText(div);
-			rng.execCommand('Paste');
-			var html = div.innerHTML;
-			div.innerHTML = '';
-			return html;
+			// Register buttons for backwards compatibility
+			ed.addButton('pastetext', {title : 'paste.paste_text_desc', cmd : 'mcePasteText'});
+			ed.addButton('pasteword', {title : 'paste.paste_word_desc', cmd : 'mcePasteWord'});
+			ed.addButton('selectall', {title : 'paste.selectall_desc', cmd : 'selectall'});
 		}
 	});