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