1 define(['aloha/core', 'aloha/ecma5shims', 'jquery'], function (Aloha, $_, jQuery) {
  2 	"use strict";
  3 
  4 	function hasAttribute(obj, attr) {
  5 		var native_method = obj.hasAttribute;
  6 		if (native_method) {
  7 			return obj.hasAttribute(attr);
  8 		}
  9 		return (typeof obj.attributes[attr] != "undefined");
 10 	}
 11 
 12 	var htmlNamespace = "http://www.w3.org/1999/xhtml";
 13  14 
	var cssStylingFlag = false;
 15 
 16 	// This is bad :(
 17 	var globalRange = null;
 18 
 19 	// Commands are stored in a dictionary where we call their actions and such
 20 	var commands = {};
 21 
 22 	///////////////////////////////////////////////////////////////////////////////
 23 	////////////////////////////// Utility functions //////////////////////////////
 24 	///////////////////////////////////////////////////////////////////////////////
 25 	//@{
 26 
 27 	// Opera 11 puts HTML elements in the null namespace, it seems.
 28 	function isHtmlNamespace(ns) {
 29 		return ns === null || !ns || ns === htmlNamespace;
 30 	}
 31 
 32 	// "An HTML element is an Element whose namespace is the HTML namespace."
 33 	//
 34 	// I allow an extra argument to more easily check whether something is a
 35 	// particular HTML element, like isNamedHtmlElement(node, 'OL').  It accepts arrays
 36 	// too, like isHtmlElementInArray(node, ["OL", "UL"]) to check if it's an ol or ul.
 37 	// TODO This function was prominent during profiling. Remove it
 38 	//      and replace with calls to isAnyHtmlElement, isNamedHtmlElement
 39 	//      and is isMappedHtmlElement.
 40 	function isHtmlElement_obsolete(node, tags) {
 41 		if (typeof tags == "string") {
 42 			tags = [tags];
 43 		}
 44 		if (typeof tags == "object") {
 45 			tags = $_(tags).map(function (tag) {
 46 				return tag.toUpperCase();
 47 			});
 48 		}
 49 		return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI) && (typeof tags == "undefined" || $_(tags).indexOf(node.tagName) != -1);
 50 	}
 51 
 52 	function isAnyHtmlElement(node) {
 53 		return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI);
 54 	}
 55 
 56 	// name should be uppercase
 57 	function isNamedHtmlElement(node, name) {
 58 		return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI)
 59 		// This function is passed in a mix of upper and lower case names
 60 			&& name.toUpperCase() === node.nodeName;
 61 	}
 62 
 63 	// TODO remove when isHtmlElementInArray is removed
 64 	function arrayContainsInsensitive(array, str) {
 65 		var i, len;
 66 		str = str.toUpperCase();
 67 		for (i = 0, len = array.length; i < len; i++) {
 68 			if (array[i].toUpperCase() === str) {
 69 				return true;
 70 			}
 71 		}
 72 		return false;
 73 	}
 74 	// TODO replace calls to this function with calls to isMappedHtmlElement()
 75 	function isHtmlElementInArray(node, array) {
 76 		return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI)
 77 		// This function is passed in a mix of upper and lower case names
 78 			&& arrayContainsInsensitive(array, node.nodeName);
 79 	}
 80 
 81 	// map must have all-uppercase keys
 82 	function isMappedHtmlElement(node, map) {
 83 		return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI) && map[node.nodeName];
 84 	}
 85 
 86 	/**
 87 	 * Method to count the number of styles in the given style
 88 	 */
 89 	function getStyleLength(node) {
 90 		var s;
 91 		var styleLength = 0;
 92 
 93 		if (!node) {
 94 			return 0;
 95 		}
 96 
 97 		if (!node.style) {
 98 			return 0;
 99 		}
100 
101 		// some browsers support .length on styles
102 		if (typeof node.style.length !== 'undefined') {
103 			return node.style.length;
104 		}
105 
106 		/*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid
107 		for (s in node.style) {
108 			if (node.style[s] && node.style[s] !== 0 && node.style[s] !== 'false') {
109 				styleLength++;
110 			}
111 		}
112 		/*jslint forin: false*/
113 
114 		return styleLength;
115 	}
116 
117 	function toArray(obj) {
118 		if (!obj) {
119 			return null;
120 		}
121 		var array = [],
122 			i,
123 		    l = obj.length;
124 		// iterate backwards ensuring that length is an UInt32
125 		i = l >>> 0;
126 		while (i--) {
127 			array[i] = obj[i];
128 		}
129 		return array;
130 	}
131 
132 	function nextNodeDescendants(node) {
133 		while (node && !node.nextSibling) {
134 			node = node.parentNode;
135 		}
136 		if (!node) {
137 			return null;
138 		}
139 		return node.nextSibling;
140 	}
141 
142 	function nextNode(node) {
143 		if (node.hasChildNodes()) {
144 			return node.firstChild;
145 		}
146 		return nextNodeDescendants(node);
147 	}
148 
149 	function previousNode(node) {
150 		if (node.previousSibling) {
151 			node = node.previousSibling;
152 			while (node.hasChildNodes()) {
153 				node = node.lastChild;
154 			}
155 			return node;
156 		}
157 		if (node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE) {
158 			return node.parentNode;
159 		}
160 		return null;
161 	}
162 
163 	/**
164 	 * Returns true if ancestor is an ancestor of descendant, false otherwise.
165 	 */
166 	function isAncestor(ancestor, descendant) {
167 		return ancestor && descendant && Boolean($_.compareDocumentPosition(ancestor, descendant) & $_.Node.DOCUMENT_POSITION_CONTAINED_BY);
168 	}
169 
170 	/**
171 	 * Returns true if ancestor is an ancestor of or equal to descendant, false
172 	 * otherwise.
173 	 */
174 	function isAncestorContainer(ancestor, descendant) {
175 		return (ancestor || descendant) && (ancestor == descendant || isAncestor(ancestor, descendant));
176 	}
177 
178 	/**
179 	 * Returns true if descendant is a descendant of ancestor, false otherwise.
180 	 */
181 	function isDescendant(descendant, ancestor) {
182 		return ancestor && descendant && Boolean($_.compareDocumentPosition(ancestor, descendant) & $_.Node.DOCUMENT_POSITION_CONTAINED_BY);
183 	}
184 
185 	/**
186 	 * Returns true if node1 is before node2 in tree order, false otherwise.
187 	 */
188 189 	function isBefore(node1, node2) {
190 		return Boolean($_.compareDocumentPosition(node1, node2) & $_.Node.DOCUMENT_POSITION_FOLLOWING);
191 	}
192 
193 	/**
194 	 * Returns true if node1 is after node2 in tree order, false otherwise.
195 	 */
196 	function isAfter(node1, node2) {
197 		return Boolean($_.compareDocumentPosition(node1, node2) & $_.Node.DOCUMENT_POSITION_PRECEDING);
198 	}
199 
200 	function getAncestors(node) {
201 		var ancestors = [];
202 		while (node.parentNode) {
203 			ancestors.unshift(node.parentNode);
204 			node = node.parentNode;
205 		}
206 		return ancestors;
207 	}
208 
209 	function getDescendants(node) {
210 		var descendants = [];
211 		var stop = nextNodeDescendants(node);
212 		while (null != (node = nextNode(node)) && node != stop) {
213 			descendants.push(node);
214 		}
215 		return descendants;
216 	}
217 
218 	function convertProperty(property) {
219 		// Special-case for now
220 		var map = {
221 			"fontFamily": "font-family",
222 			"fontSize": "font-size",
223 			"fontStyle": "font-style",
224 			"fontWeight": "font-weight",
225 			"textDecoration": "text-decoration"
226 		};
227 		if (typeof map[property] != "undefined") {
228 			return map[property];
229 		}
230 
231 		return property;
232 	}
233 
234 	// Return the <font size=X> value for the given CSS size, or undefined if there
235 	// is none.
236 	function cssSizeToLegacy(cssVal) {
237 		return {
238 			"xx-small": 1,
239 			"small": 2,
240 			"medium": 3,
241 			"large": 4,
242 			"x-large": 5,
243 			"xx-large": 6,
244 			"xxx-large": 7
245 		}[cssVal];
246 	}
247 
248 	// Return the CSS size given a legacy size.
249 	function legacySizeToCss(legacyVal) {
250 		return {
251 			1: "xx-small",
252 			2: "small",
253 			3: "medium",
254 			4: "large",
255 			5: "x-large",
256 			6: "xx-large",
257 			7: "xxx-large"
258 		}[legacyVal];
259 	}
260 
261 	// "the directionality" from HTML.  I don't bother caring about non-HTML
262 	// elements.
263 	//
264 	// "The directionality of an element is either 'ltr' or 'rtl', and is
265 	// determined as per the first appropriate set of steps from the following
266 	// list:"
267 	function getDirectionality(element) {
268 		// "If the element's dir attribute is in the ltr state
269 		//     The directionality of the element is 'ltr'."
270 		if (element.dir == "ltr") {
271 			return "ltr";
272 		}
273 
274 		// "If the element's dir attribute is in the rtl state
275 		//     The directionality of the element is 'rtl'."
276 		if (element.dir == "rtl") {
277 			return "rtl";
278 		}
279 
280 		// "If the element's dir attribute is in the auto state
281 		// "If the element is a bdi element and the dir attribute is not in a
282 		// defined state (i.e. it is not present or has an invalid value)
283 		//     [lots of complicated stuff]
284 		//
285 		// Skip this, since no browser implements it anyway.
286 
287 		// "If the element is a root element and the dir attribute is not in a
288 		// defined state (i.e. it is not present or has an invalid value)
289 		//     The directionality of the element is 'ltr'."
290 		if (!isAnyHtmlElement(element.parentNode)) {
291 			return "ltr";
292 		}
293 
294 		// "If the element has a parent element and the dir attribute is not in a
295 		// defined state (i.e. it is not present or has an invalid value)
296 		//     The directionality of the element is the same as the element's
297 		//     parent element's directionality."
298 		return getDirectionality(element.parentNode);
299 	}
300 
301 	//@}
302 
303 	///////////////////////////////////////////////////////////////////////////////
304 	///////////////////////////// DOM Range functions /////////////////////////////
305 	///////////////////////////////////////////////////////////////////////////////
306 	//@{
307 
308 	function getNodeIndex(node) {
309 		var ret = 0;
310 		while (node.previousSibling) {
311 			ret++;
312 			node = node.previousSibling;
313 		}
314 		return ret;
315 	}
316 
317 	// "The length of a Node node is the following, depending on node:
318 	//
319 	// ProcessingInstruction
320 	// DocumentType
321 	//   Always 0.
322 	// Text
323 	// Comment
324 	//   node's length.
325 	// Any other node
326 	//   node's childNodes's length."
327 	function getNodeLength(node) {
328 		switch (node.nodeType) {
329 		case $_.Node.PROCESSING_INSTRUCTION_NODE:
330 		case $_.Node.DOCUMENT_TYPE_NODE:
331 			return 0;
332 
333 		case $_.Node.TEXT_NODE:
334 		case $_.Node.COMMENT_NODE:
335 			return node.length;
336 
337 		default:
338 			return node.childNodes.length;
339 		}
340 	}
341 
342 	/**
343 	 * The position of two boundary points relative to one another, as defined by
344 	 * DOM Range.
345 	 */
346 	function getPosition(nodeA, offsetA, nodeB, offsetB) {
347 		// "If node A is the same as node B, return equal if offset A equals offset
348 		// B, before if offset A is less than offset B, and after if offset A is
349 		// greater than offset B."
350 		if (nodeA == nodeB) {
351 			if (offsetA == offsetB) {
352 				return "equal";
353 			}
354 			if (offsetA < offsetB) {
355 				return "before";
356 			}
357 			if (offsetA > offsetB) {
358 				return "after";
359 			}
360 		}
361 
362 		var documentPosition = $_.compareDocumentPosition(nodeB, nodeA);
363 		// "If node A is after node B in tree order, compute the position of (node
364 		// B, offset B) relative to (node A, offset A). If it is before, return
365 		// after. If it is after, return before."
366 		if (documentPosition & $_.Node.DOCUMENT_POSITION_FOLLOWING) {
367 			var pos = getPosition(nodeB, offsetB, nodeA, offsetA);
368 			if (pos == "before") {
369 				return "after";
370 			}
371 			if (pos == "after") {
372 				return "before";
373 			}
374 		}
375 
376 		// "If node A is an ancestor of node B:"
377 		if (documentPosition & $_.Node.DOCUMENT_POSITION_CONTAINS) {
378 			// "Let child equal node B."
379 			var child = nodeB;
380 
381 			// "While child is not a child of node A, set child to its parent."
382 			while (child.parentNode != nodeA) {
383 				child = child.parentNode;
384 			}
385 
386 			// "If the index of child is less than offset A, return after."
387 			if (getNodeIndex(child) < offsetA) {
388 				return "after";
389 			}
390 		}
391 
392 		// "Return before."
393 		return "before";
394 	}
395 
396 	/**
397 	 * Returns the furthest ancestor of a Node as defined by DOM Range.
398 	 */
399 	function getFurthestAncestor(node) {
400 		var root = node;
401 		while (root.parentNode != null) {
402 			root = root.parentNode;
403 		}
404 		return root;
405 	}
406 
407 	/**
408 	 * "contained" as defined by DOM Range: "A Node node is contained in a range
409 	 * range if node's furthest ancestor is the same as range's root, and (node, 0)
410 	 * is after range's start, and (node, length of node) is before range's end."
411 	 */
412 	function isContained(node, range) {
413 		var pos1 = getPosition(node, 0, range.startContainer, range.startOffset);
414 		if (pos1 !== "after") {
415 			return false;
416 		}
417 		var pos2 = getPosition(node, getNodeLength(node), range.endContainer, range.endOffset);
418 		if (pos2 !== "before") {
419 			return false;
420 		}
421 		return getFurthestAncestor(node) == getFurthestAncestor(range.startContainer);
422 	}
423 
424 	/**
425 	 * Return all nodes contained in range that the provided function returns true
426 	 * for, omitting any with an ancestor already being returned.
427 	 */
428 	function getContainedNodes(range, condition) {
429 		if (typeof condition == "undefined") {
430 			condition = function () {
431 				return true;
432 			};
433 		}
434 		var node = range.startContainer;
435 		if (node.hasChildNodes() && range.startOffset < node.childNodes.length) {
436 			// A child is contained
437 			node = node.childNodes[range.startOffset];
438 		} else if (range.startOffset == getNodeLength(node)) {
439 			// No descendant can be contained
440 			node = nextNodeDescendants(node);
441 		} else {
442 			// No children; this node at least can't be contained
443 			node = nextNode(node);
444 		}
445 
446 		var stop = range.endContainer;
447 		if (stop.hasChildNodes() && range.endOffset < stop.childNodes.length) {
448 			// The node after the last contained node is a child
449 			stop = stop.childNodes[range.endOffset];
450 		} else {
451 			// This node and/or some of its children might be contained
452 			stop = nextNodeDescendants(stop);
453 		}
454 
455 		var nodeList = [];
456 		while (isBefore(node, stop)) {
457 			if (isContained(node, range) && condition(node)) {
458 				nodeList.push(node);
459 				node = nextNodeDescendants(node);
460 				continue;
461 			}
462 			node = nextNode(node);
463 		}
464 		return nodeList;
465 	}
466 
467 	/**
468 	 * As above, but includes nodes with an ancestor that's already been returned.
469 	 */
470 	function getAllContainedNodes(range, condition) {
471 		if (typeof condition == "undefined") {
472 			condition = function () {
473 				return true;
474 			};
475 		}
476 		var node = range.startContainer;
477 		if (node.hasChildNodes() && range.startOffset < node.childNodes.length) {
478 			// A child is contained
479 			node = node.childNodes[range.startOffset];
480 		} else if (range.startOffset == getNodeLength(node)) {
481 			// No descendant can be contained
482 			node = nextNodeDescendants(node);
483 		} else {
484 			// No children; this node at least can't be contained
485 			node = nextNode(node);
486 		}
487 
488 		var stop = range.endContainer;
489 		if (stop.hasChildNodes() && range.endOffset < stop.childNodes.length) {
490 			// The node after the last contained node is a child
491 			stop = stop.childNodes[range.endOffset];
492 		} else {
493 			// This node and/or some of its children might be contained
494 			stop = nextNodeDescendants(stop);
495 		}
496 
497 		var nodeList = [];
498 		while (isBefore(node, stop)) {
499 			if (isContained(node, range) && condition(node)) {
500 				nodeList.push(node);
501 			}
502 			node = nextNode(node);
503 		}
504 		return nodeList;
505 	}
506 
507 	// Returns either null, or something of the form rgb(x, y, z), or something of
508 	// the form rgb(x, y, z, w) with w != 0.
509 	function normalizeColor(color) {
510 		if (color.toLowerCase() == "currentcolor") {
511 			return null;
512 		}
513 
514 		var outerSpan = document.createElement("span");
515 		document.body.appendChild(outerSpan);
516 		outerSpan.style.color = "black";
517 
518 		var innerSpan = document.createElement("span");
519 		outerSpan.appendChild(innerSpan);
520 		innerSpan.style.color = color;
521 		color = $_.getComputedStyle(innerSpan).color;
522 
523 		if (color == "rgb(0, 0, 0)") {
524 525 			// Maybe it's really black, maybe it's invalid.
526 			outerSpan.color = "white";
527 			color = $_.getComputedStyle(innerSpan).color;
528 			if (color != "rgb(0, 0, 0)") {
529 				return null;
530 			}
531 		}
532 
533 		document.body.removeChild(outerSpan);
534 
535 		// I rely on the fact that browsers generally provide consistent syntax for
536 		// getComputedStyle(), although it's not standardized.  There are only two
537 		// exceptions I found:
538 		if (/^rgba\([0-9]+, [0-9]+, [0-9]+, 1\)$/.test(color)) {
539 			// IE10PP2 seems to do this sometimes.
540 			return color.replace("rgba", "rgb").replace(", 1)", ")");
541 		}
542 		if (color == "transparent") {
543 			// IE10PP2, Firefox 7.0a2, and Opera 11.50 all return "transparent" if
544 			// the specified value is "transparent".
545 			return "rgba(0, 0, 0, 0)";
546 		}
547 		return color;
548 	}
549 
550 	// Returns either null, or something of the form #xxxxxx, or the color itself
551 	// if it's a valid keyword.
552 	function parseSimpleColor(color) {
553 		color = color.toLowerCase();
554 		if ($_(["aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkgrey", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkslategrey", "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dimgrey", "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod", "gray", "green", "greenyellow", "grey", "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", "lightgray", "lightgreen", "lightgrey", "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", "lightslategrey", "lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise", "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "slategrey", "snow", "springgreen", "steelblue", "tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen"]).indexOf(color) != -1) {
555 			return color;
556 		}
557 
558 		color = normalizeColor(color);
559 		var matches = /^rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)$/.exec(color);
560 		if (matches) {
561 			return "#" + parseInt(matches[1], 10).toString(16).replace(/^.$/, "0$&") + parseInt(matches[2], 10).toString(16).replace(/^.$/, "0$&") + parseInt(matches[3], 10).toString(16).replace(/^.$/, "0$&");
562 		}
563 		return null;
564 	}
565 
566 	//@}
567 
568 	//////////////////////////////////////////////////////////////////////////////
569 	/////////////////////////// Edit command functions ///////////////////////////
570 	//////////////////////////////////////////////////////////////////////////////
571 
572 	/////////////////////////////////////////////////
573 	///// Methods of the HTMLDocument interface /////
574 	/////////////////////////////////////////////////
575 	//@{
576 
577 	var getStateOverride,
578 	    setStateOverride,
579 	    unsetStateOverride,
580 	    getValueOverride,
581 	    setValueOverride,
582 	    unsetValueOverride;
583 
584 	var executionStackDepth = 0;
585 
586 	// Helper function for fontSize's action plus queryOutputHelper.  It's just the
587 	// middle of fontSize's action, ripped out into its own function.
588 	function normalizeFontSize(value) {
589 		// "Strip leading and trailing whitespace from value."
590 		//
591 		// Cheap hack, not following the actual algorithm.
592 		value = $_(value).trim();
593 
594 		// "If value is a valid floating point number, or would be a valid
595 		// floating point number if a single leading "+" character were
596 		// stripped:"
597 		if (/^[\-+]?[0-9]+(\.[0-9]+)?([eE][\-+]?[0-9]+)?$/.test(value)) {
598 			var mode;
599 
600 			// "If the first character of value is "+", delete the character
601 			// and let mode be "relative-plus"."
602 			if (value[0] == "+") {
603 				value = value.slice(1);
604 				mode = "relative-plus";
605 				// "Otherwise, if the first character of value is "-", delete the
606 				// character and let mode be "relative-minus"."
607 			} else if (value[0] == "-") {
608 				value = value.slice(1);
609 				mode = "relative-minus";
610 				// "Otherwise, let mode be "absolute"."
611 			} else {
612 				mode = "absolute";
613 			}
614 
615 			// "Apply the rules for parsing non-negative integers to value, and
616 			// let number be the result."
617 			//
618 			// Another cheap hack.
619 			var num = parseInt(value, 10);
620 
621 			// "If mode is "relative-plus", add three to number."
622 			if (mode == "relative-plus") {
623 				num += 3;
624 			}
625 
626 			// "If mode is "relative-minus", negate number, then add three to
627 			// it."
628 			if (mode == "relative-minus") {
629 				num = 3 - num;
630 			}
631 
632 			// "If number is less than one, let number equal 1."
633 			if (num < 1) {
634 				num = 1;
635 			}
636 
637 			// "If number is greater than seven, let number equal 7."
638 			if (num > 7) {
639 				num = 7;
640 			}
641 
642 			// "Set value to the string here corresponding to number:" [table
643 			// omitted]
644 			value = {
645 				1: "xx-small",
646 				2: "small",
647 				3: "medium",
648 				4: "large",
649 				5: "x-large",
650 				6: "xx-large",
651 				7: "xxx-large"
652 			}[num];
653 		}
654 
655 		return value;
656 	}
657 
658 	function getLegacyFontSize(size) {
659 		// For convenience in other places in my code, I handle all sizes, not just
660 		// pixel sizes as the spec says.  This means pixel sizes have to be passed
661 		// in suffixed with "px", not as plain numbers.
662 		size = normalizeFontSize(size);
663 
664 		if (jQuery.inArray(size, ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]) == -1 && !/^[0-9]+(\.[0-9]+)?(cm|mm|in|pt|pc|px)$/.test(size)) {
665 			// There is no sensible legacy size for things like "2em".
666 			return null;
667 		}
668 
669 		var font = document.createElement("font");
670 		document.body.appendChild(font);
671 		if (size == "xxx-large") {
672 			font.size = 7;
673 		} else {
674 			font.style.fontSize = size;
675 		}
676 		var pixelSize = parseInt($_.getComputedStyle(font).fontSize, 10);
677 		document.body.removeChild(font);
678 
679 		// "Let returned size be 1."
680 		var returnedSize = 1;
681 
682 		// "While returned size is less than 7:"
683 		while (returnedSize < 7) {
684 			// "Let lower bound be the resolved value of "font-size" in pixels
685 			// of a font element whose size attribute is set to returned size."
686 			font = document.createElement("font");
687 			font.size = returnedSize;
688 			document.body.appendChild(font);
689 			var lowerBound = parseInt($_.getComputedStyle(font).fontSize, 10);
690 
691 			// "Let upper bound be the resolved value of "font-size" in pixels
692 			// of a font element whose size attribute is set to one plus
693 			// returned size."
694 			font.size = 1 + returnedSize;
695 			var upperBound = parseInt($_.getComputedStyle(font).fontSize, 10);
696 			document.body.removeChild(font);
697 
698 			// "Let average be the average of upper bound and lower bound."
699 			var average = (upperBound + lowerBound) / 2;
700 
701 			// "If pixel size is less than average, return the one-element
702 			// string consisting of the digit returned size."
703 			if (pixelSize < average) {
704 				return String(returnedSize);
705 			}
706 
707 			// "Add one to returned size."
708 			returnedSize++;
709 		}
710 
711 		// "Return "7"."
712 		return "7";
713 	}
714 
715 	// Helper function for common behavior.
716 	function editCommandMethod(command, prop, range, callback) {
717 		var ret;
718 
719 		// Set up our global range magic, but only if we're the outermost function
720 		if (executionStackDepth == 0 && typeof range != "undefined") {
721 			globalRange = range;
722 		} else if (executionStackDepth == 0) {
723 			globalRange = null;
724 			globalRange = range;
725 		}
726 
727 		// "If command is not supported, raise a NOT_SUPPORTED_ERR exception."
728 		//
729 		// We can't throw a real one, but a string will do for our purposes.
730 		if (!commands.hasOwnProperty(command)) {
731 			throw "NOT_SUPPORTED_ERR";
732 		}
733 
734 		// "If command has no action, raise an INVALID_ACCESS_ERR exception."
735 		// "If command has no indeterminacy, raise an INVALID_ACCESS_ERR
736 		// exception."
737 		// "If command has no state, raise an INVALID_ACCESS_ERR exception."
738 		// "If command has no value, raise an INVALID_ACCESS_ERR exception."
739 		if (prop != "enabled" && !commands[command].hasOwnProperty(prop)) {
740 			throw "INVALID_ACCESS_ERR";
741 		}
742 
743 		executionStackDepth++;
744 		try {
745 			ret = callback();
746 		} catch (e) {
747 			executionStackDepth--;
748 			throw e;
749 		}
750 		executionStackDepth--;
751 		return ret;
752 	}
753 
754 	function myQueryCommandEnabled(command, range) {
755 		// "All of these methods must treat their command argument ASCII
756 		// case-insensitively."
757 		command = command.toLowerCase();
758 
759 		// "If command is not supported, raise a NOT_SUPPORTED_ERR exception."
760 		return editCommandMethod(command, "action", range, (function (command) {
761 			return function () {
762 				// "Among commands defined in this specification, those listed in
763 				// Miscellaneous commands are always enabled. The other commands defined
764 				// here are enabled if the active range is not null, and disabled
765 				// otherwise."
766 				return jQuery.inArray(command, ["copy", "cut", "paste", "selectall", "stylewithcss", "usecss"]) != -1 || range !== null;
767 			};
768 		}(command)));
769 	}
770 
771 	function setActiveRange(range) {
772 		var rangeObject = new window.GENTICS.Utils.RangeObject();
773 
774 		rangeObject.startContainer = range.startContainer;
775 		rangeObject.startOffset = range.startOffset;
776 		rangeObject.endContainer = range.endContainer;
777 		rangeObject.endOffset = range.endOffset;
778 
779 		rangeObject.select();
780 	}
781 
782 	function myExecCommand(commandArg, showUiArg, valueArg, range) {
783 		// "All of these methods must treat their command argument ASCII
784 		// case-insensitively."
785 		var command = commandArg.toLowerCase();
786 		var showUi = showUiArg;
787 		var value = valueArg;
788 
789 		// "If only one argument was provided, let show UI be false."
790 		//
791 		// If range was passed, I can't actually detect how many args were passed
792 		// . . .
793 		if (arguments.length == 1 || (arguments.length >= 4 && typeof showUi == "undefined")) {
794 			showUi = false;
795 		}
796 
797 		// "If only one or two arguments were provided, let value be the empty
798 		// string."
799 		if (arguments.length <= 2 || (arguments.length >= 4 && typeof value == "undefined")) {
800 			value = "";
801 		}
802 
803 		// "If command is not supported, raise a NOT_SUPPORTED_ERR exception."
804 		//
805 		// "If command has no action, raise an INVALID_ACCESS_ERR exception."
806 		return editCommandMethod(command, "action", range, (function (command, showUi, value) {
807 808 			return function () {
809 				// "If command is not enabled, return false."
810 				if (!myQueryCommandEnabled(command)) {
811 					return false;
812 				}
813 
814 				// "Take the action for command, passing value to the instructions as an
815 				// argument."
816 				commands[command].action(value, range);
817 
818 				// always fix the range after the command is complete
819 				setActiveRange(range);
820 
821 				// "Return true."
822 				return true;
823 			};
824 		}(command, showUi, value)));
825 	}
826 
827 	function myQueryCommandIndeterm(command, range) {
828 		// "All of these methods must treat their command argument ASCII
829 		// case-insensitively."
830 		command = command.toLowerCase();
831 
832 		// "If command is not supported, raise a NOT_SUPPORTED_ERR exception."
833 		//
834 		// "If command has no indeterminacy, raise an INVALID_ACCESS_ERR
835 		// exception."
836 		return editCommandMethod(command, "indeterm", range, (function (command) {
837 			return function () {
838 				// "If command is not enabled, return false."
839 				if (!myQueryCommandEnabled(command, range)) {
840 					return false;
841 				}
842 
843 				// "Return true if command is indeterminate, otherwise false."
844 				return commands[command].indeterm(range);
845 			};
846 		}(command)));
847 	}
848 
849 	function myQueryCommandState(command, range) {
850 		// "All of these methods must treat their command argument ASCII
851 		// case-insensitively."
852 		command = command.toLowerCase();
853 
854 		// "If command is not supported, raise a NOT_SUPPORTED_ERR exception."
855 		//
856 		// "If command has no state, raise an INVALID_ACCESS_ERR exception."
857 		return editCommandMethod(command, "state", range, (function (command) {
858 			return function () {
859 				// "If command is not enabled, return false."
860 				if (!myQueryCommandEnabled(command, range)) {
861 					return false;
862 				}
863 
864 				// "If the state override for command is set, return it."
865 				if (typeof getStateOverride(command, range) != "undefined") {
866 					return getStateOverride(command, range);
867 				}
868 
869 				// "Return true if command's state is true, otherwise false."
870 				return commands[command].state(range);
871 			};
872 		}(command)));
873 	}
874 
875 	// "When the queryCommandSupported(command) method on the HTMLDocument
876 	// interface is invoked, the user agent must return true if command is
877 	// supported, and false otherwise."
878 	function myQueryCommandSupported(command) {
879 		// "All of these methods must treat their command argument ASCII
880 		// case-insensitively."
881 		command = command.toLowerCase();
882 
883 		return commands.hasOwnProperty(command);
884 	}
885 
886 	function myQueryCommandValue(command, range) {
887 		// "All of these methods must treat their command argument ASCII
888 		// case-insensitively."
889 		command = command.toLowerCase();
890 
891 		// "If command is not supported, raise a NOT_SUPPORTED_ERR exception."
892 		//
893 		// "If command has no value, raise an INVALID_ACCESS_ERR exception."
894 		return editCommandMethod(command, "value", range, function () {
895 			// "If command is not enabled, return the empty string."
896 			if (!myQueryCommandEnabled(command, range)) {
897 				return "";
898 			}
899 
900 			// "If command is "fontSize" and its value override is set, convert the
901 			// value override to an integer number of pixels and return the legacy
902 			// font size for the result."
903 			if (command == "fontsize" && getValueOverride("fontsize", range) !== undefined) {
904 				return getLegacyFontSize(getValueOverride("fontsize", range));
905 			}
906 
907 			// "If the value override for command is set, return it."
908 			if (typeof getValueOverride(command, range) != "undefined") {
909 				return getValueOverride(command, range);
910 			}
911 
912 			// "Return command's value."
913 			return commands[command].value(range);
914 		});
915 	}
916 	//@}
917 
918 	//////////////////////////////
919 	///// Common definitions /////
920 	//////////////////////////////
921 922 	//@{
923 
924 	// "A prohibited paragraph child name is "address", "article", "aside",
925 	// "blockquote", "caption", "center", "col", "colgroup", "dd", "details",
926 	// "dir", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer",
927 	// "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "li",
928 	// "listing", "menu", "nav", "ol", "p", "plaintext", "pre", "section",
929 	// "summary", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "ul", or
930 	// "xmp"."
931 	var prohibitedParagraphChildNamesMap = {
932 		"ADDRESS": true,
933 		"ARTICLE": true,
934 		"ASIDE": true,
935 		"BLOCKQUOTE": true,
936 		"CAPTION": true,
937 		"CENTER": true,
938 		"COL": true,
939 		"COLGROUP": true,
940 		"DD": true,
941 		"DETAILS": true,
942 		"DIR": true,
943 		"DIV": true,
944 		"DL": true,
945 		"DT": true,
946 		"FIELDSET": true,
947 		"FIGCAPTION": true,
948 		"FIGURE": true,
949 		"FOOTER": true,
950 		"FORM": true,
951 		"H1": true,
952 		"H2": true,
953 		"H3": true,
954 		"H4": true,
955 		"H5": true,
956 		"H6": true,
957 		"HEADER": true,
958 		"HGROUP": true,
959 		"HR": true,
960 		"LI": true,
961 		"LISTING": true,
962 		"MENU": true,
963 		"NAV": true,
964 		"OL": true,
965 		"P": true,
966 		"PLAINTEXT": true,
967 		"PRE": true,
968 		"SECTION": true,
969 		"SUMMARY": true,
970 		"TABLE": true,
971 		"TBODY": true,
972 		"TD": true,
973 		"TFOOT": true,
974 		"TH": true,
975 		"THEAD": true,
976 		"TR": true,
977 		"UL": true,
978 		"XMP": true
979 	};
980 
981 	// "A prohibited paragraph child is an HTML element whose local name is a
982 	// prohibited paragraph child name."
983 	function isProhibitedParagraphChild(node) {
984 		return isMappedHtmlElement(node, prohibitedParagraphChildNamesMap);
985 	}
986 
987 	var nonBlockDisplayValuesMap = {
988 		"inline": true,
989 		"inline-block": true,
990 		"inline-table": true,
991 		"none": true
992 	};
993 
994 	// "A block node is either an Element whose "display" property does not have
995 	// resolved value "inline" or "inline-block" or "inline-table" or "none", or a
996 	// Document, or a DocumentFragment."
997 	function isBlockNode(node) {
998 		return node && ((node.nodeType == $_.Node.ELEMENT_NODE && !nonBlockDisplayValuesMap[$_.getComputedStyle(node).display]) || node.nodeType == $_.Node.DOCUMENT_NODE || node.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE);
999 	}
1000 
1001 	// "An inline node is a node that is not a block node."
1002 	function isInlineNode(node) {
1003 		return node && !isBlockNode(node);
1004 	}
1005 1006 
	// "An editing host is a node that is either an Element with a contenteditable
1007 	// attribute set to the true state, or the Element child of a Document whose
1008 	// designMode is enabled."
1009 	function isEditingHost(node) {
1010 		return node && node.nodeType == $_.Node.ELEMENT_NODE && (node.contentEditable == "true" || (node.parentNode && node.parentNode.nodeType == $_.Node.DOCUMENT_NODE && node.parentNode.designMode == "on"));
1011 	}
1012 
1013 	// "Something is editable if it is a node which is not an editing host, does
1014 	// not have a contenteditable attribute set to the false state, and whose
1015 	// parent is an editing host or editable."
1016 	function isEditable(node) {
1017 		// This is slightly a lie, because we're excluding non-HTML elements with
1018 		// contentEditable attributes.
1019 		return node && !isEditingHost(node) && (node.nodeType != $_.Node.ELEMENT_NODE || node.contentEditable != "false" || jQuery(node).hasClass('aloha-table-wrapper')) && (isEditingHost(node.parentNode) || isEditable(node.parentNode));
1020 	}
1021 
1022 	// Helper function, not defined in the spec
1023 	function hasEditableDescendants(node) {
1024 		var i;
1025 		for (i = 0; i < node.childNodes.length; i++) {
1026 			if (isEditable(node.childNodes[i]) || hasEditableDescendants(node.childNodes[i])) {
1027 				return true;
1028 			}
1029 		}
1030 		return false;
1031 	}
1032 
1033 	// "The editing host of node is null if node is neither editable nor an editing
1034 	// host; node itself, if node is an editing host; or the nearest ancestor of
1035 	// node that is an editing host, if node is editable."
1036 	function getEditingHostOf(node) {
1037 		if (isEditingHost(node)) {
1038 			return node;
1039 		}
1040 		if (isEditable(node)) {
1041 			var ancestor = node.parentNode;
1042 			while (!isEditingHost(ancestor)) {
1043 				ancestor = ancestor.parentNode;
1044 1045 			}
1046 			return ancestor;
1047 		}
1048 		return null;
1049 1050 	}
1051 
1052 	// "Two nodes are in the same editing host if the editing host of the first is
1053 	// non-null and the same as the editing host of the second."
1054 	function inSameEditingHost(node1, node2) {
1055 		return getEditingHostOf(node1) && getEditingHostOf(node1) == getEditingHostOf(node2);
1056 	}
1057 
1058 	// "A collapsed line break is a br that begins a line box which has nothing
1059 	// else in it, and therefore has zero height."
1060 	function isCollapsedLineBreak(br) {
1061 		if (!isNamedHtmlElement(br, 'br')) {
1062 			return false;
1063 		}
1064 
1065 		// Add a zwsp after it and see if that changes the height of the nearest
1066 		// non-inline parent.  Note: this is not actually reliable, because the
1067 		// parent might have a fixed height or something.
1068 		var ref = br.parentNode;
1069 		while ($_.getComputedStyle(ref).display == "inline") {
1070 			ref = ref.parentNode;
1071 		}
1072 
1073 		var origStyle = {
1074 			height: ref.style.height,
1075 			maxHeight: ref.style.maxHeight,
1076 			minHeight: ref.style.minHeight
1077 		};
1078 
1079 		ref.style.height = 'auto';
1080 		ref.style.maxHeight = 'none';
1081 		if (!(jQuery.browser.msie && jQuery.browser.version < 8)) {
1082 			ref.style.minHeight = '0';
1083 		}
1084 		var space = document.createTextNode('\u200b');
1085 		var origHeight = ref.offsetHeight;
1086 		if (origHeight == 0) {
1087 			throw 'isCollapsedLineBreak: original height is zero, bug?';
1088 		}
1089 		br.parentNode.insertBefore(space, br.nextSibling);
1090 		var finalHeight = ref.offsetHeight;
1091 		space.parentNode.removeChild(space);
1092 
1093 		ref.style.height = origStyle.height;
1094 		ref.style.maxHeight = origStyle.maxHeight;
1095 		if (!(jQuery.browser.msie && jQuery.browser.version < 8)) {
1096 			ref.style.minHeight = origStyle.minHeight;
1097 		}
1098 
1099 		// Allow some leeway in case the zwsp didn't create a whole new line, but
1100 		// only made an existing line slightly higher.  Firefox 6.0a2 shows this
1101 		// behavior when the first line is bold.
1102 		return origHeight < finalHeight - 5;
1103 	}
1104 
1105 	// "An extraneous line break is a br that has no visual effect, in that
1106 	// removing it from the DOM would not change layout, except that a br that is
1107 	// the sole child of an li is not extraneous."
1108 	function isExtraneousLineBreak(br) {
1109 
1110 		if (!isNamedHtmlElement(br, 'br')) {
1111 			return false;
1112 		}
1113 
1114 		if (isNamedHtmlElement(br.parentNode, "li") && br.parentNode.childNodes.length == 1) {
1115 			return false;
1116 		}
1117 
1118 		// Make the line break disappear and see if that changes the block's
1119 		// height.  Yes, this is an absurd hack.  We have to reset height etc. on
1120 		// the reference node because otherwise its height won't change if it's not
1121 		// auto.
1122 		var ref = br.parentNode;
1123 		while ($_.getComputedStyle(ref).display == "inline") {
1124 			ref = ref.parentNode;
1125 		}
1126 
1127 		var origStyle = {
1128 			height: ref.style.height,
1129 			maxHeight: ref.style.maxHeight,
1130 			minHeight: ref.style.minHeight,
1131 			contentEditable: ref.contentEditable
1132 		};
1133 
1134 		ref.style.height = 'auto';
1135 		ref.style.maxHeight = 'none';
1136 		ref.style.minHeight = '0';
1137 		// IE7 would ignore display:none in contentEditable, so we temporarily set it to false
1138 		if (jQuery.browser.msie && jQuery.browser.version <= 7) {
1139 1140 			ref.contentEditable = 'false';
1141 		}
1142 
1143 		var origHeight = ref.offsetHeight;
1144 		if (origHeight == 0) {
1145 			throw "isExtraneousLineBreak: original height is zero, bug?";
1146 		}
1147 
1148 		var origBrDisplay = br.style.display;
1149 		br.style.display = 'none';
1150 		var finalHeight = ref.offsetHeight;
1151 
1152 		// Restore original styles to the touched elements.
1153 		ref.style.height = origStyle.height;
1154 		ref.style.maxHeight = origStyle.maxHeight;
1155 		ref.style.minHeight = origStyle.minHeight;
1156 		// reset contentEditable for IE7
1157 		if (jQuery.browser.msie && jQuery.browser.version <= 7) {
1158 			ref.contentEditable = origStyle.contentEditable;
1159 		}
1160 		br.style.display = origBrDisplay;
1161 
1162 		// https://github.com/alohaeditor/Aloha-Editor/issues/516
1163 		// look like it works in msie > 7
1164 		/* if (jQuery.browser.msie && jQuery.browser.version < 8) {
1165 		br.removeAttribute("style");
1166 		ref.removeAttribute("style");
1167 	} */
1168 
1169 		return origHeight == finalHeight;
1170 	}
1171 
1172 	// "A whitespace node is either a Text node whose data is the empty string; or
1173 	// a Text node whose data consists only of one or more tabs (0x0009), line
1174 	// feeds (0x000A), carriage returns (0x000D), and/or spaces (0x0020), and whose
1175 	// parent is an Element whose resolved value for "white-space" is "normal" or
1176 	// "nowrap"; or a Text node whose data consists only of one or more tabs
1177 	// (0x0009), carriage returns (0x000D), and/or spaces (0x0020), and whose
1178 	// parent is an Element whose resolved value for "white-space" is "pre-line"."
1179 	function isWhitespaceNode(node) {
1180 		return node && node.nodeType == $_.Node.TEXT_NODE && (node.data == "" || (/^[\t\n\r ]+$/.test(node.data) && node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(node.parentNode).whiteSpace, ["normal", "nowrap"]) != -1) || (/^[\t\r ]+$/.test(node.data) && node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE && $_.getComputedStyle(node.parentNode).whiteSpace == "pre-line") || (/^[\t\n\r ]+$/.test(node.data) && node.parentNode && node.parentNode.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE));
1181 	}
1182 
1183 	/**
1184 	 * Collapse sequences of ignorable whitespace (tab (0x0009), line feed (0x000A), carriage return (0x000D), space (0x0020)) to only one space.
1185 	 * Preserve the given range if necessary.
1186 	 * @param node text node
1187 	 * @param range range
1188 	 */
1189 	function collapseWhitespace(node, range) {
1190 		// "If node is neither editable nor an editing host, abort these steps."
1191 		if (!isEditable(node) && !isEditingHost(node)) {
1192 			return;
1193 		}
1194 
1195 		// if the given node is not a text node, return
1196 		if (!node || node.nodeType !== $_.Node.TEXT_NODE) {
1197 			return;
1198 		}
1199 
1200 		// if the node is in a pre or pre-wrap node, return
1201 		if (jQuery.inArray($_.getComputedStyle(node.parentNode).whiteSpace, ["pre", "pre-wrap"]) != -1) {
1202 			return;
1203 		}
1204 
1205 		// if the given node does not contain sequences of at least two consecutive ignorable whitespace characters, return
1206 		if (!/[\t\n\r ]{2,}/.test(node.data)) {
1207 			return;
1208 		}
1209 
1210 		var newData = '';
1211 		var correctStart = range.startContainer == node;
1212 		var correctEnd = range.endContainer == node;
1213 		var wsFound = false;
1214 		var i;
1215 
1216 		// iterate through the node data
1217 		for (i = 0; i < node.data.length; ++i) {
1218 			if (/[\t\n\r ]/.test(node.data.substr(i, 1))) {
1219 				// found a whitespace
1220 				if (!wsFound) {
1221 					// this is the first whitespace in the current sequence
1222 					// add a whitespace to the new data sequence
1223 					newData += ' ';
1224 					// remember that we found a whitespace
1225 					wsFound = true;
1226 				} else {
1227 					// this is not the first whitespace in the sequence, so omit this character
1228 					if (correctStart && newData.length < range.startOffset) {
1229 						range.startOffset--;
1230 					}
1231 					if (correctEnd && newData.length < range.endOffset) {
1232 						range.endOffset--;
1233 					}
1234 				}
1235 			} else {
1236 				newData += node.data.substr(i, 1);
1237 				wsFound = false;
1238 			}
1239 		}
1240 
1241 		// set the new data
1242 		node.data = newData;
1243 	}
1244 
1245 	// "node is a collapsed whitespace node if the following algorithm returns
1246 	// true:"
1247 	function isCollapsedWhitespaceNode(node) {
1248 		// "If node is not a whitespace node, return false."
1249 		if (!isWhitespaceNode(node)) {
1250 			return false;
1251 		}
1252 
1253 		// "If node's data is the empty string, return true."
1254 		if (node.data == "") {
1255 			return true;
1256 		}
1257 
1258 		// "Let ancestor be node's parent."
1259 		var ancestor = node.parentNode;
1260 
1261 		// "If ancestor is null, return true."
1262 		if (!ancestor) {
1263 			return true;
1264 		}
1265 
1266 		// "If the "display" property of some ancestor of node has resolved value
1267 		// "none", return true."
1268 		if ($_(getAncestors(node)).some(function (ancestor) { return ancestor.nodeType == $_.Node.ELEMENT_NODE && $_.getComputedStyle(ancestor).display == "none"; })) {
1269 			return true;
1270 		}
1271 
1272 		// "While ancestor is not a block node and its parent is not null, set
1273 		// ancestor to its parent."
1274 		while (!isBlockNode(ancestor) && ancestor.parentNode) {
1275 			ancestor = ancestor.parentNode;
1276 		}
1277 
1278 		// "Let reference be node."
1279 		var reference = node;
1280 
1281 		// "While reference is a descendant of ancestor:"
1282 		while (reference != ancestor) {
1283 			// "Let reference be the node before it in tree order."
1284 			reference = previousNode(reference);
1285 
1286 			// "If reference is a block node or a br, return true."
1287 			if (isBlockNode(reference) || isNamedHtmlElement(reference, 'br')) {
1288 				return true;
1289 			}
1290 
1291 			// "If reference is a Text node that is not a whitespace node, or is an
1292 			// img, break from this loop."
1293 			if ((reference.nodeType == $_.Node.TEXT_NODE && !isWhitespaceNode(reference)) || isNamedHtmlElement(reference, 'img')) {
1294 				break;
1295 			}
1296 		}
1297 
1298 		// "Let reference be node."
1299 		reference = node;
1300 
1301 		// "While reference is a descendant of ancestor:"
1302 		var stop = nextNodeDescendants(ancestor);
1303 		while (reference != stop) {
1304 			// "Let reference be the node after it in tree order, or null if there
1305 			// is no such node."
1306 			reference = nextNode(reference);
1307 
1308 			// "If reference is a block node or a br, return true."
1309 			if (isBlockNode(reference) || isNamedHtmlElement(reference, 'br')) {
1310 				return true;
1311 			}
1312 
1313 			// "If reference is a Text node that is not a whitespace node, or is an
1314 			// img, break from this loop."
1315 			if ((reference && reference.nodeType == $_.Node.TEXT_NODE && !isWhitespaceNode(reference)) || isNamedHtmlElement(reference, 'img')) {
1316 				break;
1317 			}
1318 		}
1319 
1320 		// "Return false."
1321 		return false;
1322 	}
1323 
1324 	// "Something is visible if it is a node that either is a block node, or a Text
1325 	// node that is not a collapsed whitespace node, or an img, or a br that is not
1326 	// an extraneous line break, or any node with a visible descendant; excluding
1327 	// any node with an ancestor container Element whose "display" property has
1328 	// resolved value "none"."
1329 	function isVisible(node) {
1330 		var i;
1331 
1332 		if (!node) {
1333 			return false;
1334 		}
1335 
1336 		if ($_(getAncestors(node).concat(node))
1337 			    .filter(function (node) { return node.nodeType == $_.Node.ELEMENT_NODE; }, true)
1338 			    .some(function (node) { return $_.getComputedStyle(node).display == "none"; })) {
1339 			return false;
1340 		}
1341 
1342 		if (isBlockNode(node) || (node.nodeType == $_.Node.TEXT_NODE && !isCollapsedWhitespaceNode(node)) || isNamedHtmlElement(node, 'img') || (isNamedHtmlElement(node, 'br') && !isExtraneousLineBreak(node))) {
1343 			return true;
1344 		}
1345 
1346 		for (i = 0; i < node.childNodes.length; i++) {
1347 			if (isVisible(node.childNodes[i])) {
1348 1349 				return true;
1350 			}
1351 1352 		}
1353 
1354 		return false;
1355 	}
1356 
1357 	// "Something is invisible if it is a node that is not visible."
1358 	function isInvisible(node) {
1359 		return node && !isVisible(node);
1360 	}
1361 
1362 	// "A collapsed block prop is either a collapsed line break that is not an
1363 	// extraneous line break, or an Element that is an inline node and whose
1364 	// children are all either invisible or collapsed block props and that has at
1365 	// least one child that is a collapsed block prop."
1366 	function isCollapsedBlockProp(node) {
1367 		var i;
1368 
1369 		if (isCollapsedLineBreak(node) && !isExtraneousLineBreak(node)) {
1370 			return true;
1371 		}
1372 
1373 		if (!isInlineNode(node) || node.nodeType != $_.Node.ELEMENT_NODE) {
1374 			return false;
1375 		}
1376 
1377 		var hasCollapsedBlockPropChild = false;
1378 		for (i = 0; i < node.childNodes.length; i++) {
1379 			if (!isInvisible(node.childNodes[i]) && !isCollapsedBlockProp(node.childNodes[i])) {
1380 				return false;
1381 			}
1382 			if (isCollapsedBlockProp(node.childNodes[i])) {
1383 				hasCollapsedBlockPropChild = true;
1384 			}
1385 		}
1386 
1387 		return hasCollapsedBlockPropChild;
1388 	}
1389 
1390 	// Please note: This method is deprecated and will be removed. 
1391 	// Every command should use the value and range parameter. 
1392 	// 
1393 	// "The active range is the first range in the Selection given by calling
1394 	// getSelection() on the context object, or null if there is no such range."
1395 	//
1396 1397 	// We cheat and return globalRange if that's defined.  We also ensure that the
1398 	// active range meets the requirements that selection boundary points are
1399 	// supposed to meet, i.e., that the nodes are both Text or Element nodes that
1400 	// descend from a Document.
1401 	function getActiveRange() {
1402 		var ret;
1403 		if (globalRange) {
1404 			ret = globalRange;
1405 		} else if (Aloha.getSelection().rangeCount) {
1406 			ret = Aloha.getSelection().getRangeAt(0);
1407 		} else {
1408 			return null;
1409 		}
1410 		if (jQuery.inArray(ret.startContainer.nodeType, [$_.Node.TEXT_NODE, $_.Node.ELEMENT_NODE]) == -1 || jQuery.inArray(ret.endContainer.nodeType, [$_.Node.TEXT_NODE, $_.Node.ELEMENT_NODE]) == -1 || !ret.startContainer.ownerDocument || !ret.endContainer.ownerDocument || !isDescendant(ret.startContainer, ret.startContainer.ownerDocument) || !isDescendant(ret.endContainer, ret.endContainer.ownerDocument)) {
1411 			throw "Invalid active range; test bug?";
1412 		}
1413 		return ret;
1414 	}
1415 
1416 	// "For some commands, each HTMLDocument must have a boolean state override
1417 	// and/or a string value override. These do not change the command's state or
1418 	// value, but change the way some algorithms behave, as specified in those
1419 	// algorithms' definitions. Initially, both must be unset for every command.
1420 	// Whenever the number of ranges in the Selection changes to something
1421 	// different, and whenever a boundary point of the range at a given index in
1422 	// the Selection changes to something different, the state override and value
1423 	// override must be unset for every command."
1424 	//
1425 	// We implement this crudely by using setters and getters.  To verify that the
1426 	// selection hasn't changed, we copy the active range and just check the
1427 	// endpoints match.  This isn't really correct, but it's good enough for us.
1428 	// Unset state/value overrides are undefined.  We put everything in a function
1429 	// so no one can access anything except via the provided functions, since
1430 	// otherwise callers might mistakenly use outdated overrides (if the selection
1431 	// has changed).
1432 	(function () {
1433 		var stateOverrides = {};
1434 		var valueOverrides = {};
1435 		var storedRange = null;
1436 
1437 		function resetOverrides(range) {
1438 			if (!storedRange || storedRange.startContainer != range.startContainer || storedRange.endContainer != range.endContainer || storedRange.startOffset != range.startOffset || storedRange.endOffset != range.endOffset) {
1439 				stateOverrides = {};
1440 				valueOverrides = {};
1441 				storedRange = range.cloneRange();
1442 			}
1443 		}
1444 
1445 		getStateOverride = function (command, range) {
1446 			resetOverrides(range);
1447 			return stateOverrides[command];
1448 		};
1449 
1450 		setStateOverride = function (command, newState, range) {
1451 			resetOverrides(range);
1452 			stateOverrides[command] = newState;
1453 		};
1454 
1455 		unsetStateOverride = function (command, range) {
1456 			resetOverrides(range);
1457 			delete stateOverrides[command];
1458 		};
1459 
1460 		getValueOverride = function (command, range) {
1461 			resetOverrides(range);
1462 			return valueOverrides[command];
1463 		};
1464 
1465 		// "The value override for the backColor command must be the same as the
1466 		// value override for the hiliteColor command, such that setting one sets
1467 		// the other to the same thing and unsetting one unsets the other."
1468 		setValueOverride = function (command, newValue, range) {
1469 			resetOverrides(range);
1470 			valueOverrides[command] = newValue;
1471 			if (command == "backcolor") {
1472 				valueOverrides.hilitecolor = newValue;
1473 			} else if (command == "hilitecolor") {
1474 				valueOverrides.backcolor = newValue;
1475 			}
1476 		};
1477 
1478 		unsetValueOverride = function (command, range) {
1479 			resetOverrides(range);
1480 			delete valueOverrides[command];
1481 			if (command == "backcolor") {
1482 				delete valueOverrides.hilitecolor;
1483 			} else if (command == "hilitecolor") {
1484 				delete valueOverrides.backcolor;
1485 			}
1486 		};
1487 	}());
1488 
1489 	//@}
1490 
1491 	/////////////////////////////
1492 	///// Common algorithms /////
1493 	/////////////////////////////
1494 
1495 	///// Assorted common algorithms /////
1496 	//@{
1497 
1498 	function movePreservingRanges(node, newParent, newIndex, range) {
1499 		// For convenience, I allow newIndex to be -1 to mean "insert at the end".
1500 		if (newIndex == -1) {
1501 			newIndex = newParent.childNodes.length;
1502 		}
1503 
1504 		// "When the user agent is to move a Node to a new location, preserving
1505 		// ranges, it must remove the Node from its original parent (if any), then
1506 		// insert it in the new location. In doing so, however, it must ignore the
1507 		// regular range mutation rules, and instead follow these rules:"
1508 
1509 		// "Let node be the moved Node, old parent and old index be the old parent
1510 		// (which may be null) and index, and new parent and new index be the new
1511 		// parent and index."
1512 		var oldParent = node.parentNode;
1513 		var oldIndex = getNodeIndex(node);
1514 		var i;
1515 
1516 		// We only even attempt to preserve the global range object and the ranges
1517 		// in the selection, not every range out there (the latter is probably
1518 		// impossible).
1519 		var ranges = [range];
1520 		for (i = 0; i < Aloha.getSelection().rangeCount; i++) {
1521 			ranges.push(Aloha.getSelection().getRangeAt(i));
1522 		}
1523 		var boundaryPoints = [];
1524 		$_(ranges).forEach(function (range) {
1525 			boundaryPoints.push([range.startContainer, range.startOffset]);
1526 			boundaryPoints.push([range.endContainer, range.endOffset]);
1527 		});
1528 
1529 		$_(boundaryPoints).forEach(function (boundaryPoint) {
1530 			// "If a boundary point's node is the same as or a descendant of node,
1531 			// leave it unchanged, so it moves to the new location."
1532 			//
1533 			// No modifications necessary.
1534 
1535 			// "If a boundary point's node is new parent and its offset is greater
1536 			// than new index, add one to its offset."
1537 1538 			if (boundaryPoint[0] == newParent && boundaryPoint[1] > newIndex) {
1539 				boundaryPoint[1]++;
1540 			}
1541 
1542 			// "If a boundary point's node is old parent and its offset is old index or
1543 			// old index + 1, set its node to new parent and add new index − old index
1544 			// to its offset."
1545 			if (boundaryPoint[0] == oldParent && (boundaryPoint[1] == oldIndex || boundaryPoint[1] == oldIndex + 1)) {
1546 				boundaryPoint[0] = newParent;
1547 				boundaryPoint[1] += newIndex - oldIndex;
1548 			}
1549 
1550 			// "If a boundary point's node is old parent and its offset is greater than
1551 			// old index + 1, subtract one from its offset."
1552 			if (boundaryPoint[0] == oldParent && boundaryPoint[1] > oldIndex + 1) {
1553 				boundaryPoint[1]--;
1554 			}
1555 		});
1556 
1557 		// Now actually move it and preserve the ranges.
1558 		if (newParent.childNodes.length == newIndex) {
1559 			newParent.appendChild(node);
1560 		} else {
1561 			newParent.insertBefore(node, newParent.childNodes[newIndex]);
1562 		}
1563 
1564 		// if we're off actual node boundaries this implies that the move was
1565 		// part of a deletion process (backspace). If that's the case we 
1566 		// attempt to fix this by restoring the range to the first index of
1567 		// the node that has been moved
1568 		var newRange = null;
1569 		if (boundaryPoints[0][1] > boundaryPoints[0][0].childNodes.length && boundaryPoints[1][1] > boundaryPoints[1][0].childNodes.length) {
1570 			range.setStart(node, 0);
1571 			range.setEnd(node, 0);
1572 		} else {
1573 			range.setStart(boundaryPoints[0][0], boundaryPoints[0][1]);
1574 			range.setEnd(boundaryPoints[1][0], boundaryPoints[1][1]);
1575 
1576 			Aloha.getSelection().removeAllRanges();
1577 			for (i = 1; i < ranges.length; i++) {
1578 				newRange = Aloha.createRange();
1579 				newRange.setStart(boundaryPoints[2 * i][0], boundaryPoints[2 * i][1]);
1580 				newRange.setEnd(boundaryPoints[2 * i + 1][0], boundaryPoints[2 * i + 1][1]);
1581 				Aloha.getSelection().addRange(newRange);
1582 			}
1583 			if (newRange) {
1584 				range = newRange;
1585 			}
1586 		}
1587 	}
1588 
1589 	/**
1590 	 * Copy all non empty attributes from an existing to a new element
1591 	 * 
1592 	 * @param {dom} element The source DOM element
1593 	 * @param {dom} newElement The new DOM element which will get the attributes of the source DOM element
1594 	 * @return void
1595 	 */
1596 	function copyAttributes(element, newElement) {
1597 
1598 		// This is an IE7 workaround. We identified three places that were connected 
1599 		// to the mysterious ie7 crash:
1600 		// 1. Add attribute to dom element (Initialization of jquery-ui sortable)
1601 		// 2. Access the jquery expando attribute. Just reading the name is 
1602 		//    sufficient to make the browser vulnerable for the crash (Press enter)
1603 		// 3. On editable blur the Aloha.editables[0].getContents(); gets invoked.
1604 		//    This invokation somehow crashes the ie7. We assume that the access of 
1605 		//    shared expando attribute updates internal references which are not 
1606 		//    correclty handled during clone(); 
1607 		if (jQuery.browser.msie && jQuery.browser.version >= 7 && typeof element.attributes[jQuery.expando] !== 'undefined') {
1608 			jQuery(element).removeAttr(jQuery.expando);
1609 		}
1610 
1611 		var attrs = element.attributes;
1612 		var i;
1613 		for (i = 0; i < attrs.length; i++) {
1614 			var attr = attrs[i];
1615 			// attr.specified is an IE specific check to exclude attributes that were never really set.
1616 			if (typeof attr.specified === "undefined" || attr.specified) {
1617 				if (typeof newElement.setAttributeNS === 'function') {
1618 					newElement.setAttributeNS(attr.namespaceURI, attr.name, attr.value);
1619 				} else {
1620 					// fixes https://github.com/alohaeditor/Aloha-Editor/issues/515 
1621 					newElement.setAttribute(attr.name, attr.value);
1622 				}
1623 			}
1624 		}
1625 	}
1626 
1627 	function setTagName(element, newName, range) {
1628 		// "If element is an HTML element with local name equal to new name, return
1629 		// element."
1630 		if (isNamedHtmlElement(element, newName)) {
1631 			return element;
1632 		}
1633 
1634 		// "If element's parent is null, return element."
1635 		if (!element.parentNode) {
1636 			return element;
1637 		}
1638 
1639 		// "Let replacement element be the result of calling createElement(new
1640 		// name) on the ownerDocument of element."
1641 		var replacementElement = element.ownerDocument.createElement(newName);
1642 
1643 		// "Insert replacement element into element's parent immediately before
1644 		// element."
1645 		element.parentNode.insertBefore(replacementElement, element);
1646 
1647 		// "Copy all attributes of element to replacement element, in order."
1648 		copyAttributes(element, replacementElement);
1649 
1650 		// "While element has children, append the first child of element as the
1651 		// last child of replacement element, preserving ranges."
1652 		while (element.childNodes.length) {
1653 			movePreservingRanges(element.firstChild, replacementElement, replacementElement.childNodes.length, range);
1654 		}
1655 
1656 		// "Remove element from its parent."
1657 		element.parentNode.removeChild(element);
1658 
1659 		// if the range still uses the old element, we modify it to the new one
1660 		if (range.startContainer === element) {
1661 			range.startContainer = replacementElement;
1662 		}
1663 		if (range.endContainer === element) {
1664 			range.endContainer = replacementElement;
1665 		}
1666 
1667 		// "Return replacement element."
1668 		return replacementElement;
1669 	}
1670 
1671 	function removeExtraneousLineBreaksBefore(node) {
1672 		// "Let ref be the previousSibling of node."
1673 		var ref = node.previousSibling;
1674 
1675 		// "If ref is null, abort these steps."
1676 		if (!ref) {
1677 			return;
1678 		}
1679 
1680 		// "While ref has children, set ref to its lastChild."
1681 		while (ref.hasChildNodes()) {
1682 			ref = ref.lastChild;
1683 		}
1684 
1685 		// "While ref is invisible but not an extraneous line break, and ref does
1686 		// not equal node's parent, set ref to the node before it in tree order."
1687 		while (isInvisible(ref) && !isExtraneousLineBreak(ref) && ref != node.parentNode) {
1688 			ref = previousNode(ref);
1689 		}
1690 
1691 		// "If ref is an editable extraneous line break, remove it from its
1692 		// parent."
1693 		if (isEditable(ref) && isExtraneousLineBreak(ref)) {
1694 			ref.parentNode.removeChild(ref);
1695 		}
1696 	}
1697 
1698 	function removeExtraneousLineBreaksAtTheEndOf(node) {
1699 		// "Let ref be node."
1700 		var ref = node;
1701 
1702 		// "While ref has children, set ref to its lastChild."
1703 		while (ref.hasChildNodes()) {
1704 			ref = ref.lastChild;
1705 		}
1706 
1707 		// "While ref is invisible but not an extraneous line break, and ref does
1708 		// not equal node, set ref to the node before it in tree order."
1709 		while (isInvisible(ref) && !isExtraneousLineBreak(ref) && ref != node) {
1710 			ref = previousNode(ref);
1711 		}
1712 
1713 		// "If ref is an editable extraneous line break, remove it from its
1714 		// parent."
1715 		if (isEditable(ref) && isExtraneousLineBreak(ref)) {
1716 			ref.parentNode.removeChild(ref);
1717 		}
1718 	}
1719 
1720 	// "To remove extraneous line breaks from a node, first remove extraneous line
1721 	// breaks before it, then remove extraneous line breaks at the end of it."
1722 	function removeExtraneousLineBreaksFrom(node) {
1723 		removeExtraneousLineBreaksBefore(node);
1724 		removeExtraneousLineBreaksAtTheEndOf(node);
1725 	}
1726 
1727 	//@}
1728 	///// Wrapping a list of nodes /////
1729 	//@{
1730 
1731 	function wrap(nodeList, siblingCriteria, newParentInstructions, range) {
1732 		var i;
1733 
1734 		// "If not provided, sibling criteria returns false and new parent
1735 		// instructions returns null."
1736 		if (typeof siblingCriteria == "undefined") {
1737 			siblingCriteria = function () {
1738 				return false;
1739 			};
1740 		}
1741 		if (typeof newParentInstructions == "undefined") {
1742 			newParentInstructions = function () {
1743 				return null;
1744 			};
1745 		}
1746 
1747 		// "If node list is empty, or the first member of node list is not
1748 		// editable, return null and abort these steps."
1749 		if (!nodeList.length || !isEditable(nodeList[0])) {
1750 			return null;
1751 		}
1752 
1753 		// "If node list's last member is an inline node that's not a br, and node
1754 		// list's last member's nextSibling is a br, append that br to node list."
1755 		if (isInlineNode(nodeList[nodeList.length - 1]) && !isNamedHtmlElement(nodeList[nodeList.length - 1], "br") && isNamedHtmlElement(nodeList[nodeList.length - 1].nextSibling, "br")) {
1756 			nodeList.push(nodeList[nodeList.length - 1].nextSibling);
1757 		}
1758 
1759 		// "If the previousSibling of the first member of node list is editable and
1760 		// running sibling criteria on it returns true, let new parent be the
1761 		// previousSibling of the first member of node list."
1762 		var newParent;
1763 		if (isEditable(nodeList[0].previousSibling) && siblingCriteria(nodeList[0].previousSibling)) {
1764 			newParent = nodeList[0].previousSibling;
1765 
1766 			// "Otherwise, if the nextSibling of the last member of node list is
1767 			// editable and running sibling criteria on it returns true, let new parent
1768 			// be the nextSibling of the last member of node list."
1769 		} else if (isEditable(nodeList[nodeList.length - 1].nextSibling) && siblingCriteria(nodeList[nodeList.length - 1].nextSibling)) {
1770 			newParent = nodeList[nodeList.length - 1].nextSibling;
1771 
1772 			// "Otherwise, run new parent instructions, and let new parent be the
1773 			// result."
1774 		} else {
1775 			newParent = newParentInstructions();
1776 		}
1777 
1778 		// "If new parent is null, abort these steps and return null."
1779 		if (!newParent) {
1780 			return null;
1781 		}
1782 
1783 		// "If new parent's parent is null:"
1784 		if (!newParent.parentNode) {
1785 			// "Insert new parent into the parent of the first member of node list
1786 			// immediately before the first member of node list."
1787 			nodeList[0].parentNode.insertBefore(newParent, nodeList[0]);
1788 
1789 			// "If any range has a boundary point with node equal to the parent of
1790 			// new parent and offset equal to the index of new parent, add one to
1791 			// that boundary point's offset."
1792 			//
1793 			// Try to fix range
1794 			var startContainer = range.startContainer,
1795 				startOffset = range.startOffset,
1796 				endContainer = range.endContainer,
1797 				endOffset = range.endOffset;
1798 			if (startContainer == newParent.parentNode && startOffset >= getNodeIndex(newParent)) {
1799 				range.setStart(startContainer, startOffset + 1);
1800 			}
1801 			if (endContainer == newParent.parentNode && endOffset >= getNodeIndex(newParent)) {
1802 				range.setEnd(endContainer, endOffset + 1);
1803 			}
1804 
1805 			// Only try to fix the global range. TODO remove globalRange here
1806 			if (globalRange && globalRange !== range) {
1807 				startContainer = globalRange.startContainer;
1808 				startOffset = globalRange.startOffset;
1809 				endContainer = globalRange.endContainer;
1810 				endOffset = globalRange.endOffset;
1811 				if (startContainer == newParent.parentNode && startOffset >= getNodeIndex(newParent)) {
1812 					globalRange.setStart(startContainer, startOffset + 1);
1813 				}
1814 				if (endContainer == newParent.parentNode && endOffset >= getNodeIndex(newParent)) {
1815 					globalRange.setEnd(endContainer, endOffset + 1);
1816 				}
1817 			}
1818 		}
1819 
1820 		// "Let original parent be the parent of the first member of node list."
1821 		var originalParent = nodeList[0].parentNode;
1822 
1823 		// "If new parent is before the first member of node list in tree order:"
1824 		if (isBefore(newParent, nodeList[0])) {
1825 			// "If new parent is not an inline node, but the last child of new
1826 			// parent and the first member of node list are both inline nodes, and
1827 			// the last child of new parent is not a br, call createElement("br")
1828 			// on the ownerDocument of new parent and append the result as the last
1829 			// child of new parent."
1830 			if (!isInlineNode(newParent) && isInlineNode(newParent.lastChild) && isInlineNode(nodeList[0]) && !isNamedHtmlElement(newParent.lastChild, "BR")) {
1831 				newParent.appendChild(newParent.ownerDocument.createElement("br"));
1832 			}
1833 
1834 			// "For each node in node list, append node as the last child of new
1835 			// parent, preserving ranges."
1836 			for (i = 0; i < nodeList.length; i++) {
1837 				movePreservingRanges(nodeList[i], newParent, -1, range);
1838 			}
1839 
1840 			// "Otherwise:"
1841 		} else {
1842 			// "If new parent is not an inline node, but the first child of new
1843 			// parent and the last member of node list are both inline nodes, and
1844 			// the last member of node list is not a br, call createElement("br")
1845 			// on the ownerDocument of new parent and insert the result as the
1846 			// first child of new parent."
1847 			if (!isInlineNode(newParent) && isInlineNode(newParent.firstChild) && isInlineNode(nodeList[nodeList.length - 1]) && !isNamedHtmlElement(nodeList[nodeList.length - 1], "BR")) {
1848 				newParent.insertBefore(newParent.ownerDocument.createElement("br"), newParent.firstChild);
1849 			}
1850 
1851 			// "For each node in node list, in reverse order, insert node as the
1852 			// first child of new parent, preserving ranges."
1853 			for (i = nodeList.length - 1; i >= 0; i--) {
1854 				movePreservingRanges(nodeList[i], newParent, 0, range);
1855 			}
1856 		}
1857 
1858 		// "If original parent is editable and has no children, remove it from its
1859 		// parent."
1860 		if (isEditable(originalParent) && !originalParent.hasChildNodes()) {
1861 			originalParent.parentNode.removeChild(originalParent);
1862 		}
1863 
1864 1865 		// "If new parent's nextSibling is editable and running sibling criteria on
1866 		// it returns true:"
1867 		if (isEditable(newParent.nextSibling) && siblingCriteria(newParent.nextSibling)) {
1868 			// "If new parent is not an inline node, but new parent's last child
1869 			// and new parent's nextSibling's first child are both inline nodes,
1870 			// and new parent's last child is not a br, call createElement("br") on
1871 			// the ownerDocument of new parent and append the result as the last
1872 			// child of new parent."
1873 			if (!isInlineNode(newParent) && isInlineNode(newParent.lastChild) && isInlineNode(newParent.nextSibling.firstChild) && !isNamedHtmlElement(newParent.lastChild, "BR")) {
1874 				newParent.appendChild(newParent.ownerDocument.createElement("br"));
1875 			}
1876 
1877 			// "While new parent's nextSibling has children, append its first child
1878 			// as the last child of new parent, preserving ranges."
1879 			while (newParent.nextSibling.hasChildNodes()) {
1880 				movePreservingRanges(newParent.nextSibling.firstChild, newParent, -1, range);
1881 			}
1882 
1883 			// "Remove new parent's nextSibling from its parent."
1884 			newParent.parentNode.removeChild(newParent.nextSibling);
1885 		}
1886 
1887 		// "Remove extraneous line breaks from new parent."
1888 		removeExtraneousLineBreaksFrom(newParent);
1889 
1890 		// "Return new parent."
1891 		return newParent;
1892 	}
1893 
1894 
1895 	//@}
1896 	///// Allowed children /////
1897 	//@{
1898 
1899 	// "A name of an element with inline contents is "a", "abbr", "b", "bdi",
1900 	// "bdo", "cite", "code", "dfn", "em", "h1", "h2", "h3", "h4", "h5", "h6", "i",
1901 	// "kbd", "mark", "p", "pre", "q", "rp", "rt", "ruby", "s", "samp", "small",
1902 	// "span", "strong", "sub", "sup", "u", "var", "acronym", "listing", "strike",
1903 	// "xmp", "big", "blink", "font", "marquee", "nobr", or "tt"."
1904 	var namesOfElementsWithInlineContentsMap = {
1905 		"A": true,
1906 		"ABBR": true,
1907 		"B": true,
1908 		"BDI": true,
1909 		"BDO": true,
1910 		"CITE": true,
1911 		"CODE": true,
1912 		"DFN": true,
1913 		"EM": true,
1914 		"H1": true,
1915 		"H2": true,
1916 		"H3": true,
1917 		"H4": true,
1918 		"H5": true,
1919 		"H6": true,
1920 		"I": true,
1921 		"KBD": true,
1922 		"MARK": true,
1923 		"P": true,
1924 		"PRE": true,
1925 		"Q": true,
1926 		"RP": true,
1927 		"RT": true,
1928 		"RUBY": true,
1929 		"S": true,
1930 		"SAMP": true,
1931 		"SMALL": true,
1932 		"SPAN": true,
1933 		"STRONG": true,
1934 		"SUB": true,
1935 		"SUP": true,
1936 		"U": true,
1937 		"VAR": true,
1938 		"ACRONYM": true,
1939 		"LISTING": true,
1940 		"STRIKE": true,
1941 		"XMP": true,
1942 		"BIG": true,
1943 		"BLINK": true,
1944 		"FONT": true,
1945 		"MARQUEE": true,
1946 		"NOBR": true,
1947 		"TT": true
1948 	};
1949 
1950 
1951 	var tableRelatedElements = {
1952 		"colgroup": true,
1953 		"table": true,
1954 		"tbody": true,
1955 		"tfoot": true,
1956 		"thead": true,
1957 		"tr": true
1958 	};
1959 
1960 	var scriptRelatedElements = {
1961 		"script": true,
1962 		"style": true,
1963 		"plaintext": true,
1964 		"xmp": true
1965 	};
1966 
1967 	var prohibitedHeadingNestingMap = jQuery.extend({
1968 		"H1": true,
1969 		"H2": true,
1970 		"H3": true,
1971 		"H4": true,
1972 		"H5": true,
1973 		"H6": true
1974 	}, prohibitedParagraphChildNamesMap);
1975 	var prohibitedTableNestingMap = {
1976 		"CAPTION": true,
1977 		"COL": true,
1978 		"COLGROUP": true,
1979 		"TBODY": true,
1980 		"TD": true,
1981 		"TFOOT": true,
1982 		"TH": true,
1983 		"THEAD": true,
1984 		"TR": true
1985 	};
1986 	var prohibitedDefNestingMap = {
1987 		"DD": true,
1988 		"DT": true
1989 	};
1990 	var prohibitedNestingCombinationsMap = {
1991 		"A": jQuery.extend({
1992 			"A": true
1993 		}, prohibitedParagraphChildNamesMap),
1994 		"DD": prohibitedDefNestingMap,
1995 		"DT": prohibitedDefNestingMap,
1996 		"LI": {
1997 			"LI": true
1998 		},
1999 		"NOBR": jQuery.extend({
2000 			"NOBR": true
2001 		}, prohibitedParagraphChildNamesMap),
2002 		"H1": prohibitedHeadingNestingMap,
2003 		"H2": prohibitedHeadingNestingMap,
2004 		"H3": prohibitedHeadingNestingMap,
2005 		"H4": prohibitedHeadingNestingMap,
2006 		"H5": prohibitedHeadingNestingMap,
2007 		"H6": prohibitedHeadingNestingMap,
2008 		"TD": prohibitedTableNestingMap,
2009 		"TH": prohibitedTableNestingMap,
2010 		// this is the same as namesOfElementsWithInlineContentsMap excluding a and h1-h6 elements above
2011 		"ABBR": prohibitedParagraphChildNamesMap,
2012 		"B": prohibitedParagraphChildNamesMap,
2013 		"BDI": prohibitedParagraphChildNamesMap,
2014 		"BDO": prohibitedParagraphChildNamesMap,
2015 		"CITE": prohibitedParagraphChildNamesMap,
2016 		"CODE": prohibitedParagraphChildNamesMap,
2017 		"DFN": prohibitedParagraphChildNamesMap,
2018 		"EM": prohibitedParagraphChildNamesMap,
2019 		"I": prohibitedParagraphChildNamesMap,
2020 		"KBD": prohibitedParagraphChildNamesMap,
2021 		"MARK": prohibitedParagraphChildNamesMap,
2022 		"P": prohibitedParagraphChildNamesMap,
2023 		"PRE": prohibitedParagraphChildNamesMap,
2024 		"Q": prohibitedParagraphChildNamesMap,
2025 		"RP": prohibitedParagraphChildNamesMap,
2026 		"RT": prohibitedParagraphChildNamesMap,
2027 		"RUBY": prohibitedParagraphChildNamesMap,
2028 		"S": prohibitedParagraphChildNamesMap,
2029 		"SAMP": prohibitedParagraphChildNamesMap,
2030 		"SMALL": prohibitedParagraphChildNamesMap,
2031 		"SPAN": prohibitedParagraphChildNamesMap,
2032 		"STRONG": prohibitedParagraphChildNamesMap,
2033 		"SUB": prohibitedParagraphChildNamesMap,
2034 		"SUP": prohibitedParagraphChildNamesMap,
2035 		"U": prohibitedParagraphChildNamesMap,
2036 		"VAR": prohibitedParagraphChildNamesMap,
2037 		"ACRONYM": prohibitedParagraphChildNamesMap,
2038 		"LISTING": prohibitedParagraphChildNamesMap,
2039 		"STRIKE": prohibitedParagraphChildNamesMap,
2040 		"XMP": prohibitedParagraphChildNamesMap,
2041 		"BIG": prohibitedParagraphChildNamesMap,
2042 		"BLINK": prohibitedParagraphChildNamesMap,
2043 		"FONT": prohibitedParagraphChildNamesMap,
2044 		"MARQUEE": prohibitedParagraphChildNamesMap,
2045 		"TT": prohibitedParagraphChildNamesMap
2046 	};
2047 
2048 	// "An element with inline contents is an HTML element whose local name is a
2049 	// name of an element with inline contents."
2050 	function isElementWithInlineContents(node) {
2051 		return isMappedHtmlElement(node, namesOfElementsWithInlineContentsMap);
2052 	}
2053 
2054 	function isAllowedChild(child, parent_) {
2055 		// "If parent is "colgroup", "table", "tbody", "tfoot", "thead", "tr", or
2056 		// an HTML element with local name equal to one of those, and child is a
2057 		// Text node whose data does not consist solely of space characters, return
2058 		// false."
2059 		if ((tableRelatedElements[parent_] || isHtmlElementInArray(parent_, ["colgroup", "table", "tbody", "tfoot", "thead", "tr"])) && typeof child == "object" && child.nodeType == $_.Node.TEXT_NODE && !/^[ \t\n\f\r]*$/.test(child.data)) {
2060 			return false;
2061 		}
2062 
2063 		// "If parent is "script", "style", "plaintext", or "xmp", or an HTML
2064 		// element with local name equal to one of those, and child is not a Text
2065 		// node, return false."
2066 		if ((scriptRelatedElements[parent_] || isHtmlElementInArray(parent_, ["script", "style", "plaintext", "xmp"])) && (typeof child != "object" || child.nodeType != $_.Node.TEXT_NODE)) {
2067 			return false;
2068 		}
2069 
2070 		// "If child is a Document, DocumentFragment, or DocumentType, return
2071 		// false."
2072 		if (typeof child == "object" && (child.nodeType == $_.Node.DOCUMENT_NODE || child.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE || child.nodeType == $_.Node.DOCUMENT_TYPE_NODE)) {
2073 			return false;
2074 		}
2075 
2076 		// "If child is an HTML element, set child to the local name of child."
2077 		if (isAnyHtmlElement(child)) {
2078 			child = child.tagName.toLowerCase();
2079 		}
2080 
2081 		// "If child is not a string, return true."
2082 		if (typeof child != "string") {
2083 			return true;
2084 		}
2085 
2086 		// "If parent is an HTML element:"
2087 		if (isAnyHtmlElement(parent_)) {
2088 			// "If child is "a", and parent or some ancestor of parent is an a,
2089 			// return false."
2090 			//
2091 			// "If child is a prohibited paragraph child name and parent or some
2092 			// ancestor of parent is an element with inline contents, return
2093 			// false."
2094 			//
2095 			// "If child is "h1", "h2", "h3", "h4", "h5", or "h6", and parent or
2096 			// some ancestor of parent is an HTML element with local name "h1",
2097 			// "h2", "h3", "h4", "h5", or "h6", return false."
2098 			var ancestor = parent_;
2099 			while (ancestor) {
2100 				if (child == "a" && isNamedHtmlElement(ancestor, 'a')) {
2101 					return false;
2102 				}
2103 				if (prohibitedParagraphChildNamesMap[child.toUpperCase()] && isElementWithInlineContents(ancestor)) {
2104 					return false;
2105 				}
2106 				if (/^h[1-6]$/.test(child) && isAnyHtmlElement(ancestor) && /^H[1-6]$/.test(ancestor.tagName)) {
2107 					return false;
2108 				}
2109 				ancestor = ancestor.parentNode;
2110 			}
2111 
2112 			// "Let parent be the local name of parent."
2113 			parent_ = parent_.tagName.toLowerCase();
2114 		}
2115 
2116 		// "If parent is an Element or DocumentFragment, return true."
2117 		if (typeof parent_ == "object" && (parent_.nodeType == $_.Node.ELEMENT_NODE || parent_.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE)) {
2118 			return true;
2119 		}
2120 
2121 		// "If parent is not a string, return false."
2122 		if (typeof parent_ != "string") {
2123 			return false;
2124 		}
2125 
2126 		// "If parent is on the left-hand side of an entry on the following list,
2127 		// then return true if child is listed on the right-hand side of that
2128 		// entry, and false otherwise."
2129 		switch (parent_) {
2130 		case "colgroup":
2131 			return child == "col";
2132 		case "table":
2133 			return jQuery.inArray(child, ["caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"]) != -1;
2134 		case "tbody":
2135 		case "thead":
2136 		case "tfoot":
2137 			return jQuery.inArray(child, ["td", "th", "tr"]) != -1;
2138 		case "tr":
2139 			return jQuery.inArray(child, ["td", "th"]) != -1;
2140 		case "dl":
2141 			return jQuery.inArray(child, ["dt", "dd"]) != -1;
2142 		case "dir":
2143 		case "ol":
2144 		case "ul":
2145 			return jQuery.inArray(child, ["dir", "li", "ol", "ul"]) != -1;
2146 		case "hgroup":
2147 			return (/^h[1-6]$/).test(child);
2148 		}
2149 
2150 		// "If child is "body", "caption", "col", "colgroup", "frame", "frameset",
2151 		// "head", "html", "tbody", "td", "tfoot", "th", "thead", or "tr", return
2152 		// false."
2153 		if (jQuery.inArray(child, ["body", "caption", "col", "colgroup", "frame", "frameset", "head", "html", "tbody", "td", "tfoot", "th", "thead", "tr"]) != -1) {
2154 			return false;
2155 		}
2156 
2157 		// "If child is "dd" or "dt" and parent is not "dl", return false."
2158 		if (jQuery.inArray(child, ["dd", "dt"]) != -1 && parent_ != "dl") {
2159 			return false;
2160 		}
2161 
2162 		// "If child is "li" and parent is not "ol" or "ul", return false."
2163 		if (child == "li" && parent_ != "ol" && parent_ != "ul") {
2164 			return false;
2165 		}
2166 
2167 		// "If parent is on the left-hand side of an entry on the following list
2168 		// and child is listed on the right-hand side of that entry, return false."
2169 		var leftSide = prohibitedNestingCombinationsMap[parent_.toUpperCase()];
2170 2171 		if (leftSide) {
2172 			var rightSide = leftSide[child.toUpperCase()];
2173 			if (rightSide) {
2174 				return false;
2175 			}
2176 		}
2177 
2178 		// "Return true."
2179 		return true;
2180 	}
2181 
2182 
2183 	//@}
2184 
2185 	//////////////////////////////////////
2186 	///// Inline formatting commands /////
2187 	//////////////////////////////////////
2188 
2189 	///// Inline formatting command definitions /////
2190 	//@{
2191 
2192 	// "A node node is effectively contained in a range range if range is not
2193 	// collapsed, and at least one of the following holds:"
2194 	function isEffectivelyContained(node, range) {
2195 		if (range.collapsed) {
2196 			return false;
2197 		}
2198 
2199 		// "node is contained in range."
2200 		if (isContained(node, range)) {
2201 			return true;
2202 		}
2203 
2204 		// "node is range's start node, it is a Text node, and its length is
2205 		// different from range's start offset."
2206 		if (node == range.startContainer && node.nodeType == $_.Node.TEXT_NODE && getNodeLength(node) != range.startOffset) {
2207 			return true;
2208 		}
2209 
2210 		// "node is range's end node, it is a Text node, and range's end offset is
2211 		// not 0."
2212 		if (node == range.endContainer && node.nodeType == $_.Node.TEXT_NODE && range.endOffset != 0) {
2213 			return true;
2214 		}
2215 
2216 		// "node has at least one child; and all its children are effectively
2217 		// contained in range; and either range's start node is not a descendant of
2218 		// node or is not a Text node or range's start offset is zero; and either
2219 		// range's end node is not a descendant of node or is not a Text node or
2220 		// range's end offset is its end node's length."
2221 		if (node.hasChildNodes() && $_(node.childNodes).every(function (child) { return isEffectivelyContained(child, range); })
2222 			    && (!isDescendant(range.startContainer, node)
2223 					|| range.startContainer.nodeType != $_.Node.TEXT_NODE
2224 					|| range.startOffset == 0)
2225 			    && (!isDescendant(range.endContainer, node)
2226 					|| range.endContainer.nodeType != $_.Node.TEXT_NODE
2227 					|| range.endOffset == getNodeLength(range.endContainer))) {
2228 			return true;
2229 		}
2230 
2231 		return false;
2232 	}
2233 
2234 	// Like get(All)ContainedNodes(), but for effectively contained nodes.
2235 	function getEffectivelyContainedNodes(range, condition) {
2236 		if (typeof condition == "undefined") {
2237 			condition = function () {
2238 				return true;
2239 			};
2240 		}
2241 2242 		var node = range.startContainer;
2243 		while (isEffectivelyContained(node.parentNode, range)) {
2244 			node = node.parentNode;
2245 		}
2246 
2247 		var stop = nextNodeDescendants(range.endContainer);
2248 
2249 		var nodeList = [];
2250 		while (isBefore(node, stop)) {
2251 			if (isEffectivelyContained(node, range) && condition(node)) {
2252 				nodeList.push(node);
2253 				node = nextNodeDescendants(node);
2254 				continue;
2255 			}
2256 			node = nextNode(node);
2257 		}
2258 		return nodeList;
2259 	}
2260 
2261 	function getAllEffectivelyContainedNodes(range, condition) {
2262 		if (typeof condition == "undefined") {
2263 			condition = function () {
2264 				return true;
2265 			};
2266 		}
2267 		var node = range.startContainer;
2268 		while (isEffectivelyContained(node.parentNode, range)) {
2269 			node = node.parentNode;
2270 		}
2271 
2272 		var stop = nextNodeDescendants(range.endContainer);
2273 
2274 		var nodeList = [];
2275 		while (isBefore(node, stop)) {
2276 			if (isEffectivelyContained(node, range) && condition(node)) {
2277 				nodeList.push(node);
2278 			}
2279 			node = nextNode(node);
2280 		}
2281 		return nodeList;
2282 	}
2283 
2284 	// "A modifiable element is a b, em, i, s, span, strong, sub, sup, or u element
2285 	// with no attributes except possibly style; or a font element with no
2286 	// attributes except possibly style, color, face, and/or size; or an a element
2287 	// with no attributes except possibly style and/or href."
2288 	function isModifiableElement(node) {
2289 		if (!isAnyHtmlElement(node)) {
2290 			return false;
2291 		}
2292 
2293 		if (jQuery.inArray(node.tagName, ["B", "EM", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"]) != -1) {
2294 			if (node.attributes.length == 0) {
2295 				return true;
2296 			}
2297 
2298 			if (node.attributes.length == 1 && hasAttribute(node, "style")) {
2299 				return true;
2300 			}
2301 		}
2302 
2303 		if (node.tagName == "FONT" || node.tagName == "A") {
2304 			var numAttrs = node.attributes.length;
2305 
2306 			if (hasAttribute(node, "style")) {
2307 				numAttrs--;
2308 			}
2309 
2310 			if (node.tagName == "FONT") {
2311 				if (hasAttribute(node, "color")) {
2312 					numAttrs--;
2313 				}
2314 
2315 				if (hasAttribute(node, "face")) {
2316 					numAttrs--;
2317 				}
2318 
2319 				if (hasAttribute(node, "size")) {
2320 					numAttrs--;
2321 				}
2322 			}
2323 
2324 			if (node.tagName == "A" && hasAttribute(node, "href")) {
2325 				numAttrs--;
2326 			}
2327 
2328 			if (numAttrs == 0) {
2329 				return true;
2330 			}
2331 		}
2332 
2333 		return false;
2334 	}
2335 
2336 	function isSimpleModifiableElement(node) {
2337 		// "A simple modifiable element is an HTML element for which at least one
2338 		// of the following holds:"
2339 		if (!isAnyHtmlElement(node)) {
2340 			return false;
2341 		}
2342 
2343 		// Only these elements can possibly be a simple modifiable element.
2344 		if (jQuery.inArray(node.tagName, ["A", "B", "EM", "FONT", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"]) == -1) {
2345 			return false;
2346 		}
2347 
2348 		// "It is an a, b, em, font, i, s, span, strike, strong, sub, sup, or u
2349 		// element with no attributes."
2350 		if (node.attributes.length == 0) {
2351 			return true;
2352 		}
2353 
2354 		// If it's got more than one attribute, everything after this fails.
2355 		if (node.attributes.length > 1) {
2356 			return false;
2357 		}
2358 
2359 		// "It is an a, b, em, font, i, s, span, strike, strong, sub, sup, or u
2360 		// element with exactly one attribute, which is style, which sets no CSS
2361 		// properties (including invalid or unrecognized properties)."
2362 		//
2363 		// Not gonna try for invalid or unrecognized.
2364 		if (hasAttribute(node, "style") && getStyleLength(node) == 0) {
2365 			return true;
2366 		}
2367 
2368 		// "It is an a element with exactly one attribute, which is href."
2369 		if (node.tagName == "A" && hasAttribute(node, "href")) {
2370 			return true;
2371 		}
2372 
2373 		// "It is a font element with exactly one attribute, which is either color,
2374 		// face, or size."
2375 		if (node.tagName == "FONT" && (hasAttribute(node, "color") || hasAttribute(node, "face") || hasAttribute(node, "size"))) {
2376 			return true;
2377 		}
2378 
2379 		// "It is a b or strong element with exactly one attribute, which is style,
2380 		// and the style attribute sets exactly one CSS property (including invalid
2381 		// or unrecognized properties), which is "font-weight"."
2382 		if ((node.tagName == "B" || node.tagName == "STRONG") && hasAttribute(node, "style") && getStyleLength(node) == 1 && node.style.fontWeight != "") {
2383 			return true;
2384 		}
2385 
2386 		// "It is an i or em element with exactly one attribute, which is style,
2387 		// and the style attribute sets exactly one CSS property (including invalid
2388 		// or unrecognized properties), which is "font-style"."
2389 		if ((node.tagName == "I" || node.tagName == "EM") && hasAttribute(node, "style") && getStyleLength(node) == 1 && node.style.fontStyle != "") {
2390 			return true;
2391 		}
2392 
2393 		// "It is an a, font, or span element with exactly one attribute, which is
2394 		// style, and the style attribute sets exactly one CSS property (including
2395 		// invalid or unrecognized properties), and that property is not
2396 		// "text-decoration"."
2397 		if ((node.tagName == "A" || node.tagName == "FONT" || node.tagName == "SPAN") && hasAttribute(node, "style") && getStyleLength(node) == 1 && node.style.textDecoration == "") {
2398 			return true;
2399 		}
2400 
2401 		// "It is an a, font, s, span, strike, or u element with exactly one
2402 		// attribute, which is style, and the style attribute sets exactly one CSS
2403 		// property (including invalid or unrecognized properties), which is
2404 		// "text-decoration", which is set to "line-through" or "underline" or
2405 		// "overline" or "none"."
2406 		if (jQuery.inArray(node.tagName, ["A", "FONT", "S", "SPAN", "STRIKE", "U"]) != -1 && hasAttribute(node, "style") && getStyleLength(node) == 1 && (node.style.textDecoration == "line-through" || node.style.textDecoration == "underline" || node.style.textDecoration == "overline" || node.style.textDecoration == "none")) {
2407 			return true;
2408 		}
2409 
2410 		return false;
2411 	}
2412 
2413 	// "Two quantities are equivalent values for a command if either both are null,
2414 	// or both are strings and they're equal and the command does not define any
2415 2416 	// equivalent values, or both are strings and the command defines equivalent
2417 	// values and they match the definition."
2418 	function areEquivalentValues(command, val1, val2) {
2419 		if (val1 === null && val2 === null) {
2420 			return true;
2421 		}
2422 
2423 		if (typeof val1 == "string" && typeof val2 == "string" && val1 == val2 && !(commands[command].hasOwnProperty("equivalentValues"))) {
2424 			return true;
2425 		}
2426 
2427 		if (typeof val1 == "string" && typeof val2 == "string" && commands[command].hasOwnProperty("equivalentValues") && commands[command].equivalentValues(val1, val2)) {
2428 			return true;
2429 		}
2430 
2431 		return false;
2432 	}
2433 
2434 	// "Two quantities are loosely equivalent values for a command if either they
2435 	// are equivalent values for the command, or if the command is the fontSize
2436 	// command; one of the quantities is one of "xx-small", "small", "medium",
2437 	// "large", "x-large", "xx-large", or "xxx-large"; and the other quantity is
2438 	// the resolved value of "font-size" on a font element whose size attribute has
2439 	// the corresponding value set ("1" through "7" respectively)."
2440 	function areLooselyEquivalentValues(command, val1, val2) {
2441 		if (areEquivalentValues(command, val1, val2)) {
2442 			return true;
2443 		}
2444 2445 
		if (command != "fontsize" || typeof val1 != "string" || typeof val2 != "string") {
2446 			return false;
2447 		}
2448 
2449 		// Static variables in JavaScript?
2450 		var callee = areLooselyEquivalentValues;
2451 		if (callee.sizeMap === undefined) {
2452 			callee.sizeMap = {};
2453 			var font = document.createElement("font");
2454 			document.body.appendChild(font);
2455 			$_(["xx-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]).forEach(function (keyword) {
2456 				font.size = cssSizeToLegacy(keyword);
2457 				callee.sizeMap[keyword] = $_.getComputedStyle(font).fontSize;
2458 			});
2459 			document.body.removeChild(font);
2460 		}
2461 
2462 		return val1 === callee.sizeMap[val2] || val2 === callee.sizeMap[val1];
2463 	}
2464 
2465 	//@}
2466 	///// Assorted inline formatting command algorithms /////
2467 	//@{
2468 
2469 	function getEffectiveCommandValue(node, command) {
2470 		// "If neither node nor its parent is an Element, return null."
2471 		if (node.nodeType != $_.Node.ELEMENT_NODE && (!node.parentNode || node.parentNode.nodeType != $_.Node.ELEMENT_NODE)) {
2472 			return null;
2473 		}
2474 
2475 		// "If node is not an Element, return the effective command value of its
2476 		// parent for command."
2477 		if (node.nodeType != $_.Node.ELEMENT_NODE) {
2478 			return getEffectiveCommandValue(node.parentNode, command);
2479 		}
2480 
2481 		// "If command is "createLink" or "unlink":"
2482 		if (command == "createlink" || command == "unlink") {
2483 			// "While node is not null, and is not an a element that has an href
2484 			// attribute, set node to its parent."
2485 			while (node && (!isAnyHtmlElement(node) || node.tagName != "A" || !hasAttribute(node, "href"))) {
2486 				node = node.parentNode;
2487 			}
2488 
2489 			// "If node is null, return null."
2490 			if (!node) {
2491 				return null;
2492 			}
2493 
2494 			// "Return the value of node's href attribute."
2495 			return node.getAttribute("href");
2496 		}
2497 
2498 		// "If command is "backColor" or "hiliteColor":"
2499 		if (command == "backcolor" || command == "hilitecolor") {
2500 			// "While the resolved value of "background-color" on node is any
2501 			// fully transparent value, and node's parent is an Element, set
2502 			// node to its parent."
2503 			//
2504 			// Another lame hack to avoid flawed APIs.
2505 			while (($_.getComputedStyle(node).backgroundColor == "rgba(0, 0, 0, 0)" || $_.getComputedStyle(node).backgroundColor === "" || $_.getComputedStyle(node).backgroundColor == "transparent") && node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE) {
2506 				node = node.parentNode;
2507 			}
2508 
2509 			// "If the resolved value of "background-color" on node is a fully
2510 			// transparent value, return "rgb(255, 255, 255)"."
2511 			if ($_.getComputedStyle(node).backgroundColor == "rgba(0, 0, 0, 0)" || $_.getComputedStyle(node).backgroundColor === "" || $_.getComputedStyle(node).backgroundColor == "transparent") {
2512 				return "rgb(255, 255, 255)";
2513 			}
2514 
2515 			// "Otherwise, return the resolved value of "background-color" for
2516 			// node."
2517 			return $_.getComputedStyle(node).backgroundColor;
2518 		}
2519 
2520 		// "If command is "subscript" or "superscript":"
2521 		if (command == "subscript" || command == "superscript") {
2522 			// "Let affected by subscript and affected by superscript be two
2523 			// boolean variables, both initially false."
2524 			var affectedBySubscript = false;
2525 			var affectedBySuperscript = false;
2526 
2527 			// "While node is an inline node:"
2528 			while (isInlineNode(node)) {
2529 				var verticalAlign = $_.getComputedStyle(node).verticalAlign;
2530 
2531 				// "If node is a sub, set affected by subscript to true."
2532 				if (isNamedHtmlElement(node, 'sub')) {
2533 					affectedBySubscript = true;
2534 					// "Otherwise, if node is a sup, set affected by superscript to
2535 					// true."
2536 				} else if (isNamedHtmlElement(node, 'sup')) {
2537 					affectedBySuperscript = true;
2538 				}
2539 
2540 				// "Set node to its parent."
2541 				node = node.parentNode;
2542 			}
2543 
2544 			// "If affected by subscript and affected by superscript are both true,
2545 			// return the string "mixed"."
2546 			if (affectedBySubscript && affectedBySuperscript) {
2547 				return "mixed";
2548 			}
2549 
2550 			// "If affected by subscript is true, return "subscript"."
2551 			if (affectedBySubscript) {
2552 				return "subscript";
2553 			}
2554 
2555 			// "If affected by superscript is true, return "superscript"."
2556 			if (affectedBySuperscript) {
2557 				return "superscript";
2558 			}
2559 
2560 			// "Return null."
2561 			return null;
2562 		}
2563 
2564 		// "If command is "strikethrough", and the "text-decoration" property of
2565 		// node or any of its ancestors has resolved value containing
2566 		// "line-through", return "line-through". Otherwise, return null."
2567 		if (command == "strikethrough") {
2568 			do {
2569 				if ($_.getComputedStyle(node).textDecoration.indexOf("line-through") != -1) {
2570 					return "line-through";
2571 				}
2572 				node = node.parentNode;
2573 			} while (node && node.nodeType == $_.Node.ELEMENT_NODE);
2574 			return null;
2575 		}
2576 
2577 		// "If command is "underline", and the "text-decoration" property of node
2578 		// or any of its ancestors has resolved value containing "underline",
2579 		// return "underline". Otherwise, return null."
2580 		if (command == "underline") {
2581 			do {
2582 				if ($_.getComputedStyle(node).textDecoration.indexOf("underline") != -1) {
2583 					return "underline";
2584 				}
2585 				node = node.parentNode;
2586 			} while (node && node.nodeType == $_.Node.ELEMENT_NODE);
2587 			return null;
2588 		}
2589 
2590 		if (!commands[command].hasOwnProperty("relevantCssProperty")) {
2591 			throw "Bug: no relevantCssProperty for " + command + " in getEffectiveCommandValue";
2592 		}
2593 
2594 		// "Return the resolved value for node of the relevant CSS property for
2595 		// command."
2596 		return $_.getComputedStyle(node)[commands[command].relevantCssProperty].toString();
2597 	}
2598 
2599 	function getSpecifiedCommandValue(element, command) {
2600 		// "If command is "backColor" or "hiliteColor" and element's display
2601 		// property does not have resolved value "inline", return null."
2602 		if ((command == "backcolor" || command == "hilitecolor") && $_.getComputedStyle(element).display != "inline") {
2603 			return null;
2604 		}
2605 
2606 		// "If command is "createLink" or "unlink":"
2607 		if (command == "createlink" || command == "unlink") {
2608 			// "If element is an a element and has an href attribute, return the
2609 			// value of that attribute."
2610 			if (isAnyHtmlElement(element) && element.tagName == "A" && hasAttribute(element, "href")) {
2611 				return element.getAttribute("href");
2612 			}
2613 
2614 			// "Return null."
2615 			return null;
2616 		}
2617 
2618 		// "If command is "subscript" or "superscript":"
2619 		if (command == "subscript" || command == "superscript") {
2620 			// "If element is a sup, return "superscript"."
2621 			if (isNamedHtmlElement(element, 'sup')) {
2622 				return "superscript";
2623 			}
2624 
2625 			// "If element is a sub, return "subscript"."
2626 			if (isNamedHtmlElement(element, 'sub')) {
2627 				return "subscript";
2628 			}
2629 
2630 			// "Return null."
2631 			return null;
2632 		}
2633 
2634 		// "If command is "strikethrough", and element has a style attribute set,
2635 		// and that attribute sets "text-decoration":"
2636 		if (command == "strikethrough" && element.style.textDecoration != "") {
2637 			// "If element's style attribute sets "text-decoration" to a value
2638 			// containing "line-through", return "line-through"."
2639 			if (element.style.textDecoration.indexOf("line-through") != -1) {
2640 				return "line-through";
2641 			}
2642 
2643 			// "Return null."
2644 			return null;
2645 		}
2646 
2647 		// "If command is "strikethrough" and element is a s or strike element,
2648 		// return "line-through"."
2649 		if (command == "strikethrough" && isHtmlElementInArray(element, ["S", "STRIKE"])) {
2650 			return "line-through";
2651 		}
2652 
2653 		// "If command is "underline", and element has a style attribute set, and
2654 		// that attribute sets "text-decoration":"
2655 		if (command == "underline" && element.style.textDecoration != "") {
2656 			// "If element's style attribute sets "text-decoration" to a value
2657 			// containing "underline", return "underline"."
2658 			if (element.style.textDecoration.indexOf("underline") != -1) {
2659 				return "underline";
2660 			}
2661 
2662 			// "Return null."
2663 			return null;
2664 		}
2665 
2666 		// "If command is "underline" and element is a u element, return
2667 		// "underline"."
2668 		if (command == "underline" && isNamedHtmlElement(element, 'U')) {
2669 			return "underline";
2670 		}
2671 
2672 		// "Let property be the relevant CSS property for command."
2673 		var property = commands[command].relevantCssProperty;
2674 
2675 		// "If property is null, return null."
2676 		if (property === null) {
2677 			return null;
2678 		}
2679 
2680 		// "If element has a style attribute set, and that attribute has the
2681 		// effect of setting property, return the value that it sets property to."
2682 		if (element.style[property] != "") {
2683 			return element.style[property];
2684 		}
2685 
2686 		// "If element is a font element that has an attribute whose effect is
2687 		// to create a presentational hint for property, return the value that the
2688 		// hint sets property to.  (For a size of 7, this will be the non-CSS value
2689 		// "xxx-large".)"
2690 		if (isHtmlNamespace(element.namespaceURI) && element.tagName == "FONT") {
2691 			if (property == "color" && hasAttribute(element, "color")) {
2692 				return element.color;
2693 			}
2694 			if (property == "fontFamily" && hasAttribute(element, "face")) {
2695 				return element.face;
2696 			}
2697 			if (property == "fontSize" && hasAttribute(element, "size")) {
2698 				// This is not even close to correct in general.
2699 				var size = parseInt(element.size, 10);
2700 				if (size < 1) {
2701 					size = 1;
2702 				}
2703 				if (size > 7) {
2704 					size = 7;
2705 				}
2706 				return {
2707 					1: "xx-small",
2708 					2: "small",
2709 					3: "medium",
2710 					4: "large",
2711 					5: "x-large",
2712 					6: "xx-large",
2713 					7: "xxx-large"
2714 				}[size];
2715 			}
2716 		}
2717 
2718 		// "If element is in the following list, and property is equal to the
2719 		// CSS property name listed for it, return the string listed for it."
2720 		//
2721 		// A list follows, whose meaning is copied here.
2722 		if (property == "fontWeight" && (element.tagName == "B" || element.tagName == "STRONG")) {
2723 			return "bold";
2724 		}
2725 		if (property == "fontStyle" && (element.tagName == "I" || element.tagName == "EM")) {
2726 			return "italic";
2727 		}
2728 
2729 		// "Return null."
2730 		return null;
2731 	}
2732 
2733 	function reorderModifiableDescendants(node, command, newValue, range) {
2734 		// "Let candidate equal node."
2735 		var candidate = node;
2736 
2737 		// "While candidate is a modifiable element, and candidate has exactly one
2738 		// child, and that child is also a modifiable element, and candidate is not
2739 		// a simple modifiable element or candidate's specified command value for
2740 		// command is not equivalent to new value, set candidate to its child."
2741 		while (isModifiableElement(candidate) && candidate.childNodes.length == 1 && isModifiableElement(candidate.firstChild) && (!isSimpleModifiableElement(candidate) || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue))) {
2742 			candidate = candidate.firstChild;
2743 		}
2744 
2745 		// "If candidate is node, or is not a simple modifiable element, or its
2746 		// specified command value is not equivalent to new value, or its effective
2747 		// command value is not loosely equivalent to new value, abort these
2748 		// steps."
2749 		if (candidate == node || !isSimpleModifiableElement(candidate) || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue) || !areLooselyEquivalentValues(command, getEffectiveCommandValue(candidate, command), newValue)) {
2750 			return;
2751 		}
2752 
2753 		// "While candidate has children, insert the first child of candidate into
2754 		// candidate's parent immediately before candidate, preserving ranges."
2755 		while (candidate.hasChildNodes()) {
2756 			movePreservingRanges(candidate.firstChild, candidate.parentNode, getNodeIndex(candidate), range);
2757 		}
2758 
2759 		// "Insert candidate into node's parent immediately after node."
2760 		node.parentNode.insertBefore(candidate, node.nextSibling);
2761 
2762 		// "Append the node as the last child of candidate, preserving ranges."
2763 		movePreservingRanges(node, candidate, -1, range);
2764 	}
2765 
2766 	var recordValuesCommands = ["subscript", "bold", "fontname", "fontsize", "forecolor", "hilitecolor", "italic", "strikethrough", "underline"];
2767 
2768 	function recordValues(nodeList) {
2769 		// "Let values be a list of (node, command, specified command value)
2770 		// triples, initially empty."
2771 		var values = [];
2772 
2773 		// "For each node in node list, for each command in the list "subscript",
2774 		// "bold", "fontName", "fontSize", "foreColor", "hiliteColor", "italic",
2775 		// "strikethrough", and "underline" in that order:"
2776 
2777 		// Ensure we have a plain array to avoid the potential performance
2778 		// overhead of a NodeList
2779 		var nodes = jQuery.makeArray(nodeList);
2780 		var i, j;
2781 		var node;
2782 		var command;
2783 		var ancestor;
2784 		var specifiedCommandValue;
2785 		for (i = 0; i < nodes.length; i++) {
2786 			node = nodes[i];
2787 			for (j = 0; j < recordValuesCommands.length; j++) {
2788 				command = recordValuesCommands[j];
2789 
2790 				// "Let ancestor equal node."
2791 				ancestor = node;
2792 
2793 				// "If ancestor is not an Element, set it to its parent."
2794 				if (ancestor.nodeType != 1) {
2795 					ancestor = ancestor.parentNode;
2796 				}
2797 
2798 				// "While ancestor is an Element and its specified command value
2799 				// for command is null, set it to its parent."
2800 				specifiedCommandValue = null;
2801 				while (ancestor && ancestor.nodeType == 1 && (specifiedCommandValue = getSpecifiedCommandValue(ancestor, command)) === null) {
2802 					ancestor = ancestor.parentNode;
2803 				}
2804 
2805 				// "If ancestor is an Element, add (node, command, ancestor's
2806 				// specified command value for command) to values. Otherwise add
2807 				// (node, command, null) to values."
2808 				values.push([node, command, specifiedCommandValue]);
2809 			}
2810 		}
2811 
2812 		// "Return values."
2813 		return values;
2814 	}
2815 
2816 	//@}
2817 	///// Clearing an element's value /////
2818 	//@{
2819 
2820 	function clearValue(element, command, range) {
2821 		// "If element is not editable, return the empty list."
2822 		if (!isEditable(element)) {
2823 			return [];
2824 		}
2825 
2826 		// "If element's specified command value for command is null, return the
2827 		// empty list."
2828 		if (getSpecifiedCommandValue(element, command) === null) {
2829 			return [];
2830 		}
2831 
2832 		// "If element is a simple modifiable element:"
2833 		if (isSimpleModifiableElement(element)) {
2834 			// "Let children be the children of element."
2835 			var children = Array.prototype.slice.call(toArray(element.childNodes));
2836 
2837 			// "For each child in children, insert child into element's parent
2838 			// immediately before element, preserving ranges."
2839 			var i;
2840 			for (i = 0; i < children.length; i++) {
2841 				movePreservingRanges(children[i], element.parentNode, getNodeIndex(element), range);
2842 			}
2843 
2844 			// "Remove element from its parent."
2845 			element.parentNode.removeChild(element);
2846 
2847 			// "Return children."
2848 			return children;
2849 		}
2850 
2851 		// "If command is "strikethrough", and element has a style attribute that
2852 		// sets "text-decoration" to some value containing "line-through", delete
2853 		// "line-through" from the value."
2854 		if (command == "strikethrough" && element.style.textDecoration.indexOf("line-through") != -1) {
2855 			if (element.style.textDecoration == "line-through") {
2856 				element.style.textDecoration = "";
2857 			} else {
2858 				element.style.textDecoration = element.style.textDecoration.replace("line-through", "");
2859 			}
2860 			if (element.getAttribute("style") == "") {
2861 				element.removeAttribute("style");
2862 			}
2863 		}
2864 
2865 		// "If command is "underline", and element has a style attribute that sets
2866 		// "text-decoration" to some value containing "underline", delete
2867 		// "underline" from the value."
2868 		if (command == "underline" && element.style.textDecoration.indexOf("underline") != -1) {
2869 			if (element.style.textDecoration == "underline") {
2870 				element.style.textDecoration = "";
2871 			} else {
2872 				element.style.textDecoration = element.style.textDecoration.replace("underline", "");
2873 			}
2874 			if (element.getAttribute("style") == "") {
2875 				element.removeAttribute("style");
2876 			}
2877 		}
2878 
2879 		// "If the relevant CSS property for command is not null, unset the CSS
2880 		// property property of element."
2881 		if (commands[command].relevantCssProperty !== null) {
2882 			element.style[commands[command].relevantCssProperty] = '';
2883 			if (element.getAttribute("style") == "") {
2884 				element.removeAttribute("style");
2885 			}
2886 		}
2887 
2888 		// "If element is a font element:"
2889 		if (isHtmlNamespace(element.namespaceURI) && element.tagName == "FONT") {
2890 			// "If command is "foreColor", unset element's color attribute, if set."
2891 			if (command == "forecolor") {
2892 				element.removeAttribute("color");
2893 			}
2894 
2895 			// "If command is "fontName", unset element's face attribute, if set."
2896 			if (command == "fontname") {
2897 				element.removeAttribute("face");
2898 			}
2899 
2900 			// "If command is "fontSize", unset element's size attribute, if set."
2901 			if (command == "fontsize") {
2902 				element.removeAttribute("size");
2903 			}
2904 		}
2905 
2906 		// "If element is an a element and command is "createLink" or "unlink",
2907 2908 		// unset the href property of element."
2909 		if (isNamedHtmlElement(element, 'A') && (command == "createlink" || command == "unlink")) {
2910 			element.removeAttribute("href");
2911 		}
2912 
2913 		// "If element's specified command value for command is null, return the
2914 		// empty list."
2915 		if (getSpecifiedCommandValue(element, command) === null) {
2916 			return [];
2917 		}
2918 
2919 		// "Set the tag name of element to "span", and return the one-node list
2920 		// consisting of the result."
2921 		return [setTagName(element, "span", range)];
2922 	}
2923 
2924 	//@}
2925 	///// Forcing the value of a node /////
2926 	//@{
2927 
2928 	function forceValue(node, command, newValue, range) {
2929 		var children = [];
2930 		var i;
2931 		var specifiedValue;
2932 
2933 		// "If node's parent is null, abort this algorithm."
2934 		if (!node.parentNode) {
2935 			return;
2936 		}
2937 
2938 		// "If new value is null, abort this algorithm."
2939 		if (newValue === null) {
2940 2941 			return;
2942 		}
2943 
2944 		// "If node is an allowed child of "span":"
2945 		if (isAllowedChild(node, "span")) {
2946 			// "Reorder modifiable descendants of node's previousSibling."
2947 			reorderModifiableDescendants(node.previousSibling, command, newValue, range);
2948 
2949 			// "Reorder modifiable descendants of node's nextSibling."
2950 			reorderModifiableDescendants(node.nextSibling, command, newValue, range);
2951 
2952 			// "Wrap the one-node list consisting of node, with sibling criteria
2953 			// returning true for a simple modifiable element whose specified
2954 			// command value is equivalent to new value and whose effective command
2955 			// value is loosely equivalent to new value and false otherwise, and
2956 			// with new parent instructions returning null."
2957 			wrap(
2958 				[node],
2959 				function (node) {
2960 					return isSimpleModifiableElement(node) && areEquivalentValues(command, getSpecifiedCommandValue(node, command), newValue) && areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue);
2961 				},
2962 				function () {
2963 					return null;
2964 				},
2965 				range
2966 			);
2967 		}
2968 
2969 		// "If the effective command value of command is loosely equivalent to new
2970 		// value on node, abort this algorithm."
2971 		if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {
2972 			return;
2973 		}
2974 
2975 		// "If node is not an allowed child of "span":"
2976 		if (!isAllowedChild(node, "span")) {
2977 			// "Let children be all children of node, omitting any that are
2978 			// Elements whose specified command value for command is neither null
2979 			// nor equivalent to new value."
2980 			for (i = 0; i < node.childNodes.length; i++) {
2981 				if (node.childNodes[i].nodeType == $_.Node.ELEMENT_NODE) {
2982 					specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command);
2983 
2984 					if (specifiedValue !== null && !areEquivalentValues(command, newValue, specifiedValue)) {
2985 						continue;
2986 					}
2987 				}
2988 				children.push(node.childNodes[i]);
2989 			}
2990 
2991 			// "Force the value of each Node in children, with command and new
2992 			// value as in this invocation of the algorithm."
2993 			for (i = 0; i < children.length; i++) {
2994 				forceValue(children[i], command, newValue, range);
2995 			}
2996 
2997 			// "Abort this algorithm."
2998 			return;
2999 		}
3000 
3001 		// "If the effective command value of command is loosely equivalent to new
3002 		// value on node, abort this algorithm."
3003 		if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {
3004 			return;
3005 		}
3006 
3007 		// "Let new parent be null."
3008 		var newParent = null;
3009 
3010 		// "If the CSS styling flag is false:"
3011 		if (!cssStylingFlag) {
3012 			// "If command is "bold" and new value is "bold", let new parent be the
3013 			// result of calling createElement("b") on the ownerDocument of node."
3014 			if (command == "bold" && (newValue == "bold" || newValue == "700")) {
3015 				newParent = node.ownerDocument.createElement("b");
3016 			}
3017 
3018 			// "If command is "italic" and new value is "italic", let new parent be
3019 			// the result of calling createElement("i") on the ownerDocument of
3020 			// node."
3021 			if (command == "italic" && newValue == "italic") {
3022 				newParent = node.ownerDocument.createElement("i");
3023 			}
3024 
3025 			// "If command is "strikethrough" and new value is "line-through", let
3026 			// new parent be the result of calling createElement("s") on the
3027 			// ownerDocument of node."
3028 			if (command == "strikethrough" && newValue == "line-through") {
3029 				newParent = node.ownerDocument.createElement("s");
3030 			}
3031 
3032 			// "If command is "underline" and new value is "underline", let new
3033 			// parent be the result of calling createElement("u") on the
3034 			// ownerDocument of node."
3035 			if (command == "underline" && newValue == "underline") {
3036 				newParent = node.ownerDocument.createElement("u");
3037 			}
3038 
3039 			// "If command is "foreColor", and new value is fully opaque with red,
3040 			// green, and blue components in the range 0 to 255:"
3041 			if (command == "forecolor" && parseSimpleColor(newValue)) {
3042 				// "Let new parent be the result of calling createElement("span")
3043 				// on the ownerDocument of node."
3044 				// NOTE: modified this process to create span elements with style attributes
3045 				// instead of oldschool font tags with color attributes
3046 				newParent = node.ownerDocument.createElement("span");
3047 
3048 				// "If new value is an extended color keyword, set the color
3049 				// attribute of new parent to new value."
3050 				//
3051 				// "Otherwise, set the color attribute of new parent to the result
3052 				// of applying the rules for serializing simple color values to new
3053 				// value (interpreted as a simple color)."
3054 				jQuery(newParent).css('color', parseSimpleColor(newValue));
3055 			}
3056 
3057 			// "If command is "fontName", let new parent be the result of calling
3058 			// createElement("font") on the ownerDocument of node, then set the
3059 			// face attribute of new parent to new value."
3060 			if (command == "fontname") {
3061 				newParent = node.ownerDocument.createElement("font");
3062 				newParent.face = newValue;
3063 			}
3064 		}
3065 
3066 		// "If command is "createLink" or "unlink":"
3067 		if (command == "createlink" || command == "unlink") {
3068 			// "Let new parent be the result of calling createElement("a") on the
3069 			// ownerDocument of node."
3070 			newParent = node.ownerDocument.createElement("a");
3071 
3072 			// "Set the href attribute of new parent to new value."
3073 			newParent.setAttribute("href", newValue);
3074 
3075 			// "Let ancestor be node's parent."
3076 			var ancestor = node.parentNode;
3077 
3078 			// "While ancestor is not null:"
3079 			while (ancestor) {
3080 				// "If ancestor is an a, set the tag name of ancestor to "span",
3081 				// and let ancestor be the result."
3082 				if (isNamedHtmlElement(ancestor, 'A')) {
3083 					ancestor = setTagName(ancestor, "span", range);
3084 				}
3085 
3086 				// "Set ancestor to its parent."
3087 				ancestor = ancestor.parentNode;
3088 			}
3089 		}
3090 
3091 		// "If command is "fontSize"; and new value is one of "xx-small", "small",
3092 		// "medium", "large", "x-large", "xx-large", or "xxx-large"; and either the
3093 		// CSS styling flag is false, or new value is "xxx-large": let new parent
3094 		// be the result of calling createElement("font") on the ownerDocument of
3095 		// node, then set the size attribute of new parent to the number from the
3096 		// following table based on new value: [table omitted]"
3097 		if (command == "fontsize" && jQuery.inArray(newValue, ["xx-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]) != -1 && (!cssStylingFlag || newValue == "xxx-large")) {
3098 			newParent = node.ownerDocument.createElement("font");
3099 			newParent.size = cssSizeToLegacy(newValue);
3100 		}
3101 
3102 		// "If command is "subscript" or "superscript" and new value is
3103 		// "subscript", let new parent be the result of calling
3104 		// createElement("sub") on the ownerDocument of node."
3105 		if ((command == "subscript" || command == "superscript") && newValue == "subscript") {
3106 			newParent = node.ownerDocument.createElement("sub");
3107 		}
3108 
3109 		// "If command is "subscript" or "superscript" and new value is
3110 		// "superscript", let new parent be the result of calling
3111 		// createElement("sup") on the ownerDocument of node."
3112 		if ((command == "subscript" || command == "superscript") && newValue == "superscript") {
3113 			newParent = node.ownerDocument.createElement("sup");
3114 		}
3115 
3116 		// "If new parent is null, let new parent be the result of calling
3117 		// createElement("span") on the ownerDocument of node."
3118 		if (!newParent) {
3119 			newParent = node.ownerDocument.createElement("span");
3120 		}
3121 
3122 		// "Insert new parent in node's parent before node."
3123 		node.parentNode.insertBefore(newParent, node);
3124 
3125 		// "If the effective command value of command for new parent is not loosely
3126 		// equivalent to new value, and the relevant CSS property for command is
3127 		// not null, set that CSS property of new parent to new value (if the new
3128 		// value would be valid)."
3129 		var property = commands[command].relevantCssProperty;
3130 		if (property !== null && !areLooselyEquivalentValues(command, getEffectiveCommandValue(newParent, command), newValue)) {
3131 			newParent.style[property] = newValue;
3132 		}
3133 
3134 		// "If command is "strikethrough", and new value is "line-through", and the
3135 		// effective command value of "strikethrough" for new parent is not
3136 		// "line-through", set the "text-decoration" property of new parent to
3137 		// "line-through"."
3138 		if (command == "strikethrough" && newValue == "line-through" && getEffectiveCommandValue(newParent, "strikethrough") != "line-through") {
3139 			newParent.style.textDecoration = "line-through";
3140 		}
3141 
3142 		// "If command is "underline", and new value is "underline", and the
3143 		// effective command value of "underline" for new parent is not
3144 		// "underline", set the "text-decoration" property of new parent to
3145 		// "underline"."
3146 		if (command == "underline" && newValue == "underline" && getEffectiveCommandValue(newParent, "underline") != "underline") {
3147 			newParent.style.textDecoration = "underline";
3148 		}
3149 
3150 		// "Append node to new parent as its last child, preserving ranges."
3151 		movePreservingRanges(node, newParent, newParent.childNodes.length, range);
3152 
3153 		// "If node is an Element and the effective command value of command for
3154 		// node is not loosely equivalent to new value:"
3155 		if (node.nodeType == $_.Node.ELEMENT_NODE && !areEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {
3156 			// "Insert node into the parent of new parent before new parent,
3157 			// preserving ranges."
3158 			movePreservingRanges(node, newParent.parentNode, getNodeIndex(newParent), range);
3159 
3160 			// "Remove new parent from its parent."
3161 			newParent.parentNode.removeChild(newParent);
3162 
3163 			// "Let children be all children of node, omitting any that are
3164 			// Elements whose specified command value for command is neither null
3165 			// nor equivalent to new value."
3166 			children = [];
3167 			for (i = 0; i < node.childNodes.length; i++) {
3168 				if (node.childNodes[i].nodeType == $_.Node.ELEMENT_NODE) {
3169 					specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command);
3170 
3171 					if (specifiedValue !== null && !areEquivalentValues(command, newValue, specifiedValue)) {
3172 						continue;
3173 					}
3174 				}
3175 				children.push(node.childNodes[i]);
3176 			}
3177 
3178 			// "Force the value of each Node in children, with command and new
3179 			// value as in this invocation of the algorithm."
3180 			for (i = 0; i < children.length; i++) {
3181 				forceValue(children[i], command, newValue, range);
3182 			}
3183 		}
3184 	}
3185 
3186 	//@}
3187 	///// Pushing down values /////
3188 	//@{
3189 
3190 	function pushDownValues(node, command, newValue, range) {
3191 		// "If node's parent is not an Element, abort this algorithm."
3192 		if (!node.parentNode || node.parentNode.nodeType != $_.Node.ELEMENT_NODE) {
3193 			return;
3194 		}
3195 
3196 		// "If the effective command value of command is loosely equivalent to new
3197 		// value on node, abort this algorithm."
3198 		if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {
3199 			return;
3200 		}
3201 
3202 		// "Let current ancestor be node's parent."
3203 		var currentAncestor = node.parentNode;
3204 
3205 		// "Let ancestor list be a list of Nodes, initially empty."
3206 		var ancestorList = [];
3207 
3208 		// "While current ancestor is an editable Element and the effective command
3209 		// value of command is not loosely equivalent to new value on it, append
3210 		// current ancestor to ancestor list, then set current ancestor to its
3211 		// parent."
3212 		while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !areLooselyEquivalentValues(command, getEffectiveCommandValue(currentAncestor, command), newValue)) {
3213 			ancestorList.push(currentAncestor);
3214 			currentAncestor = currentAncestor.parentNode;
3215 		}
3216 
3217 		// "If ancestor list is empty, abort this algorithm."
3218 		if (!ancestorList.length) {
3219 			return;
3220 		}
3221 
3222 		// "Let propagated value be the specified command value of command on the
3223 		// last member of ancestor list."
3224 		var propagatedValue = getSpecifiedCommandValue(ancestorList[ancestorList.length - 1], command);
3225 
3226 		// "If propagated value is null and is not equal to new value, abort this
3227 		// algorithm."
3228 		if (propagatedValue === null && propagatedValue != newValue) {
3229 			return;
3230 		}
3231 
3232 		// "If the effective command value for the parent of the last member of
3233 		// ancestor list is not loosely equivalent to new value, and new value is
3234 		// not null, abort this algorithm."
3235 		if (newValue !== null && !areLooselyEquivalentValues(command, getEffectiveCommandValue(ancestorList[ancestorList.length - 1].parentNode, command), newValue)) {
3236 			return;
3237 		}
3238 
3239 		// "While ancestor list is not empty:"
3240 		while (ancestorList.length) {
3241 			// "Let current ancestor be the last member of ancestor list."
3242 			// "Remove the last member from ancestor list."
3243 			currentAncestor = ancestorList.pop();
3244 
3245 			// "If the specified command value of current ancestor for command is
3246 			// not null, set propagated value to that value."
3247 			if (getSpecifiedCommandValue(currentAncestor, command) !== null) {
3248 				propagatedValue = getSpecifiedCommandValue(currentAncestor, command);
3249 			}
3250 
3251 			// "Let children be the children of current ancestor."
3252 			var children = Array.prototype.slice.call(toArray(currentAncestor.childNodes));
3253 
3254 			// "If the specified command value of current ancestor for command is
3255 			// not null, clear the value of current ancestor."
3256 			if (getSpecifiedCommandValue(currentAncestor, command) !== null) {
3257 				clearValue(currentAncestor, command, range);
3258 			}
3259 
3260 			// "For every child in children:"
3261 			var i;
3262 			for (i = 0; i < children.length; i++) {
3263 				var child = children[i];
3264 
3265 				// "If child is node, continue with the next child."
3266 				if (child == node) {
3267 					continue;
3268 				}
3269 
3270 				// "If child is an Element whose specified command value for
3271 				// command is neither null nor equivalent to propagated value,
3272 				// continue with the next child."
3273 				if (child.nodeType == $_.Node.ELEMENT_NODE && getSpecifiedCommandValue(child, command) !== null && !areEquivalentValues(command, propagatedValue, getSpecifiedCommandValue(child, command))) {
3274 					continue;
3275 				}
3276 
3277 				// "If child is the last member of ancestor list, continue with the
3278 				// next child."
3279 				if (child == ancestorList[ancestorList.length - 1]) {
3280 					continue;
3281 				}
3282 
3283 				// "Force the value of child, with command as in this algorithm
3284 				// and new value equal to propagated value."
3285 				forceValue(child, command, propagatedValue, range);
3286 			}
3287 		}
3288 	}
3289 
3290 	function restoreValues(values, range) {
3291 		// "For each (node, command, value) triple in values:"
3292 		$_(values).forEach(function (triple) {
3293 			var node = triple[0];
3294 			var command = triple[1];
3295 			var value = triple[2];
3296 
3297 			// "Let ancestor equal node."
3298 			var ancestor = node;
3299 
3300 			// "If ancestor is not an Element, set it to its parent."
3301 			if (!ancestor || ancestor.nodeType != $_.Node.ELEMENT_NODE) {
3302 				ancestor = ancestor.parentNode;
3303 			}
3304 
3305 			// "While ancestor is an Element and its specified command value for
3306 			// command is null, set it to its parent."
3307 			while (ancestor && ancestor.nodeType == $_.Node.ELEMENT_NODE && getSpecifiedCommandValue(ancestor, command) === null) {
3308 				ancestor = ancestor.parentNode;
3309 			}
3310 
3311 			// "If value is null and ancestor is an Element, push down values on
3312 			// node for command, with new value null."
3313 			if (value === null && ancestor && ancestor.nodeType == $_.Node.ELEMENT_NODE) {
3314 				pushDownValues(node, command, null, range);
3315 
3316 				// "Otherwise, if ancestor is an Element and its specified command
3317 				// value for command is not equivalent to value, or if ancestor is not
3318 				// an Element and value is not null, force the value of command to
3319 				// value on node."
3320 			} else if ((ancestor && ancestor.nodeType == $_.Node.ELEMENT_NODE && !areEquivalentValues(command, getSpecifiedCommandValue(ancestor, command), value)) || ((!ancestor || ancestor.nodeType != $_.Node.ELEMENT_NODE) && value !== null)) {
3321 				forceValue(node, command, value, range);
3322 			}
3323 		});
3324 	}
3325 
3326 	//@}
3327 	///// Setting the selection's value /////
3328 	//@{
3329 
3330 	function setSelectionValue(command, newValue, range) {
3331 
3332 		// Use current selected range if no range passed
3333 		range = range || getActiveRange();
3334 
3335 		// "If there is no editable text node effectively contained in the active
3336 		// range:"
3337 		if (!$_(getAllEffectivelyContainedNodes(range)).filter(function (node) { return node.nodeType == $_.Node.TEXT_NODE; }, true).some(isEditable)) {
3338 			// "If command has inline command activated values, set the state
3339 			// override to true if new value is among them and false if it's not."
3340 			if (commands[command].hasOwnProperty("inlineCommandActivatedValues")) {
3341 				setStateOverride(
3342 					command,
3343 					$_(commands[command].inlineCommandActivatedValues).indexOf(newValue) != -1,
3344 					range
3345 				);
3346 3347 			}
3348 
3349 			// "If command is "subscript", unset the state override for
3350 			// "superscript"."
3351 			if (command == "subscript") {
3352 				unsetStateOverride("superscript", range);
3353 			}
3354 
3355 			// "If command is "superscript", unset the state override for
3356 			// "subscript"."
3357 			if (command == "superscript") {
3358 				unsetStateOverride("subscript", range);
3359 			}
3360 
3361 			// "If new value is null, unset the value override (if any)."
3362 			if (newValue === null) {
3363 				unsetValueOverride(command, range);
3364 
3365 				// "Otherwise, if command has a value specified, set the value override
3366 				// to new value."
3367 			} else if (commands[command].hasOwnProperty("value")) {
3368 				setValueOverride(command, newValue, range);
3369 			}
3370 
3371 			// "Abort these steps."
3372 			return;
3373 		}
3374 
3375 		// "If the active range's start node is an editable Text node, and its
3376 		// start offset is neither zero nor its start node's length, call
3377 		// splitText() on the active range's start node, with argument equal to the
3378 		// active range's start offset. Then set the active range's start node to
3379 		// the result, and its start offset to zero."
3380 		if (isEditable(range.startContainer) && range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset != 0 && range.startOffset != getNodeLength(range.startContainer)) {
3381 			// Account for browsers not following range mutation rules
3382 			var newNode = range.startContainer.splitText(range.startOffset);
3383 			var newActiveRange = Aloha.createRange();
3384 			if (range.startContainer == range.endContainer) {
3385 				var newEndOffset = range.endOffset - range.startOffset;
3386 				newActiveRange.setEnd(newNode, newEndOffset);
3387 				range.setEnd(newNode, newEndOffset);
3388 			}
3389 			newActiveRange.setStart(newNode, 0);
3390 			Aloha.getSelection().removeAllRanges();
3391 			Aloha.getSelection().addRange(newActiveRange);
3392 
3393 			range.setStart(newNode, 0);
3394 		}
3395 
3396 		// "If the active range's end node is an editable Text node, and its end
3397 		// offset is neither zero nor its end node's length, call splitText() on
3398 		// the active range's end node, with argument equal to the active range's
3399 		// end offset."
3400 		if (isEditable(range.endContainer) && range.endContainer.nodeType == $_.Node.TEXT_NODE && range.endOffset != 0 && range.endOffset != getNodeLength(range.endContainer)) {
3401 			// IE seems to mutate the range incorrectly here, so we need correction
3402 			// here as well.  The active range will be temporarily in orphaned
3403 			// nodes, so calling getActiveRange() after splitText() but before
3404 			// fixing the range will throw an exception.
3405 			// TODO: check if this is still neccessary 
3406 			var activeRange = range;
3407 			var newStart = [activeRange.startContainer, activeRange.startOffset];
3408 			var newEnd = [activeRange.endContainer, activeRange.endOffset];
3409 			activeRange.endContainer.splitText(activeRange.endOffset);
3410 			activeRange.setStart(newStart[0], newStart[1]);
3411 			activeRange.setEnd(newEnd[0], newEnd[1]);
3412 
3413 			Aloha.getSelection().removeAllRanges();
3414 			Aloha.getSelection().addRange(activeRange);
3415 		}
3416 
3417 		// "Let element list be all editable Elements effectively contained in the
3418 		// active range.
3419 		//
3420 		// "For each element in element list, clear the value of element."
3421 		$_(getAllEffectivelyContainedNodes(getActiveRange(), function (node) {
3422 			return isEditable(node) && node.nodeType == $_.Node.ELEMENT_NODE;
3423 		})).forEach(function (element) {
3424 			clearValue(element, command, range);
3425 		});
3426 
3427 		// "Let node list be all editable nodes effectively contained in the active
3428 		// range.
3429 		//
3430 		// "For each node in node list:"
3431 		$_(getAllEffectivelyContainedNodes(range, isEditable)).forEach(function (node) {
3432 			// "Push down values on node."
3433 			pushDownValues(node, command, newValue, range);
3434 
3435 			// "Force the value of node."
3436 			forceValue(node, command, newValue, range);
3437 		});
3438 	}
3439 
3440 	/**
3441 	 * attempt to retrieve a block like a table or an Aloha Block
3442 	 * which is located one step right of the current caret position.
3443 	 * If an appropriate element is found it will be returned or
3444 	 * false otherwise
3445 	 * 
3446 	 * @param {element} node current node we're in
3447 3448 	 * @param {number} offset current offset within that node
3449 	 * 
3450 	 * @return the dom node if found or false if no appropriate
3451 	 * element was found
3452 	 */
3453 	function getBlockAtNextPosition(node, offset) {
3454 		var i;
3455 
3456 		// if we're inside a text node we first have to check
3457 		// if there is nothing but tabs, newlines or the like
3458 		// after our current cursor position
3459 		if (node.nodeType === $_.Node.TEXT_NODE && offset < node.length) {
3460 			for (i = offset; i < node.length; i++) {
3461 				if ((node.data.charAt(i) !== '\t' && node.data.charAt(i) !== '\r' && node.data.charAt(i) !== '\n') || node.data.charCodeAt(i) === 160) { //  
3462 					// this is a character that has to be deleted first
3463 					return false;
3464 				}
3465 			}
3466 		}
3467 
3468 		// try the most simple approach first: the next sibling
3469 		// is a table
3470 		if (node.nextSibling && node.nextSibling.className && node.nextSibling.className.indexOf("aloha-table-wrapper") >= 0) {
3471 			return node.nextSibling;
3472 		}
3473 
3474 		// since we got only ignorable whitespace here determine if
3475 		// our nodes parents next sibling is a table
3476 		if (node.parentNode && node.parentNode.nextSibling && node.parentNode.nextSibling.className && node.parentNode.nextSibling.className.indexOf("aloha-table-wrapper") >= 0) {
3477 			return node.parentNode.nextSibling;
3478 		}
3479 
3480 		// our parents nextsibling is a pure whitespace node such as
3481 		// generated by sourcecode indentation so we'll check for
3482 		// the next next sibling
3483 		if (node.parentNode && node.parentNode.nextSibling && isWhitespaceNode(node.parentNode.nextSibling) && node.parentNode.nextSibling.nextSibling && node.parentNode.nextSibling.nextSibling.className && node.parentNode.nextSibling.nextSibling.className.indexOf("aloha-table-wrapper") >= 0) {
3484 			return node.parentNode.nextSibling.nextSibling;
3485 		}
3486 
3487 		// Note: the search above works for tables, since they cannot be
3488 		// nested deeply in paragraphs and other formatting tags. If this code
3489 		// is extended to work also for other blocks, the search probably needs to be adapted
3490 	}
3491 
3492 	/**
3493 	 * Attempt to retrieve a block like a table or an Aloha Block
3494 	 * which is located right before the current position.
3495 	 * If an appropriate element is found, it will be returned or
3496 	 * false otherwise
3497 	 * 
3498 	 * @param {element} node current node
3499 	 * @param {offset} offset current offset
3500 	 * 
3501 	 * @return dom node of found or false if no appropriate
3502 	 * element was found
3503 	 */
3504 	function getBlockAtPreviousPosition(node, offset) {
3505 		var i;
3506 
3507 		if (node.nodeType === $_.Node.TEXT_NODE && offset > 0) {
3508 			for (i = offset - 1; i >= 0; i--) {
3509 				if ((node.data.charAt(i) !== '\t' && node.data.charAt(i) !== '\r' && node.data.charAt(i) !== '\n') || node.data.charCodeAt(i) === 160) { //  
3510 					// this is a character that has to be deleted first
3511 					return false;
3512 				}
3513 			}
3514 		}
3515 
3516 		// try the previous sibling
3517 		if (node.previousSibling && node.previousSibling.className && node.previousSibling.className.indexOf("aloha-table-wrapper") >= 0) {
3518 			return node.previousSibling;
3519 		}
3520 
3521 		// try the parent's previous sibling
3522 		if (node.parentNode && node.parentNode.previousSibling && node.parentNode.previousSibling.className && node.parentNode.previousSibling.className.indexOf("aloha-table-wrapper") >= 0) {
3523 			return node.parentNode.previousSibling;
3524 		}
3525 
3526 		// the parent's previous sibling might be a whitespace node
3527 		if (node.parentNode && node.parentNode.previousSibling && isWhitespaceNode(node.parentNode.previousSibling) && node.parentNode.previousSibling.previousSibling && node.parentNode.previousSibling.previousSibling.className && node.parentNode.previousSibling.previousSibling.className.indexOf('aloha-table-wrapper') >= 0) {
3528 			return node.parentNode.previousSibling.previousSibling;
3529 		}
3530 
3531 		// Note: the search above works for tables, since they cannot be
3532 		// nested deeply in paragraphs and other formatting tags. If this code
3533 		// is extended to work also for other blocks, the search probably needs to be adapted
3534 
3535 		return false;
3536 	}
3537 
3538 	// "A boundary point (node, offset) is a block start point if either node's
3539 	// parent is null and offset is zero; or node has a child with index offset −
3540 	// 1, and that child is either a visible block node or a visible br."
3541 	function isBlockStartPoint(node, offset) {
3542 		return (!node.parentNode && offset == 0) || (0 <= offset - 1 && offset - 1 < node.childNodes.length && isVisible(node.childNodes[offset - 1]) && (isBlockNode(node.childNodes[offset - 1]) || isNamedHtmlElement(node.childNodes[offset - 1], "br")));
3543 	}
3544 
3545 	// "A boundary point (node, offset) is a block end point if either node's
3546 	// parent is null and offset is node's length; or node has a child with index
3547 	// offset, and that child is a visible block node."
3548 	function isBlockEndPoint(node, offset) {
3549 		return (!node.parentNode && offset == getNodeLength(node)) || (offset < node.childNodes.length && isVisible(node.childNodes[offset]) && isBlockNode(node.childNodes[offset]));
3550 	}
3551 
3552 	// "A boundary point is a block boundary point if it is either a block start
3553 	// point or a block end point."
3554 	function isBlockBoundaryPoint(node, offset) {
3555 		return isBlockStartPoint(node, offset) || isBlockEndPoint(node, offset);
3556 	}
3557 
3558 	function followsLineBreak(node) {
3559 		// "Let offset be zero."
3560 		var offset = 0;
3561 
3562 		// "While (node, offset) is not a block boundary point:"
3563 		while (!isBlockBoundaryPoint(node, offset)) {
3564 			// "If node has a visible child with index offset minus one, return
3565 			// false."
3566 			if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isVisible(node.childNodes[offset - 1])) {
3567 				return false;
3568 			}
3569 
3570 			// "If offset is zero or node has no children, set offset to node's
3571 			// index, then set node to its parent."
3572 			if (offset == 0 || !node.hasChildNodes()) {
3573 				offset = getNodeIndex(node);
3574 				node = node.parentNode;
3575 
3576 				// "Otherwise, set node to its child with index offset minus one, then
3577 				// set offset to node's length."
3578 			} else {
3579 				node = node.childNodes[offset - 1];
3580 				offset = getNodeLength(node);
3581 			}
3582 		}
3583 
3584 		// "Return true."
3585 		return true;
3586 	}
3587 
3588 	function precedesLineBreak(node) {
3589 		// "Let offset be node's length."
3590 		var offset = getNodeLength(node);
3591 
3592 		// "While (node, offset) is not a block boundary point:"
3593 		while (!isBlockBoundaryPoint(node, offset)) {
3594 			// "If node has a visible child with index offset, return false."
3595 			if (offset < node.childNodes.length && isVisible(node.childNodes[offset])) {
3596 				return false;
3597 			}
3598 
3599 			// "If offset is node's length or node has no children, set offset to
3600 			// one plus node's index, then set node to its parent."
3601 			if (offset == getNodeLength(node) || !node.hasChildNodes()) {
3602 				offset = 1 + getNodeIndex(node);
3603 				node = node.parentNode;
3604 
3605 				// "Otherwise, set node to its child with index offset and set offset
3606 				// to zero."
3607 			} else {
3608 				node = node.childNodes[offset];
3609 				offset = 0;
3610 			}
3611 		}
3612 
3613 		// "Return true."
3614 		return true;
3615 	}
3616 
3617 	//@}
3618 	///// Splitting a node list's parent /////
3619 	//@{
3620 
3621 	function splitParent(nodeList, range) {
3622 		var i;
3623 
3624 		// "Let original parent be the parent of the first member of node list."
3625 		var originalParent = nodeList[0].parentNode;
3626 
3627 		// "If original parent is not editable or its parent is null, do nothing
3628 		// and abort these steps."
3629 		if (!isEditable(originalParent) || !originalParent.parentNode) {
3630 			return;
3631 		}
3632 
3633 		// "If the first child of original parent is in node list, remove
3634 		// extraneous line breaks before original parent."
3635 		if (jQuery.inArray(originalParent.firstChild, nodeList) != -1) {
3636 			removeExtraneousLineBreaksBefore(originalParent);
3637 		}
3638 
3639 		var firstChildInNodeList = jQuery.inArray(originalParent.firstChild, nodeList) != -1;
3640 		var lastChildInNodeList = jQuery.inArray(originalParent.lastChild, nodeList) != -1;
3641 
3642 		// "If the first child of original parent is in node list, and original
3643 		// parent follows a line break, set follows line break to true. Otherwise,
3644 		// set follows line break to false."
3645 		var followsLineBreak_ = firstChildInNodeList && followsLineBreak(originalParent);
3646 
3647 		// "If the last child of original parent is in node list, and original
3648 		// parent precedes a line break, set precedes line break to true.
3649 		// Otherwise, set precedes line break to false."
3650 		var precedesLineBreak_ = lastChildInNodeList && precedesLineBreak(originalParent);
3651 
3652 		// "If the first child of original parent is not in node list, but its last
3653 		// child is:"
3654 		if (!firstChildInNodeList && lastChildInNodeList) {
3655 			// "For each node in node list, in reverse order, insert node into the
3656 			// parent of original parent immediately after original parent,
3657 			// preserving ranges."
3658 			for (i = nodeList.length - 1; i >= 0; i--) {
3659 				movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent), range);
3660 			}
3661 
3662 3663 			// "If precedes line break is true, and the last member of node list
3664 			// does not precede a line break, call createElement("br") on the
3665 			// context object and insert the result immediately after the last
3666 			// member of node list."
3667 			if (precedesLineBreak_ && !precedesLineBreak(nodeList[nodeList.length - 1])) {
3668 				nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling);
3669 			}
3670 
3671 			// "Remove extraneous line breaks at the end of original parent."
3672 			removeExtraneousLineBreaksAtTheEndOf(originalParent);
3673 
3674 			// "Abort these steps."
3675 			return;
3676 		}
3677 
3678 		// "If the first child of original parent is not in node list:"
3679 		if (!firstChildInNodeList) {
3680 			// "Let cloned parent be the result of calling cloneNode(false) on
3681 			// original parent."
3682 			var clonedParent = originalParent.cloneNode(false);
3683 
3684 			// "If original parent has an id attribute, unset it."
3685 			originalParent.removeAttribute("id");
3686 
3687 			// "Insert cloned parent into the parent of original parent immediately
3688 			// before original parent."
3689 			originalParent.parentNode.insertBefore(clonedParent, originalParent);
3690 
3691 			// "While the previousSibling of the first member of node list is not
3692 			// null, append the first child of original parent as the last child of
3693 			// cloned parent, preserving ranges."
3694 			while (nodeList[0].previousSibling) {
3695 				movePreservingRanges(originalParent.firstChild, clonedParent, clonedParent.childNodes.length, range);
3696 			}
3697 		}
3698 
3699 		// "For each node in node list, insert node into the parent of original
3700 		// parent immediately before original parent, preserving ranges."
3701 		for (i = 0; i < nodeList.length; i++) {
3702 			movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent), range);
3703 		}
3704 
3705 		// "If follows line break is true, and the first member of node list does
3706 		// not follow a line break, call createElement("br") on the context object
3707 		// and insert the result immediately before the first member of node list."
3708 		if (followsLineBreak_ && !followsLineBreak(nodeList[0])) {
3709 			nodeList[0].parentNode.insertBefore(document.createElement("br"), nodeList[0]);
3710 		}
3711 
3712 		// "If the last member of node list is an inline node other than a br, and
3713 		// the first child of original parent is a br, and original parent is not
3714 		// an inline node, remove the first child of original parent from original
3715 		// parent."
3716 		if (isInlineNode(nodeList[nodeList.length - 1]) && !isNamedHtmlElement(nodeList[nodeList.length - 1], "br") && isNamedHtmlElement(originalParent.firstChild, "br") && !isInlineNode(originalParent)) {
3717 			originalParent.removeChild(originalParent.firstChild);
3718 		}
3719 
3720 		// "If original parent has no children:"
3721 		if (!originalParent.hasChildNodes()) {
3722 			// if the current range is collapsed and at the end of the originalParent.parentNode
3723 			// the offset will not be available anymore after the next step (remove child)
3724 3725 			// that's why we need to fix the range to prevent a bogus offset
3726 			if (originalParent.parentNode === range.startContainer && originalParent.parentNode === range.endContainer && range.startContainer === range.endContainer && range.startOffset === range.endOffset && originalParent.parentNode.childNodes.length === range.startOffset) {
3727 				range.startOffset = originalParent.parentNode.childNodes.length - 1;
3728 				range.endOffset = range.startOffset;
3729 			}
3730 
3731 3732 			// "Remove original parent from its parent."
3733 			originalParent.parentNode.removeChild(originalParent);
3734 
3735 			// "If precedes line break is true, and the last member of node list
3736 			// does not precede a line break, call createElement("br") on the
3737 			// context object and insert the result immediately after the last
3738 			// member of node list."
3739 			if (precedesLineBreak_ && !precedesLineBreak(nodeList[nodeList.length - 1])) {
3740 				nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling);
3741 			}
3742 
3743 			// "Otherwise, remove extraneous line breaks before original parent."
3744 		} else {
3745 			removeExtraneousLineBreaksBefore(originalParent);
3746 		}
3747 
3748 		// "If node list's last member's nextSibling is null, but its parent is not
3749 		// null, remove extraneous line breaks at the end of node list's last
3750 		// member's parent."
3751 		if (!nodeList[nodeList.length - 1].nextSibling && nodeList[nodeList.length - 1].parentNode) {
3752 			removeExtraneousLineBreaksAtTheEndOf(nodeList[nodeList.length - 1].parentNode);
3753 		}
3754 	}
3755 
3756 	//@}
3757 	///// The backColor command /////
3758 	//@{
3759 	commands.backcolor = {
3760 		// Copy-pasted, same as hiliteColor
3761 		action: function (value, range) {
3762 			// Action is further copy-pasted, same as foreColor
3763 
3764 			// "If value is not a valid CSS color, prepend "#" to it."
3765 			//
3766 			// "If value is still not a valid CSS color, or if it is currentColor,
3767 			// abort these steps and do nothing."
3768 			//
3769 			// Cheap hack for testing, no attempt to be comprehensive.
3770 			if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {
3771 				value = "#" + value;
3772 			}
3773 			if (!/^(rgba?|hsla?)\(.*\)$/.test(value) && !parseSimpleColor(value) && value.toLowerCase() != "transparent") {
3774 				return;
3775 			}
3776 
3777 			// "Set the selection's value to value."
3778 			setSelectionValue("backcolor", value, range);
3779 		},
3780 		standardInlineValueCommand: true,
3781 		relevantCssProperty: "backgroundColor",
3782 		equivalentValues: function (val1, val2) {
3783 			// "Either both strings are valid CSS colors and have the same red,
3784 			// green, blue, and alpha components, or neither string is a valid CSS
3785 			// color."
3786 			return normalizeColor(val1) === normalizeColor(val2);
3787 		}
3788 	};
3789 
3790 	//@}
3791 	///// The bold command /////
3792 	//@{
3793 	commands.bold = {
3794 		action: function (value, range) {
3795 			// "If queryCommandState("bold") returns true, set the selection's
3796 			// value to "normal". Otherwise set the selection's value to "bold"."
3797 			if (myQueryCommandState("bold", range)) {
3798 				setSelectionValue("bold", "normal", range);
3799 			} else {
3800 				setSelectionValue("bold", "bold", range);
3801 			}
3802 		},
3803 		inlineCommandActivatedValues: ["bold", "600", "700", "800", "900"],
3804 		relevantCssProperty: "fontWeight",
3805 		equivalentValues: function (val1, val2) {
3806 			// "Either the two strings are equal, or one is "bold" and the other is
3807 			// "700", or one is "normal" and the other is "400"."
3808 			return val1 == val2 || (val1 == "bold" && val2 == "700") || (val1 == "700" && val2 == "bold") || (val1 == "normal" && val2 == "400") || (val1 == "400" && val2 == "normal");
3809 		}
3810 	};
3811 
3812 	//@}
3813 	///// The createLink command /////
3814 	//@{
3815 	commands.createlink = {
3816 		action: function (value, range) {
3817 			// "If value is the empty string, abort these steps and do nothing."
3818 			if (value === "") {
3819 				return;
3820 			}
3821 
3822 			// "For each editable a element that has an href attribute and is an
3823 			// ancestor of some node effectively contained in the active range, set
3824 			// that a element's href attribute to value."
3825 			//
3826 			// TODO: We don't actually do this in tree order, not that it matters
3827 			// unless you're spying with mutation events.
3828 			$_(getAllEffectivelyContainedNodes(getActiveRange())).forEach(function (node) {
3829 				$_(getAncestors(node)).forEach(function (ancestor) {
3830 					if (isEditable(ancestor) && isNamedHtmlElement(ancestor, 'a') && hasAttribute(ancestor, "href")) {
3831 						ancestor.setAttribute("href", value);
3832 					}
3833 				});
3834 			});
3835 
3836 			// "Set the selection's value to value."
3837 			setSelectionValue("createlink", value, range);
3838 		},
3839 		standardInlineValueCommand: true
3840 	};
3841 
3842 	//@}
3843 	///// The fontName command /////
3844 	//@{
3845 	commands.fontname = {
3846 		action: function (value, range) {
3847 			// "Set the selection's value to value."
3848 			setSelectionValue("fontname", value, range);
3849 		},
3850 		standardInlineValueCommand: true,
3851 		relevantCssProperty: "fontFamily"
3852 	};
3853 
3854 	//@}
3855 	///// The fontSize command /////
3856 	//@{
3857 
3858 	commands.fontsize = {
3859 		action: function (value, range) {
3860 			// "If value is the empty string, abort these steps and do nothing."
3861 			if (value === "") {
3862 				return;
3863 			}
3864 
3865 			value = normalizeFontSize(value);
3866 
3867 			// "If value is not one of the strings "xx-small", "x-small", "small",
3868 			// "medium", "large", "x-large", "xx-large", "xxx-large", and is not a
3869 			// valid CSS absolute length, then abort these steps and do nothing."
3870 			//
3871 			// More cheap hacks to skip valid CSS absolute length checks.
3872 			if (jQuery.inArray(value, ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]) == -1 && !/^[0-9]+(\.[0-9]+)?(cm|mm|in|pt|pc)$/.test(value)) {
3873 				return;
3874 			}
3875 
3876 			// "Set the selection's value to value."
3877 			setSelectionValue("fontsize", value, range);
3878 		},
3879 		indeterm: function () {
3880 			// "True if among editable Text nodes that are effectively contained in
3881 			// the active range, there are two that have distinct effective command
3882 			// values.  Otherwise false."
3883 			return $_(getAllEffectivelyContainedNodes(getActiveRange(), function (node) {
3884 				return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
3885 			})).map(function (node) {
3886 				return getEffectiveCommandValue(node, "fontsize");
3887 			}, true).filter(function (value, i, arr) {
3888 				return $_(arr.slice(0, i)).indexOf(value) == -1;
3889 			}).length >= 2;
3890 		},
3891 		value: function (range) {
3892 			// "Let pixel size be the effective command value of the first editable
3893 			// Text node that is effectively contained in the active range, or if
3894 			// there is no such node, the effective command value of the active
3895 			// range's start node, in either case interpreted as a number of
3896 			// pixels."
3897 			var node = getAllEffectivelyContainedNodes(range, function (node) {
3898 				return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
3899 			})[0];
3900 			if (node === undefined) {
3901 				node = range.startContainer;
3902 			}
3903 			var pixelSize = getEffectiveCommandValue(node, "fontsize");
3904 
3905 			// "Return the legacy font size for pixel size."
3906 			return getLegacyFontSize(pixelSize);
3907 		},
3908 		relevantCssProperty: "fontSize"
3909 	};
3910 
3911 	//@}
3912 	///// The foreColor command /////
3913 	//@{
3914 	commands.forecolor = {
3915 		action: function (value, range) {
3916 			// Copy-pasted, same as backColor and hiliteColor
3917 
3918 			// "If value is not a valid CSS color, prepend "#" to it."
3919 			//
3920 			// "If value is still not a valid CSS color, or if it is currentColor,
3921 			// abort these steps and do nothing."
3922 			//
3923 			// Cheap hack for testing, no attempt to be comprehensive.
3924 			if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {
3925 				value = "#" + value;
3926 			}
3927 			if (!/^(rgba?|hsla?)\(.*\)$/.test(value) && !parseSimpleColor(value) && value.toLowerCase() != "transparent") {
3928 				return;
3929 			}
3930 
3931 			// "Set the selection's value to value."
3932 			setSelectionValue("forecolor", value, range);
3933 		},
3934 		standardInlineValueCommand: true,
3935 		relevantCssProperty: "color",
3936 		equivalentValues: function (val1, val2) {
3937 			// "Either both strings are valid CSS colors and have the same red,
3938 			// green, blue, and alpha components, or neither string is a valid CSS
3939 			// color."
3940 			return normalizeColor(val1) === normalizeColor(val2);
3941 		}
3942 	};
3943 
3944 	//@}
3945 	///// The hiliteColor command /////
3946 	//@{
3947 	commands.hilitecolor = {
3948 		// Copy-pasted, same as backColor
3949 		action: function (value, range) {
3950 			// Action is further copy-pasted, same as foreColor
3951 
3952 			// "If value is not a valid CSS color, prepend "#" to it."
3953 			//
3954 			// "If value is still not a valid CSS color, or if it is currentColor,
3955 			// abort these steps and do nothing."
3956 			//
3957 			// Cheap hack for testing, no attempt to be comprehensive.
3958 			if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {
3959 				value = "#" + value;
3960 			}
3961 			if (!/^(rgba?|hsla?)\(.*\)$/.test(value) && !parseSimpleColor(value) && value.toLowerCase() != "transparent") {
3962 				return;
3963 			}
3964 
3965 			// "Set the selection's value to value."
3966 			setSelectionValue("hilitecolor", value, range);
3967 		},
3968 		indeterm: function () {
3969 			// "True if among editable Text nodes that are effectively contained in
3970 			// the active range, there are two that have distinct effective command
3971 			// values.  Otherwise false."
3972 			return $_(getAllEffectivelyContainedNodes(getActiveRange(), function (node) {
3973 				return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
3974 			})).map(function (node) {
3975 				return getEffectiveCommandValue(node, "hilitecolor");
3976 			}, true).filter(function (value, i, arr) {
3977 				return $_(arr.slice(0, i)).indexOf(value) == -1;
3978 			}).length >= 2;
3979 		},
3980 		standardInlineValueCommand: true,
3981 		relevantCssProperty: "backgroundColor",
3982 		equivalentValues: function (val1, val2) {
3983 			// "Either both strings are valid CSS colors and have the same red,
3984 			// green, blue, and alpha components, or neither string is a valid CSS
3985 			// color."
3986 			return normalizeColor(val1) === normalizeColor(val2);
3987 		}
3988 	};
3989 
3990 	//@}
3991 	///// The italic command /////
3992 	//@{
3993 	commands.italic = {
3994 		action: function (value, range) {
3995 			// "If queryCommandState("italic") returns true, set the selection's
3996 			// value to "normal". Otherwise set the selection's value to "italic"."
3997 			if (myQueryCommandState("italic", range)) {
3998 				setSelectionValue("italic", "normal", range);
3999 			} else {
4000 				setSelectionValue("italic", "italic", range);
4001 			}
4002 		},
4003 		inlineCommandActivatedValues: ["italic", "oblique"],
4004 		relevantCssProperty: "fontStyle"
4005 	};
4006 
4007 	//@}
4008 	///// The removeFormat command /////
4009 	//@{
4010 	commands.removeformat = {
4011 		action: function (value, range) {
4012 			var newEnd, newStart, newNode;
4013 
4014 			// "A removeFormat candidate is an editable HTML element with local
4015 			// name "abbr", "acronym", "b", "bdi", "bdo", "big", "blink", "cite",
4016 			// "code", "dfn", "em", "font", "i", "ins", "kbd", "mark", "nobr", "q",
4017 			// "s", "samp", "small", "span", "strike", "strong", "sub", "sup",
4018 			// "tt", "u", or "var"."
4019 			function isRemoveFormatCandidate(node) {
4020 				return isEditable(node) && isHtmlElementInArray(node, ["abbr", "acronym", "b", "bdi", "bdo", "big", "blink", "cite", "code", "dfn", "em", "font", "i", "ins", "kbd", "mark", "nobr", "q", "s", "samp", "small", "span", "strike", "strong", "sub", "sup", "tt", "u", "var"]);
4021 			}
4022 
4023 			// "Let elements to remove be a list of every removeFormat candidate
4024 			// effectively contained in the active range."
4025 			var elementsToRemove = getAllEffectivelyContainedNodes(getActiveRange(), isRemoveFormatCandidate);
4026 
4027 			// "For each element in elements to remove:"
4028 			$_(elementsToRemove).forEach(function (element) {
4029 				// "While element has children, insert the first child of element
4030 				// into the parent of element immediately before element,
4031 				// preserving ranges."
4032 				while (element.hasChildNodes()) {
4033 					movePreservingRanges(element.firstChild, element.parentNode, getNodeIndex(element), getActiveRange());
4034 				}
4035 
4036 				// "Remove element from its parent."
4037 				element.parentNode.removeChild(element);
4038 			});
4039 
4040 			// "If the active range's start node is an editable Text node, and its
4041 			// start offset is neither zero nor its start node's length, call
4042 			// splitText() on the active range's start node, with argument equal to
4043 			// the active range's start offset. Then set the active range's start
4044 			// node to the result, and its start offset to zero."
4045 			if (isEditable(getActiveRange().startContainer) && getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset != 0 && getActiveRange().startOffset != getNodeLength(getActiveRange().startContainer)) {
4046 				// Account for browsers not following range mutation rules
4047 				if (getActiveRange().startContainer == getActiveRange().endContainer) {
4048 					newEnd = getActiveRange().endOffset - getActiveRange().startOffset;
4049 					newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset);
4050 					getActiveRange().setStart(newNode, 0);
4051 					getActiveRange().setEnd(newNode, newEnd);
4052 				} else {
4053 					getActiveRange().setStart(getActiveRange().startContainer.splitText(getActiveRange().startOffset), 0);
4054 				}
4055 			}
4056 
4057 			// "If the active range's end node is an editable Text node, and its
4058 			// end offset is neither zero nor its end node's length, call
4059 			// splitText() on the active range's end node, with argument equal to
4060 			// the active range's end offset."
4061 			if (isEditable(getActiveRange().endContainer) && getActiveRange().endContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().endOffset != 0 && getActiveRange().endOffset != getNodeLength(getActiveRange().endContainer)) {
4062 				// IE seems to mutate the range incorrectly here, so we need
4063 				// correction here as well.  Have to be careful to set the range to
4064 				// something not including the text node so that getActiveRange()
4065 				// doesn't throw an exception due to a temporarily detached
4066 				// endpoint.
4067 				newStart = [getActiveRange().startContainer, getActiveRange().startOffset];
4068 				newEnd = [getActiveRange().endContainer, getActiveRange().endOffset];
4069 				getActiveRange().setEnd(document.documentElement, 0);
4070 				newEnd[0].splitText(newEnd[1]);
4071 				getActiveRange().setStart(newStart[0], newStart[1]);
4072 				getActiveRange().setEnd(newEnd[0], newEnd[1]);
4073 			}
4074 
4075 			// "Let node list consist of all editable nodes effectively contained
4076 			// in the active range."
4077 			//
4078 			// "For each node in node list, while node's parent is a removeFormat
4079 			// candidate in the same editing host as node, split the parent of the
4080 			// one-node list consisting of node."
4081 			$_(getAllEffectivelyContainedNodes(getActiveRange(), isEditable)).forEach(function (node) {
4082 				while (isRemoveFormatCandidate(node.parentNode) && inSameEditingHost(node.parentNode, node)) {
4083 					splitParent([node], getActiveRange());
4084 				}
4085 			});
4086 
4087 			// "For each of the entries in the following list, in the given order,
4088 			// set the selection's value to null, with command as given."
4089 			$_(["subscript", "bold", "fontname", "fontsize", "forecolor", "hilitecolor", "italic", "strikethrough", "underline"]).forEach(function (command) {
4090 				setSelectionValue(command, null, range);
4091 			});
4092 		}
4093 	};
4094 
4095 	//@}
4096 	///// The strikethrough command /////
4097 	//@{
4098 	commands.strikethrough = {
4099 		action: function (value, range) {
4100 			// "If queryCommandState("strikethrough") returns true, set the
4101 			// selection's value to null. Otherwise set the selection's value to
4102 			// "line-through"."
4103 			if (myQueryCommandState("strikethrough", range)) {
4104 				setSelectionValue("strikethrough", null, range);
4105 			} else {
4106 				setSelectionValue("strikethrough", "line-through", range);
4107 			}
4108 		},
4109 		inlineCommandActivatedValues: ["line-through"]
4110 	};
4111 
4112 	//@}
4113 	///// The subscript command /////
4114 	//@{
4115 	commands.subscript = {
4116 		action: function (value, range) {
4117 			// "Call queryCommandState("subscript"), and let state be the result."
4118 			var state = myQueryCommandState("subscript", range);
4119 
4120 			// "Set the selection's value to null."
4121 			setSelectionValue("subscript", null, range);
4122 
4123 			// "If state is false, set the selection's value to "subscript"."
4124 			if (!state) {
4125 				setSelectionValue("subscript", "subscript", range);
4126 			}
4127 		},
4128 		indeterm: function () {
4129 			// "True if either among editable Text nodes that are effectively
4130 			// contained in the active range, there is at least one with effective
4131 			// command value "subscript" and at least one with some other effective
4132 			// command value; or if there is some editable Text node effectively
4133 			// contained in the active range with effective command value "mixed".
4134 			// Otherwise false."
4135 			var nodes = getAllEffectivelyContainedNodes(getActiveRange(), function (node) {
4136 				return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
4137 			});
4138 			return (($_(nodes).some(function (node) { return getEffectiveCommandValue(node, "subscript") == "subscript"; })
4139 					 && $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "subscript") != "subscript"; }))
4140 					|| $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "subscript") == "mixed"; }));
4141 		},
4142 		inlineCommandActivatedValues: ["subscript"]
4143 	};
4144 
4145 	//@}
4146 	///// The superscript command /////
4147 	//@{
4148 	commands.superscript = {
4149 		action: function (value, range) {
4150 			// "Call queryCommandState("superscript"), and let state be the
4151 			// result."
4152 			var state = myQueryCommandState("superscript", range);
4153 
4154 			// "Set the selection's value to null."
4155 			setSelectionValue("superscript", null, range);
4156 
4157 			// "If state is false, set the selection's value to "superscript"."
4158 			if (!state) {
4159 				setSelectionValue("superscript", "superscript", range);
4160 			}
4161 		},
4162 		indeterm: function () {
4163 			// "True if either among editable Text nodes that are effectively
4164 			// contained in the active range, there is at least one with effective
4165 			// command value "superscript" and at least one with some other
4166 			// effective command value; or if there is some editable Text node
4167 			// effectively contained in the active range with effective command
4168 			// value "mixed".  Otherwise false."
4169 			var nodes = getAllEffectivelyContainedNodes(
4170 				getActiveRange(),
4171 				function (node) {
4172 					return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
4173 				}
4174 			);
4175 			return (($_(nodes).some(function (node) { return getEffectiveCommandValue(node, "superscript") == "superscript"; })
4176 					 && $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "superscript") != "superscript"; }))
4177 					|| $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "superscript") == "mixed"; }));
4178 		},
4179 		inlineCommandActivatedValues: ["superscript"]
4180 	};
4181 
4182 	//@}
4183 	///// The underline command /////
4184 	//@{
4185 	commands.underline = {
4186 		action: function (value, range) {
4187 			// "If queryCommandState("underline") returns true, set the selection's
4188 			// value to null. Otherwise set the selection's value to "underline"."
4189 			if (myQueryCommandState("underline", range)) {
4190 4191 				setSelectionValue("underline", null, range);
4192 			} else {
4193 				setSelectionValue("underline", "underline", range);
4194 			}
4195 		},
4196 		inlineCommandActivatedValues: ["underline"]
4197 	};
4198 
4199 	//@}
4200 	///// The unlink command /////
4201 	//@{
4202 	commands.unlink = {
4203 		action: function () {
4204 			// "Let hyperlinks be a list of every a element that has an href
4205 			// attribute and is contained in the active range or is an ancestor of
4206 			// one of its boundary points."
4207 			//
4208 4209 			// As usual, take care to ensure it's tree order.  The correctness of
4210 			// the following is left as an exercise for the reader.
4211 			var range = getActiveRange();
4212 			var hyperlinks = [];
4213 			var node;
4214 			for (node = range.startContainer; node; node = node.parentNode) {
4215 				if (isNamedHtmlElement(node, 'A') && hasAttribute(node, "href")) {
4216 					hyperlinks.unshift(node);
4217 				}
4218 			}
4219 			for (node = range.startContainer; node != nextNodeDescendants(range.endContainer); node = nextNode(node)) {
4220 				if (isNamedHtmlElement(node, 'A') && hasAttribute(node, "href") && (isContained(node, range) || isAncestor(node, range.endContainer) || node == range.endContainer)) {
4221 					hyperlinks.push(node);
4222 				}
4223 			}
4224 
4225 			// "Clear the value of each member of hyperlinks."
4226 			var i;
4227 			for (i = 0; i < hyperlinks.length; i++) {
4228 				clearValue(hyperlinks[i], "unlink", range);
4229 			}
4230 		},
4231 		standardInlineValueCommand: true
4232 	};
4233 
4234 	//@}
4235 
4236 	/////////////////////////////////////
4237 	///// Block formatting commands /////
4238 	/////////////////////////////////////
4239 
4240 	///// Block formatting command definitions /////
4241 	//@{
4242 
4243 	// "An indentation element is either a blockquote, or a div that has a style
4244 	// attribute that sets "margin" or some subproperty of it."
4245 	function isIndentationElement(node) {
4246 		if (!isAnyHtmlElement(node)) {
4247 			return false;
4248 		}
4249 
4250 		if (node.tagName == "BLOCKQUOTE") {
4251 			return true;
4252 		}
4253 
4254 		if (node.tagName != "DIV") {
4255 			return false;
4256 		}
4257 
4258 		if (typeof node.style.length !== 'undefined') {
4259 			var i;
4260 			for (i = 0; i < node.style.length; i++) {
4261 				// Approximate check
4262 				if (/^(-[a-z]+-)?margin/.test(node.style[i])) {
4263 					return true;
4264 				}
4265 			}
4266 		} else {
4267 			var s;
4268 			/*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid
4269 			for (s in node.style) {
4270 				if (/^(-[a-z]+-)?margin/.test(s) && node.style[s] && node.style[s] !== 0) {
4271 					return true;
4272 				}
4273 			}
4274 			/*jslint forin: false*/
4275 		}
4276 
4277 		return false;
4278 	}
4279 
4280 	// "A simple indentation element is an indentation element that has no
4281 	// attributes other than one or more of
4282 	//
4283 	//   * "a style attribute that sets no properties other than "margin", "border",
4284 	//     "padding", or subproperties of those;
4285 	//   * "a class attribute;
4286 	//   * "a dir attribute."
4287 	function isSimpleIndentationElement(node) {
4288 		if (!isIndentationElement(node)) {
4289 			return false;
4290 		}
4291 
4292 		if (node.tagName != "BLOCKQUOTE" && node.tagName != "DIV") {
4293 			return false;
4294 		}
4295 
4296 		var i;
4297 		for (i = 0; i < node.attributes.length; i++) {
4298 			if (!isHtmlNamespace(node.attributes[i].namespaceURI) || jQuery.inArray(node.attributes[i].name, ["style", "class", "dir"]) == -1) {
4299 				return false;
4300 			}
4301 		}
4302 
4303 		if (typeof node.style.length !== 'undefined') {
4304 			for (i = 0; i < node.style.length; i++) {
4305 				// This is approximate, but it works well enough for my purposes.
4306 				if (!/^(-[a-z]+-)?(margin|border|padding)/.test(node.style[i])) {
4307 					return false;
4308 				}
4309 			}
4310 		} else {
4311 			var s;
4312 			/*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid
4313 			for (s in node.style) {
4314 				// This is approximate, but it works well enough for my purposes.
4315 				if (!/^(-[a-z]+-)?(margin|border|padding)/.test(s) && node.style[s] && node.style[s] !== 0 && node.style[s] !== 'false') {
4316 					return false;
4317 				}
4318 			}
4319 			/*jslint forin: false*/
4320 		}
4321 
4322 		return true;
4323 	}
4324 
4325 	// "A non-list single-line container is an HTML element with local name
4326 	// "address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre",
4327 	// or "xmp"."
4328 	function isNonListSingleLineContainer(node) {
4329 		return isHtmlElementInArray(node, ["address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre", "xmp"]);
4330 	}
4331 
4332 	// "A single-line container is either a non-list single-line container, or an
4333 	// HTML element with local name "li", "dt", or "dd"."
4334 	function isSingleLineContainer(node) {
4335 		return isNonListSingleLineContainer(node) || isHtmlElementInArray(node, ["li", "dt", "dd"]);
4336 	}
4337 
4338 	// "The default single-line container name is "p"."
4339 	var defaultSingleLineContainerName = "p";
4340 
4341 	//@}
4342 	///// Check whether the given element is an end break /////
4343 	//@{
4344 	function isEndBreak(element) {
4345 		return (isNamedHtmlElement(element, 'br') && element.parentNode.lastChild === element);
4346 	}
4347 
4348 	//@}
4349 	///// Create an end break /////
4350 	//@{
4351 	function createEndBreak() {
4352 		return document.createElement("br");
4353 	}
4354 
4355 	/**
4356 	 * Ensure the container is editable
4357 	 * E.g. when called for an empty paragraph or header, and the browser is not IE,
4358 	 * we need to append a br (marked with class aloha-end-br)
4359 	 * For IE7, there is a special behaviour that will append zero-width whitespace
4360 	 * @param {DOMNode} container
4361 	 */
4362 	function ensureContainerEditable(container) {
4363 		if (!container) {
4364 			return;
4365 		}
4366 
4367 		if (isNamedHtmlElement(container.lastChild, "br")) {
4368 			return;
4369 		}
4370 
4371 		if ($_(container.childNodes).some(isVisible)) {
4372 			return;
4373 		}
4374 
4375 		if (!jQuery.browser.msie) {
4376 			// for normal browsers, the end-br will do
4377 			container.appendChild(createEndBreak());
4378 		} else if (jQuery.browser.msie && jQuery.browser.version <= 7 && isHtmlElementInArray(container, ["p", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "blockquote"])) {
4379 			// for IE7, we need to insert a text node containing a single zero-width whitespace character
4380 			if (!container.firstChild) {
4381 				container.appendChild(document.createTextNode('\u200b'));
4382 			}
4383 		}
4384 	}
4385 
4386 	//@}
4387 	///// Assorted block formatting command algorithms /////
4388 	//@{
4389 
4390 	function fixDisallowedAncestors(node, range) {
4391 		var i;
4392 
4393 		// "If node is not editable, abort these steps."
4394 		if (!isEditable(node)) {
4395 			return;
4396 		}
4397 
4398 		// "If node is not an allowed child of any of its ancestors in the same
4399 		// editing host, and is not an HTML element with local name equal to the
4400 		// default single-line container name:"
4401 		if ($_(getAncestors(node)).every(function (ancestor) { return !inSameEditingHost(node, ancestor) || !isAllowedChild(node, ancestor); })
4402 			    && !isHtmlElement_obsolete(node, defaultSingleLineContainerName)) {
4403 			// "If node is a dd or dt, wrap the one-node list consisting of node,
4404 			// with sibling criteria returning true for any dl with no attributes
4405 			// and false otherwise, and new parent instructions returning the
4406 			// result of calling createElement("dl") on the context object. Then
4407 			// abort these steps."
4408 			if (isHtmlElementInArray(node, ["dd", "dt"])) {
4409 				wrap(
4410 					[node],
4411 					function (sibling) {
4412 						return isNamedHtmlElement(sibling, 'dl') && !sibling.attributes.length;
4413 					},
4414 					function () {
4415 						return document.createElement("dl");
4416 					},
4417 					range
4418 				);
4419 				return;
4420 			}
4421 
4422 			// "If node is not a prohibited paragraph child, abort these steps."
4423 			if (!isProhibitedParagraphChild(node)) {
4424 				return;
4425 			}
4426 
4427 			// "Set the tag name of node to the default single-line container name,
4428 			// and let node be the result."
4429 			node = setTagName(node, defaultSingleLineContainerName, range);
4430 
4431 			ensureContainerEditable(node);
4432 
4433 			// "Fix disallowed ancestors of node."
4434 			fixDisallowedAncestors(node, range);
4435 
4436 			// "Let descendants be all descendants of node."
4437 			var descendants = getDescendants(node);
4438 
4439 			// "Fix disallowed ancestors of each member of descendants."
4440 			for (i = 0; i < descendants.length; i++) {
4441 				fixDisallowedAncestors(descendants[i], range);
4442 			}
4443 
4444 			// "Abort these steps."
4445 			return;
4446 		}
4447 
4448 		// "Record the values of the one-node list consisting of node, and let
4449 		// values be the result."
4450 		var values = recordValues([node]);
4451 		var newStartOffset, newEndOffset;
4452 
4453 		// "While node is not an allowed child of its parent, split the parent of
4454 		// the one-node list consisting of node."
4455 		while (!isAllowedChild(node, node.parentNode)) {
4456 			// If the parent contains only this node and possibly empty text nodes, we rather want to unwrap the node, instead of splitting.
4457 			// With splitting, we would get empty nodes, like:
4458 			// split: <p><p>foo</p></p> -> <p></p><p>foo</p> (bad)
4459 			// unwrap: <p><p>foo</p></p> -> <p>foo</p> (good)
4460 
4461 			// First remove empty text nodes that are children of the parent and correct the range if necessary
4462 			// we do this to have the node being the only child of its parent, so that we can replace the parent with the node
4463 			for (i = node.parentNode.childNodes.length - 1; i >= 0; --i) {
4464 				if (node.parentNode.childNodes[i].nodeType == 3 && node.parentNode.childNodes[i].data.length == 0) {
4465 					// we remove the empty text node
4466 					node.parentNode.removeChild(node.parentNode.childNodes[i]);
4467 
4468 					// if the range points to somewhere behind the removed text node, we reduce the offset
4469 					if (range.startContainer == node.parentNode && range.startOffset > i) {
4470 						range.startOffset--;
4471 					}
4472 					if (range.endContainer == node.parentNode && range.endOffset > i) {
4473 						range.endOffset--;
4474 					}
4475 				}
4476 			}
4477 
4478 			// now that the parent has only the node as child (because we
4479 			// removed any existing empty text nodes), we can safely unwrap the
4480 			// node's contents, and correct the range if necessary
4481 			if (node.parentNode.childNodes.length == 1) {
4482 				newStartOffset = range.startOffset;
4483 				newEndOffset = range.endOffset;
4484 
4485 				if (range.startContainer === node.parentNode && range.startOffset > getNodeIndex(node)) {
4486 					// the node (1 element) will be replaced by its contents (contents().length elements)
4487 					newStartOffset = range.startOffset + (jQuery(node).contents().length - 1);
4488 				}
4489 				if (range.endContainer === node.parentNode && range.endOffset > getNodeIndex(node)) {
4490 					// the node (1 element) will be replaced by its contents (contents().length elements)
4491 					newEndOffset = range.endOffset + (jQuery(node).contents().length - 1);
4492 				}
4493 				jQuery(node).contents().unwrap();
4494 				range.startOffset = newStartOffset;
4495 				range.endOffset = newEndOffset;
4496 				// after unwrapping, we are done
4497 				break;
4498 			} else {
4499 				// store the original parent
4500 				var originalParent = node.parentNode;
4501 				splitParent([node], range);
4502 				// check whether the parent did not change, so the split did not work, e.g.
4503 				// because we already reached the editing host itself.
4504 				// this situation can occur, e.g. when we insert a paragraph into an contenteditable span
4505 				// in such cases, we just unwrap the contents of the paragraph
4506 				if (originalParent === node.parentNode) {
4507 					// so we unwrap now
4508 					newStartOffset = range.startOffset;
4509 					newEndOffset = range.endOffset;
4510 
4511 					if (range.startContainer === node.parentNode && range.startOffset > getNodeIndex(node)) {
4512 						// the node (1 element) will be replaced by its contents (contents().length elements)
4513 						newStartOffset = range.startOffset + (jQuery(node).contents().length - 1);
4514 					}
4515 					if (range.endContainer === node.parentNode && range.endOffset > getNodeIndex(node)) {
4516 						// the node (1 element) will be replaced by its contents (contents().length elements)
4517 						newEndOffset = range.endOffset + (jQuery(node).contents().length - 1);
4518 					}
4519 					jQuery(node).contents().unwrap();
4520 					range.startOffset = newStartOffset;
4521 					range.endOffset = newEndOffset;
4522 					// after unwrapping, we are done
4523 					break;
4524 				}
4525 			}
4526 		}
4527 
4528 		// "Restore the values from values."
4529 		restoreValues(values, range);
4530 	}
4531 
4532 	/**
4533 	 * This method "normalizes" sublists of the given item (which is supposed to be a LI):
4534 	 * If sublists are found in the LI element, they are moved directly into the outer list.
4535 	 * @param item item
4536 	 * @param range range, which will be modified if necessary
4537 	 */
4538 	function normalizeSublists(item, range) {
4539 		// "If item is not an li or it is not editable or its parent is not
4540 		// editable, abort these steps."
4541 		if (!isNamedHtmlElement(item, 'LI') || !isEditable(item) || !isEditable(item.parentNode)) {
4542 			return;
4543 		}
4544 
4545 		// "Let new item be null."
4546 		var newItem = null;
4547 
4548 		function isOlUl(node) {
4549 			return isHtmlElementInArray(node, ["OL", "UL"]);
4550 		}
4551 
4552 		// "While item has an ol or ul child:"
4553 		while ($_(item.childNodes).some(isOlUl)) {
4554 			// "Let child be the last child of item."
4555 			var child = item.lastChild;
4556 
4557 			// "If child is an ol or ul, or new item is null and child is a Text
4558 			// node whose data consists of zero of more space characters:"
4559 			if (isHtmlElementInArray(child, ["OL", "UL"]) || (!newItem && child.nodeType == $_.Node.TEXT_NODE && /^[ \t\n\f\r]*$/.test(child.data))) {
4560 				// "Set new item to null."
4561 				newItem = null;
4562 
4563 				// "Insert child into the parent of item immediately following
4564 				// item, preserving ranges."
4565 				movePreservingRanges(child, item.parentNode, 1 + getNodeIndex(item), range);
4566 
4567 				// "Otherwise:"
4568 			} else {
4569 				// "If new item is null, let new item be the result of calling
4570 				// createElement("li") on the ownerDocument of item, then insert
4571 				// new item into the parent of item immediately after item."
4572 				if (!newItem) {
4573 					newItem = item.ownerDocument.createElement("li");
4574 					item.parentNode.insertBefore(newItem, item.nextSibling);
4575 				}
4576 
4577 				// "Insert child into new item as its first child, preserving
4578 				// ranges."
4579 				movePreservingRanges(child, newItem, 0, range);
4580 			}
4581 		}
4582 	}
4583 
4584 	/**
4585 	 * This method is the exact opposite of normalizeSublists.
4586 	 * List nodes directly nested into each other are corrected to be nested in li elements (so that the resulting lists conform the html5 specification)
4587 	 * @param item list node
4588 	 * @param range range, which is preserved when modifying the list
4589 	 */
4590 	function unNormalizeSublists(item, range) {
4591 		// "If item is not an ol or ol or it is not editable or its parent is not
4592 		// editable, abort these steps."
4593 		if (!isHtmlElementInArray(item, ["OL", "UL"]) || !isEditable(item)) {
4594 			return;
4595 		}
4596 
4597 		var $list = jQuery(item);
4598 		$list.children("ol,ul").each(function (index, sublist) {
4599 			if (isNamedHtmlElement(sublist.previousSibling, "LI")) {
4600 				// move the sublist into the LI
4601 				movePreservingRanges(sublist, sublist.previousSibling, sublist.previousSibling.childNodes.length, range);
4602 			}
4603 		});
4604 	}
4605 
4606 	//@}
4607 	///// Block-extending a range /////
4608 	//@{
4609 
4610 	function blockExtend(range) {
4611 		// "Let start node, start offset, end node, and end offset be the start
4612 		// and end nodes and offsets of the range."
4613 		var startNode = range.startContainer;
4614 		var startOffset = range.startOffset;
4615 		var endNode = range.endContainer;
4616 		var endOffset = range.endOffset;
4617 
4618 		// "If some ancestor container of start node is an li, set start offset to
4619 		// the index of the last such li in tree order, and set start node to that
4620 		// li's parent."
4621 		var liAncestors = $_(getAncestors(startNode).concat(startNode)).filter(function (ancestor) { return isNamedHtmlElement(ancestor, 'li'); }).slice(-1);
4622 		if (liAncestors.length) {
4623 			startOffset = getNodeIndex(liAncestors[0]);
4624 			startNode = liAncestors[0].parentNode;
4625 		}
4626 
4627 		// "If (start node, start offset) is not a block start point, repeat the
4628 		// following steps:"
4629 		if (!isBlockStartPoint(startNode, startOffset)) {
4630 			do {
4631 				// "If start offset is zero, set it to start node's index, then set
4632 				// start node to its parent."
4633 				if (startOffset == 0) {
4634 					startOffset = getNodeIndex(startNode);
4635 					startNode = startNode.parentNode;
4636 
4637 					// "Otherwise, subtract one from start offset."
4638 				} else {
4639 					startOffset--;
4640 				}
4641 
4642 				// "If (start node, start offset) is a block boundary point, break from
4643 				// this loop."
4644 			} while (!isBlockBoundaryPoint(startNode, startOffset));
4645 		}
4646 
4647 		// "While start offset is zero and start node's parent is not null, set
4648 		// start offset to start node's index, then set start node to its parent."
4649 		while (startOffset == 0 && startNode.parentNode) {
4650 			startOffset = getNodeIndex(startNode);
4651 			startNode = startNode.parentNode;
4652 		}
4653 
4654 		// "If some ancestor container of end node is an li, set end offset to one
4655 		// plus the index of the last such li in tree order, and set end node to
4656 		// that li's parent."
4657 		liAncestors = $_(getAncestors(endNode).concat(endNode)).filter(function (ancestor) { return isNamedHtmlElement(ancestor, 'li'); }).slice(-1);
4658 		if (liAncestors.length) {
4659 			endOffset = 1 + getNodeIndex(liAncestors[0]);
4660 			endNode = liAncestors[0].parentNode;
4661 		}
4662 
4663 		// "If (end node, end offset) is not a block end point, repeat the
4664 		// following steps:"
4665 		if (!isBlockEndPoint(endNode, endOffset)) {
4666 			do {
4667 				// "If end offset is end node's length, set it to one plus end node's
4668 				// index, then set end node to its parent."
4669 				if (endOffset == getNodeLength(endNode)) {
4670 					endOffset = 1 + getNodeIndex(endNode);
4671 					endNode = endNode.parentNode;
4672 
4673 					// "Otherwise, add one to end offset.
4674 				} else {
4675 					endOffset++;
4676 				}
4677 
4678 				// "If (end node, end offset) is a block boundary point, break from
4679 				// this loop."
4680 			} while (!isBlockBoundaryPoint(endNode, endOffset));
4681 		}
4682 
4683 		// "While end offset is end node's length and end node's parent is not
4684 		// null, set end offset to one plus end node's index, then set end node to
4685 		// its parent."
4686 		while (endOffset == getNodeLength(endNode) && endNode.parentNode) {
4687 			endOffset = 1 + getNodeIndex(endNode);
4688 			endNode = endNode.parentNode;
4689 		}
4690 
4691 		// "Let new range be a new range whose start and end nodes and offsets
4692 		// are start node, start offset, end node, and end offset."
4693 		var newRange = Aloha.createRange();
4694 		newRange.setStart(startNode, startOffset);
4695 		newRange.setEnd(endNode, endOffset);
4696 
4697 		// "Return new range."
4698 		return newRange;
4699 	}
4700 
4701 	function getSelectionListState() {
4702 		// "Block-extend the active range, and let new range be the result."
4703 		var newRange = blockExtend(getActiveRange());
4704 
4705 		// "Let node list be a list of nodes, initially empty."
4706 		//
4707 		// "For each node contained in new range, append node to node list if the
4708 		// last member of node list (if any) is not an ancestor of node; node is
4709 		// editable; node is not an indentation element; and node is either an ol
4710 		// or ul, or the child of an ol or ul, or an allowed child of "li"."
4711 		var nodeList = getContainedNodes(newRange, function (node) {
4712 			return isEditable(node) && !isIndentationElement(node) && (isHtmlElementInArray(node, ["ol", "ul"]) || isHtmlElementInArray(node.parentNode, ["ol", "ul"]) || isAllowedChild(node, "li"));
4713 		});
4714 
4715 		// "If node list is empty, return "none"."
4716 		if (!nodeList.length) {
4717 			return "none";
4718 		}
4719 
4720 		// "If every member of node list is either an ol or the child of an ol or
4721 		// the child of an li child of an ol, and none is a ul or an ancestor of a
4722 		// ul, return "ol"."
4723 		if ($_(nodeList).every(function (node) { return (isNamedHtmlElement(node, 'ol')
4724 														 || isNamedHtmlElement(node.parentNode, "ol")
4725 														 || (isNamedHtmlElement(node.parentNode, "li")
4726 															 && isNamedHtmlElement(node.parentNode.parentNode, "ol"))); })
4727 			    && !$_(nodeList).some(function (node) { return isNamedHtmlElement(node, 'ul') || (node.querySelector && node.querySelector("ul")); })) {
4728 			return "ol";
4729 		}
4730 
4731 		// "If every member of node list is either a ul or the child of a ul or the
4732 		// child of an li child of a ul, and none is an ol or an ancestor of an ol,
4733 		// return "ul"."
4734 		if ($_(nodeList).every(function (node) { return (isNamedHtmlElement(node, 'ul')
4735 														 || isNamedHtmlElement(node.parentNode, "ul")
4736 														 || (isNamedHtmlElement(node.parentNode, "li")
4737 															 && isNamedHtmlElement(node.parentNode.parentNode, "ul"))); })
4738 			    && !$_(nodeList).some(function (node) { return isNamedHtmlElement(node, 'ol') || (node.querySelector && node.querySelector("ol")); })) {
4739 			return "ul";
4740 		}
4741 
4742 		var hasOl = $_(nodeList).some(function (node) {
4743 			return (isNamedHtmlElement(node, 'ol')
4744 					|| isNamedHtmlElement(node.parentNode, "ol")
4745 					|| (node.querySelector && node.querySelector("ol"))
4746 					|| (isNamedHtmlElement(node.parentNode, "li")
4747 						&& isNamedHtmlElement(node.parentNode.parentNode, "ol")));
4748 		});
4749 		var hasUl = $_(nodeList).some(function (node) {
4750 			return (isNamedHtmlElement(node, 'ul')
4751 					|| isNamedHtmlElement(node.parentNode, "ul")
4752 					|| (node.querySelector && node.querySelector("ul"))
4753 					|| (isNamedHtmlElement(node.parentNode, "li")
4754 						&& isNamedHtmlElement(node.parentNode.parentNode, "ul")));
4755 		});
4756 		// "If some member of node list is either an ol or the child or ancestor of
4757 		// an ol or the child of an li child of an ol, and some member of node list
4758 		// is either a ul or the child or ancestor of a ul or the child of an li
4759 		// child of a ul, return "mixed"."
4760 		if (hasOl && hasUl) {
4761 			return "mixed";
4762 		}
4763 
4764 		// "If some member of node list is either an ol or the child or ancestor of
4765 		// an ol or the child of an li child of an ol, return "mixed ol"."
4766 		if (hasOl) {
4767 			return "mixed ol";
4768 		}
4769 
4770 		// "If some member of node list is either a ul or the child or ancestor of
4771 		// a ul or the child of an li child of a ul, return "mixed ul"."
4772 		if (hasUl) {
4773 			return "mixed ul";
4774 		}
4775 
4776 		// "Return "none"."
4777 		return "none";
4778 	}
4779 
4780 	function getAlignmentValue(node) {
4781 		// "While node is neither null nor an Element, or it is an Element but its
4782 		// "display" property has resolved value "inline" or "none", set node to
4783 		// its parent."
4784 		while ((node && node.nodeType != $_.Node.ELEMENT_NODE) || (node.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(node).display, ["inline", "none"]) != -1)) {
4785 			node = node.parentNode;
4786 		}
4787 
4788 		// "If node is not an Element, return "left"."
4789 		if (!node || node.nodeType != $_.Node.ELEMENT_NODE) {
4790 			return "left";
4791 		}
4792 
4793 		var resolvedValue = $_.getComputedStyle(node).textAlign
4794 		// Hack around browser non-standardness
4795 			.replace(/^-(moz|webkit)-/, "").replace(/^auto$/, "start");
4796 
4797 		// "If node's "text-align" property has resolved value "start", return
4798 		// "left" if the directionality of node is "ltr", "right" if it is "rtl"."
4799 		if (resolvedValue == "start") {
4800 			return getDirectionality(node) == "ltr" ? "left" : "right";
4801 		}
4802 
4803 		// "If node's "text-align" property has resolved value "end", return
4804 		// "right" if the directionality of node is "ltr", "left" if it is "rtl"."
4805 		if (resolvedValue == "end") {
4806 			return getDirectionality(node) == "ltr" ? "right" : "left";
4807 		}
4808 
4809 		// "If node's "text-align" property has resolved value "center", "justify",
4810 		// "left", or "right", return that value."
4811 		if (jQuery.inArray(resolvedValue, ["center", "justify", "left", "right"]) != -1) {
4812 			return resolvedValue;
4813 		}
4814 
4815 		// "Return "left"."
4816 		return "left";
4817 	}
4818 
4819 	//@}
4820 	///// Recording and restoring overrides /////
4821 	//@{
4822 
4823 	function recordCurrentOverrides(range) {
4824 		// "Let overrides be a list of (string, string or boolean) ordered pairs,
4825 		// initially empty."
4826 		var overrides = [];
4827 
4828 		// "If there is a value override for "createLink", add ("createLink", value
4829 		// override for "createLink") to overrides."
4830 		if (getValueOverride("createlink", range) !== undefined) {
4831 			overrides.push(["createlink", getValueOverride("createlink", range)]);
4832 		}
4833 
4834 		// "For each command in the list "bold", "italic", "strikethrough",
4835 		// "subscript", "superscript", "underline", in order: if there is a state
4836 		// override for command, add (command, command's state override) to
4837 		// overrides."
4838 		$_(["bold", "italic", "strikethrough", "subscript", "superscript", "underline"]).forEach(function (command) {
4839 			if (getStateOverride(command, range) !== undefined) {
4840 				overrides.push([command, getStateOverride(command, range)]);
4841 			}
4842 		});
4843 4844 
		// "For each command in the list "fontName", "fontSize", "foreColor",
4845 		// "hiliteColor", in order: if there is a value override for command, add
4846 		// (command, command's value override) to overrides."
4847 		$_(["fontname", "fontsize", "forecolor", "hilitecolor"]).forEach(function (command) {
4848 			if (getValueOverride(command, range) !== undefined) {
4849 				overrides.push([command, getValueOverride(command, range)]);
4850 			}
4851 		});
4852 
4853 		// "Return overrides."
4854 		return overrides;
4855 	}
4856 
4857 	function recordCurrentStatesAndValues(range) {
4858 		// "Let overrides be a list of (string, string or boolean) ordered pairs,
4859 		// initially empty."
4860 		var overrides = [];
4861 
4862 		// "Let node be the first editable Text node effectively contained in the
4863 		// active range, or null if there is none."
4864 		var node = $_(getAllEffectivelyContainedNodes(range)).filter(function (node) {
4865 			return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
4866 		})[0];
4867 
4868 		// "If node is null, return overrides."
4869 		if (!node) {
4870 			return overrides;
4871 		}
4872 
4873 		// "Add ("createLink", value for "createLink") to overrides."
4874 		overrides.push(["createlink", commands.createlink.value(range)]);
4875 
4876 		// "For each command in the list "bold", "italic", "strikethrough",
4877 		// "subscript", "superscript", "underline", in order: if node's effective
4878 		// command value for command is one of its inline command activated values,
4879 		// add (command, true) to overrides, and otherwise add (command, false) to
4880 		// overrides."
4881 		$_(["bold", "italic", "strikethrough", "subscript", "superscript", "underline"]).forEach(function (command) {
4882 			if ($_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(node, command)) != -1) {
4883 				overrides.push([command, true]);
4884 			} else {
4885 				overrides.push([command, false]);
4886 			}
4887 		});
4888 
4889 		// "For each command in the list "fontName", "foreColor", "hiliteColor", in
4890 		// order: add (command, command's value) to overrides."
4891 
4892 		$_(["fontname", "fontsize", "forecolor", "hilitecolor"]).forEach(function (command) {
4893 			overrides.push([command, commands[command].value(range)]);
4894 		});
4895 
4896 		// "Add ("fontSize", node's effective command value for "fontSize") to
4897 		// overrides."
4898 		overrides.push(["fontsize", getEffectiveCommandValue(node, "fontsize")]);
4899 
4900 		// "Return overrides."
4901 		return overrides;
4902 	}
4903 
4904 	function restoreStatesAndValues(overrides, range) {
4905 		var i;
4906 		var command;
4907 		var override;
4908 		// "Let node be the first editable Text node effectively contained in the
4909 		// active range, or null if there is none."
4910 		var node = $_(getAllEffectivelyContainedNodes(range)).filter(function (node) {
4911 			return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
4912 		})[0];
4913 
4914 		function isEditableTextNode(node) {
4915 			return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
4916 		}
4917 
4918 		// "If node is not null, then for each (command, override) pair in
4919 		// overrides, in order:"
4920 		if (node) {
4921 
4922 			for (i = 0; i < overrides.length; i++) {
4923 				command = overrides[i][0];
4924 				override = overrides[i][1];
4925 
4926 				// "If override is a boolean, and queryCommandState(command)
4927 				// returns something different from override, call
4928 				// execCommand(command)."
4929 				if (typeof override == "boolean" && myQueryCommandState(command, range) != override) {
4930 					myExecCommand(command);
4931 
4932 					// "Otherwise, if override is a string, and command is not
4933 					// "fontSize", and queryCommandValue(command) returns something not
4934 					// equivalent to override, call execCommand(command, false,
4935 					// override)."
4936 				} else if (typeof override == "string" && command != "fontsize" && !areEquivalentValues(command, myQueryCommandValue(command, range), override)) {
4937 					myExecCommand(command, false, override, range);
4938 
4939 					// "Otherwise, if override is a string; and command is "fontSize";
4940 					// and either there is a value override for "fontSize" that is not
4941 					// equal to override, or there is no value override for "fontSize"
4942 					// and node's effective command value for "fontSize" is not loosely
4943 					// equivalent to override: call execCommand("fontSize", false,
4944 					// override)."
4945 				} else if (typeof override == "string"
4946 						   && command == "fontsize"
4947 						   && ((getValueOverride("fontsize", range) !== undefined
4948 								&& getValueOverride("fontsize", range) !== override)
4949 							   || (getValueOverride("fontsize", range) === undefined
4950 								   && !areLooselyEquivalentValues(command, getEffectiveCommandValue(node, "fontsize"), override)))) {
4951 					myExecCommand("fontsize", false, override, range);
4952 
4953 					// "Otherwise, continue this loop from the beginning."
4954 				} else {
4955 					continue;
4956 				}
4957 
4958 				// "Set node to the first editable Text node effectively contained
4959 				// in the active range, if there is one."
4960 				node = $_(getAllEffectivelyContainedNodes(range)).filter(isEditableTextNode)[0] || node;
4961 			}
4962 
4963 			// "Otherwise, for each (command, override) pair in overrides, in order:"
4964 		} else {
4965 			for (i = 0; i < overrides.length; i++) {
4966 				command = overrides[i][0];
4967 				override = overrides[i][1];
4968 
4969 				// "If override is a boolean, set the state override for command to
4970 				// override."
4971 				if (typeof override == "boolean") {
4972 					setStateOverride(command, override, range);
4973 				}
4974 
4975 				// "If override is a string, set the value override for command to
4976 				// override."
4977 				if (typeof override == "string") {
4978 					setValueOverride(command, override, range);
4979 				}
4980 			}
4981 		}
4982 	}
4983 
4984 	//@}
4985 	///// Canonical space sequences /////
4986 	//@{
4987 
4988 	function canonicalSpaceSequence(n, nonBreakingStart, nonBreakingEnd) {
4989 		// "If n is zero, return the empty string."
4990 		if (n == 0) {
4991 			return "";
4992 		}
4993 
4994 		// "If n is one and both non-breaking start and non-breaking end are false,
4995 		// return a single space (U+0020)."
4996 		if (n == 1 && !nonBreakingStart && !nonBreakingEnd) {
4997 			return " ";
4998 		}
4999 
5000 		// "If n is one, return a single non-breaking space (U+00A0)."
5001 		if (n == 1) {
5002 			return "\xa0";
5003 		}
5004 
5005 		// "Let buffer be the empty string."
5006 		var buffer = "";
5007 
5008 		// "If non-breaking start is true, let repeated pair be U+00A0 U+0020.
5009 		// Otherwise, let it be U+0020 U+00A0."
5010 		var repeatedPair;
5011 		if (nonBreakingStart) {
5012 5013 			repeatedPair = "\xa0 ";
5014 		} else {
5015 			repeatedPair = " \xa0";
5016 		}
5017 
5018 		// "While n is greater than three, append repeated pair to buffer and
5019 		// subtract two from n."
5020 		while (n > 3) {
5021 			buffer += repeatedPair;
5022 			n -= 2;
5023 		}
5024 
5025 		// "If n is three, append a three-element string to buffer depending on
5026 		// non-breaking start and non-breaking end:"
5027 		if (n == 3) {
5028 			buffer += !nonBreakingStart && !nonBreakingEnd ? " \xa0 " : nonBreakingStart && !nonBreakingEnd ? "\xa0\xa0 " : !nonBreakingStart && nonBreakingEnd ? " \xa0\xa0" : nonBreakingStart && nonBreakingEnd ? "\xa0 \xa0" : "impossible";
5029 
5030 			// "Otherwise, append a two-element string to buffer depending on
5031 			// non-breaking start and non-breaking end:"
5032 		} else {
5033 			buffer += !nonBreakingStart && !nonBreakingEnd ? "\xa0 " : nonBreakingStart && !nonBreakingEnd ? "\xa0 " : !nonBreakingStart && nonBreakingEnd ? " \xa0" : nonBreakingStart && nonBreakingEnd ? "\xa0\xa0" : "impossible";
5034 		}
5035 
5036 		// "Return buffer."
5037 		return buffer;
5038 	}
5039 
5040 	function canonicalizeWhitespace(node, offset) {
5041 		// "If node is neither editable nor an editing host, abort these steps."
5042 		if (!isEditable(node) && !isEditingHost(node)) {
5043 			return;
5044 		}
5045 
5046 		// "Let start node equal node and let start offset equal offset."
5047 		var startNode = node;
5048 		var startOffset = offset;
5049 
5050 		// "Repeat the following steps:"
5051 		while (true) {
5052 			// "If start node has a child in the same editing host with index start
5053 			// offset minus one, set start node to that child, then set start
5054 			// offset to start node's length."
5055 			if (0 <= startOffset - 1 && inSameEditingHost(startNode, startNode.childNodes[startOffset - 1])) {
5056 				startNode = startNode.childNodes[startOffset - 1];
5057 				startOffset = getNodeLength(startNode);
5058 
5059 				// "Otherwise, if start offset is zero and start node does not follow a
5060 				// line break and start node's parent is in the same editing host, set
5061 				// start offset to start node's index, then set start node to its
5062 				// parent."
5063 			} else if (startOffset == 0 && !followsLineBreak(startNode) && inSameEditingHost(startNode, startNode.parentNode)) {
5064 				startOffset = getNodeIndex(startNode);
5065 				startNode = startNode.parentNode;
5066 
5067 				// "Otherwise, if start node is a Text node and its parent's resolved
5068 				// value for "white-space" is neither "pre" nor "pre-wrap" and start
5069 				// offset is not zero and the (start offset − 1)st element of start
5070 				// node's data is a space (0x0020) or non-breaking space (0x00A0),
5071 				// subtract one from start offset."
5072 			} else if (startNode.nodeType == $_.Node.TEXT_NODE && jQuery.inArray($_.getComputedStyle(startNode.parentNode).whiteSpace, ["pre", "pre-wrap"]) == -1 && startOffset != 0 && /[ \xa0]/.test(startNode.data[startOffset - 1])) {
5073 				startOffset--;
5074 
5075 				// "Otherwise, break from this loop."
5076 			} else {
5077 				break;
5078 			}
5079 		}
5080 
5081 		// "Let end node equal start node and end offset equal start offset."
5082 		var endNode = startNode;
5083 		var endOffset = startOffset;
5084 
5085 		// "Let length equal zero."
5086 		var length = 0;
5087 
5088 		// "Let follows space be false."
5089 		var followsSpace = false;
5090 
5091 		// "Repeat the following steps:"
5092 		while (true) {
5093 			// "If end node has a child in the same editing host with index end
5094 			// offset, set end node to that child, then set end offset to zero."
5095 			if (endOffset < endNode.childNodes.length && inSameEditingHost(endNode, endNode.childNodes[endOffset])) {
5096 				endNode = endNode.childNodes[endOffset];
5097 				endOffset = 0;
5098 
5099 				// "Otherwise, if end offset is end node's length and end node does not
5100 				// precede a line break and end node's parent is in the same editing
5101 				// host, set end offset to one plus end node's index, then set end node
5102 				// to its parent."
5103 			} else if (endOffset == getNodeLength(endNode) && !precedesLineBreak(endNode) && inSameEditingHost(endNode, endNode.parentNode)) {
5104 				endOffset = 1 + getNodeIndex(endNode);
5105 				endNode = endNode.parentNode;
5106 
5107 				// "Otherwise, if end node is a Text node and its parent's resolved
5108 				// value for "white-space" is neither "pre" nor "pre-wrap" and end
5109 				// offset is not end node's length and the end offsetth element of
5110 				// end node's data is a space (0x0020) or non-breaking space (0x00A0):"
5111 			} else if (endNode.nodeType == $_.Node.TEXT_NODE && jQuery.inArray($_.getComputedStyle(endNode.parentNode).whiteSpace, ["pre", "pre-wrap"]) == -1 && endOffset != getNodeLength(endNode) && /[ \xa0]/.test(endNode.data[endOffset])) {
5112 				// "If follows space is true and the end offsetth element of end
5113 				// node's data is a space (0x0020), call deleteData(end offset, 1)
5114 				// on end node, then continue this loop from the beginning."
5115 				if (followsSpace && " " == endNode.data[endOffset]) {
5116 					endNode.deleteData(endOffset, 1);
5117 					continue;
5118 				}
5119 
5120 				// "Set follows space to true if the end offsetth element of end
5121 				// node's data is a space (0x0020), false otherwise."
5122 				followsSpace = " " == endNode.data[endOffset];
5123 
5124 				// "Add one to end offset."
5125 				endOffset++;
5126 
5127 				// "Add one to length."
5128 				length++;
5129 
5130 				// "Otherwise, break from this loop."
5131 			} else {
5132 				break;
5133 			}
5134 		}
5135 
5136 		// "Let replacement whitespace be the canonical space sequence of length
5137 5138 		// length. non-breaking start is true if start offset is zero and start
5139 		// node follows a line break, and false otherwise. non-breaking end is true
5140 		// if end offset is end node's length and end node precedes a line break,
5141 		// and false otherwise."
5142 		var replacementWhitespace = canonicalSpaceSequence(length, startOffset == 0 && followsLineBreak(startNode), endOffset == getNodeLength(endNode) && precedesLineBreak(endNode));
5143 
5144 		// "While (start node, start offset) is before (end node, end offset):"
5145 		while (getPosition(startNode, startOffset, endNode, endOffset) == "before") {
5146 			// "If start node has a child with index start offset, set start node
5147 			// to that child, then set start offset to zero."
5148 			if (startOffset < startNode.childNodes.length) {
5149 				startNode = startNode.childNodes[startOffset];
5150 				startOffset = 0;
5151 
5152 				// "Otherwise, if start node is not a Text node or if start offset is
5153 				// start node's length, set start offset to one plus start node's
5154 				// index, then set start node to its parent."
5155 			} else if (startNode.nodeType != $_.Node.TEXT_NODE || startOffset == getNodeLength(startNode)) {
5156 				startOffset = 1 + getNodeIndex(startNode);
5157 				startNode = startNode.parentNode;
5158 
5159 				// "Otherwise:"
5160 			} else {
5161 				// "Remove the first element from replacement whitespace, and let
5162 				// element be that element."
5163 				var element = replacementWhitespace[0];
5164 				replacementWhitespace = replacementWhitespace.slice(1);
5165 
5166 				// "If element is not the same as the start offsetth element of
5167 				// start node's data:"
5168 				if (element != startNode.data[startOffset]) {
5169 					// "Call insertData(start offset, element) on start node."
5170 					startNode.insertData(startOffset, element);
5171 
5172 					// "Call deleteData(start offset + 1, 1) on start node."
5173 					startNode.deleteData(startOffset + 1, 1);
5174 				}
5175 
5176 				// "Add one to start offset."
5177 				startOffset++;
5178 			}
5179 		}
5180 	}
5181 
5182 	//@}
5183 	///// Deleting the contents of a range /////
5184 	//@{
5185 
5186 	function deleteContents(arg1, arg2, arg3, arg4, arg5) {
5187 		// We accept several different calling conventions:
5188 		//
5189 		// 1) A single argument, which is a range.
5190 		//
5191 		// 2) Two arguments, the first being a range and the second flags.
5192 		//
5193 		// 3) Four arguments, the start and end of a range.
5194 		//
5195 		// 4) Five arguments, the start and end of a range plus flags.
5196 		//
5197 		// The flags argument is a dictionary that can have up to two keys,
5198 		// blockMerging and stripWrappers, whose corresponding values are
5199 		// interpreted as boolean.  E.g., {stripWrappers: false}.
5200 		var range;
5201 		var flags = {};
5202 		var i;
5203 
5204 		if (arguments.length < 3) {
5205 			range = arg1;
5206 		} else {
5207 			range = Aloha.createRange();
5208 			range.setStart(arg1, arg2);
5209 			range.setEnd(arg3, arg4);
5210 		}
5211 		if (arguments.length == 2) {
5212 			flags = arg2;
5213 		}
5214 		if (arguments.length == 5) {
5215 			flags = arg5;
5216 		}
5217 
5218 		var blockMerging = null != flags.blockMerging ? !!flags.blockMerging : true;
5219 		var stripWrappers = null != flags.stripWrappers ? !!flags.stripWrappers : true;
5220 
5221 		// "If range is null, abort these steps and do nothing."
5222 		if (!range) {
5223 			return;
5224 		}
5225 
5226 		// "Let start node, start offset, end node, and end offset be range's start
5227 		// and end nodes and offsets."
5228 		var startNode = range.startContainer;
5229 		var startOffset = range.startOffset;
5230 		var endNode = range.endContainer;
5231 		var endOffset = range.endOffset;
5232 		var referenceNode;
5233 
5234 		// "While start node has at least one child:"
5235 		while (startNode.hasChildNodes()) {
5236 			// "If start offset is start node's length, and start node's parent is
5237 			// in the same editing host, and start node is an inline node, set
5238 			// start offset to one plus the index of start node, then set start
5239 			// node to its parent and continue this loop from the beginning."
5240 			if (startOffset == getNodeLength(startNode) && inSameEditingHost(startNode, startNode.parentNode) && isInlineNode(startNode)) {
5241 				startOffset = 1 + getNodeIndex(startNode);
5242 				startNode = startNode.parentNode;
5243 				continue;
5244 			}
5245 
5246 			// "If start offset is start node's length, break from this loop."
5247 			if (startOffset == getNodeLength(startNode)) {
5248 				break;
5249 			}
5250 
5251 			// "Let reference node be the child of start node with index equal to
5252 			// start offset."
5253 			referenceNode = startNode.childNodes[startOffset];
5254 
5255 			// "If reference node is a block node or an Element with no children,
5256 			// or is neither an Element nor a Text node, break from this loop."
5257 			if (isBlockNode(referenceNode) || (referenceNode.nodeType == $_.Node.ELEMENT_NODE && !referenceNode.hasChildNodes()) || (referenceNode.nodeType != $_.Node.ELEMENT_NODE && referenceNode.nodeType != $_.Node.TEXT_NODE)) {
5258 				break;
5259 			}
5260 
5261 			// "Set start node to reference node and start offset to 0."
5262 			startNode = referenceNode;
5263 			startOffset = 0;
5264 		}
5265 
5266 		// "While end node has at least one child:"
5267 		while (endNode.hasChildNodes()) {
5268 			// "If end offset is 0, and end node's parent is in the same editing
5269 			// host, and end node is an inline node, set end offset to the index of
5270 			// end node, then set end node to its parent and continue this loop
5271 			// from the beginning."
5272 			if (endOffset == 0 && inSameEditingHost(endNode, endNode.parentNode) && isInlineNode(endNode)) {
5273 				endOffset = getNodeIndex(endNode);
5274 				endNode = endNode.parentNode;
5275 				continue;
5276 			}
5277 
5278 			// "If end offset is 0, break from this loop."
5279 			if (endOffset == 0) {
5280 				break;
5281 			}
5282 
5283 			// "Let reference node be the child of end node with index equal to end
5284 			// offset minus one."
5285 			referenceNode = endNode.childNodes[endOffset - 1];
5286 
5287 			// "If reference node is a block node or an Element with no children,
5288 			// or is neither an Element nor a Text node, break from this loop."
5289 			if (isBlockNode(referenceNode) || (referenceNode.nodeType == $_.Node.ELEMENT_NODE && !referenceNode.hasChildNodes()) || (referenceNode.nodeType != $_.Node.ELEMENT_NODE && referenceNode.nodeType != $_.Node.TEXT_NODE)) {
5290 				break;
5291 			}
5292 
5293 			// "Set end node to reference node and end offset to the length of
5294 			// reference node."
5295 			endNode = referenceNode;
5296 			endOffset = getNodeLength(referenceNode);
5297 		}
5298 
5299 		// "If (end node, end offset) is not after (start node, start offset), set
5300 		// range's end to its start and abort these steps."
5301 		if (getPosition(endNode, endOffset, startNode, startOffset) !== "after") {
5302 			range.setEnd(range.startContainer, range.startOffset);
5303 			return;
5304 		}
5305 
5306 		// "If start node is a Text node and start offset is 0, set start offset to
5307 		// the index of start node, then set start node to its parent."
5308 		// Commented out for unknown reason
5309 		//if (startNode.nodeType == $_.Node.TEXT_NODE && startOffset == 0 && startNode != endNode) {
5310 		//		startOffset = getNodeIndex(startNode);
5311 		//		startNode = startNode.parentNode;
5312 		//}
5313 
5314 		// "If end node is a Text node and end offset is its length, set end offset
5315 		// to one plus the index of end node, then set end node to its parent."
5316 		if (endNode.nodeType == $_.Node.TEXT_NODE && endOffset == getNodeLength(endNode) && startNode != endNode) {
5317 			endOffset = 1 + getNodeIndex(endNode);
5318 			endNode = endNode.parentNode;
5319 		}
5320 
5321 		// "Set range's start to (start node, start offset) and its end to (end
5322 		// node, end offset)."
5323 		range.setStart(startNode, startOffset);
5324 		range.setEnd(endNode, endOffset);
5325 
5326 		// "Let start block be the start node of range."
5327 		var startBlock = range.startContainer;
5328 
5329 		// "While start block's parent is in the same editing host and start block
5330 		// is an inline node, set start block to its parent."
5331 		while (inSameEditingHost(startBlock, startBlock.parentNode) && isInlineNode(startBlock)) {
5332 			startBlock = startBlock.parentNode;
5333 		}
5334 
5335 		// "If start block is neither a block node nor an editing host, or "span"
5336 		// is not an allowed child of start block, or start block is a td or th,
5337 		// set start block to null."
5338 		if ((!isBlockNode(startBlock) && !isEditingHost(startBlock)) || !isAllowedChild("span", startBlock) || isHtmlElementInArray(startBlock, ["td", "th"])) {
5339 			startBlock = null;
5340 		}
5341 
5342 		// "Let end block be the end node of range."
5343 		var endBlock = range.endContainer;
5344 
5345 		// "While end block's parent is in the same editing host and end block is
5346 		// an inline node, set end block to its parent."
5347 		while (inSameEditingHost(endBlock, endBlock.parentNode) && isInlineNode(endBlock)) {
5348 			endBlock = endBlock.parentNode;
5349 		}
5350 
5351 		// "If end block is neither a block node nor an editing host, or "span" is
5352 		// not an allowed child of end block, or end block is a td or th, set end
5353 		// block to null."
5354 		if ((!isBlockNode(endBlock) && !isEditingHost(endBlock)) || !isAllowedChild("span", endBlock) || isHtmlElementInArray(endBlock, ["td", "th"])) {
5355 			endBlock = null;
5356 		}
5357 
5358 		// "Record current states and values, and let overrides be the result."
5359 		var overrides = recordCurrentStatesAndValues(range);
5360 		var parent_;
5361 		// "If start node and end node are the same, and start node is an editable
5362 		// Text node:"
5363 		if (startNode == endNode && isEditable(startNode) && startNode.nodeType == $_.Node.TEXT_NODE) {
5364 			// "Let parent be the parent of node."
5365 			parent_ = startNode.parentNode;
5366 
5367 			// "Call deleteData(start offset, end offset − start offset) on start
5368 			// node."
5369 			startNode.deleteData(startOffset, endOffset - startOffset);
5370 
5371 			// if deleting the text moved two spaces together, we replace the left one by a  , which makes the two spaces a visible
5372 			// two space sequence
5373 			if (startOffset > 0 && startNode.data.substr(startOffset - 1, 1) === ' ' && startOffset < startNode.data.length && startNode.data.substr(startOffset, 1) === ' ') {
5374 				startNode.replaceData(startOffset - 1, 1, '\xa0');
5375 			}
5376 
5377 			// "Canonicalize whitespace at (start node, start offset)."
5378 			canonicalizeWhitespace(startNode, startOffset);
5379 
5380 			// "Set range's end to its start."
5381 			// Ok, also set the range's start to its start, because modifying the text 
5382 			// might have somehow corrupted the range
5383 			range.setStart(range.startContainer, range.startOffset);
5384 			range.setEnd(range.startContainer, range.startOffset);
5385 
5386 			// "Restore states and values from overrides."
5387 			restoreStatesAndValues(overrides, range);
5388 
5389 			// "If parent is editable or an editing host, is not an inline node,
5390 			// and has no children, call createElement("br") on the context object
5391 			// and append the result as the last child of parent."
5392 			// only do this, if the offsetHeight is 0
5393 			if ((isEditable(parent_) || isEditingHost(parent_)) && !isInlineNode(parent_)) {
5394 				// TODO is this always correct?
5395 				ensureContainerEditable(parent_);
5396 			}
5397 
5398 			// "Abort these steps."
5399 			return;
5400 		}
5401 
5402 		// "If start node is an editable Text node, call deleteData() on it, with
5403 		// start offset as the first argument and (length of start node − start
5404 		// offset) as the second argument."
5405 		if (isEditable(startNode) && startNode.nodeType == $_.Node.TEXT_NODE) {
5406 			startNode.deleteData(startOffset, getNodeLength(startNode) - startOffset);
5407 		}
5408 
5409 		// "Let node list be a list of nodes, initially empty."
5410 		//
5411 		// "For each node contained in range, append node to node list if the last
5412 		// member of node list (if any) is not an ancestor of node; node is
5413 		// editable; and node is not a thead, tbody, tfoot, tr, th, or td."
5414 		var nodeList = getContainedNodes(
5415 			range,
5416 			function (node) {
5417 				return isEditable(node) && !isHtmlElementInArray(node, ["thead", "tbody", "tfoot", "tr", "th", "td"]);
5418 			}
5419 5420 		);
5421 
5422 		// "For each node in node list:"
5423 		for (i = 0; i < nodeList.length; i++) {
5424 			var node = nodeList[i];
5425 
5426 			// "Let parent be the parent of node."
5427 			parent_ = node.parentNode;
5428 
5429 			// "Remove node from parent."
5430 			parent_.removeChild(node);
5431 
5432 			// "If strip wrappers is true or parent is not an ancestor container of
5433 			// start node, while parent is an editable inline node with length 0,
5434 			// let grandparent be the parent of parent, then remove parent from
5435 			// grandparent, then set parent to grandparent."
5436 			if (stripWrappers || (!isAncestor(parent_, startNode) && parent_ != startNode)) {
5437 				while (isEditable(parent_) && isInlineNode(parent_) && getNodeLength(parent_) == 0) {
5438 					var grandparent = parent_.parentNode;
5439 					grandparent.removeChild(parent_);
5440 					parent_ = grandparent;
5441 				}
5442 			}
5443 
5444 			// "If parent is editable or an editing host, is not an inline node,
5445 			// and has no children, call createElement("br") on the context object
5446 			// and append the result as the last child of parent."
5447 			// only do this, if the offsetHeight is 0
5448 			if ((isEditable(parent_) || isEditingHost(parent_)) && !isInlineNode(parent_)) {
5449 				ensureContainerEditable(parent_);
5450 			}
5451 		}
5452 
5453 		// "If end node is an editable Text node, call deleteData(0, end offset) on
5454 		// it."
5455 		if (isEditable(endNode) && endNode.nodeType == $_.Node.TEXT_NODE) {
5456 			endNode.deleteData(0, endOffset);
5457 		}
5458 
5459 		// "Canonicalize whitespace at range's start."
5460 		canonicalizeWhitespace(range.startContainer, range.startOffset);
5461 
5462 		// "Canonicalize whitespace at range's end."
5463 		canonicalizeWhitespace(range.endContainer, range.endOffset);
5464 
5465 		// "If block merging is false, or start block or end block is null, or
5466 		// start block is not in the same editing host as end block, or start block
5467 		// and end block are the same:"
5468 		if (!blockMerging || !startBlock || !endBlock || !inSameEditingHost(startBlock, endBlock) || startBlock == endBlock) {
5469 			// "Set range's end to its start."
5470 			range.setEnd(range.startContainer, range.startOffset);
5471 
5472 			// "Restore states and values from overrides."
5473 			restoreStatesAndValues(overrides, range);
5474 
5475 			// "Abort these steps."
5476 			return;
5477 		}
5478 
5479 		// "If start block has one child, which is a collapsed block prop, remove
5480 		// its child from it."
5481 		if (startBlock.children.length == 1 && isCollapsedBlockProp(startBlock.firstChild)) {
5482 			startBlock.removeChild(startBlock.firstChild);
5483 		}
5484 
5485 		// "If end block has one child, which is a collapsed block prop, remove its
5486 		// child from it."
5487 		if (endBlock.children.length == 1 && isCollapsedBlockProp(endBlock.firstChild)) {
5488 			endBlock.removeChild(endBlock.firstChild);
5489 		}
5490 
5491 		var values;
5492 5493 		// "If start block is an ancestor of end block:"
5494 		if (isAncestor(startBlock, endBlock)) {
5495 			// "Let reference node be end block."
5496 			referenceNode = endBlock;
5497 
5498 			// "While reference node is not a child of start block, set reference
5499 			// node to its parent."
5500 			while (referenceNode.parentNode != startBlock) {
5501 				referenceNode = referenceNode.parentNode;
5502 			}
5503 
5504 			// "Set the start and end of range to (start block, index of reference
5505 			// node)."
5506 			range.setStart(startBlock, getNodeIndex(referenceNode));
5507 			range.setEnd(startBlock, getNodeIndex(referenceNode));
5508 
5509 			// "If end block has no children:"
5510 			if (!endBlock.hasChildNodes()) {
5511 				// "While end block is editable and is the only child of its parent
5512 				// and is not a child of start block, let parent equal end block,
5513 				// then remove end block from parent, then set end block to
5514 				// parent."
5515 				while (isEditable(endBlock) && endBlock.parentNode.childNodes.length == 1 && endBlock.parentNode != startBlock) {
5516 					parent_ = endBlock;
5517 					parent_.removeChild(endBlock);
5518 					endBlock = parent_;
5519 				}
5520 
5521 				// "If end block is editable and is not an inline node, and its
5522 				// previousSibling and nextSibling are both inline nodes, call
5523 				// createElement("br") on the context object and insert it into end
5524 				// block's parent immediately after end block."
5525 
5526 				if (isEditable(endBlock) && !isInlineNode(endBlock) && isInlineNode(endBlock.previousSibling) && isInlineNode(endBlock.nextSibling)) {
5527 					endBlock.parentNode.insertBefore(document.createElement("br"), endBlock.nextSibling);
5528 				}
5529 
5530 				// "If end block is editable, remove it from its parent."
5531 				if (isEditable(endBlock)) {
5532 					endBlock.parentNode.removeChild(endBlock);
5533 				}
5534 
5535 				// "Restore states and values from overrides."
5536 				restoreStatesAndValues(overrides, range);
5537 
5538 				// "Abort these steps."
5539 				return;
5540 			}
5541 
5542 			// "If end block's firstChild is not an inline node, restore states and
5543 			// values from overrides, then abort these steps."
5544 			if (!isInlineNode(endBlock.firstChild)) {
5545 				restoreStatesAndValues(overrides, range);
5546 				return;
5547 			}
5548 
5549 			// "Let children be a list of nodes, initially empty."
5550 			var children = [];
5551 
5552 			// "Append the first child of end block to children."
5553 			children.push(endBlock.firstChild);
5554 
5555 			// "While children's last member is not a br, and children's last
5556 			// member's nextSibling is an inline node, append children's last
5557 			// member's nextSibling to children."
5558 			while (!isNamedHtmlElement(children[children.length - 1], "br") && isInlineNode(children[children.length - 1].nextSibling)) {
5559 				children.push(children[children.length - 1].nextSibling);
5560 			}
5561 
5562 			// "Record the values of children, and let values be the result."
5563 			values = recordValues(children);
5564 
5565 			// "While children's first member's parent is not start block, split
5566 			// the parent of children."
5567 			while (children[0].parentNode != startBlock) {
5568 				splitParent(children, range);
5569 			}
5570 
5571 			// "If children's first member's previousSibling is an editable br,
5572 			// remove that br from its parent."
5573 			if (isEditable(children[0].previousSibling) && isNamedHtmlElement(children[0].previousSibling, "br")) {
5574 5575 				children[0].parentNode.removeChild(children[0].previousSibling);
5576 			}
5577 
5578 			// "Otherwise, if start block is a descendant of end block:"
5579 		} else if (isDescendant(startBlock, endBlock)) {
5580 			// "Set the start and end of range to (start block, length of start
5581 			// block)."
5582 			range.setStart(startBlock, getNodeLength(startBlock));
5583 			range.setEnd(startBlock, getNodeLength(startBlock));
5584 
5585 			// "Let reference node be start block."
5586 			referenceNode = startBlock;
5587 
5588 			// "While reference node is not a child of end block, set reference
5589 			// node to its parent."
5590 			while (referenceNode.parentNode != endBlock) {
5591 				referenceNode = referenceNode.parentNode;
5592 			}
5593 
5594 			// "If reference node's nextSibling is an inline node and start block's
5595 			// lastChild is a br, remove start block's lastChild from it."
5596 			if (isInlineNode(referenceNode.nextSibling) && isNamedHtmlElement(startBlock.lastChild, "br")) {
5597 				startBlock.removeChild(startBlock.lastChild);
5598 			}
5599 
5600 			// "Let nodes to move be a list of nodes, initially empty."
5601 			var nodesToMove = [];
5602 
5603 			// "If reference node's nextSibling is neither null nor a br nor a
5604 			// block node, append it to nodes to move."
5605 			if (referenceNode.nextSibling && !isNamedHtmlElement(referenceNode.nextSibling, "br") && !isBlockNode(referenceNode.nextSibling)) {
5606 				nodesToMove.push(referenceNode.nextSibling);
5607 			}
5608 
5609 			// "While nodes to move is nonempty and its last member's nextSibling
5610 			// is neither null nor a br nor a block node, append it to nodes to
5611 			// move."
5612 			if (nodesToMove.length && nodesToMove[nodesToMove.length - 1].nextSibling && !isNamedHtmlElement(nodesToMove[nodesToMove.length - 1].nextSibling, "br") && !isBlockNode(nodesToMove[nodesToMove.length - 1].nextSibling)) {
5613 				nodesToMove.push(nodesToMove[nodesToMove.length - 1].nextSibling);
5614 			}
5615 
5616 			// "Record the values of nodes to move, and let values be the result."
5617 			values = recordValues(nodesToMove);
5618 
5619 			// "For each node in nodes to move, append node as the last child of
5620 			// start block, preserving ranges."
5621 			$_(nodesToMove).forEach(function (node) {
5622 				movePreservingRanges(node, startBlock, -1, range);
5623 			});
5624 
5625 			// "If the nextSibling of reference node is a br, remove it from its
5626 			// parent."
5627 			if (isNamedHtmlElement(referenceNode.nextSibling, "br")) {
5628 				referenceNode.parentNode.removeChild(referenceNode.nextSibling);
5629 			}
5630 
5631 			// "Otherwise:"
5632 		} else {
5633 			// "Set the start and end of range to (start block, length of start
5634 			// block)."
5635 			range.setStart(startBlock, getNodeLength(startBlock));
5636 			range.setEnd(startBlock, getNodeLength(startBlock));
5637 
5638 			// "If end block's firstChild is an inline node and start block's
5639 			// lastChild is a br, remove start block's lastChild from it."
5640 			if (isInlineNode(endBlock.firstChild) && isNamedHtmlElement(startBlock.lastChild, "br")) {
5641 				startBlock.removeChild(startBlock.lastChild);
5642 			}
5643 
5644 			// "Record the values of end block's children, and let values be the
5645 			// result."
5646 			values = recordValues([].slice.call(toArray(endBlock.childNodes)));
5647 
5648 			// "While end block has children, append the first child of end block
5649 			// to start block, preserving ranges."
5650 			while (endBlock.hasChildNodes()) {
5651 				movePreservingRanges(endBlock.firstChild, startBlock, -1, range);
5652 			}
5653 
5654 			// "While end block has no children, let parent be the parent of end
5655 			// block, then remove end block from parent, then set end block to
5656 			// parent."
5657 			while (!endBlock.hasChildNodes()) {
5658 				parent_ = endBlock.parentNode;
5659 				parent_.removeChild(endBlock);
5660 				endBlock = parent_;
5661 			}
5662 		}
5663 
5664 		// "Restore the values from values."
5665 		restoreValues(values, range);
5666 
5667 		// "If start block has no children, call createElement("br") on the context
5668 		// object and append the result as the last child of start block."
5669 		ensureContainerEditable(startBlock);
5670 
5671 		// "Restore states and values from overrides."
5672 		restoreStatesAndValues(overrides, range);
5673 	}
5674 
5675 	// "To remove a node node while preserving its descendants, split the parent of
5676 	// node's children if it has any. If it has no children, instead remove it from
5677 	// its parent."
5678 	function removePreservingDescendants(node, range) {
5679 		if (node.hasChildNodes()) {
5680 			splitParent([].slice.call(toArray(node.childNodes)), range);
5681 		} else {
5682 			node.parentNode.removeChild(node);
5683 		}
5684 	}
5685 
5686 	//@}
5687 	///// Indenting and outdenting /////
5688 	//@{
5689 
5690 	function cleanLists(node, range) {
5691 		// remove any whitespace nodes around list nodes
5692 		if (node) {
5693 			jQuery(node).find('ul,ol,li').each(function () {
5694 				jQuery(this).contents().each(function () {
5695 					if (isWhitespaceNode(this)) {
5696 						var index = getNodeIndex(this);
5697 
5698 						// if the range points to somewhere behind the removed text node, we reduce the offset
5699 						if (range.startContainer === this.parentNode && range.startOffset > index) {
5700 							range.startOffset--;
5701 						} else if (range.startContainer === this) {
5702 							// the range starts in the removed text node, let it start right before
5703 							range.startContainer = this.parentNode;
5704 							range.startOffset = index;
5705 						}
5706 						// same thing for end of the range
5707 						if (range.endContainer === this.parentNode && range.endOffset > index) {
5708 							range.endOffset--;
5709 						} else if (range.endContainer === this) {
5710 							range.endContainer = this.parentNode;
5711 							range.endOffset = index;
5712 						}
5713 						// finally remove the whitespace node
5714 						jQuery(this).remove();
5715 					}
5716 				});
5717 			});
5718 		}
5719 	}
5720 
5721 
5722 	//@}
5723 	///// Indenting and outdenting /////
5724 	//@{
5725 
5726 	function indentNodes(nodeList, range) {
5727 		// "If node list is empty, do nothing and abort these steps."
5728 		if (!nodeList.length) {
5729 			return;
5730 		}
5731 
5732 		// "Let first node be the first member of node list."
5733 		var firstNode = nodeList[0];
5734 
5735 		// "If first node's parent is an ol or ul:"
5736 		if (isHtmlElementInArray(firstNode.parentNode, ["OL", "UL"])) {
5737 			// "Let tag be the local name of the parent of first node."
5738 			var tag = firstNode.parentNode.tagName;
5739 
5740 			// "Wrap node list, with sibling criteria returning true for an HTML
5741 			// element with local name tag and false otherwise, and new parent
5742 			// instructions returning the result of calling createElement(tag) on
5743 			// the ownerDocument of first node."
5744 			wrap(
5745 				nodeList,
5746 				function (node) {
5747 					return isHtmlElement_obsolete(node, tag);
5748 				},
5749 				function () {
5750 					return firstNode.ownerDocument.createElement(tag);
5751 				},
5752 				range
5753 			);
5754 
5755 			// "Abort these steps."
5756 			return;
5757 		}
5758 
5759 		// "Wrap node list, with sibling criteria returning true for a simple
5760 		// indentation element and false otherwise, and new parent instructions
5761 		// returning the result of calling createElement("blockquote") on the
5762 		// ownerDocument of first node. Let new parent be the result."
5763 		var newParent = wrap(
5764 			nodeList,
5765 			function (node) {
5766 				return isSimpleIndentationElement(node);
5767 			},
5768 			function () {
5769 				return firstNode.ownerDocument.createElement("blockquote");
5770 			},
5771 			range
5772 		);
5773 
5774 		// "Fix disallowed ancestors of new parent."
5775 		fixDisallowedAncestors(newParent, range);
5776 	}
5777 
5778 	function outdentNode(node, range) {
5779 		// "If node is not editable, abort these steps."
5780 		if (!isEditable(node)) {
5781 			return;
5782 		}
5783 
5784 		// "If node is a simple indentation element, remove node, preserving its
5785 		// descendants.  Then abort these steps."
5786 		if (isSimpleIndentationElement(node)) {
5787 			removePreservingDescendants(node, range);
5788 			return;
5789 		}
5790 
5791 		// "If node is an indentation element:"
5792 		if (isIndentationElement(node)) {
5793 			// "Unset the class and dir attributes of node, if any."
5794 			node.removeAttribute("class");
5795 			node.removeAttribute("dir");
5796 
5797 			// "Unset the margin, padding, and border CSS properties of node."
5798 			node.style.margin = "";
5799 			node.style.padding = "";
5800 			node.style.border = "";
5801 			if (node.getAttribute("style") == "") {
5802 				node.removeAttribute("style");
5803 			}
5804 
5805 			// "Set the tag name of node to "div"."
5806 			setTagName(node, "div", range);
5807 
5808 			// "Abort these steps."
5809 			return;
5810 		}
5811 
5812 		// "Let current ancestor be node's parent."
5813 		var currentAncestor = node.parentNode;
5814 
5815 		// "Let ancestor list be a list of nodes, initially empty."
5816 		var ancestorList = [];
5817 
5818 		// "While current ancestor is an editable Element that is neither a simple
5819 		// indentation element nor an ol nor a ul, append current ancestor to
5820 		// ancestor list and then set current ancestor to its parent."
5821 		while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !isSimpleIndentationElement(currentAncestor) && !isHtmlElementInArray(currentAncestor, ["ol", "ul"])) {
5822 			ancestorList.push(currentAncestor);
5823 			currentAncestor = currentAncestor.parentNode;
5824 		}
5825 
5826 		// "If current ancestor is not an editable simple indentation element:"
5827 		if (!isEditable(currentAncestor) || !isSimpleIndentationElement(currentAncestor)) {
5828 			// "Let current ancestor be node's parent."
5829 			currentAncestor = node.parentNode;
5830 
5831 			// "Let ancestor list be the empty list."
5832 			ancestorList = [];
5833 
5834 			// "While current ancestor is an editable Element that is neither an
5835 			// indentation element nor an ol nor a ul, append current ancestor to
5836 			// ancestor list and then set current ancestor to its parent."
5837 			while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !isIndentationElement(currentAncestor) && !isHtmlElementInArray(currentAncestor, ["ol", "ul"])) {
5838 				ancestorList.push(currentAncestor);
5839 				currentAncestor = currentAncestor.parentNode;
5840 			}
5841 		}
5842 
5843 		// "If node is an ol or ul and current ancestor is not an editable
5844 		// indentation element:"
5845 		if (isHtmlElementInArray(node, ["OL", "UL"]) && (!isEditable(currentAncestor) || !isIndentationElement(currentAncestor))) {
5846 			// "Unset the reversed, start, and type attributes of node, if any are
5847 			// set."
5848 			node.removeAttribute("reversed");
5849 			node.removeAttribute("start");
5850 			node.removeAttribute("type");
5851 
5852 			// "Let children be the children of node."
5853 			var children = [].slice.call(toArray(node.childNodes));
5854 
5855 			// "If node has attributes, and its parent is not an ol or ul, set the
5856 			// tag name of node to "div"."
5857 			if (node.attributes.length && !isHtmlElementInArray(node.parentNode, ["OL", "UL"])) {
5858 				setTagName(node, "div", range);
5859 
5860 				// "Otherwise:"
5861 			} else {
5862 				// "Record the values of node's children, and let values be the
5863 				// result."
5864 				var values = recordValues([].slice.call(toArray(node.childNodes)));
5865 
5866 				// "Remove node, preserving its descendants."
5867 				removePreservingDescendants(node, range);
5868 
5869 				// "Restore the values from values."
5870 				restoreValues(values, range);
5871 			}
5872 
5873 			// "Fix disallowed ancestors of each member of children."
5874 			var i;
5875 			for (i = 0; i < children.length; i++) {
5876 				fixDisallowedAncestors(children[i], range);
5877 			}
5878 
5879 			// "Abort these steps."
5880 			return;
5881 		}
5882 
5883 		// "If current ancestor is not an editable indentation element, abort these
5884 		// steps."
5885 		if (!isEditable(currentAncestor) || !isIndentationElement(currentAncestor)) {
5886 			return;
5887 		}
5888 
5889 		// "Append current ancestor to ancestor list."
5890 		ancestorList.push(currentAncestor);
5891 
5892 		// "Let original ancestor be current ancestor."
5893 		var originalAncestor = currentAncestor;
5894 
5895 		// "While ancestor list is not empty:"
5896 		while (ancestorList.length) {
5897 			// "Let current ancestor be the last member of ancestor list."
5898 			//
5899 			// "Remove the last member of ancestor list."
5900 			currentAncestor = ancestorList.pop();
5901 
5902 			// "Let target be the child of current ancestor that is equal to either
5903 			// node or the last member of ancestor list."
5904 			var target = node.parentNode == currentAncestor ? node : ancestorList[ancestorList.length - 1];
5905 
5906 			// "If target is an inline node that is not a br, and its nextSibling
5907 			// is a br, remove target's nextSibling from its parent."
5908 			if (isInlineNode(target) && !isNamedHtmlElement(target, 'BR') && isNamedHtmlElement(target.nextSibling, "BR")) {
5909 				target.parentNode.removeChild(target.nextSibling);
5910 			}
5911 
5912 			// "Let preceding siblings be the preceding siblings of target, and let
5913 			// following siblings be the following siblings of target."
5914 			var precedingSiblings = [].slice.call(toArray(currentAncestor.childNodes), 0, getNodeIndex(target));
5915 			var followingSiblings = [].slice.call(toArray(currentAncestor.childNodes), 1 + getNodeIndex(target));
5916 
5917 			// "Indent preceding siblings."
5918 			indentNodes(precedingSiblings, range);
5919 
5920 			// "Indent following siblings."
5921 			indentNodes(followingSiblings, range);
5922 		}
5923 
5924 		// "Outdent original ancestor."
5925 		outdentNode(originalAncestor, range);
5926 	}
5927 
5928 
5929 	//@}
5930 	///// Toggling lists /////
5931 	//@{
5932 
5933 	function toggleLists(tagName, range) {
5934 		// "Let mode be "disable" if the selection's list state is tag name, and
5935 		// "enable" otherwise."
5936 		var mode = getSelectionListState() == tagName ? "disable" : "enable";
5937 
5938 		tagName = tagName.toUpperCase();
5939 
5940 		// "Let other tag name be "ol" if tag name is "ul", and "ul" if tag name is
5941 		// "ol"."
5942 		var otherTagName = tagName == "OL" ? "UL" : "OL";
5943 
5944 		// "Let items be a list of all lis that are ancestor containers of the
5945 		// range's start and/or end node."
5946 		//
5947 		// It's annoying to get this in tree order using functional stuff without
5948 		// doing getDescendants(document), which is slow, so I do it imperatively.
5949 		var items = [];
5950 		(function () {
5951 			var ancestorContainer;
5952 			for (ancestorContainer = range.endContainer;
5953 				     ancestorContainer != range.commonAncestorContainer;
5954 				     ancestorContainer = ancestorContainer.parentNode) {
5955 				if (isNamedHtmlElement(ancestorContainer, "li")) {
5956 					items.unshift(ancestorContainer);
5957 				}
5958 			}
5959 			for (ancestorContainer = range.startContainer;
5960 				     ancestorContainer;
5961 				     ancestorContainer = ancestorContainer.parentNode) {
5962 				if (isNamedHtmlElement(ancestorContainer, "li")) {
5963 					items.unshift(ancestorContainer);
5964 				}
5965 			}
5966 		}());
5967 
5968 		// "For each item in items, normalize sublists of item."
5969 		$_(items).forEach(function (thisArg) {
5970 			normalizeSublists(thisArg, range);
5971 		});
5972 
5973 		// "Block-extend the range, and let new range be the result."
5974 		var newRange = blockExtend(range);
5975 
5976 		// "If mode is "enable", then let lists to convert consist of every
5977 		// editable HTML element with local name other tag name that is contained
5978 		// in new range, and for every list in lists to convert:"
5979 		if (mode == "enable") {
5980 			$_(getAllContainedNodes(newRange, function (node) {
5981 				return isEditable(node) && isHtmlElement_obsolete(node, otherTagName);
5982 			})).forEach(function (list) {
5983 				// "If list's previousSibling or nextSibling is an editable HTML
5984 				// element with local name tag name:"
5985 				if ((isEditable(list.previousSibling) && isHtmlElement_obsolete(list.previousSibling, tagName)) || (isEditable(list.nextSibling) && isHtmlElement_obsolete(list.nextSibling, tagName))) {
5986 					// "Let children be list's children."
5987 					var children = [].slice.call(toArray(list.childNodes));
5988 
5989 					// "Record the values of children, and let values be the
5990 					// result."
5991 					var values = recordValues(children);
5992 
5993 					// "Split the parent of children."
5994 					splitParent(children, range);
5995 
5996 					// "Wrap children, with sibling criteria returning true for an
5997 					// HTML element with local name tag name and false otherwise."
5998 					wrap(
5999 						children,
6000 						function (node) {
6001 							return isHtmlElement_obsolete(node, tagName);
6002 						},
6003 						function () {
6004 							return null;
6005 						},
6006 						range
6007 					);
6008 
6009 					// "Restore the values from values."
6010 					restoreValues(values, range);
6011 
6012 					// "Otherwise, set the tag name of list to tag name."
6013 				} else {
6014 					setTagName(list, tagName, range);
6015 				}
6016 			});
6017 		}
6018 
6019 		// "Let node list be a list of nodes, initially empty."
6020 		//
6021 		// "For each node node contained in new range, if node is editable; the
6022 		// last member of node list (if any) is not an ancestor of node; node
6023 		// is not an indentation element; and either node is an ol or ul, or its
6024 		// parent is an ol or ul, or it is an allowed child of "li"; then append
6025 		// node to node list."
6026 		var nodeList = getContainedNodes(newRange, function (node) {
6027 			return isEditable(node) && !isIndentationElement(node) && (isHtmlElementInArray(node, ["OL", "UL"]) || isHtmlElementInArray(node.parentNode, ["OL", "UL"]) || isAllowedChild(node, "li"));
6028 		});
6029 
6030 		// "If mode is "enable", remove from node list any ol or ul whose parent is
6031 		// not also an ol or ul."
6032 		if (mode == "enable") {
6033 			nodeList = $_(nodeList).filter(function (node) {
6034 				return !isHtmlElementInArray(node, ["ol", "ul"]) || isHtmlElementInArray(node.parentNode, ["ol", "ul"]);
6035 			});
6036 		}
6037 
6038 		// "If mode is "disable", then while node list is not empty:"
6039 		var sublist, values;
6040 
6041 		function createLi() {
6042 			return document.createElement("li");
6043 		}
6044 
6045 		function isOlUl(node) {
6046 			return isHtmlElementInArray(node, ["ol", "ul"]);
6047 		}
6048 
6049 		function makeIsElementPred(tagName) {
6050 			return function (node) {
6051 				return isHtmlElement_obsolete(node, tagName);
6052 			};
6053 		}
6054 
6055 		function makeCreateElement(tagName) {
6056 			return function () {
6057 				return document.createElement(tagName);
6058 			};
6059 		}
6060 
6061 		function makeCreateElementSublist(tagName, sublist, range) {
6062 			return function () {
6063 				// "If sublist's first member's parent is not an editable
6064 				// simple indentation element, or sublist's first member's
6065 				// parent's previousSibling is not an editable HTML element
6066 				// with local name tag name, call createElement(tag name)
6067 				// on the context object and return the result."
6068 				if (!isEditable(sublist[0].parentNode) || !isSimpleIndentationElement(sublist[0].parentNode) || !isEditable(sublist[0].parentNode.previousSibling) || !isHtmlElement_obsolete(sublist[0].parentNode.previousSibling, tagName)) {
6069 					return document.createElement(tagName);
6070 				}
6071 
6072 				// "Let list be sublist's first member's parent's
6073 				// previousSibling."
6074 				var list = sublist[0].parentNode.previousSibling;
6075 
6076 				// "Normalize sublists of list's lastChild."
6077 				normalizeSublists(list.lastChild, range);
6078 
6079 				// "If list's lastChild is not an editable HTML element
6080 				// with local name tag name, call createElement(tag name)
6081 				// on the context object, and append the result as the last
6082 				// child of list."
6083 				if (!isEditable(list.lastChild) || !isHtmlElement_obsolete(list.lastChild, tagName)) {
6084 					list.appendChild(document.createElement(tagName));
6085 				}
6086 
6087 				// "Return the last child of list."
6088 				return list.lastChild;
6089 			};
6090 		}
6091 
6092 		if (mode == "disable") {
6093 			while (nodeList.length) {
6094 				// "Let sublist be an empty list of nodes."
6095 				sublist = [];
6096 
6097 				// "Remove the first member from node list and append it to
6098 				// sublist."
6099 				sublist.push(nodeList.shift());
6100 
6101 				// "If the first member of sublist is an HTML element with local
6102 				// name tag name, outdent it and continue this loop from the
6103 				// beginning."
6104 				if (isHtmlElement_obsolete(sublist[0], tagName)) {
6105 					outdentNode(sublist[0], range);
6106 					continue;
6107 				}
6108 
6109 				// "While node list is not empty, and the first member of node list
6110 				// is the nextSibling of the last member of sublist and is not an
6111 				// HTML element with local name tag name, remove the first member
6112 				// from node list and append it to sublist."
6113 				while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isHtmlElement_obsolete(nodeList[0], tagName)) {
6114 					sublist.push(nodeList.shift());
6115 				}
6116 
6117 				// "Record the values of sublist, and let values be the result."
6118 				values = recordValues(sublist);
6119 
6120 				// "Split the parent of sublist."
6121 				splitParent(sublist, range);
6122 
6123 				// "Fix disallowed ancestors of each member of sublist."
6124 				var i;
6125 				for (i = 0; i < sublist.length; i++) {
6126 					fixDisallowedAncestors(sublist[i], range);
6127 				}
6128 
6129 				// "Restore the values from values."
6130 				restoreValues(values, range);
6131 			}
6132 
6133 			// "Otherwise, while node list is not empty:"
6134 		} else {
6135 			while (nodeList.length) {
6136 				// "Let sublist be an empty list of nodes."
6137 				sublist = [];
6138 
6139 				// "While either sublist is empty, or node list is not empty and
6140 				// its first member is the nextSibling of sublist's last member:"
6141 				while (!sublist.length || (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling)) {
6142 					// "If node list's first member is a p or div, set the tag name
6143 					// of node list's first member to "li", and append the result
6144 					// to sublist. Remove the first member from node list."
6145 					if (isHtmlElementInArray(nodeList[0], ["p", "div"])) {
6146 						sublist.push(setTagName(nodeList[0], "li", range));
6147 						nodeList.shift();
6148 
6149 						// "Otherwise, if the first member of node list is an li or ol
6150 						// or ul, remove it from node list and append it to sublist."
6151 					} else if (isHtmlElementInArray(nodeList[0], ["li", "ol", "ul"])) {
6152 						sublist.push(nodeList.shift());
6153 
6154 						// "Otherwise:"
6155 					} else {
6156 						// "Let nodes to wrap be a list of nodes, initially empty."
6157 						var nodesToWrap = [];
6158 
6159 						// "While nodes to wrap is empty, or node list is not empty
6160 						// and its first member is the nextSibling of nodes to
6161 						// wrap's last member and the first member of node list is
6162 						// an inline node and the last member of nodes to wrap is
6163 						// an inline node other than a br, remove the first member
6164 						// from node list and append it to nodes to wrap."
6165 						while (!nodesToWrap.length || (nodeList.length && nodeList[0] == nodesToWrap[nodesToWrap.length - 1].nextSibling && isInlineNode(nodeList[0]) && isInlineNode(nodesToWrap[nodesToWrap.length - 1]) && !isNamedHtmlElement(nodesToWrap[nodesToWrap.length - 1], "br"))) {
6166 							nodesToWrap.push(nodeList.shift());
6167 						}
6168 
6169 						// "Wrap nodes to wrap, with new parent instructions
6170 						// returning the result of calling createElement("li") on
6171 						// the context object. Append the result to sublist."
6172 						sublist.push(wrap(
6173 							nodesToWrap,
6174 							undefined,
6175 							createLi,
6176 							range
6177 						));
6178 					}
6179 				}
6180 
6181 				// "If sublist's first member's parent is an HTML element with
6182 				// local name tag name, or if every member of sublist is an ol or
6183 				// ul, continue this loop from the beginning."
6184 				if (isHtmlElement_obsolete(sublist[0].parentNode, tagName) || $_(sublist).every(isOlUl)) {
6185 					continue;
6186 				}
6187 
6188 				// "If sublist's first member's parent is an HTML element with
6189 				// local name other tag name:"
6190 				if (isHtmlElement_obsolete(sublist[0].parentNode, otherTagName)) {
6191 					// "Record the values of sublist, and let values be the
6192 					// result."
6193 					values = recordValues(sublist);
6194 
6195 					// "Split the parent of sublist."
6196 					splitParent(sublist, range);
6197 
6198 					// "Wrap sublist, with sibling criteria returning true for an
6199 					// HTML element with local name tag name and false otherwise,
6200 					// and new parent instructions returning the result of calling
6201 					// createElement(tag name) on the context object."
6202 					wrap(
6203 						sublist,
6204 						makeIsElementPred(tagName),
6205 						makeCreateElement(tagName),
6206 						range
6207 					);
6208 
6209 					// "Restore the values from values."
6210 					restoreValues(values, range);
6211 
6212 					// "Continue this loop from the beginning."
6213 					continue;
6214 				}
6215 
6216 				// "Wrap sublist, with sibling criteria returning true for an HTML
6217 				// element with local name tag name and false otherwise, and new
6218 				// parent instructions being the following:"
6219 				// . . .
6220 				// "Fix disallowed ancestors of the previous step's result."
6221 				fixDisallowedAncestors(wrap(
6222 					sublist,
6223 					makeIsElementPred(tagName),
6224 					makeCreateElementSublist(tagName, sublist, range),
6225 					range
6226 				), range);
6227 			}
6228 		}
6229 	}
6230 
6231 
6232 	//@}
6233 	///// Justifying the selection /////
6234 	//@{
6235 
6236 	function justifySelection(alignment, range) {
6237 
6238 		// "Block-extend the active range, and let new range be the result."
6239 		var newRange = blockExtend(range);
6240 
6241 		// "Let element list be a list of all editable Elements contained in new
6242 		// range that either has an attribute in the HTML namespace whose local
6243 		// name is "align", or has a style attribute that sets "text-align", or is
6244 		// a center."
6245 		var elementList = getAllContainedNodes(newRange, function (node) {
6246 			return node.nodeType == $_.Node.ELEMENT_NODE && isEditable(node)
6247 			// Ignoring namespaces here
6248 				&& (hasAttribute(node, "align") || node.style.textAlign != "" || isNamedHtmlElement(node, 'center'));
6249 		});
6250 
6251 		// "For each element in element list:"
6252 		var i;
6253 		for (i = 0; i < elementList.length; i++) {
6254 			var element = elementList[i];
6255 
6256 			// "If element has an attribute in the HTML namespace whose local name
6257 			// is "align", remove that attribute."
6258 			element.removeAttribute("align");
6259 
6260 			// "Unset the CSS property "text-align" on element, if it's set by a
6261 			// style attribute."
6262 			element.style.textAlign = "";
6263 			if (element.getAttribute("style") == "") {
6264 				element.removeAttribute("style");
6265 			}
6266 
6267 			// "If element is a div or span or center with no attributes, remove
6268 			// it, preserving its descendants."
6269 			if (isHtmlElementInArray(element, ["div", "span", "center"]) && !element.attributes.length) {
6270 				removePreservingDescendants(element, range);
6271 			}
6272 
6273 			// "If element is a center with one or more attributes, set the tag
6274 			// name of element to "div"."
6275 			if (isNamedHtmlElement(element, 'center') && element.attributes.length) {
6276 				setTagName(element, "div", range);
6277 			}
6278 		}
6279 
6280 		// "Block-extend the active range, and let new range be the result."
6281 		newRange = blockExtend(globalRange);
6282 
6283 		// "Let node list be a list of nodes, initially empty."
6284 		var nodeList = [];
6285 
6286 		// "For each node node contained in new range, append node to node list if
6287 		// the last member of node list (if any) is not an ancestor of node; node
6288 		// is editable; node is an allowed child of "div"; and node's alignment
6289 		// value is not alignment."
6290 		nodeList = getContainedNodes(newRange, function (node) {
6291 			return isEditable(node) && isAllowedChild(node, "div") && getAlignmentValue(node) != alignment;
6292 		});
6293 
6294 		function makeIsAlignedDiv(alignment) {
6295 			return function (node) {
6296 				return isNamedHtmlElement(node, 'div') && $_(node.attributes).every(function (attr) {
6297 					return (attr.name == "align" && attr.value.toLowerCase() == alignment) || (attr.name == "style" && getStyleLength(node) == 1 && node.style.textAlign == alignment);
6298 				});
6299 			};
6300 		}
6301 
6302 		function makeCreateAlignedDiv(alignment) {
6303 			return function () {
6304 				var newParent = document.createElement("div");
6305 				newParent.setAttribute("style", "text-align: " + alignment);
6306 				return newParent;
6307 			};
6308 		}
6309 
6310 		// "While node list is not empty:"
6311 		while (nodeList.length) {
6312 			// "Let sublist be a list of nodes, initially empty."
6313 			var sublist = [];
6314 
6315 			// "Remove the first member of node list and append it to sublist."
6316 			sublist.push(nodeList.shift());
6317 
6318 			// "While node list is not empty, and the first member of node list is
6319 			// the nextSibling of the last member of sublist, remove the first
6320 			// member of node list and append it to sublist."
6321 			while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling) {
6322 				sublist.push(nodeList.shift());
6323 			}
6324 
6325 			// "Wrap sublist. Sibling criteria returns true for any div that has
6326 			// one or both of the following two attributes and no other attributes,
6327 			// and false otherwise:"
6328 			//
6329 			//   * "An align attribute whose value is an ASCII case-insensitive
6330 			//     match for alignment.
6331 			//   * "A style attribute which sets exactly one CSS property
6332 			//     (including unrecognized or invalid attributes), which is
6333 			//     "text-align", which is set to alignment.
6334 			//
6335 			// "New parent instructions are to call createElement("div") on the
6336 			// context object, then set its CSS property "text-align" to alignment
6337 			// and return the result."
6338 			wrap(
6339 				sublist,
6340 				makeIsAlignedDiv(alignment),
6341 				makeCreateAlignedDiv(alignment),
6342 				range
6343 			);
6344 		}
6345 	}
6346 
6347 	//@}
6348 	///// Move the given collapsed range over adjacent zero-width whitespace characters.
6349 	///// The range is 
6350 	//@{
6351 	/**
6352 	 * Move the given collapsed range over adjacent zero-width whitespace characters.
6353 	 * If the range is not collapsed or is not contained in a text node, it is not modified
6354 	 * @param range range to modify
6355 	 * @param forward {Boolean} true to move forward, false to move backward
6356 	 */
6357 	function moveOverZWSP(range, forward) {
6358 		var offset;
6359 		if (!range.collapsed) {
6360 			return;
6361 		}
6362 
6363 		offset = range.startOffset;
6364 
6365 		if (forward) {
6366 			// check whether the range starts in a text node
6367 			if (range.startContainer && range.startContainer.nodeType === $_.Node.TEXT_NODE) {
6368 				// move forward (i.e. increase offset) as long as we stay in the text node and have zwsp characters to the right
6369 				while (offset < range.startContainer.data.length && range.startContainer.data.charAt(offset) === '\u200b') {
6370 					offset++;
6371 				}
6372 			}
6373 		} else {
6374 			// check whether the range starts in a text node
6375 			if (range.startContainer && range.startContainer.nodeType === $_.Node.TEXT_NODE) {
6376 				// move backward (i.e. decrease offset) as long as we stay in the text node and have zwsp characters to the left
6377 				while (offset > 0 && range.startContainer.data.charAt(offset - 1) === '\u200b') {
6378 					offset--;
6379 				}
6380 			}
6381 		}
6382 
6383 		// if the offset was changed, set it back to the collapsed range
6384 		if (offset !== range.startOffset) {
6385 			range.setStart(range.startContainer, offset);
6386 			range.setEnd(range.startContainer, offset);
6387 		}
6388 	}
6389 
6390 	/**
6391 	 * implementation of the delete command
6392 	 * will attempt to delete contents within range if non-collapsed
6393 	 * or delete the character left of the cursor position if range
6394 	 * is collapsed. Is used to define the behaviour of the backspace
6395 	 * button.
6396 	 *
6397 	 * @param      value   is just there for compatibility with the commands api. parameter is ignored.
6398 	 * @param      range   the range to execute the delete command for
6399 	 * @return     void
6400 	 */
6401 	commands["delete"] = {
6402 		action: function (value, range) {
6403 			var i;
6404 
6405 			// special behaviour for skipping zero-width whitespaces in IE7
6406 			if (jQuery.browser.msie && jQuery.browser.version <= 7) {
6407 				moveOverZWSP(range, false);
6408 			}
6409 
6410 			// "If the active range is not collapsed, delete the contents of the
6411 			// active range and abort these steps."
6412 			if (!range.collapsed) {
6413 				deleteContents(range);
6414 				return;
6415 			}
6416 
6417 			// "Canonicalize whitespace at (active range's start node, active
6418 			// range's start offset)."
6419 			canonicalizeWhitespace(range.startContainer, range.startOffset);
6420 
6421 			// "Let node and offset be the active range's start node and offset."
6422 			var node = range.startContainer;
6423 			var offset = range.startOffset;
6424 			var isBr = false;
6425 			var isHr = false;
6426 
6427 			// "Repeat the following steps:"
6428 			while (true) {
6429 				// we need to reset isBr and isHr on every interation of the loop
6430 				if (offset > 0) {
6431 					isBr = isNamedHtmlElement(node.childNodes[offset - 1], "br") || false;
6432 					isHr = isNamedHtmlElement(node.childNodes[offset - 1], "hr") || false;
6433 				}
6434 				// "If offset is zero and node's previousSibling is an editable
6435 				// invisible node, remove node's previousSibling from its parent."
6436 				if (offset == 0 && isEditable(node.previousSibling) && isInvisible(node.previousSibling)) {
6437 					node.parentNode.removeChild(node.previousSibling);
6438 					continue;
6439 				}
6440 				// "Otherwise, if node has a child with index offset − 1 and that
6441 				// child is an editable invisible node, remove that child from
6442 				// node, then subtract one from offset."
6443 				if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isEditable(node.childNodes[offset - 1]) && (isInvisible(node.childNodes[offset - 1]) || isBr || isHr)) {
6444 					node.removeChild(node.childNodes[offset - 1]);
6445 					offset--;
6446 					if (isBr || isHr) {
6447 						range.setStart(node, offset);
6448 						range.setEnd(node, offset);
6449 						return;
6450 					}
6451 					continue;
6452 
6453 				}
6454 				// "Otherwise, if offset is zero and node is an inline node, or if
6455 				// node is an invisible node, set offset to the index of node, then
6456 				// set node to its parent."
6457 				if ((offset == 0 && isInlineNode(node)) || isInvisible(node)) {
6458 					offset = getNodeIndex(node);
6459 					node = node.parentNode;
6460 					continue;
6461 				}
6462 				// "Otherwise, if node has a child with index offset − 1 and that
6463 				// child is an editable a, remove that child from node, preserving
6464 				// its descendants. Then abort these steps."
6465 				if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isEditable(node.childNodes[offset - 1]) && isNamedHtmlElement(node.childNodes[offset - 1], "a")) {
6466 					removePreservingDescendants(node.childNodes[offset - 1], range);
6467 					return;
6468 
6469 				}
6470 				// "Otherwise, if node has a child with index offset − 1 and that
6471 				// child is not a block node or a br or an img, set node to that
6472 				// child, then set offset to the length of node."
6473 				if (0 <= offset - 1 && offset - 1 < node.childNodes.length && !isBlockNode(node.childNodes[offset - 1]) && !isHtmlElementInArray(node.childNodes[offset - 1], ["br", "img"])) {
6474 					node = node.childNodes[offset - 1];
6475 					offset = getNodeLength(node);
6476 					continue;
6477 				}
6478 				// "Otherwise, break from this loop."
6479 				// brk is a quick and dirty jslint workaround since I don't want to rewrite this loop
6480 				var brk = true;
6481 				if (brk) {
6482 					break;
6483 				}
6484 			}
6485 
6486 			// if the previous node is an aloha-table we want to delete it
6487 			var delBlock = getBlockAtPreviousPosition(node, offset);
6488 			if (delBlock) {
6489 				delBlock.parentNode.removeChild(delBlock);
6490 				return;
6491 			}
6492 
6493 			// "If node is a Text node and offset is not zero, call collapse(node,
6494 			// offset) on the Selection. Then delete the contents of the range with
6495 			// start (node, offset − 1) and end (node, offset) and abort these
6496 			// steps."
6497 			if (node.nodeType == $_.Node.TEXT_NODE && offset != 0) {
6498 				range.setStart(node, offset - 1);
6499 				range.setEnd(node, offset - 1);
6500 				deleteContents(node, offset - 1, node, offset);
6501 				return;
6502 			}
6503 6504 
			// @iebug
6505 			// when inserting a special char via the plugin 
6506 			// there where problems deleting them again with backspace after insertation
6507 			// see https://github.com/alohaeditor/Aloha-Editor/issues/517
6508 			if (node.nodeType == $_.Node.TEXT_NODE && offset == 0 && jQuery.browser.msie) {
6509 				offset = 1;
6510 				range.setStart(node, offset);
6511 				range.setEnd(node, offset);
6512 				range.startOffset = 0;
6513 				deleteContents(range);
6514 				return;
6515 			}
6516 
6517 			// "If node is an inline node, abort these steps."
6518 			if (isInlineNode(node)) {
6519 				return;
6520 			}
6521 
6522 			// "If node has a child with index offset − 1 and that child is a br or
6523 			// hr or img, call collapse(node, offset) on the Selection. Then delete
6524 			// the contents of the range with start (node, offset − 1) and end
6525 			// (node, offset) and abort these steps."
6526 			if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isHtmlElementInArray(node.childNodes[offset - 1], ["br", "hr", "img"])) {
6527 				range.setStart(node, offset);
6528 				range.setEnd(node, offset);
6529 				deleteContents(range);
6530 				return;
6531 			}
6532 
6533 			// "If node is an li or dt or dd and is the first child of its parent,
6534 6535 			// and offset is zero:"
6536 			if (isHtmlElementInArray(node, ["li", "dt", "dd"]) && node == node.parentNode.firstChild && offset == 0) {
6537 				// "Let items be a list of all lis that are ancestors of node."
6538 				//
6539 				// Remember, must be in tree order.
6540 				var items = [];
6541 				var ancestor;
6542 				for (ancestor = node.parentNode; ancestor; ancestor = ancestor.parentNode) {
6543 					if (isNamedHtmlElement(ancestor, 'li')) {
6544 						items.unshift(ancestor);
6545 					}
6546 				}
6547 
6548 				// "Normalize sublists of each item in items."
6549 				for (i = 0; i < items.length; i++) {
6550 					normalizeSublists(items[i], range);
6551 				}
6552 
6553 				// "Record the values of the one-node list consisting of node, and
6554 				// let values be the result."
6555 				var values = recordValues([node]);
6556 
6557 				// "Split the parent of the one-node list consisting of node."
6558 				splitParent([node], range);
6559 
6560 				// "Restore the values from values."
6561 				restoreValues(values, range);
6562 
6563 				// "If node is a dd or dt, and it is not an allowed child of any of
6564 				// its ancestors in the same editing host, set the tag name of node
6565 				// to the default single-line container name and let node be the
6566 				// result."
6567 				if (isHtmlElementInArray(node, ["dd", "dt"]) && $_(getAncestors(node)).every(function (ancestor) { return !inSameEditingHost(node, ancestor) || !isAllowedChild(node, ancestor); })) {
6568 					node = setTagName(node, defaultSingleLineContainerName, range);
6569 				}
6570 
6571 				// "Fix disallowed ancestors of node."
6572 				fixDisallowedAncestors(node, range);
6573 
6574 				// fix the lists to be html5 conformant
6575 				for (i = 0; i < items.length; i++) {
6576 					unNormalizeSublists(items[i].parentNode, range);
6577 				}
6578 
6579 				// "Abort these steps."
6580 				return;
6581 			}
6582 
6583 			// "Let start node equal node and let start offset equal offset."
6584 			var startNode = node;
6585 			var startOffset = offset;
6586 
6587 			// "Repeat the following steps:"
6588 			while (true) {
6589 				// "If start offset is zero, set start offset to the index of start
6590 				// node and then set start node to its parent."
6591 				if (startOffset == 0) {
6592 					startOffset = getNodeIndex(startNode);
6593 					startNode = startNode.parentNode;
6594 
6595 					// "Otherwise, if start node has an editable invisible child with
6596 					// index start offset minus one, remove it from start node and
6597 					// subtract one from start offset."
6598 				} else if (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length && isEditable(startNode.childNodes[startOffset - 1]) && isInvisible(startNode.childNodes[startOffset - 1])) {
6599 					startNode.removeChild(startNode.childNodes[startOffset - 1]);
6600 					startOffset--;
6601 
6602 					// "Otherwise, break from this loop."
6603 				} else {
6604 					break;
6605 				}
6606 			}
6607 
6608 			// "If offset is zero, and node has an editable ancestor container in
6609 			// the same editing host that's an indentation element:"
6610 			if (offset == 0 && $_(getAncestors(node).concat(node)).filter(function (ancestor) { return isEditable(ancestor) && inSameEditingHost(ancestor, node) && isIndentationElement(ancestor); }).length) {
6611 				// "Block-extend the range whose start and end are both (node, 0),
6612 				// and let new range be the result."
6613 				var newRange = Aloha.createRange();
6614 				newRange.setStart(node, 0);
6615 				newRange.setEnd(node, 0);
6616 				newRange = blockExtend(newRange);
6617 
6618 				// "Let node list be a list of nodes, initially empty."
6619 				//
6620 				// "For each node current node contained in new range, append
6621 				// current node to node list if the last member of node list (if
6622 				// any) is not an ancestor of current node, and current node is
6623 				// editable but has no editable descendants."
6624 				var nodeList = getContainedNodes(newRange, function (currentNode) {
6625 					return isEditable(currentNode) && !hasEditableDescendants(currentNode);
6626 				});
6627 
6628 				// "Outdent each node in node list."
6629 				for (i = 0; i < nodeList.length; i++) {
6630 6631 					outdentNode(nodeList[i], range);
6632 				}
6633 
6634 				// "Abort these steps."
6635 				return;
6636 			}
6637 
6638 			// "If the child of start node with index start offset is a table,
6639 			// abort these steps."
6640 			if (isNamedHtmlElement(startNode.childNodes[startOffset], "table")) {
6641 				return;
6642 			}
6643 
6644 			// "If start node has a child with index start offset − 1, and that
6645 			// child is a table:"
6646 			if (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length && isNamedHtmlElement(startNode.childNodes[startOffset - 1], "table")) {
6647 				// "Call collapse(start node, start offset − 1) on the context
6648 				// object's Selection."
6649 				range.setStart(startNode, startOffset - 1);
6650 
6651 				// "Call extend(start node, start offset) on the context object's
6652 				// Selection."
6653 				range.setEnd(startNode, startOffset);
6654 
6655 				// "Abort these steps."
6656 				return;
6657 			}
6658 
6659 			// "If offset is zero; and either the child of start node with index
6660 			// start offset minus one is an hr, or the child is a br whose
6661 			// previousSibling is either a br or not an inline node:"
6662 			if (offset == 0
6663 				    && (isNamedHtmlElement(startNode.childNodes[startOffset - 1], "hr")
6664 						|| (isNamedHtmlElement(startNode.childNodes[startOffset - 1], "br")
6665 							&& (isNamedHtmlElement(startNode.childNodes[startOffset - 1].previousSibling, "br")
6666 								|| !isInlineNode(startNode.childNodes[startOffset - 1].previousSibling))))) {
6667 				// "Call collapse(node, offset) on the Selection."
6668 				range.setStart(node, offset);
6669 				range.setEnd(node, offset);
6670 
6671 				// "Delete the contents of the range with start (start node, start
6672 				// offset − 1) and end (start node, start offset)."
6673 				deleteContents(startNode, startOffset - 1, startNode, startOffset);
6674 
6675 				// "Abort these steps."
6676 				return;
6677 			}
6678 
6679 			// "If the child of start node with index start offset is an li or dt
6680 			// or dd, and that child's firstChild is an inline node, and start
6681 			// offset is not zero:"
6682 			if (isHtmlElementInArray(startNode.childNodes[startOffset], ["li", "dt", "dd"]) && isInlineNode(startNode.childNodes[startOffset].firstChild) && startOffset != 0) {
6683 				// "Let previous item be the child of start node with index start
6684 				// offset minus one."
6685 				var previousItem = startNode.childNodes[startOffset - 1];
6686 
6687 				// "If previous item's lastChild is an inline node other than a br,
6688 				// call createElement("br") on the context object and append the
6689 				// result as the last child of previous item."
6690 				if (isInlineNode(previousItem.lastChild) && !isNamedHtmlElement(previousItem.lastChild, "br")) {
6691 					previousItem.appendChild(document.createElement("br"));
6692 				}
6693 
6694 				// "If previous item's lastChild is an inline node, call
6695 				// createElement("br") on the context object and append the result
6696 				// as the last child of previous item."
6697 				if (isInlineNode(previousItem.lastChild)) {
6698 					previousItem.appendChild(document.createElement("br"));
6699 				}
6700 			}
6701 
6702 			// "If the child of start node with index start offset is an li or dt
6703 			// or dd, and its previousSibling is also an li or dt or dd, set start
6704 			// node to its child with index start offset − 1, then set start offset
6705 			// to start node's length, then set node to start node's nextSibling,
6706 			// then set offset to 0."
6707 			if (isHtmlElementInArray(startNode.childNodes[startOffset], ["li", "dt", "dd"]) && isHtmlElementInArray(startNode.childNodes[startOffset - 1], ["li", "dt", "dd"])) {
6708 				startNode = startNode.childNodes[startOffset - 1];
6709 				startOffset = getNodeLength(startNode);
6710 				node = startNode.nextSibling;
6711 				offset = 0;
6712 
6713 				// "Otherwise, while start node has a child with index start offset
6714 				// minus one:"
6715 			} else {
6716 				while (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length) {
6717 					// "If start node's child with index start offset minus one is
6718 					// editable and invisible, remove it from start node, then
6719 					// subtract one from start offset."
6720 					if (isEditable(startNode.childNodes[startOffset - 1]) && isInvisible(startNode.childNodes[startOffset - 1])) {
6721 						startNode.removeChild(startNode.childNodes[startOffset - 1]);
6722 						startOffset--;
6723 
6724 						// "Otherwise, set start node to its child with index start
6725 						// offset minus one, then set start offset to the length of
6726 						// start node."
6727 					} else {
6728 						startNode = startNode.childNodes[startOffset - 1];
6729 						startOffset = getNodeLength(startNode);
6730 					}
6731 				}
6732 			}
6733 
6734 			// "Delete the contents of the range with start (start node, start
6735 			// offset) and end (node, offset)."
6736 			var delRange = Aloha.createRange();
6737 			delRange.setStart(startNode, startOffset);
6738 			delRange.setEnd(node, offset);
6739 			deleteContents(delRange);
6740 
6741 			if (!isAncestorContainer(document.body, range.startContainer)) {
6742 				if (delRange.startContainer.hasChildNodes() || delRange.startContainer.nodeType == $_.Node.TEXT_NODE) {
6743 					range.setStart(delRange.startContainer, delRange.startOffset);
6744 					range.setEnd(delRange.startContainer, delRange.startOffset);
6745 				} else {
6746 					range.setStart(delRange.startContainer.parentNode, getNodeIndex(delRange.startContainer));
6747 					range.setEnd(delRange.startContainer.parentNode, getNodeIndex(delRange.startContainer));
6748 				}
6749 			}
6750 		}
6751 	};
6752 
6753 	//@}
6754 	///// The formatBlock command /////
6755 	//@{
6756 	// "A formattable block name is "address", "dd", "div", "dt", "h1", "h2", "h3",
6757 	// "h4", "h5", "h6", "p", or "pre"."
6758 	var formattableBlockNames = ["address", "dd", "div", "dt", "h1", "h2", "h3", "h4", "h5", "h6", "p", "pre"];
6759 
6760 	commands.formatblock = {
6761 		action: function (value) {
6762 			var i;
6763 
6764 			// "If value begins with a "<" character and ends with a ">" character,
6765 			// remove the first and last characters from it."
6766 			if (/^<.*>$/.test(value)) {
6767 				value = value.slice(1, -1);
6768 			}
6769 
6770 			// "Let value be converted to ASCII lowercase."
6771 			value = value.toLowerCase();
6772 
6773 			// "If value is not a formattable block name, abort these steps and do
6774 			// nothing."
6775 			if ($_(formattableBlockNames).indexOf(value) == -1) {
6776 				return;
6777 			}
6778 
6779 			// "Block-extend the active range, and let new range be the result."
6780 			var newRange = blockExtend(getActiveRange());
6781 
6782 			// "Let node list be an empty list of nodes."
6783 			//
6784 			// "For each node node contained in new range, append node to node list
6785 			// if it is editable, the last member of original node list (if any) is
6786 			// not an ancestor of node, node is either a non-list single-line
6787 			// container or an allowed child of "p" or a dd or dt, and node is not
6788 			// the ancestor of a prohibited paragraph child."
6789 			var nodeList = getContainedNodes(newRange, function (node) {
6790 				return isEditable(node) && (isNonListSingleLineContainer(node) || isAllowedChild(node, "p") || isHtmlElementInArray(node, ["dd", "dt"])) && !$_(getDescendants(node)).some(isProhibitedParagraphChild);
6791 			});
6792 
6793 			// "Record the values of node list, and let values be the result."
6794 			var values = recordValues(nodeList);
6795 
6796 			function makeIsEditableElementInSameEditingHostDoesNotContainProhibitedParagraphChildren(node) {
6797 				return function (ancestor) {
6798 					return (isEditable(ancestor)
6799 							&& inSameEditingHost(ancestor, node)
6800 							&& isHtmlElement_obsolete(ancestor, formattableBlockNames)
6801 							&& !$_(getDescendants(ancestor)).some(isProhibitedParagraphChild));
6802 				};
6803 			}
6804 
6805 			function makeIsElementWithoutAttributes(value) {
6806 				return function (node) {
6807 					return isHtmlElement_obsolete(node, value) && !node.attributes.length;
6808 				};
6809 			}
6810 
6811 			function returnFalse() {
6812 				return false;
6813 			}
6814 
6815 			function makeCreateElement(value) {
6816 				return function () {
6817 					return document.createElement(value);
6818 				};
6819 			}
6820 
6821 			// "For each node in node list, while node is the descendant of an
6822 			// editable HTML element in the same editing host, whose local name is
6823 			// a formattable block name, and which is not the ancestor of a
6824 			// prohibited paragraph child, split the parent of the one-node list
6825 			// consisting of node."
6826 			for (i = 0; i < nodeList.length; i++) {
6827 				var node = nodeList[i];
6828 				while ($_(getAncestors(node)).some(makeIsEditableElementInSameEditingHostDoesNotContainProhibitedParagraphChildren(node))) {
6829 					splitParent([node], newRange);
6830 				}
6831 			}
6832 
6833 			// "Restore the values from values."
6834 			restoreValues(values, newRange);
6835 
6836 			// "While node list is not empty:"
6837 			while (nodeList.length) {
6838 				var sublist;
6839 
6840 				// "If the first member of node list is a single-line
6841 				// container:"
6842 				if (isSingleLineContainer(nodeList[0])) {
6843 					// "Let sublist be the children of the first member of node
6844 					// list."
6845 					sublist = [].slice.call(toArray(nodeList[0].childNodes));
6846 
6847 					// "Record the values of sublist, and let values be the
6848 					// result."
6849 					values = recordValues(sublist);
6850 
6851 					// "Remove the first member of node list from its parent,
6852 					// preserving its descendants."
6853 					removePreservingDescendants(nodeList[0], newRange);
6854 
6855 					// "Restore the values from values."
6856 					restoreValues(values, newRange);
6857 
6858 					// "Remove the first member from node list."
6859 					nodeList.shift();
6860 
6861 					// "Otherwise:"
6862 				} else {
6863 					// "Let sublist be an empty list of nodes."
6864 					sublist = [];
6865 
6866 					// "Remove the first member of node list and append it to
6867 					// sublist."
6868 					sublist.push(nodeList.shift());
6869 
6870 					// "While node list is not empty, and the first member of
6871 					// node list is the nextSibling of the last member of
6872 					// sublist, and the first member of node list is not a
6873 					// single-line container, and the last member of sublist is
6874 					// not a br, remove the first member of node list and
6875 					// append it to sublist."
6876 					while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isSingleLineContainer(nodeList[0]) && !isNamedHtmlElement(sublist[sublist.length - 1], "BR")) {
6877 						sublist.push(nodeList.shift());
6878 					}
6879 				}
6880 
6881 				// "Wrap sublist. If value is "div" or "p", sibling criteria
6882 				// returns false; otherwise it returns true for an HTML element
6883 				// with local name value and no attributes, and false otherwise.
6884 				// New parent instructions return the result of running
6885 				// createElement(value) on the context object. Then fix disallowed
6886 				// ancestors of the result."
6887 				fixDisallowedAncestors(wrap(
6888 					sublist,
6889 					jQuery.inArray(value, ["div", "p"]) == -1 ? makeIsElementWithoutAttributes(value) : returnFalse,
6890 					makeCreateElement(value),
6891 					newRange
6892 				), newRange);
6893 			}
6894 		},
6895 		indeterm: function () {
6896 			// "Block-extend the active range, and let new range be the result."
6897 			var newRange = blockExtend(getActiveRange());
6898 
6899 			// "Let node list be all visible editable nodes that are contained in
6900 			// new range and have no children."
6901 			var nodeList = getAllContainedNodes(newRange, function (node) {
6902 				return isVisible(node) && isEditable(node) && !node.hasChildNodes();
6903 			});
6904 
6905 			// "If node list is empty, return false."
6906 			if (!nodeList.length) {
6907 				return false;
6908 			}
6909 
6910 			// "Let type be null."
6911 			var type = null;
6912 
6913 			// "For each node in node list:"
6914 			var i;
6915 			for (i = 0; i < nodeList.length; i++) {
6916 				var node = nodeList[i];
6917 
6918 				// "While node's parent is editable and in the same editing host as
6919 				// node, and node is not an HTML element whose local name is a
6920 				// formattable block name, set node to its parent."
6921 				while (isEditable(node.parentNode) && inSameEditingHost(node, node.parentNode) && !isHtmlElement_obsolete(node, formattableBlockNames)) {
6922 					node = node.parentNode;
6923 				}
6924 
6925 				// "Let current type be the empty string."
6926 				var currentType = "";
6927 
6928 				// "If node is an editable HTML element whose local name is a
6929 				// formattable block name, and node is not the ancestor of a
6930 				// prohibited paragraph child, set current type to node's local
6931 				// name."
6932 				if (isEditable(node) && isHtmlElement_obsolete(node, formattableBlockNames) && !$_(getDescendants(node)).some(isProhibitedParagraphChild)) {
6933 					currentType = node.tagName;
6934 				}
6935 
6936 				// "If type is null, set type to current type."
6937 				if (type === null) {
6938 					type = currentType;
6939 
6940 					// "Otherwise, if type does not equal current type, return true."
6941 				} else if (type != currentType) {
6942 					return true;
6943 				}
6944 			}
6945 
6946 			// "Return false."
6947 			return false;
6948 		},
6949 		value: function () {
6950 			// "Block-extend the active range, and let new range be the result."
6951 			var newRange = blockExtend(getActiveRange());
6952 
6953 			// "Let node be the first visible editable node that is contained in
6954 			// new range and has no children. If there is no such node, return the
6955 			// empty string."
6956 			var nodes = getAllContainedNodes(newRange, function (node) {
6957 				return isVisible(node) && isEditable(node) && !node.hasChildNodes();
6958 			});
6959 			if (!nodes.length) {
6960 				return "";
6961 			}
6962 			var node = nodes[0];
6963 
6964 			// "While node's parent is editable and in the same editing host as
6965 			// node, and node is not an HTML element whose local name is a
6966 			// formattable block name, set node to its parent."
6967 			while (isEditable(node.parentNode) && inSameEditingHost(node, node.parentNode) && !isHtmlElement_obsolete(node, formattableBlockNames)) {
6968 				node = node.parentNode;
6969 			}
6970 
6971 			// "If node is an editable HTML element whose local name is a
6972 			// formattable block name, and node is not the ancestor of a prohibited
6973 			// paragraph child, return node's local name, converted to ASCII
6974 			// lowercase."
6975 			if (isEditable(node) && isHtmlElement_obsolete(node, formattableBlockNames) && !$_(getDescendants(node)).some(isProhibitedParagraphChild)) {
6976 				return node.tagName.toLowerCase();
6977 			}
6978 
6979 			// "Return the empty string."
6980 			return "";
6981 		}
6982 	};
6983 
6984 	//@}
6985 	///// The forwardDelete command /////
6986 	//@{
6987 	commands.forwarddelete = {
6988 		action: function (value, range) {
6989 			// special behaviour for skipping zero-width whitespaces in IE7
6990 			if (jQuery.browser.msie && jQuery.browser.version <= 7) {
6991 				moveOverZWSP(range, true);
6992 			}
6993 
6994 			// "If the active range is not collapsed, delete the contents of the
6995 			// active range and abort these steps."
6996 			if (!range.collapsed) {
6997 				deleteContents(range);
6998 				return;
6999 			}
7000 
7001 			// "Canonicalize whitespace at (active range's start node, active
7002 			// range's start offset)."
7003 			canonicalizeWhitespace(range.startContainer, range.startOffset);
7004 
7005 			// "Let node and offset be the active range's start node and offset."
7006 			var node = range.startContainer;
7007 			var offset = range.startOffset;
7008 			var isBr = false;
7009 			var isHr = false;
7010 
7011 			// "Repeat the following steps:"
7012 			while (true) {
7013 				// check whether the next element is a br or hr
7014 				// Commented out for unknown reason.
7015 				//if (offset < node.childNodes.length) {
7016 				//				isBr = isHtmlElement_obsolete(node.childNodes[offset], "br") || false;
7017 				//				isHr = isHtmlElement_obsolete(node.childNodes[offset], "hr") || false;
7018 				//}
7019 
7020 				// "If offset is the length of node and node's nextSibling is an
7021 				// editable invisible node, remove node's nextSibling from its
7022 				// parent."
7023 				if (offset == getNodeLength(node) && isEditable(node.nextSibling) && isInvisible(node.nextSibling)) {
7024 					node.parentNode.removeChild(node.nextSibling);
7025 
7026 					// "Otherwise, if node has a child with index offset and that child
7027 					// is an editable invisible node, remove that child from node."
7028 				} else if (offset < node.childNodes.length && isEditable(node.childNodes[offset]) && (isInvisible(node.childNodes[offset]) || isBr || isHr)) {
7029 					node.removeChild(node.childNodes[offset]);
7030 					if (isBr || isHr) {
7031 						ensureContainerEditable(node);
7032 						range.setStart(node, offset);
7033 						range.setEnd(node, offset);
7034 						return;
7035 					}
7036 
7037 					// "Otherwise, if node has a child with index offset and that child
7038 					// is a collapsed block prop, add one to offset."
7039 				} else if (offset < node.childNodes.length && isCollapsedBlockProp(node.childNodes[offset])) {
7040 					offset++;
7041 
7042 					// "Otherwise, if offset is the length of node and node is an
7043 					// inline node, or if node is invisible, set offset to one plus the
7044 					// index of node, then set node to its parent."
7045 				} else if ((offset == getNodeLength(node) && isInlineNode(node)) || isInvisible(node)) {
7046 					offset = 1 + getNodeIndex(node);
7047 					node = node.parentNode;
7048 
7049 					// "Otherwise, if node has a child with index offset and that child
7050 					// is not a block node or a br or an img, set node to that child,
7051 					// then set offset to zero."
7052 				} else if (offset < node.childNodes.length && !isBlockNode(node.childNodes[offset]) && !isHtmlElementInArray(node.childNodes[offset], ["br", "img"])) {
7053 					node = node.childNodes[offset];
7054 					offset = 0;
7055 
7056 					// "Otherwise, break from this loop."
7057 				} else {
7058 					break;
7059 				}
7060 			}
7061 
7062 			// collapse whitespace in the node, if it is a text node
7063 			canonicalizeWhitespace(range.startContainer, range.startOffset);
7064 
7065 			// if the next node is an aloha-table we want to delete it
7066 			var delBlock = getBlockAtNextPosition(node, offset);
7067 			if (delBlock) {
7068 				delBlock.parentNode.removeChild(delBlock);
7069 				return;
7070 			}
7071 
7072 			var endOffset;
7073 			// "If node is a Text node and offset is not node's length:"
7074 			if (node.nodeType == $_.Node.TEXT_NODE && offset != getNodeLength(node)) {
7075 				// "Call collapse(node, offset) on the Selection."
7076 				range.setStart(node, offset);
7077 				range.setEnd(node, offset);
7078 
7079 				// "Let end offset be offset plus one."
7080 				endOffset = offset + 1;
7081 
7082 				// "While end offset is not node's length and the end offsetth
7083 				// element of node's data has general category M when interpreted
7084 				// as a Unicode code point, add one to end offset."
7085 				//
7086 				// TODO: Not even going to try handling anything beyond the most
7087 				// basic combining marks, since I couldn't find a good list.  I
7088 				// special-case a few Hebrew diacritics too to test basic coverage
7089 				// of non-Latin stuff.
7090 				while (endOffset != node.length && /^[\u0300-\u036f\u0591-\u05bd\u05c1\u05c2]$/.test(node.data[endOffset])) {
7091 					endOffset++;
7092 				}
7093 
7094 				// "Delete the contents of the range with start (node, offset) and
7095 				// end (node, end offset)."
7096 				deleteContents(node, offset, node, endOffset);
7097 
7098 				// "Abort these steps."
7099 				return;
7100 			}
7101 
7102 			// "If node is an inline node, abort these steps."
7103 			if (isInlineNode(node)) {
7104 				return;
7105 			}
7106 
7107 			// "If node has a child with index offset and that child is a br or hr
7108 			// or img, call collapse(node, offset) on the Selection. Then delete
7109 			// the contents of the range with start (node, offset) and end (node,
7110 			// offset + 1) and abort these steps."
7111 			if (offset < node.childNodes.length && isHtmlElementInArray(node.childNodes[offset], ["br", "hr", "img"])) {
7112 				range.setStart(node, offset);
7113 				range.setEnd(node, offset);
7114 				deleteContents(node, offset, node, offset + 1);
7115 				return;
7116 			}
7117 
7118 			// "Let end node equal node and let end offset equal offset."
7119 			var endNode = node;
7120 			endOffset = offset;
7121 
7122 			// "Repeat the following steps:"
7123 			while (true) {
7124 				// "If end offset is the length of end node, set end offset to one
7125 				// plus the index of end node and then set end node to its parent."
7126 				if (endOffset == getNodeLength(endNode)) {
7127 					endOffset = 1 + getNodeIndex(endNode);
7128 					endNode = endNode.parentNode;
7129 
7130 					// "Otherwise, if end node has a an editable invisible child with
7131 					// index end offset, remove it from end node."
7132 				} else if (endOffset < endNode.childNodes.length && isEditable(endNode.childNodes[endOffset]) && isInvisible(endNode.childNodes[endOffset])) {
7133 					endNode.removeChild(endNode.childNodes[endOffset]);
7134 
7135 					// "Otherwise, break from this loop."
7136 				} else {
7137 					break;
7138 				}
7139 			}
7140 
7141 			// "If the child of end node with index end offset minus one is a
7142 			// table, abort these steps."
7143 			if (isNamedHtmlElement(endNode.childNodes[endOffset - 1], "table")) {
7144 				return;
7145 			}
7146 
7147 			// "If the child of end node with index end offset is a table:"
7148 			if (isNamedHtmlElement(endNode.childNodes[endOffset], "table")) {
7149 				// "Call collapse(end node, end offset) on the context object's
7150 				// Selection."
7151 				range.setStart(endNode, endOffset);
7152 
7153 				// "Call extend(end node, end offset + 1) on the context object's
7154 				// Selection."
7155 				range.setEnd(endNode, endOffset + 1);
7156 
7157 				// "Abort these steps."
7158 				return;
7159 			}
7160 
7161 			// "If offset is the length of node, and the child of end node with
7162 			// index end offset is an hr or br:"
7163 			if (offset == getNodeLength(node) && isHtmlElementInArray(endNode.childNodes[endOffset], ["br", "hr"])) {
7164 				// "Call collapse(node, offset) on the Selection."
7165 				range.setStart(node, offset);
7166 				range.setEnd(node, offset);
7167 
7168 				// "Delete the contents of the range with end (end node, end
7169 				// offset) and end (end node, end offset + 1)."
7170 				deleteContents(endNode, endOffset, endNode, endOffset + 1);
7171 
7172 				// "Abort these steps."
7173 				return;
7174 			}
7175 
7176 			// "While end node has a child with index end offset:"
7177 			while (endOffset < endNode.childNodes.length) {
7178 				// "If end node's child with index end offset is editable and
7179 				// invisible, remove it from end node."
7180 				if (isEditable(endNode.childNodes[endOffset]) && isInvisible(endNode.childNodes[endOffset])) {
7181 					endNode.removeChild(endNode.childNodes[endOffset]);
7182 
7183 					// "Otherwise, set end node to its child with index end offset and
7184 					// set end offset to zero."
7185 				} else {
7186 					endNode = endNode.childNodes[endOffset];
7187 					endOffset = 0;
7188 				}
7189 			}
7190 
7191 			// "Delete the contents of the range with start (node, offset) and end
7192 			// (end node, end offset)."
7193 			deleteContents(node, offset, endNode, endOffset);
7194 		}
7195 	};
7196 
7197 	//@}
7198 	///// The indent command /////
7199 	//@{
7200 	commands.indent = {
7201 		action: function () {
7202 			// "Let items be a list of all lis that are ancestor containers of the
7203 			// active range's start and/or end node."
7204 			//
7205 			// Has to be in tree order, remember!
7206 			var items = [];
7207 			var node;
7208 			for (node = getActiveRange().endContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) {
7209 				if (isNamedHtmlElement(node, "LI")) {
7210 					items.unshift(node);
7211 				}
7212 			}
7213 			for (node = getActiveRange().startContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) {
7214 				if (isNamedHtmlElement(node, "LI")) {
7215 					items.unshift(node);
7216 				}
7217 			}
7218 			for (node = getActiveRange().commonAncestorContainer; node; node = node.parentNode) {
7219 				if (isNamedHtmlElement(node, "LI")) {
7220 					items.unshift(node);
7221 				}
7222 			}
7223 
7224 			// "For each item in items, normalize sublists of item."
7225 			var i;
7226 			for (i = 0; i < items.length; i++) {
7227 				normalizeSublists(items[i], getActiveRange());
7228 			}
7229 
7230 			// "Block-extend the active range, and let new range be the result."
7231 			var newRange = blockExtend(getActiveRange());
7232 
7233 			// "Let node list be a list of nodes, initially empty."
7234 			var nodeList = [];
7235 
7236 			// "For each node node contained in new range, if node is editable and
7237 			// is an allowed child of "div" or "ol" and if the last member of node
7238 			// list (if any) is not an ancestor of node, append node to node list."
7239 			nodeList = getContainedNodes(newRange, function (node) {
7240 				return isEditable(node) && (isAllowedChild(node, "div") || isAllowedChild(node, "ol"));
7241 			});
7242 
7243 			// "If the first member of node list is an li whose parent is an ol or
7244 			// ul, and its previousSibling is an li as well, normalize sublists of
7245 			// its previousSibling."
7246 			if (nodeList.length && isNamedHtmlElement(nodeList[0], "LI") && isHtmlElementInArray(nodeList[0].parentNode, ["OL", "UL"]) && isNamedHtmlElement(nodeList[0].previousSibling, "LI")) {
7247 				normalizeSublists(nodeList[0].previousSibling, newRange);
7248 			}
7249 
7250 			// "While node list is not empty:"
7251 			while (nodeList.length) {
7252 				// "Let sublist be a list of nodes, initially empty."
7253 				var sublist = [];
7254 
7255 				// "Remove the first member of node list and append it to sublist."
7256 				sublist.push(nodeList.shift());
7257 
7258 				// "While the first member of node list is the nextSibling of the
7259 				// last member of sublist, remove the first member of node list and
7260 				// append it to sublist."
7261 				while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling) {
7262 					sublist.push(nodeList.shift());
7263 				}
7264 
7265 				// "Indent sublist."
7266 				indentNodes(sublist, newRange);
7267 			}
7268 		}
7269 	};
7270 
7271 	//@}
7272 	///// The insertHorizontalRule command /////
7273 	//@{
7274 	commands.inserthorizontalrule = {
7275 		action: function (value, range) {
7276 
7277 			// "While range's start offset is 0 and its start node's parent is not
7278 			// null, set range's start to (parent of start node, index of start
7279 			// node)."
7280 			while (range.startOffset == 0 && range.startContainer.parentNode) {
7281 				range.setStart(range.startContainer.parentNode, getNodeIndex(range.startContainer));
7282 			}
7283 
7284 			// "While range's end offset is the length of its end node, and its end
7285 			// node's parent is not null, set range's end to (parent of end node, 1
7286 			// + index of start node)."
7287 			while (range.endOffset == getNodeLength(range.endContainer) && range.endContainer.parentNode) {
7288 				range.setEnd(range.endContainer.parentNode, 1 + getNodeIndex(range.endContainer));
7289 			}
7290 
7291 			// "Delete the contents of range, with block merging false."
7292 			deleteContents(range, {
7293 				blockMerging: false
7294 			});
7295 
7296 			// "If the active range's start node is neither editable nor an editing
7297 			// host, abort these steps."
7298 			if (!isEditable(getActiveRange().startContainer) && !isEditingHost(getActiveRange().startContainer)) {
7299 				return;
7300 			}
7301 
7302 			// "If the active range's start node is a Text node and its start
7303 			// offset is zero, set the active range's start and end to (parent of
7304 			// start node, index of start node)."
7305 			if (getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset == 0) {
7306 				getActiveRange().setStart(getActiveRange().startContainer.parentNode, getNodeIndex(getActiveRange().startContainer));
7307 				getActiveRange().collapse(true);
7308 			}
7309 
7310 			// "If the active range's start node is a Text node and its start
7311 			// offset is the length of its start node, set the active range's start
7312 			// and end to (parent of start node, 1 + index of start node)."
7313 			if (getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset == getNodeLength(getActiveRange().startContainer)) {
7314 				getActiveRange().setStart(getActiveRange().startContainer.parentNode, 1 + getNodeIndex(getActiveRange().startContainer));
7315 				getActiveRange().collapse(true);
7316 			}
7317 
7318 			// "Let hr be the result of calling createElement("hr") on the
7319 			// context object."
7320 			var hr = document.createElement("hr");
7321 
7322 			// "Run insertNode(hr) on the range."
7323 			range.insertNode(hr);
7324 
7325 			// "Fix disallowed ancestors of hr."
7326 			fixDisallowedAncestors(hr, range);
7327 
7328 			// "Run collapse() on the Selection, with first argument equal to the
7329 			// parent of hr and the second argument equal to one plus the index of
7330 			// hr."
7331 			//
7332 			// Not everyone actually supports collapse(), so we do it manually
7333 			// instead.  Also, we need to modify the actual range we're given as
7334 			// well, for the sake of autoimplementation.html's range-filling-in.
7335 			range.setStart(hr.parentNode, 1 + getNodeIndex(hr));
7336 			range.setEnd(hr.parentNode, 1 + getNodeIndex(hr));
7337 			Aloha.getSelection().removeAllRanges();
7338 			Aloha.getSelection().addRange(range);
7339 		}
7340 	};
7341 
7342 	//@}
7343 	///// The insertHTML command /////
7344 	//@{
7345 	commands.inserthtml = {
7346 		action: function (value, range) {
7347 
7348 
7349 			// "Delete the contents of the active range."
7350 			deleteContents(range);
7351 
7352 			// "If the active range's start node is neither editable nor an editing
7353 			// host, abort these steps."
7354 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
7355 				return;
7356 			}
7357 
7358 			// "Let frag be the result of calling createContextualFragment(value)
7359 			// on the active range."
7360 			var frag = range.createContextualFragment(value);
7361 
7362 			// "Let last child be the lastChild of frag."
7363 			var lastChild = frag.lastChild;
7364 
7365 			// "If last child is null, abort these steps."
7366 			if (!lastChild) {
7367 				return;
7368 			}
7369 
7370 			// "Let descendants be all descendants of frag."
7371 			var descendants = getDescendants(frag);
7372 
7373 			// "If the active range's start node is a block node:"
7374 			if (isBlockNode(range.startContainer)) {
7375 				// "Let collapsed block props be all editable collapsed block prop
7376 				// children of the active range's start node that have index
7377 				// greater than or equal to the active range's start offset."
7378 				//
7379 				// "For each node in collapsed block props, remove node from its
7380 				// parent."
7381 				$_(range.startContainer.childNodes).filter(function (node, range) {
7382 					return isEditable(node) && isCollapsedBlockProp(node) && getNodeIndex(node) >= range.startOffset;
7383 				}, true).forEach(function (node) {
7384 					node.parentNode.removeChild(node);
7385 				});
7386 			}
7387 
7388 			// "Call insertNode(frag) on the active range."
7389 			range.insertNode(frag);
7390 
7391 			// "If the active range's start node is a block node with no visible
7392 			// children, call createElement("br") on the context object and append
7393 			// the result as the last child of the active range's start node."
7394 			if (isBlockNode(range.startContainer)) {
7395 				ensureContainerEditable(range.startContainer);
7396 			}
7397 
7398 			// "Call collapse() on the context object's Selection, with last
7399 			// child's parent as the first argument and one plus its index as the
7400 			// second."
7401 			range.setStart(lastChild.parentNode, 1 + getNodeIndex(lastChild));
7402 			range.setEnd(lastChild.parentNode, 1 + getNodeIndex(lastChild));
7403 
7404 			// "Fix disallowed ancestors of each member of descendants."
7405 			var i;
7406 			for (i = 0; i < descendants.length; i++) {
7407 				fixDisallowedAncestors(descendants[i], range);
7408 			}
7409 
7410 			setActiveRange(range);
7411 		}
7412 	};
7413 
7414 	//@}
7415 	///// The insertImage command /////
7416 	//@{
7417 	commands.insertimage = {
7418 		action: function (value) {
7419 			// "If value is the empty string, abort these steps and do nothing."
7420 			if (value === "") {
7421 				return;
7422 			}
7423 
7424 			// "Let range be the active range."
7425 			var range = getActiveRange();
7426 
7427 			// "Delete the contents of range, with strip wrappers false."
7428 			deleteContents(range, {
7429 				stripWrappers: false
7430 			});
7431 
7432 			// "If the active range's start node is neither editable nor an editing
7433 			// host, abort these steps."
7434 			if (!isEditable(getActiveRange().startContainer) && !isEditingHost(getActiveRange().startContainer)) {
7435 				return;
7436 			}
7437 
7438 			// "If range's start node is a block node whose sole child is a br, and
7439 			// its start offset is 0, remove its start node's child from it."
7440 			if (isBlockNode(range.startContainer) && range.startContainer.childNodes.length == 1 && isNamedHtmlElement(range.startContainer.firstChild, "br") && range.startOffset == 0) {
7441 				range.startContainer.removeChild(range.startContainer.firstChild);
7442 			}
7443 
7444 			// "Let img be the result of calling createElement("img") on the
7445 			// context object."
7446 			var img = document.createElement("img");
7447 
7448 			// "Run setAttribute("src", value) on img."
7449 			img.setAttribute("src", value);
7450 
7451 			// "Run insertNode(img) on the range."
7452 			range.insertNode(img);
7453 
7454 			// "Run collapse() on the Selection, with first argument equal to the
7455 			// parent of img and the second argument equal to one plus the index of
7456 			// img."
7457 			//
7458 			// Not everyone actually supports collapse(), so we do it manually
7459 			// instead.  Also, we need to modify the actual range we're given as
7460 			// well, for the sake of autoimplementation.html's range-filling-in.
7461 			range.setStart(img.parentNode, 1 + getNodeIndex(img));
7462 			range.setEnd(img.parentNode, 1 + getNodeIndex(img));
7463 			Aloha.getSelection().removeAllRanges();
7464 			Aloha.getSelection().addRange(range);
7465 
7466 			// IE adds width and height attributes for some reason, so remove those
7467 			// to actually do what the spec says.
7468 			img.removeAttribute("width");
7469 			img.removeAttribute("height");
7470 		}
7471 	};
7472 
7473 	//@}
7474 	///// The insertLineBreak command /////
7475 	//@{
7476 7477 	commands.insertlinebreak = {
7478 		action: function (value, range) {
7479 			// "Delete the contents of the active range, with strip wrappers false."
7480 			deleteContents(range, {
7481 				stripWrappers: false
7482 			});
7483 
7484 			// "If the active range's start node is neither editable nor an editing
7485 			// host, abort these steps."
7486 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
7487 				return;
7488 			}
7489 
7490 			// "If the active range's start node is an Element, and "br" is not an
7491 			// allowed child of it, abort these steps."
7492 			if (range.startContainer.nodeType == $_.Node.ELEMENT_NODE && !isAllowedChild("br", range.startContainer)) {
7493 				return;
7494 			}
7495 
7496 			// "If the active range's start node is not an Element, and "br" is not
7497 			// an allowed child of the active range's start node's parent, abort
7498 			// these steps."
7499 			if (range.startContainer.nodeType != $_.Node.ELEMENT_NODE && !isAllowedChild("br", range.startContainer.parentNode)) {
7500 				return;
7501 			}
7502 
7503 			// "If the active range's start node is a Text node and its start
7504 			// offset is zero, call collapse() on the context object's Selection,
7505 			// with first argument equal to the active range's start node's parent
7506 			// and second argument equal to the active range's start node's index."
7507 			var newNode, newOffset;
7508 			if (range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset == 0) {
7509 				newNode = range.startContainer.parentNode;
7510 				newOffset = getNodeIndex(range.startContainer);
7511 				Aloha.getSelection().collapse(newNode, newOffset);
7512 				range.setStart(newNode, newOffset);
7513 				range.setEnd(newNode, newOffset);
7514 			}
7515 
7516 			// "If the active range's start node is a Text node and its start
7517 			// offset is the length of its start node, call collapse() on the
7518 			// context object's Selection, with first argument equal to the active
7519 			// range's start node's parent and second argument equal to one plus
7520 			// the active range's start node's index."
7521 			if (range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset == getNodeLength(range.startContainer)) {
7522 				newNode = range.startContainer.parentNode;
7523 				newOffset = 1 + getNodeIndex(range.startContainer);
7524 				Aloha.getSelection().collapse(newNode, newOffset);
7525 				range.setStart(newNode, newOffset);
7526 				range.setEnd(newNode, newOffset);
7527 			}
7528 
7529 			// "Let br be the result of calling createElement("br") on the context
7530 			// object."
7531 			var br = document.createElement("br");
7532 
7533 			// "Call insertNode(br) on the active range."
7534 			range.insertNode(br);
7535 
7536 			// "Call collapse() on the context object's Selection, with br's parent
7537 			// as the first argument and one plus br's index as the second
7538 			// argument."
7539 			Aloha.getSelection().collapse(br.parentNode, 1 + getNodeIndex(br));
7540 			range.setStart(br.parentNode, 1 + getNodeIndex(br));
7541 			range.setEnd(br.parentNode, 1 + getNodeIndex(br));
7542 
7543 			// "If br is a collapsed line break, call createElement("br") on the
7544 			// context object and let extra br be the result, then call
7545 			// insertNode(extra br) on the active range."
7546 			if (isCollapsedLineBreak(br)) {
7547 				// TODO
7548 				range.insertNode(createEndBreak());
7549 
7550 				// Compensate for nonstandard implementations of insertNode
7551 				Aloha.getSelection().collapse(br.parentNode, 1 + getNodeIndex(br));
7552 				range.setStart(br.parentNode, 1 + getNodeIndex(br));
7553 				range.setEnd(br.parentNode, 1 + getNodeIndex(br));
7554 			}
7555 
7556 			// IE7 is adding this styles: height: auto; min-height: 0px; max-height: none;
7557 			// with that there is the ugly "IE-editable-outline"
7558 			if (jQuery.browser.msie && jQuery.browser.version < 8) {
7559 				br.parentNode.removeAttribute("style");
7560 			}
7561 		}
7562 	};
7563 
7564 	//@}
7565 	///// The insertOrderedList command /////
7566 	//@{
7567 	commands.insertorderedlist = {
7568 		// "Toggle lists with tag name "ol"."
7569 		action: function (value, range) {
7570 			toggleLists("ol", range);
7571 		},
7572 		// "True if the selection's list state is "mixed" or "mixed ol", false
7573 		// otherwise."
7574 		indeterm: function () {
7575 			return (/^mixed( ol)?$/).test(getSelectionListState());
7576 		},
7577 		// "True if the selection's list state is "ol", false otherwise."
7578 		state: function () {
7579 			return getSelectionListState() == "ol";
7580 		}
7581 	};
7582 
7583 	var listRelatedElements = {
7584 		"LI": true,
7585 		"DT": true,
7586 		"DD": true
7587 	};
7588 
7589 	//@}
7590 	///// The insertParagraph command /////
7591 	//@{
7592 	commands.insertparagraph = {
7593 		action: function (value, range) {
7594 			var i;
7595 
7596 			// "Delete the contents of the active range."
7597 			deleteContents(range);
7598 
7599 			// clean lists in the editing host, this will remove any whitespace nodes around lists
7600 			// because the following algorithm is not prepared to deal with them
7601 			cleanLists(getEditingHostOf(range.startContainer), range);
7602 
7603 			// "If the active range's start node is neither editable nor an editing
7604 			// host, abort these steps."
7605 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
7606 				return;
7607 			}
7608 
7609 			// "Let node and offset be the active range's start node and offset."
7610 			var node = range.startContainer;
7611 			var offset = range.startOffset;
7612 
7613 			// "If node is a Text node, and offset is neither 0 nor the length of
7614 			// node, call splitText(offset) on node."
7615 			if (node.nodeType == $_.Node.TEXT_NODE && offset != 0 && offset != getNodeLength(node)) {
7616 				node.splitText(offset);
7617 			}
7618 
7619 			// "If node is a Text node and offset is its length, set offset to one
7620 			// plus the index of node, then set node to its parent."
7621 			if (node.nodeType == $_.Node.TEXT_NODE && offset == getNodeLength(node)) {
7622 				offset = 1 + getNodeIndex(node);
7623 				node = node.parentNode;
7624 			}
7625 
7626 			// "If node is a Text or Comment node, set offset to the index of node,
7627 			// then set node to its parent."
7628 			if (node.nodeType == $_.Node.TEXT_NODE || node.nodeType == $_.Node.COMMENT_NODE) {
7629 				offset = getNodeIndex(node);
7630 				node = node.parentNode;
7631 			}
7632 
7633 			// "Call collapse(node, offset) on the context object's Selection."
7634 			Aloha.getSelection().collapse(node, offset);
7635 			range.setStart(node, offset);
7636 			range.setEnd(node, offset);
7637 
7638 			// "Let container equal node."
7639 			var container = node;
7640 
7641 			// "While container is not a single-line container, and container's
7642 			// parent is editable and in the same editing host as node, set
7643 			// container to its parent."
7644 			while (!isSingleLineContainer(container) && isEditable(container.parentNode) && inSameEditingHost(node, container.parentNode)) {
7645 				container = container.parentNode;
7646 			}
7647 
7648 			// "If container is not editable or not in the same editing host as
7649 			// node or is not a single-line container:"
7650 			if (!isEditable(container) || !inSameEditingHost(container, node) || !isSingleLineContainer(container)) {
7651 				// "Let tag be the default single-line container name."
7652 				var tag = defaultSingleLineContainerName;
7653 
7654 				// "Block-extend the active range, and let new range be the
7655 				// result."
7656 				var newRange = blockExtend(range);
7657 
7658 				// "Let node list be a list of nodes, initially empty."
7659 				//
7660 				// "Append to node list the first node in tree order that is
7661 				// contained in new range and is an allowed child of "p", if any."
7662 				var nodeList = getContainedNodes(newRange, function (node) {
7663 					return isAllowedChild(node, "p");
7664 				}).slice(0, 1);
7665 
7666 				// "If node list is empty:"
7667 				if (!nodeList.length) {
7668 					// "If tag is not an allowed child of the active range's start
7669 					// node, abort these steps."
7670 					if (!isAllowedChild(tag, range.startContainer)) {
7671 						return;
7672 					}
7673 
7674 					// "Set container to the result of calling createElement(tag)
7675 					// on the context object."
7676 					container = document.createElement(tag);
7677 
7678 					// "Call insertNode(container) on the active range."
7679 					range.insertNode(container);
7680 
7681 					// "Call createElement("br") on the context object, and append
7682 					// the result as the last child of container."
7683 					// TODO not always
7684 					container.appendChild(createEndBreak());
7685 
7686 					// "Call collapse(container, 0) on the context object's
7687 					// Selection."
7688 					// TODO: remove selection from command
7689 					Aloha.getSelection().collapse(container, 0);
7690 					range.setStart(container, 0);
7691 					range.setEnd(container, 0);
7692 
7693 					// "Abort these steps."
7694 					return;
7695 				}
7696 
7697 				// "While the nextSibling of the last member of node list is not
7698 				// null and is an allowed child of "p", append it to node list."
7699 				while (nodeList[nodeList.length - 1].nextSibling && isAllowedChild(nodeList[nodeList.length - 1].nextSibling, "p")) {
7700 					nodeList.push(nodeList[nodeList.length - 1].nextSibling);
7701 				}
7702 
7703 				// "Wrap node list, with sibling criteria returning false and new
7704 				// parent instructions returning the result of calling
7705 				// createElement(tag) on the context object. Set container to the
7706 				// result."
7707 				container = wrap(
7708 					nodeList,
7709 					function () {
7710 						return false;
7711 					},
7712 					function () {
7713 						return document.createElement(tag);
7714 					},
7715 					range
7716 				);
7717 			}
7718 
7719 			// "If container's local name is "address", "listing", or "pre":"
7720 			var oldHeight, newHeight;
7721 			if (container.tagName == "ADDRESS" || container.tagName == "LISTING" || container.tagName == "PRE") {
7722 				// "Let br be the result of calling createElement("br") on the
7723 				// context object."
7724 				var br = document.createElement("br");
7725 
7726 				// remember the old height
7727 				oldHeight = container.offsetHeight;
7728 
7729 				// "Call insertNode(br) on the active range."
7730 				range.insertNode(br);
7731 
7732 				// determine the new height
7733 				newHeight = container.offsetHeight;
7734 
7735 				// "Call collapse(node, offset + 1) on the context object's
7736 				// Selection."
7737 				Aloha.getSelection().collapse(node, offset + 1);
7738 				range.setStart(node, offset + 1);
7739 				range.setEnd(node, offset + 1);
7740 
7741 				// "If br is the last descendant of container, let br be the result
7742 				// of calling createElement("br") on the context object, then call
7743 				// insertNode(br) on the active range." (Fix: only do this, if the container height did not change by inserting a single <br/>)
7744 				//
7745 				// Work around browser bugs: some browsers select the
7746 				// newly-inserted node, not per spec.
7747 				if (oldHeight == newHeight && !isDescendant(nextNode(br), container)) {
7748 					// TODO check
7749 					range.insertNode(createEndBreak());
7750 					Aloha.getSelection().collapse(node, offset + 1);
7751 					range.setEnd(node, offset + 1);
7752 				}
7753 
7754 				// "Abort these steps."
7755 				return;
7756 			}
7757 
7758 			// "If container's local name is "li", "dt", or "dd"; and either it has
7759 			// no children or it has a single child and that child is a br:"
7760 			if (listRelatedElements[container.tagName] && (!container.hasChildNodes() || (container.childNodes.length == 1 && isNamedHtmlElement(container.firstChild, "br")))) {
7761 				// "Split the parent of the one-node list consisting of container."
7762 				splitParent([container], range);
7763 
7764 				// "If container has no children, call createElement("br") on the
7765 				// context object and append the result as the last child of
7766 				// container."
7767 				// only do this, if inserting the br does NOT modify the offset height of the container
7768 				//			if (!container.hasChildNodes()) {
7769 				//				var oldHeight = container.offsetHeight, endBr = createEndBreak();
7770 				//				container.appendChild(endBr);
7771 				//				if (container.offsetHeight !== oldHeight) {
7772 				//					container.removeChild(endBr);
7773 				//				}
7774 				//			}
7775 
7776 				// "If container is a dd or dt, and it is not an allowed child of
7777 				// any of its ancestors in the same editing host, set the tag name
7778 				// of container to the default single-line container name and let
7779 				// container be the result."
7780 				if (isHtmlElementInArray(container, ["dd", "dt"]) && $_(getAncestors(container)).every(function (ancestor) { return !inSameEditingHost(container, ancestor) || !isAllowedChild(container, ancestor); })) {
7781 					container = setTagName(container, defaultSingleLineContainerName, range);
7782 				}
7783 
7784 				// "Fix disallowed ancestors of container."
7785 				fixDisallowedAncestors(container, range);
7786 
7787 				// fix invalid nested lists
7788 				if (isNamedHtmlElement(container, 'li') && isNamedHtmlElement(container.nextSibling, "li") && isHtmlElementInArray(container.nextSibling.firstChild, ["ol", "ul"])) {
7789 					// we found a li containing only a br followed by a li containing a list as first element: merge the two li's
7790 					var listParent = container.nextSibling,
7791 						length = container.nextSibling.childNodes.length;
7792 					for (i = 0; i < length; i++) {
7793 						// we always move the first child into the container
7794 						container.appendChild(listParent.childNodes[0]);
7795 					}
7796 					listParent.parentNode.removeChild(listParent);
7797 				}
7798 
7799 				// "Abort these steps."
7800 				return;
7801 			}
7802 
7803 			// "Let new line range be a new range whose start is the same as
7804 			// the active range's, and whose end is (container, length of
7805 			// container)."
7806 			var newLineRange = Aloha.createRange();
7807 			newLineRange.setStart(range.startContainer, range.startOffset);
7808 			newLineRange.setEnd(container, getNodeLength(container));
7809 
7810 			// "While new line range's start offset is zero and its start node is
7811 			// not container, set its start to (parent of start node, index of
7812 			// start node)."
7813 			while (newLineRange.startOffset == 0 && newLineRange.startContainer != container) {
7814 				newLineRange.setStart(newLineRange.startContainer.parentNode, getNodeIndex(newLineRange.startContainer));
7815 			}
7816 
7817 			// "While new line range's start offset is the length of its start node
7818 			// and its start node is not container, set its start to (parent of
7819 			// start node, 1 + index of start node)."
7820 			while (newLineRange.startOffset == getNodeLength(newLineRange.startContainer) && newLineRange.startContainer != container) {
7821 				newLineRange.setStart(newLineRange.startContainer.parentNode, 1 + getNodeIndex(newLineRange.startContainer));
7822 			}
7823 
7824 			// "Let end of line be true if new line range contains either nothing
7825 			// or a single br, and false otherwise."
7826 			var containedInNewLineRange = getContainedNodes(newLineRange);
7827 			var endOfLine = !containedInNewLineRange.length || (containedInNewLineRange.length == 1 && isNamedHtmlElement(containedInNewLineRange[0], "br"));
7828 
7829 			// "If the local name of container is "h1", "h2", "h3", "h4", "h5", or
7830 			// "h6", and end of line is true, let new container name be the default
7831 			// single-line container name."
7832 			var newContainerName;
7833 			if (/^H[1-6]$/.test(container.tagName) && endOfLine) {
7834 				newContainerName = defaultSingleLineContainerName;
7835 
7836 				// "Otherwise, if the local name of container is "dt" and end of line
7837 				// is true, let new container name be "dd"."
7838 			} else if (container.tagName == "DT" && endOfLine) {
7839 				newContainerName = "dd";
7840 
7841 				// "Otherwise, if the local name of container is "dd" and end of line
7842 				// is true, let new container name be "dt"."
7843 			} else if (container.tagName == "DD" && endOfLine) {
7844 				newContainerName = "dt";
7845 
7846 				// "Otherwise, let new container name be the local name of container."
7847 			} else {
7848 				newContainerName = container.tagName.toLowerCase();
7849 			}
7850 
7851 			// "Let new container be the result of calling createElement(new
7852 			// container name) on the context object."
7853 			var newContainer = document.createElement(newContainerName);
7854 
7855 			// "Copy all non empty attributes of the container to new container."
7856 			copyAttributes(container, newContainer);
7857 
7858 			// "If new container has an id attribute, unset it."
7859 			newContainer.removeAttribute("id");
7860 
7861 			// "Insert new container into the parent of container immediately after
7862 			// container."
7863 			container.parentNode.insertBefore(newContainer, container.nextSibling);
7864 
7865 			// "Let contained nodes be all nodes contained in new line range."
7866 			var containedNodes = getAllContainedNodes(newLineRange);
7867 
7868 			// "Let frag be the result of calling extractContents() on new line
7869 			// range."
7870 			var frag = newLineRange.extractContents();
7871 
7872 			// "Unset the id attribute (if any) of each Element descendant of frag
7873 			// that is not in contained nodes."
7874 			var descendants = getDescendants(frag);
7875 			for (i = 0; i < descendants.length; i++) {
7876 				if (descendants[i].nodeType == $_.Node.ELEMENT_NODE && $_(containedNodes).indexOf(descendants[i]) == -1) {
7877 					descendants[i].removeAttribute("id");
7878 				}
7879 			}
7880 
7881 			var fragChildren = [],
7882 				fragChild = frag.firstChild;
7883 			if (fragChild) {
7884 				do {
7885 					if (!isWhitespaceNode(fragChild)) {
7886 						fragChildren.push(fragChild);
7887 					}
7888 				} while (null != (fragChild = fragChild.nextSibling));
7889 			}
7890 
7891 			// if newContainer is a li and frag contains only a list, we add a br in the li (but only if the height would not change)
7892 			if (isNamedHtmlElement(newContainer, 'li') && fragChildren.length && isHtmlElementInArray(fragChildren[0], ["ul", "ol"])) {
7893 				oldHeight = newContainer.offsetHeight;
7894 				var endBr = createEndBreak();
7895 				newContainer.appendChild(endBr);
7896 				newHeight = newContainer.offsetHeight;
7897 				if (oldHeight !== newHeight) {
7898 					newContainer.removeChild(endBr);
7899 				}
7900 			}
7901 
7902 			// "Call appendChild(frag) on new container."
7903 			newContainer.appendChild(frag);
7904 
7905 			// "If container has no visible children, call createElement("br") on
7906 			// the context object, and append the result as the last child of
7907 			// container."
7908 			ensureContainerEditable(container);
7909 
7910 			// "If new container has no visible children, call createElement("br")
7911 			// on the context object, and append the result as the last child of
7912 			// new container."
7913 			ensureContainerEditable(newContainer);
7914 
7915 			// "Call collapse(new container, 0) on the context object's Selection."
7916 			Aloha.getSelection().collapse(newContainer, 0);
7917 			range.setStart(newContainer, 0);
7918 			range.setEnd(newContainer, 0);
7919 		}
7920 	};
7921 7922 
	//@}
7923 	///// The insertText command /////
7924 	//@{
7925 	commands.inserttext = {
7926 		action: function (value, range) {
7927 			var i;
7928 
7929 			// "Delete the contents of the active range, with strip wrappers
7930 			// false."
7931 			deleteContents(range, {
7932 				stripWrappers: false
7933 			});
7934 
7935 			// "If the active range's start node is neither editable nor an editing
7936 			// host, abort these steps."
7937 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
7938 				return;
7939 			}
7940 
7941 			// "If value's length is greater than one:"
7942 			if (value.length > 1) {
7943 				// "For each element el in value, take the action for the
7944 				// insertText command, with value equal to el."
7945 				for (i = 0; i < value.length; i++) {
7946 					commands.inserttext.action(value[i], range);
7947 				}
7948 
7949 				// "Abort these steps."
7950 				return;
7951 			}
7952 
7953 			// "If value is the empty string, abort these steps."
7954 			if (value == "") {
7955 				return;
7956 			}
7957 
7958 			// "If value is a newline (U+00A0), take the action for the
7959 			// insertParagraph command and abort these steps."
7960 			if (value == "\n") {
7961 				commands.insertparagraph.action('', range);
7962 				return;
7963 			}
7964 
7965 			// "Let node and offset be the active range's start node and offset."
7966 			var node = range.startContainer;
7967 			var offset = range.startOffset;
7968 
7969 			// "If node has a child whose index is offset − 1, and that child is a
7970 			// Text node, set node to that child, then set offset to node's
7971 			// length."
7972 			if (0 <= offset - 1 && offset - 1 < node.childNodes.length && node.childNodes[offset - 1].nodeType == $_.Node.TEXT_NODE) {
7973 				node = node.childNodes[offset - 1];
7974 				offset = getNodeLength(node);
7975 			}
7976 
7977 			// "If node has a child whose index is offset, and that child is a Text
7978 			// node, set node to that child, then set offset to zero."
7979 			if (0 <= offset && offset < node.childNodes.length && node.childNodes[offset].nodeType == $_.Node.TEXT_NODE) {
7980 				node = node.childNodes[offset];
7981 				offset = 0;
7982 			}
7983 
7984 			// "If value is a space (U+0020), and either node is an Element whose
7985 			// resolved value for "white-space" is neither "pre" nor "pre-wrap" or
7986 			// node is not an Element but its parent is an Element whose resolved
7987 			// value for "white-space" is neither "pre" nor "pre-wrap", set value
7988 			// to a non-breaking space (U+00A0)."
7989 			var refElement = node.nodeType == $_.Node.ELEMENT_NODE ? node : node.parentNode;
7990 			if (value == " " && refElement.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(refElement).whiteSpace, ["pre", "pre-wrap"]) == -1) {
7991 				value = "\xa0";
7992 			}
7993 
7994 			// "Record current overrides, and let overrides be the result."
7995 			var overrides = recordCurrentOverrides(range);
7996 
7997 			// "If node is a Text node:"
7998 			if (node.nodeType == $_.Node.TEXT_NODE) {
7999 				// "Call insertData(offset, value) on node."
8000 				node.insertData(offset, value);
8001 
8002 				// "Call collapse(node, offset) on the context object's Selection."
8003 				Aloha.getSelection().collapse(node, offset);
8004 				range.setStart(node, offset);
8005 
8006 				// "Call extend(node, offset + 1) on the context object's
8007 				// Selection."
8008 				Aloha.getSelection().extend(node, offset + 1);
8009 				range.setEnd(node, offset + 1);
8010 
8011 				// "Otherwise:"
8012 			} else {
8013 				// "If node has only one child, which is a collapsed line break,
8014 				// remove its child from it."
8015 				//
8016 				// FIXME: IE incorrectly returns false here instead of true
8017 				// sometimes?
8018 				if (node.childNodes.length == 1 && isCollapsedLineBreak(node.firstChild)) {
8019 					node.removeChild(node.firstChild);
8020 				}
8021 
8022 				// "Let text be the result of calling createTextNode(value) on the
8023 				// context object."
8024 				var text = document.createTextNode(value);
8025 
8026 				// "Call insertNode(text) on the active range."
8027 				range.insertNode(text);
8028 
8029 				// "Call collapse(text, 0) on the context object's Selection."
8030 				Aloha.getSelection().collapse(text, 0);
8031 				range.setStart(text, 0);
8032 
8033 				// "Call extend(text, 1) on the context object's Selection."
8034 				Aloha.getSelection().extend(text, 1);
8035 				range.setEnd(text, 1);
8036 			}
8037 
8038 			// "Restore states and values from overrides."
8039 			restoreStatesAndValues(overrides);
8040 
8041 			// "Canonicalize whitespace at the active range's start."
8042 			canonicalizeWhitespace(range.startContainer, range.startOffset);
8043 
8044 			// "Canonicalize whitespace at the active range's end."
8045 			canonicalizeWhitespace(range.endContainer, range.endOffset);
8046 
8047 			// "Call collapseToEnd() on the context object's Selection."
8048 			Aloha.getSelection().collapseToEnd();
8049 			range.collapse(false);
8050 		}
8051 	};
8052 
8053 	//@}
8054 	///// The insertUnorderedList command /////
8055 	//@{
8056 	commands.insertunorderedlist = {
8057 		// "Toggle lists with tag name "ul"."
8058 		action: function (value, range) {
8059 			toggleLists("ul", range);
8060 		},
8061 		// "True if the selection's list state is "mixed" or "mixed ul", false
8062 		// otherwise."
8063 		indeterm: function () {
8064 			return (/^mixed( ul)?$/).test(getSelectionListState());
8065 		},
8066 		// "True if the selection's list state is "ul", false otherwise."
8067 		state: function () {
8068 			return getSelectionListState() == "ul";
8069 		}
8070 	};
8071 
8072 	//@}
8073 	///// The justifyCenter command /////
8074 	//@{
8075 	commands.justifycenter = {
8076 		// "Justify the selection with alignment "center"."
8077 		action: function (value, range) {
8078 			justifySelection("center", range);
8079 		},
8080 		indeterm: function () {
8081 			// "Block-extend the active range. Return true if among visible
8082 			// editable nodes that are contained in the result and have no
8083 			// children, at least one has alignment value "center" and at least one
8084 			// does not. Otherwise return false."
8085 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8086 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8087 			});
8088 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "center"; })
8089 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "center"; });
8090 		},
8091 		state: function () {
8092 			// "Block-extend the active range. Return true if there is at least one
8093 			// visible editable node that is contained in the result and has no
8094 			// children, and all such nodes have alignment value "center".
8095 			// Otherwise return false."
8096 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8097 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8098 			});
8099 			return nodes.length && $_(nodes).every(function (node) {
8100 				return getAlignmentValue(node) == "center";
8101 			});
8102 		},
8103 		value: function () {
8104 			// "Block-extend the active range, and return the alignment value of
8105 			// the first visible editable node that is contained in the result and
8106 			// has no children. If there is no such node, return "left"."
8107 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8108 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8109 			});
8110 			if (nodes.length) {
8111 				return getAlignmentValue(nodes[0]);
8112 			}
8113 			return "left";
8114 		}
8115 	};
8116 
8117 	//@}
8118 	///// The justifyFull command /////
8119 	//@{
8120 	commands.justifyfull = {
8121 		// "Justify the selection with alignment "justify"."
8122 		action: function (value, range) {
8123 			justifySelection("justify", range);
8124 		},
8125 		indeterm: function () {
8126 			// "Block-extend the active range. Return true if among visible
8127 			// editable nodes that are contained in the result and have no
8128 			// children, at least one has alignment value "justify" and at least
8129 			// one does not. Otherwise return false."
8130 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8131 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8132 			});
8133 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "justify"; })
8134 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "justify"; });
8135 		},
8136 		state: function () {
8137 			// "Block-extend the active range. Return true if there is at least one
8138 			// visible editable node that is contained in the result and has no
8139 			// children, and all such nodes have alignment value "justify".
8140 			// Otherwise return false."
8141 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8142 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8143 			});
8144 			return nodes.length && $_(nodes).every(function (node) {
8145 				return getAlignmentValue(node) == "justify";
8146 			});
8147 		},
8148 		value: function () {
8149 			// "Block-extend the active range, and return the alignment value of
8150 			// the first visible editable node that is contained in the result and
8151 			// has no children. If there is no such node, return "left"."
8152 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8153 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8154 			});
8155 			if (nodes.length) {
8156 				return getAlignmentValue(nodes[0]);
8157 			}
8158 			return "left";
8159 		}
8160 	};
8161 
8162 	//@}
8163 	///// The justifyLeft command /////
8164 	//@{
8165 	commands.justifyleft = {
8166 		// "Justify the selection with alignment "left"."
8167 		action: function (value, range) {
8168 			justifySelection("left", range);
8169 		},
8170 		indeterm: function () {
8171 			// "Block-extend the active range. Return true if among visible
8172 			// editable nodes that are contained in the result and have no
8173 			// children, at least one has alignment value "left" and at least one
8174 			// does not. Otherwise return false."
8175 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8176 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8177 			});
8178 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "left"; })
8179 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "left"; });
8180 		},
8181 		state: function () {
8182 			// "Block-extend the active range. Return true if there is at least one
8183 			// visible editable node that is contained in the result and has no
8184 			// children, and all such nodes have alignment value "left".  Otherwise
8185 			// return false."
8186 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8187 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8188 			});
8189 			return nodes.length && $_(nodes).every(function (node) {
8190 				return getAlignmentValue(node) == "left";
8191 			});
8192 		},
8193 		value: function () {
8194 			// "Block-extend the active range, and return the alignment value of
8195 			// the first visible editable node that is contained in the result and
8196 			// has no children. If there is no such node, return "left"."
8197 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8198 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8199 			});
8200 			if (nodes.length) {
8201 				return getAlignmentValue(nodes[0]);
8202 			}
8203 			return "left";
8204 		}
8205 	};
8206 
8207 	//@}
8208 	///// The justifyRight command /////
8209 	//@{
8210 	commands.justifyright = {
8211 		// "Justify the selection with alignment "right"."
8212 		action: function (value, range) {
8213 			justifySelection("right", range);
8214 		},
8215 		indeterm: function () {
8216 			// "Block-extend the active range. Return true if among visible
8217 			// editable nodes that are contained in the result and have no
8218 			// children, at least one has alignment value "right" and at least one
8219 			// does not. Otherwise return false."
8220 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8221 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8222 			});
8223 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "right"; })
8224 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "right"; });
8225 		},
8226 		state: function () {
8227 			// "Block-extend the active range. Return true if there is at least one
8228 			// visible editable node that is contained in the result and has no
8229 			// children, and all such nodes have alignment value "right".
8230 			// Otherwise return false."
8231 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8232 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8233 			});
8234 			return nodes.length && $_(nodes).every(function (node) {
8235 				return getAlignmentValue(node) == "right";
8236 			});
8237 		},
8238 		value: function () {
8239 			// "Block-extend the active range, and return the alignment value of
8240 			// the first visible editable node that is contained in the result and
8241 			// has no children. If there is no such node, return "left"."
8242 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8243 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8244 			});
8245 			if (nodes.length) {
8246 				return getAlignmentValue(nodes[0]);
8247 			}
8248 			return "left";
8249 		}
8250 	};
8251 
8252 	//@}
8253 	///// The outdent command /////
8254 	//@{
8255 	commands.outdent = {
8256 		action: function () {
8257 			// "Let items be a list of all lis that are ancestor containers of the
8258 			// range's start and/or end node."
8259 			//
8260 			// It's annoying to get this in tree order using functional stuff
8261 			// without doing getDescendants(document), which is slow, so I do it
8262 			// imperatively.
8263 			var items = [];
8264 			(function () {
8265 				var ancestorContainer;
8266 				for (ancestorContainer = getActiveRange().endContainer;
8267 					     ancestorContainer != getActiveRange().commonAncestorContainer;
8268 					     ancestorContainer = ancestorContainer.parentNode) {
8269 					if (isNamedHtmlElement(ancestorContainer, "li")) {
8270 						items.unshift(ancestorContainer);
8271 					}
8272 				}
8273 				for (ancestorContainer = getActiveRange().startContainer;
8274 					     ancestorContainer;
8275 					     ancestorContainer = ancestorContainer.parentNode) {
8276 					if (isNamedHtmlElement(ancestorContainer, "li")) {
8277 						items.unshift(ancestorContainer);
8278 					}
8279 				}
8280 			}());
8281 
8282 			// "For each item in items, normalize sublists of item."
8283 			$_(items).forEach(function (thisArg) {
8284 				normalizeSublists(thisArg, getActiveRange());
8285 			});
8286 
8287 			// "Block-extend the active range, and let new range be the result."
8288 			var newRange = blockExtend(getActiveRange());
8289 
8290 			// "Let node list be a list of nodes, initially empty."
8291 			//
8292 			// "For each node node contained in new range, append node to node list
8293 			// if the last member of node list (if any) is not an ancestor of node;
8294 			// node is editable; and either node has no editable descendants, or is
8295 			// an ol or ul, or is an li whose parent is an ol or ul."
8296 			var nodeList = getContainedNodes(newRange, function (node) {
8297 				return isEditable(node) && (!$_(getDescendants(node)).some(isEditable) || isHtmlElementInArray(node, ["ol", "ul"]) || (isNamedHtmlElement(node, 'li') && isHtmlElementInArray(node.parentNode, ["ol", "ul"])));
8298 			});
8299 
8300 			// "While node list is not empty:"
8301 			while (nodeList.length) {
8302 				// "While the first member of node list is an ol or ul or is not
8303 				// the child of an ol or ul, outdent it and remove it from node
8304 				// list."
8305 				while (nodeList.length && (isHtmlElementInArray(nodeList[0], ["OL", "UL"]) || !isHtmlElementInArray(nodeList[0].parentNode, ["OL", "UL"]))) {
8306 					outdentNode(nodeList.shift(), newRange);
8307 				}
8308 
8309 				// "If node list is empty, break from these substeps."
8310 				if (!nodeList.length) {
8311 					break;
8312 				}
8313 
8314 				// "Let sublist be a list of nodes, initially empty."
8315 				var sublist = [];
8316 
8317 				// "Remove the first member of node list and append it to sublist."
8318 				sublist.push(nodeList.shift());
8319 
8320 				// "While the first member of node list is the nextSibling of the
8321 				// last member of sublist, and the first member of node list is not
8322 				// an ol or ul, remove the first member of node list and append it
8323 				// to sublist."
8324 				while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isHtmlElementInArray(nodeList[0], ["OL", "UL"])) {
8325 					sublist.push(nodeList.shift());
8326 				}
8327 
8328 				// "Record the values of sublist, and let values be the result."
8329 				var values = recordValues(sublist);
8330 
8331 				// "Split the parent of sublist, with new parent null."
8332 				splitParent(sublist, newRange);
8333 
8334 				// "Fix disallowed ancestors of each member of sublist."
8335 				$_(sublist).forEach(fixDisallowedAncestors);
8336 
8337 				// "Restore the values from values."
8338 				restoreValues(values, newRange);
8339 			}
8340 		}
8341 	};
8342 
8343 	//@}
8344 
8345 	//////////////////////////////////
8346 	///// Miscellaneous commands /////
8347 	//////////////////////////////////
8348 
8349 	///// The selectAll command /////
8350 	//@{
8351 	commands.selectall = {
8352 		// Note, this ignores the whole globalRange/getActiveRange() thing and
8353 		// works with actual selections.  Not suitable for autoimplementation.html.
8354 		action: function () {
8355 			// "Let target be the body element of the context object."
8356 			var target = document.body;
8357 
8358 			// "If target is null, let target be the context object's
8359 			// documentElement."
8360 			if (!target) {
8361 				target = document.documentElement;
8362 			}
8363 
8364 			// "If target is null, call getSelection() on the context object, and
8365 			// call removeAllRanges() on the result."
8366 			if (!target) {
8367 				Aloha.getSelection().removeAllRanges();
8368 
8369 				// "Otherwise, call getSelection() on the context object, and call
8370 				// selectAllChildren(target) on the result."
8371 			} else {
8372 				Aloha.getSelection().selectAllChildren(target);
8373 			}
8374 		}
8375 	};
8376 
8377 	//@}
8378 	///// The styleWithCSS command /////
8379 	//@{
8380 	commands.stylewithcss = {
8381 		action: function (value) {
8382 			// "If value is an ASCII case-insensitive match for the string
8383 			// "false", set the CSS styling flag to false. Otherwise, set the
8384 			// CSS styling flag to true."
8385 			cssStylingFlag = String(value).toLowerCase() != "false";
8386 		},
8387 		state: function () {
8388 			return cssStylingFlag;
8389 		}
8390 	};
8391 
8392 	//@}
8393 	///// The useCSS command /////
8394 	//@{
8395 	commands.usecss = {
8396 		action: function (value) {
8397 			// "If value is an ASCII case-insensitive match for the string "false",
8398 			// set the CSS styling flag to true. Otherwise, set the CSS styling
8399 			// flag to false."
8400 			cssStylingFlag = String(value).toLowerCase() == "false";
8401 		}
8402 	};
8403 	//@}
8404 
8405 	// Some final setup
8406 	//@{
8407 	(function () {
8408 		// Opera 11.50 doesn't implement Object.keys, so I have to make an explicit
8409 		// temporary, which means I need an extra closure to not leak the temporaries
8410 		// into the global namespace.  >:(
8411 8412 		var commandNames = [];
8413 		var command;
8414 		for (command in commands) {
8415 			if (commands.hasOwnProperty(command)) {
8416 				commandNames.push(command);
8417 			}
8418 		}
8419 		$_(commandNames).forEach(function (command) {
8420 8421 			// "If a command does not have a relevant CSS property specified, it
8422 			// defaults to null."
8423 			if (null == commands[command].relevantCssProperty) {
8424 				commands[command].relevantCssProperty = null;
8425 			}
8426 
8427 			// "If a command has inline command activated values defined but
8428 			// nothing else defines when it is indeterminate, it is indeterminate
8429 			// if among editable Text nodes effectively contained in the active
8430 			// range, there is at least one whose effective command value is one of
8431 			// the given values and at least one whose effective command value is
8432 			// not one of the given values."
8433 			if (null != commands[command].inlineCommandActivatedValues && null == commands[command].indeterm) {
8434 				commands[command].indeterm = function (range) {
8435 					var values = $_(getAllEffectivelyContainedNodes(range, function (node) { return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; }))
8436 						.map(function (node) { return getEffectiveCommandValue(node, command); });
8437 
8438 					var matchingValues = $_(values).filter(function (value) {
8439 						return $_(commands[command].inlineCommandActivatedValues).indexOf(value) != -1;
8440 					});
8441 
8442 					return matchingValues.length >= 1 && values.length - matchingValues.length >= 1;
8443 				};
8444 			}
8445 
8446 			// "If a command has inline command activated values defined, its state
8447 			// is true if either no editable Text node is effectively contained in
8448 			// the active range, and the active range's start node's effective
8449 			// command value is one of the given values; or if there is at least
8450 			// one editable Text node effectively contained in the active range,
8451 			// and all of them have an effective command value equal to one of the
8452 			// given values."
8453 			if (null != commands[command].inlineCommandActivatedValues) {
8454 				commands[command].state = function (range) {
8455 					var nodes = getAllEffectivelyContainedNodes(range, function (node) {
8456 						return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
8457 					});
8458 
8459 					if (nodes.length == 0) {
8460 						return $_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(range.startContainer, command)) != -1;
8461 					}
8462 					return $_(nodes).every(function (node) {
8463 						return $_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(node, command)) != -1;
8464 					});
8465 				};
8466 			}
8467 
8468 			// "If a command is a standard inline value command, it is
8469 			// indeterminate if among editable Text nodes that are effectively
8470 			// contained in the active range, there are two that have distinct
8471 			// effective command values. Its value is the effective command value
8472 			// of the first editable Text node that is effectively contained in the
8473 			// active range, or if there is no such node, the effective command
8474 			// value of the active range's start node."
8475 			if (null != commands[command].standardInlineValueCommand) {
8476 				commands[command].indeterm = function () {
8477 					var values = $_(getAllEffectivelyContainedNodes(getActiveRange())).filter(function (node) { return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; }, true)
8478 						.map(function (node) { return getEffectiveCommandValue(node, command); });
8479 					var i;
8480 					for (i = 1; i < values.length; i++) {
8481 						if (values[i] != values[i - 1]) {
8482 							return true;
8483 						}
8484 					}
8485 					return false;
8486 				};
8487 
8488 				commands[command].value = function (range) {
8489 					var refNode = getAllEffectivelyContainedNodes(range, function (node) {
8490 						return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
8491 					})[0];
8492 
8493 					if (typeof refNode == "undefined") {
8494 						refNode = range.startContainer;
8495 					}
8496 
8497 8498 					return getEffectiveCommandValue(refNode, command);
8499 				};
8500 			}
8501 		});
8502 	}());
8503 	//@}
8504 	return {
8505 		commands: commands,
8506 		execCommand: myExecCommand,
8507 		queryCommandIndeterm: myQueryCommandIndeterm,
8508 		queryCommandState: myQueryCommandState,
8509 		queryCommandValue: myQueryCommandValue,
8510 		queryCommandEnabled: myQueryCommandEnabled,
8511 		queryCommandSupported: myQueryCommandSupported,
8512 		copyAttributes: copyAttributes,
8513 		createEndBreak: createEndBreak,
8514 		isEndBreak: isEndBreak,
8515 		ensureContainerEditable: ensureContainerEditable,
8516 		isEditingHost: isEditingHost,
8517 		isEditable: isEditable
8518 	};
8519 }); // end define
8520 // vim: foldmarker=@{,@} foldmethod=marker
8521