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