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 		if (!isAnyHtmlElement(node)) {
4336 			return false;
4337 		}
4338 
4339 		if (node.tagName == "BLOCKQUOTE") {
4340 			return true;
4341 		}
4342 
4343 		if (node.tagName != "DIV") {
4344 			return false;
4345 		}
4346 
4347 		if (typeof node.style.length !== 'undefined') {
4348 			var i;
4349 			for (i = 0; i < node.style.length; i++) {
4350 				// Approximate check
4351 				if (/^(-[a-z]+-)?margin/.test(node.style[i])) {
4352 					return true;
4353 				}
4354 			}
4355 		} else {
4356 			var s;
4357 			/*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid
4358 			for (s in node.style) {
4359 				if (/^(-[a-z]+-)?margin/.test(s) && node.style[s] && node.style[s] !== 0) {
4360 					return true;
4361 				}
4362 			}
4363 			/*jslint forin: false*/
4364 		}
4365 
4366 		return false;
4367 	}
4368 
4369 	// "A simple indentation element is an indentation element that has no
4370 	// attributes other than one or more of
4371 	//
4372 	//   * "a style attribute that sets no properties other than "margin", "border",
4373 	//     "padding", or subproperties of those;
4374 	//   * "a class attribute;
4375 	//   * "a dir attribute."
4376 	function isSimpleIndentationElement(node) {
4377 		if (!isIndentationElement(node)) {
4378 			return false;
4379 		}
4380 
4381 		if (node.tagName != "BLOCKQUOTE" && node.tagName != "DIV") {
4382 			return false;
4383 		}
4384 
4385 		var i;
4386 		for (i = 0; i < node.attributes.length; i++) {
4387 			if (!isHtmlNamespace(node.attributes[i].namespaceURI) || jQuery.inArray(node.attributes[i].name, ["style", "class", "dir"]) == -1) {
4388 				return false;
4389 			}
4390 		}
4391 
4392 		if (typeof node.style.length !== 'undefined') {
4393 			for (i = 0; i < node.style.length; i++) {
4394 				// This is approximate, but it works well enough for my purposes.
4395 				if (!/^(-[a-z]+-)?(margin|border|padding)/.test(node.style[i])) {
4396 					return false;
4397 				}
4398 			}
4399 		} else {
4400 			var s;
4401 			/*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid
4402 			for (s in node.style) {
4403 				// This is approximate, but it works well enough for my purposes.
4404 				if (!/^(-[a-z]+-)?(margin|border|padding)/.test(s) && node.style[s] && node.style[s] !== 0 && node.style[s] !== 'false') {
4405 					return false;
4406 				}
4407 			}
4408 			/*jslint forin: false*/
4409 		}
4410 
4411 		return true;
4412 	}
4413 
4414 	// "A non-list single-line container is an HTML element with local name
4415 	// "address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre",
4416 	// or "xmp"."
4417 	function isNonListSingleLineContainer(node) {
4418 		return isHtmlElementInArray(node, ["address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre", "xmp"]);
4419 	}
4420 
4421 	// "A single-line container is either a non-list single-line container, or an
4422 	// HTML element with local name "li", "dt", or "dd"."
4423 	function isSingleLineContainer(node) {
4424 		return isNonListSingleLineContainer(node) || isHtmlElementInArray(node, ["li", "dt", "dd"]);
4425 	}
4426 
4427 	// "The default single-line container name is "p"."
4428 	var defaultSingleLineContainerName = "p";
4429 
4430 	//@}
4431 	///// Check whether the given element is an end break /////
4432 	//@{
4433 	function isEndBreak(element) {
4434 		return (isNamedHtmlElement(element, 'br') && element.parentNode.lastChild === element);
4435 	}
4436 
4437 	//@}
4438 	///// Create an end break /////
4439 	//@{
4440 	function createEndBreak() {
4441 		return document.createElement("br");
4442 	}
4443 
4444 	/**
4445 	 * Ensure the container is editable
4446 	 * E.g. when called for an empty paragraph or header, and the browser is not IE,
4447 	 * we need to append a br (marked with class aloha-end-br)
4448 	 * For IE7, there is a special behaviour that will append zero-width whitespace
4449 	 * @param {DOMNode} container
4450 	 */
4451 	function ensureContainerEditable(container) {
4452 		if (!container) {
4453 			return;
4454 		}
4455 
4456 		// Because it is useful to be able to completely empty the contents of
4457 		// an editing host during editing.  So long as the container's
4458 		// contenteditable attribute is "true" (as is the case during editing),
4459 		// the element will be rendered visibly in all browsers.  This fact
4460 		// allows us to not have to prop up the container with a <br> in order
4461 		// to keep it accessible to the editor.
4462 		if (isEditingHost(container)) {
4463 			return;
4464 		}
4465 
4466 		if (isNamedHtmlElement(container.lastChild, "br")) {
4467 			return;
4468 		}
4469 
4470 		if ($_(container.childNodes).some(isVisible)) {
4471 			return;
4472 		}
4473 
4474 		if (!jQuery.browser.msie) {
4475 			// for normal browsers, the end-br will do
4476 			container.appendChild(createEndBreak());
4477 		} else if (jQuery.browser.msie && jQuery.browser.version <= 7 && isHtmlElementInArray(container, ["p", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "blockquote"])) {
4478 			// for IE7, we need to insert a text node containing a single zero-width whitespace character
4479 			if (!container.firstChild) {
4480 				container.appendChild(document.createTextNode('\u200b'));
4481 			}
4482 		}
4483 	}
4484 
4485 	//@}
4486 	///// Assorted block formatting command algorithms /////
4487 	//@{
4488 
4489 	function fixDisallowedAncestors(node, range) {
4490 		var i;
4491 
4492 		// "If node is not editable, abort these steps."
4493 		if (!isEditable(node)) {
4494 			return;
4495 		}
4496 
4497 		// "If node is not an allowed child of any of its ancestors in the same
4498 		// editing host, and is not an HTML element with local name equal to the
4499 		// default single-line container name:"
4500 		if ($_(getAncestors(node)).every(function (ancestor) { return !inSameEditingHost(node, ancestor) || !isAllowedChild(node, ancestor); })
4501 			    && !isHtmlElement_obsolete(node, defaultSingleLineContainerName)) {
4502 			// "If node is a dd or dt, wrap the one-node list consisting of node,
4503 			// with sibling criteria returning true for any dl with no attributes
4504 			// and false otherwise, and new parent instructions returning the
4505 			// result of calling createElement("dl") on the context object. Then
4506 			// abort these steps."
4507 			if (isHtmlElementInArray(node, ["dd", "dt"])) {
4508 				wrap(
4509 					[node],
4510 					function (sibling) {
4511 						return isNamedHtmlElement(sibling, 'dl') && !sibling.attributes.length;
4512 					},
4513 					function () {
4514 						return document.createElement("dl");
4515 					},
4516 					range
4517 				);
4518 				return;
4519 			}
4520 
4521 			// "If node is not a prohibited paragraph child, abort these steps."
4522 			if (!isProhibitedParagraphChild(node)) {
4523 				return;
4524 			}
4525 
4526 			// "Set the tag name of node to the default single-line container name,
4527 			// and let node be the result."
4528 			node = setTagName(node, defaultSingleLineContainerName, range);
4529 
4530 			ensureContainerEditable(node);
4531 
4532 			// "Fix disallowed ancestors of node."
4533 			fixDisallowedAncestors(node, range);
4534 
4535 			// "Let descendants be all descendants of node."
4536 			var descendants = getDescendants(node);
4537 
4538 			// "Fix disallowed ancestors of each member of descendants."
4539 			for (i = 0; i < descendants.length; i++) {
4540 				fixDisallowedAncestors(descendants[i], range);
4541 			}
4542 
4543 			// "Abort these steps."
4544 			return;
4545 		}
4546 
4547 		// "Record the values of the one-node list consisting of node, and let
4548 		// values be the result."
4549 		var values = recordValues([node]);
4550 		var newStartOffset, newEndOffset;
4551 
4552 		// "While node is not an allowed child of its parent, split the parent of
4553 		// the one-node list consisting of node."
4554 		while (!isAllowedChild(node, node.parentNode)) {
4555 			// If the parent contains only this node and possibly empty text nodes, we rather want to unwrap the node, instead of splitting.
4556 			// With splitting, we would get empty nodes, like:
4557 			// split: <p><p>foo</p></p> -> <p></p><p>foo</p> (bad)
4558 			// unwrap: <p><p>foo</p></p> -> <p>foo</p> (good)
4559 
4560 			// First remove empty text nodes that are children of the parent and correct the range if necessary
4561 			// we do this to have the node being the only child of its parent, so that we can replace the parent with the node
4562 			for (i = node.parentNode.childNodes.length - 1; i >= 0; --i) {
4563 				if (node.parentNode.childNodes[i].nodeType == 3 && node.parentNode.childNodes[i].data.length == 0) {
4564 					// we remove the empty text node
4565 					node.parentNode.removeChild(node.parentNode.childNodes[i]);
4566 
4567 					// if the range points to somewhere behind the removed text node, we reduce the offset
4568 					if (range.startContainer == node.parentNode && range.startOffset > i) {
4569 						range.startOffset--;
4570 					}
4571 					if (range.endContainer == node.parentNode && range.endOffset > i) {
4572 						range.endOffset--;
4573 					}
4574 				}
4575 			}
4576 4577 
			// now that the parent has only the node as child (because we
4578 			// removed any existing empty text nodes), we can safely unwrap the
4579 			// node's contents, and correct the range if necessary
4580 			if (node.parentNode.childNodes.length == 1) {
4581 				newStartOffset = range.startOffset;
4582 				newEndOffset = range.endOffset;
4583 
4584 				if (range.startContainer === node.parentNode && range.startOffset > getNodeIndex(node)) {
4585 					// the node (1 element) will be replaced by its contents (contents().length elements)
4586 					newStartOffset = range.startOffset + (jQuery(node).contents().length - 1);
4587 				}
4588 				if (range.endContainer === node.parentNode && range.endOffset > getNodeIndex(node)) {
4589 					// the node (1 element) will be replaced by its contents (contents().length elements)
4590 					newEndOffset = range.endOffset + (jQuery(node).contents().length - 1);
4591 				}
4592 				jQuery(node).contents().unwrap();
4593 				range.startOffset = newStartOffset;
4594 				range.endOffset = newEndOffset;
4595 				// after unwrapping, we are done
4596 				break;
4597 			} else {
4598 				// store the original parent
4599 				var originalParent = node.parentNode;
4600 				splitParent([node], range);
4601 				// check whether the parent did not change, so the split did not work, e.g.
4602 				// because we already reached the editing host itself.
4603 				// this situation can occur, e.g. when we insert a paragraph into an contenteditable span
4604 				// in such cases, we just unwrap the contents of the paragraph
4605 				if (originalParent === node.parentNode) {
4606 					// so we unwrap now
4607 					newStartOffset = range.startOffset;
4608 					newEndOffset = range.endOffset;
4609 
4610 					if (range.startContainer === node.parentNode && range.startOffset > getNodeIndex(node)) {
4611 						// the node (1 element) will be replaced by its contents (contents().length elements)
4612 						newStartOffset = range.startOffset + (jQuery(node).contents().length - 1);
4613 					}
4614 					if (range.endContainer === node.parentNode && range.endOffset > getNodeIndex(node)) {
4615 						// the node (1 element) will be replaced by its contents (contents().length elements)
4616 						newEndOffset = range.endOffset + (jQuery(node).contents().length - 1);
4617 					}
4618 					jQuery(node).contents().unwrap();
4619 					range.startOffset = newStartOffset;
4620 					range.endOffset = newEndOffset;
4621 					// after unwrapping, we are done
4622 					break;
4623 				}
4624 			}
4625 		}
4626 
4627 		// "Restore the values from values."
4628 		restoreValues(values, range);
4629 	}
4630 
4631 	/**
4632 	 * This method "normalizes" sublists of the given item (which is supposed to be a LI):
4633 	 * If sublists are found in the LI element, they are moved directly into the outer list.
4634 	 * @param item item
4635 	 * @param range range, which will be modified if necessary
4636 	 */
4637 	function normalizeSublists(item, range) {
4638 		// "If item is not an li or it is not editable or its parent is not
4639 		// editable, abort these steps."
4640 		if (!isNamedHtmlElement(item, 'LI') || !isEditable(item) || !isEditable(item.parentNode)) {
4641 			return;
4642 		}
4643 
4644 		// "Let new item be null."
4645 		var newItem = null;
4646 
4647 		function isOlUl(node) {
4648 			return isHtmlElementInArray(node, ["OL", "UL"]);
4649 		}
4650 
4651 		// "While item has an ol or ul child:"
4652 		while ($_(item.childNodes).some(isOlUl)) {
4653 			// "Let child be the last child of item."
4654 			var child = item.lastChild;
4655 
4656 			// "If child is an ol or ul, or new item is null and child is a Text
4657 			// node whose data consists of zero of more space characters:"
4658 			if (isHtmlElementInArray(child, ["OL", "UL"]) || (!newItem && child.nodeType == $_.Node.TEXT_NODE && /^[ \t\n\f\r]*$/.test(child.data))) {
4659 				// "Set new item to null."
4660 				newItem = null;
4661 
4662 				// "Insert child into the parent of item immediately following
4663 				// item, preserving ranges."
4664 				movePreservingRanges(child, item.parentNode, 1 + getNodeIndex(item), range);
4665 
4666 				// "Otherwise:"
4667 			} else {
4668 				// "If new item is null, let new item be the result of calling
4669 				// createElement("li") on the ownerDocument of item, then insert
4670 				// new item into the parent of item immediately after item."
4671 				if (!newItem) {
4672 					newItem = item.ownerDocument.createElement("li");
4673 					item.parentNode.insertBefore(newItem, item.nextSibling);
4674 				}
4675 
4676 				// "Insert child into new item as its first child, preserving
4677 				// ranges."
4678 				movePreservingRanges(child, newItem, 0, range);
4679 			}
4680 		}
4681 	}
4682 
4683 	/**
4684 	 * This method is the exact opposite of normalizeSublists.
4685 	 * List nodes directly nested into each other are corrected to be nested in li elements (so that the resulting lists conform the html5 specification)
4686 	 * @param item list node
4687 	 * @param range range, which is preserved when modifying the list
4688 	 */
4689 	function unNormalizeSublists(item, range) {
4690 		// "If item is not an ol or ol or it is not editable or its parent is not
4691 		// editable, abort these steps."
4692 		if (!isHtmlElementInArray(item, ["OL", "UL"]) || !isEditable(item)) {
4693 			return;
4694 		}
4695 
4696 		var $list = jQuery(item);
4697 		$list.children("ol,ul").each(function (index, sublist) {
4698 			if (isNamedHtmlElement(sublist.previousSibling, "LI")) {
4699 				// move the sublist into the LI
4700 				movePreservingRanges(sublist, sublist.previousSibling, sublist.previousSibling.childNodes.length, range);
4701 			}
4702 		});
4703 	}
4704 
4705 	//@}
4706 	///// Block-extending a range /////
4707 	//@{
4708 
4709 	function blockExtend(range) {
4710 		// "Let start node, start offset, end node, and end offset be the start
4711 		// and end nodes and offsets of the range."
4712 		var startNode = range.startContainer;
4713 		var startOffset = range.startOffset;
4714 		var endNode = range.endContainer;
4715 		var endOffset = range.endOffset;
4716 
4717 		// "If some ancestor container of start node is an li, set start offset to
4718 		// the index of the last such li in tree order, and set start node to that
4719 		// li's parent."
4720 		var liAncestors = $_(getAncestors(startNode).concat(startNode)).filter(function (ancestor) { return isNamedHtmlElement(ancestor, 'li'); }).slice(-1);
4721 		if (liAncestors.length) {
4722 			startOffset = getNodeIndex(liAncestors[0]);
4723 			startNode = liAncestors[0].parentNode;
4724 		}
4725 
4726 		// "If (start node, start offset) is not a block start point, repeat the
4727 		// following steps:"
4728 		if (!isBlockStartPoint(startNode, startOffset)) {
4729 			do {
4730 				// "If start offset is zero, set it to start node's index, then set
4731 				// start node to its parent."
4732 				if (startOffset == 0) {
4733 					startOffset = getNodeIndex(startNode);
4734 					startNode = startNode.parentNode;
4735 
4736 					// "Otherwise, subtract one from start offset."
4737 				} else {
4738 					startOffset--;
4739 				}
4740 
4741 				// "If (start node, start offset) is a block boundary point, break from
4742 				// this loop."
4743 			} while (!isBlockBoundaryPoint(startNode, startOffset));
4744 		}
4745 
4746 		// "While start offset is zero and start node's parent is not null, set
4747 		// start offset to start node's index, then set start node to its parent."
4748 		while (startOffset == 0 && startNode.parentNode) {
4749 			startOffset = getNodeIndex(startNode);
4750 			startNode = startNode.parentNode;
4751 		}
4752 
4753 		// "If some ancestor container of end node is an li, set end offset to one
4754 		// plus the index of the last such li in tree order, and set end node to
4755 		// that li's parent."
4756 		liAncestors = $_(getAncestors(endNode).concat(endNode)).filter(function (ancestor) { return isNamedHtmlElement(ancestor, 'li'); }).slice(-1);
4757 		if (liAncestors.length) {
4758 			endOffset = 1 + getNodeIndex(liAncestors[0]);
4759 			endNode = liAncestors[0].parentNode;
4760 		}
4761 
4762 		// "If (end node, end offset) is not a block end point, repeat the
4763 		// following steps:"
4764 		if (!isBlockEndPoint(endNode, endOffset)) {
4765 			do {
4766 				// "If end offset is end node's length, set it to one plus end node's
4767 				// index, then set end node to its parent."
4768 				if (endOffset == getNodeLength(endNode)) {
4769 					endOffset = 1 + getNodeIndex(endNode);
4770 					endNode = endNode.parentNode;
4771 
4772 					// "Otherwise, add one to end offset.
4773 				} else {
4774 					endOffset++;
4775 				}
4776 
4777 				// "If (end node, end offset) is a block boundary point, break from
4778 				// this loop."
4779 			} while (!isBlockBoundaryPoint(endNode, endOffset));
4780 		}
4781 
4782 		// "While end offset is end node's length and end node's parent is not
4783 		// null, set end offset to one plus end node's index, then set end node to
4784 		// its parent."
4785 		while (endOffset == getNodeLength(endNode) && endNode.parentNode) {
4786 			endOffset = 1 + getNodeIndex(endNode);
4787 			endNode = endNode.parentNode;
4788 		}
4789 
4790 		// "Let new range be a new range whose start and end nodes and offsets
4791 		// are start node, start offset, end node, and end offset."
4792 		var newRange = Aloha.createRange();
4793 		newRange.setStart(startNode, startOffset);
4794 		newRange.setEnd(endNode, endOffset);
4795 
4796 		// "Return new range."
4797 		return newRange;
4798 	}
4799 
4800 	function getSelectionListState() {
4801 		// "Block-extend the active range, and let new range be the result."
4802 		var newRange = blockExtend(getActiveRange());
4803 
4804 		// "Let node list be a list of nodes, initially empty."
4805 		//
4806 		// "For each node contained in new range, append node to node list if the
4807 		// last member of node list (if any) is not an ancestor of node; node is
4808 		// editable; node is not an indentation element; and node is either an ol
4809 		// or ul, or the child of an ol or ul, or an allowed child of "li"."
4810 		var nodeList = getContainedNodes(newRange, function (node) {
4811 			return isEditable(node) && !isIndentationElement(node) && (isHtmlElementInArray(node, ["ol", "ul"]) || isHtmlElementInArray(node.parentNode, ["ol", "ul"]) || isAllowedChild(node, "li"));
4812 		});
4813 
4814 		// "If node list is empty, return "none"."
4815 		if (!nodeList.length) {
4816 			return "none";
4817 		}
4818 
4819 		// "If every member of node list is either an ol or the child of an ol or
4820 		// the child of an li child of an ol, and none is a ul or an ancestor of a
4821 		// ul, return "ol"."
4822 		if ($_(nodeList).every(function (node) { return (isNamedHtmlElement(node, 'ol')
4823 														 || isNamedHtmlElement(node.parentNode, "ol")
4824 														 || (isNamedHtmlElement(node.parentNode, "li")
4825 															 && isNamedHtmlElement(node.parentNode.parentNode, "ol"))); })
4826 			    && !$_(nodeList).some(function (node) { return isNamedHtmlElement(node, 'ul') || (node.querySelector && node.querySelector("ul")); })) {
4827 			return "ol";
4828 		}
4829 
4830 		// "If every member of node list is either a ul or the child of a ul or the
4831 		// child of an li child of a ul, and none is an ol or an ancestor of an ol,
4832 		// return "ul"."
4833 		if ($_(nodeList).every(function (node) { return (isNamedHtmlElement(node, 'ul')
4834 														 || isNamedHtmlElement(node.parentNode, "ul")
4835 														 || (isNamedHtmlElement(node.parentNode, "li")
4836 															 && isNamedHtmlElement(node.parentNode.parentNode, "ul"))); })
4837 			    && !$_(nodeList).some(function (node) { return isNamedHtmlElement(node, 'ol') || (node.querySelector && node.querySelector("ol")); })) {
4838 			return "ul";
4839 		}
4840 
4841 		var hasOl = $_(nodeList).some(function (node) {
4842 			return (isNamedHtmlElement(node, 'ol')
4843 					|| isNamedHtmlElement(node.parentNode, "ol")
4844 					|| (node.querySelector && node.querySelector("ol"))
4845 					|| (isNamedHtmlElement(node.parentNode, "li")
4846 						&& isNamedHtmlElement(node.parentNode.parentNode, "ol")));
4847 		});
4848 		var hasUl = $_(nodeList).some(function (node) {
4849 			return (isNamedHtmlElement(node, 'ul')
4850 					|| isNamedHtmlElement(node.parentNode, "ul")
4851 					|| (node.querySelector && node.querySelector("ul"))
4852 					|| (isNamedHtmlElement(node.parentNode, "li")
4853 						&& isNamedHtmlElement(node.parentNode.parentNode, "ul")));
4854 		});
4855 		// "If some member of node list is either an ol or the child or ancestor of
4856 		// an ol or the child of an li child of an ol, and some member of node list
4857 		// is either a ul or the child or ancestor of a ul or the child of an li
4858 		// child of a ul, return "mixed"."
4859 		if (hasOl && hasUl) {
4860 			return "mixed";
4861 		}
4862 
4863 		// "If some member of node list is either an ol or the child or ancestor of
4864 		// an ol or the child of an li child of an ol, return "mixed ol"."
4865 		if (hasOl) {
4866 			return "mixed ol";
4867 		}
4868 
4869 		// "If some member of node list is either a ul or the child or ancestor of
4870 		// a ul or the child of an li child of a ul, return "mixed ul"."
4871 		if (hasUl) {
4872 			return "mixed ul";
4873 		}
4874 
4875 		// "Return "none"."
4876 		return "none";
4877 	}
4878 
4879 	function getAlignmentValue(node) {
4880 		// "While node is neither null nor an Element, or it is an Element but its
4881 		// "display" property has resolved value "inline" or "none", set node to
4882 		// its parent."
4883 		while ((node && node.nodeType != $_.Node.ELEMENT_NODE) || (node.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(node).display, ["inline", "none"]) != -1)) {
4884 			node = node.parentNode;
4885 		}
4886 
4887 		// "If node is not an Element, return "left"."
4888 		if (!node || node.nodeType != $_.Node.ELEMENT_NODE) {
4889 			return "left";
4890 		}
4891 
4892 		var resolvedValue = $_.getComputedStyle(node).textAlign
4893 		// Hack around browser non-standardness
4894 			.replace(/^-(moz|webkit)-/, "").replace(/^auto$/, "start");
4895 
4896 		// "If node's "text-align" property has resolved value "start", return
4897 		// "left" if the directionality of node is "ltr", "right" if it is "rtl"."
4898 		if (resolvedValue == "start") {
4899 			return getDirectionality(node) == "ltr" ? "left" : "right";
4900 		}
4901 
4902 		// "If node's "text-align" property has resolved value "end", return
4903 		// "right" if the directionality of node is "ltr", "left" if it is "rtl"."
4904 		if (resolvedValue == "end") {
4905 			return getDirectionality(node) == "ltr" ? "right" : "left";
4906 		}
4907 
4908 		// "If node's "text-align" property has resolved value "center", "justify",
4909 		// "left", or "right", return that value."
4910 		if (jQuery.inArray(resolvedValue, ["center", "justify", "left", "right"]) != -1) {
4911 			return resolvedValue;
4912 		}
4913 
4914 		// "Return "left"."
4915 		return "left";
4916 	}
4917 
4918 	//@}
4919 	///// Recording and restoring overrides /////
4920 	//@{
4921 
4922 	function recordCurrentOverrides(range) {
4923 		// "Let overrides be a list of (string, string or boolean) ordered pairs,
4924 		// initially empty."
4925 		var overrides = [];
4926 
4927 		// "If there is a value override for "createLink", add ("createLink", value
4928 		// override for "createLink") to overrides."
4929 		if (getValueOverride("createlink", range) !== undefined) {
4930 			overrides.push(["createlink", getValueOverride("createlink", range)]);
4931 		}
4932 
4933 		// "For each command in the list "bold", "italic", "strikethrough",
4934 		// "subscript", "superscript", "underline", in order: if there is a state
4935 		// override for command, add (command, command's state override) to
4936 		// overrides."
4937 		$_(["bold", "italic", "strikethrough", "subscript", "superscript", "underline"]).forEach(function (command) {
4938 			if (getStateOverride(command, range) !== undefined) {
4939 				overrides.push([command, getStateOverride(command, range)]);
4940 			}
4941 		});
4942 
4943 		// "For each command in the list "fontName", "fontSize", "foreColor",
4944 		// "hiliteColor", in order: if there is a value override for command, add
4945 		// (command, command's value override) to overrides."
4946 		$_(["fontname", "fontsize", "forecolor", "hilitecolor"]).forEach(function (command) {
4947 			if (getValueOverride(command, range) !== undefined) {
4948 				overrides.push([command, getValueOverride(command, range)]);
4949 			}
4950 		});
4951 
4952 		// "Return overrides."
4953 		return overrides;
4954 	}
4955 
4956 	function recordCurrentStatesAndValues(range) {
4957 		// "Let overrides be a list of (string, string or boolean) ordered pairs,
4958 		// initially empty."
4959 		var overrides = [];
4960 
4961 		// "Let node be the first editable Text node effectively contained in the
4962 		// active range, or null if there is none."
4963 		var node = $_(getAllEffectivelyContainedNodes(range)).filter(function (node) {
4964 			return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
4965 		})[0];
4966 
4967 		// "If node is null, return overrides."
4968 		if (!node) {
4969 			return overrides;
4970 		}
4971 
4972 		// "Add ("createLink", value for "createLink") to overrides."
4973 		overrides.push(["createlink", commands.createlink.value(range)]);
4974 
4975 		// "For each command in the list "bold", "italic", "strikethrough",
4976 		// "subscript", "superscript", "underline", in order: if node's effective
4977 		// command value for command is one of its inline command activated values,
4978 		// add (command, true) to overrides, and otherwise add (command, false) to
4979 		// overrides."
4980 		$_(["bold", "italic", "strikethrough", "subscript", "superscript", "underline"]).forEach(function (command) {
4981 			if ($_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(node, command)) != -1) {
4982 				overrides.push([command, true]);
4983 			} else {
4984 				overrides.push([command, false]);
4985 			}
4986 		});
4987 
4988 		// "For each command in the list "fontName", "foreColor", "hiliteColor", in
4989 		// order: add (command, command's value) to overrides."
4990 
4991 		$_(["fontname", "fontsize", "forecolor", "hilitecolor"]).forEach(function (command) {
4992 			overrides.push([command, commands[command].value(range)]);
4993 		});
4994 
4995 		// "Add ("fontSize", node's effective command value for "fontSize") to
4996 		// overrides."
4997 		overrides.push(["fontsize", getEffectiveCommandValue(node, "fontsize")]);
4998 
4999 		// "Return overrides."
5000 		return overrides;
5001 	}
5002 
5003 	function restoreStatesAndValues(overrides, range) {
5004 		var i;
5005 		var command;
5006 		var override;
5007 		// "Let node be the first editable Text node effectively contained in the
5008 		// active range, or null if there is none."
5009 		var node = $_(getAllEffectivelyContainedNodes(range)).filter(function (node) {
5010 			return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
5011 		})[0];
5012 
5013 		function isEditableTextNode(node) {
5014 			return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
5015 		}
5016 
5017 		// "If node is not null, then for each (command, override) pair in
5018 		// overrides, in order:"
5019 		if (node) {
5020 
5021 			for (i = 0; i < overrides.length; i++) {
5022 				command = overrides[i][0];
5023 				override = overrides[i][1];
5024 
5025 				// "If override is a boolean, and queryCommandState(command)
5026 				// returns something different from override, call
5027 				// execCommand(command)."
5028 				if (typeof override == "boolean" && myQueryCommandState(command, range) != override) {
5029 					myExecCommand(command, false, override, range);
5030 
5031 					// "Otherwise, if override is a string, and command is not
5032 					// "fontSize", and queryCommandValue(command) returns something not
5033 					// equivalent to override, call execCommand(command, false,
5034 					// override)."
5035 				} else if (typeof override == "string" && command != "fontsize" && !areEquivalentValues(command, myQueryCommandValue(command, range), override)) {
5036 					myExecCommand(command, false, override, range);
5037 
5038 					// "Otherwise, if override is a string; and command is "fontSize";
5039 					// and either there is a value override for "fontSize" that is not
5040 					// equal to override, or there is no value override for "fontSize"
5041 					// and node's effective command value for "fontSize" is not loosely
5042 					// equivalent to override: call execCommand("fontSize", false,
5043 					// override)."
5044 				} else if (typeof override == "string"
5045 						   && command == "fontsize"
5046 						   && ((getValueOverride("fontsize", range) !== undefined
5047 								&& getValueOverride("fontsize", range) !== override)
5048 							   || (getValueOverride("fontsize", range) === undefined
5049 								   && !areLooselyEquivalentValues(command, getEffectiveCommandValue(node, "fontsize"), override)))) {
5050 					myExecCommand("fontsize", false, override, range);
5051 
5052 					// "Otherwise, continue this loop from the beginning."
5053 				} else {
5054 					continue;
5055 				}
5056 
5057 				// "Set node to the first editable Text node effectively contained
5058 				// in the active range, if there is one."
5059 				node = $_(getAllEffectivelyContainedNodes(range)).filter(isEditableTextNode)[0] || node;
5060 			}
5061 
5062 			// "Otherwise, for each (command, override) pair in overrides, in order:"
5063 		} else {
5064 			for (i = 0; i < overrides.length; i++) {
5065 				command = overrides[i][0];
5066 				override = overrides[i][1];
5067 
5068 				// "If override is a boolean, set the state override for command to
5069 				// override."
5070 				if (typeof override == "boolean") {
5071 					setStateOverride(command, override, range);
5072 				}
5073 
5074 				// "If override is a string, set the value override for command to
5075 				// override."
5076 				if (typeof override == "string") {
5077 					setValueOverride(command, override, range);
5078 				}
5079 			}
5080 		}
5081 	}
5082 
5083 	//@}
5084 	///// Canonical space sequences /////
5085 	//@{
5086 
5087 	function canonicalSpaceSequence(n, nonBreakingStart, nonBreakingEnd) {
5088 		// "If n is zero, return the empty string."
5089 		if (n == 0) {
5090 			return "";
5091 		}
5092 
5093 		// "If n is one and both non-breaking start and non-breaking end are false,
5094 		// return a single space (U+0020)."
5095 		if (n == 1 && !nonBreakingStart && !nonBreakingEnd) {
5096 			return " ";
5097 		}
5098 
5099 		// "If n is one, return a single non-breaking space (U+00A0)."
5100 		if (n == 1) {
5101 			return "\xa0";
5102 		}
5103 
5104 		// "Let buffer be the empty string."
5105 		var buffer = "";
5106 
5107 		// "If non-breaking start is true, let repeated pair be U+00A0 U+0020.
5108 		// Otherwise, let it be U+0020 U+00A0."
5109 		var repeatedPair;
5110 		if (nonBreakingStart) {
5111 			repeatedPair = "\xa0 ";
5112 		} else {
5113 			repeatedPair = " \xa0";
5114 		}
5115 
5116 		// "While n is greater than three, append repeated pair to buffer and
5117 		// subtract two from n."
5118 		while (n > 3) {
5119 			buffer += repeatedPair;
5120 			n -= 2;
5121 		}
5122 
5123 		// "If n is three, append a three-element string to buffer depending on
5124 		// non-breaking start and non-breaking end:"
5125 		if (n == 3) {
5126 			buffer += !nonBreakingStart && !nonBreakingEnd ? " \xa0 " : nonBreakingStart && !nonBreakingEnd ? "\xa0\xa0 " : !nonBreakingStart && nonBreakingEnd ? " \xa0\xa0" : nonBreakingStart && nonBreakingEnd ? "\xa0 \xa0" : "impossible";
5127 
5128 			// "Otherwise, append a two-element string to buffer depending on
5129 			// non-breaking start and non-breaking end:"
5130 		} else {
5131 			buffer += !nonBreakingStart && !nonBreakingEnd ? "\xa0 " : nonBreakingStart && !nonBreakingEnd ? "\xa0 " : !nonBreakingStart && nonBreakingEnd ? " \xa0" : nonBreakingStart && nonBreakingEnd ? "\xa0\xa0" : "impossible";
5132 		}
5133 
5134 		// "Return buffer."
5135 		return buffer;
5136 	}
5137 
5138 	function canonicalizeWhitespace(node, offset) {
5139 		// "If node is neither editable nor an editing host, abort these steps."
5140 		if (!isEditable(node) && !isEditingHost(node)) {
5141 			return;
5142 		}
5143 
5144 		// "Let start node equal node and let start offset equal offset."
5145 		var startNode = node;
5146 		var startOffset = offset;
5147 
5148 		// "Repeat the following steps:"
5149 		while (true) {
5150 			// "If start node has a child in the same editing host with index start
5151 			// offset minus one, set start node to that child, then set start
5152 			// offset to start node's length."
5153 			if (0 <= startOffset - 1 && inSameEditingHost(startNode, startNode.childNodes[startOffset - 1])) {
5154 				startNode = startNode.childNodes[startOffset - 1];
5155 				startOffset = getNodeLength(startNode);
5156 
5157 				// "Otherwise, if start offset is zero and start node does not follow a
5158 				// line break and start node's parent is in the same editing host, set
5159 				// start offset to start node's index, then set start node to its
5160 				// parent."
5161 			} else if (startOffset == 0 && !followsLineBreak(startNode) && inSameEditingHost(startNode, startNode.parentNode)) {
5162 				startOffset = getNodeIndex(startNode);
5163 				startNode = startNode.parentNode;
5164 
5165 				// "Otherwise, if start node is a Text node and its parent's resolved
5166 				// value for "white-space" is neither "pre" nor "pre-wrap" and start
5167 				// offset is not zero and the (start offset − 1)st element of start
5168 				// node's data is a space (0x0020) or non-breaking space (0x00A0),
5169 				// subtract one from start offset."
5170 			} 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])) {
5171 				startOffset--;
5172 
5173 				// "Otherwise, break from this loop."
5174 			} else {
5175 				break;
5176 			}
5177 		}
5178 
5179 		// "Let end node equal start node and end offset equal start offset."
5180 		var endNode = startNode;
5181 		var endOffset = startOffset;
5182 
5183 		// "Let length equal zero."
5184 		var length = 0;
5185 
5186 		// "Let follows space be false."
5187 		var followsSpace = false;
5188 
5189 		// "Repeat the following steps:"
5190 		while (true) {
5191 			// "If end node has a child in the same editing host with index end
5192 			// offset, set end node to that child, then set end offset to zero."
5193 			if (endOffset < endNode.childNodes.length && inSameEditingHost(endNode, endNode.childNodes[endOffset])) {
5194 				endNode = endNode.childNodes[endOffset];
5195 				endOffset = 0;
5196 
5197 				// "Otherwise, if end offset is end node's length and end node does not
5198 				// precede a line break and end node's parent is in the same editing
5199 				// host, set end offset to one plus end node's index, then set end node
5200 				// to its parent."
5201 			} else if (endOffset == getNodeLength(endNode) && !precedesLineBreak(endNode) && inSameEditingHost(endNode, endNode.parentNode)) {
5202 				endOffset = 1 + getNodeIndex(endNode);
5203 				endNode = endNode.parentNode;
5204 
5205 				// "Otherwise, if end node is a Text node and its parent's resolved
5206 				// value for "white-space" is neither "pre" nor "pre-wrap" and end
5207 				// offset is not end node's length and the end offsetth element of
5208 				// end node's data is a space (0x0020) or non-breaking space (0x00A0):"
5209 			} 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])) {
5210 				// "If follows space is true and the end offsetth element of end
5211 				// node's data is a space (0x0020), call deleteData(end offset, 1)
5212 				// on end node, then continue this loop from the beginning."
5213 				if (followsSpace && " " == endNode.data[endOffset]) {
5214 					endNode.deleteData(endOffset, 1);
5215 					continue;
5216 				}
5217 
5218 				// "Set follows space to true if the end offsetth element of end
5219 				// node's data is a space (0x0020), false otherwise."
5220 				followsSpace = " " == endNode.data[endOffset];
5221 
5222 				// "Add one to end offset."
5223 				endOffset++;
5224 
5225 				// "Add one to length."
5226 				length++;
5227 
5228 				// "Otherwise, break from this loop."
5229 			} else {
5230 				break;
5231 			}
5232 		}
5233 
5234 		// "Let replacement whitespace be the canonical space sequence of length
5235 		// length. non-breaking start is true if start offset is zero and start
5236 		// node follows a line break, and false otherwise. non-breaking end is true
5237 		// if end offset is end node's length and end node precedes a line break,
5238 		// and false otherwise."
5239 		var replacementWhitespace = canonicalSpaceSequence(length, startOffset == 0 && followsLineBreak(startNode), endOffset == getNodeLength(endNode) && precedesLineBreak(endNode));
5240 
5241 		// "While (start node, start offset) is before (end node, end offset):"
5242 		while (getPosition(startNode, startOffset, endNode, endOffset) == "before") {
5243 			// "If start node has a child with index start offset, set start node
5244 			// to that child, then set start offset to zero."
5245 			if (startOffset < startNode.childNodes.length) {
5246 				startNode = startNode.childNodes[startOffset];
5247 				startOffset = 0;
5248 
5249 				// "Otherwise, if start node is not a Text node or if start offset is
5250 				// start node's length, set start offset to one plus start node's
5251 				// index, then set start node to its parent."
5252 			} else if (startNode.nodeType != $_.Node.TEXT_NODE || startOffset == getNodeLength(startNode)) {
5253 				startOffset = 1 + getNodeIndex(startNode);
5254 				startNode = startNode.parentNode;
5255 
5256 				// "Otherwise:"
5257 			} else {
5258 				// "Remove the first element from replacement whitespace, and let
5259 				// element be that element."
5260 				var element = replacementWhitespace[0];
5261 				replacementWhitespace = replacementWhitespace.slice(1);
5262 
5263 				// "If element is not the same as the start offsetth element of
5264 				// start node's data:"
5265 				if (element != startNode.data[startOffset]) {
5266 					// "Call insertData(start offset, element) on start node."
5267 					startNode.insertData(startOffset, element);
5268 
5269 					// "Call deleteData(start offset + 1, 1) on start node."
5270 					startNode.deleteData(startOffset + 1, 1);
5271 				}
5272 
5273 				// "Add one to start offset."
5274 				startOffset++;
5275 			}
5276 		}
5277 	}
5278 
5279 	//@}
5280 	///// Deleting the contents of a range /////
5281 	//@{
5282 
5283 	function deleteContents(arg1, arg2, arg3, arg4, arg5) {
5284 		// We accept several different calling conventions:
5285 		//
5286 		// 1) A single argument, which is a range.
5287 		//
5288 		// 2) Two arguments, the first being a range and the second flags.
5289 		//
5290 		// 3) Four arguments, the start and end of a range.
5291 		//
5292 		// 4) Five arguments, the start and end of a range plus flags.
5293 		//
5294 		// The flags argument is a dictionary that can have up to two keys,
5295 		// blockMerging and stripWrappers, whose corresponding values are
5296 		// interpreted as boolean.  E.g., {stripWrappers: false}.
5297 		var range;
5298 		var flags = {};
5299 		var i;
5300 
5301 		if (arguments.length < 3) {
5302 			range = arg1;
5303 		} else {
5304 			range = Aloha.createRange();
5305 			range.setStart(arg1, arg2);
5306 			range.setEnd(arg3, arg4);
5307 		}
5308 		if (arguments.length == 2) {
5309 			flags = arg2;
5310 		}
5311 		if (arguments.length == 5) {
5312 			flags = arg5;
5313 		}
5314 
5315 		var blockMerging = null != flags.blockMerging ? !!flags.blockMerging : true;
5316 		var stripWrappers = null != flags.stripWrappers ? !!flags.stripWrappers : true;
5317 
5318 		// "If range is null, abort these steps and do nothing."
5319 		if (!range) {
5320 			return;
5321 		}
5322 
5323 		// "Let start node, start offset, end node, and end offset be range's start
5324 		// and end nodes and offsets."
5325 		var startNode = range.startContainer;
5326 		var startOffset = range.startOffset;
5327 		var endNode = range.endContainer;
5328 		var endOffset = range.endOffset;
5329 		var referenceNode;
5330 
5331 		// "While start node has at least one child:"
5332 		while (startNode.hasChildNodes()) {
5333 			// "If start offset is start node's length, and start node's parent is
5334 			// in the same editing host, and start node is an inline node, set
5335 			// start offset to one plus the index of start node, then set start
5336 			// node to its parent and continue this loop from the beginning."
5337 			if (startOffset == getNodeLength(startNode) && inSameEditingHost(startNode, startNode.parentNode) && isInlineNode(startNode)) {
5338 				startOffset = 1 + getNodeIndex(startNode);
5339 				startNode = startNode.parentNode;
5340 				continue;
5341 			}
5342 
5343 			// "If start offset is start node's length, break from this loop."
5344 			if (startOffset == getNodeLength(startNode)) {
5345 				break;
5346 			}
5347 
5348 			// "Let reference node be the child of start node with index equal to
5349 			// start offset."
5350 			referenceNode = startNode.childNodes[startOffset];
5351 
5352 			// "If reference node is a block node or an Element with no children,
5353 			// or is neither an Element nor a Text node, break from this loop."
5354 			if (isBlockNode(referenceNode) || (referenceNode.nodeType == $_.Node.ELEMENT_NODE && !referenceNode.hasChildNodes()) || (referenceNode.nodeType != $_.Node.ELEMENT_NODE && referenceNode.nodeType != $_.Node.TEXT_NODE)) {
5355 				break;
5356 			}
5357 
5358 			// "Set start node to reference node and start offset to 0."
5359 			startNode = referenceNode;
5360 			startOffset = 0;
5361 		}
5362 
5363 		// "While end node has at least one child:"
5364 		while (endNode.hasChildNodes()) {
5365 			// "If end offset is 0, and end node's parent is in the same editing
5366 			// host, and end node is an inline node, set end offset to the index of
5367 			// end node, then set end node to its parent and continue this loop
5368 			// from the beginning."
5369 			if (endOffset == 0 && inSameEditingHost(endNode, endNode.parentNode) && isInlineNode(endNode)) {
5370 				endOffset = getNodeIndex(endNode);
5371 				endNode = endNode.parentNode;
5372 				continue;
5373 			}
5374 
5375 			// "If end offset is 0, break from this loop."
5376 			if (endOffset == 0) {
5377 				break;
5378 			}
5379 
5380 			// "Let reference node be the child of end node with index equal to end
5381 			// offset minus one."
5382 			referenceNode = endNode.childNodes[endOffset - 1];
5383 
5384 			// "If reference node is a block node or an Element with no children,
5385 			// or is neither an Element nor a Text node, break from this loop."
5386 			if (isBlockNode(referenceNode) || (referenceNode.nodeType == $_.Node.ELEMENT_NODE && !referenceNode.hasChildNodes()) || (referenceNode.nodeType != $_.Node.ELEMENT_NODE && referenceNode.nodeType != $_.Node.TEXT_NODE)) {
5387 				break;
5388 			}
5389 
5390 			// "Set end node to reference node and end offset to the length of
5391 			// reference node."
5392 			endNode = referenceNode;
5393 			endOffset = getNodeLength(referenceNode);
5394 		}
5395 
5396 		// "If (end node, end offset) is not after (start node, start offset), set
5397 		// range's end to its start and abort these steps."
5398 		if (getPosition(endNode, endOffset, startNode, startOffset) !== "after") {
5399 			range.setEnd(range.startContainer, range.startOffset);
5400 			return range;
5401 		}
5402 
5403 		// "If start node is a Text node and start offset is 0, set start offset to
5404 		// the index of start node, then set start node to its parent."
5405 		// Commented out for unknown reason
5406 		//if (startNode.nodeType == $_.Node.TEXT_NODE && startOffset == 0 && startNode != endNode) {
5407 		//		startOffset = getNodeIndex(startNode);
5408 		//		startNode = startNode.parentNode;
5409 		//}
5410 
5411 		// "If end node is a Text node and end offset is its length, set end offset
5412 		// to one plus the index of end node, then set end node to its parent."
5413 		if (endNode.nodeType == $_.Node.TEXT_NODE && endOffset == getNodeLength(endNode) && startNode != endNode) {
5414 			endOffset = 1 + getNodeIndex(endNode);
5415 			endNode = endNode.parentNode;
5416 		}
5417 
5418 		// "Set range's start to (start node, start offset) and its end to (end
5419 		// node, end offset)."
5420 		range.setStart(startNode, startOffset);
5421 		range.setEnd(endNode, endOffset);
5422 
5423 		// "Let start block be the start node of range."
5424 		var startBlock = range.startContainer;
5425 
5426 		// "While start block's parent is in the same editing host and start block
5427 		// is an inline node, set start block to its parent."
5428 		while (inSameEditingHost(startBlock, startBlock.parentNode) && isInlineNode(startBlock)) {
5429 			startBlock = startBlock.parentNode;
5430 		}
5431 
5432 		// "If start block is neither a block node nor an editing host, or "span"
5433 		// is not an allowed child of start block, or start block is a td or th,
5434 		// set start block to null."
5435 		if ((!isBlockNode(startBlock) && !isEditingHost(startBlock)) || !isAllowedChild("span", startBlock) || isHtmlElementInArray(startBlock, ["td", "th"])) {
5436 			startBlock = null;
5437 		}
5438 
5439 		// "Let end block be the end node of range."
5440 		var endBlock = range.endContainer;
5441 
5442 		// "While end block's parent is in the same editing host and end block is
5443 		// an inline node, set end block to its parent."
5444 		while (inSameEditingHost(endBlock, endBlock.parentNode) && isInlineNode(endBlock)) {
5445 			endBlock = endBlock.parentNode;
5446 		}
5447 
5448 		// "If end block is neither a block node nor an editing host, or "span" is
5449 		// not an allowed child of end block, or end block is a td or th, set end
5450 		// block to null."
5451 		if ((!isBlockNode(endBlock) && !isEditingHost(endBlock)) || !isAllowedChild("span", endBlock) || isHtmlElementInArray(endBlock, ["td", "th"])) {
5452 			endBlock = null;
5453 		}
5454 
5455 		// "Record current states and values, and let overrides be the result."
5456 		var overrides = recordCurrentStatesAndValues(range);
5457 		var parent_;
5458 		// "If start node and end node are the same, and start node is an editable
5459 		// Text node:"
5460 		if (startNode == endNode && isEditable(startNode) && startNode.nodeType == $_.Node.TEXT_NODE) {
5461 			// "Let parent be the parent of node."
5462 			parent_ = startNode.parentNode;
5463 
5464 			// "Call deleteData(start offset, end offset − start offset) on start
5465 			// node."
5466 			startNode.deleteData(startOffset, endOffset - startOffset);
5467 
5468 			// if deleting the text moved two spaces together, we replace the left one by a  , which makes the two spaces a visible
5469 			// two space sequence
5470 			if (startOffset > 0 && startNode.data.substr(startOffset - 1, 1) === ' ' && startOffset < startNode.data.length && startNode.data.substr(startOffset, 1) === ' ') {
5471 				startNode.replaceData(startOffset - 1, 1, '\xa0');
5472 			}
5473 
5474 			// "Canonicalize whitespace at (start node, start offset)."
5475 			canonicalizeWhitespace(startNode, startOffset);
5476 
5477 			// "Set range's end to its start."
5478 			// Ok, also set the range's start to its start, because modifying the text
5479 			// might have somehow corrupted the range
5480 			range.setStart(range.startContainer, range.startOffset);
5481 			range.setEnd(range.startContainer, range.startOffset);
5482 
5483 			// "Restore states and values from overrides."
5484 			restoreStatesAndValues(overrides, range);
5485 
5486 			// "If parent is editable or an editing host, is not an inline node,
5487 			// and has no children, call createElement("br") on the context object
5488 			// and append the result as the last child of parent."
5489 			// only do this, if the offsetHeight is 0
5490 			if ((isEditable(parent_) || isEditingHost(parent_)) && !isInlineNode(parent_)) {
5491 				ensureContainerEditable(parent_);
5492 			}
5493 
5494 			// "Abort these steps."
5495 			return range;
5496 		}
5497 
5498 		// "If start node is an editable Text node, call deleteData() on it, with
5499 		// start offset as the first argument and (length of start node − start
5500 		// offset) as the second argument."
5501 		if (isEditable(startNode) && startNode.nodeType == $_.Node.TEXT_NODE) {
5502 			startNode.deleteData(startOffset, getNodeLength(startNode) - startOffset);
5503 		}
5504 
5505 		// "Let node list be a list of nodes, initially empty."
5506 		//
5507 		// "For each node contained in range, append node to node list if the last
5508 		// member of node list (if any) is not an ancestor of node; node is
5509 		// editable; and node is not a thead, tbody, tfoot, tr, th, or td."
5510 		var nodeList = getContainedNodes(
5511 			range,
5512 			function (node) {
5513 				return isEditable(node) && !isHtmlElementInArray(node, ["thead", "tbody", "tfoot", "tr", "th", "td"]);
5514 			}
5515 		);
5516 
5517 		// "For each node in node list:"
5518 		for (i = 0; i < nodeList.length; i++) {
5519 			var node = nodeList[i];
5520 
5521 			// "Let parent be the parent of node."
5522 			parent_ = node.parentNode;
5523 
5524 			// "Remove node from parent."
5525 			parent_.removeChild(node);
5526 
5527 			// "If strip wrappers is true or parent is not an ancestor container of
5528 			// start node, while parent is an editable inline node with length 0,
5529 			// let grandparent be the parent of parent, then remove parent from
5530 			// grandparent, then set parent to grandparent."
5531 			if (stripWrappers || (!isAncestor(parent_, startNode) && parent_ != startNode)) {
5532 				while (isEditable(parent_) && isInlineNode(parent_) && getNodeLength(parent_) == 0) {
5533 					var grandparent = parent_.parentNode;
5534 					grandparent.removeChild(parent_);
5535 					parent_ = grandparent;
5536 				}
5537 			}
5538 
5539 			// "If parent is editable or an editing host, is not an inline node,
5540 			// and has no children, call createElement("br") on the context object
5541 			// and append the result as the last child of parent."
5542 			// only do this, if the offsetHeight is 0
5543 			if ((isEditable(parent_) || isEditingHost(parent_)) && !isInlineNode(parent_)) {
5544 				ensureContainerEditable(parent_);
5545 			}
5546 		}
5547 
5548 		// "If end node is an editable Text node, call deleteData(0, end offset) on
5549 		// it."
5550 		if (isEditable(endNode) && endNode.nodeType == $_.Node.TEXT_NODE) {
5551 			endNode.deleteData(0, endOffset);
5552 		}
5553 
5554 		// "Canonicalize whitespace at range's start."
5555 		canonicalizeWhitespace(range.startContainer, range.startOffset);
5556 
5557 		// "Canonicalize whitespace at range's end."
5558 		canonicalizeWhitespace(range.endContainer, range.endOffset);
5559 
5560 		// A reference to the position where a node is removed.
5561 		var pos;
5562 
5563 		// "If block merging is false, or start block or end block is null, or
5564 		// start block is not in the same editing host as end block, or start block
5565 		// and end block are the same:"
5566 		if (!blockMerging || !startBlock || !endBlock || !inSameEditingHost(startBlock, endBlock) || startBlock == endBlock) {
5567 			// "Set range's end to its start."
5568 			range.setEnd(range.startContainer, range.startOffset);
5569 
5570 			// Calling delete on the give markup:
5571 			// <editable><block><br>[]</block></editable>
5572 			// should result in:
5573 			// <editable>[]</editable>
5574 			var block = startBlock || endBlock;
5575 			if (isEmptyOnlyChildOfEditingHost(block)) {
5576 				pos = removeNode(block);
5577 				range.setStart(pos.node, pos.offset);
5578 				range.setEnd(pos.node, pos.offset);
5579 			}
5580 
5581 			// "Restore states and values from overrides."
5582 			restoreStatesAndValues(overrides, range);
5583 
5584 			// "Abort these steps."
5585 			return range;
5586 		}
5587 
5588 		// "If start block has one child, which is a collapsed block prop, remove
5589 		// its child from it."
5590 		if (startBlock.children.length == 1 && isCollapsedBlockProp(startBlock.firstChild)) {
5591 			startBlock.removeChild(startBlock.firstChild);
5592 		}
5593 
5594 		// "If end block has one child, which is a collapsed block prop, remove its
5595 		// child from it."
5596 		if (endBlock.children.length == 1 && isCollapsedBlockProp(endBlock.firstChild)) {
5597 			endBlock.removeChild(endBlock.firstChild);
5598 		}
5599 
5600 		var values;
5601 		// "If start block is an ancestor of end block:"
5602 		if (isAncestor(startBlock, endBlock)) {
5603 			// "Let reference node be end block."
5604 			referenceNode = endBlock;
5605 
5606 			// "While reference node is not a child of start block, set reference
5607 			// node to its parent."
5608 			while (referenceNode.parentNode != startBlock) {
5609 				referenceNode = referenceNode.parentNode;
5610 			}
5611 
5612 			// "Set the start and end of range to (start block, index of reference
5613 			// node)."
5614 			range.setStart(startBlock, getNodeIndex(referenceNode));
5615 			range.setEnd(startBlock, getNodeIndex(referenceNode));
5616 
5617 			// "If end block has no children:"
5618 			if (!endBlock.hasChildNodes()) {
5619 				// "While end block is editable and is the only child of its parent
5620 				// and is not a child of start block, let parent equal end block,
5621 				// then remove end block from parent, then set end block to
5622 				// parent."
5623 				while (isEditable(endBlock) && endBlock.parentNode.childNodes.length == 1 && endBlock.parentNode != startBlock) {
5624 					parent_ = endBlock;
5625 					parent_.removeChild(endBlock);
5626 					endBlock = parent_;
5627 				}
5628 
5629 				// "If end block is editable and is not an inline node, and its
5630 				// previousSibling and nextSibling are both inline nodes, call
5631 				// createElement("br") on the context object and insert it into end
5632 				// block's parent immediately after end block."
5633 
5634 				if (isEditable(endBlock) && !isInlineNode(endBlock) && isInlineNode(endBlock.previousSibling) && isInlineNode(endBlock.nextSibling)) {
5635 					endBlock.parentNode.insertBefore(document.createElement("br"), endBlock.nextSibling);
5636 				}
5637 
5638 				// "If end block is editable, remove it from its parent."
5639 				if (isEditable(endBlock)) {
5640 					endBlock.parentNode.removeChild(endBlock);
5641 				}
5642 
5643 				// "Restore states and values from overrides."
5644 				restoreStatesAndValues(overrides, range);
5645 
5646 				// "Abort these steps."
5647 				return range;
5648 			}
5649 
5650 			// "If end block's firstChild is not an inline node, restore states and
5651 			// values from overrides, then abort these steps."
5652 			if (!isInlineNode(endBlock.firstChild)) {
5653 				restoreStatesAndValues(overrides, range);
5654 				return range;
5655 			}
5656 
5657 			// "Let children be a list of nodes, initially empty."
5658 			var children = [];
5659 
5660 			// "Append the first child of end block to children."
5661 			children.push(endBlock.firstChild);
5662 
5663 			// "While children's last member is not a br, and children's last
5664 			// member's nextSibling is an inline node, append children's last
5665 			// member's nextSibling to children."
5666 			while (!isNamedHtmlElement(children[children.length - 1], "br") && isInlineNode(children[children.length - 1].nextSibling)) {
5667 				children.push(children[children.length - 1].nextSibling);
5668 			}
5669 
5670 			// "Record the values of children, and let values be the result."
5671 			values = recordValues(children);
5672 
5673 			// "While children's first member's parent is not start block, split
5674 			// the parent of children."
5675 			while (children[0].parentNode != startBlock) {
5676 				splitParent(children, range);
5677 			}
5678 
5679 			// "If children's first member's previousSibling is an editable br,
5680 			// remove that br from its parent."
5681 			if (isEditable(children[0].previousSibling) && isNamedHtmlElement(children[0].previousSibling, "br")) {
5682 				children[0].parentNode.removeChild(children[0].previousSibling);
5683 			}
5684 
5685 			// "Otherwise, if start block is a descendant of end block:"
5686 		} else if (isDescendant(startBlock, endBlock)) {
5687 			// "Set the start and end of range to (start block, length of start
5688 			// block)."
5689 			range.setStart(startBlock, getNodeLength(startBlock));
5690 			range.setEnd(startBlock, getNodeLength(startBlock));
5691 
5692 			// "Let reference node be start block."
5693 			referenceNode = startBlock;
5694 
5695 			// "While reference node is not a child of end block, set reference
5696 			// node to its parent."
5697 			while (referenceNode.parentNode != endBlock) {
5698 				referenceNode = referenceNode.parentNode;
5699 			}
5700 
5701 			// "If reference node's nextSibling is an inline node and start block's
5702 			// lastChild is a br, remove start block's lastChild from it."
5703 			if (isInlineNode(referenceNode.nextSibling) && isNamedHtmlElement(startBlock.lastChild, "br")) {
5704 				startBlock.removeChild(startBlock.lastChild);
5705 			}
5706 
5707 			// "Let nodes to move be a list of nodes, initially empty."
5708 			var nodesToMove = [];
5709 
5710 			// "If reference node's nextSibling is neither null nor a br nor a
5711 			// block node, append it to nodes to move."
5712 			if (referenceNode.nextSibling && !isNamedHtmlElement(referenceNode.nextSibling, "br") && !isBlockNode(referenceNode.nextSibling)) {
5713 				nodesToMove.push(referenceNode.nextSibling);
5714 			}
5715 
5716 			// "While nodes to move is nonempty and its last member's nextSibling
5717 			// is neither null nor a br nor a block node, append it to nodes to
5718 			// move."
5719 			if (nodesToMove.length && nodesToMove[nodesToMove.length - 1].nextSibling && !isNamedHtmlElement(nodesToMove[nodesToMove.length - 1].nextSibling, "br") && !isBlockNode(nodesToMove[nodesToMove.length - 1].nextSibling)) {
5720 				nodesToMove.push(nodesToMove[nodesToMove.length - 1].nextSibling);
5721 			}
5722 
5723 			// "Record the values of nodes to move, and let values be the result."
5724 			values = recordValues(nodesToMove);
5725 
5726 			// "For each node in nodes to move, append node as the last child of
5727 			// start block, preserving ranges."
5728 			$_(nodesToMove).forEach(function (node) {
5729 				movePreservingRanges(node, startBlock, -1, range);
5730 			});
5731 
5732 			// "If the nextSibling of reference node is a br, remove it from its
5733 			// parent."
5734 			if (isNamedHtmlElement(referenceNode.nextSibling, "br")) {
5735 				referenceNode.parentNode.removeChild(referenceNode.nextSibling);
5736 			}
5737 
5738 			// "Otherwise:"
5739 		} else {
5740 			// "Set the start and end of range to (start block, length of start
5741 			// block)."
5742 			range.setStart(startBlock, getNodeLength(startBlock));
5743 			range.setEnd(startBlock, getNodeLength(startBlock));
5744 
5745 			// "If end block's firstChild is an inline node and start block's
5746 			// lastChild is a br, remove start block's lastChild from it."
5747 			if (isInlineNode(endBlock.firstChild) && isNamedHtmlElement(startBlock.lastChild, "br")) {
5748 				startBlock.removeChild(startBlock.lastChild);
5749 			}
5750 
5751 			// "Record the values of end block's children, and let values be the
5752 			// result."
5753 			values = recordValues([].slice.call(toArray(endBlock.childNodes)));
5754 
5755 			// "While end block has children, append the first child of end block
5756 			// to start block, preserving ranges."
5757 			while (endBlock.hasChildNodes()) {
5758 				movePreservingRanges(endBlock.firstChild, startBlock, -1, range);
5759 			}
5760 
5761 			// "While end block has no children, let parent be the parent of end
5762 			// block, then remove end block from parent, then set end block to
5763 			// parent."
5764 			while (!endBlock.hasChildNodes()) {
5765 				parent_ = endBlock.parentNode;
5766 				parent_.removeChild(endBlock);
5767 				endBlock = parent_;
5768 			}
5769 		}
5770 
5771 		// "Restore the values from values."
5772 		restoreValues(values, range);
5773 
5774 		// Because otherwise calling deleteContents() with the given selection:
5775 		//
5776 		// <editable><block>[foo</block><block>bar]</block></editable>
5777 		//
5778 		// would result in:
5779 		//
5780 		// <editable><block>[]<br /></block></editable>
5781 		//
5782 		// instead of:
5783 		//
5784 		// <editable>[]</editable>
5785 		//
5786 		// Therefore, the below makes it possible to completely empty contents
5787 		// of editing hosts via operations like CTRL+A, DEL.
5788 		//
5789 		// If startBlock is empty, and startBlock is the immediate and only
5790 		// child of its parent editing host, then remove startBlock and collapse
5791 		// the selection at the beginning of the editing post.
5792 		if (isEmptyOnlyChildOfEditingHost(startBlock)) {
5793 			pos = removeNode(startBlock);
5794 			range.setStart(pos.node, pos.offset);
5795 			range.setEnd(pos.node, pos.offset);
5796 			startBlock = pos.node;
5797 		}
5798 
5799 		// "If start block has no children, call createElement("br") on the context
5800 		// object and append the result as the last child of start block."
5801 		ensureContainerEditable(startBlock);
5802 
5803 		// "Restore states and values from overrides."
5804 		restoreStatesAndValues(overrides, range);
5805 
5806 		return range;
5807 	}
5808 
5809 	// "To remove a node node while preserving its descendants, split the parent of
5810 	// node's children if it has any. If it has no children, instead remove it from
5811 	// its parent."
5812 	function removePreservingDescendants(node, range) {
5813 		if (node.hasChildNodes()) {
5814 			splitParent([].slice.call(toArray(node.childNodes)), range);
5815 		} else {
5816 			node.parentNode.removeChild(node);
5817 		}
5818 	}
5819 
5820 	//@}
5821 	///// Indenting and outdenting /////
5822 	//@{
5823 
5824 	function cleanLists(node, range) {
5825 		// remove any whitespace nodes around list nodes
5826 		if (node) {
5827 			jQuery(node).find('ul,ol,li').each(function () {
5828 				jQuery(this).contents().each(function () {
5829 					if (isWhitespaceNode(this)) {
5830 						var index = getNodeIndex(this);
5831 
5832 						// if the range points to somewhere behind the removed text node, we reduce the offset
5833 						if (range.startContainer === this.parentNode && range.startOffset > index) {
5834 							range.startOffset--;
5835 						} else if (range.startContainer === this) {
5836 5837 							// the range starts in the removed text node, let it start right before
5838 							range.startContainer = this.parentNode;
5839 							range.startOffset = index;
5840 						}
5841 						// same thing for end of the range
5842 						if (range.endContainer === this.parentNode && range.endOffset > index) {
5843 							range.endOffset--;
5844 						} else if (range.endContainer === this) {
5845 							range.endContainer = this.parentNode;
5846 							range.endOffset = index;
5847 						}
5848 						// finally remove the whitespace node
5849 						jQuery(this).remove();
5850 					}
5851 				});
5852 			});
5853 		}
5854 	}
5855 
5856 
5857 	//@}
5858 	///// Indenting and outdenting /////
5859 	//@{
5860 
5861 	function indentNodes(nodeList, range) {
5862 		// "If node list is empty, do nothing and abort these steps."
5863 		if (!nodeList.length) {
5864 			return;
5865 		}
5866 
5867 		// "Let first node be the first member of node list."
5868 		var firstNode = nodeList[0];
5869 
5870 		// "If first node's parent is an ol or ul:"
5871 		if (isHtmlElementInArray(firstNode.parentNode, ["OL", "UL"])) {
5872 			// "Let tag be the local name of the parent of first node."
5873 			var tag = firstNode.parentNode.tagName;
5874 
5875 			// "Wrap node list, with sibling criteria returning true for an HTML
5876 			// element with local name tag and false otherwise, and new parent
5877 			// instructions returning the result of calling createElement(tag) on
5878 			// the ownerDocument of first node."
5879 			wrap(
5880 				nodeList,
5881 				function (node) {
5882 					return isHtmlElement_obsolete(node, tag);
5883 				},
5884 				function () {
5885 					return firstNode.ownerDocument.createElement(tag);
5886 				},
5887 				range
5888 			);
5889 
5890 			// "Abort these steps."
5891 			return;
5892 		}
5893 
5894 		// "Wrap node list, with sibling criteria returning true for a simple
5895 		// indentation element and false otherwise, and new parent instructions
5896 		// returning the result of calling createElement("blockquote") on the
5897 		// ownerDocument of first node. Let new parent be the result."
5898 		var newParent = wrap(
5899 			nodeList,
5900 			function (node) {
5901 				return isSimpleIndentationElement(node);
5902 			},
5903 			function () {
5904 				return firstNode.ownerDocument.createElement("blockquote");
5905 			},
5906 			range
5907 		);
5908 
5909 		// "Fix disallowed ancestors of new parent."
5910 		fixDisallowedAncestors(newParent, range);
5911 	}
5912 
5913 	function outdentNode(node, range) {
5914 		// "If node is not editable, abort these steps."
5915 		if (!isEditable(node)) {
5916 			return;
5917 		}
5918 
5919 		// "If node is a simple indentation element, remove node, preserving its
5920 		// descendants.  Then abort these steps."
5921 		if (isSimpleIndentationElement(node)) {
5922 			removePreservingDescendants(node, range);
5923 			return;
5924 		}
5925 
5926 		// "If node is an indentation element:"
5927 		if (isIndentationElement(node)) {
5928 			// "Unset the class and dir attributes of node, if any."
5929 			node.removeAttribute("class");
5930 			node.removeAttribute("dir");
5931 
5932 			// "Unset the margin, padding, and border CSS properties of node."
5933 			node.style.margin = "";
5934 			node.style.padding = "";
5935 			node.style.border = "";
5936 			if (node.getAttribute("style") == "") {
5937 				node.removeAttribute("style");
5938 			}
5939 
5940 			// "Set the tag name of node to "div"."
5941 			setTagName(node, "div", range);
5942 
5943 			// "Abort these steps."
5944 			return;
5945 		}
5946 
5947 		// "Let current ancestor be node's parent."
5948 		var currentAncestor = node.parentNode;
5949 
5950 		// "Let ancestor list be a list of nodes, initially empty."
5951 		var ancestorList = [];
5952 
5953 		// "While current ancestor is an editable Element that is neither a simple
5954 		// indentation element nor an ol nor a ul, append current ancestor to
5955 		// ancestor list and then set current ancestor to its parent."
5956 		while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !isSimpleIndentationElement(currentAncestor) && !isHtmlElementInArray(currentAncestor, ["ol", "ul"])) {
5957 			ancestorList.push(currentAncestor);
5958 			currentAncestor = currentAncestor.parentNode;
5959 		}
5960 
5961 		// "If current ancestor is not an editable simple indentation element:"
5962 		if (!isEditable(currentAncestor) || !isSimpleIndentationElement(currentAncestor)) {
5963 			// "Let current ancestor be node's parent."
5964 			currentAncestor = node.parentNode;
5965 
5966 			// "Let ancestor list be the empty list."
5967 			ancestorList = [];
5968 
5969 			// "While current ancestor is an editable Element that is neither an
5970 			// indentation element nor an ol nor a ul, append current ancestor to
5971 			// ancestor list and then set current ancestor to its parent."
5972 			while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !isIndentationElement(currentAncestor) && !isHtmlElementInArray(currentAncestor, ["ol", "ul"])) {
5973 				ancestorList.push(currentAncestor);
5974 				currentAncestor = currentAncestor.parentNode;
5975 			}
5976 		}
5977 
5978 		// "If node is an ol or ul and current ancestor is not an editable
5979 		// indentation element:"
5980 		if (isHtmlElementInArray(node, ["OL", "UL"]) && (!isEditable(currentAncestor) || !isIndentationElement(currentAncestor))) {
5981 			// "Unset the reversed, start, and type attributes of node, if any are
5982 			// set."
5983 			node.removeAttribute("reversed");
5984 			node.removeAttribute("start");
5985 			node.removeAttribute("type");
5986 
5987 			// "Let children be the children of node."
5988 			var children = [].slice.call(toArray(node.childNodes));
5989 
5990 			// "If node has attributes, and its parent is not an ol or ul, set the
5991 			// tag name of node to "div"."
5992 			if (node.attributes.length && !isHtmlElementInArray(node.parentNode, ["OL", "UL"])) {
5993 				setTagName(node, "div", range);
5994 
5995 				// "Otherwise:"
5996 			} else {
5997 				// "Record the values of node's children, and let values be the
5998 				// result."
5999 				var values = recordValues([].slice.call(toArray(node.childNodes)));
6000 
6001 				// "Remove node, preserving its descendants."
6002 				removePreservingDescendants(node, range);
6003 
6004 				// "Restore the values from values."
6005 				restoreValues(values, range);
6006 			}
6007 
6008 			// "Fix disallowed ancestors of each member of children."
6009 			var i;
6010 			for (i = 0; i < children.length; i++) {
6011 				fixDisallowedAncestors(children[i], range);
6012 			}
6013 
6014 			// "Abort these steps."
6015 			return;
6016 		}
6017 
6018 		// "If current ancestor is not an editable indentation element, abort these
6019 		// steps."
6020 		if (!isEditable(currentAncestor) || !isIndentationElement(currentAncestor)) {
6021 			return;
6022 		}
6023 
6024 		// "Append current ancestor to ancestor list."
6025 		ancestorList.push(currentAncestor);
6026 
6027 		// "Let original ancestor be current ancestor."
6028 		var originalAncestor = currentAncestor;
6029 
6030 		// "While ancestor list is not empty:"
6031 		while (ancestorList.length) {
6032 			// "Let current ancestor be the last member of ancestor list."
6033 			//
6034 			// "Remove the last member of ancestor list."
6035 			currentAncestor = ancestorList.pop();
6036 
6037 			// "Let target be the child of current ancestor that is equal to either
6038 			// node or the last member of ancestor list."
6039 			var target = node.parentNode == currentAncestor ? node : ancestorList[ancestorList.length - 1];
6040 
6041 			// "If target is an inline node that is not a br, and its nextSibling
6042 			// is a br, remove target's nextSibling from its parent."
6043 			if (isInlineNode(target) && !isNamedHtmlElement(target, 'BR') && isNamedHtmlElement(target.nextSibling, "BR")) {
6044 				target.parentNode.removeChild(target.nextSibling);
6045 			}
6046 
6047 			// "Let preceding siblings be the preceding siblings of target, and let
6048 			// following siblings be the following siblings of target."
6049 6050 			var precedingSiblings = [].slice.call(toArray(currentAncestor.childNodes), 0, getNodeIndex(target));
6051 			var followingSiblings = [].slice.call(toArray(currentAncestor.childNodes), 1 + getNodeIndex(target));
6052 
6053 			// "Indent preceding siblings."
6054 			indentNodes(precedingSiblings, range);
6055 
6056 			// "Indent following siblings."
6057 			indentNodes(followingSiblings, range);
6058 		}
6059 
6060 		// "Outdent original ancestor."
6061 		outdentNode(originalAncestor, range);
6062 	}
6063 
6064 
6065 	//@}
6066 	///// Toggling lists /////
6067 	//@{
6068 
6069 	function toggleLists(tagName, range) {
6070 		// "Let mode be "disable" if the selection's list state is tag name, and
6071 		// "enable" otherwise."
6072 		var mode = getSelectionListState() == tagName ? "disable" : "enable";
6073 
6074 		tagName = tagName.toUpperCase();
6075 
6076 		// "Let other tag name be "ol" if tag name is "ul", and "ul" if tag name is
6077 		// "ol"."
6078 		var otherTagName = tagName == "OL" ? "UL" : "OL";
6079 
6080 		// "Let items be a list of all lis that are ancestor containers of the
6081 		// range's start and/or end node."
6082 		//
6083 		// It's annoying to get this in tree order using functional stuff without
6084 		// doing getDescendants(document), which is slow, so I do it imperatively.
6085 		var items = [];
6086 		(function () {
6087 			var ancestorContainer;
6088 			for (ancestorContainer = range.endContainer;
6089 				     ancestorContainer != range.commonAncestorContainer;
6090 				     ancestorContainer = ancestorContainer.parentNode) {
6091 				if (isNamedHtmlElement(ancestorContainer, "li")) {
6092 					items.unshift(ancestorContainer);
6093 				}
6094 			}
6095 			for (ancestorContainer = range.startContainer;
6096 				     ancestorContainer;
6097 				     ancestorContainer = ancestorContainer.parentNode) {
6098 				if (isNamedHtmlElement(ancestorContainer, "li")) {
6099 					items.unshift(ancestorContainer);
6100 				}
6101 			}
6102 		}());
6103 
6104 		// "For each item in items, normalize sublists of item."
6105 		$_(items).forEach(function (thisArg) {
6106 			normalizeSublists(thisArg, range);
6107 		});
6108 
6109 		// "Block-extend the range, and let new range be the result."
6110 		var newRange = blockExtend(range);
6111 
6112 		// "If mode is "enable", then let lists to convert consist of every
6113 		// editable HTML element with local name other tag name that is contained
6114 		// in new range, and for every list in lists to convert:"
6115 		if (mode == "enable") {
6116 			$_(getAllContainedNodes(newRange, function (node) {
6117 				return isEditable(node) && isHtmlElement_obsolete(node, otherTagName);
6118 			})).forEach(function (list) {
6119 				// "If list's previousSibling or nextSibling is an editable HTML
6120 				// element with local name tag name:"
6121 				if ((isEditable(list.previousSibling) && isHtmlElement_obsolete(list.previousSibling, tagName)) || (isEditable(list.nextSibling) && isHtmlElement_obsolete(list.nextSibling, tagName))) {
6122 					// "Let children be list's children."
6123 					var children = [].slice.call(toArray(list.childNodes));
6124 
6125 					// "Record the values of children, and let values be the
6126 					// result."
6127 					var values = recordValues(children);
6128 
6129 					// "Split the parent of children."
6130 					splitParent(children, range);
6131 
6132 					// "Wrap children, with sibling criteria returning true for an
6133 					// HTML element with local name tag name and false otherwise."
6134 					wrap(
6135 						children,
6136 						function (node) {
6137 							return isHtmlElement_obsolete(node, tagName);
6138 						},
6139 						function () {
6140 							return null;
6141 						},
6142 						range
6143 					);
6144 
6145 					// "Restore the values from values."
6146 					restoreValues(values, range);
6147 
6148 					// "Otherwise, set the tag name of list to tag name."
6149 				} else {
6150 					setTagName(list, tagName, range);
6151 				}
6152 			});
6153 		}
6154 
6155 		// "Let node list be a list of nodes, initially empty."
6156 		//
6157 		// "For each node node contained in new range, if node is editable; the
6158 		// last member of node list (if any) is not an ancestor of node; node
6159 		// is not an indentation element; and either node is an ol or ul, or its
6160 		// parent is an ol or ul, or it is an allowed child of "li"; then append
6161 		// node to node list."
6162 		var nodeList = getContainedNodes(newRange, function (node) {
6163 			return isEditable(node) && !isIndentationElement(node) && (isHtmlElementInArray(node, ["OL", "UL"]) || isHtmlElementInArray(node.parentNode, ["OL", "UL"]) || isAllowedChild(node, "li"));
6164 		});
6165 
6166 		// "If mode is "enable", remove from node list any ol or ul whose parent is
6167 		// not also an ol or ul."
6168 		if (mode == "enable") {
6169 			nodeList = $_(nodeList).filter(function (node) {
6170 				return !isHtmlElementInArray(node, ["ol", "ul"]) || isHtmlElementInArray(node.parentNode, ["ol", "ul"]);
6171 			});
6172 		}
6173 
6174 		// "If mode is "disable", then while node list is not empty:"
6175 		var sublist, values;
6176 
6177 		function createLi() {
6178 			return document.createElement("li");
6179 		}
6180 
6181 		function isOlUl(node) {
6182 			return isHtmlElementInArray(node, ["ol", "ul"]);
6183 		}
6184 
6185 		function makeIsElementPred(tagName) {
6186 			return function (node) {
6187 				return isHtmlElement_obsolete(node, tagName);
6188 			};
6189 		}
6190 
6191 		function makeCreateElement(tagName) {
6192 			return function () {
6193 				return document.createElement(tagName);
6194 			};
6195 		}
6196 
6197 		function makeCreateElementSublist(tagName, sublist, range) {
6198 			return function () {
6199 				// "If sublist's first member's parent is not an editable
6200 				// simple indentation element, or sublist's first member's
6201 				// parent's previousSibling is not an editable HTML element
6202 				// with local name tag name, call createElement(tag name)
6203 				// on the context object and return the result."
6204 				if (!isEditable(sublist[0].parentNode) || !isSimpleIndentationElement(sublist[0].parentNode) || !isEditable(sublist[0].parentNode.previousSibling) || !isHtmlElement_obsolete(sublist[0].parentNode.previousSibling, tagName)) {
6205 					return document.createElement(tagName);
6206 				}
6207 
6208 				// "Let list be sublist's first member's parent's
6209 				// previousSibling."
6210 				var list = sublist[0].parentNode.previousSibling;
6211 
6212 				// "Normalize sublists of list's lastChild."
6213 				normalizeSublists(list.lastChild, range);
6214 
6215 				// "If list's lastChild is not an editable HTML element
6216 				// with local name tag name, call createElement(tag name)
6217 				// on the context object, and append the result as the last
6218 				// child of list."
6219 				if (!isEditable(list.lastChild) || !isHtmlElement_obsolete(list.lastChild, tagName)) {
6220 					list.appendChild(document.createElement(tagName));
6221 				}
6222 
6223 				// "Return the last child of list."
6224 				return list.lastChild;
6225 			};
6226 		}
6227 
6228 		if (mode == "disable") {
6229 			while (nodeList.length) {
6230 				// "Let sublist be an empty list of nodes."
6231 				sublist = [];
6232 
6233 				// "Remove the first member from node list and append it to
6234 				// sublist."
6235 				sublist.push(nodeList.shift());
6236 
6237 				// "If the first member of sublist is an HTML element with local
6238 				// name tag name, outdent it and continue this loop from the
6239 				// beginning."
6240 				if (isHtmlElement_obsolete(sublist[0], tagName)) {
6241 					outdentNode(sublist[0], range);
6242 					continue;
6243 				}
6244 
6245 				// "While node list is not empty, and the first member of node list
6246 				// is the nextSibling of the last member of sublist and is not an
6247 				// HTML element with local name tag name, remove the first member
6248 				// from node list and append it to sublist."
6249 				while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isHtmlElement_obsolete(nodeList[0], tagName)) {
6250 					sublist.push(nodeList.shift());
6251 				}
6252 
6253 				// "Record the values of sublist, and let values be the result."
6254 				values = recordValues(sublist);
6255 
6256 				// "Split the parent of sublist."
6257 				splitParent(sublist, range);
6258 
6259 				// "Fix disallowed ancestors of each member of sublist."
6260 				var i;
6261 				for (i = 0; i < sublist.length; i++) {
6262 					fixDisallowedAncestors(sublist[i], range);
6263 				}
6264 
6265 				// "Restore the values from values."
6266 				restoreValues(values, range);
6267 			}
6268 
6269 			// "Otherwise, while node list is not empty:"
6270 		} else {
6271 			while (nodeList.length) {
6272 				// "Let sublist be an empty list of nodes."
6273 				sublist = [];
6274 
6275 				// "While either sublist is empty, or node list is not empty and
6276 				// its first member is the nextSibling of sublist's last member:"
6277 				while (!sublist.length || (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling)) {
6278 					// "If node list's first member is a p or div, set the tag name
6279 					// of node list's first member to "li", and append the result
6280 					// to sublist. Remove the first member from node list."
6281 					if (isHtmlElementInArray(nodeList[0], ["p", "div"])) {
6282 						sublist.push(setTagName(nodeList[0], "li", range));
6283 						nodeList.shift();
6284 
6285 						// "Otherwise, if the first member of node list is an li or ol
6286 						// or ul, remove it from node list and append it to sublist."
6287 					} else if (isHtmlElementInArray(nodeList[0], ["li", "ol", "ul"])) {
6288 						sublist.push(nodeList.shift());
6289 
6290 						// "Otherwise:"
6291 					} else {
6292 						// "Let nodes to wrap be a list of nodes, initially empty."
6293 						var nodesToWrap = [];
6294 
6295 						// "While nodes to wrap is empty, or node list is not empty
6296 						// and its first member is the nextSibling of nodes to
6297 						// wrap's last member and the first member of node list is
6298 						// an inline node and the last member of nodes to wrap is
6299 						// an inline node other than a br, remove the first member
6300 						// from node list and append it to nodes to wrap."
6301 						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"))) {
6302 							nodesToWrap.push(nodeList.shift());
6303 						}
6304 
6305 						// "Wrap nodes to wrap, with new parent instructions
6306 						// returning the result of calling createElement("li") on
6307 						// the context object. Append the result to sublist."
6308 						sublist.push(wrap(
6309 							nodesToWrap,
6310 							undefined,
6311 							createLi,
6312 							range
6313 						));
6314 					}
6315 				}
6316 
6317 				// "If sublist's first member's parent is an HTML element with
6318 				// local name tag name, or if every member of sublist is an ol or
6319 				// ul, continue this loop from the beginning."
6320 				if (isHtmlElement_obsolete(sublist[0].parentNode, tagName) || $_(sublist).every(isOlUl)) {
6321 					continue;
6322 				}
6323 
6324 				// "If sublist's first member's parent is an HTML element with
6325 				// local name other tag name:"
6326 				if (isHtmlElement_obsolete(sublist[0].parentNode, otherTagName)) {
6327 					// "Record the values of sublist, and let values be the
6328 					// result."
6329 					values = recordValues(sublist);
6330 
6331 					// "Split the parent of sublist."
6332 					splitParent(sublist, range);
6333 
6334 					// "Wrap sublist, with sibling criteria returning true for an
6335 					// HTML element with local name tag name and false otherwise,
6336 					// and new parent instructions returning the result of calling
6337 					// createElement(tag name) on the context object."
6338 					wrap(
6339 						sublist,
6340 						makeIsElementPred(tagName),
6341 						makeCreateElement(tagName),
6342 						range
6343 					);
6344 
6345 					// "Restore the values from values."
6346 					restoreValues(values, range);
6347 
6348 					// "Continue this loop from the beginning."
6349 					continue;
6350 				}
6351 
6352 				// "Wrap sublist, with sibling criteria returning true for an HTML
6353 				// element with local name tag name and false otherwise, and new
6354 				// parent instructions being the following:"
6355 				// . . .
6356 				// "Fix disallowed ancestors of the previous step's result."
6357 				fixDisallowedAncestors(wrap(
6358 					sublist,
6359 					makeIsElementPred(tagName),
6360 					makeCreateElementSublist(tagName, sublist, range),
6361 					range
6362 				), range);
6363 			}
6364 		}
6365 	}
6366 
6367 
6368 	//@}
6369 6370 	///// Justifying the selection /////
6371 	//@{
6372 
6373 	function justifySelection(alignment, range) {
6374 
6375 		// "Block-extend the active range, and let new range be the result."
6376 		var newRange = blockExtend(range);
6377 
6378 		// "Let element list be a list of all editable Elements contained in new
6379 		// range that either has an attribute in the HTML namespace whose local
6380 		// name is "align", or has a style attribute that sets "text-align", or is
6381 		// a center."
6382 		var elementList = getAllContainedNodes(newRange, function (node) {
6383 			return node.nodeType == $_.Node.ELEMENT_NODE && isEditable(node)
6384 			// Ignoring namespaces here
6385 				&& (hasAttribute(node, "align") || node.style.textAlign != "" || isNamedHtmlElement(node, 'center'));
6386 		});
6387 
6388 		// "For each element in element list:"
6389 		var i;
6390 		for (i = 0; i < elementList.length; i++) {
6391 			var element = elementList[i];
6392 
6393 			// "If element has an attribute in the HTML namespace whose local name
6394 			// is "align", remove that attribute."
6395 			element.removeAttribute("align");
6396 
6397 			// "Unset the CSS property "text-align" on element, if it's set by a
6398 			// style attribute."
6399 			element.style.textAlign = "";
6400 			if (element.getAttribute("style") == "") {
6401 				element.removeAttribute("style");
6402 			}
6403 
6404 			// "If element is a div or span or center with no attributes, remove
6405 			// it, preserving its descendants."
6406 			if (isHtmlElementInArray(element, ["div", "span", "center"]) && !element.attributes.length) {
6407 				removePreservingDescendants(element, range);
6408 			}
6409 
6410 			// "If element is a center with one or more attributes, set the tag
6411 			// name of element to "div"."
6412 			if (isNamedHtmlElement(element, 'center') && element.attributes.length) {
6413 				setTagName(element, "div", range);
6414 			}
6415 		}
6416 
6417 		// "Block-extend the active range, and let new range be the result."
6418 		newRange = blockExtend(globalRange);
6419 
6420 		// "Let node list be a list of nodes, initially empty."
6421 		var nodeList = [];
6422 
6423 		// "For each node node contained in new range, append node to node list if
6424 		// the last member of node list (if any) is not an ancestor of node; node
6425 		// is editable; node is an allowed child of "div"; and node's alignment
6426 		// value is not alignment."
6427 		nodeList = getContainedNodes(newRange, function (node) {
6428 			return isEditable(node) && isAllowedChild(node, "div") && getAlignmentValue(node) != alignment;
6429 		});
6430 
6431 		function makeIsAlignedDiv(alignment) {
6432 			return function (node) {
6433 				return isNamedHtmlElement(node, 'div') && $_(node.attributes).every(function (attr) {
6434 					return (attr.name == "align" && attr.value.toLowerCase() == alignment) || (attr.name == "style" && getStyleLength(node) == 1 && node.style.textAlign == alignment);
6435 				});
6436 			};
6437 		}
6438 
6439 		function makeCreateAlignedDiv(alignment) {
6440 			return function () {
6441 				var newParent = document.createElement("div");
6442 				newParent.setAttribute("style", "text-align: " + alignment);
6443 				return newParent;
6444 			};
6445 		}
6446 
6447 		// "While node list is not empty:"
6448 		while (nodeList.length) {
6449 			// "Let sublist be a list of nodes, initially empty."
6450 			var sublist = [];
6451 
6452 			// "Remove the first member of node list and append it to sublist."
6453 			sublist.push(nodeList.shift());
6454 
6455 			// "While node list is not empty, and the first member of node list is
6456 			// the nextSibling of the last member of sublist, remove the first
6457 			// member of node list and append it to sublist."
6458 			while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling) {
6459 				sublist.push(nodeList.shift());
6460 			}
6461 
6462 			// "Wrap sublist. Sibling criteria returns true for any div that has
6463 			// one or both of the following two attributes and no other attributes,
6464 			// and false otherwise:"
6465 			//
6466 			//   * "An align attribute whose value is an ASCII case-insensitive
6467 			//     match for alignment.
6468 			//   * "A style attribute which sets exactly one CSS property
6469 			//     (including unrecognized or invalid attributes), which is
6470 			//     "text-align", which is set to alignment.
6471 			//
6472 			// "New parent instructions are to call createElement("div") on the
6473 			// context object, then set its CSS property "text-align" to alignment
6474 			// and return the result."
6475 			wrap(
6476 				sublist,
6477 				makeIsAlignedDiv(alignment),
6478 				makeCreateAlignedDiv(alignment),
6479 				range
6480 			);
6481 		}
6482 	}
6483 
6484 	//@}
6485 	///// Move the given collapsed range over adjacent zero-width whitespace characters.
6486 	///// The range is
6487 	//@{
6488 	/**
6489 	 * Move the given collapsed range over adjacent zero-width whitespace characters.
6490 	 * If the range is not collapsed or is not contained in a text node, it is not modified
6491 	 * @param range range to modify
6492 	 * @param forward {Boolean} true to move forward, false to move backward
6493 	 */
6494 	function moveOverZWSP(range, forward) {
6495 		var offset;
6496 		if (!range.collapsed) {
6497 			return;
6498 		}
6499 
6500 		offset = range.startOffset;
6501 
6502 		if (forward) {
6503 			// check whether the range starts in a text node
6504 			if (range.startContainer && range.startContainer.nodeType === $_.Node.TEXT_NODE) {
6505 				// move forward (i.e. increase offset) as long as we stay in the text node and have zwsp characters to the right
6506 				while (offset < range.startContainer.data.length && range.startContainer.data.charAt(offset) === '\u200b') {
6507 					offset++;
6508 				}
6509 			}
6510 		} else {
6511 			// check whether the range starts in a text node
6512 			if (range.startContainer && range.startContainer.nodeType === $_.Node.TEXT_NODE) {
6513 				// move backward (i.e. decrease offset) as long as we stay in the text node and have zwsp characters to the left
6514 				while (offset > 0 && range.startContainer.data.charAt(offset - 1) === '\u200b') {
6515 					offset--;
6516 				}
6517 			}
6518 		}
6519 
6520 		// if the offset was changed, set it back to the collapsed range
6521 		if (offset !== range.startOffset) {
6522 			range.setStart(range.startContainer, offset);
6523 			range.setEnd(range.startContainer, offset);
6524 		}
6525 	}
6526 
6527 	/**
6528 	 * implementation of the delete command
6529 	 * will attempt to delete contents within range if non-collapsed
6530 	 * or delete the character left of the cursor position if range
6531 	 * is collapsed. Is used to define the behaviour of the backspace
6532 	 * button.
6533 	 *
6534 	 * @param      value   is just there for compatibility with the commands api. parameter is ignored.
6535 	 * @param      range   the range to execute the delete command for
6536 	 * @return     void
6537 	 */
6538 	commands["delete"] = {
6539 		action: function (value, range) {
6540 			var i;
6541 
6542 			// special behaviour for skipping zero-width whitespaces in IE7
6543 			if (jQuery.browser.msie && jQuery.browser.version <= 7) {
6544 				moveOverZWSP(range, false);
6545 			}
6546 
6547 			// "If the active range is not collapsed, delete the contents of the
6548 			// active range and abort these steps."
6549 			if (!range.collapsed) {
6550 				deleteContents(range);
6551 				return;
6552 			}
6553 
6554 			// "Canonicalize whitespace at (active range's start node, active
6555 			// range's start offset)."
6556 			canonicalizeWhitespace(range.startContainer, range.startOffset);
6557 
6558 			// "Let node and offset be the active range's start node and offset."
6559 			var node = range.startContainer;
6560 			var offset = range.startOffset;
6561 			var isBr = false;
6562 			var isHr = false;
6563 
6564 			// "Repeat the following steps:"
6565 			while (true) {
6566 				// we need to reset isBr and isHr on every interation of the loop
6567 				if (offset > 0) {
6568 					isBr = isNamedHtmlElement(node.childNodes[offset - 1], "br") || false;
6569 					isHr = isNamedHtmlElement(node.childNodes[offset - 1], "hr") || false;
6570 				}
6571 				// "If offset is zero and node's previousSibling is an editable
6572 				// invisible node, remove node's previousSibling from its parent."
6573 				if (offset == 0 && isEditable(node.previousSibling) && isInvisible(node.previousSibling)) {
6574 					node.parentNode.removeChild(node.previousSibling);
6575 					continue;
6576 				}
6577 				// "Otherwise, if node has a child with index offset − 1 and that
6578 				// child is an editable invisible node, remove that child from
6579 				// node, then subtract one from offset."
6580 				if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isEditable(node.childNodes[offset - 1]) && (isInvisible(node.childNodes[offset - 1]) || isBr || isHr)) {
6581 					node.removeChild(node.childNodes[offset - 1]);
6582 					offset--;
6583 					if (isBr || isHr) {
6584 						range.setStart(node, offset);
6585 						range.setEnd(node, offset);
6586 						return;
6587 					}
6588 					continue;
6589 
6590 				}
6591 				// "Otherwise, if offset is zero and node is an inline node, or if
6592 				// node is an invisible node, set offset to the index of node, then
6593 				// set node to its parent."
6594 				if ((offset == 0 && isInlineNode(node)) || isInvisible(node)) {
6595 					offset = getNodeIndex(node);
6596 					node = node.parentNode;
6597 					continue;
6598 				}
6599 				// "Otherwise, if node has a child with index offset − 1 and that
6600 				// child is an editable a, remove that child from node, preserving
6601 				// its descendants. Then abort these steps."
6602 				if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isEditable(node.childNodes[offset - 1]) && isNamedHtmlElement(node.childNodes[offset - 1], "a")) {
6603 					removePreservingDescendants(node.childNodes[offset - 1], range);
6604 					return;
6605 
6606 				}
6607 				// "Otherwise, if node has a child with index offset − 1 and that
6608 				// child is not a block node or a br or an img, set node to that
6609 				// child, then set offset to the length of node."
6610 				if (0 <= offset - 1 && offset - 1 < node.childNodes.length && !isBlockNode(node.childNodes[offset - 1]) && !isHtmlElementInArray(node.childNodes[offset - 1], ["br", "img"])) {
6611 					node = node.childNodes[offset - 1];
6612 					offset = getNodeLength(node);
6613 					continue;
6614 				}
6615 				// "Otherwise, break from this loop."
6616 				// brk is a quick and dirty jslint workaround since I don't want to rewrite this loop
6617 				var brk = true;
6618 				if (brk) {
6619 					break;
6620 				}
6621 			}
6622 
6623 			// if the previous node is an aloha-table we want to delete it
6624 			var delBlock = getBlockAtPreviousPosition(node, offset);
6625 			if (delBlock) {
6626 				delBlock.parentNode.removeChild(delBlock);
6627 				return;
6628 			}
6629 
6630 			// "If node is a Text node and offset is not zero, call collapse(node,
6631 			// offset) on the Selection. Then delete the contents of the range with
6632 			// start (node, offset − 1) and end (node, offset) and abort these
6633 			// steps."
6634 			if (node.nodeType == $_.Node.TEXT_NODE && offset != 0) {
6635 				range.setStart(node, offset - 1);
6636 				range.setEnd(node, offset - 1);
6637 				deleteContents(node, offset - 1, node, offset);
6638 				return;
6639 			}
6640 
6641 			// @iebug
6642 			// when inserting a special char via the plugin
6643 			// there where problems deleting them again with backspace after insertation
6644 			// see https://github.com/alohaeditor/Aloha-Editor/issues/517
6645 			if (node.nodeType == $_.Node.TEXT_NODE && offset == 0 && jQuery.browser.msie) {
6646 				offset = 1;
6647 				range.setStart(node, offset);
6648 				range.setEnd(node, offset);
6649 				range.startOffset = 0;
6650 				deleteContents(range);
6651 				return;
6652 			}
6653 
6654 			// "If node is an inline node, abort these steps."
6655 			if (isInlineNode(node)) {
6656 				return;
6657 			}
6658 
6659 			// "If node has a child with index offset − 1 and that child is a br or
6660 			// hr or img, call collapse(node, offset) on the Selection. Then delete
6661 			// the contents of the range with start (node, offset − 1) and end
6662 			// (node, offset) and abort these steps."
6663 			if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isHtmlElementInArray(node.childNodes[offset - 1], ["br", "hr", "img"])) {
6664 				range.setStart(node, offset);
6665 				range.setEnd(node, offset);
6666 				deleteContents(range);
6667 				return;
6668 			}
6669 
6670 			// "If node is an li or dt or dd and is the first child of its parent,
6671 			// and offset is zero:"
6672 			if (isHtmlElementInArray(node, ["li", "dt", "dd"]) && node == node.parentNode.firstChild && offset == 0) {
6673 				// "Let items be a list of all lis that are ancestors of node."
6674 				//
6675 				// Remember, must be in tree order.
6676 				var items = [];
6677 				var ancestor;
6678 				for (ancestor = node.parentNode; ancestor; ancestor = ancestor.parentNode) {
6679 					if (isNamedHtmlElement(ancestor, 'li')) {
6680 						items.unshift(ancestor);
6681 					}
6682 				}
6683 
6684 				// "Normalize sublists of each item in items."
6685 				for (i = 0; i < items.length; i++) {
6686 					normalizeSublists(items[i], range);
6687 				}
6688 
6689 				// "Record the values of the one-node list consisting of node, and
6690 				// let values be the result."
6691 				var values = recordValues([node]);
6692 
6693 				// "Split the parent of the one-node list consisting of node."
6694 				splitParent([node], range);
6695 
6696 				// "Restore the values from values."
6697 				restoreValues(values, range);
6698 
6699 				// "If node is a dd or dt, and it is not an allowed child of any of
6700 				// its ancestors in the same editing host, set the tag name of node
6701 				// to the default single-line container name and let node be the
6702 				// result."
6703 				if (isHtmlElementInArray(node, ["dd", "dt"]) && $_(getAncestors(node)).every(function (ancestor) { return !inSameEditingHost(node, ancestor) || !isAllowedChild(node, ancestor); })) {
6704 					node = setTagName(node, defaultSingleLineContainerName, range);
6705 				}
6706 
6707 				// "Fix disallowed ancestors of node."
6708 				fixDisallowedAncestors(node, range);
6709 
6710 				// fix the lists to be html5 conformant
6711 				for (i = 0; i < items.length; i++) {
6712 					unNormalizeSublists(items[i].parentNode, range);
6713 				}
6714 
6715 				// "Abort these steps."
6716 				return;
6717 			}
6718 
6719 			// "Let start node equal node and let start offset equal offset."
6720 			var startNode = node;
6721 			var startOffset = offset;
6722 
6723 			// "Repeat the following steps:"
6724 			while (true) {
6725 				// "If start offset is zero, set start offset to the index of start
6726 				// node and then set start node to its parent."
6727 				if (startOffset == 0) {
6728 					startOffset = getNodeIndex(startNode);
6729 					startNode = startNode.parentNode;
6730 
6731 					// "Otherwise, if start node has an editable invisible child with
6732 					// index start offset minus one, remove it from start node and
6733 					// subtract one from start offset."
6734 				} else if (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length && isEditable(startNode.childNodes[startOffset - 1]) && isInvisible(startNode.childNodes[startOffset - 1])) {
6735 					startNode.removeChild(startNode.childNodes[startOffset - 1]);
6736 6737 					startOffset--;
6738 
6739 					// "Otherwise, break from this loop."
6740 				} else {
6741 					break;
6742 				}
6743 			}
6744 
6745 			// "If offset is zero, and node has an editable ancestor container in
6746 			// the same editing host that's an indentation element:"
6747 			if (offset == 0 && $_(getAncestors(node).concat(node)).filter(function (ancestor) { return isEditable(ancestor) && inSameEditingHost(ancestor, node) && isIndentationElement(ancestor); }).length) {
6748 				// "Block-extend the range whose start and end are both (node, 0),
6749 				// and let new range be the result."
6750 				var newRange = Aloha.createRange();
6751 				newRange.setStart(node, 0);
6752 				newRange.setEnd(node, 0);
6753 				newRange = blockExtend(newRange);
6754 
6755 				// "Let node list be a list of nodes, initially empty."
6756 				//
6757 				// "For each node current node contained in new range, append
6758 				// current node to node list if the last member of node list (if
6759 				// any) is not an ancestor of current node, and current node is
6760 				// editable but has no editable descendants."
6761 				var nodeList = getContainedNodes(newRange, function (currentNode) {
6762 					return isEditable(currentNode) && !hasEditableDescendants(currentNode);
6763 				});
6764 
6765 				// "Outdent each node in node list."
6766 				for (i = 0; i < nodeList.length; i++) {
6767 					outdentNode(nodeList[i], range);
6768 				}
6769 
6770 				// "Abort these steps."
6771 				return;
6772 			}
6773 
6774 			// "If the child of start node with index start offset is a table,
6775 			// abort these steps."
6776 			if (isNamedHtmlElement(startNode.childNodes[startOffset], "table")) {
6777 				return;
6778 			}
6779 
6780 			// "If start node has a child with index start offset − 1, and that
6781 			// child is a table:"
6782 			if (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length && isNamedHtmlElement(startNode.childNodes[startOffset - 1], "table")) {
6783 				// "Call collapse(start node, start offset − 1) on the context
6784 				// object's Selection."
6785 				range.setStart(startNode, startOffset - 1);
6786 
6787 				// "Call extend(start node, start offset) on the context object's
6788 				// Selection."
6789 				range.setEnd(startNode, startOffset);
6790 
6791 				// "Abort these steps."
6792 				return;
6793 			}
6794 
6795 			// "If offset is zero; and either the child of start node with index
6796 			// start offset minus one is an hr, or the child is a br whose
6797 			// previousSibling is either a br or not an inline node:"
6798 			if (offset == 0
6799 				    && (isNamedHtmlElement(startNode.childNodes[startOffset - 1], "hr")
6800 						|| (isNamedHtmlElement(startNode.childNodes[startOffset - 1], "br")
6801 							&& (isNamedHtmlElement(startNode.childNodes[startOffset - 1].previousSibling, "br")
6802 								|| !isInlineNode(startNode.childNodes[startOffset - 1].previousSibling))))) {
6803 				// "Call collapse(node, offset) on the Selection."
6804 				range.setStart(node, offset);
6805 				range.setEnd(node, offset);
6806 
6807 				// "Delete the contents of the range with start (start node, start
6808 				// offset − 1) and end (start node, start offset)."
6809 				deleteContents(startNode, startOffset - 1, startNode, startOffset);
6810 
6811 				// "Abort these steps."
6812 				return;
6813 			}
6814 
6815 			// "If the child of start node with index start offset is an li or dt
6816 			// or dd, and that child's firstChild is an inline node, and start
6817 			// offset is not zero:"
6818 			if (isHtmlElementInArray(startNode.childNodes[startOffset], ["li", "dt", "dd"]) && isInlineNode(startNode.childNodes[startOffset].firstChild) && startOffset != 0) {
6819 				// "Let previous item be the child of start node with index start
6820 				// offset minus one."
6821 				var previousItem = startNode.childNodes[startOffset - 1];
6822 
6823 				// "If previous item's lastChild is an inline node other than a br,
6824 				// call createElement("br") on the context object and append the
6825 				// result as the last child of previous item."
6826 				if (isInlineNode(previousItem.lastChild) && !isNamedHtmlElement(previousItem.lastChild, "br")) {
6827 					previousItem.appendChild(document.createElement("br"));
6828 				}
6829 
6830 				// "If previous item's lastChild is an inline node, call
6831 				// createElement("br") on the context object and append the result
6832 				// as the last child of previous item."
6833 				if (isInlineNode(previousItem.lastChild)) {
6834 					previousItem.appendChild(document.createElement("br"));
6835 				}
6836 			}
6837 
6838 			// "If the child of start node with index start offset is an li or dt
6839 			// or dd, and its previousSibling is also an li or dt or dd, set start
6840 			// node to its child with index start offset − 1, then set start offset
6841 			// to start node's length, then set node to start node's nextSibling,
6842 			// then set offset to 0."
6843 			if (isHtmlElementInArray(startNode.childNodes[startOffset], ["li", "dt", "dd"]) && isHtmlElementInArray(startNode.childNodes[startOffset - 1], ["li", "dt", "dd"])) {
6844 				startNode = startNode.childNodes[startOffset - 1];
6845 				startOffset = getNodeLength(startNode);
6846 				node = startNode.nextSibling;
6847 				offset = 0;
6848 
6849 				// "Otherwise, while start node has a child with index start offset
6850 				// minus one:"
6851 			} else {
6852 				while (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length) {
6853 					// "If start node's child with index start offset minus one is
6854 					// editable and invisible, remove it from start node, then
6855 					// subtract one from start offset."
6856 					if (isEditable(startNode.childNodes[startOffset - 1]) && isInvisible(startNode.childNodes[startOffset - 1])) {
6857 						startNode.removeChild(startNode.childNodes[startOffset - 1]);
6858 						startOffset--;
6859 
6860 						// "Otherwise, set start node to its child with index start
6861 						// offset minus one, then set start offset to the length of
6862 						// start node."
6863 					} else {
6864 						startNode = startNode.childNodes[startOffset - 1];
6865 						startOffset = getNodeLength(startNode);
6866 					}
6867 				}
6868 			}
6869 
6870 6871 			// "Delete the contents of the range with start (start node, start
6872 			// offset) and end (node, offset)."
6873 			var delRange = Aloha.createRange();
6874 			delRange.setStart(startNode, startOffset);
6875 			delRange.setEnd(node, offset);
6876 			deleteContents(delRange);
6877 
6878 			if (!isAncestorContainer(document.body, range.startContainer)) {
6879 				if (delRange.startContainer.hasChildNodes()
6880 						|| delRange.startContainer.nodeType == $_.Node.TEXT_NODE
6881 							|| isEditingHost(delRange.startContainer)) {
6882 					range.setStart(delRange.startContainer, delRange.startOffset);
6883 					range.setEnd(delRange.startContainer, delRange.startOffset);
6884 				} else {
6885 					range.setStart(delRange.startContainer.parentNode, getNodeIndex(delRange.startContainer));
6886 					range.setEnd(delRange.startContainer.parentNode, getNodeIndex(delRange.startContainer));
6887 				}
6888 			}
6889 		}
6890 	};
6891 
6892 	//@}
6893 	///// The formatBlock command /////
6894 	//@{
6895 	// "A formattable block name is "address", "dd", "div", "dt", "h1", "h2", "h3",
6896 	// "h4", "h5", "h6", "p", or "pre"."
6897 	var formattableBlockNames = ["address", "dd", "div", "dt", "h1", "h2", "h3", "h4", "h5", "h6", "p", "pre"];
6898 
6899 	commands.formatblock = {
6900 		action: function (value) {
6901 			var i;
6902 
6903 			// "If value begins with a "<" character and ends with a ">" character,
6904 			// remove the first and last characters from it."
6905 			if (/^<.*>$/.test(value)) {
6906 				value = value.slice(1, -1);
6907 			}
6908 
6909 			// "Let value be converted to ASCII lowercase."
6910 			value = value.toLowerCase();
6911 
6912 			// "If value is not a formattable block name, abort these steps and do
6913 			// nothing."
6914 			if ($_(formattableBlockNames).indexOf(value) == -1) {
6915 				return;
6916 			}
6917 
6918 			// "Block-extend the active range, and let new range be the result."
6919 			var newRange = blockExtend(getActiveRange());
6920 
6921 			// "Let node list be an empty list of nodes."
6922 			//
6923 			// "For each node node contained in new range, append node to node list
6924 			// if it is editable, the last member of original node list (if any) is
6925 			// not an ancestor of node, node is either a non-list single-line
6926 			// container or an allowed child of "p" or a dd or dt, and node is not
6927 			// the ancestor of a prohibited paragraph child."
6928 			var nodeList = getContainedNodes(newRange, function (node) {
6929 				return isEditable(node) && (isNonListSingleLineContainer(node) || isAllowedChild(node, "p") || isHtmlElementInArray(node, ["dd", "dt"])) && !$_(getDescendants(node)).some(isProhibitedParagraphChild);
6930 			});
6931 
6932 			// "Record the values of node list, and let values be the result."
6933 			var values = recordValues(nodeList);
6934 
6935 			function makeIsEditableElementInSameEditingHostDoesNotContainProhibitedParagraphChildren(node) {
6936 				return function (ancestor) {
6937 					return (isEditable(ancestor)
6938 							&& inSameEditingHost(ancestor, node)
6939 							&& isHtmlElement_obsolete(ancestor, formattableBlockNames)
6940 							&& !$_(getDescendants(ancestor)).some(isProhibitedParagraphChild));
6941 				};
6942 			}
6943 
6944 			function makeIsElementWithoutAttributes(value) {
6945 				return function (node) {
6946 					return isHtmlElement_obsolete(node, value) && !node.attributes.length;
6947 				};
6948 			}
6949 
6950 			function returnFalse() {
6951 				return false;
6952 			}
6953 
6954 			function makeCreateElement(value) {
6955 				return function () {
6956 					return document.createElement(value);
6957 				};
6958 			}
6959 
6960 			// "For each node in node list, while node is the descendant of an
6961 			// editable HTML element in the same editing host, whose local name is
6962 			// a formattable block name, and which is not the ancestor of a
6963 			// prohibited paragraph child, split the parent of the one-node list
6964 			// consisting of node."
6965 			for (i = 0; i < nodeList.length; i++) {
6966 				var node = nodeList[i];
6967 				while ($_(getAncestors(node)).some(makeIsEditableElementInSameEditingHostDoesNotContainProhibitedParagraphChildren(node))) {
6968 					splitParent([node], newRange);
6969 				}
6970 			}
6971 
6972 			// "Restore the values from values."
6973 			restoreValues(values, newRange);
6974 
6975 			// "While node list is not empty:"
6976 			while (nodeList.length) {
6977 				var sublist;
6978 
6979 				// "If the first member of node list is a single-line
6980 				// container:"
6981 				if (isSingleLineContainer(nodeList[0])) {
6982 					// "Let sublist be the children of the first member of node
6983 					// list."
6984 					sublist = [].slice.call(toArray(nodeList[0].childNodes));
6985 
6986 					// "Record the values of sublist, and let values be the
6987 					// result."
6988 					values = recordValues(sublist);
6989 
6990 					// "Remove the first member of node list from its parent,
6991 					// preserving its descendants."
6992 					removePreservingDescendants(nodeList[0], newRange);
6993 
6994 					// "Restore the values from values."
6995 					restoreValues(values, newRange);
6996 
6997 					// "Remove the first member from node list."
6998 					nodeList.shift();
6999 
7000 					// "Otherwise:"
7001 				} else {
7002 					// "Let sublist be an empty list of nodes."
7003 					sublist = [];
7004 
7005 					// "Remove the first member of node list and append it to
7006 					// sublist."
7007 					sublist.push(nodeList.shift());
7008 
7009 					// "While node list is not empty, and the first member of
7010 					// node list is the nextSibling of the last member of
7011 					// sublist, and the first member of node list is not a
7012 					// single-line container, and the last member of sublist is
7013 					// not a br, remove the first member of node list and
7014 					// append it to sublist."
7015 					while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isSingleLineContainer(nodeList[0]) && !isNamedHtmlElement(sublist[sublist.length - 1], "BR")) {
7016 						sublist.push(nodeList.shift());
7017 					}
7018 				}
7019 
7020 				// "Wrap sublist. If value is "div" or "p", sibling criteria
7021 				// returns false; otherwise it returns true for an HTML element
7022 				// with local name value and no attributes, and false otherwise.
7023 				// New parent instructions return the result of running
7024 				// createElement(value) on the context object. Then fix disallowed
7025 				// ancestors of the result."
7026 				fixDisallowedAncestors(wrap(
7027 					sublist,
7028 					jQuery.inArray(value, ["div", "p"]) == -1 ? makeIsElementWithoutAttributes(value) : returnFalse,
7029 					makeCreateElement(value),
7030 					newRange
7031 				), newRange);
7032 			}
7033 		},
7034 		indeterm: function () {
7035 			// "Block-extend the active range, and let new range be the result."
7036 			var newRange = blockExtend(getActiveRange());
7037 
7038 			// "Let node list be all visible editable nodes that are contained in
7039 			// new range and have no children."
7040 			var nodeList = getAllContainedNodes(newRange, function (node) {
7041 				return isVisible(node) && isEditable(node) && !node.hasChildNodes();
7042 			});
7043 
7044 			// "If node list is empty, return false."
7045 			if (!nodeList.length) {
7046 				return false;
7047 			}
7048 
7049 			// "Let type be null."
7050 			var type = null;
7051 
7052 			// "For each node in node list:"
7053 			var i;
7054 			for (i = 0; i < nodeList.length; i++) {
7055 				var node = nodeList[i];
7056 
7057 				// "While node's parent is editable and in the same editing host as
7058 				// node, and node is not an HTML element whose local name is a
7059 				// formattable block name, set node to its parent."
7060 				while (isEditable(node.parentNode) && inSameEditingHost(node, node.parentNode) && !isHtmlElement_obsolete(node, formattableBlockNames)) {
7061 					node = node.parentNode;
7062 				}
7063 
7064 7065 				// "Let current type be the empty string."
7066 				var currentType = "";
7067 
7068 				// "If node is an editable HTML element whose local name is a
7069 				// formattable block name, and node is not the ancestor of a
7070 				// prohibited paragraph child, set current type to node's local
7071 				// name."
7072 				if (isEditable(node) && isHtmlElement_obsolete(node, formattableBlockNames) && !$_(getDescendants(node)).some(isProhibitedParagraphChild)) {
7073 					currentType = node.tagName;
7074 				}
7075 
7076 				// "If type is null, set type to current type."
7077 				if (type === null) {
7078 					type = currentType;
7079 
7080 					// "Otherwise, if type does not equal current type, return true."
7081 				} else if (type != currentType) {
7082 					return true;
7083 				}
7084 			}
7085 
7086 			// "Return false."
7087 			return false;
7088 		},
7089 		value: function () {
7090 			// "Block-extend the active range, and let new range be the result."
7091 			var newRange = blockExtend(getActiveRange());
7092 
7093 			// "Let node be the first visible editable node that is contained in
7094 			// new range and has no children. If there is no such node, return the
7095 			// empty string."
7096 			var nodes = getAllContainedNodes(newRange, function (node) {
7097 				return isVisible(node) && isEditable(node) && !node.hasChildNodes();
7098 			});
7099 7100 			if (!nodes.length) {
7101 				return "";
7102 			}
7103 			var node = nodes[0];
7104 
7105 			// "While node's parent is editable and in the same editing host as
7106 			// node, and node is not an HTML element whose local name is a
7107 			// formattable block name, set node to its parent."
7108 			while (isEditable(node.parentNode) && inSameEditingHost(node, node.parentNode) && !isHtmlElement_obsolete(node, formattableBlockNames)) {
7109 				node = node.parentNode;
7110 			}
7111 
7112 			// "If node is an editable HTML element whose local name is a
7113 			// formattable block name, and node is not the ancestor of a prohibited
7114 			// paragraph child, return node's local name, converted to ASCII
7115 			// lowercase."
7116 			if (isEditable(node) && isHtmlElement_obsolete(node, formattableBlockNames) && !$_(getDescendants(node)).some(isProhibitedParagraphChild)) {
7117 				return node.tagName.toLowerCase();
7118 			}
7119 
7120 			// "Return the empty string."
7121 			return "";
7122 		}
7123 	};
7124 
7125 	//@}
7126 	///// The forwardDelete command /////
7127 	//@{
7128 	commands.forwarddelete = {
7129 		action: function (value, range) {
7130 			// special behaviour for skipping zero-width whitespaces in IE7
7131 			if (jQuery.browser.msie && jQuery.browser.version <= 7) {
7132 				moveOverZWSP(range, true);
7133 			}
7134 
7135 			// "If the active range is not collapsed, delete the contents of the
7136 			// active range and abort these steps."
7137 			if (!range.collapsed) {
7138 				deleteContents(range);
7139 				return;
7140 			}
7141 
7142 			// "Canonicalize whitespace at (active range's start node, active
7143 			// range's start offset)."
7144 			canonicalizeWhitespace(range.startContainer, range.startOffset);
7145 
7146 			// "Let node and offset be the active range's start node and offset."
7147 			var node = range.startContainer;
7148 			var offset = range.startOffset;
7149 			var isBr = false;
7150 			var isHr = false;
7151 
7152 			// "Repeat the following steps:"
7153 			while (true) {
7154 				// check whether the next element is a br or hr
7155 				// Commented out for unknown reason.
7156 				//if (offset < node.childNodes.length) {
7157 				//				isBr = isHtmlElement_obsolete(node.childNodes[offset], "br") || false;
7158 				//				isHr = isHtmlElement_obsolete(node.childNodes[offset], "hr") || false;
7159 				//}
7160 
7161 				// "If offset is the length of node and node's nextSibling is an
7162 				// editable invisible node, remove node's nextSibling from its
7163 				// parent."
7164 				if (offset == getNodeLength(node) && isEditable(node.nextSibling) && isInvisible(node.nextSibling)) {
7165 					node.parentNode.removeChild(node.nextSibling);
7166 
7167 					// "Otherwise, if node has a child with index offset and that child
7168 					// is an editable invisible node, remove that child from node."
7169 				} else if (offset < node.childNodes.length && isEditable(node.childNodes[offset]) && (isInvisible(node.childNodes[offset]) || isBr || isHr)) {
7170 					node.removeChild(node.childNodes[offset]);
7171 					if (isBr || isHr) {
7172 						ensureContainerEditable(node);
7173 						range.setStart(node, offset);
7174 						range.setEnd(node, offset);
7175 						return;
7176 					}
7177 
7178 					// "Otherwise, if node has a child with index offset and that child
7179 					// is a collapsed block prop, add one to offset."
7180 				} else if (offset < node.childNodes.length && isCollapsedBlockProp(node.childNodes[offset])) {
7181 					offset++;
7182 
7183 					// "Otherwise, if offset is the length of node and node is an
7184 					// inline node, or if node is invisible, set offset to one plus the
7185 					// index of node, then set node to its parent."
7186 				} else if ((offset == getNodeLength(node) && isInlineNode(node)) || isInvisible(node)) {
7187 					offset = 1 + getNodeIndex(node);
7188 					node = node.parentNode;
7189 
7190 					// "Otherwise, if node has a child with index offset and that child
7191 					// is not a block node or a br or an img, set node to that child,
7192 					// then set offset to zero."
7193 				} else if (offset < node.childNodes.length && !isBlockNode(node.childNodes[offset]) && !isHtmlElementInArray(node.childNodes[offset], ["br", "img"])) {
7194 					node = node.childNodes[offset];
7195 					offset = 0;
7196 
7197 					// "Otherwise, break from this loop."
7198 				} else {
7199 					break;
7200 				}
7201 			}
7202 
7203 			// collapse whitespace in the node, if it is a text node
7204 			canonicalizeWhitespace(range.startContainer, range.startOffset);
7205 
7206 			// if the next node is an aloha-table we want to delete it
7207 			var delBlock = getBlockAtNextPosition(node, offset);
7208 			if (delBlock) {
7209 				delBlock.parentNode.removeChild(delBlock);
7210 				return;
7211 			}
7212 
7213 			var endOffset;
7214 			// "If node is a Text node and offset is not node's length:"
7215 			if (node.nodeType == $_.Node.TEXT_NODE && offset != getNodeLength(node)) {
7216 				// "Call collapse(node, offset) on the Selection."
7217 				range.setStart(node, offset);
7218 				range.setEnd(node, offset);
7219 
7220 				// "Let end offset be offset plus one."
7221 				endOffset = offset + 1;
7222 
7223 				// "While end offset is not node's length and the end offsetth
7224 				// element of node's data has general category M when interpreted
7225 				// as a Unicode code point, add one to end offset."
7226 				//
7227 				// TODO: Not even going to try handling anything beyond the most
7228 				// basic combining marks, since I couldn't find a good list.  I
7229 				// special-case a few Hebrew diacritics too to test basic coverage
7230 				// of non-Latin stuff.
7231 				while (endOffset != node.length && /^[\u0300-\u036f\u0591-\u05bd\u05c1\u05c2]$/.test(node.data[endOffset])) {
7232 					endOffset++;
7233 7234 				}
7235 
7236 				// "Delete the contents of the range with start (node, offset) and
7237 				// end (node, end offset)."
7238 				deleteContents(node, offset, node, endOffset);
7239 
7240 				// "Abort these steps."
7241 				return;
7242 			}
7243 
7244 			// "If node is an inline node, abort these steps."
7245 			if (isInlineNode(node)) {
7246 				return;
7247 			}
7248 
7249 			// "If node has a child with index offset and that child is a br or hr
7250 			// or img, call collapse(node, offset) on the Selection. Then delete
7251 			// the contents of the range with start (node, offset) and end (node,
7252 			// offset + 1) and abort these steps."
7253 			if (offset < node.childNodes.length && isHtmlElementInArray(node.childNodes[offset], ["br", "hr", "img"])) {
7254 				range.setStart(node, offset);
7255 				range.setEnd(node, offset);
7256 				deleteContents(node, offset, node, offset + 1);
7257 				return;
7258 			}
7259 
7260 			// "Let end node equal node and let end offset equal offset."
7261 7262 			var endNode = node;
7263 			endOffset = offset;
7264 
7265 			// "Repeat the following steps:"
7266 			while (true) {
7267 				// "If end offset is the length of end node, set end offset to one
7268 				// plus the index of end node and then set end node to its parent."
7269 				if (endOffset == getNodeLength(endNode)) {
7270 					endOffset = 1 + getNodeIndex(endNode);
7271 					endNode = endNode.parentNode;
7272 
7273 					// "Otherwise, if end node has a an editable invisible child with
7274 					// index end offset, remove it from end node."
7275 				} else if (endOffset < endNode.childNodes.length && isEditable(endNode.childNodes[endOffset]) && isInvisible(endNode.childNodes[endOffset])) {
7276 					endNode.removeChild(endNode.childNodes[endOffset]);
7277 
7278 					// "Otherwise, break from this loop."
7279 				} else {
7280 					break;
7281 				}
7282 			}
7283 
7284 			// "If the child of end node with index end offset minus one is a
7285 			// table, abort these steps."
7286 			if (isNamedHtmlElement(endNode.childNodes[endOffset - 1], "table")) {
7287 				return;
7288 			}
7289 
7290 			// "If the child of end node with index end offset is a table:"
7291 			if (isNamedHtmlElement(endNode.childNodes[endOffset], "table")) {
7292 				// "Call collapse(end node, end offset) on the context object's
7293 				// Selection."
7294 				range.setStart(endNode, endOffset);
7295 
7296 				// "Call extend(end node, end offset + 1) on the context object's
7297 				// Selection."
7298 				range.setEnd(endNode, endOffset + 1);
7299 
7300 				// "Abort these steps."
7301 				return;
7302 			}
7303 
7304 			// "If offset is the length of node, and the child of end node with
7305 			// index end offset is an hr or br:"
7306 			if (offset == getNodeLength(node) && isHtmlElementInArray(endNode.childNodes[endOffset], ["br", "hr"])) {
7307 				// "Call collapse(node, offset) on the Selection."
7308 				range.setStart(node, offset);
7309 				range.setEnd(node, offset);
7310 
7311 				// "Delete the contents of the range with end (end node, end
7312 				// offset) and end (end node, end offset + 1)."
7313 				deleteContents(endNode, endOffset, endNode, endOffset + 1);
7314 
7315 				// "Abort these steps."
7316 				return;
7317 			}
7318 
7319 			// "While end node has a child with index end offset:"
7320 			while (endOffset < endNode.childNodes.length) {
7321 				// "If end node's child with index end offset is editable and
7322 				// invisible, remove it from end node."
7323 				if (isEditable(endNode.childNodes[endOffset]) && isInvisible(endNode.childNodes[endOffset])) {
7324 					endNode.removeChild(endNode.childNodes[endOffset]);
7325 
7326 					// "Otherwise, set end node to its child with index end offset and
7327 					// set end offset to zero."
7328 				} else {
7329 					endNode = endNode.childNodes[endOffset];
7330 					endOffset = 0;
7331 				}
7332 			}
7333 
7334 			// "Delete the contents of the range with start (node, offset) and end
7335 			// (end node, end offset)."
7336 			var newRange = deleteContents(node, offset, endNode, endOffset);
7337 			range.setStart(newRange.startContainer, newRange.startOffset);
7338 			range.setEnd(newRange.endContainer, newRange.endOffset);
7339 		}
7340 	};
7341 
7342 	//@}
7343 	///// The indent command /////
7344 	//@{
7345 	commands.indent = {
7346 		action: function () {
7347 			// "Let items be a list of all lis that are ancestor containers of the
7348 			// active range's start and/or end node."
7349 			//
7350 			// Has to be in tree order, remember!
7351 			var items = [];
7352 			var node;
7353 			for (node = getActiveRange().endContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) {
7354 				if (isNamedHtmlElement(node, "LI")) {
7355 					items.unshift(node);
7356 				}
7357 			}
7358 			for (node = getActiveRange().startContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) {
7359 				if (isNamedHtmlElement(node, "LI")) {
7360 					items.unshift(node);
7361 				}
7362 			}
7363 			for (node = getActiveRange().commonAncestorContainer; node; node = node.parentNode) {
7364 				if (isNamedHtmlElement(node, "LI")) {
7365 					items.unshift(node);
7366 				}
7367 			}
7368 
7369 			// "For each item in items, normalize sublists of item."
7370 			var i;
7371 			for (i = 0; i < items.length; i++) {
7372 				normalizeSublists(items[i], getActiveRange());
7373 			}
7374 
7375 			// "Block-extend the active range, and let new range be the result."
7376 			var newRange = blockExtend(getActiveRange());
7377 
7378 			// "Let node list be a list of nodes, initially empty."
7379 			var nodeList = [];
7380 
7381 			// "For each node node contained in new range, if node is editable and
7382 			// is an allowed child of "div" or "ol" and if the last member of node
7383 			// list (if any) is not an ancestor of node, append node to node list."
7384 			nodeList = getContainedNodes(newRange, function (node) {
7385 				return isEditable(node) && (isAllowedChild(node, "div") || isAllowedChild(node, "ol"));
7386 			});
7387 
7388 			// "If the first member of node list is an li whose parent is an ol or
7389 			// ul, and its previousSibling is an li as well, normalize sublists of
7390 			// its previousSibling."
7391 			if (nodeList.length && isNamedHtmlElement(nodeList[0], "LI") && isHtmlElementInArray(nodeList[0].parentNode, ["OL", "UL"]) && isNamedHtmlElement(nodeList[0].previousSibling, "LI")) {
7392 				normalizeSublists(nodeList[0].previousSibling, newRange);
7393 			}
7394 
7395 			// "While node list is not empty:"
7396 			while (nodeList.length) {
7397 				// "Let sublist be a list of nodes, initially empty."
7398 				var sublist = [];
7399 
7400 				// "Remove the first member of node list and append it to sublist."
7401 				sublist.push(nodeList.shift());
7402 
7403 				// "While the first member of node list is the nextSibling of the
7404 				// last member of sublist, remove the first member of node list and
7405 				// append it to sublist."
7406 				while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling) {
7407 					sublist.push(nodeList.shift());
7408 				}
7409 
7410 				// "Indent sublist."
7411 				indentNodes(sublist, newRange);
7412 			}
7413 		}
7414 	};
7415 
7416 	//@}
7417 	///// The insertHorizontalRule command /////
7418 	//@{
7419 	commands.inserthorizontalrule = {
7420 		action: function (value, range) {
7421 
7422 			// "While range's start offset is 0 and its start node's parent is not
7423 			// null, set range's start to (parent of start node, index of start
7424 			// node)."
7425 			while (range.startOffset == 0 && range.startContainer.parentNode) {
7426 				range.setStart(range.startContainer.parentNode, getNodeIndex(range.startContainer));
7427 			}
7428 
7429 			// "While range's end offset is the length of its end node, and its end
7430 			// node's parent is not null, set range's end to (parent of end node, 1
7431 			// + index of start node)."
7432 			while (range.endOffset == getNodeLength(range.endContainer) && range.endContainer.parentNode) {
7433 				range.setEnd(range.endContainer.parentNode, 1 + getNodeIndex(range.endContainer));
7434 			}
7435 
7436 			// "Delete the contents of range, with block merging false."
7437 			deleteContents(range, {
7438 				blockMerging: false
7439 			});
7440 
7441 			// "If the active range's start node is neither editable nor an editing
7442 			// host, abort these steps."
7443 			if (!isEditable(getActiveRange().startContainer) && !isEditingHost(getActiveRange().startContainer)) {
7444 				return;
7445 			}
7446 
7447 			// "If the active range's start node is a Text node and its start
7448 			// offset is zero, set the active range's start and end to (parent of
7449 			// start node, index of start node)."
7450 			if (getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset == 0) {
7451 				getActiveRange().setStart(getActiveRange().startContainer.parentNode, getNodeIndex(getActiveRange().startContainer));
7452 				getActiveRange().collapse(true);
7453 			}
7454 
7455 			// "If the active range's start node is a Text node and its start
7456 			// offset is the length of its start node, set the active range's start
7457 			// and end to (parent of start node, 1 + index of start node)."
7458 			if (getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset == getNodeLength(getActiveRange().startContainer)) {
7459 				getActiveRange().setStart(getActiveRange().startContainer.parentNode, 1 + getNodeIndex(getActiveRange().startContainer));
7460 				getActiveRange().collapse(true);
7461 			}
7462 
7463 			// "Let hr be the result of calling createElement("hr") on the
7464 			// context object."
7465 			var hr = document.createElement("hr");
7466 
7467 			// "Run insertNode(hr) on the range."
7468 			range.insertNode(hr);
7469 
7470 			// "Fix disallowed ancestors of hr."
7471 			fixDisallowedAncestors(hr, range);
7472 
7473 			// "Run collapse() on the Selection, with first argument equal to the
7474 			// parent of hr and the second argument equal to one plus the index of
7475 			// hr."
7476 			//
7477 			// Not everyone actually supports collapse(), so we do it manually
7478 7479 			// instead.  Also, we need to modify the actual range we're given as
7480 			// well, for the sake of autoimplementation.html's range-filling-in.
7481 			range.setStart(hr.parentNode, 1 + getNodeIndex(hr));
7482 			range.setEnd(hr.parentNode, 1 + getNodeIndex(hr));
7483 			Aloha.getSelection().removeAllRanges();
7484 			Aloha.getSelection().addRange(range);
7485 		}
7486 	};
7487 
7488 	//@}
7489 	///// The insertHTML command /////
7490 	//@{
7491 	commands.inserthtml = {
7492 		action: function (value, range) {
7493 
7494 
7495 			// "Delete the contents of the active range."
7496 			deleteContents(range);
7497 
7498 			// "If the active range's start node is neither editable nor an editing
7499 			// host, abort these steps."
7500 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
7501 				return;
7502 			}
7503 
7504 			// "Let frag be the result of calling createContextualFragment(value)
7505 			// on the active range."
7506 			var frag = range.createContextualFragment(value);
7507 
7508 			// "Let last child be the lastChild of frag."
7509 			var lastChild = frag.lastChild;
7510 
7511 			// "If last child is null, abort these steps."
7512 			if (!lastChild) {
7513 				return;
7514 			}
7515 
7516 			// "Let descendants be all descendants of frag."
7517 			var descendants = getDescendants(frag);
7518 
7519 			// "If the active range's start node is a block node:"
7520 			if (isBlockNode(range.startContainer)) {
7521 				// "Let collapsed block props be all editable collapsed block prop
7522 				// children of the active range's start node that have index
7523 				// greater than or equal to the active range's start offset."
7524 				//
7525 				// "For each node in collapsed block props, remove node from its
7526 				// parent."
7527 				$_(range.startContainer.childNodes).filter(function (node, range) {
7528 					return isEditable(node) && isCollapsedBlockProp(node) && getNodeIndex(node) >= range.startOffset;
7529 				}, true).forEach(function (node) {
7530 					node.parentNode.removeChild(node);
7531 				});
7532 			}
7533 
7534 			// "Call insertNode(frag) on the active range."
7535 			range.insertNode(frag);
7536 
7537 			// "If the active range's start node is a block node with no visible
7538 			// children, call createElement("br") on the context object and append
7539 			// the result as the last child of the active range's start node."
7540 			if (isBlockNode(range.startContainer)) {
7541 				ensureContainerEditable(range.startContainer);
7542 			}
7543 
7544 			// "Call collapse() on the context object's Selection, with last
7545 			// child's parent as the first argument and one plus its index as the
7546 			// second."
7547 			range.setStart(lastChild.parentNode, 1 + getNodeIndex(lastChild));
7548 			range.setEnd(lastChild.parentNode, 1 + getNodeIndex(lastChild));
7549 
7550 			// "Fix disallowed ancestors of each member of descendants."
7551 			var i;
7552 			for (i = 0; i < descendants.length; i++) {
7553 				fixDisallowedAncestors(descendants[i], range);
7554 			}
7555 
7556 			setActiveRange(range);
7557 		}
7558 	};
7559 
7560 	//@}
7561 	///// The insertImage command /////
7562 	//@{
7563 	commands.insertimage = {
7564 		action: function (value) {
7565 			// "If value is the empty string, abort these steps and do nothing."
7566 			if (value === "") {
7567 				return;
7568 			}
7569 
7570 			// "Let range be the active range."
7571 			var range = getActiveRange();
7572 
7573 			// "Delete the contents of range, with strip wrappers false."
7574 			deleteContents(range, {
7575 				stripWrappers: false
7576 			});
7577 
7578 			// "If the active range's start node is neither editable nor an editing
7579 			// host, abort these steps."
7580 			if (!isEditable(getActiveRange().startContainer) && !isEditingHost(getActiveRange().startContainer)) {
7581 				return;
7582 			}
7583 
7584 			// "If range's start node is a block node whose sole child is a br, and
7585 			// its start offset is 0, remove its start node's child from it."
7586 			if (isBlockNode(range.startContainer) && range.startContainer.childNodes.length == 1 && isNamedHtmlElement(range.startContainer.firstChild, "br") && range.startOffset == 0) {
7587 				range.startContainer.removeChild(range.startContainer.firstChild);
7588 			}
7589 
7590 			// "Let img be the result of calling createElement("img") on the
7591 			// context object."
7592 			var img = document.createElement("img");
7593 
7594 			// "Run setAttribute("src", value) on img."
7595 			img.setAttribute("src", value);
7596 
7597 			// "Run insertNode(img) on the range."
7598 			range.insertNode(img);
7599 
7600 			// "Run collapse() on the Selection, with first argument equal to the
7601 			// parent of img and the second argument equal to one plus the index of
7602 			// img."
7603 			//
7604 			// Not everyone actually supports collapse(), so we do it manually
7605 			// instead.  Also, we need to modify the actual range we're given as
7606 			// well, for the sake of autoimplementation.html's range-filling-in.
7607 			range.setStart(img.parentNode, 1 + getNodeIndex(img));
7608 			range.setEnd(img.parentNode, 1 + getNodeIndex(img));
7609 			Aloha.getSelection().removeAllRanges();
7610 			Aloha.getSelection().addRange(range);
7611 
7612 			// IE adds width and height attributes for some reason, so remove those
7613 			// to actually do what the spec says.
7614 			img.removeAttribute("width");
7615 			img.removeAttribute("height");
7616 		}
7617 	};
7618 
7619 	//@}
7620 	///// The insertLineBreak command /////
7621 	//@{
7622 	commands.insertlinebreak = {
7623 		action: function (value, range) {
7624 			// "Delete the contents of the active range, with strip wrappers false."
7625 			deleteContents(range, {
7626 				stripWrappers: false
7627 			});
7628 
7629 			// "If the active range's start node is neither editable nor an editing
7630 			// host, abort these steps."
7631 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
7632 				return;
7633 			}
7634 
7635 			// "If the active range's start node is an Element, and "br" is not an
7636 			// allowed child of it, abort these steps."
7637 			if (range.startContainer.nodeType == $_.Node.ELEMENT_NODE && !isAllowedChild("br", range.startContainer)) {
7638 				return;
7639 			}
7640 
7641 			// "If the active range's start node is not an Element, and "br" is not
7642 			// an allowed child of the active range's start node's parent, abort
7643 			// these steps."
7644 			if (range.startContainer.nodeType != $_.Node.ELEMENT_NODE && !isAllowedChild("br", range.startContainer.parentNode)) {
7645 				return;
7646 			}
7647 
7648 			// "If the active range's start node is a Text node and its start
7649 			// offset is zero, call collapse() on the context object's Selection,
7650 			// with first argument equal to the active range's start node's parent
7651 			// and second argument equal to the active range's start node's index."
7652 			var newNode, newOffset;
7653 			if (range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset == 0) {
7654 				newNode = range.startContainer.parentNode;
7655 				newOffset = getNodeIndex(range.startContainer);
7656 				Aloha.getSelection().collapse(newNode, newOffset);
7657 				range.setStart(newNode, newOffset);
7658 				range.setEnd(newNode, newOffset);
7659 			}
7660 
7661 			// "If the active range's start node is a Text node and its start
7662 			// offset is the length of its start node, call collapse() on the
7663 			// context object's Selection, with first argument equal to the active
7664 			// range's start node's parent and second argument equal to one plus
7665 			// the active range's start node's index."
7666 			if (range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset == getNodeLength(range.startContainer)) {
7667 				newNode = range.startContainer.parentNode;
7668 				newOffset = 1 + getNodeIndex(range.startContainer);
7669 				Aloha.getSelection().collapse(newNode, newOffset);
7670 				range.setStart(newNode, newOffset);
7671 				range.setEnd(newNode, newOffset);
7672 			}
7673 
7674 			// "Let br be the result of calling createElement("br") on the context
7675 			// object."
7676 			var br = document.createElement("br");
7677 
7678 			// "Call insertNode(br) on the active range."
7679 			range.insertNode(br);
7680 
7681 			// "Call collapse() on the context object's Selection, with br's parent
7682 			// as the first argument and one plus br's index as the second
7683 			// argument."
7684 			Aloha.getSelection().collapse(br.parentNode, 1 + getNodeIndex(br));
7685 			range.setStart(br.parentNode, 1 + getNodeIndex(br));
7686 			range.setEnd(br.parentNode, 1 + getNodeIndex(br));
7687 
7688 			// "If br is a collapsed line break, call createElement("br") on the
7689 7690 			// context object and let extra br be the result, then call
7691 			// insertNode(extra br) on the active range."
7692 			if (isCollapsedLineBreak(br)) {
7693 				// TODO
7694 				range.insertNode(createEndBreak());
7695 
7696 				// Compensate for nonstandard implementations of insertNode
7697 				Aloha.getSelection().collapse(br.parentNode, 1 + getNodeIndex(br));
7698 				range.setStart(br.parentNode, 1 + getNodeIndex(br));
7699 				range.setEnd(br.parentNode, 1 + getNodeIndex(br));
7700 			}
7701 
7702 			// IE7 is adding this styles: height: auto; min-height: 0px; max-height: none;
7703 			// with that there is the ugly "IE-editable-outline"
7704 			if (jQuery.browser.msie && jQuery.browser.version < 8) {
7705 				br.parentNode.removeAttribute("style");
7706 			}
7707 		}
7708 	};
7709 
7710 	//@}
7711 	///// The insertOrderedList command /////
7712 	//@{
7713 	commands.insertorderedlist = {
7714 		// "Toggle lists with tag name "ol"."
7715 		action: function (value, range) {
7716 			toggleLists("ol", range);
7717 		},
7718 		// "True if the selection's list state is "mixed" or "mixed ol", false
7719 		// otherwise."
7720 		indeterm: function () {
7721 			return (/^mixed( ol)?$/).test(getSelectionListState());
7722 		},
7723 		// "True if the selection's list state is "ol", false otherwise."
7724 		state: function () {
7725 			return getSelectionListState() == "ol";
7726 		}
7727 	};
7728 7729 
	var listRelatedElements = {
7730 		"LI": true,
7731 		"DT": true,
7732 		"DD": true
7733 	};
7734 
7735 	//@}
7736 	///// The insertParagraph command /////
7737 	//@{
7738 	commands.insertparagraph = {
7739 		action: function (value, range) {
7740 			var i;
7741 
7742 			// "Delete the contents of the active range."
7743 			deleteContents(range);
7744 
7745 			// clean lists in the editing host, this will remove any whitespace nodes around lists
7746 			// because the following algorithm is not prepared to deal with them
7747 			cleanLists(getEditingHostOf(range.startContainer), range);
7748 
7749 			// "If the active range's start node is neither editable nor an editing
7750 			// host, abort these steps."
7751 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
7752 				return;
7753 			}
7754 
7755 			// "Let node and offset be the active range's start node and offset."
7756 			var node = range.startContainer;
7757 			var offset = range.startOffset;
7758 
7759 			// "If node is a Text node, and offset is neither 0 nor the length of
7760 			// node, call splitText(offset) on node."
7761 			if (node.nodeType == $_.Node.TEXT_NODE && offset != 0 && offset != getNodeLength(node)) {
7762 				node.splitText(offset);
7763 			}
7764 
7765 			// "If node is a Text node and offset is its length, set offset to one
7766 			// plus the index of node, then set node to its parent."
7767 			if (node.nodeType == $_.Node.TEXT_NODE && offset == getNodeLength(node)) {
7768 				offset = 1 + getNodeIndex(node);
7769 				node = node.parentNode;
7770 			}
7771 
7772 			// "If node is a Text or Comment node, set offset to the index of node,
7773 			// then set node to its parent."
7774 			if (node.nodeType == $_.Node.TEXT_NODE || node.nodeType == $_.Node.COMMENT_NODE) {
7775 				offset = getNodeIndex(node);
7776 				node = node.parentNode;
7777 			}
7778 
7779 			// "Call collapse(node, offset) on the context object's Selection."
7780 			Aloha.getSelection().collapse(node, offset);
7781 			range.setStart(node, offset);
7782 			range.setEnd(node, offset);
7783 
7784 			// "Let container equal node."
7785 			var container = node;
7786 
7787 			// "While container is not a single-line container, and container's
7788 			// parent is editable and in the same editing host as node, set
7789 			// container to its parent."
7790 			while (!isSingleLineContainer(container) && isEditable(container.parentNode) && inSameEditingHost(node, container.parentNode)) {
7791 				container = container.parentNode;
7792 			}
7793 
7794 			// "If container is not editable or not in the same editing host as
7795 			// node or is not a single-line container:"
7796 			if (!isEditable(container) || !inSameEditingHost(container, node) || !isSingleLineContainer(container)) {
7797 				// "Let tag be the default single-line container name."
7798 				var tag = defaultSingleLineContainerName;
7799 
7800 				// "Block-extend the active range, and let new range be the
7801 				// result."
7802 				var newRange = blockExtend(range);
7803 
7804 				// "Let node list be a list of nodes, initially empty."
7805 				//
7806 				// "Append to node list the first node in tree order that is
7807 				// contained in new range and is an allowed child of "p", if any."
7808 				var nodeList = getContainedNodes(newRange, function (node) {
7809 					return isAllowedChild(node, "p");
7810 				}).slice(0, 1);
7811 
7812 				// "If node list is empty:"
7813 				if (!nodeList.length) {
7814 					// "If tag is not an allowed child of the active range's start
7815 					// node, abort these steps."
7816 					if (!isAllowedChild(tag, range.startContainer)) {
7817 						return;
7818 					}
7819 
7820 					// "Set container to the result of calling createElement(tag)
7821 					// on the context object."
7822 					container = document.createElement(tag);
7823 
7824 					// "Call insertNode(container) on the active range."
7825 					range.insertNode(container);
7826 
7827 					// "Call createElement("br") on the context object, and append
7828 					// the result as the last child of container."
7829 					// TODO not always
7830 					container.appendChild(createEndBreak());
7831 
7832 					// "Call collapse(container, 0) on the context object's
7833 					// Selection."
7834 					// TODO: remove selection from command
7835 					Aloha.getSelection().collapse(container, 0);
7836 					range.setStart(container, 0);
7837 					range.setEnd(container, 0);
7838 
7839 					// "Abort these steps."
7840 					return;
7841 				}
7842 
7843 				// "While the nextSibling of the last member of node list is not
7844 				// null and is an allowed child of "p", append it to node list."
7845 				while (nodeList[nodeList.length - 1].nextSibling && isAllowedChild(nodeList[nodeList.length - 1].nextSibling, "p")) {
7846 					nodeList.push(nodeList[nodeList.length - 1].nextSibling);
7847 				}
7848 
7849 				// "Wrap node list, with sibling criteria returning false and new
7850 				// parent instructions returning the result of calling
7851 				// createElement(tag) on the context object. Set container to the
7852 				// result."
7853 				container = wrap(
7854 					nodeList,
7855 					function () {
7856 						return false;
7857 					},
7858 					function () {
7859 						return document.createElement(tag);
7860 					},
7861 					range
7862 7863 				);
7864 			}
7865 
7866 			// If no container has been set yet, it is not possible to insert a paragraph at this position;
7867 			// the following steps are skipped in order to prevent critical errors from occurring;
7868 			if (!container) {
7869 				return;
7870 			}
7871 
7872 			// "If container's local name is "address", "listing", or "pre":"
7873 			var oldHeight, newHeight;
7874 			if (container.tagName == "ADDRESS" || container.tagName == "LISTING" || container.tagName == "PRE") {
7875 				// "Let br be the result of calling createElement("br") on the
7876 				// context object."
7877 				var br = document.createElement("br");
7878 
7879 				// remember the old height
7880 				oldHeight = container.offsetHeight;
7881 
7882 				// "Call insertNode(br) on the active range."
7883 				range.insertNode(br);
7884 
7885 				// determine the new height
7886 				newHeight = container.offsetHeight;
7887 
7888 				// "Call collapse(node, offset + 1) on the context object's
7889 				// Selection."
7890 				Aloha.getSelection().collapse(node, offset + 1);
7891 				range.setStart(node, offset + 1);
7892 				range.setEnd(node, offset + 1);
7893 
7894 				// "If br is the last descendant of container, let br be the result
7895 				// of calling createElement("br") on the context object, then call
7896 				// insertNode(br) on the active range." (Fix: only do this, if the container height did not change by inserting a single <br/>)
7897 				//
7898 				// Work around browser bugs: some browsers select the
7899 				// newly-inserted node, not per spec.
7900 				if (oldHeight == newHeight && !isDescendant(nextNode(br), container)) {
7901 					// TODO check
7902 					range.insertNode(createEndBreak());
7903 					Aloha.getSelection().collapse(node, offset + 1);
7904 					range.setEnd(node, offset + 1);
7905 				}
7906 
7907 				// "Abort these steps."
7908 				return;
7909 			}
7910 
7911 			// "If container's local name is "li", "dt", or "dd"; and either it has
7912 			// no children or it has a single child and that child is a br:"
7913 			if (listRelatedElements[container.tagName] && (!container.hasChildNodes() || (container.childNodes.length == 1 && isNamedHtmlElement(container.firstChild, "br")))) {
7914 				// "Split the parent of the one-node list consisting of container."
7915 				splitParent([container], range);
7916 
7917 				// "If container has no children, call createElement("br") on the
7918 				// context object and append the result as the last child of
7919 				// container."
7920 				// only do this, if inserting the br does NOT modify the offset height of the container
7921 				//			if (!container.hasChildNodes()) {
7922 				//				var oldHeight = container.offsetHeight, endBr = createEndBreak();
7923 				//				container.appendChild(endBr);
7924 				//				if (container.offsetHeight !== oldHeight) {
7925 				//					container.removeChild(endBr);
7926 				//				}
7927 				//			}
7928 
7929 				// "If container is a dd or dt, and it is not an allowed child of
7930 				// any of its ancestors in the same editing host, set the tag name
7931 				// of container to the default single-line container name and let
7932 				// container be the result."
7933 				if (isHtmlElementInArray(container, ["dd", "dt"]) && $_(getAncestors(container)).every(function (ancestor) { return !inSameEditingHost(container, ancestor) || !isAllowedChild(container, ancestor); })) {
7934 					container = setTagName(container, defaultSingleLineContainerName, range);
7935 				}
7936 
7937 				// "Fix disallowed ancestors of container."
7938 				fixDisallowedAncestors(container, range);
7939 
7940 				// fix invalid nested lists
7941 				if (isNamedHtmlElement(container, 'li') && isNamedHtmlElement(container.nextSibling, "li") && isHtmlElementInArray(container.nextSibling.firstChild, ["ol", "ul"])) {
7942 					// we found a li containing only a br followed by a li containing a list as first element: merge the two li's
7943 					var listParent = container.nextSibling,
7944 						length = container.nextSibling.childNodes.length;
7945 					for (i = 0; i < length; i++) {
7946 						// we always move the first child into the container
7947 						container.appendChild(listParent.childNodes[0]);
7948 					}
7949 					listParent.parentNode.removeChild(listParent);
7950 				}
7951 
7952 				// "Abort these steps."
7953 				return;
7954 			}
7955 
7956 			// "Let new line range be a new range whose start is the same as
7957 			// the active range's, and whose end is (container, length of
7958 			// container)."
7959 			var newLineRange = Aloha.createRange();
7960 			newLineRange.setStart(range.startContainer, range.startOffset);
7961 			newLineRange.setEnd(container, getNodeLength(container));
7962 
7963 			// "While new line range's start offset is zero and its start node is
7964 			// not container, set its start to (parent of start node, index of
7965 			// start node)."
7966 			while (newLineRange.startOffset == 0 && newLineRange.startContainer != container) {
7967 				newLineRange.setStart(newLineRange.startContainer.parentNode, getNodeIndex(newLineRange.startContainer));
7968 			}
7969 
7970 			// "While new line range's start offset is the length of its start node
7971 			// and its start node is not container, set its start to (parent of
7972 			// start node, 1 + index of start node)."
7973 			while (newLineRange.startOffset == getNodeLength(newLineRange.startContainer) && newLineRange.startContainer != container) {
7974 				newLineRange.setStart(newLineRange.startContainer.parentNode, 1 + getNodeIndex(newLineRange.startContainer));
7975 			}
7976 
7977 			// "Let end of line be true if new line range contains either nothing
7978 			// or a single br, and false otherwise."
7979 			var containedInNewLineRange = getContainedNodes(newLineRange);
7980 			var endOfLine = !containedInNewLineRange.length || (containedInNewLineRange.length == 1 && isNamedHtmlElement(containedInNewLineRange[0], "br"));
7981 
7982 			// "If the local name of container is "h1", "h2", "h3", "h4", "h5", or
7983 			// "h6", and end of line is true, let new container name be the default
7984 			// single-line container name."
7985 			var newContainerName;
7986 			if (/^H[1-6]$/.test(container.tagName) && endOfLine) {
7987 				newContainerName = defaultSingleLineContainerName;
7988 
7989 				// "Otherwise, if the local name of container is "dt" and end of line
7990 				// is true, let new container name be "dd"."
7991 			} else if (container.tagName == "DT" && endOfLine) {
7992 				newContainerName = "dd";
7993 
7994 				// "Otherwise, if the local name of container is "dd" and end of line
7995 				// is true, let new container name be "dt"."
7996 			} else if (container.tagName == "DD" && endOfLine) {
7997 				newContainerName = "dt";
7998 
7999 				// "Otherwise, let new container name be the local name of container."
8000 			} else {
8001 				newContainerName = container.tagName.toLowerCase();
8002 			}
8003 
8004 			// "Let new container be the result of calling createElement(new
8005 			// container name) on the context object."
8006 			var newContainer = document.createElement(newContainerName);
8007 
8008 			// "Copy all non empty attributes of the container to new container."
8009 			copyAttributes(container, newContainer);
8010 
8011 			// "If new container has an id attribute, unset it."
8012 			newContainer.removeAttribute("id");
8013 
8014 			// "Insert new container into the parent of container immediately after
8015 			// container."
8016 			container.parentNode.insertBefore(newContainer, container.nextSibling);
8017 
8018 			// "Let contained nodes be all nodes contained in new line range."
8019 			var containedNodes = getAllContainedNodes(newLineRange);
8020 
8021 			// "Let frag be the result of calling extractContents() on new line
8022 			// range."
8023 			var frag = newLineRange.extractContents();
8024 
8025 			// "Unset the id attribute (if any) of each Element descendant of frag
8026 			// that is not in contained nodes."
8027 			var descendants = getDescendants(frag);
8028 			for (i = 0; i < descendants.length; i++) {
8029 				if (descendants[i].nodeType == $_.Node.ELEMENT_NODE && $_(containedNodes).indexOf(descendants[i]) == -1) {
8030 					descendants[i].removeAttribute("id");
8031 				}
8032 			}
8033 
8034 			var fragChildren = [],
8035 				fragChild = frag.firstChild;
8036 			if (fragChild) {
8037 				do {
8038 					if (!isWhitespaceNode(fragChild)) {
8039 						fragChildren.push(fragChild);
8040 					}
8041 				} while (null != (fragChild = fragChild.nextSibling));
8042 			}
8043 
8044 			// 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)
8045 			if (isNamedHtmlElement(newContainer, 'li') && fragChildren.length && isHtmlElementInArray(fragChildren[0], ["ul", "ol"])) {
8046 				oldHeight = newContainer.offsetHeight;
8047 				var endBr = createEndBreak();
8048 				newContainer.appendChild(endBr);
8049 				newHeight = newContainer.offsetHeight;
8050 				if (oldHeight !== newHeight) {
8051 					newContainer.removeChild(endBr);
8052 				}
8053 			}
8054 
8055 			// "Call appendChild(frag) on new container."
8056 			newContainer.appendChild(frag);
8057 
8058 			// "If container has no visible children, call createElement("br") on
8059 			// the context object, and append the result as the last child of
8060 			// container."
8061 			ensureContainerEditable(container);
8062 
8063 			// "If new container has no visible children, call createElement("br")
8064 			// on the context object, and append the result as the last child of
8065 			// new container."
8066 			ensureContainerEditable(newContainer);
8067 
8068 			// "Call collapse(new container, 0) on the context object's Selection."
8069 			Aloha.getSelection().collapse(newContainer, 0);
8070 			range.setStart(newContainer, 0);
8071 			range.setEnd(newContainer, 0);
8072 		}
8073 	};
8074 
8075 	//@}
8076 	///// The insertText command /////
8077 	//@{
8078 	commands.inserttext = {
8079 		action: function (value, range) {
8080 			var i;
8081 
8082 			// "Delete the contents of the active range, with strip wrappers
8083 			// false."
8084 			deleteContents(range, {
8085 				stripWrappers: false
8086 			});
8087 
8088 			// "If the active range's start node is neither editable nor an editing
8089 			// host, abort these steps."
8090 			if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) {
8091 				return;
8092 			}
8093 
8094 			// "If value's length is greater than one:"
8095 			if (value.length > 1) {
8096 				// "For each element el in value, take the action for the
8097 				// insertText command, with value equal to el."
8098 				for (i = 0; i < value.length; i++) {
8099 					commands.inserttext.action(value[i], range);
8100 				}
8101 
8102 				// "Abort these steps."
8103 				return;
8104 			}
8105 
8106 			// "If value is the empty string, abort these steps."
8107 			if (value == "") {
8108 				return;
8109 			}
8110 
8111 			// "If value is a newline (U+00A0), take the action for the
8112 			// insertParagraph command and abort these steps."
8113 			if (value == "\n") {
8114 				commands.insertparagraph.action('', range);
8115 				return;
8116 			}
8117 
8118 			// "Let node and offset be the active range's start node and offset."
8119 			var node = range.startContainer;
8120 			var offset = range.startOffset;
8121 
8122 			// "If node has a child whose index is offset − 1, and that child is a
8123 			// Text node, set node to that child, then set offset to node's
8124 			// length."
8125 			if (0 <= offset - 1 && offset - 1 < node.childNodes.length && node.childNodes[offset - 1].nodeType == $_.Node.TEXT_NODE) {
8126 				node = node.childNodes[offset - 1];
8127 				offset = getNodeLength(node);
8128 			}
8129 
8130 			// "If node has a child whose index is offset, and that child is a Text
8131 			// node, set node to that child, then set offset to zero."
8132 			if (0 <= offset && offset < node.childNodes.length && node.childNodes[offset].nodeType == $_.Node.TEXT_NODE) {
8133 				node = node.childNodes[offset];
8134 				offset = 0;
8135 			}
8136 
8137 			// "If value is a space (U+0020), and either node is an Element whose
8138 			// resolved value for "white-space" is neither "pre" nor "pre-wrap" or
8139 			// node is not an Element but its parent is an Element whose resolved
8140 			// value for "white-space" is neither "pre" nor "pre-wrap", set value
8141 			// to a non-breaking space (U+00A0)."
8142 			var refElement = node.nodeType == $_.Node.ELEMENT_NODE ? node : node.parentNode;
8143 			if (value == " " && refElement.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(refElement).whiteSpace, ["pre", "pre-wrap"]) == -1) {
8144 				value = "\xa0";
8145 			}
8146 8147 
			// "Record current overrides, and let overrides be the result."
8148 			var overrides = recordCurrentOverrides(range);
8149 
8150 			// "If node is a Text node:"
8151 			if (node.nodeType == $_.Node.TEXT_NODE) {
8152 				// "Call insertData(offset, value) on node."
8153 				node.insertData(offset, value);
8154 
8155 				// "Call collapse(node, offset) on the context object's Selection."
8156 				Aloha.getSelection().collapse(node, offset);
8157 				range.setStart(node, offset);
8158 
8159 				// "Call extend(node, offset + 1) on the context object's
8160 				// Selection."
8161 				Aloha.getSelection().extend(node, offset + 1);
8162 				range.setEnd(node, offset + 1);
8163 
8164 				// "Otherwise:"
8165 			} else {
8166 				// "If node has only one child, which is a collapsed line break,
8167 				// remove its child from it."
8168 				//
8169 				// FIXME: IE incorrectly returns false here instead of true
8170 				// sometimes?
8171 				if (node.childNodes.length == 1 && isCollapsedLineBreak(node.firstChild)) {
8172 					node.removeChild(node.firstChild);
8173 				}
8174 
8175 				// "Let text be the result of calling createTextNode(value) on the
8176 				// context object."
8177 				var text = document.createTextNode(value);
8178 
8179 				// "Call insertNode(text) on the active range."
8180 				range.insertNode(text);
8181 
8182 				// "Call collapse(text, 0) on the context object's Selection."
8183 				Aloha.getSelection().collapse(text, 0);
8184 				range.setStart(text, 0);
8185 
8186 				// "Call extend(text, 1) on the context object's Selection."
8187 				Aloha.getSelection().extend(text, 1);
8188 				range.setEnd(text, 1);
8189 			}
8190 
8191 			// "Restore states and values from overrides."
8192 			restoreStatesAndValues(overrides, range);
8193 
8194 			// "Canonicalize whitespace at the active range's start."
8195 			canonicalizeWhitespace(range.startContainer, range.startOffset);
8196 
8197 			// "Canonicalize whitespace at the active range's end."
8198 			canonicalizeWhitespace(range.endContainer, range.endOffset);
8199 
8200 			// "Call collapseToEnd() on the context object's Selection."
8201 			Aloha.getSelection().collapseToEnd();
8202 			range.collapse(false);
8203 		}
8204 	};
8205 
8206 	//@}
8207 	///// The insertUnorderedList command /////
8208 	//@{
8209 	commands.insertunorderedlist = {
8210 		// "Toggle lists with tag name "ul"."
8211 		action: function (value, range) {
8212 			toggleLists("ul", range);
8213 		},
8214 		// "True if the selection's list state is "mixed" or "mixed ul", false
8215 		// otherwise."
8216 		indeterm: function () {
8217 			return (/^mixed( ul)?$/).test(getSelectionListState());
8218 		},
8219 		// "True if the selection's list state is "ul", false otherwise."
8220 		state: function () {
8221 			return getSelectionListState() == "ul";
8222 		}
8223 	};
8224 
8225 	//@}
8226 	///// The justifyCenter command /////
8227 	//@{
8228 	commands.justifycenter = {
8229 		// "Justify the selection with alignment "center"."
8230 		action: function (value, range) {
8231 			justifySelection("center", range);
8232 		},
8233 		indeterm: function () {
8234 			// "Block-extend the active range. Return true if among visible
8235 			// editable nodes that are contained in the result and have no
8236 			// children, at least one has alignment value "center" and at least one
8237 			// does not. Otherwise return false."
8238 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8239 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8240 			});
8241 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "center"; })
8242 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "center"; });
8243 		},
8244 		state: function () {
8245 			// "Block-extend the active range. Return true if there is at least one
8246 			// visible editable node that is contained in the result and has no
8247 			// children, and all such nodes have alignment value "center".
8248 			// Otherwise return false."
8249 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8250 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8251 			});
8252 			return nodes.length && $_(nodes).every(function (node) {
8253 				return getAlignmentValue(node) == "center";
8254 			});
8255 		},
8256 		value: function () {
8257 			// "Block-extend the active range, and return the alignment value of
8258 			// the first visible editable node that is contained in the result and
8259 			// has no children. If there is no such node, return "left"."
8260 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8261 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8262 			});
8263 			if (nodes.length) {
8264 				return getAlignmentValue(nodes[0]);
8265 			}
8266 			return "left";
8267 		}
8268 	};
8269 
8270 	//@}
8271 	///// The justifyFull command /////
8272 	//@{
8273 	commands.justifyfull = {
8274 		// "Justify the selection with alignment "justify"."
8275 		action: function (value, range) {
8276 			justifySelection("justify", range);
8277 		},
8278 		indeterm: function () {
8279 			// "Block-extend the active range. Return true if among visible
8280 			// editable nodes that are contained in the result and have no
8281 			// children, at least one has alignment value "justify" and at least
8282 			// one does not. Otherwise return false."
8283 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8284 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8285 			});
8286 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "justify"; })
8287 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "justify"; });
8288 		},
8289 		state: function () {
8290 			// "Block-extend the active range. Return true if there is at least one
8291 			// visible editable node that is contained in the result and has no
8292 			// children, and all such nodes have alignment value "justify".
8293 			// Otherwise return false."
8294 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8295 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8296 			});
8297 			return nodes.length && $_(nodes).every(function (node) {
8298 				return getAlignmentValue(node) == "justify";
8299 			});
8300 		},
8301 		value: function () {
8302 			// "Block-extend the active range, and return the alignment value of
8303 			// the first visible editable node that is contained in the result and
8304 			// has no children. If there is no such node, return "left"."
8305 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8306 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8307 			});
8308 			if (nodes.length) {
8309 				return getAlignmentValue(nodes[0]);
8310 			}
8311 			return "left";
8312 		}
8313 	};
8314 
8315 	//@}
8316 	///// The justifyLeft command /////
8317 	//@{
8318 	commands.justifyleft = {
8319 		// "Justify the selection with alignment "left"."
8320 		action: function (value, range) {
8321 			justifySelection("left", range);
8322 		},
8323 		indeterm: function () {
8324 			// "Block-extend the active range. Return true if among visible
8325 			// editable nodes that are contained in the result and have no
8326 			// children, at least one has alignment value "left" and at least one
8327 			// does not. Otherwise return false."
8328 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8329 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8330 			});
8331 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "left"; })
8332 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "left"; });
8333 		},
8334 		state: function () {
8335 8336 			// "Block-extend the active range. Return true if there is at least one
8337 			// visible editable node that is contained in the result and has no
8338 			// children, and all such nodes have alignment value "left".  Otherwise
8339 			// return false."
8340 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8341 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8342 			});
8343 			return nodes.length && $_(nodes).every(function (node) {
8344 				return getAlignmentValue(node) == "left";
8345 			});
8346 		},
8347 		value: function () {
8348 			// "Block-extend the active range, and return the alignment value of
8349 			// the first visible editable node that is contained in the result and
8350 			// has no children. If there is no such node, return "left"."
8351 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8352 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8353 			});
8354 			if (nodes.length) {
8355 				return getAlignmentValue(nodes[0]);
8356 			}
8357 			return "left";
8358 		}
8359 	};
8360 
8361 	//@}
8362 	///// The justifyRight command /////
8363 	//@{
8364 	commands.justifyright = {
8365 		// "Justify the selection with alignment "right"."
8366 		action: function (value, range) {
8367 			justifySelection("right", range);
8368 		},
8369 		indeterm: function () {
8370 			// "Block-extend the active range. Return true if among visible
8371 			// editable nodes that are contained in the result and have no
8372 			// children, at least one has alignment value "right" and at least one
8373 			// does not. Otherwise return false."
8374 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8375 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8376 			});
8377 			return $_(nodes).some(function (node) { return getAlignmentValue(node) == "right"; })
8378 				&& $_(nodes).some(function (node) { return getAlignmentValue(node) != "right"; });
8379 		},
8380 		state: function () {
8381 			// "Block-extend the active range. Return true if there is at least one
8382 			// visible editable node that is contained in the result and has no
8383 			// children, and all such nodes have alignment value "right".
8384 			// Otherwise return false."
8385 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8386 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8387 			});
8388 			return nodes.length && $_(nodes).every(function (node) {
8389 				return getAlignmentValue(node) == "right";
8390 			});
8391 		},
8392 		value: function () {
8393 			// "Block-extend the active range, and return the alignment value of
8394 			// the first visible editable node that is contained in the result and
8395 			// has no children. If there is no such node, return "left"."
8396 			var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) {
8397 				return isEditable(node) && isVisible(node) && !node.hasChildNodes();
8398 			});
8399 			if (nodes.length) {
8400 				return getAlignmentValue(nodes[0]);
8401 			}
8402 			return "left";
8403 		}
8404 	};
8405 
8406 	//@}
8407 	///// The outdent command /////
8408 	//@{
8409 	commands.outdent = {
8410 		action: function () {
8411 			// "Let items be a list of all lis that are ancestor containers of the
8412 			// range's start and/or end node."
8413 			//
8414 			// It's annoying to get this in tree order using functional stuff
8415 			// without doing getDescendants(document), which is slow, so I do it
8416 			// imperatively.
8417 			var items = [];
8418 			(function () {
8419 				var ancestorContainer;
8420 				for (ancestorContainer = getActiveRange().endContainer;
8421 					     ancestorContainer != getActiveRange().commonAncestorContainer;
8422 					     ancestorContainer = ancestorContainer.parentNode) {
8423 					if (isNamedHtmlElement(ancestorContainer, "li")) {
8424 						items.unshift(ancestorContainer);
8425 					}
8426 				}
8427 				for (ancestorContainer = getActiveRange().startContainer;
8428 					     ancestorContainer;
8429 					     ancestorContainer = ancestorContainer.parentNode) {
8430 					if (isNamedHtmlElement(ancestorContainer, "li")) {
8431 						items.unshift(ancestorContainer);
8432 					}
8433 				}
8434 			}());
8435 
8436 			// "For each item in items, normalize sublists of item."
8437 			$_(items).forEach(function (thisArg) {
8438 				normalizeSublists(thisArg, getActiveRange());
8439 			});
8440 
8441 			// "Block-extend the active range, and let new range be the result."
8442 			var newRange = blockExtend(getActiveRange());
8443 
8444 			// "Let node list be a list of nodes, initially empty."
8445 			//
8446 			// "For each node node contained in new range, append node to node list
8447 8448 			// if the last member of node list (if any) is not an ancestor of node;
8449 			// node is editable; and either node has no editable descendants, or is
8450 			// an ol or ul, or is an li whose parent is an ol or ul."
8451 			var nodeList = getContainedNodes(newRange, function (node) {
8452 				return isEditable(node) && (!$_(getDescendants(node)).some(isEditable) || isHtmlElementInArray(node, ["ol", "ul"]) || (isNamedHtmlElement(node, 'li') && isHtmlElementInArray(node.parentNode, ["ol", "ul"])));
8453 			});
8454 
8455 			// "While node list is not empty:"
8456 			while (nodeList.length) {
8457 				// "While the first member of node list is an ol or ul or is not
8458 				// the child of an ol or ul, outdent it and remove it from node
8459 				// list."
8460 				while (nodeList.length && (isHtmlElementInArray(nodeList[0], ["OL", "UL"]) || !isHtmlElementInArray(nodeList[0].parentNode, ["OL", "UL"]))) {
8461 					outdentNode(nodeList.shift(), newRange);
8462 				}
8463 
8464 				// "If node list is empty, break from these substeps."
8465 				if (!nodeList.length) {
8466 					break;
8467 				}
8468 
8469 				// "Let sublist be a list of nodes, initially empty."
8470 				var sublist = [];
8471 
8472 				// "Remove the first member of node list and append it to sublist."
8473 				sublist.push(nodeList.shift());
8474 
8475 				// "While the first member of node list is the nextSibling of the
8476 				// last member of sublist, and the first member of node list is not
8477 				// an ol or ul, remove the first member of node list and append it
8478 				// to sublist."
8479 				while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isHtmlElementInArray(nodeList[0], ["OL", "UL"])) {
8480 					sublist.push(nodeList.shift());
8481 				}
8482 
8483 				// "Record the values of sublist, and let values be the result."
8484 				var values = recordValues(sublist);
8485 
8486 				// "Split the parent of sublist, with new parent null."
8487 				splitParent(sublist, newRange);
8488 
8489 				// "Fix disallowed ancestors of each member of sublist."
8490 				$_(sublist).forEach(fixDisallowedAncestors);
8491 
8492 				// "Restore the values from values."
8493 				restoreValues(values, newRange);
8494 			}
8495 		}
8496 	};
8497 
8498 	//@}
8499 
8500 	//////////////////////////////////
8501 	///// Miscellaneous commands /////
8502 	//////////////////////////////////
8503 
8504 	///// The selectAll command /////
8505 	//@{
8506 	commands.selectall = {
8507 		// Note, this ignores the whole globalRange/getActiveRange() thing and
8508 		// works with actual selections.  Not suitable for autoimplementation.html.
8509 		action: function () {
8510 			// "Let target be the body element of the context object."
8511 			var target = document.body;
8512 
8513 			// "If target is null, let target be the context object's
8514 			// documentElement."
8515 			if (!target) {
8516 				target = document.documentElement;
8517 			}
8518 
8519 			// "If target is null, call getSelection() on the context object, and
8520 			// call removeAllRanges() on the result."
8521 			if (!target) {
8522 				Aloha.getSelection().removeAllRanges();
8523 
8524 				// "Otherwise, call getSelection() on the context object, and call
8525 				// selectAllChildren(target) on the result."
8526 			} else {
8527 				Aloha.getSelection().selectAllChildren(target);
8528 			}
8529 		}
8530 	};
8531 
8532 	//@}
8533 	///// The styleWithCSS command /////
8534 	//@{
8535 	commands.stylewithcss = {
8536 		action: function (value) {
8537 			// "If value is an ASCII case-insensitive match for the string
8538 			// "false", set the CSS styling flag to false. Otherwise, set the
8539 			// CSS styling flag to true."
8540 8541 			cssStylingFlag = String(value).toLowerCase() != "false";
8542 		},
8543 		state: function () {
8544 			return cssStylingFlag;
8545 		}
8546 	};
8547 
8548 	//@}
8549 	///// The useCSS command /////
8550 	//@{
8551 	commands.usecss = {
8552 		action: function (value) {
8553 			// "If value is an ASCII case-insensitive match for the string "false",
8554 			// set the CSS styling flag to true. Otherwise, set the CSS styling
8555 			// flag to false."
8556 			cssStylingFlag = String(value).toLowerCase() == "false";
8557 		}
8558 	};
8559 	//@}
8560 
8561 	// Some final setup
8562 	//@{
8563 	(function () {
8564 		// Opera 11.50 doesn't implement Object.keys, so I have to make an explicit
8565 		// temporary, which means I need an extra closure to not leak the temporaries
8566 		// into the global namespace.  >:(
8567 		var commandNames = [];
8568 		var command;
8569 		for (command in commands) {
8570 			if (commands.hasOwnProperty(command)) {
8571 				commandNames.push(command);
8572 			}
8573 		}
8574 		$_(commandNames).forEach(function (command) {
8575 			// "If a command does not have a relevant CSS property specified, it
8576 			// defaults to null."
8577 			if (null == commands[command].relevantCssProperty) {
8578 				commands[command].relevantCssProperty = null;
8579 			}
8580 
8581 			// "If a command has inline command activated values defined but
8582 			// nothing else defines when it is indeterminate, it is indeterminate
8583 			// if among editable Text nodes effectively contained in the active
8584 			// range, there is at least one whose effective command value is one of
8585 			// the given values and at least one whose effective command value is
8586 			// not one of the given values."
8587 			if (null != commands[command].inlineCommandActivatedValues && null == commands[command].indeterm) {
8588 				commands[command].indeterm = function (range) {
8589 					var values = $_(getAllEffectivelyContainedNodes(range, function (node) { return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; }))
8590 						.map(function (node) { return getEffectiveCommandValue(node, command); });
8591 
8592 					var matchingValues = $_(values).filter(function (value) {
8593 						return $_(commands[command].inlineCommandActivatedValues).indexOf(value) != -1;
8594 					});
8595 
8596 					return matchingValues.length >= 1 && values.length - matchingValues.length >= 1;
8597 				};
8598 			}
8599 
8600 			// "If a command has inline command activated values defined, its state
8601 			// is true if either no editable Text node is effectively contained in
8602 			// the active range, and the active range's start node's effective
8603 			// command value is one of the given values; or if there is at least
8604 			// one editable Text node effectively contained in the active range,
8605 			// and all of them have an effective command value equal to one of the
8606 			// given values."
8607 			if (null != commands[command].inlineCommandActivatedValues) {
8608 				commands[command].state = function (range) {
8609 					var nodes = getAllEffectivelyContainedNodes(range, function (node) {
8610 						return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
8611 					});
8612 
8613 					if (nodes.length == 0) {
8614 						return $_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(range.startContainer, command)) != -1;
8615 					}
8616 					return $_(nodes).every(function (node) {
8617 						return $_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(node, command)) != -1;
8618 					});
8619 				};
8620 			}
8621 
8622 			// "If a command is a standard inline value command, it is
8623 			// indeterminate if among editable Text nodes that are effectively
8624 			// contained in the active range, there are two that have distinct
8625 			// effective command values. Its value is the effective command value
8626 			// of the first editable Text node that is effectively contained in the
8627 			// active range, or if there is no such node, the effective command
8628 			// value of the active range's start node."
8629 			if (null != commands[command].standardInlineValueCommand) {
8630 				commands[command].indeterm = function () {
8631 					var values = $_(getAllEffectivelyContainedNodes(getActiveRange())).filter(function (node) { return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; }, true)
8632 						.map(function (node) { return getEffectiveCommandValue(node, command); });
8633 					var i;
8634 					for (i = 1; i < values.length; i++) {
8635 						if (values[i] != values[i - 1]) {
8636 							return true;
8637 						}
8638 					}
8639 					return false;
8640 				};
8641 
8642 				commands[command].value = function (range) {
8643 					var refNode = getAllEffectivelyContainedNodes(range, function (node) {
8644 						return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE;
8645 					})[0];
8646 
8647 					if (typeof refNode == "undefined") {
8648 						refNode = range.startContainer;
8649 					}
8650 
8651 					return getEffectiveCommandValue(refNode, command);
8652 				};
8653 			}
8654 		});
8655 	}());
8656 	//@}
8657 	return {
8658 		commands: commands,
8659 		execCommand: myExecCommand,
8660 		queryCommandIndeterm: myQueryCommandIndeterm,
8661 		queryCommandState: myQueryCommandState,
8662 		queryCommandValue: myQueryCommandValue,
8663 		queryCommandEnabled: myQueryCommandEnabled,
8664 		queryCommandSupported: myQueryCommandSupported,
8665 		copyAttributes: copyAttributes,
8666 		createEndBreak: createEndBreak,
8667 		isEndBreak: isEndBreak,
8668 		ensureContainerEditable: ensureContainerEditable,
8669 		isEditingHost: isEditingHost,
8670 		isEditable: isEditable,
8671 		getStateOverride: getStateOverride,
8672 		setStateOverride: setStateOverride,
8673 		resetOverrides: resetOverrides,
8674 		unsetStateOverride: unsetStateOverride
8675 	};
8676 }); // end define
8677 // vim: foldmarker=@{,@} foldmethod=marker
8678