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