diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /testing/web-platform/tests/editing/include | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/editing/include')
6 files changed, 15034 insertions, 0 deletions
diff --git a/testing/web-platform/tests/editing/include/editor-test-utils.js b/testing/web-platform/tests/editing/include/editor-test-utils.js new file mode 100644 index 0000000000..a4c24c94ed --- /dev/null +++ b/testing/web-platform/tests/editing/include/editor-test-utils.js @@ -0,0 +1,416 @@ +/** + * EditorTestUtils is a helper utilities to test HTML editor. This can be + * instantiated per an editing host. If you test `designMode`, the editing + * host should be the <body> element. + * Note that if you want to use sendKey in a sub-document, you need to include + * testdriver.js (and related files) from the sub-document before creating this. + */ +class EditorTestUtils { + kShift = "\uE008"; + kMeta = "\uE03d"; + kControl = "\uE009"; + kAlt = "\uE00A"; + + editingHost; + + constructor(aEditingHost, aHarnessWindow = window) { + this.editingHost = aEditingHost; + if (aHarnessWindow != this.window && this.window.test_driver) { + this.window.test_driver.set_test_context(aHarnessWindow); + } + } + + get document() { + return this.editingHost.ownerDocument; + } + get window() { + return this.document.defaultView; + } + get selection() { + return this.window.getSelection(); + } + + sendKey(key, modifier) { + if (!modifier) { + return new this.window.test_driver.Actions() + .keyDown(key) + .keyUp(key) + .send(); + } + return new this.window.test_driver.Actions() + .keyDown(modifier) + .keyDown(key) + .keyUp(key) + .keyUp(modifier) + .send(); + } + + sendDeleteKey(modifier) { + const kDeleteKey = "\uE017"; + return this.sendKey(kDeleteKey, modifier); + } + + sendBackspaceKey(modifier) { + const kBackspaceKey = "\uE003"; + return this.sendKey(kBackspaceKey, modifier); + } + + sendArrowLeftKey(modifier) { + const kArrowLeft = "\uE012"; + return this.sendKey(kArrowLeft, modifier); + } + + sendArrowRightKey(modifier) { + const kArrowRight = "\uE014"; + return this.sendKey(kArrowRight, modifier); + } + + sendHomeKey(modifier) { + const kHome = "\uE011"; + return this.sendKey(kHome, modifier); + } + + sendEndKey(modifier) { + const kEnd = "\uE010"; + return this.sendKey(kEnd, modifier); + } + + sendEnterKey(modifier) { + const kEnter = "\uE007"; + return this.sendKey(kEnter, modifier); + } + + sendSelectAllShortcutKey() { + return this.sendKey( + "a", + this.window.navigator.platform.includes("Mac") + ? this.kMeta + : this.kControl + ); + } + + // Similar to `setupDiv` in editing/include/tests.js, this method sets + // innerHTML value of this.editingHost, and sets multiple selection ranges + // specified with the markers. + // - `[` specifies start boundary in a text node + // - `{` specifies start boundary before a node + // - `]` specifies end boundary in a text node + // - `}` specifies end boundary after a node + // + // options can have following fields: + // - selection: how to set selection, "addRange" (default), + // "setBaseAndExtent", "setBaseAndExtent-reverse". + setupEditingHost(innerHTMLWithRangeMarkers, options = {}) { + if (!options.selection) { + options.selection = "addRange"; + } + const startBoundaries = innerHTMLWithRangeMarkers.match(/\{|\[/g) || []; + const endBoundaries = innerHTMLWithRangeMarkers.match(/\}|\]/g) || []; + if (startBoundaries.length !== endBoundaries.length) { + throw "Should match number of open/close markers"; + } + + this.editingHost.innerHTML = innerHTMLWithRangeMarkers; + this.editingHost.focus(); + + if (startBoundaries.length === 0) { + // Don't remove the range for now since some tests may assume that + // setting innerHTML does not remove all selection ranges. + return; + } + + let getNextRangeAndDeleteMarker = startNode => { + let getNextLeafNode = node => { + let inclusiveDeepestFirstChildNode = container => { + while (container.firstChild) { + container = container.firstChild; + } + return container; + }; + if (node.hasChildNodes()) { + return inclusiveDeepestFirstChildNode(node); + } + if (node === this.editingHost) { + return null; + } + if (node.nextSibling) { + return inclusiveDeepestFirstChildNode(node.nextSibling); + } + let nextSibling = (child => { + for ( + let parent = child.parentElement; + parent && parent != this.editingHost; + parent = parent.parentElement + ) { + if (parent.nextSibling) { + return parent.nextSibling; + } + } + return null; + })(node); + if (!nextSibling) { + return null; + } + return inclusiveDeepestFirstChildNode(nextSibling); + }; + let scanMarkerInTextNode = (textNode, offset) => { + return /[\{\[\]\}]/.exec(textNode.data.substr(offset)); + }; + let startMarker = ((startContainer, startOffset) => { + let scanStartMakerInTextNode = (textNode, offset) => { + let scanResult = scanMarkerInTextNode(textNode, offset); + if (scanResult === null) { + return null; + } + if (scanResult[0] === "}" || scanResult[0] === "]") { + throw "An end marker is found before a start marker"; + } + return { + marker: scanResult[0], + container: textNode, + offset: scanResult.index + offset, + }; + }; + if (startContainer.nodeType === Node.TEXT_NODE) { + let scanResult = scanStartMakerInTextNode( + startContainer, + startOffset + ); + if (scanResult !== null) { + return scanResult; + } + } + let nextNode = startContainer; + while ((nextNode = getNextLeafNode(nextNode))) { + if (nextNode.nodeType === Node.TEXT_NODE) { + let scanResult = scanStartMakerInTextNode(nextNode, 0); + if (scanResult !== null) { + return scanResult; + } + continue; + } + } + return null; + })(startNode, 0); + if (startMarker === null) { + return null; + } + let endMarker = ((startContainer, startOffset) => { + let scanEndMarkerInTextNode = (textNode, offset) => { + let scanResult = scanMarkerInTextNode(textNode, offset); + if (scanResult === null) { + return null; + } + if (scanResult[0] === "{" || scanResult[0] === "[") { + throw "A start marker is found before an end marker"; + } + return { + marker: scanResult[0], + container: textNode, + offset: scanResult.index + offset, + }; + }; + if (startContainer.nodeType === Node.TEXT_NODE) { + let scanResult = scanEndMarkerInTextNode(startContainer, startOffset); + if (scanResult !== null) { + return scanResult; + } + } + let nextNode = startContainer; + while ((nextNode = getNextLeafNode(nextNode))) { + if (nextNode.nodeType === Node.TEXT_NODE) { + let scanResult = scanEndMarkerInTextNode(nextNode, 0); + if (scanResult !== null) { + return scanResult; + } + continue; + } + } + return null; + })(startMarker.container, startMarker.offset + 1); + if (endMarker === null) { + throw "Found an open marker, but not found corresponding close marker"; + } + let indexOfContainer = (container, child) => { + let offset = 0; + for (let node = container.firstChild; node; node = node.nextSibling) { + if (node == child) { + return offset; + } + offset++; + } + throw "child must be a child node of container"; + }; + let deleteFoundMarkers = () => { + let removeNode = node => { + let container = node.parentElement; + let offset = indexOfContainer(container, node); + node.remove(); + return { container, offset }; + }; + if (startMarker.container == endMarker.container) { + // If the text node becomes empty, remove it and set collapsed range + // to the position where there is the text node. + if (startMarker.container.length === 2) { + if (!/[\[\{][\]\}]/.test(startMarker.container.data)) { + throw `Unexpected text node (data: "${startMarker.container.data}")`; + } + let { container, offset } = removeNode(startMarker.container); + startMarker.container = endMarker.container = container; + startMarker.offset = endMarker.offset = offset; + startMarker.marker = endMarker.marker = ""; + return; + } + startMarker.container.data = `${startMarker.container.data.substring( + 0, + startMarker.offset + )}${startMarker.container.data.substring( + startMarker.offset + 1, + endMarker.offset + )}${startMarker.container.data.substring(endMarker.offset + 1)}`; + if (startMarker.offset >= startMarker.container.length) { + startMarker.offset = endMarker.offset = + startMarker.container.length; + return; + } + endMarker.offset--; // remove the start marker's length + if (endMarker.offset > endMarker.container.length) { + endMarker.offset = endMarker.container.length; + } + return; + } + if (startMarker.container.length === 1) { + let { container, offset } = removeNode(startMarker.container); + startMarker.container = container; + startMarker.offset = offset; + startMarker.marker = ""; + } else { + startMarker.container.data = `${startMarker.container.data.substring( + 0, + startMarker.offset + )}${startMarker.container.data.substring(startMarker.offset + 1)}`; + } + if (endMarker.container.length === 1) { + let { container, offset } = removeNode(endMarker.container); + endMarker.container = container; + endMarker.offset = offset; + endMarker.marker = ""; + } else { + endMarker.container.data = `${endMarker.container.data.substring( + 0, + endMarker.offset + )}${endMarker.container.data.substring(endMarker.offset + 1)}`; + } + }; + deleteFoundMarkers(); + + let handleNodeSelectMarker = () => { + if (startMarker.marker === "{") { + if (startMarker.offset === 0) { + // The range start with the text node. + let container = startMarker.container.parentElement; + startMarker.offset = indexOfContainer( + container, + startMarker.container + ); + startMarker.container = container; + } else if (startMarker.offset === startMarker.container.data.length) { + // The range start after the text node. + let container = startMarker.container.parentElement; + startMarker.offset = + indexOfContainer(container, startMarker.container) + 1; + startMarker.container = container; + } else { + throw 'Start marker "{" is allowed start or end of a text node'; + } + } + if (endMarker.marker === "}") { + if (endMarker.offset === 0) { + // The range ends before the text node. + let container = endMarker.container.parentElement; + endMarker.offset = indexOfContainer(container, endMarker.container); + endMarker.container = container; + } else if (endMarker.offset === endMarker.container.data.length) { + // The range ends with the text node. + let container = endMarker.container.parentElement; + endMarker.offset = + indexOfContainer(container, endMarker.container) + 1; + endMarker.container = container; + } else { + throw 'End marker "}" is allowed start or end of a text node'; + } + } + }; + handleNodeSelectMarker(); + + let range = document.createRange(); + range.setStart(startMarker.container, startMarker.offset); + range.setEnd(endMarker.container, endMarker.offset); + return range; + }; + + let ranges = []; + for ( + let range = getNextRangeAndDeleteMarker(this.editingHost.firstChild); + range; + range = getNextRangeAndDeleteMarker(range.endContainer) + ) { + ranges.push(range); + } + + if (options.selection != "addRange" && ranges.length > 1) { + throw `Failed due to invalid selection option, ${options.selection}, for multiple selection ranges`; + } + + this.selection.removeAllRanges(); + for (const range of ranges) { + if (options.selection == "addRange") { + this.selection.addRange(range); + } else if (options.selection == "setBaseAndExtent") { + this.selection.setBaseAndExtent( + range.startContainer, + range.startOffset, + range.endContainer, + range.endOffset + ); + } else if (options.selection == "setBaseAndExtent-reverse") { + this.selection.setBaseAndExtent( + range.endContainer, + range.endOffset, + range.startContainer, + range.startOffset + ); + } else { + throw `Failed due to invalid selection option, ${options.selection}`; + } + } + + if (this.selection.rangeCount != ranges.length) { + throw `Failed to set selection to the given ranges whose length is ${ranges.length}, but only ${this.selection.rangeCount} ranges are added`; + } + } + + // Originated from normalizeSerializedStyle in include/tests.js + normalizeStyleAttributeValues() { + for (const element of Array.from( + this.editingHost.querySelectorAll("[style]") + )) { + element.setAttribute( + "style", + element + .getAttribute("style") + // Random spacing differences + .replace(/; ?$/, "") + .replace(/: /g, ":") + // Gecko likes "transparent" + .replace(/transparent/g, "rgba(0, 0, 0, 0)") + // WebKit likes to look overly precise + .replace(/, 0.496094\)/g, ", 0.5)") + // Gecko converts anything with full alpha to "transparent" which + // then becomes "rgba(0, 0, 0, 0)", so we have to make other + // browsers match + .replace(/rgba\([0-9]+, [0-9]+, [0-9]+, 0\)/g, "rgba(0, 0, 0, 0)") + ); + } + } +} diff --git a/testing/web-platform/tests/editing/include/implementation.js b/testing/web-platform/tests/editing/include/implementation.js new file mode 100644 index 0000000000..44a7afd82d --- /dev/null +++ b/testing/web-platform/tests/editing/include/implementation.js @@ -0,0 +1,8526 @@ +"use strict"; + +var htmlNamespace = "http://www.w3.org/1999/xhtml"; + +var cssStylingFlag = false; + +var defaultSingleLineContainerName = "div"; + +// This is bad :( +var globalRange = null; + +// Commands are stored in a dictionary where we call their actions and such +var commands = {}; + +/////////////////////////////////////////////////////////////////////////////// +////////////////////////////// Utility functions ////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +//@{ + +function nextNode(node) { + if (node.hasChildNodes()) { + return node.firstChild; + } + return nextNodeDescendants(node); +} + +function previousNode(node) { + if (node.previousSibling) { + node = node.previousSibling; + while (node.hasChildNodes()) { + node = node.lastChild; + } + return node; + } + if (node.parentNode + && node.parentNode.nodeType == Node.ELEMENT_NODE) { + return node.parentNode; + } + return null; +} + +function nextNodeDescendants(node) { + while (node && !node.nextSibling) { + node = node.parentNode; + } + if (!node) { + return null; + } + return node.nextSibling; +} + +/** + * Returns true if ancestor is an ancestor of descendant, false otherwise. + */ +function isAncestor(ancestor, descendant) { + return ancestor + && descendant + && Boolean(ancestor.compareDocumentPosition(descendant) & Node.DOCUMENT_POSITION_CONTAINED_BY); +} + +/** + * Returns true if ancestor is an ancestor of or equal to descendant, false + * otherwise. + */ +function isAncestorContainer(ancestor, descendant) { + return (ancestor || descendant) + && (ancestor == descendant || isAncestor(ancestor, descendant)); +} + +/** + * Returns true if descendant is a descendant of ancestor, false otherwise. + */ +function isDescendant(descendant, ancestor) { + return ancestor + && descendant + && Boolean(ancestor.compareDocumentPosition(descendant) & Node.DOCUMENT_POSITION_CONTAINED_BY); +} + +/** + * Returns true if node1 is before node2 in tree order, false otherwise. + */ +function isBefore(node1, node2) { + return Boolean(node1.compareDocumentPosition(node2) & Node.DOCUMENT_POSITION_FOLLOWING); +} + +/** + * Returns true if node1 is after node2 in tree order, false otherwise. + */ +function isAfter(node1, node2) { + return Boolean(node1.compareDocumentPosition(node2) & Node.DOCUMENT_POSITION_PRECEDING); +} + +function getAncestors(node) { + var ancestors = []; + while (node.parentNode) { + ancestors.unshift(node.parentNode); + node = node.parentNode; + } + return ancestors; +} + +function getInclusiveAncestors(node) { + return getAncestors(node).concat(node); +} + +function getDescendants(node) { + var descendants = []; + var stop = nextNodeDescendants(node); + while ((node = nextNode(node)) + && node != stop) { + descendants.push(node); + } + return descendants; +} + +function getInclusiveDescendants(node) { + return [node].concat(getDescendants(node)); +} + +function convertProperty(property) { + // Special-case for now + var map = { + "fontFamily": "font-family", + "fontSize": "font-size", + "fontStyle": "font-style", + "fontWeight": "font-weight", + "textDecoration": "text-decoration", + }; + if (typeof map[property] != "undefined") { + return map[property]; + } + + return property; +} + +// Return the <font size=X> value for the given CSS size, or undefined if there +// is none. +function cssSizeToLegacy(cssVal) { + return { + "x-small": 1, + "small": 2, + "medium": 3, + "large": 4, + "x-large": 5, + "xx-large": 6, + "xxx-large": 7 + }[cssVal]; +} + +// Return the CSS size given a legacy size. +function legacySizeToCss(legacyVal) { + return { + 1: "x-small", + 2: "small", + 3: "medium", + 4: "large", + 5: "x-large", + 6: "xx-large", + 7: "xxx-large", + }[legacyVal]; +} + +// Opera 11 puts HTML elements in the null namespace, it seems. +function isHtmlNamespace(ns) { + return ns === null + || ns === htmlNamespace; +} + +// "the directionality" from HTML. I don't bother caring about non-HTML +// elements. +// +// "The directionality of an element is either 'ltr' or 'rtl', and is +// determined as per the first appropriate set of steps from the following +// list:" +function getDirectionality(element) { + // "If the element's dir attribute is in the ltr state + // The directionality of the element is 'ltr'." + if (element.dir == "ltr") { + return "ltr"; + } + + // "If the element's dir attribute is in the rtl state + // The directionality of the element is 'rtl'." + if (element.dir == "rtl") { + return "rtl"; + } + + // "If the element's dir attribute is in the auto state + // "If the element is a bdi element and the dir attribute is not in a + // defined state (i.e. it is not present or has an invalid value) + // [lots of complicated stuff] + // + // Skip this, since no browser implements it anyway. + + // "If the element is a root element and the dir attribute is not in a + // defined state (i.e. it is not present or has an invalid value) + // The directionality of the element is 'ltr'." + if (!isHtmlElement(element.parentNode)) { + return "ltr"; + } + + // "If the element has a parent element and the dir attribute is not in a + // defined state (i.e. it is not present or has an invalid value) + // The directionality of the element is the same as the element's + // parent element's directionality." + return getDirectionality(element.parentNode); +} + +//@} + +/////////////////////////////////////////////////////////////////////////////// +///////////////////////////// DOM Range functions ///////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +//@{ + +function getNodeIndex(node) { + var ret = 0; + while (node.previousSibling) { + ret++; + node = node.previousSibling; + } + return ret; +} + +// "The length of a Node node is the following, depending on node: +// +// ProcessingInstruction +// DocumentType +// Always 0. +// Text +// Comment +// node's length. +// Any other node +// node's childNodes's length." +function getNodeLength(node) { + switch (node.nodeType) { + case Node.PROCESSING_INSTRUCTION_NODE: + case Node.DOCUMENT_TYPE_NODE: + return 0; + + case Node.TEXT_NODE: + case Node.COMMENT_NODE: + return node.length; + + default: + return node.childNodes.length; + } +} + +/** + * The position of two boundary points relative to one another, as defined by + * DOM Range. + */ +function getPosition(nodeA, offsetA, nodeB, offsetB) { + // "If node A is the same as node B, return equal if offset A equals offset + // B, before if offset A is less than offset B, and after if offset A is + // greater than offset B." + if (nodeA == nodeB) { + if (offsetA == offsetB) { + return "equal"; + } + if (offsetA < offsetB) { + return "before"; + } + if (offsetA > offsetB) { + return "after"; + } + } + + // "If node A is after node B in tree order, compute the position of (node + // B, offset B) relative to (node A, offset A). If it is before, return + // after. If it is after, return before." + if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_FOLLOWING) { + var pos = getPosition(nodeB, offsetB, nodeA, offsetA); + if (pos == "before") { + return "after"; + } + if (pos == "after") { + return "before"; + } + } + + // "If node A is an ancestor of node B:" + if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_CONTAINS) { + // "Let child equal node B." + var child = nodeB; + + // "While child is not a child of node A, set child to its parent." + while (child.parentNode != nodeA) { + child = child.parentNode; + } + + // "If the index of child is less than offset A, return after." + if (getNodeIndex(child) < offsetA) { + return "after"; + } + } + + // "Return before." + return "before"; +} + +/** + * Returns the furthest ancestor of a Node as defined by DOM Range. + */ +function getFurthestAncestor(node) { + var root = node; + while (root.parentNode != null) { + root = root.parentNode; + } + return root; +} + +/** + * "contained" as defined by DOM Range: "A Node node is contained in a range + * range if node's furthest ancestor is the same as range's root, and (node, 0) + * is after range's start, and (node, length of node) is before range's end." + */ +function isContained(node, range) { + var pos1 = getPosition(node, 0, range.startContainer, range.startOffset); + var pos2 = getPosition(node, getNodeLength(node), range.endContainer, range.endOffset); + + return getFurthestAncestor(node) == getFurthestAncestor(range.startContainer) + && pos1 == "after" + && pos2 == "before"; +} + +/** + * Return all nodes contained in range that the provided function returns true + * for, omitting any with an ancestor already being returned. + */ +function getContainedNodes(range, condition) { + if (typeof condition == "undefined") { + condition = function() { return true }; + } + var node = range.startContainer; + if (node.hasChildNodes() + && range.startOffset < node.childNodes.length) { + // A child is contained + node = node.childNodes[range.startOffset]; + } else if (range.startOffset == getNodeLength(node)) { + // No descendant can be contained + node = nextNodeDescendants(node); + } else { + // No children; this node at least can't be contained + node = nextNode(node); + } + + var stop = range.endContainer; + if (stop.hasChildNodes() + && range.endOffset < stop.childNodes.length) { + // The node after the last contained node is a child + stop = stop.childNodes[range.endOffset]; + } else { + // This node and/or some of its children might be contained + stop = nextNodeDescendants(stop); + } + + var nodeList = []; + while (isBefore(node, stop)) { + if (isContained(node, range) + && condition(node)) { + nodeList.push(node); + node = nextNodeDescendants(node); + continue; + } + node = nextNode(node); + } + return nodeList; +} + +/** + * As above, but includes nodes with an ancestor that's already been returned. + */ +function getAllContainedNodes(range, condition) { + if (typeof condition == "undefined") { + condition = function() { return true }; + } + var node = range.startContainer; + if (node.hasChildNodes() + && range.startOffset < node.childNodes.length) { + // A child is contained + node = node.childNodes[range.startOffset]; + } else if (range.startOffset == getNodeLength(node)) { + // No descendant can be contained + node = nextNodeDescendants(node); + } else { + // No children; this node at least can't be contained + node = nextNode(node); + } + + var stop = range.endContainer; + if (stop.hasChildNodes() + && range.endOffset < stop.childNodes.length) { + // The node after the last contained node is a child + stop = stop.childNodes[range.endOffset]; + } else { + // This node and/or some of its children might be contained + stop = nextNodeDescendants(stop); + } + + var nodeList = []; + while (isBefore(node, stop)) { + if (isContained(node, range) + && condition(node)) { + nodeList.push(node); + } + node = nextNode(node); + } + return nodeList; +} + +// Returns either null, or something of the form rgb(x, y, z), or something of +// the form rgb(x, y, z, w) with w != 0. +function normalizeColor(color) { + if (color.toLowerCase() == "currentcolor") { + return null; + } + + if (normalizeColor.resultCache === undefined) { + normalizeColor.resultCache = {}; + } + + if (normalizeColor.resultCache[color] !== undefined) { + return normalizeColor.resultCache[color]; + } + + var originalColor = color; + + var outerSpan = document.createElement("span"); + document.body.appendChild(outerSpan); + outerSpan.style.color = "black"; + + var innerSpan = document.createElement("span"); + outerSpan.appendChild(innerSpan); + innerSpan.style.color = color; + color = getComputedStyle(innerSpan).color; + + if (color == "rgb(0, 0, 0)") { + // Maybe it's really black, maybe it's invalid. + outerSpan.color = "white"; + color = getComputedStyle(innerSpan).color; + if (color != "rgb(0, 0, 0)") { + return normalizeColor.resultCache[originalColor] = null; + } + } + + document.body.removeChild(outerSpan); + + // I rely on the fact that browsers generally provide consistent syntax for + // getComputedStyle(), although it's not standardized. There are only + // three exceptions I found: + if (/^rgba\([0-9]+, [0-9]+, [0-9]+, 1\)$/.test(color)) { + // IE10PP2 seems to do this sometimes. + return normalizeColor.resultCache[originalColor] = + color.replace("rgba", "rgb").replace(", 1)", ")"); + } + if (color == "transparent") { + // IE10PP2, Firefox 7.0a2, and Opera 11.50 all return "transparent" if + // the specified value is "transparent". + return normalizeColor.resultCache[originalColor] = + "rgba(0, 0, 0, 0)"; + } + // Chrome 15 dev adds way too many significant figures. This isn't a full + // fix, it just fixes one case that comes up in tests. + color = color.replace(/, 0.496094\)$/, ", 0.5)"); + return normalizeColor.resultCache[originalColor] = color; +} + +// Returns either null, or something of the form #xxxxxx. +function parseSimpleColor(color) { + color = normalizeColor(color); + var matches = /^rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)$/.exec(color); + if (matches) { + return "#" + + parseInt(matches[1]).toString(16).replace(/^.$/, "0$&") + + parseInt(matches[2]).toString(16).replace(/^.$/, "0$&") + + parseInt(matches[3]).toString(16).replace(/^.$/, "0$&"); + } + return null; +} + +//@} + +////////////////////////////////////////////////////////////////////////////// +/////////////////////////// Edit command functions /////////////////////////// +////////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////// +///// Methods of the HTMLDocument interface ///// +///////////////////////////////////////////////// +//@{ + +var executionStackDepth = 0; + +// Helper function for common behavior. +function editCommandMethod(command, range, callback) { + // Set up our global range magic, but only if we're the outermost function + if (executionStackDepth == 0 && typeof range != "undefined") { + globalRange = range; + } else if (executionStackDepth == 0) { + globalRange = null; + globalRange = getActiveRange(); + } + + executionStackDepth++; + try { + var ret = callback(); + } catch(e) { + executionStackDepth--; + throw e; + } + executionStackDepth--; + return ret; +} + +function myExecCommand(command, showUi, value, range) { + // "All of these methods must treat their command argument ASCII + // case-insensitively." + command = command.toLowerCase(); + + // "If only one argument was provided, let show UI be false." + // + // If range was passed, I can't actually detect how many args were passed + // . . . + if (arguments.length == 1 + || (arguments.length >=4 && typeof showUi == "undefined")) { + showUi = false; + } + + // "If only one or two arguments were provided, let value be the empty + // string." + if (arguments.length <= 2 + || (arguments.length >=4 && typeof value == "undefined")) { + value = ""; + } + + return editCommandMethod(command, range, (function(command, showUi, value) { return function() { + // "If command is not supported or not enabled, return false." + if (!(command in commands) || !myQueryCommandEnabled(command)) { + return false; + } + + // "Take the action for command, passing value to the instructions as an + // argument." + var ret = commands[command].action(value); + + // Check for bugs + if (ret !== true && ret !== false) { + throw "execCommand() didn't return true or false: " + ret; + } + + // "If the previous step returned false, return false." + if (ret === false) { + return false; + } + + // "Return true." + return true; + }})(command, showUi, value)); +} + +function myQueryCommandEnabled(command, range) { + // "All of these methods must treat their command argument ASCII + // case-insensitively." + command = command.toLowerCase(); + + return editCommandMethod(command, range, (function(command) { return function() { + // "Return true if command is both supported and enabled, false + // otherwise." + if (!(command in commands)) { + return false; + } + + // "Among commands defined in this specification, those listed in + // Miscellaneous commands are always enabled, except for the cut + // command and the paste command. The other commands defined here are + // enabled if the active range is not null, its start node is either + // editable or an editing host, its end node is either editable or an + // editing host, and there is some editing host that is an inclusive + // ancestor of both its start node and its end node." + return ["copy", "defaultparagraphseparator", "selectall", "stylewithcss", + "usecss"].indexOf(command) != -1 + || ( + getActiveRange() !== null + && (isEditable(getActiveRange().startContainer) || isEditingHost(getActiveRange().startContainer)) + && (isEditable(getActiveRange().endContainer) || isEditingHost(getActiveRange().endContainer)) + && (getInclusiveAncestors(getActiveRange().commonAncestorContainer).some(isEditingHost)) + ); + }})(command)); +} + +function myQueryCommandIndeterm(command, range) { + // "All of these methods must treat their command argument ASCII + // case-insensitively." + command = command.toLowerCase(); + + return editCommandMethod(command, range, (function(command) { return function() { + // "If command is not supported or has no indeterminacy, return false." + if (!(command in commands) || !("indeterm" in commands[command])) { + return false; + } + + // "Return true if command is indeterminate, otherwise false." + return commands[command].indeterm(); + }})(command)); +} + +function myQueryCommandState(command, range) { + // "All of these methods must treat their command argument ASCII + // case-insensitively." + command = command.toLowerCase(); + + return editCommandMethod(command, range, (function(command) { return function() { + // "If command is not supported or has no state, return false." + if (!(command in commands) || !("state" in commands[command])) { + return false; + } + + // "If the state override for command is set, return it." + if (typeof getStateOverride(command) != "undefined") { + return getStateOverride(command); + } + + // "Return true if command's state is true, otherwise false." + return commands[command].state(); + }})(command)); +} + +// "When the queryCommandSupported(command) method on the HTMLDocument +// interface is invoked, the user agent must return true if command is +// supported, and false otherwise." +function myQueryCommandSupported(command) { + // "All of these methods must treat their command argument ASCII + // case-insensitively." + command = command.toLowerCase(); + + return command in commands; +} + +function myQueryCommandValue(command, range) { + // "All of these methods must treat their command argument ASCII + // case-insensitively." + command = command.toLowerCase(); + + return editCommandMethod(command, range, function() { + // "If command is not supported or has no value, return the empty string." + if (!(command in commands) || !("value" in commands[command])) { + return ""; + } + + // "If command is "fontSize" and its value override is set, convert the + // value override to an integer number of pixels and return the legacy + // font size for the result." + if (command == "fontsize" + && getValueOverride("fontsize") !== undefined) { + return getLegacyFontSize(getValueOverride("fontsize")); + } + + // "If the value override for command is set, return it." + if (typeof getValueOverride(command) != "undefined") { + return getValueOverride(command); + } + + // "Return command's value." + return commands[command].value(); + }); +} +//@} + +////////////////////////////// +///// Common definitions ///// +////////////////////////////// +//@{ + +// "An HTML element is an Element whose namespace is the HTML namespace." +// +// I allow an extra argument to more easily check whether something is a +// particular HTML element, like isHtmlElement(node, "OL"). It accepts arrays +// too, like isHtmlElement(node, ["OL", "UL"]) to check if it's an ol or ul. +function isHtmlElement(node, tags) { + if (typeof tags == "string") { + tags = [tags]; + } + if (typeof tags == "object") { + tags = tags.map(function(tag) { return tag.toUpperCase() }); + } + return node + && node.nodeType == Node.ELEMENT_NODE + && isHtmlNamespace(node.namespaceURI) + && (typeof tags == "undefined" || tags.indexOf(node.tagName) != -1); +} + +// "A prohibited paragraph child name is "address", "article", "aside", +// "blockquote", "caption", "center", "col", "colgroup", "dd", "details", +// "dir", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer", +// "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "li", +// "listing", "menu", "nav", "ol", "p", "plaintext", "pre", "section", +// "summary", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "ul", or +// "xmp"." +var prohibitedParagraphChildNames = ["address", "article", "aside", + "blockquote", "caption", "center", "col", "colgroup", "dd", "details", + "dir", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer", + "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "li", + "listing", "menu", "nav", "ol", "p", "plaintext", "pre", "section", + "summary", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "ul", + "xmp"]; + +// "A prohibited paragraph child is an HTML element whose local name is a +// prohibited paragraph child name." +function isProhibitedParagraphChild(node) { + return isHtmlElement(node, prohibitedParagraphChildNames); +} + +// "A block node is either an Element whose "display" property does not have +// resolved value "inline" or "inline-block" or "inline-table" or "none", or a +// Document, or a DocumentFragment." +function isBlockNode(node) { + return node + && ((node.nodeType == Node.ELEMENT_NODE && ["inline", "inline-block", "inline-table", "none"].indexOf(getComputedStyle(node).display) == -1) + || node.nodeType == Node.DOCUMENT_NODE + || node.nodeType == Node.DOCUMENT_FRAGMENT_NODE); +} + +// "An inline node is a node that is not a block node." +function isInlineNode(node) { + return node && !isBlockNode(node); +} + +// "An editing host is a node that is either an HTML element with a +// contenteditable attribute set to the true state, or the HTML element child +// of a Document whose designMode is enabled." +function isEditingHost(node) { + return node + && isHtmlElement(node) + && (node.contentEditable == "true" + || (node.parentNode + && node.parentNode.nodeType == Node.DOCUMENT_NODE + && node.parentNode.designMode == "on")); +} + +// "Something is editable if it is a node; it is not an editing host; it does +// not have a contenteditable attribute set to the false state; its parent is +// an editing host or editable; and either it is an HTML element, or it is an +// svg or math element, or it is not an Element and its parent is an HTML +// element." +function isEditable(node) { + return node + && !isEditingHost(node) + && (node.nodeType != Node.ELEMENT_NODE || node.contentEditable != "false") + && (isEditingHost(node.parentNode) || isEditable(node.parentNode)) + && (isHtmlElement(node) + || (node.nodeType == Node.ELEMENT_NODE && node.namespaceURI == "http://www.w3.org/2000/svg" && node.localName == "svg") + || (node.nodeType == Node.ELEMENT_NODE && node.namespaceURI == "http://www.w3.org/1998/Math/MathML" && node.localName == "math") + || (node.nodeType != Node.ELEMENT_NODE && isHtmlElement(node.parentNode))); +} + +// Helper function, not defined in the spec +function hasEditableDescendants(node) { + for (var i = 0; i < node.childNodes.length; i++) { + if (isEditable(node.childNodes[i]) + || hasEditableDescendants(node.childNodes[i])) { + return true; + } + } + return false; +} + +// "The editing host of node is null if node is neither editable nor an editing +// host; node itself, if node is an editing host; or the nearest ancestor of +// node that is an editing host, if node is editable." +function getEditingHostOf(node) { + if (isEditingHost(node)) { + return node; + } else if (isEditable(node)) { + var ancestor = node.parentNode; + while (!isEditingHost(ancestor)) { + ancestor = ancestor.parentNode; + } + return ancestor; + } else { + return null; + } +} + +// "Two nodes are in the same editing host if the editing host of the first is +// non-null and the same as the editing host of the second." +function inSameEditingHost(node1, node2) { + return getEditingHostOf(node1) + && getEditingHostOf(node1) == getEditingHostOf(node2); +} + +// "A collapsed line break is a br that begins a line box which has nothing +// else in it, and therefore has zero height." +function isCollapsedLineBreak(br) { + if (!isHtmlElement(br, "br")) { + return false; + } + + // Add a zwsp after it and see if that changes the height of the nearest + // non-inline parent. Note: this is not actually reliable, because the + // parent might have a fixed height or something. + var ref = br.parentNode; + while (getComputedStyle(ref).display == "inline") { + ref = ref.parentNode; + } + var refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null; + ref.style.height = "auto"; + ref.style.maxHeight = "none"; + ref.style.minHeight = "0"; + var space = document.createTextNode("\u200b"); + var origHeight = ref.offsetHeight; + if (origHeight == 0) { + throw "isCollapsedLineBreak: original height is zero, bug?"; + } + br.parentNode.insertBefore(space, br.nextSibling); + var finalHeight = ref.offsetHeight; + space.parentNode.removeChild(space); + if (refStyle === null) { + // Without the setAttribute() line, removeAttribute() doesn't work in + // Chrome 14 dev. I have no idea why. + ref.setAttribute("style", ""); + ref.removeAttribute("style"); + } else { + ref.setAttribute("style", refStyle); + } + + // Allow some leeway in case the zwsp didn't create a whole new line, but + // only made an existing line slightly higher. Firefox 6.0a2 shows this + // behavior when the first line is bold. + return origHeight < finalHeight - 5; +} + +// "An extraneous line break is a br that has no visual effect, in that +// removing it from the DOM would not change layout, except that a br that is +// the sole child of an li is not extraneous." +// +// FIXME: This doesn't work in IE, since IE ignores display: none in +// contenteditable. +function isExtraneousLineBreak(br) { + if (!isHtmlElement(br, "br")) { + return false; + } + + if (isHtmlElement(br.parentNode, "li") + && br.parentNode.childNodes.length == 1) { + return false; + } + + // Make the line break disappear and see if that changes the block's + // height. Yes, this is an absurd hack. We have to reset height etc. on + // the reference node because otherwise its height won't change if it's not + // auto. + var ref = br.parentNode; + while (getComputedStyle(ref).display == "inline") { + ref = ref.parentNode; + } + var refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null; + ref.style.height = "auto"; + ref.style.maxHeight = "none"; + ref.style.minHeight = "0"; + var brStyle = br.hasAttribute("style") ? br.getAttribute("style") : null; + var origHeight = ref.offsetHeight; + if (origHeight == 0) { + throw "isExtraneousLineBreak: original height is zero, bug?"; + } + br.setAttribute("style", "display:none"); + var finalHeight = ref.offsetHeight; + if (refStyle === null) { + // Without the setAttribute() line, removeAttribute() doesn't work in + // Chrome 14 dev. I have no idea why. + ref.setAttribute("style", ""); + ref.removeAttribute("style"); + } else { + ref.setAttribute("style", refStyle); + } + if (brStyle === null) { + br.removeAttribute("style"); + } else { + br.setAttribute("style", brStyle); + } + + return origHeight == finalHeight; +} + +// "A whitespace node is either a Text node whose data is the empty string; or +// a Text node whose data consists only of one or more tabs (0x0009), line +// feeds (0x000A), carriage returns (0x000D), and/or spaces (0x0020), and whose +// parent is an Element whose resolved value for "white-space" is "normal" or +// "nowrap"; or a Text node whose data consists only of one or more tabs +// (0x0009), carriage returns (0x000D), and/or spaces (0x0020), and whose +// parent is an Element whose resolved value for "white-space" is "pre-line"." +function isWhitespaceNode(node) { + return node + && node.nodeType == Node.TEXT_NODE + && (node.data == "" + || ( + /^[\t\n\r ]+$/.test(node.data) + && node.parentNode + && node.parentNode.nodeType == Node.ELEMENT_NODE + && ["normal", "nowrap"].indexOf(getComputedStyle(node.parentNode).whiteSpace) != -1 + ) || ( + /^[\t\r ]+$/.test(node.data) + && node.parentNode + && node.parentNode.nodeType == Node.ELEMENT_NODE + && getComputedStyle(node.parentNode).whiteSpace == "pre-line" + )); +} + +// "node is a collapsed whitespace node if the following algorithm returns +// true:" +function isCollapsedWhitespaceNode(node) { + // "If node is not a whitespace node, return false." + if (!isWhitespaceNode(node)) { + return false; + } + + // "If node's data is the empty string, return true." + if (node.data == "") { + return true; + } + + // "Let ancestor be node's parent." + var ancestor = node.parentNode; + + // "If ancestor is null, return true." + if (!ancestor) { + return true; + } + + // "If the "display" property of some ancestor of node has resolved value + // "none", return true." + if (getAncestors(node).some(function(ancestor) { + return ancestor.nodeType == Node.ELEMENT_NODE + && getComputedStyle(ancestor).display == "none"; + })) { + return true; + } + + // "While ancestor is not a block node and its parent is not null, set + // ancestor to its parent." + while (!isBlockNode(ancestor) + && ancestor.parentNode) { + ancestor = ancestor.parentNode; + } + + // "Let reference be node." + var reference = node; + + // "While reference is a descendant of ancestor:" + while (reference != ancestor) { + // "Let reference be the node before it in tree order." + reference = previousNode(reference); + + // "If reference is a block node or a br, return true." + if (isBlockNode(reference) + || isHtmlElement(reference, "br")) { + return true; + } + + // "If reference is a Text node that is not a whitespace node, or is an + // img, break from this loop." + if ((reference.nodeType == Node.TEXT_NODE && !isWhitespaceNode(reference)) + || isHtmlElement(reference, "img")) { + break; + } + } + + // "Let reference be node." + reference = node; + + // "While reference is a descendant of ancestor:" + var stop = nextNodeDescendants(ancestor); + while (reference != stop) { + // "Let reference be the node after it in tree order, or null if there + // is no such node." + reference = nextNode(reference); + + // "If reference is a block node or a br, return true." + if (isBlockNode(reference) + || isHtmlElement(reference, "br")) { + return true; + } + + // "If reference is a Text node that is not a whitespace node, or is an + // img, break from this loop." + if ((reference && reference.nodeType == Node.TEXT_NODE && !isWhitespaceNode(reference)) + || isHtmlElement(reference, "img")) { + break; + } + } + + // "Return false." + return false; +} + +// "Something is visible if it is a node that either is a block node, or a Text +// node that is not a collapsed whitespace node, or an img, or a br that is not +// an extraneous line break, or any node with a visible descendant; excluding +// any node with an ancestor container Element whose "display" property has +// resolved value "none"." +function isVisible(node) { + if (!node) { + return false; + } + + if (getAncestors(node).concat(node) + .filter(function(node) { return node.nodeType == Node.ELEMENT_NODE }) + .some(function(node) { return getComputedStyle(node).display == "none" })) { + return false; + } + + if (isBlockNode(node) + || (node.nodeType == Node.TEXT_NODE && !isCollapsedWhitespaceNode(node)) + || isHtmlElement(node, "img") + || (isHtmlElement(node, "br") && !isExtraneousLineBreak(node))) { + return true; + } + + for (var i = 0; i < node.childNodes.length; i++) { + if (isVisible(node.childNodes[i])) { + return true; + } + } + + return false; +} + +// "Something is invisible if it is a node that is not visible." +function isInvisible(node) { + return node && !isVisible(node); +} + +// "A collapsed block prop is either a collapsed line break that is not an +// extraneous line break, or an Element that is an inline node and whose +// children are all either invisible or collapsed block props and that has at +// least one child that is a collapsed block prop." +function isCollapsedBlockProp(node) { + if (isCollapsedLineBreak(node) + && !isExtraneousLineBreak(node)) { + return true; + } + + if (!isInlineNode(node) + || node.nodeType != Node.ELEMENT_NODE) { + return false; + } + + var hasCollapsedBlockPropChild = false; + for (var i = 0; i < node.childNodes.length; i++) { + if (!isInvisible(node.childNodes[i]) + && !isCollapsedBlockProp(node.childNodes[i])) { + return false; + } + if (isCollapsedBlockProp(node.childNodes[i])) { + hasCollapsedBlockPropChild = true; + } + } + + return hasCollapsedBlockPropChild; +} + +// "The active range is the range of the selection given by calling +// getSelection() on the context object. (Thus the active range may be null.)" +// +// We cheat and return globalRange if that's defined. We also ensure that the +// active range meets the requirements that selection boundary points are +// supposed to meet, i.e., that the nodes are both Text or Element nodes that +// descend from a Document. +function getActiveRange() { + var ret; + if (globalRange) { + ret = globalRange; + } else if (getSelection().rangeCount) { + ret = getSelection().getRangeAt(0); + } else { + return null; + } + if ([Node.TEXT_NODE, Node.ELEMENT_NODE].indexOf(ret.startContainer.nodeType) == -1 + || [Node.TEXT_NODE, Node.ELEMENT_NODE].indexOf(ret.endContainer.nodeType) == -1 + || !ret.startContainer.ownerDocument + || !ret.endContainer.ownerDocument + || !isDescendant(ret.startContainer, ret.startContainer.ownerDocument) + || !isDescendant(ret.endContainer, ret.endContainer.ownerDocument)) { + throw "Invalid active range; test bug?"; + } + return ret; +} + +// "For some commands, each HTMLDocument must have a boolean state override +// and/or a string value override. These do not change the command's state or +// value, but change the way some algorithms behave, as specified in those +// algorithms' definitions. Initially, both must be unset for every command. +// Whenever the number of ranges in the Selection changes to something +// different, and whenever a boundary point of the range at a given index in +// the Selection changes to something different, the state override and value +// override must be unset for every command." +// +// We implement this crudely by using setters and getters. To verify that the +// selection hasn't changed, we copy the active range and just check the +// endpoints match. This isn't really correct, but it's good enough for us. +// Unset state/value overrides are undefined. We put everything in a function +// so no one can access anything except via the provided functions, since +// otherwise callers might mistakenly use outdated overrides (if the selection +// has changed). +var getStateOverride, setStateOverride, unsetStateOverride, + getValueOverride, setValueOverride, unsetValueOverride; +(function() { + var stateOverrides = {}; + var valueOverrides = {}; + var storedRange = null; + + function resetOverrides() { + if (!storedRange + || storedRange.startContainer != getActiveRange().startContainer + || storedRange.endContainer != getActiveRange().endContainer + || storedRange.startOffset != getActiveRange().startOffset + || storedRange.endOffset != getActiveRange().endOffset) { + stateOverrides = {}; + valueOverrides = {}; + storedRange = getActiveRange().cloneRange(); + } + } + + getStateOverride = function(command) { + resetOverrides(); + return stateOverrides[command]; + }; + + setStateOverride = function(command, newState) { + resetOverrides(); + stateOverrides[command] = newState; + }; + + unsetStateOverride = function(command) { + resetOverrides(); + delete stateOverrides[command]; + } + + getValueOverride = function(command) { + resetOverrides(); + return valueOverrides[command]; + } + + // "The value override for the backColor command must be the same as the + // value override for the hiliteColor command, such that setting one sets + // the other to the same thing and unsetting one unsets the other." + setValueOverride = function(command, newValue) { + resetOverrides(); + valueOverrides[command] = newValue; + if (command == "backcolor") { + valueOverrides.hilitecolor = newValue; + } else if (command == "hilitecolor") { + valueOverrides.backcolor = newValue; + } + } + + unsetValueOverride = function(command) { + resetOverrides(); + delete valueOverrides[command]; + if (command == "backcolor") { + delete valueOverrides.hilitecolor; + } else if (command == "hilitecolor") { + delete valueOverrides.backcolor; + } + } +})(); + +//@} + +///////////////////////////// +///// Common algorithms ///// +///////////////////////////// + +///// Assorted common algorithms ///// +//@{ + +// Magic array of extra ranges whose endpoints we want to preserve. +var extraRanges = []; + +function movePreservingRanges(node, newParent, newIndex) { + // For convenience, I allow newIndex to be -1 to mean "insert at the end". + if (newIndex == -1) { + newIndex = newParent.childNodes.length; + } + + // "When the user agent is to move a Node to a new location, preserving + // ranges, it must remove the Node from its original parent (if any), then + // insert it in the new location. In doing so, however, it must ignore the + // regular range mutation rules, and instead follow these rules:" + + // "Let node be the moved Node, old parent and old index be the old parent + // (which may be null) and index, and new parent and new index be the new + // parent and index." + var oldParent = node.parentNode; + var oldIndex = getNodeIndex(node); + + // We preserve the global range object, the ranges in the selection, and + // any range that's in the extraRanges array. Any other ranges won't get + // updated, because we have no references to them. + var ranges = [globalRange].concat(extraRanges); + for (var i = 0; i < getSelection().rangeCount; i++) { + ranges.push(getSelection().getRangeAt(i)); + } + var boundaryPoints = []; + ranges.forEach(function(range) { + boundaryPoints.push([range.startContainer, range.startOffset]); + boundaryPoints.push([range.endContainer, range.endOffset]); + }); + + boundaryPoints.forEach(function(boundaryPoint) { + // "If a boundary point's node is the same as or a descendant of node, + // leave it unchanged, so it moves to the new location." + // + // No modifications necessary. + + // "If a boundary point's node is new parent and its offset is greater + // than new index, add one to its offset." + if (boundaryPoint[0] == newParent + && boundaryPoint[1] > newIndex) { + boundaryPoint[1]++; + } + + // "If a boundary point's node is old parent and its offset is old index or + // old index + 1, set its node to new parent and add new index − old index + // to its offset." + if (boundaryPoint[0] == oldParent + && (boundaryPoint[1] == oldIndex + || boundaryPoint[1] == oldIndex + 1)) { + boundaryPoint[0] = newParent; + boundaryPoint[1] += newIndex - oldIndex; + } + + // "If a boundary point's node is old parent and its offset is greater than + // old index + 1, subtract one from its offset." + if (boundaryPoint[0] == oldParent + && boundaryPoint[1] > oldIndex + 1) { + boundaryPoint[1]--; + } + }); + + // Now actually move it and preserve the ranges. + if (newParent.childNodes.length == newIndex) { + newParent.appendChild(node); + } else { + newParent.insertBefore(node, newParent.childNodes[newIndex]); + } + + globalRange.setStart(boundaryPoints[0][0], boundaryPoints[0][1]); + globalRange.setEnd(boundaryPoints[1][0], boundaryPoints[1][1]); + + for (var i = 0; i < extraRanges.length; i++) { + extraRanges[i].setStart(boundaryPoints[2*i + 2][0], boundaryPoints[2*i + 2][1]); + extraRanges[i].setEnd(boundaryPoints[2*i + 3][0], boundaryPoints[2*i + 3][1]); + } + + getSelection().removeAllRanges(); + for (var i = 1 + extraRanges.length; i < ranges.length; i++) { + var newRange = document.createRange(); + newRange.setStart(boundaryPoints[2*i][0], boundaryPoints[2*i][1]); + newRange.setEnd(boundaryPoints[2*i + 1][0], boundaryPoints[2*i + 1][1]); + getSelection().addRange(newRange); + } +} + +function setTagName(element, newName) { + // "If element is an HTML element with local name equal to new name, return + // element." + if (isHtmlElement(element, newName.toUpperCase())) { + return element; + } + + // "If element's parent is null, return element." + if (!element.parentNode) { + return element; + } + + // "Let replacement element be the result of calling createElement(new + // name) on the ownerDocument of element." + var replacementElement = element.ownerDocument.createElement(newName); + + // "Insert replacement element into element's parent immediately before + // element." + element.parentNode.insertBefore(replacementElement, element); + + // "Copy all attributes of element to replacement element, in order." + for (var i = 0; i < element.attributes.length; i++) { + replacementElement.setAttributeNS(element.attributes[i].namespaceURI, element.attributes[i].name, element.attributes[i].value); + } + + // "While element has children, append the first child of element as the + // last child of replacement element, preserving ranges." + while (element.childNodes.length) { + movePreservingRanges(element.firstChild, replacementElement, replacementElement.childNodes.length); + } + + // "Remove element from its parent." + element.parentNode.removeChild(element); + + // "Return replacement element." + return replacementElement; +} + +function removeExtraneousLineBreaksBefore(node) { + // "Let ref be the previousSibling of node." + var ref = node.previousSibling; + + // "If ref is null, abort these steps." + if (!ref) { + return; + } + + // "While ref has children, set ref to its lastChild." + while (ref.hasChildNodes()) { + ref = ref.lastChild; + } + + // "While ref is invisible but not an extraneous line break, and ref does + // not equal node's parent, set ref to the node before it in tree order." + while (isInvisible(ref) + && !isExtraneousLineBreak(ref) + && ref != node.parentNode) { + ref = previousNode(ref); + } + + // "If ref is an editable extraneous line break, remove it from its + // parent." + if (isEditable(ref) + && isExtraneousLineBreak(ref)) { + ref.parentNode.removeChild(ref); + } +} + +function removeExtraneousLineBreaksAtTheEndOf(node) { + // "Let ref be node." + var ref = node; + + // "While ref has children, set ref to its lastChild." + while (ref.hasChildNodes()) { + ref = ref.lastChild; + } + + // "While ref is invisible but not an extraneous line break, and ref does + // not equal node, set ref to the node before it in tree order." + while (isInvisible(ref) + && !isExtraneousLineBreak(ref) + && ref != node) { + ref = previousNode(ref); + } + + // "If ref is an editable extraneous line break:" + if (isEditable(ref) + && isExtraneousLineBreak(ref)) { + // "While ref's parent is editable and invisible, set ref to its + // parent." + while (isEditable(ref.parentNode) + && isInvisible(ref.parentNode)) { + ref = ref.parentNode; + } + + // "Remove ref from its parent." + ref.parentNode.removeChild(ref); + } +} + +// "To remove extraneous line breaks from a node, first remove extraneous line +// breaks before it, then remove extraneous line breaks at the end of it." +function removeExtraneousLineBreaksFrom(node) { + removeExtraneousLineBreaksBefore(node); + removeExtraneousLineBreaksAtTheEndOf(node); +} + +//@} +///// Wrapping a list of nodes ///// +//@{ + +function wrap(nodeList, siblingCriteria, newParentInstructions) { + // "If not provided, sibling criteria returns false and new parent + // instructions returns null." + if (typeof siblingCriteria == "undefined") { + siblingCriteria = function() { return false }; + } + if (typeof newParentInstructions == "undefined") { + newParentInstructions = function() { return null }; + } + + // "If every member of node list is invisible, and none is a br, return + // null and abort these steps." + if (nodeList.every(isInvisible) + && !nodeList.some(function(node) { return isHtmlElement(node, "br") })) { + return null; + } + + // "If node list's first member's parent is null, return null and abort + // these steps." + if (!nodeList[0].parentNode) { + return null; + } + + // "If node list's last member is an inline node that's not a br, and node + // list's last member's nextSibling is a br, append that br to node list." + if (isInlineNode(nodeList[nodeList.length - 1]) + && !isHtmlElement(nodeList[nodeList.length - 1], "br") + && isHtmlElement(nodeList[nodeList.length - 1].nextSibling, "br")) { + nodeList.push(nodeList[nodeList.length - 1].nextSibling); + } + + // "While node list's first member's previousSibling is invisible, prepend + // it to node list." + while (isInvisible(nodeList[0].previousSibling)) { + nodeList.unshift(nodeList[0].previousSibling); + } + + // "While node list's last member's nextSibling is invisible, append it to + // node list." + while (isInvisible(nodeList[nodeList.length - 1].nextSibling)) { + nodeList.push(nodeList[nodeList.length - 1].nextSibling); + } + + // "If the previousSibling of the first member of node list is editable and + // running sibling criteria on it returns true, let new parent be the + // previousSibling of the first member of node list." + var newParent; + if (isEditable(nodeList[0].previousSibling) + && siblingCriteria(nodeList[0].previousSibling)) { + newParent = nodeList[0].previousSibling; + + // "Otherwise, if the nextSibling of the last member of node list is + // editable and running sibling criteria on it returns true, let new parent + // be the nextSibling of the last member of node list." + } else if (isEditable(nodeList[nodeList.length - 1].nextSibling) + && siblingCriteria(nodeList[nodeList.length - 1].nextSibling)) { + newParent = nodeList[nodeList.length - 1].nextSibling; + + // "Otherwise, run new parent instructions, and let new parent be the + // result." + } else { + newParent = newParentInstructions(); + } + + // "If new parent is null, abort these steps and return null." + if (!newParent) { + return null; + } + + // "If new parent's parent is null:" + if (!newParent.parentNode) { + // "Insert new parent into the parent of the first member of node list + // immediately before the first member of node list." + nodeList[0].parentNode.insertBefore(newParent, nodeList[0]); + + // "If any range has a boundary point with node equal to the parent of + // new parent and offset equal to the index of new parent, add one to + // that boundary point's offset." + // + // Only try to fix the global range. + if (globalRange.startContainer == newParent.parentNode + && globalRange.startOffset == getNodeIndex(newParent)) { + globalRange.setStart(globalRange.startContainer, globalRange.startOffset + 1); + } + if (globalRange.endContainer == newParent.parentNode + && globalRange.endOffset == getNodeIndex(newParent)) { + globalRange.setEnd(globalRange.endContainer, globalRange.endOffset + 1); + } + } + + // "Let original parent be the parent of the first member of node list." + var originalParent = nodeList[0].parentNode; + + // "If new parent is before the first member of node list in tree order:" + if (isBefore(newParent, nodeList[0])) { + // "If new parent is not an inline node, but the last visible child of + // new parent and the first visible member of node list are both inline + // nodes, and the last child of new parent is not a br, call + // createElement("br") on the ownerDocument of new parent and append + // the result as the last child of new parent." + if (!isInlineNode(newParent) + && isInlineNode([].filter.call(newParent.childNodes, isVisible).slice(-1)[0]) + && isInlineNode(nodeList.filter(isVisible)[0]) + && !isHtmlElement(newParent.lastChild, "BR")) { + newParent.appendChild(newParent.ownerDocument.createElement("br")); + } + + // "For each node in node list, append node as the last child of new + // parent, preserving ranges." + for (var i = 0; i < nodeList.length; i++) { + movePreservingRanges(nodeList[i], newParent, -1); + } + + // "Otherwise:" + } else { + // "If new parent is not an inline node, but the first visible child of + // new parent and the last visible member of node list are both inline + // nodes, and the last member of node list is not a br, call + // createElement("br") on the ownerDocument of new parent and insert + // the result as the first child of new parent." + if (!isInlineNode(newParent) + && isInlineNode([].filter.call(newParent.childNodes, isVisible)[0]) + && isInlineNode(nodeList.filter(isVisible).slice(-1)[0]) + && !isHtmlElement(nodeList[nodeList.length - 1], "BR")) { + newParent.insertBefore(newParent.ownerDocument.createElement("br"), newParent.firstChild); + } + + // "For each node in node list, in reverse order, insert node as the + // first child of new parent, preserving ranges." + for (var i = nodeList.length - 1; i >= 0; i--) { + movePreservingRanges(nodeList[i], newParent, 0); + } + } + + // "If original parent is editable and has no children, remove it from its + // parent." + if (isEditable(originalParent) && !originalParent.hasChildNodes()) { + originalParent.parentNode.removeChild(originalParent); + } + + // "If new parent's nextSibling is editable and running sibling criteria on + // it returns true:" + if (isEditable(newParent.nextSibling) + && siblingCriteria(newParent.nextSibling)) { + // "If new parent is not an inline node, but new parent's last child + // and new parent's nextSibling's first child are both inline nodes, + // and new parent's last child is not a br, call createElement("br") on + // the ownerDocument of new parent and append the result as the last + // child of new parent." + if (!isInlineNode(newParent) + && isInlineNode(newParent.lastChild) + && isInlineNode(newParent.nextSibling.firstChild) + && !isHtmlElement(newParent.lastChild, "BR")) { + newParent.appendChild(newParent.ownerDocument.createElement("br")); + } + + // "While new parent's nextSibling has children, append its first child + // as the last child of new parent, preserving ranges." + while (newParent.nextSibling.hasChildNodes()) { + movePreservingRanges(newParent.nextSibling.firstChild, newParent, -1); + } + + // "Remove new parent's nextSibling from its parent." + newParent.parentNode.removeChild(newParent.nextSibling); + } + + // "Remove extraneous line breaks from new parent." + removeExtraneousLineBreaksFrom(newParent); + + // "Return new parent." + return newParent; +} + + +//@} +///// Allowed children ///// +//@{ + +// "A name of an element with inline contents is "a", "abbr", "b", "bdi", +// "bdo", "cite", "code", "dfn", "em", "h1", "h2", "h3", "h4", "h5", "h6", "i", +// "kbd", "mark", "p", "pre", "q", "rp", "rt", "ruby", "s", "samp", "small", +// "span", "strong", "sub", "sup", "u", "var", "acronym", "listing", "strike", +// "xmp", "big", "blink", "font", "marquee", "nobr", or "tt"." +var namesOfElementsWithInlineContents = ["a", "abbr", "b", "bdi", "bdo", + "cite", "code", "dfn", "em", "h1", "h2", "h3", "h4", "h5", "h6", "i", + "kbd", "mark", "p", "pre", "q", "rp", "rt", "ruby", "s", "samp", "small", + "span", "strong", "sub", "sup", "u", "var", "acronym", "listing", "strike", + "xmp", "big", "blink", "font", "marquee", "nobr", "tt"]; + +// "An element with inline contents is an HTML element whose local name is a +// name of an element with inline contents." +function isElementWithInlineContents(node) { + return isHtmlElement(node, namesOfElementsWithInlineContents); +} + +function isAllowedChild(child, parent_) { + // "If parent is "colgroup", "table", "tbody", "tfoot", "thead", "tr", or + // an HTML element with local name equal to one of those, and child is a + // Text node whose data does not consist solely of space characters, return + // false." + if ((["colgroup", "table", "tbody", "tfoot", "thead", "tr"].indexOf(parent_) != -1 + || isHtmlElement(parent_, ["colgroup", "table", "tbody", "tfoot", "thead", "tr"])) + && typeof child == "object" + && child.nodeType == Node.TEXT_NODE + && !/^[ \t\n\f\r]*$/.test(child.data)) { + return false; + } + + // "If parent is "script", "style", "plaintext", or "xmp", or an HTML + // element with local name equal to one of those, and child is not a Text + // node, return false." + if ((["script", "style", "plaintext", "xmp"].indexOf(parent_) != -1 + || isHtmlElement(parent_, ["script", "style", "plaintext", "xmp"])) + && (typeof child != "object" || child.nodeType != Node.TEXT_NODE)) { + return false; + } + + // "If child is a Document, DocumentFragment, or DocumentType, return + // false." + if (typeof child == "object" + && (child.nodeType == Node.DOCUMENT_NODE + || child.nodeType == Node.DOCUMENT_FRAGMENT_NODE + || child.nodeType == Node.DOCUMENT_TYPE_NODE)) { + return false; + } + + // "If child is an HTML element, set child to the local name of child." + if (isHtmlElement(child)) { + child = child.tagName.toLowerCase(); + } + + // "If child is not a string, return true." + if (typeof child != "string") { + return true; + } + + // "If parent is an HTML element:" + if (isHtmlElement(parent_)) { + // "If child is "a", and parent or some ancestor of parent is an a, + // return false." + // + // "If child is a prohibited paragraph child name and parent or some + // ancestor of parent is an element with inline contents, return + // false." + // + // "If child is "h1", "h2", "h3", "h4", "h5", or "h6", and parent or + // some ancestor of parent is an HTML element with local name "h1", + // "h2", "h3", "h4", "h5", or "h6", return false." + var ancestor = parent_; + while (ancestor) { + if (child == "a" && isHtmlElement(ancestor, "a")) { + return false; + } + if (prohibitedParagraphChildNames.indexOf(child) != -1 + && isElementWithInlineContents(ancestor)) { + return false; + } + if (/^h[1-6]$/.test(child) + && isHtmlElement(ancestor) + && /^H[1-6]$/.test(ancestor.tagName)) { + return false; + } + ancestor = ancestor.parentNode; + } + + // "Let parent be the local name of parent." + parent_ = parent_.tagName.toLowerCase(); + } + + // "If parent is an Element or DocumentFragment, return true." + if (typeof parent_ == "object" + && (parent_.nodeType == Node.ELEMENT_NODE + || parent_.nodeType == Node.DOCUMENT_FRAGMENT_NODE)) { + return true; + } + + // "If parent is not a string, return false." + if (typeof parent_ != "string") { + return false; + } + + // "If parent is on the left-hand side of an entry on the following list, + // then return true if child is listed on the right-hand side of that + // entry, and false otherwise." + switch (parent_) { + case "colgroup": + return child == "col"; + case "table": + return ["caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"].indexOf(child) != -1; + case "tbody": + case "thead": + case "tfoot": + return ["td", "th", "tr"].indexOf(child) != -1; + case "tr": + return ["td", "th"].indexOf(child) != -1; + case "dl": + return ["dt", "dd"].indexOf(child) != -1; + case "dir": + case "ol": + case "ul": + return ["dir", "li", "ol", "ul"].indexOf(child) != -1; + case "hgroup": + return /^h[1-6]$/.test(child); + } + + // "If child is "body", "caption", "col", "colgroup", "frame", "frameset", + // "head", "html", "tbody", "td", "tfoot", "th", "thead", or "tr", return + // false." + if (["body", "caption", "col", "colgroup", "frame", "frameset", "head", + "html", "tbody", "td", "tfoot", "th", "thead", "tr"].indexOf(child) != -1) { + return false; + } + + // "If child is "dd" or "dt" and parent is not "dl", return false." + if (["dd", "dt"].indexOf(child) != -1 + && parent_ != "dl") { + return false; + } + + // "If child is "li" and parent is not "ol" or "ul", return false." + if (child == "li" + && parent_ != "ol" + && parent_ != "ul") { + return false; + } + + // "If parent is on the left-hand side of an entry on the following list + // and child is listed on the right-hand side of that entry, return false." + var table = [ + [["a"], ["a"]], + [["dd", "dt"], ["dd", "dt"]], + [["h1", "h2", "h3", "h4", "h5", "h6"], ["h1", "h2", "h3", "h4", "h5", "h6"]], + [["li"], ["li"]], + [["nobr"], ["nobr"]], + [namesOfElementsWithInlineContents, prohibitedParagraphChildNames], + [["td", "th"], ["caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"]], + ]; + for (var i = 0; i < table.length; i++) { + if (table[i][0].indexOf(parent_) != -1 + && table[i][1].indexOf(child) != -1) { + return false; + } + } + + // "Return true." + return true; +} + + +//@} + +////////////////////////////////////// +///// Inline formatting commands ///// +////////////////////////////////////// + +///// Inline formatting command definitions ///// +//@{ + +// "A node node is effectively contained in a range range if range is not +// collapsed, and at least one of the following holds:" +function isEffectivelyContained(node, range) { + if (range.collapsed) { + return false; + } + + // "node is contained in range." + if (isContained(node, range)) { + return true; + } + + // "node is range's start node, it is a Text node, and its length is + // different from range's start offset." + if (node == range.startContainer + && node.nodeType == Node.TEXT_NODE + && getNodeLength(node) != range.startOffset) { + return true; + } + + // "node is range's end node, it is a Text node, and range's end offset is + // not 0." + if (node == range.endContainer + && node.nodeType == Node.TEXT_NODE + && range.endOffset != 0) { + return true; + } + + // "node has at least one child; and all its children are effectively + // contained in range; and either range's start node is not a descendant of + // node or is not a Text node or range's start offset is zero; and either + // range's end node is not a descendant of node or is not a Text node or + // range's end offset is its end node's length." + if (node.hasChildNodes() + && [].every.call(node.childNodes, function(child) { return isEffectivelyContained(child, range) }) + && (!isDescendant(range.startContainer, node) + || range.startContainer.nodeType != Node.TEXT_NODE + || range.startOffset == 0) + && (!isDescendant(range.endContainer, node) + || range.endContainer.nodeType != Node.TEXT_NODE + || range.endOffset == getNodeLength(range.endContainer))) { + return true; + } + + return false; +} + +// Like get(All)ContainedNodes(), but for effectively contained nodes. +function getEffectivelyContainedNodes(range, condition) { + if (typeof condition == "undefined") { + condition = function() { return true }; + } + var node = range.startContainer; + while (isEffectivelyContained(node.parentNode, range)) { + node = node.parentNode; + } + + var stop = nextNodeDescendants(range.endContainer); + + var nodeList = []; + while (isBefore(node, stop)) { + if (isEffectivelyContained(node, range) + && condition(node)) { + nodeList.push(node); + node = nextNodeDescendants(node); + continue; + } + node = nextNode(node); + } + return nodeList; +} + +function getAllEffectivelyContainedNodes(range, condition) { + if (typeof condition == "undefined") { + condition = function() { return true }; + } + var node = range.startContainer; + while (isEffectivelyContained(node.parentNode, range)) { + node = node.parentNode; + } + + var stop = nextNodeDescendants(range.endContainer); + + var nodeList = []; + while (isBefore(node, stop)) { + if (isEffectivelyContained(node, range) + && condition(node)) { + nodeList.push(node); + } + node = nextNode(node); + } + return nodeList; +} + +// "A modifiable element is a b, em, i, s, span, strong, sub, sup, or u element +// with no attributes except possibly style; or a font element with no +// attributes except possibly style, color, face, and/or size; or an a element +// with no attributes except possibly style and/or href." +function isModifiableElement(node) { + if (!isHtmlElement(node)) { + return false; + } + + if (["B", "EM", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"].indexOf(node.tagName) != -1) { + if (node.attributes.length == 0) { + return true; + } + + if (node.attributes.length == 1 + && node.hasAttribute("style")) { + return true; + } + } + + if (node.tagName == "FONT" || node.tagName == "A") { + var numAttrs = node.attributes.length; + + if (node.hasAttribute("style")) { + numAttrs--; + } + + if (node.tagName == "FONT") { + if (node.hasAttribute("color")) { + numAttrs--; + } + + if (node.hasAttribute("face")) { + numAttrs--; + } + + if (node.hasAttribute("size")) { + numAttrs--; + } + } + + if (node.tagName == "A" + && node.hasAttribute("href")) { + numAttrs--; + } + + if (numAttrs == 0) { + return true; + } + } + + return false; +} + +function isSimpleModifiableElement(node) { + // "A simple modifiable element is an HTML element for which at least one + // of the following holds:" + if (!isHtmlElement(node)) { + return false; + } + + // Only these elements can possibly be a simple modifiable element. + if (["A", "B", "EM", "FONT", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"].indexOf(node.tagName) == -1) { + return false; + } + + // "It is an a, b, em, font, i, s, span, strike, strong, sub, sup, or u + // element with no attributes." + if (node.attributes.length == 0) { + return true; + } + + // If it's got more than one attribute, everything after this fails. + if (node.attributes.length > 1) { + return false; + } + + // "It is an a, b, em, font, i, s, span, strike, strong, sub, sup, or u + // element with exactly one attribute, which is style, which sets no CSS + // properties (including invalid or unrecognized properties)." + // + // Not gonna try for invalid or unrecognized. + if (node.hasAttribute("style") + && node.style.length == 0) { + return true; + } + + // "It is an a element with exactly one attribute, which is href." + if (node.tagName == "A" + && node.hasAttribute("href")) { + return true; + } + + // "It is a font element with exactly one attribute, which is either color, + // face, or size." + if (node.tagName == "FONT" + && (node.hasAttribute("color") + || node.hasAttribute("face") + || node.hasAttribute("size") + )) { + return true; + } + + // "It is a b or strong element with exactly one attribute, which is style, + // and the style attribute sets exactly one CSS property (including invalid + // or unrecognized properties), which is "font-weight"." + if ((node.tagName == "B" || node.tagName == "STRONG") + && node.hasAttribute("style") + && node.style.length == 1 + && node.style.fontWeight != "") { + return true; + } + + // "It is an i or em element with exactly one attribute, which is style, + // and the style attribute sets exactly one CSS property (including invalid + // or unrecognized properties), which is "font-style"." + if ((node.tagName == "I" || node.tagName == "EM") + && node.hasAttribute("style") + && node.style.length == 1 + && node.style.fontStyle != "") { + return true; + } + + // "It is an a, font, or span element with exactly one attribute, which is + // style, and the style attribute sets exactly one CSS property (including + // invalid or unrecognized properties), and that property is not + // "text-decoration"." + if ((node.tagName == "A" || node.tagName == "FONT" || node.tagName == "SPAN") + && node.hasAttribute("style") + && node.style.length == 1 + && node.style.textDecoration == "") { + return true; + } + + // "It is an a, font, s, span, strike, or u element with exactly one + // attribute, which is style, and the style attribute sets exactly one CSS + // property (including invalid or unrecognized properties), which is + // "text-decoration", which is set to "line-through" or "underline" or + // "overline" or "none"." + // + // The weird extra node.style.length check is for Firefox, which as of + // 8.0a2 has annoying and weird behavior here. + if (["A", "FONT", "S", "SPAN", "STRIKE", "U"].indexOf(node.tagName) != -1 + && node.hasAttribute("style") + && (node.style.length == 1 + || (node.style.length == 4 + && "MozTextBlink" in node.style + && "MozTextDecorationColor" in node.style + && "MozTextDecorationLine" in node.style + && "MozTextDecorationStyle" in node.style) + || (node.style.length == 4 + && "MozTextBlink" in node.style + && "textDecorationColor" in node.style + && "textDecorationLine" in node.style + && "textDecorationStyle" in node.style) + ) + && (node.style.textDecoration == "line-through" + || node.style.textDecoration == "underline" + || node.style.textDecoration == "overline" + || node.style.textDecoration == "none")) { + return true; + } + + return false; +} + +// "A formattable node is an editable visible node that is either a Text node, +// an img, or a br." +function isFormattableNode(node) { + return isEditable(node) + && isVisible(node) + && (node.nodeType == Node.TEXT_NODE + || isHtmlElement(node, ["img", "br"])); +} + +// "Two quantities are equivalent values for a command if either both are null, +// or both are strings and they're equal and the command does not define any +// equivalent values, or both are strings and the command defines equivalent +// values and they match the definition." +function areEquivalentValues(command, val1, val2) { + if (val1 === null && val2 === null) { + return true; + } + + if (typeof val1 == "string" + && typeof val2 == "string" + && val1 == val2 + && !("equivalentValues" in commands[command])) { + return true; + } + + if (typeof val1 == "string" + && typeof val2 == "string" + && "equivalentValues" in commands[command] + && commands[command].equivalentValues(val1, val2)) { + return true; + } + + return false; +} + +// "Two quantities are loosely equivalent values for a command if either they +// are equivalent values for the command, or if the command is the fontSize +// command; one of the quantities is one of "x-small", "small", "medium", +// "large", "x-large", "xx-large", or "xxx-large"; and the other quantity is +// the resolved value of "font-size" on a font element whose size attribute has +// the corresponding value set ("1" through "7" respectively)." +function areLooselyEquivalentValues(command, val1, val2) { + if (areEquivalentValues(command, val1, val2)) { + return true; + } + + if (command != "fontsize" + || typeof val1 != "string" + || typeof val2 != "string") { + return false; + } + + // Static variables in JavaScript? + var callee = areLooselyEquivalentValues; + if (callee.sizeMap === undefined) { + callee.sizeMap = {}; + var font = document.createElement("font"); + document.body.appendChild(font); + ["x-small", "small", "medium", "large", "x-large", "xx-large", + "xxx-large"].forEach(function(keyword) { + font.size = cssSizeToLegacy(keyword); + callee.sizeMap[keyword] = getComputedStyle(font).fontSize; + }); + document.body.removeChild(font); + } + + return val1 === callee.sizeMap[val2] + || val2 === callee.sizeMap[val1]; +} + +//@} +///// Assorted inline formatting command algorithms ///// +//@{ + +function getEffectiveCommandValue(node, command) { + // "If neither node nor its parent is an Element, return null." + if (node.nodeType != Node.ELEMENT_NODE + && (!node.parentNode || node.parentNode.nodeType != Node.ELEMENT_NODE)) { + return null; + } + + // "If node is not an Element, return the effective command value of its + // parent for command." + if (node.nodeType != Node.ELEMENT_NODE) { + return getEffectiveCommandValue(node.parentNode, command); + } + + // "If command is "createLink" or "unlink":" + if (command == "createlink" || command == "unlink") { + // "While node is not null, and is not an a element that has an href + // attribute, set node to its parent." + while (node + && (!isHtmlElement(node) + || node.tagName != "A" + || !node.hasAttribute("href"))) { + node = node.parentNode; + } + + // "If node is null, return null." + if (!node) { + return null; + } + + // "Return the value of node's href attribute." + return node.getAttribute("href"); + } + + // "If command is "backColor" or "hiliteColor":" + if (command == "backcolor" + || command == "hilitecolor") { + // "While the resolved value of "background-color" on node is any + // fully transparent value, and node's parent is an Element, set + // node to its parent." + // + // Another lame hack to avoid flawed APIs. + while ((getComputedStyle(node).backgroundColor == "rgba(0, 0, 0, 0)" + || getComputedStyle(node).backgroundColor === "" + || getComputedStyle(node).backgroundColor == "transparent") + && node.parentNode + && node.parentNode.nodeType == Node.ELEMENT_NODE) { + node = node.parentNode; + } + + // "Return the resolved value of "background-color" for node." + return getComputedStyle(node).backgroundColor; + } + + // "If command is "subscript" or "superscript":" + if (command == "subscript" || command == "superscript") { + // "Let affected by subscript and affected by superscript be two + // boolean variables, both initially false." + var affectedBySubscript = false; + var affectedBySuperscript = false; + + // "While node is an inline node:" + while (isInlineNode(node)) { + var verticalAlign = getComputedStyle(node).verticalAlign; + + // "If node is a sub, set affected by subscript to true." + if (isHtmlElement(node, "sub")) { + affectedBySubscript = true; + // "Otherwise, if node is a sup, set affected by superscript to + // true." + } else if (isHtmlElement(node, "sup")) { + affectedBySuperscript = true; + } + + // "Set node to its parent." + node = node.parentNode; + } + + // "If affected by subscript and affected by superscript are both true, + // return the string "mixed"." + if (affectedBySubscript && affectedBySuperscript) { + return "mixed"; + } + + // "If affected by subscript is true, return "subscript"." + if (affectedBySubscript) { + return "subscript"; + } + + // "If affected by superscript is true, return "superscript"." + if (affectedBySuperscript) { + return "superscript"; + } + + // "Return null." + return null; + } + + // "If command is "strikethrough", and the "text-decoration" property of + // node or any of its ancestors has resolved value containing + // "line-through", return "line-through". Otherwise, return null." + if (command == "strikethrough") { + do { + if (getComputedStyle(node).textDecoration.indexOf("line-through") != -1) { + return "line-through"; + } + node = node.parentNode; + } while (node && node.nodeType == Node.ELEMENT_NODE); + return null; + } + + // "If command is "underline", and the "text-decoration" property of node + // or any of its ancestors has resolved value containing "underline", + // return "underline". Otherwise, return null." + if (command == "underline") { + do { + if (getComputedStyle(node).textDecoration.indexOf("underline") != -1) { + return "underline"; + } + node = node.parentNode; + } while (node && node.nodeType == Node.ELEMENT_NODE); + return null; + } + + if (!("relevantCssProperty" in commands[command])) { + throw "Bug: no relevantCssProperty for " + command + " in getEffectiveCommandValue"; + } + + // "Return the resolved value for node of the relevant CSS property for + // command." + return getComputedStyle(node)[commands[command].relevantCssProperty]; +} + +function getSpecifiedCommandValue(element, command) { + // "If command is "backColor" or "hiliteColor" and element's display + // property does not have resolved value "inline", return null." + if ((command == "backcolor" || command == "hilitecolor") + && getComputedStyle(element).display != "inline") { + return null; + } + + // "If command is "createLink" or "unlink":" + if (command == "createlink" || command == "unlink") { + // "If element is an a element and has an href attribute, return the + // value of that attribute." + if (isHtmlElement(element) + && element.tagName == "A" + && element.hasAttribute("href")) { + return element.getAttribute("href"); + } + + // "Return null." + return null; + } + + // "If command is "subscript" or "superscript":" + if (command == "subscript" || command == "superscript") { + // "If element is a sup, return "superscript"." + if (isHtmlElement(element, "sup")) { + return "superscript"; + } + + // "If element is a sub, return "subscript"." + if (isHtmlElement(element, "sub")) { + return "subscript"; + } + + // "Return null." + return null; + } + + // "If command is "strikethrough", and element has a style attribute set, + // and that attribute sets "text-decoration":" + if (command == "strikethrough" + && element.style.textDecoration != "") { + // "If element's style attribute sets "text-decoration" to a value + // containing "line-through", return "line-through"." + if (element.style.textDecoration.indexOf("line-through") != -1) { + return "line-through"; + } + + // "Return null." + return null; + } + + // "If command is "strikethrough" and element is a s or strike element, + // return "line-through"." + if (command == "strikethrough" + && isHtmlElement(element, ["S", "STRIKE"])) { + return "line-through"; + } + + // "If command is "underline", and element has a style attribute set, and + // that attribute sets "text-decoration":" + if (command == "underline" + && element.style.textDecoration != "") { + // "If element's style attribute sets "text-decoration" to a value + // containing "underline", return "underline"." + if (element.style.textDecoration.indexOf("underline") != -1) { + return "underline"; + } + + // "Return null." + return null; + } + + // "If command is "underline" and element is a u element, return + // "underline"." + if (command == "underline" + && isHtmlElement(element, "U")) { + return "underline"; + } + + // "Let property be the relevant CSS property for command." + var property = commands[command].relevantCssProperty; + + // "If property is null, return null." + if (property === null) { + return null; + } + + // "If element has a style attribute set, and that attribute has the + // effect of setting property, return the value that it sets property to." + if (element.style[property] != "") { + return element.style[property]; + } + + // "If element is a font element that has an attribute whose effect is + // to create a presentational hint for property, return the value that the + // hint sets property to. (For a size of 7, this will be the non-CSS value + // "xxx-large".)" + if (isHtmlNamespace(element.namespaceURI) + && element.tagName == "FONT") { + if (property == "color" && element.hasAttribute("color")) { + return element.color; + } + if (property == "fontFamily" && element.hasAttribute("face")) { + return element.face; + } + if (property == "fontSize" && element.hasAttribute("size")) { + // This is not even close to correct in general. + var size = parseInt(element.size); + if (size < 1) { + size = 1; + } + if (size > 7) { + size = 7; + } + return { + 1: "x-small", + 2: "small", + 3: "medium", + 4: "large", + 5: "x-large", + 6: "xx-large", + 7: "xxx-large" + }[size]; + } + } + + // "If element is in the following list, and property is equal to the + // CSS property name listed for it, return the string listed for it." + // + // A list follows, whose meaning is copied here. + if (property == "fontWeight" + && (element.tagName == "B" || element.tagName == "STRONG")) { + return "bold"; + } + if (property == "fontStyle" + && (element.tagName == "I" || element.tagName == "EM")) { + return "italic"; + } + + // "Return null." + return null; +} + +function reorderModifiableDescendants(node, command, newValue) { + // "Let candidate equal node." + var candidate = node; + + // "While candidate is a modifiable element, and candidate has exactly one + // child, and that child is also a modifiable element, and candidate is not + // a simple modifiable element or candidate's specified command value for + // command is not equivalent to new value, set candidate to its child." + while (isModifiableElement(candidate) + && candidate.childNodes.length == 1 + && isModifiableElement(candidate.firstChild) + && (!isSimpleModifiableElement(candidate) + || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue))) { + candidate = candidate.firstChild; + } + + // "If candidate is node, or is not a simple modifiable element, or its + // specified command value is not equivalent to new value, or its effective + // command value is not loosely equivalent to new value, abort these + // steps." + if (candidate == node + || !isSimpleModifiableElement(candidate) + || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue) + || !areLooselyEquivalentValues(command, getEffectiveCommandValue(candidate, command), newValue)) { + return; + } + + // "While candidate has children, insert the first child of candidate into + // candidate's parent immediately before candidate, preserving ranges." + while (candidate.hasChildNodes()) { + movePreservingRanges(candidate.firstChild, candidate.parentNode, getNodeIndex(candidate)); + } + + // "Insert candidate into node's parent immediately after node." + node.parentNode.insertBefore(candidate, node.nextSibling); + + // "Append the node as the last child of candidate, preserving ranges." + movePreservingRanges(node, candidate, -1); +} + +function recordValues(nodeList) { + // "Let values be a list of (node, command, specified command value) + // triples, initially empty." + var values = []; + + // "For each node in node list, for each command in the list "subscript", + // "bold", "fontName", "fontSize", "foreColor", "hiliteColor", "italic", + // "strikethrough", and "underline" in that order:" + nodeList.forEach(function(node) { + ["subscript", "bold", "fontname", "fontsize", "forecolor", + "hilitecolor", "italic", "strikethrough", "underline"].forEach(function(command) { + // "Let ancestor equal node." + var ancestor = node; + + // "If ancestor is not an Element, set it to its parent." + if (ancestor.nodeType != Node.ELEMENT_NODE) { + ancestor = ancestor.parentNode; + } + + // "While ancestor is an Element and its specified command value + // for command is null, set it to its parent." + while (ancestor + && ancestor.nodeType == Node.ELEMENT_NODE + && getSpecifiedCommandValue(ancestor, command) === null) { + ancestor = ancestor.parentNode; + } + + // "If ancestor is an Element, add (node, command, ancestor's + // specified command value for command) to values. Otherwise add + // (node, command, null) to values." + if (ancestor && ancestor.nodeType == Node.ELEMENT_NODE) { + values.push([node, command, getSpecifiedCommandValue(ancestor, command)]); + } else { + values.push([node, command, null]); + } + }); + }); + + // "Return values." + return values; +} + +function restoreValues(values) { + // "For each (node, command, value) triple in values:" + values.forEach(function(triple) { + var node = triple[0]; + var command = triple[1]; + var value = triple[2]; + + // "Let ancestor equal node." + var ancestor = node; + + // "If ancestor is not an Element, set it to its parent." + if (!ancestor || ancestor.nodeType != Node.ELEMENT_NODE) { + ancestor = ancestor.parentNode; + } + + // "While ancestor is an Element and its specified command value for + // command is null, set it to its parent." + while (ancestor + && ancestor.nodeType == Node.ELEMENT_NODE + && getSpecifiedCommandValue(ancestor, command) === null) { + ancestor = ancestor.parentNode; + } + + // "If value is null and ancestor is an Element, push down values on + // node for command, with new value null." + if (value === null + && ancestor + && ancestor.nodeType == Node.ELEMENT_NODE) { + pushDownValues(node, command, null); + + // "Otherwise, if ancestor is an Element and its specified command + // value for command is not equivalent to value, or if ancestor is not + // an Element and value is not null, force the value of command to + // value on node." + } else if ((ancestor + && ancestor.nodeType == Node.ELEMENT_NODE + && !areEquivalentValues(command, getSpecifiedCommandValue(ancestor, command), value)) + || ((!ancestor || ancestor.nodeType != Node.ELEMENT_NODE) + && value !== null)) { + forceValue(node, command, value); + } + }); +} + + +//@} +///// Clearing an element's value ///// +//@{ + +function clearValue(element, command) { + // "If element is not editable, return the empty list." + if (!isEditable(element)) { + return []; + } + + // "If element's specified command value for command is null, return the + // empty list." + if (getSpecifiedCommandValue(element, command) === null) { + return []; + } + + // "If element is a simple modifiable element:" + if (isSimpleModifiableElement(element)) { + // "Let children be the children of element." + var children = Array.prototype.slice.call(element.childNodes); + + // "For each child in children, insert child into element's parent + // immediately before element, preserving ranges." + for (var i = 0; i < children.length; i++) { + movePreservingRanges(children[i], element.parentNode, getNodeIndex(element)); + } + + // "Remove element from its parent." + element.parentNode.removeChild(element); + + // "Return children." + return children; + } + + // "If command is "strikethrough", and element has a style attribute that + // sets "text-decoration" to some value containing "line-through", delete + // "line-through" from the value." + if (command == "strikethrough" + && element.style.textDecoration.indexOf("line-through") != -1) { + if (element.style.textDecoration == "line-through") { + element.style.textDecoration = ""; + } else { + element.style.textDecoration = element.style.textDecoration.replace("line-through", ""); + } + if (element.getAttribute("style") == "") { + element.removeAttribute("style"); + } + } + + // "If command is "underline", and element has a style attribute that sets + // "text-decoration" to some value containing "underline", delete + // "underline" from the value." + if (command == "underline" + && element.style.textDecoration.indexOf("underline") != -1) { + if (element.style.textDecoration == "underline") { + element.style.textDecoration = ""; + } else { + element.style.textDecoration = element.style.textDecoration.replace("underline", ""); + } + if (element.getAttribute("style") == "") { + element.removeAttribute("style"); + } + } + + // "If the relevant CSS property for command is not null, unset the CSS + // property property of element." + if (commands[command].relevantCssProperty !== null) { + element.style[commands[command].relevantCssProperty] = ''; + if (element.getAttribute("style") == "") { + element.removeAttribute("style"); + } + } + + // "If element is a font element:" + if (isHtmlNamespace(element.namespaceURI) && element.tagName == "FONT") { + // "If command is "foreColor", unset element's color attribute, if set." + if (command == "forecolor") { + element.removeAttribute("color"); + } + + // "If command is "fontName", unset element's face attribute, if set." + if (command == "fontname") { + element.removeAttribute("face"); + } + + // "If command is "fontSize", unset element's size attribute, if set." + if (command == "fontsize") { + element.removeAttribute("size"); + } + } + + // "If element is an a element and command is "createLink" or "unlink", + // unset the href property of element." + if (isHtmlElement(element, "A") + && (command == "createlink" || command == "unlink")) { + element.removeAttribute("href"); + } + + // "If element's specified command value for command is null, return the + // empty list." + if (getSpecifiedCommandValue(element, command) === null) { + return []; + } + + // "Set the tag name of element to "span", and return the one-node list + // consisting of the result." + return [setTagName(element, "span")]; +} + + +//@} +///// Pushing down values ///// +//@{ + +function pushDownValues(node, command, newValue) { + // "If node's parent is not an Element, abort this algorithm." + if (!node.parentNode + || node.parentNode.nodeType != Node.ELEMENT_NODE) { + return; + } + + // "If the effective command value of command is loosely equivalent to new + // value on node, abort this algorithm." + if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { + return; + } + + // "Let current ancestor be node's parent." + var currentAncestor = node.parentNode; + + // "Let ancestor list be a list of Nodes, initially empty." + var ancestorList = []; + + // "While current ancestor is an editable Element and the effective command + // value of command is not loosely equivalent to new value on it, append + // current ancestor to ancestor list, then set current ancestor to its + // parent." + while (isEditable(currentAncestor) + && currentAncestor.nodeType == Node.ELEMENT_NODE + && !areLooselyEquivalentValues(command, getEffectiveCommandValue(currentAncestor, command), newValue)) { + ancestorList.push(currentAncestor); + currentAncestor = currentAncestor.parentNode; + } + + // "If ancestor list is empty, abort this algorithm." + if (!ancestorList.length) { + return; + } + + // "Let propagated value be the specified command value of command on the + // last member of ancestor list." + var propagatedValue = getSpecifiedCommandValue(ancestorList[ancestorList.length - 1], command); + + // "If propagated value is null and is not equal to new value, abort this + // algorithm." + if (propagatedValue === null && propagatedValue != newValue) { + return; + } + + // "If the effective command value for the parent of the last member of + // ancestor list is not loosely equivalent to new value, and new value is + // not null, abort this algorithm." + if (newValue !== null + && !areLooselyEquivalentValues(command, getEffectiveCommandValue(ancestorList[ancestorList.length - 1].parentNode, command), newValue)) { + return; + } + + // "While ancestor list is not empty:" + while (ancestorList.length) { + // "Let current ancestor be the last member of ancestor list." + // "Remove the last member from ancestor list." + var currentAncestor = ancestorList.pop(); + + // "If the specified command value of current ancestor for command is + // not null, set propagated value to that value." + if (getSpecifiedCommandValue(currentAncestor, command) !== null) { + propagatedValue = getSpecifiedCommandValue(currentAncestor, command); + } + + // "Let children be the children of current ancestor." + var children = Array.prototype.slice.call(currentAncestor.childNodes); + + // "If the specified command value of current ancestor for command is + // not null, clear the value of current ancestor." + if (getSpecifiedCommandValue(currentAncestor, command) !== null) { + clearValue(currentAncestor, command); + } + + // "For every child in children:" + for (var i = 0; i < children.length; i++) { + var child = children[i]; + + // "If child is node, continue with the next child." + if (child == node) { + continue; + } + + // "If child is an Element whose specified command value for + // command is neither null nor equivalent to propagated value, + // continue with the next child." + if (child.nodeType == Node.ELEMENT_NODE + && getSpecifiedCommandValue(child, command) !== null + && !areEquivalentValues(command, propagatedValue, getSpecifiedCommandValue(child, command))) { + continue; + } + + // "If child is the last member of ancestor list, continue with the + // next child." + if (child == ancestorList[ancestorList.length - 1]) { + continue; + } + + // "Force the value of child, with command as in this algorithm + // and new value equal to propagated value." + forceValue(child, command, propagatedValue); + } + } +} + + +//@} +///// Forcing the value of a node ///// +//@{ + +function forceValue(node, command, newValue) { + // "If node's parent is null, abort this algorithm." + if (!node.parentNode) { + return; + } + + // "If new value is null, abort this algorithm." + if (newValue === null) { + return; + } + + // "If node is an allowed child of "span":" + if (isAllowedChild(node, "span")) { + // "Reorder modifiable descendants of node's previousSibling." + reorderModifiableDescendants(node.previousSibling, command, newValue); + + // "Reorder modifiable descendants of node's nextSibling." + reorderModifiableDescendants(node.nextSibling, command, newValue); + + // "Wrap the one-node list consisting of node, with sibling criteria + // returning true for a simple modifiable element whose specified + // command value is equivalent to new value and whose effective command + // value is loosely equivalent to new value and false otherwise, and + // with new parent instructions returning null." + wrap([node], + function(node) { + return isSimpleModifiableElement(node) + && areEquivalentValues(command, getSpecifiedCommandValue(node, command), newValue) + && areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue); + }, + function() { return null } + ); + } + + // "If node is invisible, abort this algorithm." + if (isInvisible(node)) { + return; + } + + // "If the effective command value of command is loosely equivalent to new + // value on node, abort this algorithm." + if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { + return; + } + + // "If node is not an allowed child of "span":" + if (!isAllowedChild(node, "span")) { + // "Let children be all children of node, omitting any that are + // Elements whose specified command value for command is neither null + // nor equivalent to new value." + var children = []; + for (var i = 0; i < node.childNodes.length; i++) { + if (node.childNodes[i].nodeType == Node.ELEMENT_NODE) { + var specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command); + + if (specifiedValue !== null + && !areEquivalentValues(command, newValue, specifiedValue)) { + continue; + } + } + children.push(node.childNodes[i]); + } + + // "Force the value of each Node in children, with command and new + // value as in this invocation of the algorithm." + for (var i = 0; i < children.length; i++) { + forceValue(children[i], command, newValue); + } + + // "Abort this algorithm." + return; + } + + // "If the effective command value of command is loosely equivalent to new + // value on node, abort this algorithm." + if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { + return; + } + + // "Let new parent be null." + var newParent = null; + + // "If the CSS styling flag is false:" + if (!cssStylingFlag) { + // "If command is "bold" and new value is "bold", let new parent be the + // result of calling createElement("b") on the ownerDocument of node." + if (command == "bold" && (newValue == "bold" || newValue == "700")) { + newParent = node.ownerDocument.createElement("b"); + } + + // "If command is "italic" and new value is "italic", let new parent be + // the result of calling createElement("i") on the ownerDocument of + // node." + if (command == "italic" && newValue == "italic") { + newParent = node.ownerDocument.createElement("i"); + } + + // "If command is "strikethrough" and new value is "line-through", let + // new parent be the result of calling createElement("s") on the + // ownerDocument of node." + if (command == "strikethrough" && newValue == "line-through") { + newParent = node.ownerDocument.createElement("s"); + } + + // "If command is "underline" and new value is "underline", let new + // parent be the result of calling createElement("u") on the + // ownerDocument of node." + if (command == "underline" && newValue == "underline") { + newParent = node.ownerDocument.createElement("u"); + } + + // "If command is "foreColor", and new value is fully opaque with red, + // green, and blue components in the range 0 to 255:" + if (command == "forecolor" && parseSimpleColor(newValue)) { + // "Let new parent be the result of calling createElement("font") + // on the ownerDocument of node." + newParent = node.ownerDocument.createElement("font"); + + // "Set the color attribute of new parent to the result of applying + // the rules for serializing simple color values to new value + // (interpreted as a simple color)." + newParent.setAttribute("color", parseSimpleColor(newValue)); + } + + // "If command is "fontName", let new parent be the result of calling + // createElement("font") on the ownerDocument of node, then set the + // face attribute of new parent to new value." + if (command == "fontname") { + newParent = node.ownerDocument.createElement("font"); + newParent.face = newValue; + } + } + + // "If command is "createLink" or "unlink":" + if (command == "createlink" || command == "unlink") { + // "Let new parent be the result of calling createElement("a") on the + // ownerDocument of node." + newParent = node.ownerDocument.createElement("a"); + + // "Set the href attribute of new parent to new value." + newParent.setAttribute("href", newValue); + + // "Let ancestor be node's parent." + var ancestor = node.parentNode; + + // "While ancestor is not null:" + while (ancestor) { + // "If ancestor is an a, set the tag name of ancestor to "span", + // and let ancestor be the result." + if (isHtmlElement(ancestor, "A")) { + ancestor = setTagName(ancestor, "span"); + } + + // "Set ancestor to its parent." + ancestor = ancestor.parentNode; + } + } + + // "If command is "fontSize"; and new value is one of "x-small", "small", + // "medium", "large", "x-large", "xx-large", or "xxx-large"; and either the + // CSS styling flag is false, or new value is "xxx-large": let new parent + // be the result of calling createElement("font") on the ownerDocument of + // node, then set the size attribute of new parent to the number from the + // following table based on new value: [table omitted]" + if (command == "fontsize" + && ["x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"].indexOf(newValue) != -1 + && (!cssStylingFlag || newValue == "xxx-large")) { + newParent = node.ownerDocument.createElement("font"); + newParent.size = cssSizeToLegacy(newValue); + } + + // "If command is "subscript" or "superscript" and new value is + // "subscript", let new parent be the result of calling + // createElement("sub") on the ownerDocument of node." + if ((command == "subscript" || command == "superscript") + && newValue == "subscript") { + newParent = node.ownerDocument.createElement("sub"); + } + + // "If command is "subscript" or "superscript" and new value is + // "superscript", let new parent be the result of calling + // createElement("sup") on the ownerDocument of node." + if ((command == "subscript" || command == "superscript") + && newValue == "superscript") { + newParent = node.ownerDocument.createElement("sup"); + } + + // "If new parent is null, let new parent be the result of calling + // createElement("span") on the ownerDocument of node." + if (!newParent) { + newParent = node.ownerDocument.createElement("span"); + } + + // "Insert new parent in node's parent before node." + node.parentNode.insertBefore(newParent, node); + + // "If the effective command value of command for new parent is not loosely + // equivalent to new value, and the relevant CSS property for command is + // not null, set that CSS property of new parent to new value (if the new + // value would be valid)." + var property = commands[command].relevantCssProperty; + if (property !== null + && !areLooselyEquivalentValues(command, getEffectiveCommandValue(newParent, command), newValue)) { + newParent.style[property] = newValue; + } + + // "If command is "strikethrough", and new value is "line-through", and the + // effective command value of "strikethrough" for new parent is not + // "line-through", set the "text-decoration" property of new parent to + // "line-through"." + if (command == "strikethrough" + && newValue == "line-through" + && getEffectiveCommandValue(newParent, "strikethrough") != "line-through") { + newParent.style.textDecoration = "line-through"; + } + + // "If command is "underline", and new value is "underline", and the + // effective command value of "underline" for new parent is not + // "underline", set the "text-decoration" property of new parent to + // "underline"." + if (command == "underline" + && newValue == "underline" + && getEffectiveCommandValue(newParent, "underline") != "underline") { + newParent.style.textDecoration = "underline"; + } + + // "Append node to new parent as its last child, preserving ranges." + movePreservingRanges(node, newParent, newParent.childNodes.length); + + // "If node is an Element and the effective command value of command for + // node is not loosely equivalent to new value:" + if (node.nodeType == Node.ELEMENT_NODE + && !areEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { + // "Insert node into the parent of new parent before new parent, + // preserving ranges." + movePreservingRanges(node, newParent.parentNode, getNodeIndex(newParent)); + + // "Remove new parent from its parent." + newParent.parentNode.removeChild(newParent); + + // "Let children be all children of node, omitting any that are + // Elements whose specified command value for command is neither null + // nor equivalent to new value." + var children = []; + for (var i = 0; i < node.childNodes.length; i++) { + if (node.childNodes[i].nodeType == Node.ELEMENT_NODE) { + var specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command); + + if (specifiedValue !== null + && !areEquivalentValues(command, newValue, specifiedValue)) { + continue; + } + } + children.push(node.childNodes[i]); + } + + // "Force the value of each Node in children, with command and new + // value as in this invocation of the algorithm." + for (var i = 0; i < children.length; i++) { + forceValue(children[i], command, newValue); + } + } +} + + +//@} +///// Setting the selection's value ///// +//@{ + +function setSelectionValue(command, newValue) { + // "If there is no formattable node effectively contained in the active + // range:" + if (!getAllEffectivelyContainedNodes(getActiveRange()) + .some(isFormattableNode)) { + // "If command has inline command activated values, set the state + // override to true if new value is among them and false if it's not." + if ("inlineCommandActivatedValues" in commands[command]) { + setStateOverride(command, commands[command].inlineCommandActivatedValues + .indexOf(newValue) != -1); + } + + // "If command is "subscript", unset the state override for + // "superscript"." + if (command == "subscript") { + unsetStateOverride("superscript"); + } + + // "If command is "superscript", unset the state override for + // "subscript"." + if (command == "superscript") { + unsetStateOverride("subscript"); + } + + // "If new value is null, unset the value override (if any)." + if (newValue === null) { + unsetValueOverride(command); + + // "Otherwise, if command is "createLink" or it has a value specified, + // set the value override to new value." + } else if (command == "createlink" || "value" in commands[command]) { + setValueOverride(command, newValue); + } + + // "Abort these steps." + return; + } + + // "If the active range's start node is an editable Text node, and its + // start offset is neither zero nor its start node's length, call + // splitText() on the active range's start node, with argument equal to the + // active range's start offset. Then set the active range's start node to + // the result, and its start offset to zero." + if (isEditable(getActiveRange().startContainer) + && getActiveRange().startContainer.nodeType == Node.TEXT_NODE + && getActiveRange().startOffset != 0 + && getActiveRange().startOffset != getNodeLength(getActiveRange().startContainer)) { + // Account for browsers not following range mutation rules + var newActiveRange = document.createRange(); + var newNode; + if (getActiveRange().startContainer == getActiveRange().endContainer) { + var newEndOffset = getActiveRange().endOffset - getActiveRange().startOffset; + newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset); + newActiveRange.setEnd(newNode, newEndOffset); + getActiveRange().setEnd(newNode, newEndOffset); + } else { + newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset); + } + newActiveRange.setStart(newNode, 0); + getSelection().removeAllRanges(); + getSelection().addRange(newActiveRange); + + getActiveRange().setStart(newNode, 0); + } + + // "If the active range's end node is an editable Text node, and its end + // offset is neither zero nor its end node's length, call splitText() on + // the active range's end node, with argument equal to the active range's + // end offset." + if (isEditable(getActiveRange().endContainer) + && getActiveRange().endContainer.nodeType == Node.TEXT_NODE + && getActiveRange().endOffset != 0 + && getActiveRange().endOffset != getNodeLength(getActiveRange().endContainer)) { + // IE seems to mutate the range incorrectly here, so we need correction + // here as well. The active range will be temporarily in orphaned + // nodes, so calling getActiveRange() after splitText() but before + // fixing the range will throw an exception. + var activeRange = getActiveRange(); + var newStart = [activeRange.startContainer, activeRange.startOffset]; + var newEnd = [activeRange.endContainer, activeRange.endOffset]; + activeRange.endContainer.splitText(activeRange.endOffset); + activeRange.setStart(newStart[0], newStart[1]); + activeRange.setEnd(newEnd[0], newEnd[1]); + + getSelection().removeAllRanges(); + getSelection().addRange(activeRange); + } + + // "Let element list be all editable Elements effectively contained in the + // active range. + // + // "For each element in element list, clear the value of element." + getAllEffectivelyContainedNodes(getActiveRange(), function(node) { + return isEditable(node) && node.nodeType == Node.ELEMENT_NODE; + }).forEach(function(element) { + clearValue(element, command); + }); + + // "Let node list be all editable nodes effectively contained in the active + // range. + // + // "For each node in node list:" + getAllEffectivelyContainedNodes(getActiveRange(), isEditable).forEach(function(node) { + // "Push down values on node." + pushDownValues(node, command, newValue); + + // "If node is an allowed child of span, force the value of node." + if (isAllowedChild(node, "span")) { + forceValue(node, command, newValue); + } + }); +} + + +//@} +///// The backColor command ///// +//@{ +commands.backcolor = { + // Copy-pasted, same as hiliteColor + action: function(value) { + // Action is further copy-pasted, same as foreColor + + // "If value is not a valid CSS color, prepend "#" to it." + // + // "If value is still not a valid CSS color, or if it is currentColor, + // return false." + // + // Cheap hack for testing, no attempt to be comprehensive. + if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) { + value = "#" + value; + } + if (!/^(rgba?|hsla?)\(.*\)$/.test(value) + && !parseSimpleColor(value) + && value.toLowerCase() != "transparent") { + return false; + } + + // "Set the selection's value to value." + setSelectionValue("backcolor", value); + + // "Return true." + return true; + }, standardInlineValueCommand: true, relevantCssProperty: "backgroundColor", + equivalentValues: function(val1, val2) { + // "Either both strings are valid CSS colors and have the same red, + // green, blue, and alpha components, or neither string is a valid CSS + // color." + return normalizeColor(val1) === normalizeColor(val2); + }, +}; + +//@} +///// The bold command ///// +//@{ +commands.bold = { + action: function() { + // "If queryCommandState("bold") returns true, set the selection's + // value to "normal". Otherwise set the selection's value to "bold". + // Either way, return true." + if (myQueryCommandState("bold")) { + setSelectionValue("bold", "normal"); + } else { + setSelectionValue("bold", "bold"); + } + return true; + }, inlineCommandActivatedValues: ["bold", "600", "700", "800", "900"], + relevantCssProperty: "fontWeight", + equivalentValues: function(val1, val2) { + // "Either the two strings are equal, or one is "bold" and the other is + // "700", or one is "normal" and the other is "400"." + return val1 == val2 + || (val1 == "bold" && val2 == "700") + || (val1 == "700" && val2 == "bold") + || (val1 == "normal" && val2 == "400") + || (val1 == "400" && val2 == "normal"); + }, +}; + +//@} +///// The createLink command ///// +//@{ +commands.createlink = { + action: function(value) { + // "If value is the empty string, return false." + if (value === "") { + return false; + } + + // "For each editable a element that has an href attribute and is an + // ancestor of some node effectively contained in the active range, set + // that a element's href attribute to value." + // + // TODO: We don't actually do this in tree order, not that it matters + // unless you're spying with mutation events. + getAllEffectivelyContainedNodes(getActiveRange()).forEach(function(node) { + getAncestors(node).forEach(function(ancestor) { + if (isEditable(ancestor) + && isHtmlElement(ancestor, "a") + && ancestor.hasAttribute("href")) { + ancestor.setAttribute("href", value); + } + }); + }); + + // "Set the selection's value to value." + setSelectionValue("createlink", value); + + // "Return true." + return true; + } +}; + +//@} +///// The fontName command ///// +//@{ +commands.fontname = { + action: function(value) { + // "Set the selection's value to value, then return true." + setSelectionValue("fontname", value); + return true; + }, standardInlineValueCommand: true, relevantCssProperty: "fontFamily" +}; + +//@} +///// The fontSize command ///// +//@{ + +// Helper function for fontSize's action plus queryOutputHelper. It's just the +// middle of fontSize's action, ripped out into its own function. Returns null +// if the size is invalid. +function normalizeFontSize(value) { + // "Strip leading and trailing whitespace from value." + // + // Cheap hack, not following the actual algorithm. + value = value.trim(); + + // "If value is not a valid floating point number, and would not be a valid + // floating point number if a single leading "+" character were stripped, + // return false." + if (!/^[-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/.test(value)) { + return null; + } + + var mode; + + // "If the first character of value is "+", delete the character and let + // mode be "relative-plus"." + if (value[0] == "+") { + value = value.slice(1); + mode = "relative-plus"; + // "Otherwise, if the first character of value is "-", delete the character + // and let mode be "relative-minus"." + } else if (value[0] == "-") { + value = value.slice(1); + mode = "relative-minus"; + // "Otherwise, let mode be "absolute"." + } else { + mode = "absolute"; + } + + // "Apply the rules for parsing non-negative integers to value, and let + // number be the result." + // + // Another cheap hack. + var num = parseInt(value); + + // "If mode is "relative-plus", add three to number." + if (mode == "relative-plus") { + num += 3; + } + + // "If mode is "relative-minus", negate number, then add three to it." + if (mode == "relative-minus") { + num = 3 - num; + } + + // "If number is less than one, let number equal 1." + if (num < 1) { + num = 1; + } + + // "If number is greater than seven, let number equal 7." + if (num > 7) { + num = 7; + } + + // "Set value to the string here corresponding to number:" [table omitted] + value = { + 1: "x-small", + 2: "small", + 3: "medium", + 4: "large", + 5: "x-large", + 6: "xx-large", + 7: "xxx-large" + }[num]; + + return value; +} + +commands.fontsize = { + action: function(value) { + value = normalizeFontSize(value); + if (value === null) { + return false; + } + + // "Set the selection's value to value." + setSelectionValue("fontsize", value); + + // "Return true." + return true; + }, indeterm: function() { + // "True if among formattable nodes that are effectively contained in + // the active range, there are two that have distinct effective command + // values. Otherwise false." + return getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode) + .map(function(node) { + return getEffectiveCommandValue(node, "fontsize"); + }).filter(function(value, i, arr) { + return arr.slice(0, i).indexOf(value) == -1; + }).length >= 2; + }, value: function() { + // "If the active range is null, return the empty string." + if (!getActiveRange()) { + return ""; + } + + // "Let pixel size be the effective command value of the first + // formattable node that is effectively contained in the active range, + // or if there is no such node, the effective command value of the + // active range's start node, in either case interpreted as a number of + // pixels." + var node = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode)[0]; + if (node === undefined) { + node = getActiveRange().startContainer; + } + var pixelSize = getEffectiveCommandValue(node, "fontsize"); + + // "Return the legacy font size for pixel size." + return getLegacyFontSize(pixelSize); + }, relevantCssProperty: "fontSize" +}; + +function getLegacyFontSize(size) { + if (getLegacyFontSize.resultCache === undefined) { + getLegacyFontSize.resultCache = {}; + } + + if (getLegacyFontSize.resultCache[size] !== undefined) { + return getLegacyFontSize.resultCache[size]; + } + + // For convenience in other places in my code, I handle all sizes, not just + // pixel sizes as the spec says. This means pixel sizes have to be passed + // in suffixed with "px", not as plain numbers. + if (normalizeFontSize(size) !== null) { + return getLegacyFontSize.resultCache[size] = cssSizeToLegacy(normalizeFontSize(size)); + } + + if (["x-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"].indexOf(size) == -1 + && !/^[0-9]+(\.[0-9]+)?(cm|mm|in|pt|pc|px)$/.test(size)) { + // There is no sensible legacy size for things like "2em". + return getLegacyFontSize.resultCache[size] = null; + } + + var font = document.createElement("font"); + document.body.appendChild(font); + if (size == "xxx-large") { + font.size = 7; + } else { + font.style.fontSize = size; + } + var pixelSize = parseInt(getComputedStyle(font).fontSize); + document.body.removeChild(font); + + // "Let returned size be 1." + var returnedSize = 1; + + // "While returned size is less than 7:" + while (returnedSize < 7) { + // "Let lower bound be the resolved value of "font-size" in pixels + // of a font element whose size attribute is set to returned size." + var font = document.createElement("font"); + font.size = returnedSize; + document.body.appendChild(font); + var lowerBound = parseInt(getComputedStyle(font).fontSize); + + // "Let upper bound be the resolved value of "font-size" in pixels + // of a font element whose size attribute is set to one plus + // returned size." + font.size = 1 + returnedSize; + var upperBound = parseInt(getComputedStyle(font).fontSize); + document.body.removeChild(font); + + // "Let average be the average of upper bound and lower bound." + var average = (upperBound + lowerBound)/2; + + // "If pixel size is less than average, return the one-element + // string consisting of the digit returned size." + if (pixelSize < average) { + return getLegacyFontSize.resultCache[size] = String(returnedSize); + } + + // "Add one to returned size." + returnedSize++; + } + + // "Return "7"." + return getLegacyFontSize.resultCache[size] = "7"; +} + +//@} +///// The foreColor command ///// +//@{ +commands.forecolor = { + action: function(value) { + // Copy-pasted, same as backColor and hiliteColor + + // "If value is not a valid CSS color, prepend "#" to it." + // + // "If value is still not a valid CSS color, or if it is currentColor, + // return false." + // + // Cheap hack for testing, no attempt to be comprehensive. + if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) { + value = "#" + value; + } + if (!/^(rgba?|hsla?)\(.*\)$/.test(value) + && !parseSimpleColor(value) + && value.toLowerCase() != "transparent") { + return false; + } + + // "Set the selection's value to value." + setSelectionValue("forecolor", value); + + // "Return true." + return true; + }, standardInlineValueCommand: true, relevantCssProperty: "color", + equivalentValues: function(val1, val2) { + // "Either both strings are valid CSS colors and have the same red, + // green, blue, and alpha components, or neither string is a valid CSS + // color." + return normalizeColor(val1) === normalizeColor(val2); + }, +}; + +//@} +///// The hiliteColor command ///// +//@{ +commands.hilitecolor = { + // Copy-pasted, same as backColor + action: function(value) { + // Action is further copy-pasted, same as foreColor + + // "If value is not a valid CSS color, prepend "#" to it." + // + // "If value is still not a valid CSS color, or if it is currentColor, + // return false." + // + // Cheap hack for testing, no attempt to be comprehensive. + if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) { + value = "#" + value; + } + if (!/^(rgba?|hsla?)\(.*\)$/.test(value) + && !parseSimpleColor(value) + && value.toLowerCase() != "transparent") { + return false; + } + + // "Set the selection's value to value." + setSelectionValue("hilitecolor", value); + + // "Return true." + return true; + }, indeterm: function() { + // "True if among editable Text nodes that are effectively contained in + // the active range, there are two that have distinct effective command + // values. Otherwise false." + return getAllEffectivelyContainedNodes(getActiveRange(), function(node) { + return isEditable(node) && node.nodeType == Node.TEXT_NODE; + }).map(function(node) { + return getEffectiveCommandValue(node, "hilitecolor"); + }).filter(function(value, i, arr) { + return arr.slice(0, i).indexOf(value) == -1; + }).length >= 2; + }, standardInlineValueCommand: true, relevantCssProperty: "backgroundColor", + equivalentValues: function(val1, val2) { + // "Either both strings are valid CSS colors and have the same red, + // green, blue, and alpha components, or neither string is a valid CSS + // color." + return normalizeColor(val1) === normalizeColor(val2); + }, +}; + +//@} +///// The italic command ///// +//@{ +commands.italic = { + action: function() { + // "If queryCommandState("italic") returns true, set the selection's + // value to "normal". Otherwise set the selection's value to "italic". + // Either way, return true." + if (myQueryCommandState("italic")) { + setSelectionValue("italic", "normal"); + } else { + setSelectionValue("italic", "italic"); + } + return true; + }, inlineCommandActivatedValues: ["italic", "oblique"], + relevantCssProperty: "fontStyle" +}; + +//@} +///// The removeFormat command ///// +//@{ +commands.removeformat = { + action: function() { + // "A removeFormat candidate is an editable HTML element with local + // name "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", or "var"." + function isRemoveFormatCandidate(node) { + return isEditable(node) + && isHtmlElement(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"]); + } + + // "Let elements to remove be a list of every removeFormat candidate + // effectively contained in the active range." + var elementsToRemove = getAllEffectivelyContainedNodes(getActiveRange(), isRemoveFormatCandidate); + + // "For each element in elements to remove:" + elementsToRemove.forEach(function(element) { + // "While element has children, insert the first child of element + // into the parent of element immediately before element, + // preserving ranges." + while (element.hasChildNodes()) { + movePreservingRanges(element.firstChild, element.parentNode, getNodeIndex(element)); + } + + // "Remove element from its parent." + element.parentNode.removeChild(element); + }); + + // "If the active range's start node is an editable Text node, and its + // start offset is neither zero nor its start node's length, call + // splitText() on the active range's start node, with argument equal to + // the active range's start offset. Then set the active range's start + // node to the result, and its start offset to zero." + if (isEditable(getActiveRange().startContainer) + && getActiveRange().startContainer.nodeType == Node.TEXT_NODE + && getActiveRange().startOffset != 0 + && getActiveRange().startOffset != getNodeLength(getActiveRange().startContainer)) { + // Account for browsers not following range mutation rules + if (getActiveRange().startContainer == getActiveRange().endContainer) { + var newEnd = getActiveRange().endOffset - getActiveRange().startOffset; + var newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset); + getActiveRange().setStart(newNode, 0); + getActiveRange().setEnd(newNode, newEnd); + } else { + getActiveRange().setStart(getActiveRange().startContainer.splitText(getActiveRange().startOffset), 0); + } + } + + // "If the active range's end node is an editable Text node, and its + // end offset is neither zero nor its end node's length, call + // splitText() on the active range's end node, with argument equal to + // the active range's end offset." + if (isEditable(getActiveRange().endContainer) + && getActiveRange().endContainer.nodeType == Node.TEXT_NODE + && getActiveRange().endOffset != 0 + && getActiveRange().endOffset != getNodeLength(getActiveRange().endContainer)) { + // IE seems to mutate the range incorrectly here, so we need + // correction here as well. Have to be careful to set the range to + // something not including the text node so that getActiveRange() + // doesn't throw an exception due to a temporarily detached + // endpoint. + var newStart = [getActiveRange().startContainer, getActiveRange().startOffset]; + var newEnd = [getActiveRange().endContainer, getActiveRange().endOffset]; + getActiveRange().setEnd(document.documentElement, 0); + newEnd[0].splitText(newEnd[1]); + getActiveRange().setStart(newStart[0], newStart[1]); + getActiveRange().setEnd(newEnd[0], newEnd[1]); + } + + // "Let node list consist of all editable nodes effectively contained + // in the active range." + // + // "For each node in node list, while node's parent is a removeFormat + // candidate in the same editing host as node, split the parent of the + // one-node list consisting of node." + getAllEffectivelyContainedNodes(getActiveRange(), isEditable).forEach(function(node) { + while (isRemoveFormatCandidate(node.parentNode) + && inSameEditingHost(node.parentNode, node)) { + splitParent([node]); + } + }); + + // "For each of the entries in the following list, in the given order, + // set the selection's value to null, with command as given." + [ + "subscript", + "bold", + "fontname", + "fontsize", + "forecolor", + "hilitecolor", + "italic", + "strikethrough", + "underline", + ].forEach(function(command) { + setSelectionValue(command, null); + }); + + // "Return true." + return true; + } +}; + +//@} +///// The strikethrough command ///// +//@{ +commands.strikethrough = { + action: function() { + // "If queryCommandState("strikethrough") returns true, set the + // selection's value to null. Otherwise set the selection's value to + // "line-through". Either way, return true." + if (myQueryCommandState("strikethrough")) { + setSelectionValue("strikethrough", null); + } else { + setSelectionValue("strikethrough", "line-through"); + } + return true; + }, inlineCommandActivatedValues: ["line-through"] +}; + +//@} +///// The subscript command ///// +//@{ +commands.subscript = { + action: function() { + // "Call queryCommandState("subscript"), and let state be the result." + var state = myQueryCommandState("subscript"); + + // "Set the selection's value to null." + setSelectionValue("subscript", null); + + // "If state is false, set the selection's value to "subscript"." + if (!state) { + setSelectionValue("subscript", "subscript"); + } + + // "Return true." + return true; + }, indeterm: function() { + // "True if either among formattable nodes that are effectively + // contained in the active range, there is at least one with effective + // command value "subscript" and at least one with some other effective + // command value; or if there is some formattable node effectively + // contained in the active range with effective command value "mixed". + // Otherwise false." + var nodes = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode); + return (nodes.some(function(node) { return getEffectiveCommandValue(node, "subscript") == "subscript" }) + && nodes.some(function(node) { return getEffectiveCommandValue(node, "subscript") != "subscript" })) + || nodes.some(function(node) { return getEffectiveCommandValue(node, "subscript") == "mixed" }); + }, inlineCommandActivatedValues: ["subscript"], +}; + +//@} +///// The superscript command ///// +//@{ +commands.superscript = { + action: function() { + // "Call queryCommandState("superscript"), and let state be the + // result." + var state = myQueryCommandState("superscript"); + + // "Set the selection's value to null." + setSelectionValue("superscript", null); + + // "If state is false, set the selection's value to "superscript"." + if (!state) { + setSelectionValue("superscript", "superscript"); + } + + // "Return true." + return true; + }, indeterm: function() { + // "True if either among formattable nodes that are effectively + // contained in the active range, there is at least one with effective + // command value "superscript" and at least one with some other + // effective command value; or if there is some formattable node + // effectively contained in the active range with effective command + // value "mixed". Otherwise false." + var nodes = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode); + return (nodes.some(function(node) { return getEffectiveCommandValue(node, "superscript") == "superscript" }) + && nodes.some(function(node) { return getEffectiveCommandValue(node, "superscript") != "superscript" })) + || nodes.some(function(node) { return getEffectiveCommandValue(node, "superscript") == "mixed" }); + }, inlineCommandActivatedValues: ["superscript"], +}; + +//@} +///// The underline command ///// +//@{ +commands.underline = { + action: function() { + // "If queryCommandState("underline") returns true, set the selection's + // value to null. Otherwise set the selection's value to "underline". + // Either way, return true." + if (myQueryCommandState("underline")) { + setSelectionValue("underline", null); + } else { + setSelectionValue("underline", "underline"); + } + return true; + }, inlineCommandActivatedValues: ["underline"] +}; + +//@} +///// The unlink command ///// +//@{ +commands.unlink = { + action: function() { + // "Let hyperlinks be a list of every a element that has an href + // attribute and is contained in the active range or is an ancestor of + // one of its boundary points." + // + // As usual, take care to ensure it's tree order. The correctness of + // the following is left as an exercise for the reader. + var range = getActiveRange(); + var hyperlinks = []; + for ( + var node = range.startContainer; + node; + node = node.parentNode + ) { + if (isHtmlElement(node, "A") + && node.hasAttribute("href")) { + hyperlinks.unshift(node); + } + } + for ( + var node = range.startContainer; + node != nextNodeDescendants(range.endContainer); + node = nextNode(node) + ) { + if (isHtmlElement(node, "A") + && node.hasAttribute("href") + && (isContained(node, range) + || isAncestor(node, range.endContainer) + || node == range.endContainer)) { + hyperlinks.push(node); + } + } + + // "Clear the value of each member of hyperlinks." + for (var i = 0; i < hyperlinks.length; i++) { + clearValue(hyperlinks[i], "unlink"); + } + + // "Return true." + return true; + } +}; + +//@} + +///////////////////////////////////// +///// Block formatting commands ///// +///////////////////////////////////// + +///// Block formatting command definitions ///// +//@{ + +// "An indentation element is either a blockquote, or a div that has a style +// attribute that sets "margin" or some subproperty of it." +function isIndentationElement(node) { + if (!isHtmlElement(node)) { + return false; + } + + if (node.tagName == "BLOCKQUOTE") { + return true; + } + + if (node.tagName != "DIV") { + return false; + } + + for (var i = 0; i < node.style.length; i++) { + // Approximate check + if (/^(-[a-z]+-)?margin/.test(node.style[i])) { + return true; + } + } + + return false; +} + +// "A simple indentation element is an indentation element that has no +// attributes except possibly +// +// * "a style attribute that sets no properties other than "margin", +// "border", "padding", or subproperties of those; and/or +// * "a dir attribute." +function isSimpleIndentationElement(node) { + if (!isIndentationElement(node)) { + return false; + } + + for (var i = 0; i < node.attributes.length; i++) { + if (!isHtmlNamespace(node.attributes[i].namespaceURI) + || ["style", "dir"].indexOf(node.attributes[i].name) == -1) { + return false; + } + } + + for (var i = 0; i < node.style.length; i++) { + // This is approximate, but it works well enough for my purposes. + if (!/^(-[a-z]+-)?(margin|border|padding)/.test(node.style[i])) { + return false; + } + } + + return true; +} + +// "A non-list single-line container is an HTML element with local name +// "address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre", +// or "xmp"." +function isNonListSingleLineContainer(node) { + return isHtmlElement(node, ["address", "div", "h1", "h2", "h3", "h4", "h5", + "h6", "listing", "p", "pre", "xmp"]); +} + +// "A single-line container is either a non-list single-line container, or an +// HTML element with local name "li", "dt", or "dd"." +function isSingleLineContainer(node) { + return isNonListSingleLineContainer(node) + || isHtmlElement(node, ["li", "dt", "dd"]); +} + +function getBlockNodeOf(node) { + // "While node is an inline node, set node to its parent." + while (isInlineNode(node)) { + node = node.parentNode; + } + + // "Return node." + return node; +} + +//@} +///// Assorted block formatting command algorithms ///// +//@{ + +function fixDisallowedAncestors(node) { + // "If node is not editable, abort these steps." + if (!isEditable(node)) { + return; + } + + // "If node is not an allowed child of any of its ancestors in the same + // editing host:" + if (getAncestors(node).every(function(ancestor) { + return !inSameEditingHost(node, ancestor) + || !isAllowedChild(node, ancestor) + })) { + // "If node is a dd or dt, wrap the one-node list consisting of node, + // with sibling criteria returning true for any dl with no attributes + // and false otherwise, and new parent instructions returning the + // result of calling createElement("dl") on the context object. Then + // abort these steps." + if (isHtmlElement(node, ["dd", "dt"])) { + wrap([node], + function(sibling) { return isHtmlElement(sibling, "dl") && !sibling.attributes.length }, + function() { return document.createElement("dl") }); + return; + } + + // "If "p" is not an allowed child of the editing host of node, abort + // these steps." + if (!isAllowedChild("p", getEditingHostOf(node))) { + return; + } + + // "If node is not a prohibited paragraph child, abort these steps." + if (!isProhibitedParagraphChild(node)) { + return; + } + + // "Set the tag name of node to the default single-line container name, + // and let node be the result." + node = setTagName(node, defaultSingleLineContainerName); + + // "Fix disallowed ancestors of node." + fixDisallowedAncestors(node); + + // "Let children be node's children." + var children = [].slice.call(node.childNodes); + + // "For each child in children, if child is a prohibited paragraph + // child:" + children.filter(isProhibitedParagraphChild) + .forEach(function(child) { + // "Record the values of the one-node list consisting of child, and + // let values be the result." + var values = recordValues([child]); + + // "Split the parent of the one-node list consisting of child." + splitParent([child]); + + // "Restore the values from values." + restoreValues(values); + }); + + // "Abort these steps." + return; + } + + // "Record the values of the one-node list consisting of node, and let + // values be the result." + var values = recordValues([node]); + + // "While node is not an allowed child of its parent, split the parent of + // the one-node list consisting of node." + while (!isAllowedChild(node, node.parentNode)) { + splitParent([node]); + } + + // "Restore the values from values." + restoreValues(values); +} + +function normalizeSublists(item) { + // "If item is not an li or it is not editable or its parent is not + // editable, abort these steps." + if (!isHtmlElement(item, "LI") + || !isEditable(item) + || !isEditable(item.parentNode)) { + return; + } + + // "Let new item be null." + var newItem = null; + + // "While item has an ol or ul child:" + while ([].some.call(item.childNodes, function (node) { return isHtmlElement(node, ["OL", "UL"]) })) { + // "Let child be the last child of item." + var child = item.lastChild; + + // "If child is an ol or ul, or new item is null and child is a Text + // node whose data consists of zero of more space characters:" + if (isHtmlElement(child, ["OL", "UL"]) + || (!newItem && child.nodeType == Node.TEXT_NODE && /^[ \t\n\f\r]*$/.test(child.data))) { + // "Set new item to null." + newItem = null; + + // "Insert child into the parent of item immediately following + // item, preserving ranges." + movePreservingRanges(child, item.parentNode, 1 + getNodeIndex(item)); + + // "Otherwise:" + } else { + // "If new item is null, let new item be the result of calling + // createElement("li") on the ownerDocument of item, then insert + // new item into the parent of item immediately after item." + if (!newItem) { + newItem = item.ownerDocument.createElement("li"); + item.parentNode.insertBefore(newItem, item.nextSibling); + } + + // "Insert child into new item as its first child, preserving + // ranges." + movePreservingRanges(child, newItem, 0); + } + } +} + +function getSelectionListState() { + // "If the active range is null, return "none"." + if (!getActiveRange()) { + return "none"; + } + + // "Block-extend the active range, and let new range be the result." + var newRange = blockExtend(getActiveRange()); + + // "Let node list be a list of nodes, initially empty." + // + // "For each node contained in new range, append node to node list if the + // last member of node list (if any) is not an ancestor of node; node is + // editable; node is not an indentation element; and node is either an ol + // or ul, or the child of an ol or ul, or an allowed child of "li"." + var nodeList = getContainedNodes(newRange, function(node) { + return isEditable(node) + && !isIndentationElement(node) + && (isHtmlElement(node, ["ol", "ul"]) + || isHtmlElement(node.parentNode, ["ol", "ul"]) + || isAllowedChild(node, "li")); + }); + + // "If node list is empty, return "none"." + if (!nodeList.length) { + return "none"; + } + + // "If every member of node list is either an ol or the child of an ol or + // the child of an li child of an ol, and none is a ul or an ancestor of a + // ul, return "ol"." + if (nodeList.every(function(node) { + return isHtmlElement(node, "ol") + || isHtmlElement(node.parentNode, "ol") + || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ol")); + }) + && !nodeList.some(function(node) { return isHtmlElement(node, "ul") || ("querySelector" in node && node.querySelector("ul")) })) { + return "ol"; + } + + // "If every member of node list is either a ul or the child of a ul or the + // child of an li child of a ul, and none is an ol or an ancestor of an ol, + // return "ul"." + if (nodeList.every(function(node) { + return isHtmlElement(node, "ul") + || isHtmlElement(node.parentNode, "ul") + || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ul")); + }) + && !nodeList.some(function(node) { return isHtmlElement(node, "ol") || ("querySelector" in node && node.querySelector("ol")) })) { + return "ul"; + } + + var hasOl = nodeList.some(function(node) { + return isHtmlElement(node, "ol") + || isHtmlElement(node.parentNode, "ol") + || ("querySelector" in node && node.querySelector("ol")) + || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ol")); + }); + var hasUl = nodeList.some(function(node) { + return isHtmlElement(node, "ul") + || isHtmlElement(node.parentNode, "ul") + || ("querySelector" in node && node.querySelector("ul")) + || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ul")); + }); + // "If some member of node list is either an ol or the child or ancestor of + // an ol or the child of an li child of an ol, and some member of node list + // is either a ul or the child or ancestor of a ul or the child of an li + // child of a ul, return "mixed"." + if (hasOl && hasUl) { + return "mixed"; + } + + // "If some member of node list is either an ol or the child or ancestor of + // an ol or the child of an li child of an ol, return "mixed ol"." + if (hasOl) { + return "mixed ol"; + } + + // "If some member of node list is either a ul or the child or ancestor of + // a ul or the child of an li child of a ul, return "mixed ul"." + if (hasUl) { + return "mixed ul"; + } + + // "Return "none"." + return "none"; +} + +function getAlignmentValue(node) { + // "While node is neither null nor an Element, or it is an Element but its + // "display" property has resolved value "inline" or "none", set node to + // its parent." + while ((node && node.nodeType != Node.ELEMENT_NODE) + || (node.nodeType == Node.ELEMENT_NODE + && ["inline", "none"].indexOf(getComputedStyle(node).display) != -1)) { + node = node.parentNode; + } + + // "If node is not an Element, return "left"." + if (!node || node.nodeType != Node.ELEMENT_NODE) { + return "left"; + } + + var resolvedValue = getComputedStyle(node).textAlign + // Hack around browser non-standardness + .replace(/^-(moz|webkit)-/, "") + .replace(/^auto$/, "start"); + + // "If node's "text-align" property has resolved value "start", return + // "left" if the directionality of node is "ltr", "right" if it is "rtl"." + if (resolvedValue == "start") { + return getDirectionality(node) == "ltr" ? "left" : "right"; + } + + // "If node's "text-align" property has resolved value "end", return + // "right" if the directionality of node is "ltr", "left" if it is "rtl"." + if (resolvedValue == "end") { + return getDirectionality(node) == "ltr" ? "right" : "left"; + } + + // "If node's "text-align" property has resolved value "center", "justify", + // "left", or "right", return that value." + if (["center", "justify", "left", "right"].indexOf(resolvedValue) != -1) { + return resolvedValue; + } + + // "Return "left"." + return "left"; +} + +function getNextEquivalentPoint(node, offset) { + // "If node's length is zero, return null." + if (getNodeLength(node) == 0) { + return null; + } + + // "If offset is node's length, and node's parent is not null, and node is + // an inline node, return (node's parent, 1 + node's index)." + if (offset == getNodeLength(node) + && node.parentNode + && isInlineNode(node)) { + return [node.parentNode, 1 + getNodeIndex(node)]; + } + + // "If node has a child with index offset, and that child's length is not + // zero, and that child is an inline node, return (that child, 0)." + if (0 <= offset + && offset < node.childNodes.length + && getNodeLength(node.childNodes[offset]) != 0 + && isInlineNode(node.childNodes[offset])) { + return [node.childNodes[offset], 0]; + } + + // "Return null." + return null; +} + +function getPreviousEquivalentPoint(node, offset) { + // "If node's length is zero, return null." + if (getNodeLength(node) == 0) { + return null; + } + + // "If offset is 0, and node's parent is not null, and node is an inline + // node, return (node's parent, node's index)." + if (offset == 0 + && node.parentNode + && isInlineNode(node)) { + return [node.parentNode, getNodeIndex(node)]; + } + + // "If node has a child with index offset − 1, and that child's length is + // not zero, and that child is an inline node, return (that child, that + // child's length)." + if (0 <= offset - 1 + && offset - 1 < node.childNodes.length + && getNodeLength(node.childNodes[offset - 1]) != 0 + && isInlineNode(node.childNodes[offset - 1])) { + return [node.childNodes[offset - 1], getNodeLength(node.childNodes[offset - 1])]; + } + + // "Return null." + return null; +} + +function getFirstEquivalentPoint(node, offset) { + // "While (node, offset)'s previous equivalent point is not null, set + // (node, offset) to its previous equivalent point." + var prev; + while (prev = getPreviousEquivalentPoint(node, offset)) { + node = prev[0]; + offset = prev[1]; + } + + // "Return (node, offset)." + return [node, offset]; +} + +function getLastEquivalentPoint(node, offset) { + // "While (node, offset)'s next equivalent point is not null, set (node, + // offset) to its next equivalent point." + var next; + while (next = getNextEquivalentPoint(node, offset)) { + node = next[0]; + offset = next[1]; + } + + // "Return (node, offset)." + return [node, offset]; +} + +//@} +///// Block-extending a range ///// +//@{ + +// "A boundary point (node, offset) is a block start point if either node's +// parent is null and offset is zero; or node has a child with index offset − +// 1, and that child is either a visible block node or a visible br." +function isBlockStartPoint(node, offset) { + return (!node.parentNode && offset == 0) + || (0 <= offset - 1 + && offset - 1 < node.childNodes.length + && isVisible(node.childNodes[offset - 1]) + && (isBlockNode(node.childNodes[offset - 1]) + || isHtmlElement(node.childNodes[offset - 1], "br"))); +} + +// "A boundary point (node, offset) is a block end point if either node's +// parent is null and offset is node's length; or node has a child with index +// offset, and that child is a visible block node." +function isBlockEndPoint(node, offset) { + return (!node.parentNode && offset == getNodeLength(node)) + || (offset < node.childNodes.length + && isVisible(node.childNodes[offset]) + && isBlockNode(node.childNodes[offset])); +} + +// "A boundary point is a block boundary point if it is either a block start +// point or a block end point." +function isBlockBoundaryPoint(node, offset) { + return isBlockStartPoint(node, offset) + || isBlockEndPoint(node, offset); +} + +function blockExtend(range) { + // "Let start node, start offset, end node, and end offset be the start + // and end nodes and offsets of the range." + var startNode = range.startContainer; + var startOffset = range.startOffset; + var endNode = range.endContainer; + var endOffset = range.endOffset; + + // "If some ancestor container of start node is an li, set start offset to + // the index of the last such li in tree order, and set start node to that + // li's parent." + var liAncestors = getAncestors(startNode).concat(startNode) + .filter(function(ancestor) { return isHtmlElement(ancestor, "li") }) + .slice(-1); + if (liAncestors.length) { + startOffset = getNodeIndex(liAncestors[0]); + startNode = liAncestors[0].parentNode; + } + + // "If (start node, start offset) is not a block start point, repeat the + // following steps:" + if (!isBlockStartPoint(startNode, startOffset)) do { + // "If start offset is zero, set it to start node's index, then set + // start node to its parent." + if (startOffset == 0) { + startOffset = getNodeIndex(startNode); + startNode = startNode.parentNode; + + // "Otherwise, subtract one from start offset." + } else { + startOffset--; + } + + // "If (start node, start offset) is a block boundary point, break from + // this loop." + } while (!isBlockBoundaryPoint(startNode, startOffset)); + + // "While start offset is zero and start node's parent is not null, set + // start offset to start node's index, then set start node to its parent." + while (startOffset == 0 + && startNode.parentNode) { + startOffset = getNodeIndex(startNode); + startNode = startNode.parentNode; + } + + // "If some ancestor container of end node is an li, set end offset to one + // plus the index of the last such li in tree order, and set end node to + // that li's parent." + var liAncestors = getAncestors(endNode).concat(endNode) + .filter(function(ancestor) { return isHtmlElement(ancestor, "li") }) + .slice(-1); + if (liAncestors.length) { + endOffset = 1 + getNodeIndex(liAncestors[0]); + endNode = liAncestors[0].parentNode; + } + + // "If (end node, end offset) is not a block end point, repeat the + // following steps:" + if (!isBlockEndPoint(endNode, endOffset)) do { + // "If end offset is end node's length, set it to one plus end node's + // index, then set end node to its parent." + if (endOffset == getNodeLength(endNode)) { + endOffset = 1 + getNodeIndex(endNode); + endNode = endNode.parentNode; + + // "Otherwise, add one to end offset. + } else { + endOffset++; + } + + // "If (end node, end offset) is a block boundary point, break from + // this loop." + } while (!isBlockBoundaryPoint(endNode, endOffset)); + + // "While end offset is end node's length and end node's parent is not + // null, set end offset to one plus end node's index, then set end node to + // its parent." + while (endOffset == getNodeLength(endNode) + && endNode.parentNode) { + endOffset = 1 + getNodeIndex(endNode); + endNode = endNode.parentNode; + } + + // "Let new range be a new range whose start and end nodes and offsets + // are start node, start offset, end node, and end offset." + var newRange = startNode.ownerDocument.createRange(); + newRange.setStart(startNode, startOffset); + newRange.setEnd(endNode, endOffset); + + // "Return new range." + return newRange; +} + +function followsLineBreak(node) { + // "Let offset be zero." + var offset = 0; + + // "While (node, offset) is not a block boundary point:" + while (!isBlockBoundaryPoint(node, offset)) { + // "If node has a visible child with index offset minus one, return + // false." + if (0 <= offset - 1 + && offset - 1 < node.childNodes.length + && isVisible(node.childNodes[offset - 1])) { + return false; + } + + // "If offset is zero or node has no children, set offset to node's + // index, then set node to its parent." + if (offset == 0 + || !node.hasChildNodes()) { + offset = getNodeIndex(node); + node = node.parentNode; + + // "Otherwise, set node to its child with index offset minus one, then + // set offset to node's length." + } else { + node = node.childNodes[offset - 1]; + offset = getNodeLength(node); + } + } + + // "Return true." + return true; +} + +function precedesLineBreak(node) { + // "Let offset be node's length." + var offset = getNodeLength(node); + + // "While (node, offset) is not a block boundary point:" + while (!isBlockBoundaryPoint(node, offset)) { + // "If node has a visible child with index offset, return false." + if (offset < node.childNodes.length + && isVisible(node.childNodes[offset])) { + return false; + } + + // "If offset is node's length or node has no children, set offset to + // one plus node's index, then set node to its parent." + if (offset == getNodeLength(node) + || !node.hasChildNodes()) { + offset = 1 + getNodeIndex(node); + node = node.parentNode; + + // "Otherwise, set node to its child with index offset and set offset + // to zero." + } else { + node = node.childNodes[offset]; + offset = 0; + } + } + + // "Return true." + return true; +} + +//@} +///// Recording and restoring overrides ///// +//@{ + +function recordCurrentOverrides() { + // "Let overrides be a list of (string, string or boolean) ordered pairs, + // initially empty." + var overrides = []; + + // "If there is a value override for "createLink", add ("createLink", value + // override for "createLink") to overrides." + if (getValueOverride("createlink") !== undefined) { + overrides.push(["createlink", getValueOverride("createlink")]); + } + + // "For each command in the list "bold", "italic", "strikethrough", + // "subscript", "superscript", "underline", in order: if there is a state + // override for command, add (command, command's state override) to + // overrides." + ["bold", "italic", "strikethrough", "subscript", "superscript", + "underline"].forEach(function(command) { + if (getStateOverride(command) !== undefined) { + overrides.push([command, getStateOverride(command)]); + } + }); + + // "For each command in the list "fontName", "fontSize", "foreColor", + // "hiliteColor", in order: if there is a value override for command, add + // (command, command's value override) to overrides." + ["fontname", "fontsize", "forecolor", + "hilitecolor"].forEach(function(command) { + if (getValueOverride(command) !== undefined) { + overrides.push([command, getValueOverride(command)]); + } + }); + + // "Return overrides." + return overrides; +} + +function recordCurrentStatesAndValues() { + // "Let overrides be a list of (string, string or boolean) ordered pairs, + // initially empty." + var overrides = []; + + // "Let node be the first formattable node effectively contained in the + // active range, or null if there is none." + var node = getAllEffectivelyContainedNodes(getActiveRange()) + .filter(isFormattableNode)[0]; + + // "If node is null, return overrides." + if (!node) { + return overrides; + } + + // "Add ("createLink", node's effective command value for "createLink") to + // overrides." + overrides.push(["createlink", getEffectiveCommandValue(node, "createlink")]); + + // "For each command in the list "bold", "italic", "strikethrough", + // "subscript", "superscript", "underline", in order: if node's effective + // command value for command is one of its inline command activated values, + // add (command, true) to overrides, and otherwise add (command, false) to + // overrides." + ["bold", "italic", "strikethrough", "subscript", "superscript", + "underline"].forEach(function(command) { + if (commands[command].inlineCommandActivatedValues + .indexOf(getEffectiveCommandValue(node, command)) != -1) { + overrides.push([command, true]); + } else { + overrides.push([command, false]); + } + }); + + // "For each command in the list "fontName", "foreColor", "hiliteColor", in + // order: add (command, command's value) to overrides." + ["fontname", "fontsize", "forecolor", "hilitecolor"].forEach(function(command) { + overrides.push([command, commands[command].value()]); + }); + + // "Add ("fontSize", node's effective command value for "fontSize") to + // overrides." + overrides.push(["fontsize", getEffectiveCommandValue(node, "fontsize")]); + + // "Return overrides." + return overrides; +} + +function restoreStatesAndValues(overrides) { + // "Let node be the first formattable node effectively contained in the + // active range, or null if there is none." + var node = getAllEffectivelyContainedNodes(getActiveRange()) + .filter(isFormattableNode)[0]; + + // "If node is not null, then for each (command, override) pair in + // overrides, in order:" + if (node) { + for (var i = 0; i < overrides.length; i++) { + var command = overrides[i][0]; + var override = overrides[i][1]; + + // "If override is a boolean, and queryCommandState(command) + // returns something different from override, take the action for + // command, with value equal to the empty string." + if (typeof override == "boolean" + && myQueryCommandState(command) != override) { + commands[command].action(""); + + // "Otherwise, if override is a string, and command is neither + // "createLink" nor "fontSize", and queryCommandValue(command) + // returns something not equivalent to override, take the action + // for command, with value equal to override." + } else if (typeof override == "string" + && command != "createlink" + && command != "fontsize" + && !areEquivalentValues(command, myQueryCommandValue(command), override)) { + commands[command].action(override); + + // "Otherwise, if override is a string; and command is + // "createLink"; and either there is a value override for + // "createLink" that is not equal to override, or there is no value + // override for "createLink" and node's effective command value for + // "createLink" is not equal to override: take the action for + // "createLink", with value equal to override." + } else if (typeof override == "string" + && command == "createlink" + && ( + ( + getValueOverride("createlink") !== undefined + && getValueOverride("createlink") !== override + ) || ( + getValueOverride("createlink") === undefined + && getEffectiveCommandValue(node, "createlink") !== override + ) + )) { + commands.createlink.action(override); + + // "Otherwise, if override is a string; and command is "fontSize"; + // and either there is a value override for "fontSize" that is not + // equal to override, or there is no value override for "fontSize" + // and node's effective command value for "fontSize" is not loosely + // equivalent to override:" + } else if (typeof override == "string" + && command == "fontsize" + && ( + ( + getValueOverride("fontsize") !== undefined + && getValueOverride("fontsize") !== override + ) || ( + getValueOverride("fontsize") === undefined + && !areLooselyEquivalentValues(command, getEffectiveCommandValue(node, "fontsize"), override) + ) + )) { + // "Convert override to an integer number of pixels, and set + // override to the legacy font size for the result." + override = getLegacyFontSize(override); + + // "Take the action for "fontSize", with value equal to + // override." + commands.fontsize.action(override); + + // "Otherwise, continue this loop from the beginning." + } else { + continue; + } + + // "Set node to the first formattable node effectively contained in + // the active range, if there is one." + node = getAllEffectivelyContainedNodes(getActiveRange()) + .filter(isFormattableNode)[0] + || node; + } + + // "Otherwise, for each (command, override) pair in overrides, in order:" + } else { + for (var i = 0; i < overrides.length; i++) { + var command = overrides[i][0]; + var override = overrides[i][1]; + + // "If override is a boolean, set the state override for command to + // override." + if (typeof override == "boolean") { + setStateOverride(command, override); + } + + // "If override is a string, set the value override for command to + // override." + if (typeof override == "string") { + setValueOverride(command, override); + } + } + } +} + +//@} +///// Deleting the selection ///// +//@{ + +// The flags argument is a dictionary that can have blockMerging, +// stripWrappers, and/or direction as keys. +function deleteSelection(flags) { + if (flags === undefined) { + flags = {}; + } + + var blockMerging = "blockMerging" in flags ? Boolean(flags.blockMerging) : true; + var stripWrappers = "stripWrappers" in flags ? Boolean(flags.stripWrappers) : true; + var direction = "direction" in flags ? flags.direction : "forward"; + + // "If the active range is null, abort these steps and do nothing." + if (!getActiveRange()) { + return; + } + + // "Canonicalize whitespace at the active range's start." + canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset); + + // "Canonicalize whitespace at the active range's end." + canonicalizeWhitespace(getActiveRange().endContainer, getActiveRange().endOffset); + + // "Let (start node, start offset) be the last equivalent point for the + // active range's start." + var start = getLastEquivalentPoint(getActiveRange().startContainer, getActiveRange().startOffset); + var startNode = start[0]; + var startOffset = start[1]; + + // "Let (end node, end offset) be the first equivalent point for the active + // range's end." + var end = getFirstEquivalentPoint(getActiveRange().endContainer, getActiveRange().endOffset); + var endNode = end[0]; + var endOffset = end[1]; + + // "If (end node, end offset) is not after (start node, start offset):" + if (getPosition(endNode, endOffset, startNode, startOffset) !== "after") { + // "If direction is "forward", call collapseToStart() on the context + // object's Selection." + // + // Here and in a few other places, we check rangeCount to work around a + // WebKit bug: it will sometimes incorrectly remove ranges from the + // selection if nodes are removed, so collapseToStart() will throw. + // This will break everything if we're using an actual selection, but + // if getActiveRange() is really just returning globalRange and that's + // all we care about, it will work fine. I only add the extra check + // for errors I actually hit in testing. + if (direction == "forward") { + if (getSelection().rangeCount) { + getSelection().collapseToStart(); + } + getActiveRange().collapse(true); + + // "Otherwise, call collapseToEnd() on the context object's Selection." + } else { + getSelection().collapseToEnd(); + getActiveRange().collapse(false); + } + + // "Abort these steps." + return; + } + + // "If start node is a Text node and start offset is 0, set start offset to + // the index of start node, then set start node to its parent." + if (startNode.nodeType == Node.TEXT_NODE + && startOffset == 0) { + startOffset = getNodeIndex(startNode); + startNode = startNode.parentNode; + } + + // "If end node is a Text node and end offset is its length, set end offset + // to one plus the index of end node, then set end node to its parent." + if (endNode.nodeType == Node.TEXT_NODE + && endOffset == getNodeLength(endNode)) { + endOffset = 1 + getNodeIndex(endNode); + endNode = endNode.parentNode; + } + + // "Call collapse(start node, start offset) on the context object's + // Selection." + getSelection().collapse(startNode, startOffset); + getActiveRange().setStart(startNode, startOffset); + + // "Call extend(end node, end offset) on the context object's Selection." + getSelection().extend(endNode, endOffset); + getActiveRange().setEnd(endNode, endOffset); + + // "Let start block be the active range's start node." + var startBlock = getActiveRange().startContainer; + + // "While start block's parent is in the same editing host and start block + // is an inline node, set start block to its parent." + while (inSameEditingHost(startBlock, startBlock.parentNode) + && isInlineNode(startBlock)) { + startBlock = startBlock.parentNode; + } + + // "If start block is neither a block node nor an editing host, or "span" + // is not an allowed child of start block, or start block is a td or th, + // set start block to null." + if ((!isBlockNode(startBlock) && !isEditingHost(startBlock)) + || !isAllowedChild("span", startBlock) + || isHtmlElement(startBlock, ["td", "th"])) { + startBlock = null; + } + + // "Let end block be the active range's end node." + var endBlock = getActiveRange().endContainer; + + // "While end block's parent is in the same editing host and end block is + // an inline node, set end block to its parent." + while (inSameEditingHost(endBlock, endBlock.parentNode) + && isInlineNode(endBlock)) { + endBlock = endBlock.parentNode; + } + + // "If end block is neither a block node nor an editing host, or "span" is + // not an allowed child of end block, or end block is a td or th, set end + // block to null." + if ((!isBlockNode(endBlock) && !isEditingHost(endBlock)) + || !isAllowedChild("span", endBlock) + || isHtmlElement(endBlock, ["td", "th"])) { + endBlock = null; + } + + // "Record current states and values, and let overrides be the result." + var overrides = recordCurrentStatesAndValues(); + + // "If start node and end node are the same, and start node is an editable + // Text node:" + if (startNode == endNode + && isEditable(startNode) + && startNode.nodeType == Node.TEXT_NODE) { + // "Call deleteData(start offset, end offset − start offset) on start + // node." + startNode.deleteData(startOffset, endOffset - startOffset); + + // "Canonicalize whitespace at (start node, start offset), with fix + // collapsed space false." + canonicalizeWhitespace(startNode, startOffset, false); + + // "If direction is "forward", call collapseToStart() on the context + // object's Selection." + if (direction == "forward") { + if (getSelection().rangeCount) { + getSelection().collapseToStart(); + } + getActiveRange().collapse(true); + + // "Otherwise, call collapseToEnd() on the context object's Selection." + } else { + getSelection().collapseToEnd(); + getActiveRange().collapse(false); + } + + // "Restore states and values from overrides." + restoreStatesAndValues(overrides); + + // "Abort these steps." + return; + } + + // "If start node is an editable Text node, call deleteData() on it, with + // start offset as the first argument and (length of start node − start + // offset) as the second argument." + if (isEditable(startNode) + && startNode.nodeType == Node.TEXT_NODE) { + startNode.deleteData(startOffset, getNodeLength(startNode) - startOffset); + } + + // "Let node list be a list of nodes, initially empty." + // + // "For each node contained in the active range, append node to node list + // if the last member of node list (if any) is not an ancestor of node; + // node is editable; and node is not a thead, tbody, tfoot, tr, th, or td." + var nodeList = getContainedNodes(getActiveRange(), + function(node) { + return isEditable(node) + && !isHtmlElement(node, ["thead", "tbody", "tfoot", "tr", "th", "td"]); + } + ); + + // "For each node in node list:" + for (var i = 0; i < nodeList.length; i++) { + var node = nodeList[i]; + + // "Let parent be the parent of node." + var parent_ = node.parentNode; + + // "Remove node from parent." + parent_.removeChild(node); + + // "If the block node of parent has no visible children, and parent is + // editable or an editing host, call createElement("br") on the context + // object and append the result as the last child of parent." + if (![].some.call(getBlockNodeOf(parent_).childNodes, isVisible) + && (isEditable(parent_) || isEditingHost(parent_))) { + parent_.appendChild(document.createElement("br")); + } + + // "If strip wrappers is true or parent is not an ancestor container of + // start node, while parent is an editable inline node with length 0, + // let grandparent be the parent of parent, then remove parent from + // grandparent, then set parent to grandparent." + if (stripWrappers + || (!isAncestor(parent_, startNode) && parent_ != startNode)) { + while (isEditable(parent_) + && isInlineNode(parent_) + && getNodeLength(parent_) == 0) { + var grandparent = parent_.parentNode; + grandparent.removeChild(parent_); + parent_ = grandparent; + } + } + } + + // "If end node is an editable Text node, call deleteData(0, end offset) on + // it." + if (isEditable(endNode) + && endNode.nodeType == Node.TEXT_NODE) { + endNode.deleteData(0, endOffset); + } + + // "Canonicalize whitespace at the active range's start, with fix collapsed + // space false." + canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset, false); + + // "Canonicalize whitespace at the active range's end, with fix collapsed + // space false." + canonicalizeWhitespace(getActiveRange().endContainer, getActiveRange().endOffset, false); + + // "If block merging is false, or start block or end block is null, or + // start block is not in the same editing host as end block, or start block + // and end block are the same:" + if (!blockMerging + || !startBlock + || !endBlock + || !inSameEditingHost(startBlock, endBlock) + || startBlock == endBlock) { + // "If direction is "forward", call collapseToStart() on the context + // object's Selection." + if (direction == "forward") { + if (getSelection().rangeCount) { + getSelection().collapseToStart(); + } + getActiveRange().collapse(true); + + // "Otherwise, call collapseToEnd() on the context object's Selection." + } else { + if (getSelection().rangeCount) { + getSelection().collapseToEnd(); + } + getActiveRange().collapse(false); + } + + // "Restore states and values from overrides." + restoreStatesAndValues(overrides); + + // "Abort these steps." + return; + } + + // "If start block has one child, which is a collapsed block prop, remove + // its child from it." + if (startBlock.children.length == 1 + && isCollapsedBlockProp(startBlock.firstChild)) { + startBlock.removeChild(startBlock.firstChild); + } + + // "If start block is an ancestor of end block:" + if (isAncestor(startBlock, endBlock)) { + // "Let reference node be end block." + var referenceNode = endBlock; + + // "While reference node is not a child of start block, set reference + // node to its parent." + while (referenceNode.parentNode != startBlock) { + referenceNode = referenceNode.parentNode; + } + + // "Call collapse() on the context object's Selection, with first + // argument start block and second argument the index of reference + // node." + getSelection().collapse(startBlock, getNodeIndex(referenceNode)); + getActiveRange().setStart(startBlock, getNodeIndex(referenceNode)); + getActiveRange().collapse(true); + + // "If end block has no children:" + if (!endBlock.hasChildNodes()) { + // "While end block is editable and is the only child of its parent + // and is not a child of start block, let parent equal end block, + // then remove end block from parent, then set end block to + // parent." + while (isEditable(endBlock) + && endBlock.parentNode.childNodes.length == 1 + && endBlock.parentNode != startBlock) { + var parent_ = endBlock; + parent_.removeChild(endBlock); + endBlock = parent_; + } + + // "If end block is editable and is not an inline node, and its + // previousSibling and nextSibling are both inline nodes, call + // createElement("br") on the context object and insert it into end + // block's parent immediately after end block." + if (isEditable(endBlock) + && !isInlineNode(endBlock) + && isInlineNode(endBlock.previousSibling) + && isInlineNode(endBlock.nextSibling)) { + endBlock.parentNode.insertBefore(document.createElement("br"), endBlock.nextSibling); + } + + // "If end block is editable, remove it from its parent." + if (isEditable(endBlock)) { + endBlock.parentNode.removeChild(endBlock); + } + + // "Restore states and values from overrides." + restoreStatesAndValues(overrides); + + // "Abort these steps." + return; + } + + // "If end block's firstChild is not an inline node, restore states and + // values from overrides, then abort these steps." + if (!isInlineNode(endBlock.firstChild)) { + restoreStatesAndValues(overrides); + return; + } + + // "Let children be a list of nodes, initially empty." + var children = []; + + // "Append the first child of end block to children." + children.push(endBlock.firstChild); + + // "While children's last member is not a br, and children's last + // member's nextSibling is an inline node, append children's last + // member's nextSibling to children." + while (!isHtmlElement(children[children.length - 1], "br") + && isInlineNode(children[children.length - 1].nextSibling)) { + children.push(children[children.length - 1].nextSibling); + } + + // "Record the values of children, and let values be the result." + var values = recordValues(children); + + // "While children's first member's parent is not start block, split + // the parent of children." + while (children[0].parentNode != startBlock) { + splitParent(children); + } + + // "If children's first member's previousSibling is an editable br, + // remove that br from its parent." + if (isEditable(children[0].previousSibling) + && isHtmlElement(children[0].previousSibling, "br")) { + children[0].parentNode.removeChild(children[0].previousSibling); + } + + // "Otherwise, if start block is a descendant of end block:" + } else if (isDescendant(startBlock, endBlock)) { + // "Call collapse() on the context object's Selection, with first + // argument start block and second argument start block's length." + getSelection().collapse(startBlock, getNodeLength(startBlock)); + getActiveRange().setStart(startBlock, getNodeLength(startBlock)); + getActiveRange().collapse(true); + + // "Let reference node be start block." + var referenceNode = startBlock; + + // "While reference node is not a child of end block, set reference + // node to its parent." + while (referenceNode.parentNode != endBlock) { + referenceNode = referenceNode.parentNode; + } + + // "If reference node's nextSibling is an inline node and start block's + // lastChild is a br, remove start block's lastChild from it." + if (isInlineNode(referenceNode.nextSibling) + && isHtmlElement(startBlock.lastChild, "br")) { + startBlock.removeChild(startBlock.lastChild); + } + + // "Let nodes to move be a list of nodes, initially empty." + var nodesToMove = []; + + // "If reference node's nextSibling is neither null nor a block node, + // append it to nodes to move." + if (referenceNode.nextSibling + && !isBlockNode(referenceNode.nextSibling)) { + nodesToMove.push(referenceNode.nextSibling); + } + + // "While nodes to move is nonempty and its last member isn't a br and + // its last member's nextSibling is neither null nor a block node, + // append its last member's nextSibling to nodes to move." + if (nodesToMove.length + && !isHtmlElement(nodesToMove[nodesToMove.length - 1], "br") + && nodesToMove[nodesToMove.length - 1].nextSibling + && !isBlockNode(nodesToMove[nodesToMove.length - 1].nextSibling)) { + nodesToMove.push(nodesToMove[nodesToMove.length - 1].nextSibling); + } + + // "Record the values of nodes to move, and let values be the result." + var values = recordValues(nodesToMove); + + // "For each node in nodes to move, append node as the last child of + // start block, preserving ranges." + nodesToMove.forEach(function(node) { + movePreservingRanges(node, startBlock, -1); + }); + + // "Otherwise:" + } else { + // "Call collapse() on the context object's Selection, with first + // argument start block and second argument start block's length." + getSelection().collapse(startBlock, getNodeLength(startBlock)); + getActiveRange().setStart(startBlock, getNodeLength(startBlock)); + getActiveRange().collapse(true); + + // "If end block's firstChild is an inline node and start block's + // lastChild is a br, remove start block's lastChild from it." + if (isInlineNode(endBlock.firstChild) + && isHtmlElement(startBlock.lastChild, "br")) { + startBlock.removeChild(startBlock.lastChild); + } + + // "Record the values of end block's children, and let values be the + // result." + var values = recordValues([].slice.call(endBlock.childNodes)); + + // "While end block has children, append the first child of end block + // to start block, preserving ranges." + while (endBlock.hasChildNodes()) { + movePreservingRanges(endBlock.firstChild, startBlock, -1); + } + + // "While end block has no children, let parent be the parent of end + // block, then remove end block from parent, then set end block to + // parent." + while (!endBlock.hasChildNodes()) { + var parent_ = endBlock.parentNode; + parent_.removeChild(endBlock); + endBlock = parent_; + } + } + + // "Let ancestor be start block." + var ancestor = startBlock; + + // "While ancestor has an inclusive ancestor ol in the same editing host + // whose nextSibling is also an ol in the same editing host, or an + // inclusive ancestor ul in the same editing host whose nextSibling is also + // a ul in the same editing host:" + while (getInclusiveAncestors(ancestor).some(function(node) { + return inSameEditingHost(ancestor, node) + && ( + (isHtmlElement(node, "ol") && isHtmlElement(node.nextSibling, "ol")) + || (isHtmlElement(node, "ul") && isHtmlElement(node.nextSibling, "ul")) + ) && inSameEditingHost(ancestor, node.nextSibling); + })) { + // "While ancestor and its nextSibling are not both ols in the same + // editing host, and are also not both uls in the same editing host, + // set ancestor to its parent." + while (!( + isHtmlElement(ancestor, "ol") + && isHtmlElement(ancestor.nextSibling, "ol") + && inSameEditingHost(ancestor, ancestor.nextSibling) + ) && !( + isHtmlElement(ancestor, "ul") + && isHtmlElement(ancestor.nextSibling, "ul") + && inSameEditingHost(ancestor, ancestor.nextSibling) + )) { + ancestor = ancestor.parentNode; + } + + // "While ancestor's nextSibling has children, append ancestor's + // nextSibling's firstChild as the last child of ancestor, preserving + // ranges." + while (ancestor.nextSibling.hasChildNodes()) { + movePreservingRanges(ancestor.nextSibling.firstChild, ancestor, -1); + } + + // "Remove ancestor's nextSibling from its parent." + ancestor.parentNode.removeChild(ancestor.nextSibling); + } + + // "Restore the values from values." + restoreValues(values); + + // "If start block has no children, call createElement("br") on the context + // object and append the result as the last child of start block." + if (!startBlock.hasChildNodes()) { + startBlock.appendChild(document.createElement("br")); + } + + // "Remove extraneous line breaks at the end of start block." + removeExtraneousLineBreaksAtTheEndOf(startBlock); + + // "Restore states and values from overrides." + restoreStatesAndValues(overrides); +} + + +//@} +///// Splitting a node list's parent ///// +//@{ + +function splitParent(nodeList) { + // "Let original parent be the parent of the first member of node list." + var originalParent = nodeList[0].parentNode; + + // "If original parent is not editable or its parent is null, do nothing + // and abort these steps." + if (!isEditable(originalParent) + || !originalParent.parentNode) { + return; + } + + // "If the first child of original parent is in node list, remove + // extraneous line breaks before original parent." + if (nodeList.indexOf(originalParent.firstChild) != -1) { + removeExtraneousLineBreaksBefore(originalParent); + } + + // "If the first child of original parent is in node list, and original + // parent follows a line break, set follows line break to true. Otherwise, + // set follows line break to false." + var followsLineBreak_ = nodeList.indexOf(originalParent.firstChild) != -1 + && followsLineBreak(originalParent); + + // "If the last child of original parent is in node list, and original + // parent precedes a line break, set precedes line break to true. + // Otherwise, set precedes line break to false." + var precedesLineBreak_ = nodeList.indexOf(originalParent.lastChild) != -1 + && precedesLineBreak(originalParent); + + // "If the first child of original parent is not in node list, but its last + // child is:" + if (nodeList.indexOf(originalParent.firstChild) == -1 + && nodeList.indexOf(originalParent.lastChild) != -1) { + // "For each node in node list, in reverse order, insert node into the + // parent of original parent immediately after original parent, + // preserving ranges." + for (var i = nodeList.length - 1; i >= 0; i--) { + movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent)); + } + + // "If precedes line break is true, and the last member of node list + // does not precede a line break, call createElement("br") on the + // context object and insert the result immediately after the last + // member of node list." + if (precedesLineBreak_ + && !precedesLineBreak(nodeList[nodeList.length - 1])) { + nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling); + } + + // "Remove extraneous line breaks at the end of original parent." + removeExtraneousLineBreaksAtTheEndOf(originalParent); + + // "Abort these steps." + return; + } + + // "If the first child of original parent is not in node list:" + if (nodeList.indexOf(originalParent.firstChild) == -1) { + // "Let cloned parent be the result of calling cloneNode(false) on + // original parent." + var clonedParent = originalParent.cloneNode(false); + + // "If original parent has an id attribute, unset it." + originalParent.removeAttribute("id"); + + // "Insert cloned parent into the parent of original parent immediately + // before original parent." + originalParent.parentNode.insertBefore(clonedParent, originalParent); + + // "While the previousSibling of the first member of node list is not + // null, append the first child of original parent as the last child of + // cloned parent, preserving ranges." + while (nodeList[0].previousSibling) { + movePreservingRanges(originalParent.firstChild, clonedParent, clonedParent.childNodes.length); + } + } + + // "For each node in node list, insert node into the parent of original + // parent immediately before original parent, preserving ranges." + for (var i = 0; i < nodeList.length; i++) { + movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent)); + } + + // "If follows line break is true, and the first member of node list does + // not follow a line break, call createElement("br") on the context object + // and insert the result immediately before the first member of node list." + if (followsLineBreak_ + && !followsLineBreak(nodeList[0])) { + nodeList[0].parentNode.insertBefore(document.createElement("br"), nodeList[0]); + } + + // "If the last member of node list is an inline node other than a br, and + // the first child of original parent is a br, and original parent is not + // an inline node, remove the first child of original parent from original + // parent." + if (isInlineNode(nodeList[nodeList.length - 1]) + && !isHtmlElement(nodeList[nodeList.length - 1], "br") + && isHtmlElement(originalParent.firstChild, "br") + && !isInlineNode(originalParent)) { + originalParent.removeChild(originalParent.firstChild); + } + + // "If original parent has no children:" + if (!originalParent.hasChildNodes()) { + // "Remove original parent from its parent." + originalParent.parentNode.removeChild(originalParent); + + // "If precedes line break is true, and the last member of node list + // does not precede a line break, call createElement("br") on the + // context object and insert the result immediately after the last + // member of node list." + if (precedesLineBreak_ + && !precedesLineBreak(nodeList[nodeList.length - 1])) { + nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling); + } + + // "Otherwise, remove extraneous line breaks before original parent." + } else { + removeExtraneousLineBreaksBefore(originalParent); + } + + // "If node list's last member's nextSibling is null, but its parent is not + // null, remove extraneous line breaks at the end of node list's last + // member's parent." + if (!nodeList[nodeList.length - 1].nextSibling + && nodeList[nodeList.length - 1].parentNode) { + removeExtraneousLineBreaksAtTheEndOf(nodeList[nodeList.length - 1].parentNode); + } +} + +// "To remove a node node while preserving its descendants, split the parent of +// node's children if it has any. If it has no children, instead remove it from +// its parent." +function removePreservingDescendants(node) { + if (node.hasChildNodes()) { + splitParent([].slice.call(node.childNodes)); + } else { + node.parentNode.removeChild(node); + } +} + + +//@} +///// Canonical space sequences ///// +//@{ + +function canonicalSpaceSequence(n, nonBreakingStart, nonBreakingEnd) { + // "If n is zero, return the empty string." + if (n == 0) { + return ""; + } + + // "If n is one and both non-breaking start and non-breaking end are false, + // return a single space (U+0020)." + if (n == 1 && !nonBreakingStart && !nonBreakingEnd) { + return " "; + } + + // "If n is one, return a single non-breaking space (U+00A0)." + if (n == 1) { + return "\xa0"; + } + + // "Let buffer be the empty string." + var buffer = ""; + + // "If non-breaking start is true, let repeated pair be U+00A0 U+0020. + // Otherwise, let it be U+0020 U+00A0." + var repeatedPair; + if (nonBreakingStart) { + repeatedPair = "\xa0 "; + } else { + repeatedPair = " \xa0"; + } + + // "While n is greater than three, append repeated pair to buffer and + // subtract two from n." + while (n > 3) { + buffer += repeatedPair; + n -= 2; + } + + // "If n is three, append a three-element string to buffer depending on + // non-breaking start and non-breaking end:" + if (n == 3) { + buffer += + !nonBreakingStart && !nonBreakingEnd ? " \xa0 " + : nonBreakingStart && !nonBreakingEnd ? "\xa0\xa0 " + : !nonBreakingStart && nonBreakingEnd ? " \xa0\xa0" + : nonBreakingStart && nonBreakingEnd ? "\xa0 \xa0" + : "impossible"; + + // "Otherwise, append a two-element string to buffer depending on + // non-breaking start and non-breaking end:" + } else { + buffer += + !nonBreakingStart && !nonBreakingEnd ? "\xa0 " + : nonBreakingStart && !nonBreakingEnd ? "\xa0 " + : !nonBreakingStart && nonBreakingEnd ? " \xa0" + : nonBreakingStart && nonBreakingEnd ? "\xa0\xa0" + : "impossible"; + } + + // "Return buffer." + return buffer; +} + +function canonicalizeWhitespace(node, offset, fixCollapsedSpace) { + if (fixCollapsedSpace === undefined) { + // "an optional boolean argument fix collapsed space that defaults to + // true" + fixCollapsedSpace = true; + } + + // "If node is neither editable nor an editing host, abort these steps." + if (!isEditable(node) && !isEditingHost(node)) { + return; + } + + // "Let start node equal node and let start offset equal offset." + var startNode = node; + var startOffset = offset; + + // "Repeat the following steps:" + while (true) { + // "If start node has a child in the same editing host with index start + // offset minus one, set start node to that child, then set start + // offset to start node's length." + if (0 <= startOffset - 1 + && inSameEditingHost(startNode, startNode.childNodes[startOffset - 1])) { + startNode = startNode.childNodes[startOffset - 1]; + startOffset = getNodeLength(startNode); + + // "Otherwise, if start offset is zero and start node does not follow a + // line break and start node's parent is in the same editing host, set + // start offset to start node's index, then set start node to its + // parent." + } else if (startOffset == 0 + && !followsLineBreak(startNode) + && inSameEditingHost(startNode, startNode.parentNode)) { + startOffset = getNodeIndex(startNode); + startNode = startNode.parentNode; + + // "Otherwise, if start node is a Text node and its parent's resolved + // value for "white-space" is neither "pre" nor "pre-wrap" and start + // offset is not zero and the (start offset − 1)st element of start + // node's data is a space (0x0020) or non-breaking space (0x00A0), + // subtract one from start offset." + } else if (startNode.nodeType == Node.TEXT_NODE + && ["pre", "pre-wrap"].indexOf(getComputedStyle(startNode.parentNode).whiteSpace) == -1 + && startOffset != 0 + && /[ \xa0]/.test(startNode.data[startOffset - 1])) { + startOffset--; + + // "Otherwise, break from this loop." + } else { + break; + } + } + + // "Let end node equal start node and end offset equal start offset." + var endNode = startNode; + var endOffset = startOffset; + + // "Let length equal zero." + var length = 0; + + // "Let collapse spaces be true if start offset is zero and start node + // follows a line break, otherwise false." + var collapseSpaces = startOffset == 0 && followsLineBreak(startNode); + + // "Repeat the following steps:" + while (true) { + // "If end node has a child in the same editing host with index end + // offset, set end node to that child, then set end offset to zero." + if (endOffset < endNode.childNodes.length + && inSameEditingHost(endNode, endNode.childNodes[endOffset])) { + endNode = endNode.childNodes[endOffset]; + endOffset = 0; + + // "Otherwise, if end offset is end node's length and end node does not + // precede a line break and end node's parent is in the same editing + // host, set end offset to one plus end node's index, then set end node + // to its parent." + } else if (endOffset == getNodeLength(endNode) + && !precedesLineBreak(endNode) + && inSameEditingHost(endNode, endNode.parentNode)) { + endOffset = 1 + getNodeIndex(endNode); + endNode = endNode.parentNode; + + // "Otherwise, if end node is a Text node and its parent's resolved + // value for "white-space" is neither "pre" nor "pre-wrap" and end + // offset is not end node's length and the end offsetth element of + // end node's data is a space (0x0020) or non-breaking space (0x00A0):" + } else if (endNode.nodeType == Node.TEXT_NODE + && ["pre", "pre-wrap"].indexOf(getComputedStyle(endNode.parentNode).whiteSpace) == -1 + && endOffset != getNodeLength(endNode) + && /[ \xa0]/.test(endNode.data[endOffset])) { + // "If fix collapsed space is true, and collapse spaces is true, + // and the end offsetth code unit of end node's data is a space + // (0x0020): call deleteData(end offset, 1) on end node, then + // continue this loop from the beginning." + if (fixCollapsedSpace + && collapseSpaces + && " " == endNode.data[endOffset]) { + endNode.deleteData(endOffset, 1); + continue; + } + + // "Set collapse spaces to true if the end offsetth element of end + // node's data is a space (0x0020), false otherwise." + collapseSpaces = " " == endNode.data[endOffset]; + + // "Add one to end offset." + endOffset++; + + // "Add one to length." + length++; + + // "Otherwise, break from this loop." + } else { + break; + } + } + + // "If fix collapsed space is true, then while (start node, start offset) + // is before (end node, end offset):" + if (fixCollapsedSpace) { + while (getPosition(startNode, startOffset, endNode, endOffset) == "before") { + // "If end node has a child in the same editing host with index end + // offset − 1, set end node to that child, then set end offset to end + // node's length." + if (0 <= endOffset - 1 + && endOffset - 1 < endNode.childNodes.length + && inSameEditingHost(endNode, endNode.childNodes[endOffset - 1])) { + endNode = endNode.childNodes[endOffset - 1]; + endOffset = getNodeLength(endNode); + + // "Otherwise, if end offset is zero and end node's parent is in the + // same editing host, set end offset to end node's index, then set end + // node to its parent." + } else if (endOffset == 0 + && inSameEditingHost(endNode, endNode.parentNode)) { + endOffset = getNodeIndex(endNode); + endNode = endNode.parentNode; + + // "Otherwise, if end node is a Text node and its parent's resolved + // value for "white-space" is neither "pre" nor "pre-wrap" and end + // offset is end node's length and the last code unit of end node's + // data is a space (0x0020) and end node precedes a line break:" + } else if (endNode.nodeType == Node.TEXT_NODE + && ["pre", "pre-wrap"].indexOf(getComputedStyle(endNode.parentNode).whiteSpace) == -1 + && endOffset == getNodeLength(endNode) + && endNode.data[endNode.data.length - 1] == " " + && precedesLineBreak(endNode)) { + // "Subtract one from end offset." + endOffset--; + + // "Subtract one from length." + length--; + + // "Call deleteData(end offset, 1) on end node." + endNode.deleteData(endOffset, 1); + + // "Otherwise, break from this loop." + } else { + break; + } + } + } + + // "Let replacement whitespace be the canonical space sequence of length + // length. non-breaking start is true if start offset is zero and start + // node follows a line break, and false otherwise. non-breaking end is true + // if end offset is end node's length and end node precedes a line break, + // and false otherwise." + var replacementWhitespace = canonicalSpaceSequence(length, + startOffset == 0 && followsLineBreak(startNode), + endOffset == getNodeLength(endNode) && precedesLineBreak(endNode)); + + // "While (start node, start offset) is before (end node, end offset):" + while (getPosition(startNode, startOffset, endNode, endOffset) == "before") { + // "If start node has a child with index start offset, set start node + // to that child, then set start offset to zero." + if (startOffset < startNode.childNodes.length) { + startNode = startNode.childNodes[startOffset]; + startOffset = 0; + + // "Otherwise, if start node is not a Text node or if start offset is + // start node's length, set start offset to one plus start node's + // index, then set start node to its parent." + } else if (startNode.nodeType != Node.TEXT_NODE + || startOffset == getNodeLength(startNode)) { + startOffset = 1 + getNodeIndex(startNode); + startNode = startNode.parentNode; + + // "Otherwise:" + } else { + // "Remove the first element from replacement whitespace, and let + // element be that element." + var element = replacementWhitespace[0]; + replacementWhitespace = replacementWhitespace.slice(1); + + // "If element is not the same as the start offsetth element of + // start node's data:" + if (element != startNode.data[startOffset]) { + // "Call insertData(start offset, element) on start node." + startNode.insertData(startOffset, element); + + // "Call deleteData(start offset + 1, 1) on start node." + startNode.deleteData(startOffset + 1, 1); + } + + // "Add one to start offset." + startOffset++; + } + } +} + + +//@} +///// Indenting and outdenting ///// +//@{ + +function indentNodes(nodeList) { + // "If node list is empty, do nothing and abort these steps." + if (!nodeList.length) { + return; + } + + // "Let first node be the first member of node list." + var firstNode = nodeList[0]; + + // "If first node's parent is an ol or ul:" + if (isHtmlElement(firstNode.parentNode, ["OL", "UL"])) { + // "Let tag be the local name of the parent of first node." + var tag = firstNode.parentNode.tagName; + + // "Wrap node list, with sibling criteria returning true for an HTML + // element with local name tag and false otherwise, and new parent + // instructions returning the result of calling createElement(tag) on + // the ownerDocument of first node." + wrap(nodeList, + function(node) { return isHtmlElement(node, tag) }, + function() { return firstNode.ownerDocument.createElement(tag) }); + + // "Abort these steps." + return; + } + + // "Wrap node list, with sibling criteria returning true for a simple + // indentation element and false otherwise, and new parent instructions + // returning the result of calling createElement("blockquote") on the + // ownerDocument of first node. Let new parent be the result." + var newParent = wrap(nodeList, + function(node) { return isSimpleIndentationElement(node) }, + function() { return firstNode.ownerDocument.createElement("blockquote") }); + + // "Fix disallowed ancestors of new parent." + fixDisallowedAncestors(newParent); +} + +function outdentNode(node) { + // "If node is not editable, abort these steps." + if (!isEditable(node)) { + return; + } + + // "If node is a simple indentation element, remove node, preserving its + // descendants. Then abort these steps." + if (isSimpleIndentationElement(node)) { + removePreservingDescendants(node); + return; + } + + // "If node is an indentation element:" + if (isIndentationElement(node)) { + // "Unset the dir attribute of node, if any." + node.removeAttribute("dir"); + + // "Unset the margin, padding, and border CSS properties of node." + node.style.margin = ""; + node.style.padding = ""; + node.style.border = ""; + if (node.getAttribute("style") == "" + // Crazy WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=68551 + || node.getAttribute("style") == "border-width: initial; border-color: initial; ") { + node.removeAttribute("style"); + } + + // "Set the tag name of node to "div"." + setTagName(node, "div"); + + // "Abort these steps." + return; + } + + // "Let current ancestor be node's parent." + var currentAncestor = node.parentNode; + + // "Let ancestor list be a list of nodes, initially empty." + var ancestorList = []; + + // "While current ancestor is an editable Element that is neither a simple + // indentation element nor an ol nor a ul, append current ancestor to + // ancestor list and then set current ancestor to its parent." + while (isEditable(currentAncestor) + && currentAncestor.nodeType == Node.ELEMENT_NODE + && !isSimpleIndentationElement(currentAncestor) + && !isHtmlElement(currentAncestor, ["ol", "ul"])) { + ancestorList.push(currentAncestor); + currentAncestor = currentAncestor.parentNode; + } + + // "If current ancestor is not an editable simple indentation element:" + if (!isEditable(currentAncestor) + || !isSimpleIndentationElement(currentAncestor)) { + // "Let current ancestor be node's parent." + currentAncestor = node.parentNode; + + // "Let ancestor list be the empty list." + ancestorList = []; + + // "While current ancestor is an editable Element that is neither an + // indentation element nor an ol nor a ul, append current ancestor to + // ancestor list and then set current ancestor to its parent." + while (isEditable(currentAncestor) + && currentAncestor.nodeType == Node.ELEMENT_NODE + && !isIndentationElement(currentAncestor) + && !isHtmlElement(currentAncestor, ["ol", "ul"])) { + ancestorList.push(currentAncestor); + currentAncestor = currentAncestor.parentNode; + } + } + + // "If node is an ol or ul and current ancestor is not an editable + // indentation element:" + if (isHtmlElement(node, ["OL", "UL"]) + && (!isEditable(currentAncestor) + || !isIndentationElement(currentAncestor))) { + // "Unset the reversed, start, and type attributes of node, if any are + // set." + node.removeAttribute("reversed"); + node.removeAttribute("start"); + node.removeAttribute("type"); + + // "Let children be the children of node." + var children = [].slice.call(node.childNodes); + + // "If node has attributes, and its parent is not an ol or ul, set the + // tag name of node to "div"." + if (node.attributes.length + && !isHtmlElement(node.parentNode, ["OL", "UL"])) { + setTagName(node, "div"); + + // "Otherwise:" + } else { + // "Record the values of node's children, and let values be the + // result." + var values = recordValues([].slice.call(node.childNodes)); + + // "Remove node, preserving its descendants." + removePreservingDescendants(node); + + // "Restore the values from values." + restoreValues(values); + } + + // "Fix disallowed ancestors of each member of children." + for (var i = 0; i < children.length; i++) { + fixDisallowedAncestors(children[i]); + } + + // "Abort these steps." + return; + } + + // "If current ancestor is not an editable indentation element, abort these + // steps." + if (!isEditable(currentAncestor) + || !isIndentationElement(currentAncestor)) { + return; + } + + // "Append current ancestor to ancestor list." + ancestorList.push(currentAncestor); + + // "Let original ancestor be current ancestor." + var originalAncestor = currentAncestor; + + // "While ancestor list is not empty:" + while (ancestorList.length) { + // "Let current ancestor be the last member of ancestor list." + // + // "Remove the last member of ancestor list." + currentAncestor = ancestorList.pop(); + + // "Let target be the child of current ancestor that is equal to either + // node or the last member of ancestor list." + var target = node.parentNode == currentAncestor + ? node + : ancestorList[ancestorList.length - 1]; + + // "If target is an inline node that is not a br, and its nextSibling + // is a br, remove target's nextSibling from its parent." + if (isInlineNode(target) + && !isHtmlElement(target, "BR") + && isHtmlElement(target.nextSibling, "BR")) { + target.parentNode.removeChild(target.nextSibling); + } + + // "Let preceding siblings be the preceding siblings of target, and let + // following siblings be the following siblings of target." + var precedingSiblings = [].slice.call(currentAncestor.childNodes, 0, getNodeIndex(target)); + var followingSiblings = [].slice.call(currentAncestor.childNodes, 1 + getNodeIndex(target)); + + // "Indent preceding siblings." + indentNodes(precedingSiblings); + + // "Indent following siblings." + indentNodes(followingSiblings); + } + + // "Outdent original ancestor." + outdentNode(originalAncestor); +} + + +//@} +///// Toggling lists ///// +//@{ + +function toggleLists(tagName) { + // "Let mode be "disable" if the selection's list state is tag name, and + // "enable" otherwise." + var mode = getSelectionListState() == tagName ? "disable" : "enable"; + + var range = getActiveRange(); + tagName = tagName.toUpperCase(); + + // "Let other tag name be "ol" if tag name is "ul", and "ul" if tag name is + // "ol"." + var otherTagName = tagName == "OL" ? "UL" : "OL"; + + // "Let items be a list of all lis that are ancestor containers of the + // range's start and/or end node." + // + // It's annoying to get this in tree order using functional stuff without + // doing getDescendants(document), which is slow, so I do it imperatively. + var items = []; + (function(){ + for ( + var ancestorContainer = range.endContainer; + ancestorContainer != range.commonAncestorContainer; + ancestorContainer = ancestorContainer.parentNode + ) { + if (isHtmlElement(ancestorContainer, "li")) { + items.unshift(ancestorContainer); + } + } + for ( + var ancestorContainer = range.startContainer; + ancestorContainer; + ancestorContainer = ancestorContainer.parentNode + ) { + if (isHtmlElement(ancestorContainer, "li")) { + items.unshift(ancestorContainer); + } + } + })(); + + // "For each item in items, normalize sublists of item." + items.forEach(normalizeSublists); + + // "Block-extend the range, and let new range be the result." + var newRange = blockExtend(range); + + // "If mode is "enable", then let lists to convert consist of every + // editable HTML element with local name other tag name that is contained + // in new range, and for every list in lists to convert:" + if (mode == "enable") { + getAllContainedNodes(newRange, function(node) { + return isEditable(node) + && isHtmlElement(node, otherTagName); + }).forEach(function(list) { + // "If list's previousSibling or nextSibling is an editable HTML + // element with local name tag name:" + if ((isEditable(list.previousSibling) && isHtmlElement(list.previousSibling, tagName)) + || (isEditable(list.nextSibling) && isHtmlElement(list.nextSibling, tagName))) { + // "Let children be list's children." + var children = [].slice.call(list.childNodes); + + // "Record the values of children, and let values be the + // result." + var values = recordValues(children); + + // "Split the parent of children." + splitParent(children); + + // "Wrap children, with sibling criteria returning true for an + // HTML element with local name tag name and false otherwise." + wrap(children, function(node) { return isHtmlElement(node, tagName) }); + + // "Restore the values from values." + restoreValues(values); + + // "Otherwise, set the tag name of list to tag name." + } else { + setTagName(list, tagName); + } + }); + } + + // "Let node list be a list of nodes, initially empty." + // + // "For each node node contained in new range, if node is editable; the + // last member of node list (if any) is not an ancestor of node; node + // is not an indentation element; and either node is an ol or ul, or its + // parent is an ol or ul, or it is an allowed child of "li"; then append + // node to node list." + var nodeList = getContainedNodes(newRange, function(node) { + return isEditable(node) + && !isIndentationElement(node) + && (isHtmlElement(node, ["OL", "UL"]) + || isHtmlElement(node.parentNode, ["OL", "UL"]) + || isAllowedChild(node, "li")); + }); + + // "If mode is "enable", remove from node list any ol or ul whose parent is + // not also an ol or ul." + if (mode == "enable") { + nodeList = nodeList.filter(function(node) { + return !isHtmlElement(node, ["ol", "ul"]) + || isHtmlElement(node.parentNode, ["ol", "ul"]); + }); + } + + // "If mode is "disable", then while node list is not empty:" + if (mode == "disable") { + while (nodeList.length) { + // "Let sublist be an empty list of nodes." + var sublist = []; + + // "Remove the first member from node list and append it to + // sublist." + sublist.push(nodeList.shift()); + + // "If the first member of sublist is an HTML element with local + // name tag name, outdent it and continue this loop from the + // beginning." + if (isHtmlElement(sublist[0], tagName)) { + outdentNode(sublist[0]); + continue; + } + + // "While node list is not empty, and the first member of node list + // is the nextSibling of the last member of sublist and is not an + // HTML element with local name tag name, remove the first member + // from node list and append it to sublist." + while (nodeList.length + && nodeList[0] == sublist[sublist.length - 1].nextSibling + && !isHtmlElement(nodeList[0], tagName)) { + sublist.push(nodeList.shift()); + } + + // "Record the values of sublist, and let values be the result." + var values = recordValues(sublist); + + // "Split the parent of sublist." + splitParent(sublist); + + // "Fix disallowed ancestors of each member of sublist." + for (var i = 0; i < sublist.length; i++) { + fixDisallowedAncestors(sublist[i]); + } + + // "Restore the values from values." + restoreValues(values); + } + + // "Otherwise, while node list is not empty:" + } else { + while (nodeList.length) { + // "Let sublist be an empty list of nodes." + var sublist = []; + + // "While either sublist is empty, or node list is not empty and + // its first member is the nextSibling of sublist's last member:" + while (!sublist.length + || (nodeList.length + && nodeList[0] == sublist[sublist.length - 1].nextSibling)) { + // "If node list's first member is a p or div, set the tag name + // of node list's first member to "li", and append the result + // to sublist. Remove the first member from node list." + if (isHtmlElement(nodeList[0], ["p", "div"])) { + sublist.push(setTagName(nodeList[0], "li")); + nodeList.shift(); + + // "Otherwise, if the first member of node list is an li or ol + // or ul, remove it from node list and append it to sublist." + } else if (isHtmlElement(nodeList[0], ["li", "ol", "ul"])) { + sublist.push(nodeList.shift()); + + // "Otherwise:" + } else { + // "Let nodes to wrap be a list of nodes, initially empty." + var nodesToWrap = []; + + // "While nodes to wrap is empty, or node list is not empty + // and its first member is the nextSibling of nodes to + // wrap's last member and the first member of node list is + // an inline node and the last member of nodes to wrap is + // an inline node other than a br, remove the first member + // from node list and append it to nodes to wrap." + while (!nodesToWrap.length + || (nodeList.length + && nodeList[0] == nodesToWrap[nodesToWrap.length - 1].nextSibling + && isInlineNode(nodeList[0]) + && isInlineNode(nodesToWrap[nodesToWrap.length - 1]) + && !isHtmlElement(nodesToWrap[nodesToWrap.length - 1], "br"))) { + nodesToWrap.push(nodeList.shift()); + } + + // "Wrap nodes to wrap, with new parent instructions + // returning the result of calling createElement("li") on + // the context object. Append the result to sublist." + sublist.push(wrap(nodesToWrap, + undefined, + function() { return document.createElement("li") })); + } + } + + // "If sublist's first member's parent is an HTML element with + // local name tag name, or if every member of sublist is an ol or + // ul, continue this loop from the beginning." + if (isHtmlElement(sublist[0].parentNode, tagName) + || sublist.every(function(node) { return isHtmlElement(node, ["ol", "ul"]) })) { + continue; + } + + // "If sublist's first member's parent is an HTML element with + // local name other tag name:" + if (isHtmlElement(sublist[0].parentNode, otherTagName)) { + // "Record the values of sublist, and let values be the + // result." + var values = recordValues(sublist); + + // "Split the parent of sublist." + splitParent(sublist); + + // "Wrap sublist, with sibling criteria returning true for an + // HTML element with local name tag name and false otherwise, + // and new parent instructions returning the result of calling + // createElement(tag name) on the context object." + wrap(sublist, + function(node) { return isHtmlElement(node, tagName) }, + function() { return document.createElement(tagName) }); + + // "Restore the values from values." + restoreValues(values); + + // "Continue this loop from the beginning." + continue; + } + + // "Wrap sublist, with sibling criteria returning true for an HTML + // element with local name tag name and false otherwise, and new + // parent instructions being the following:" + // . . . + // "Fix disallowed ancestors of the previous step's result." + fixDisallowedAncestors(wrap(sublist, + function(node) { return isHtmlElement(node, tagName) }, + function() { + // "If sublist's first member's parent is not an editable + // simple indentation element, or sublist's first member's + // parent's previousSibling is not an editable HTML element + // with local name tag name, call createElement(tag name) + // on the context object and return the result." + if (!isEditable(sublist[0].parentNode) + || !isSimpleIndentationElement(sublist[0].parentNode) + || !isEditable(sublist[0].parentNode.previousSibling) + || !isHtmlElement(sublist[0].parentNode.previousSibling, tagName)) { + return document.createElement(tagName); + } + + // "Let list be sublist's first member's parent's + // previousSibling." + var list = sublist[0].parentNode.previousSibling; + + // "Normalize sublists of list's lastChild." + normalizeSublists(list.lastChild); + + // "If list's lastChild is not an editable HTML element + // with local name tag name, call createElement(tag name) + // on the context object, and append the result as the last + // child of list." + if (!isEditable(list.lastChild) + || !isHtmlElement(list.lastChild, tagName)) { + list.appendChild(document.createElement(tagName)); + } + + // "Return the last child of list." + return list.lastChild; + } + )); + } + } +} + + +//@} +///// Justifying the selection ///// +//@{ + +function justifySelection(alignment) { + // "Block-extend the active range, and let new range be the result." + var newRange = blockExtend(globalRange); + + // "Let element list be a list of all editable Elements contained in new + // range that either has an attribute in the HTML namespace whose local + // name is "align", or has a style attribute that sets "text-align", or is + // a center." + var elementList = getAllContainedNodes(newRange, function(node) { + return node.nodeType == Node.ELEMENT_NODE + && isEditable(node) + // Ignoring namespaces here + && ( + node.hasAttribute("align") + || node.style.textAlign != "" + || isHtmlElement(node, "center") + ); + }); + + // "For each element in element list:" + for (var i = 0; i < elementList.length; i++) { + var element = elementList[i]; + + // "If element has an attribute in the HTML namespace whose local name + // is "align", remove that attribute." + element.removeAttribute("align"); + + // "Unset the CSS property "text-align" on element, if it's set by a + // style attribute." + element.style.textAlign = ""; + if (element.getAttribute("style") == "") { + element.removeAttribute("style"); + } + + // "If element is a div or span or center with no attributes, remove + // it, preserving its descendants." + if (isHtmlElement(element, ["div", "span", "center"]) + && !element.attributes.length) { + removePreservingDescendants(element); + } + + // "If element is a center with one or more attributes, set the tag + // name of element to "div"." + if (isHtmlElement(element, "center") + && element.attributes.length) { + setTagName(element, "div"); + } + } + + // "Block-extend the active range, and let new range be the result." + newRange = blockExtend(globalRange); + + // "Let node list be a list of nodes, initially empty." + var nodeList = []; + + // "For each node node contained in new range, append node to node list if + // the last member of node list (if any) is not an ancestor of node; node + // is editable; node is an allowed child of "div"; and node's alignment + // value is not alignment." + nodeList = getContainedNodes(newRange, function(node) { + return isEditable(node) + && isAllowedChild(node, "div") + && getAlignmentValue(node) != alignment; + }); + + // "While node list is not empty:" + while (nodeList.length) { + // "Let sublist be a list of nodes, initially empty." + var sublist = []; + + // "Remove the first member of node list and append it to sublist." + sublist.push(nodeList.shift()); + + // "While node list is not empty, and the first member of node list is + // the nextSibling of the last member of sublist, remove the first + // member of node list and append it to sublist." + while (nodeList.length + && nodeList[0] == sublist[sublist.length - 1].nextSibling) { + sublist.push(nodeList.shift()); + } + + // "Wrap sublist. Sibling criteria returns true for any div that has + // one or both of the following two attributes and no other attributes, + // and false otherwise:" + // + // * "An align attribute whose value is an ASCII case-insensitive + // match for alignment. + // * "A style attribute which sets exactly one CSS property + // (including unrecognized or invalid attributes), which is + // "text-align", which is set to alignment. + // + // "New parent instructions are to call createElement("div") on the + // context object, then set its CSS property "text-align" to alignment + // and return the result." + wrap(sublist, + function(node) { + return isHtmlElement(node, "div") + && [].every.call(node.attributes, function(attr) { + return (attr.name == "align" && attr.value.toLowerCase() == alignment) + || (attr.name == "style" && node.style.length == 1 && node.style.textAlign == alignment); + }); + }, + function() { + var newParent = document.createElement("div"); + newParent.setAttribute("style", "text-align: " + alignment); + return newParent; + } + ); + } +} + + +//@} +///// Automatic linking ///// +//@{ +// "An autolinkable URL is a string of the following form:" +var autolinkableUrlRegexp = + // "Either a string matching the scheme pattern from RFC 3986 section 3.1 + // followed by the literal string ://, or the literal string mailto:; + // followed by" + // + // From the RFC: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) + "([a-zA-Z][a-zA-Z0-9+.-]*://|mailto:)" + // "Zero or more characters other than space characters; followed by" + + "[^ \t\n\f\r]*" + // "A character that is not one of the ASCII characters !"'(),-.:;<>[]`{}." + + "[^!\"'(),\\-.:;<>[\\]`{}]"; + +// "A valid e-mail address is a string that matches the ABNF production 1*( +// atext / "." ) "@" ldh-str *( "." ldh-str ) where atext is defined in RFC +// 5322 section 3.2.3, and ldh-str is defined in RFC 1034 section 3.5." +// +// atext: ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / +// "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~" +// +//<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str> +//<let-dig-hyp> ::= <let-dig> | "-" +//<let-dig> ::= <letter> | <digit> +var validEmailRegexp = + "[a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~.]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*"; + +function autolink(node, endOffset) { + // "While (node, end offset)'s previous equivalent point is not null, set + // it to its previous equivalent point." + while (getPreviousEquivalentPoint(node, endOffset)) { + var prev = getPreviousEquivalentPoint(node, endOffset); + node = prev[0]; + endOffset = prev[1]; + } + + // "If node is not a Text node, or has an a ancestor, do nothing and abort + // these steps." + if (node.nodeType != Node.TEXT_NODE + || getAncestors(node).some(function(ancestor) { return isHtmlElement(ancestor, "a") })) { + return; + } + + // "Let search be the largest substring of node's data whose end is end + // offset and that contains no space characters." + var search = /[^ \t\n\f\r]*$/.exec(node.substringData(0, endOffset))[0]; + + // "If some substring of search is an autolinkable URL:" + if (new RegExp(autolinkableUrlRegexp).test(search)) { + // "While there is no substring of node's data ending at end offset + // that is an autolinkable URL, decrement end offset." + while (!(new RegExp(autolinkableUrlRegexp + "$").test(node.substringData(0, endOffset)))) { + endOffset--; + } + + // "Let start offset be the start index of the longest substring of + // node's data that is an autolinkable URL ending at end offset." + var startOffset = new RegExp(autolinkableUrlRegexp + "$").exec(node.substringData(0, endOffset)).index; + + // "Let href be the substring of node's data starting at start offset + // and ending at end offset." + var href = node.substringData(startOffset, endOffset - startOffset); + + // "Otherwise, if some substring of search is a valid e-mail address:" + } else if (new RegExp(validEmailRegexp).test(search)) { + // "While there is no substring of node's data ending at end offset + // that is a valid e-mail address, decrement end offset." + while (!(new RegExp(validEmailRegexp + "$").test(node.substringData(0, endOffset)))) { + endOffset--; + } + + // "Let start offset be the start index of the longest substring of + // node's data that is a valid e-mail address ending at end offset." + var startOffset = new RegExp(validEmailRegexp + "$").exec(node.substringData(0, endOffset)).index; + + // "Let href be "mailto:" concatenated with the substring of node's + // data starting at start offset and ending at end offset." + var href = "mailto:" + node.substringData(startOffset, endOffset - startOffset); + + // "Otherwise, do nothing and abort these steps." + } else { + return; + } + + // "Let original range be the active range." + var originalRange = getActiveRange(); + + // "Create a new range with start (node, start offset) and end (node, end + // offset), and set the context object's selection's range to it." + var newRange = document.createRange(); + newRange.setStart(node, startOffset); + newRange.setEnd(node, endOffset); + getSelection().removeAllRanges(); + getSelection().addRange(newRange); + globalRange = newRange; + + // "Take the action for "createLink", with value equal to href." + commands.createlink.action(href); + + // "Set the context object's selection's range to original range." + getSelection().removeAllRanges(); + getSelection().addRange(originalRange); + globalRange = originalRange; +} +//@} +///// The delete command ///// +//@{ +commands["delete"] = { + preservesOverrides: true, + action: function() { + // "If the active range is not collapsed, delete the selection and + // return true." + if (!getActiveRange().collapsed) { + deleteSelection(); + return true; + } + + // "Canonicalize whitespace at the active range's start." + canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset); + + // "Let node and offset be the active range's start node and offset." + var node = getActiveRange().startContainer; + var offset = getActiveRange().startOffset; + + // "Repeat the following steps:" + while (true) { + // "If offset is zero and node's previousSibling is an editable + // invisible node, remove node's previousSibling from its parent." + if (offset == 0 + && isEditable(node.previousSibling) + && isInvisible(node.previousSibling)) { + node.parentNode.removeChild(node.previousSibling); + + // "Otherwise, if node has a child with index offset − 1 and that + // child is an editable invisible node, remove that child from + // node, then subtract one from offset." + } else if (0 <= offset - 1 + && offset - 1 < node.childNodes.length + && isEditable(node.childNodes[offset - 1]) + && isInvisible(node.childNodes[offset - 1])) { + node.removeChild(node.childNodes[offset - 1]); + offset--; + + // "Otherwise, if offset is zero and node is an inline node, or if + // node is an invisible node, set offset to the index of node, then + // set node to its parent." + } else if ((offset == 0 + && isInlineNode(node)) + || isInvisible(node)) { + offset = getNodeIndex(node); + node = node.parentNode; + + // "Otherwise, if node has a child with index offset − 1 and that + // child is an editable a, remove that child from node, preserving + // its descendants. Then return true." + } else if (0 <= offset - 1 + && offset - 1 < node.childNodes.length + && isEditable(node.childNodes[offset - 1]) + && isHtmlElement(node.childNodes[offset - 1], "a")) { + removePreservingDescendants(node.childNodes[offset - 1]); + return true; + + // "Otherwise, if node has a child with index offset − 1 and that + // child is not a block node or a br or an img, set node to that + // child, then set offset to the length of node." + } else if (0 <= offset - 1 + && offset - 1 < node.childNodes.length + && !isBlockNode(node.childNodes[offset - 1]) + && !isHtmlElement(node.childNodes[offset - 1], ["br", "img"])) { + node = node.childNodes[offset - 1]; + offset = getNodeLength(node); + + // "Otherwise, break from this loop." + } else { + break; + } + } + + // "If node is a Text node and offset is not zero, or if node is a + // block node that has a child with index offset − 1 and that child is + // a br or hr or img:" + if ((node.nodeType == Node.TEXT_NODE + && offset != 0) + || (isBlockNode(node) + && 0 <= offset - 1 + && offset - 1 < node.childNodes.length + && isHtmlElement(node.childNodes[offset - 1], ["br", "hr", "img"]))) { + // "Call collapse(node, offset) on the context object's Selection." + getSelection().collapse(node, offset); + getActiveRange().setEnd(node, offset); + + // "Call extend(node, offset − 1) on the context object's + // Selection." + getSelection().extend(node, offset - 1); + getActiveRange().setStart(node, offset - 1); + + // "Delete the selection." + deleteSelection(); + + // "Return true." + return true; + } + + // "If node is an inline node, return true." + if (isInlineNode(node)) { + return true; + } + + // "If node is an li or dt or dd and is the first child of its parent, + // and offset is zero:" + if (isHtmlElement(node, ["li", "dt", "dd"]) + && node == node.parentNode.firstChild + && offset == 0) { + // "Let items be a list of all lis that are ancestors of node." + // + // Remember, must be in tree order. + var items = []; + for (var ancestor = node.parentNode; ancestor; ancestor = ancestor.parentNode) { + if (isHtmlElement(ancestor, "li")) { + items.unshift(ancestor); + } + } + + // "Normalize sublists of each item in items." + for (var i = 0; i < items.length; i++) { + normalizeSublists(items[i]); + } + + // "Record the values of the one-node list consisting of node, and + // let values be the result." + var values = recordValues([node]); + + // "Split the parent of the one-node list consisting of node." + splitParent([node]); + + // "Restore the values from values." + restoreValues(values); + + // "If node is a dd or dt, and it is not an allowed child of any of + // its ancestors in the same editing host, set the tag name of node + // to the default single-line container name and let node be the + // result." + if (isHtmlElement(node, ["dd", "dt"]) + && getAncestors(node).every(function(ancestor) { + return !inSameEditingHost(node, ancestor) + || !isAllowedChild(node, ancestor) + })) { + node = setTagName(node, defaultSingleLineContainerName); + } + + // "Fix disallowed ancestors of node." + fixDisallowedAncestors(node); + + // "Return true." + return true; + } + + // "Let start node equal node and let start offset equal offset." + var startNode = node; + var startOffset = offset; + + // "Repeat the following steps:" + while (true) { + // "If start offset is zero, set start offset to the index of start + // node and then set start node to its parent." + if (startOffset == 0) { + startOffset = getNodeIndex(startNode); + startNode = startNode.parentNode; + + // "Otherwise, if start node has an editable invisible child with + // index start offset minus one, remove it from start node and + // subtract one from start offset." + } else if (0 <= startOffset - 1 + && startOffset - 1 < startNode.childNodes.length + && isEditable(startNode.childNodes[startOffset - 1]) + && isInvisible(startNode.childNodes[startOffset - 1])) { + startNode.removeChild(startNode.childNodes[startOffset - 1]); + startOffset--; + + // "Otherwise, break from this loop." + } else { + break; + } + } + + // "If offset is zero, and node has an editable ancestor container in + // the same editing host that's an indentation element:" + if (offset == 0 + && getAncestors(node).concat(node).filter(function(ancestor) { + return isEditable(ancestor) + && inSameEditingHost(ancestor, node) + && isIndentationElement(ancestor); + }).length) { + // "Block-extend the range whose start and end are both (node, 0), + // and let new range be the result." + var newRange = document.createRange(); + newRange.setStart(node, 0); + newRange = blockExtend(newRange); + + // "Let node list be a list of nodes, initially empty." + // + // "For each node current node contained in new range, append + // current node to node list if the last member of node list (if + // any) is not an ancestor of current node, and current node is + // editable but has no editable descendants." + var nodeList = getContainedNodes(newRange, function(currentNode) { + return isEditable(currentNode) + && !hasEditableDescendants(currentNode); + }); + + // "Outdent each node in node list." + for (var i = 0; i < nodeList.length; i++) { + outdentNode(nodeList[i]); + } + + // "Return true." + return true; + } + + // "If the child of start node with index start offset is a table, + // return true." + if (isHtmlElement(startNode.childNodes[startOffset], "table")) { + return true; + } + + // "If start node has a child with index start offset − 1, and that + // child is a table:" + if (0 <= startOffset - 1 + && startOffset - 1 < startNode.childNodes.length + && isHtmlElement(startNode.childNodes[startOffset - 1], "table")) { + // "Call collapse(start node, start offset − 1) on the context + // object's Selection." + getSelection().collapse(startNode, startOffset - 1); + getActiveRange().setStart(startNode, startOffset - 1); + + // "Call extend(start node, start offset) on the context object's + // Selection." + getSelection().extend(startNode, startOffset); + getActiveRange().setEnd(startNode, startOffset); + + // "Return true." + return true; + } + + // "If offset is zero; and either the child of start node with index + // start offset minus one is an hr, or the child is a br whose + // previousSibling is either a br or not an inline node:" + if (offset == 0 + && (isHtmlElement(startNode.childNodes[startOffset - 1], "hr") + || ( + isHtmlElement(startNode.childNodes[startOffset - 1], "br") + && ( + isHtmlElement(startNode.childNodes[startOffset - 1].previousSibling, "br") + || !isInlineNode(startNode.childNodes[startOffset - 1].previousSibling) + ) + ) + )) { + // "Call collapse(start node, start offset − 1) on the context + // object's Selection." + getSelection().collapse(startNode, startOffset - 1); + getActiveRange().setStart(startNode, startOffset - 1); + + // "Call extend(start node, start offset) on the context object's + // Selection." + getSelection().extend(startNode, startOffset); + getActiveRange().setEnd(startNode, startOffset); + + // "Delete the selection." + deleteSelection(); + + // "Call collapse(node, offset) on the Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + getActiveRange().collapse(true); + + // "Return true." + return true; + } + + // "If the child of start node with index start offset is an li or dt + // or dd, and that child's firstChild is an inline node, and start + // offset is not zero:" + if (isHtmlElement(startNode.childNodes[startOffset], ["li", "dt", "dd"]) + && isInlineNode(startNode.childNodes[startOffset].firstChild) + && startOffset != 0) { + // "Let previous item be the child of start node with index start + // offset minus one." + var previousItem = startNode.childNodes[startOffset - 1]; + + // "If previous item's lastChild is an inline node other than a br, + // call createElement("br") on the context object and append the + // result as the last child of previous item." + if (isInlineNode(previousItem.lastChild) + && !isHtmlElement(previousItem.lastChild, "br")) { + previousItem.appendChild(document.createElement("br")); + } + + // "If previous item's lastChild is an inline node, call + // createElement("br") on the context object and append the result + // as the last child of previous item." + if (isInlineNode(previousItem.lastChild)) { + previousItem.appendChild(document.createElement("br")); + } + } + + // "If start node's child with index start offset is an li or dt or dd, + // and that child's previousSibling is also an li or dt or dd:" + if (isHtmlElement(startNode.childNodes[startOffset], ["li", "dt", "dd"]) + && isHtmlElement(startNode.childNodes[startOffset].previousSibling, ["li", "dt", "dd"])) { + // "Call cloneRange() on the active range, and let original range + // be the result." + // + // We need to add it to extraRanges so it will actually get updated + // when moving preserving ranges. + var originalRange = getActiveRange().cloneRange(); + extraRanges.push(originalRange); + + // "Set start node to its child with index start offset − 1." + startNode = startNode.childNodes[startOffset - 1]; + + // "Set start offset to start node's length." + startOffset = getNodeLength(startNode); + + // "Set node to start node's nextSibling." + node = startNode.nextSibling; + + // "Call collapse(start node, start offset) on the context object's + // Selection." + getSelection().collapse(startNode, startOffset); + getActiveRange().setStart(startNode, startOffset); + + // "Call extend(node, 0) on the context object's Selection." + getSelection().extend(node, 0); + getActiveRange().setEnd(node, 0); + + // "Delete the selection." + deleteSelection(); + + // "Call removeAllRanges() on the context object's Selection." + getSelection().removeAllRanges(); + + // "Call addRange(original range) on the context object's + // Selection." + getSelection().addRange(originalRange); + getActiveRange().setStart(originalRange.startContainer, originalRange.startOffset); + getActiveRange().setEnd(originalRange.endContainer, originalRange.endOffset); + + // "Return true." + extraRanges.pop(); + return true; + } + + // "While start node has a child with index start offset minus one:" + while (0 <= startOffset - 1 + && startOffset - 1 < startNode.childNodes.length) { + // "If start node's child with index start offset minus one is + // editable and invisible, remove it from start node, then subtract + // one from start offset." + if (isEditable(startNode.childNodes[startOffset - 1]) + && isInvisible(startNode.childNodes[startOffset - 1])) { + startNode.removeChild(startNode.childNodes[startOffset - 1]); + startOffset--; + + // "Otherwise, set start node to its child with index start offset + // minus one, then set start offset to the length of start node." + } else { + startNode = startNode.childNodes[startOffset - 1]; + startOffset = getNodeLength(startNode); + } + } + + // "Call collapse(start node, start offset) on the context object's + // Selection." + getSelection().collapse(startNode, startOffset); + getActiveRange().setStart(startNode, startOffset); + + // "Call extend(node, offset) on the context object's Selection." + getSelection().extend(node, offset); + getActiveRange().setEnd(node, offset); + + // "Delete the selection, with direction "backward"." + deleteSelection({direction: "backward"}); + + // "Return true." + return true; + } +}; + +//@} +///// The formatBlock command ///// +//@{ +// "A formattable block name is "address", "dd", "div", "dt", "h1", "h2", "h3", +// "h4", "h5", "h6", "p", or "pre"." +var formattableBlockNames = ["address", "dd", "div", "dt", "h1", "h2", "h3", + "h4", "h5", "h6", "p", "pre"]; + +commands.formatblock = { + preservesOverrides: true, + action: function(value) { + // "If value begins with a "<" character and ends with a ">" character, + // remove the first and last characters from it." + if (/^<.*>$/.test(value)) { + value = value.slice(1, -1); + } + + // "Let value be converted to ASCII lowercase." + value = value.toLowerCase(); + + // "If value is not a formattable block name, return false." + if (formattableBlockNames.indexOf(value) == -1) { + return false; + } + + // "Block-extend the active range, and let new range be the result." + var newRange = blockExtend(getActiveRange()); + + // "Let node list be an empty list of nodes." + // + // "For each node node contained in new range, append node to node list + // if it is editable, the last member of original node list (if any) is + // not an ancestor of node, node is either a non-list single-line + // container or an allowed child of "p" or a dd or dt, and node is not + // the ancestor of a prohibited paragraph child." + var nodeList = getContainedNodes(newRange, function(node) { + return isEditable(node) + && (isNonListSingleLineContainer(node) + || isAllowedChild(node, "p") + || isHtmlElement(node, ["dd", "dt"])) + && !getDescendants(node).some(isProhibitedParagraphChild); + }); + + // "Record the values of node list, and let values be the result." + var values = recordValues(nodeList); + + // "For each node in node list, while node is the descendant of an + // editable HTML element in the same editing host, whose local name is + // a formattable block name, and which is not the ancestor of a + // prohibited paragraph child, split the parent of the one-node list + // consisting of node." + for (var i = 0; i < nodeList.length; i++) { + var node = nodeList[i]; + while (getAncestors(node).some(function(ancestor) { + return isEditable(ancestor) + && inSameEditingHost(ancestor, node) + && isHtmlElement(ancestor, formattableBlockNames) + && !getDescendants(ancestor).some(isProhibitedParagraphChild); + })) { + splitParent([node]); + } + } + + // "Restore the values from values." + restoreValues(values); + + // "While node list is not empty:" + while (nodeList.length) { + var sublist; + + // "If the first member of node list is a single-line + // container:" + if (isSingleLineContainer(nodeList[0])) { + // "Let sublist be the children of the first member of node + // list." + sublist = [].slice.call(nodeList[0].childNodes); + + // "Record the values of sublist, and let values be the + // result." + var values = recordValues(sublist); + + // "Remove the first member of node list from its parent, + // preserving its descendants." + removePreservingDescendants(nodeList[0]); + + // "Restore the values from values." + restoreValues(values); + + // "Remove the first member from node list." + nodeList.shift(); + + // "Otherwise:" + } else { + // "Let sublist be an empty list of nodes." + sublist = []; + + // "Remove the first member of node list and append it to + // sublist." + sublist.push(nodeList.shift()); + + // "While node list is not empty, and the first member of + // node list is the nextSibling of the last member of + // sublist, and the first member of node list is not a + // single-line container, and the last member of sublist is + // not a br, remove the first member of node list and + // append it to sublist." + while (nodeList.length + && nodeList[0] == sublist[sublist.length - 1].nextSibling + && !isSingleLineContainer(nodeList[0]) + && !isHtmlElement(sublist[sublist.length - 1], "BR")) { + sublist.push(nodeList.shift()); + } + } + + // "Wrap sublist. If value is "div" or "p", sibling criteria + // returns false; otherwise it returns true for an HTML element + // with local name value and no attributes, and false otherwise. + // New parent instructions return the result of running + // createElement(value) on the context object. Then fix disallowed + // ancestors of the result." + fixDisallowedAncestors(wrap(sublist, + ["div", "p"].indexOf(value) == - 1 + ? function(node) { return isHtmlElement(node, value) && !node.attributes.length } + : function() { return false }, + function() { return document.createElement(value) })); + } + + // "Return true." + return true; + }, indeterm: function() { + // "If the active range is null, return false." + if (!getActiveRange()) { + return false; + } + + // "Block-extend the active range, and let new range be the result." + var newRange = blockExtend(getActiveRange()); + + // "Let node list be all visible editable nodes that are contained in + // new range and have no children." + var nodeList = getAllContainedNodes(newRange, function(node) { + return isVisible(node) + && isEditable(node) + && !node.hasChildNodes(); + }); + + // "If node list is empty, return false." + if (!nodeList.length) { + return false; + } + + // "Let type be null." + var type = null; + + // "For each node in node list:" + for (var i = 0; i < nodeList.length; i++) { + var node = nodeList[i]; + + // "While node's parent is editable and in the same editing host as + // node, and node is not an HTML element whose local name is a + // formattable block name, set node to its parent." + while (isEditable(node.parentNode) + && inSameEditingHost(node, node.parentNode) + && !isHtmlElement(node, formattableBlockNames)) { + node = node.parentNode; + } + + // "Let current type be the empty string." + var currentType = ""; + + // "If node is an editable HTML element whose local name is a + // formattable block name, and node is not the ancestor of a + // prohibited paragraph child, set current type to node's local + // name." + if (isEditable(node) + && isHtmlElement(node, formattableBlockNames) + && !getDescendants(node).some(isProhibitedParagraphChild)) { + currentType = node.tagName; + } + + // "If type is null, set type to current type." + if (type === null) { + type = currentType; + + // "Otherwise, if type does not equal current type, return true." + } else if (type != currentType) { + return true; + } + } + + // "Return false." + return false; + }, value: function() { + // "If the active range is null, return the empty string." + if (!getActiveRange()) { + return ""; + } + + // "Block-extend the active range, and let new range be the result." + var newRange = blockExtend(getActiveRange()); + + // "Let node be the first visible editable node that is contained in + // new range and has no children. If there is no such node, return the + // empty string." + var nodes = getAllContainedNodes(newRange, function(node) { + return isVisible(node) + && isEditable(node) + && !node.hasChildNodes(); + }); + if (!nodes.length) { + return ""; + } + var node = nodes[0]; + + // "While node's parent is editable and in the same editing host as + // node, and node is not an HTML element whose local name is a + // formattable block name, set node to its parent." + while (isEditable(node.parentNode) + && inSameEditingHost(node, node.parentNode) + && !isHtmlElement(node, formattableBlockNames)) { + node = node.parentNode; + } + + // "If node is an editable HTML element whose local name is a + // formattable block name, and node is not the ancestor of a prohibited + // paragraph child, return node's local name, converted to ASCII + // lowercase." + if (isEditable(node) + && isHtmlElement(node, formattableBlockNames) + && !getDescendants(node).some(isProhibitedParagraphChild)) { + return node.tagName.toLowerCase(); + } + + // "Return the empty string." + return ""; + } +}; + +//@} +///// The forwardDelete command ///// +//@{ +commands.forwarddelete = { + preservesOverrides: true, + action: function() { + // "If the active range is not collapsed, delete the selection and + // return true." + if (!getActiveRange().collapsed) { + deleteSelection(); + return true; + } + + // "Canonicalize whitespace at the active range's start." + canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset); + + // "Let node and offset be the active range's start node and offset." + var node = getActiveRange().startContainer; + var offset = getActiveRange().startOffset; + + // "Repeat the following steps:" + while (true) { + // "If offset is the length of node and node's nextSibling is an + // editable invisible node, remove node's nextSibling from its + // parent." + if (offset == getNodeLength(node) + && isEditable(node.nextSibling) + && isInvisible(node.nextSibling)) { + node.parentNode.removeChild(node.nextSibling); + + // "Otherwise, if node has a child with index offset and that child + // is an editable invisible node, remove that child from node." + } else if (offset < node.childNodes.length + && isEditable(node.childNodes[offset]) + && isInvisible(node.childNodes[offset])) { + node.removeChild(node.childNodes[offset]); + + // "Otherwise, if offset is the length of node and node is an + // inline node, or if node is invisible, set offset to one plus the + // index of node, then set node to its parent." + } else if ((offset == getNodeLength(node) + && isInlineNode(node)) + || isInvisible(node)) { + offset = 1 + getNodeIndex(node); + node = node.parentNode; + + // "Otherwise, if node has a child with index offset and that child + // is neither a block node nor a br nor an img nor a collapsed + // block prop, set node to that child, then set offset to zero." + } else if (offset < node.childNodes.length + && !isBlockNode(node.childNodes[offset]) + && !isHtmlElement(node.childNodes[offset], ["br", "img"]) + && !isCollapsedBlockProp(node.childNodes[offset])) { + node = node.childNodes[offset]; + offset = 0; + + // "Otherwise, break from this loop." + } else { + break; + } + } + + // "If node is a Text node and offset is not node's length:" + if (node.nodeType == Node.TEXT_NODE + && offset != getNodeLength(node)) { + // "Let end offset be offset plus one." + var endOffset = offset + 1; + + // "While end offset is not node's length and the end offsetth + // element of node's data has general category M when interpreted + // as a Unicode code point, add one to end offset." + // + // TODO: Not even going to try handling anything beyond the most + // basic combining marks, since I couldn't find a good list. I + // special-case a few Hebrew diacritics too to test basic coverage + // of non-Latin stuff. + while (endOffset != node.length + && /^[\u0300-\u036f\u0591-\u05bd\u05c1\u05c2]$/.test(node.data[endOffset])) { + endOffset++; + } + + // "Call collapse(node, offset) on the context object's Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + + // "Call extend(node, end offset) on the context object's + // Selection." + getSelection().extend(node, endOffset); + getActiveRange().setEnd(node, endOffset); + + // "Delete the selection." + deleteSelection(); + + // "Return true." + return true; + } + + // "If node is an inline node, return true." + if (isInlineNode(node)) { + return true; + } + + // "If node has a child with index offset and that child is a br or hr + // or img, but is not a collapsed block prop:" + if (offset < node.childNodes.length + && isHtmlElement(node.childNodes[offset], ["br", "hr", "img"]) + && !isCollapsedBlockProp(node.childNodes[offset])) { + // "Call collapse(node, offset) on the context object's Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + + // "Call extend(node, offset + 1) on the context object's + // Selection." + getSelection().extend(node, offset + 1); + getActiveRange().setEnd(node, offset + 1); + + // "Delete the selection." + deleteSelection(); + + // "Return true." + return true; + } + + // "Let end node equal node and let end offset equal offset." + var endNode = node; + var endOffset = offset; + + // "If end node has a child with index end offset, and that child is a + // collapsed block prop, add one to end offset." + if (endOffset < endNode.childNodes.length + && isCollapsedBlockProp(endNode.childNodes[endOffset])) { + endOffset++; + } + + // "Repeat the following steps:" + while (true) { + // "If end offset is the length of end node, set end offset to one + // plus the index of end node and then set end node to its parent." + if (endOffset == getNodeLength(endNode)) { + endOffset = 1 + getNodeIndex(endNode); + endNode = endNode.parentNode; + + // "Otherwise, if end node has a an editable invisible child with + // index end offset, remove it from end node." + } else if (endOffset < endNode.childNodes.length + && isEditable(endNode.childNodes[endOffset]) + && isInvisible(endNode.childNodes[endOffset])) { + endNode.removeChild(endNode.childNodes[endOffset]); + + // "Otherwise, break from this loop." + } else { + break; + } + } + + // "If the child of end node with index end offset minus one is a + // table, return true." + if (isHtmlElement(endNode.childNodes[endOffset - 1], "table")) { + return true; + } + + // "If the child of end node with index end offset is a table:" + if (isHtmlElement(endNode.childNodes[endOffset], "table")) { + // "Call collapse(end node, end offset) on the context object's + // Selection." + getSelection().collapse(endNode, endOffset); + getActiveRange().setStart(endNode, endOffset); + + // "Call extend(end node, end offset + 1) on the context object's + // Selection." + getSelection().extend(endNode, endOffset + 1); + getActiveRange().setEnd(endNode, endOffset + 1); + + // "Return true." + return true; + } + + // "If offset is the length of node, and the child of end node with + // index end offset is an hr or br:" + if (offset == getNodeLength(node) + && isHtmlElement(endNode.childNodes[endOffset], ["br", "hr"])) { + // "Call collapse(end node, end offset) on the context object's + // Selection." + getSelection().collapse(endNode, endOffset); + getActiveRange().setStart(endNode, endOffset); + + // "Call extend(end node, end offset + 1) on the context object's + // Selection." + getSelection().extend(endNode, endOffset + 1); + getActiveRange().setEnd(endNode, endOffset + 1); + + // "Delete the selection." + deleteSelection(); + + // "Call collapse(node, offset) on the Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + getActiveRange().collapse(true); + + // "Return true." + return true; + } + + // "While end node has a child with index end offset:" + while (endOffset < endNode.childNodes.length) { + // "If end node's child with index end offset is editable and + // invisible, remove it from end node." + if (isEditable(endNode.childNodes[endOffset]) + && isInvisible(endNode.childNodes[endOffset])) { + endNode.removeChild(endNode.childNodes[endOffset]); + + // "Otherwise, set end node to its child with index end offset and + // set end offset to zero." + } else { + endNode = endNode.childNodes[endOffset]; + endOffset = 0; + } + } + + // "Call collapse(node, offset) on the context object's Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + + // "Call extend(end node, end offset) on the context object's + // Selection." + getSelection().extend(endNode, endOffset); + getActiveRange().setEnd(endNode, endOffset); + + // "Delete the selection." + deleteSelection(); + + // "Return true." + return true; + } +}; + +//@} +///// The indent command ///// +//@{ +commands.indent = { + preservesOverrides: true, + action: function() { + // "Let items be a list of all lis that are ancestor containers of the + // active range's start and/or end node." + // + // Has to be in tree order, remember! + var items = []; + for (var node = getActiveRange().endContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) { + if (isHtmlElement(node, "LI")) { + items.unshift(node); + } + } + for (var node = getActiveRange().startContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) { + if (isHtmlElement(node, "LI")) { + items.unshift(node); + } + } + for (var node = getActiveRange().commonAncestorContainer; node; node = node.parentNode) { + if (isHtmlElement(node, "LI")) { + items.unshift(node); + } + } + + // "For each item in items, normalize sublists of item." + for (var i = 0; i < items.length; i++) { + normalizeSublists(items[i]); + } + + // "Block-extend the active range, and let new range be the result." + var newRange = blockExtend(getActiveRange()); + + // "Let node list be a list of nodes, initially empty." + var nodeList = []; + + // "For each node node contained in new range, if node is editable and + // is an allowed child of "div" or "ol" and if the last member of node + // list (if any) is not an ancestor of node, append node to node list." + nodeList = getContainedNodes(newRange, function(node) { + return isEditable(node) + && (isAllowedChild(node, "div") + || isAllowedChild(node, "ol")); + }); + + // "If the first visible member of node list is an li whose parent is + // an ol or ul:" + if (isHtmlElement(nodeList.filter(isVisible)[0], "li") + && isHtmlElement(nodeList.filter(isVisible)[0].parentNode, ["ol", "ul"])) { + // "Let sibling be node list's first visible member's + // previousSibling." + var sibling = nodeList.filter(isVisible)[0].previousSibling; + + // "While sibling is invisible, set sibling to its + // previousSibling." + while (isInvisible(sibling)) { + sibling = sibling.previousSibling; + } + + // "If sibling is an li, normalize sublists of sibling." + if (isHtmlElement(sibling, "li")) { + normalizeSublists(sibling); + } + } + + // "While node list is not empty:" + while (nodeList.length) { + // "Let sublist be a list of nodes, initially empty." + var sublist = []; + + // "Remove the first member of node list and append it to sublist." + sublist.push(nodeList.shift()); + + // "While the first member of node list is the nextSibling of the + // last member of sublist, remove the first member of node list and + // append it to sublist." + while (nodeList.length + && nodeList[0] == sublist[sublist.length - 1].nextSibling) { + sublist.push(nodeList.shift()); + } + + // "Indent sublist." + indentNodes(sublist); + } + + // "Return true." + return true; + } +}; + +//@} +///// The insertHorizontalRule command ///// +//@{ +commands.inserthorizontalrule = { + preservesOverrides: true, + action: function() { + // "Let start node, start offset, end node, and end offset be the + // active range's start and end nodes and offsets." + var startNode = getActiveRange().startContainer; + var startOffset = getActiveRange().startOffset; + var endNode = getActiveRange().endContainer; + var endOffset = getActiveRange().endOffset; + + // "While start offset is 0 and start node's parent is not null, set + // start offset to start node's index, then set start node to its + // parent." + while (startOffset == 0 + && startNode.parentNode) { + startOffset = getNodeIndex(startNode); + startNode = startNode.parentNode; + } + + // "While end offset is end node's length, and end node's parent is not + // null, set end offset to one plus end node's index, then set end node + // to its parent." + while (endOffset == getNodeLength(endNode) + && endNode.parentNode) { + endOffset = 1 + getNodeIndex(endNode); + endNode = endNode.parentNode; + } + + // "Call collapse(start node, start offset) on the context object's + // Selection." + getSelection().collapse(startNode, startOffset); + getActiveRange().setStart(startNode, startOffset); + + // "Call extend(end node, end offset) on the context object's + // Selection." + getSelection().extend(endNode, endOffset); + getActiveRange().setEnd(endNode, endOffset); + + // "Delete the selection, with block merging false." + deleteSelection({blockMerging: false}); + + // "If the active range's start node is neither editable nor an editing + // host, return true." + if (!isEditable(getActiveRange().startContainer) + && !isEditingHost(getActiveRange().startContainer)) { + return true; + } + + // "If the active range's start node is a Text node and its start + // offset is zero, call collapse() on the context object's Selection, + // with first argument the active range's start node's parent and + // second argument the active range's start node's index." + if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE + && getActiveRange().startOffset == 0) { + var newNode = getActiveRange().startContainer.parentNode; + var newOffset = getNodeIndex(getActiveRange().startContainer); + getSelection().collapse(newNode, newOffset); + getActiveRange().setStart(newNode, newOffset); + getActiveRange().collapse(true); + } + + // "If the active range's start node is a Text node and its start + // offset is the length of its start node, call collapse() on the + // context object's Selection, with first argument the active range's + // start node's parent, and the second argument one plus the active + // range's start node's index." + if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE + && getActiveRange().startOffset == getNodeLength(getActiveRange().startContainer)) { + var newNode = getActiveRange().startContainer.parentNode; + var newOffset = 1 + getNodeIndex(getActiveRange().startContainer); + getSelection().collapse(newNode, newOffset); + getActiveRange().setStart(newNode, newOffset); + getActiveRange().collapse(true); + } + + // "Let hr be the result of calling createElement("hr") on the + // context object." + var hr = document.createElement("hr"); + + // "Run insertNode(hr) on the active range." + getActiveRange().insertNode(hr); + + // "Fix disallowed ancestors of hr." + fixDisallowedAncestors(hr); + + // "Run collapse() on the context object's Selection, with first + // argument hr's parent and the second argument equal to one plus hr's + // index." + getSelection().collapse(hr.parentNode, 1 + getNodeIndex(hr)); + getActiveRange().setStart(hr.parentNode, 1 + getNodeIndex(hr)); + getActiveRange().collapse(true); + + // "Return true." + return true; + } +}; + +//@} +///// The insertHTML command ///// +//@{ +commands.inserthtml = { + preservesOverrides: true, + action: function(value) { + // "Delete the selection." + deleteSelection(); + + // "If the active range's start node is neither editable nor an editing + // host, return true." + if (!isEditable(getActiveRange().startContainer) + && !isEditingHost(getActiveRange().startContainer)) { + return true; + } + + // "Let frag be the result of calling createContextualFragment(value) + // on the active range." + var frag = getActiveRange().createContextualFragment(value); + + // "Let last child be the lastChild of frag." + var lastChild = frag.lastChild; + + // "If last child is null, return true." + if (!lastChild) { + return true; + } + + // "Let descendants be all descendants of frag." + var descendants = getDescendants(frag); + + // "If the active range's start node is a block node:" + if (isBlockNode(getActiveRange().startContainer)) { + // "Let collapsed block props be all editable collapsed block prop + // children of the active range's start node that have index + // greater than or equal to the active range's start offset." + // + // "For each node in collapsed block props, remove node from its + // parent." + [].filter.call(getActiveRange().startContainer.childNodes, function(node) { + return isEditable(node) + && isCollapsedBlockProp(node) + && getNodeIndex(node) >= getActiveRange().startOffset; + }).forEach(function(node) { + node.parentNode.removeChild(node); + }); + } + + // "Call insertNode(frag) on the active range." + getActiveRange().insertNode(frag); + + // "If the active range's start node is a block node with no visible + // children, call createElement("br") on the context object and append + // the result as the last child of the active range's start node." + if (isBlockNode(getActiveRange().startContainer) + && ![].some.call(getActiveRange().startContainer.childNodes, isVisible)) { + getActiveRange().startContainer.appendChild(document.createElement("br")); + } + + // "Call collapse() on the context object's Selection, with last + // child's parent as the first argument and one plus its index as the + // second." + getActiveRange().setStart(lastChild.parentNode, 1 + getNodeIndex(lastChild)); + getActiveRange().setEnd(lastChild.parentNode, 1 + getNodeIndex(lastChild)); + + // "Fix disallowed ancestors of each member of descendants." + for (var i = 0; i < descendants.length; i++) { + fixDisallowedAncestors(descendants[i]); + } + + // "Return true." + return true; + } +}; + +//@} +///// The insertImage command ///// +//@{ +commands.insertimage = { + preservesOverrides: true, + action: function(value) { + // "If value is the empty string, return false." + if (value === "") { + return false; + } + + // "Delete the selection, with strip wrappers false." + deleteSelection({stripWrappers: false}); + + // "Let range be the active range." + var range = getActiveRange(); + + // "If the active range's start node is neither editable nor an editing + // host, return true." + if (!isEditable(getActiveRange().startContainer) + && !isEditingHost(getActiveRange().startContainer)) { + return true; + } + + // "If range's start node is a block node whose sole child is a br, and + // its start offset is 0, remove its start node's child from it." + if (isBlockNode(range.startContainer) + && range.startContainer.childNodes.length == 1 + && isHtmlElement(range.startContainer.firstChild, "br") + && range.startOffset == 0) { + range.startContainer.removeChild(range.startContainer.firstChild); + } + + // "Let img be the result of calling createElement("img") on the + // context object." + var img = document.createElement("img"); + + // "Run setAttribute("src", value) on img." + img.setAttribute("src", value); + + // "Run insertNode(img) on the range." + range.insertNode(img); + + // "Run collapse() on the Selection, with first argument equal to the + // parent of img and the second argument equal to one plus the index of + // img." + // + // Not everyone actually supports collapse(), so we do it manually + // instead. Also, we need to modify the actual range we're given as + // well, for the sake of autoimplementation.html's range-filling-in. + range.setStart(img.parentNode, 1 + getNodeIndex(img)); + range.setEnd(img.parentNode, 1 + getNodeIndex(img)); + getSelection().removeAllRanges(); + getSelection().addRange(range); + + // IE adds width and height attributes for some reason, so remove those + // to actually do what the spec says. + img.removeAttribute("width"); + img.removeAttribute("height"); + + // "Return true." + return true; + } +}; + +//@} +///// The insertLineBreak command ///// +//@{ +commands.insertlinebreak = { + preservesOverrides: true, + action: function(value) { + // "Delete the selection, with strip wrappers false." + deleteSelection({stripWrappers: false}); + + // "If the active range's start node is neither editable nor an editing + // host, return true." + if (!isEditable(getActiveRange().startContainer) + && !isEditingHost(getActiveRange().startContainer)) { + return true; + } + + // "If the active range's start node is an Element, and "br" is not an + // allowed child of it, return true." + if (getActiveRange().startContainer.nodeType == Node.ELEMENT_NODE + && !isAllowedChild("br", getActiveRange().startContainer)) { + return true; + } + + // "If the active range's start node is not an Element, and "br" is not + // an allowed child of the active range's start node's parent, return + // true." + if (getActiveRange().startContainer.nodeType != Node.ELEMENT_NODE + && !isAllowedChild("br", getActiveRange().startContainer.parentNode)) { + return true; + } + + // "If the active range's start node is a Text node and its start + // offset is zero, call collapse() on the context object's Selection, + // with first argument equal to the active range's start node's parent + // and second argument equal to the active range's start node's index." + if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE + && getActiveRange().startOffset == 0) { + var newNode = getActiveRange().startContainer.parentNode; + var newOffset = getNodeIndex(getActiveRange().startContainer); + getSelection().collapse(newNode, newOffset); + getActiveRange().setStart(newNode, newOffset); + getActiveRange().setEnd(newNode, newOffset); + } + + // "If the active range's start node is a Text node and its start + // offset is the length of its start node, call collapse() on the + // context object's Selection, with first argument equal to the active + // range's start node's parent and second argument equal to one plus + // the active range's start node's index." + if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE + && getActiveRange().startOffset == getNodeLength(getActiveRange().startContainer)) { + var newNode = getActiveRange().startContainer.parentNode; + var newOffset = 1 + getNodeIndex(getActiveRange().startContainer); + getSelection().collapse(newNode, newOffset); + getActiveRange().setStart(newNode, newOffset); + getActiveRange().setEnd(newNode, newOffset); + } + + // "Let br be the result of calling createElement("br") on the context + // object." + var br = document.createElement("br"); + + // "Call insertNode(br) on the active range." + getActiveRange().insertNode(br); + + // "Call collapse() on the context object's Selection, with br's parent + // as the first argument and one plus br's index as the second + // argument." + getSelection().collapse(br.parentNode, 1 + getNodeIndex(br)); + getActiveRange().setStart(br.parentNode, 1 + getNodeIndex(br)); + getActiveRange().setEnd(br.parentNode, 1 + getNodeIndex(br)); + + // "If br is a collapsed line break, call createElement("br") on the + // context object and let extra br be the result, then call + // insertNode(extra br) on the active range." + if (isCollapsedLineBreak(br)) { + getActiveRange().insertNode(document.createElement("br")); + + // Compensate for nonstandard implementations of insertNode + getSelection().collapse(br.parentNode, 1 + getNodeIndex(br)); + getActiveRange().setStart(br.parentNode, 1 + getNodeIndex(br)); + getActiveRange().setEnd(br.parentNode, 1 + getNodeIndex(br)); + } + + // "Return true." + return true; + } +}; + +//@} +///// The insertOrderedList command ///// +//@{ +commands.insertorderedlist = { + preservesOverrides: true, + // "Toggle lists with tag name "ol", then return true." + action: function() { toggleLists("ol"); return true }, + // "True if the selection's list state is "mixed" or "mixed ol", false + // otherwise." + indeterm: function() { return /^mixed( ol)?$/.test(getSelectionListState()) }, + // "True if the selection's list state is "ol", false otherwise." + state: function() { return getSelectionListState() == "ol" }, +}; + +//@} +///// The insertParagraph command ///// +//@{ +commands.insertparagraph = { + preservesOverrides: true, + action: function() { + // "Delete the selection." + deleteSelection(); + + // "If the active range's start node is neither editable nor an editing + // host, return true." + if (!isEditable(getActiveRange().startContainer) + && !isEditingHost(getActiveRange().startContainer)) { + return true; + } + + // "Let node and offset be the active range's start node and offset." + var node = getActiveRange().startContainer; + var offset = getActiveRange().startOffset; + + // "If node is a Text node, and offset is neither 0 nor the length of + // node, call splitText(offset) on node." + if (node.nodeType == Node.TEXT_NODE + && offset != 0 + && offset != getNodeLength(node)) { + node.splitText(offset); + } + + // "If node is a Text node and offset is its length, set offset to one + // plus the index of node, then set node to its parent." + if (node.nodeType == Node.TEXT_NODE + && offset == getNodeLength(node)) { + offset = 1 + getNodeIndex(node); + node = node.parentNode; + } + + // "If node is a Text or Comment node, set offset to the index of node, + // then set node to its parent." + if (node.nodeType == Node.TEXT_NODE + || node.nodeType == Node.COMMENT_NODE) { + offset = getNodeIndex(node); + node = node.parentNode; + } + + // "Call collapse(node, offset) on the context object's Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + getActiveRange().setEnd(node, offset); + + // "Let container equal node." + var container = node; + + // "While container is not a single-line container, and container's + // parent is editable and in the same editing host as node, set + // container to its parent." + while (!isSingleLineContainer(container) + && isEditable(container.parentNode) + && inSameEditingHost(node, container.parentNode)) { + container = container.parentNode; + } + + // "If container is an editable single-line container in the same + // editing host as node, and its local name is "p" or "div":" + if (isEditable(container) + && isSingleLineContainer(container) + && inSameEditingHost(node, container.parentNode) + && (container.tagName == "P" || container.tagName == "DIV")) { + // "Let outer container equal container." + var outerContainer = container; + + // "While outer container is not a dd or dt or li, and outer + // container's parent is editable, set outer container to its + // parent." + while (!isHtmlElement(outerContainer, ["dd", "dt", "li"]) + && isEditable(outerContainer.parentNode)) { + outerContainer = outerContainer.parentNode; + } + + // "If outer container is a dd or dt or li, set container to outer + // container." + if (isHtmlElement(outerContainer, ["dd", "dt", "li"])) { + container = outerContainer; + } + } + + // "If container is not editable or not in the same editing host as + // node or is not a single-line container:" + if (!isEditable(container) + || !inSameEditingHost(container, node) + || !isSingleLineContainer(container)) { + // "Let tag be the default single-line container name." + var tag = defaultSingleLineContainerName; + + // "Block-extend the active range, and let new range be the + // result." + var newRange = blockExtend(getActiveRange()); + + // "Let node list be a list of nodes, initially empty." + // + // "Append to node list the first node in tree order that is + // contained in new range and is an allowed child of "p", if any." + var nodeList = getContainedNodes(newRange, function(node) { return isAllowedChild(node, "p") }) + .slice(0, 1); + + // "If node list is empty:" + if (!nodeList.length) { + // "If tag is not an allowed child of the active range's start + // node, return true." + if (!isAllowedChild(tag, getActiveRange().startContainer)) { + return true; + } + + // "Set container to the result of calling createElement(tag) + // on the context object." + container = document.createElement(tag); + + // "Call insertNode(container) on the active range." + getActiveRange().insertNode(container); + + // "Call createElement("br") on the context object, and append + // the result as the last child of container." + container.appendChild(document.createElement("br")); + + // "Call collapse(container, 0) on the context object's + // Selection." + getSelection().collapse(container, 0); + getActiveRange().setStart(container, 0); + getActiveRange().setEnd(container, 0); + + // "Return true." + return true; + } + + // "While the nextSibling of the last member of node list is not + // null and is an allowed child of "p", append it to node list." + while (nodeList[nodeList.length - 1].nextSibling + && isAllowedChild(nodeList[nodeList.length - 1].nextSibling, "p")) { + nodeList.push(nodeList[nodeList.length - 1].nextSibling); + } + + // "Wrap node list, with sibling criteria returning false and new + // parent instructions returning the result of calling + // createElement(tag) on the context object. Set container to the + // result." + container = wrap(nodeList, + function() { return false }, + function() { return document.createElement(tag) } + ); + } + + // "If container's local name is "address", "listing", or "pre":" + if (container.tagName == "ADDRESS" + || container.tagName == "LISTING" + || container.tagName == "PRE") { + // "Let br be the result of calling createElement("br") on the + // context object." + var br = document.createElement("br"); + + // "Call insertNode(br) on the active range." + getActiveRange().insertNode(br); + + // "Call collapse(node, offset + 1) on the context object's + // Selection." + getSelection().collapse(node, offset + 1); + getActiveRange().setStart(node, offset + 1); + getActiveRange().setEnd(node, offset + 1); + + // "If br is the last descendant of container, let br be the result + // of calling createElement("br") on the context object, then call + // insertNode(br) on the active range." + // + // Work around browser bugs: some browsers select the + // newly-inserted node, not per spec. + if (!isDescendant(nextNode(br), container)) { + getActiveRange().insertNode(document.createElement("br")); + getSelection().collapse(node, offset + 1); + getActiveRange().setEnd(node, offset + 1); + } + + // "Return true." + return true; + } + + // "If container's local name is "li", "dt", or "dd"; and either it has + // no children or it has a single child and that child is a br:" + if (["LI", "DT", "DD"].indexOf(container.tagName) != -1 + && (!container.hasChildNodes() + || (container.childNodes.length == 1 + && isHtmlElement(container.firstChild, "br")))) { + // "Split the parent of the one-node list consisting of container." + splitParent([container]); + + // "If container has no children, call createElement("br") on the + // context object and append the result as the last child of + // container." + if (!container.hasChildNodes()) { + container.appendChild(document.createElement("br")); + } + + // "If container is a dd or dt, and it is not an allowed child of + // any of its ancestors in the same editing host, set the tag name + // of container to the default single-line container name and let + // container be the result." + if (isHtmlElement(container, ["dd", "dt"]) + && getAncestors(container).every(function(ancestor) { + return !inSameEditingHost(container, ancestor) + || !isAllowedChild(container, ancestor) + })) { + container = setTagName(container, defaultSingleLineContainerName); + } + + // "Fix disallowed ancestors of container." + fixDisallowedAncestors(container); + + // "Return true." + return true; + } + + // "Let new line range be a new range whose start is the same as + // the active range's, and whose end is (container, length of + // container)." + var newLineRange = document.createRange(); + newLineRange.setStart(getActiveRange().startContainer, getActiveRange().startOffset); + newLineRange.setEnd(container, getNodeLength(container)); + + // "While new line range's start offset is zero and its start node is + // not a prohibited paragraph child, set its start to (parent of start + // node, index of start node)." + while (newLineRange.startOffset == 0 + && !isProhibitedParagraphChild(newLineRange.startContainer)) { + newLineRange.setStart(newLineRange.startContainer.parentNode, getNodeIndex(newLineRange.startContainer)); + } + + // "While new line range's start offset is the length of its start node + // and its start node is not a prohibited paragraph child, set its + // start to (parent of start node, 1 + index of start node)." + while (newLineRange.startOffset == getNodeLength(newLineRange.startContainer) + && !isProhibitedParagraphChild(newLineRange.startContainer)) { + newLineRange.setStart(newLineRange.startContainer.parentNode, 1 + getNodeIndex(newLineRange.startContainer)); + } + + // "Let end of line be true if new line range contains either nothing + // or a single br, and false otherwise." + var containedInNewLineRange = getContainedNodes(newLineRange); + var endOfLine = !containedInNewLineRange.length + || (containedInNewLineRange.length == 1 + && isHtmlElement(containedInNewLineRange[0], "br")); + + // "If the local name of container is "h1", "h2", "h3", "h4", "h5", or + // "h6", and end of line is true, let new container name be the default + // single-line container name." + var newContainerName; + if (/^H[1-6]$/.test(container.tagName) + && endOfLine) { + newContainerName = defaultSingleLineContainerName; + + // "Otherwise, if the local name of container is "dt" and end of line + // is true, let new container name be "dd"." + } else if (container.tagName == "DT" + && endOfLine) { + newContainerName = "dd"; + + // "Otherwise, if the local name of container is "dd" and end of line + // is true, let new container name be "dt"." + } else if (container.tagName == "DD" + && endOfLine) { + newContainerName = "dt"; + + // "Otherwise, let new container name be the local name of container." + } else { + newContainerName = container.tagName.toLowerCase(); + } + + // "Let new container be the result of calling createElement(new + // container name) on the context object." + var newContainer = document.createElement(newContainerName); + + // "Copy all attributes of container to new container." + for (var i = 0; i < container.attributes.length; i++) { + newContainer.setAttributeNS(container.attributes[i].namespaceURI, container.attributes[i].name, container.attributes[i].value); + } + + // "If new container has an id attribute, unset it." + newContainer.removeAttribute("id"); + + // "Insert new container into the parent of container immediately after + // container." + container.parentNode.insertBefore(newContainer, container.nextSibling); + + // "Let contained nodes be all nodes contained in new line range." + var containedNodes = getAllContainedNodes(newLineRange); + + // "Let frag be the result of calling extractContents() on new line + // range." + var frag = newLineRange.extractContents(); + + // "Unset the id attribute (if any) of each Element descendant of frag + // that is not in contained nodes." + var descendants = getDescendants(frag); + for (var i = 0; i < descendants.length; i++) { + if (descendants[i].nodeType == Node.ELEMENT_NODE + && containedNodes.indexOf(descendants[i]) == -1) { + descendants[i].removeAttribute("id"); + } + } + + // "Call appendChild(frag) on new container." + newContainer.appendChild(frag); + + // "While container's lastChild is a prohibited paragraph child, set + // container to its lastChild." + while (isProhibitedParagraphChild(container.lastChild)) { + container = container.lastChild; + } + + // "While new container's lastChild is a prohibited paragraph child, + // set new container to its lastChild." + while (isProhibitedParagraphChild(newContainer.lastChild)) { + newContainer = newContainer.lastChild; + } + + // "If container has no visible children, call createElement("br") on + // the context object, and append the result as the last child of + // container." + if (![].some.call(container.childNodes, isVisible)) { + container.appendChild(document.createElement("br")); + } + + // "If new container has no visible children, call createElement("br") + // on the context object, and append the result as the last child of + // new container." + if (![].some.call(newContainer.childNodes, isVisible)) { + newContainer.appendChild(document.createElement("br")); + } + + // "Call collapse(new container, 0) on the context object's Selection." + getSelection().collapse(newContainer, 0); + getActiveRange().setStart(newContainer, 0); + getActiveRange().setEnd(newContainer, 0); + + // "Return true." + return true; + } +}; + +//@} +///// The insertText command ///// +//@{ +commands.inserttext = { + action: function(value) { + // "Delete the selection, with strip wrappers false." + deleteSelection({stripWrappers: false}); + + // "If the active range's start node is neither editable nor an editing + // host, return true." + if (!isEditable(getActiveRange().startContainer) + && !isEditingHost(getActiveRange().startContainer)) { + return true; + } + + // "If value's length is greater than one:" + if (value.length > 1) { + // "For each element el in value, take the action for the + // insertText command, with value equal to el." + for (var i = 0; i < value.length; i++) { + commands.inserttext.action(value[i]); + } + + // "Return true." + return true; + } + + // "If value is the empty string, return true." + if (value == "") { + return true; + } + + // "If value is a newline (U+00A0), take the action for the + // insertParagraph command and return true." + if (value == "\n") { + commands.insertparagraph.action(); + return true; + } + + // "Let node and offset be the active range's start node and offset." + var node = getActiveRange().startContainer; + var offset = getActiveRange().startOffset; + + // "If node has a child whose index is offset − 1, and that child is a + // Text node, set node to that child, then set offset to node's + // length." + if (0 <= offset - 1 + && offset - 1 < node.childNodes.length + && node.childNodes[offset - 1].nodeType == Node.TEXT_NODE) { + node = node.childNodes[offset - 1]; + offset = getNodeLength(node); + } + + // "If node has a child whose index is offset, and that child is a Text + // node, set node to that child, then set offset to zero." + if (0 <= offset + && offset < node.childNodes.length + && node.childNodes[offset].nodeType == Node.TEXT_NODE) { + node = node.childNodes[offset]; + offset = 0; + } + + // "Record current overrides, and let overrides be the result." + var overrides = recordCurrentOverrides(); + + // "Call collapse(node, offset) on the context object's Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + getActiveRange().setEnd(node, offset); + + // "Canonicalize whitespace at (node, offset)." + canonicalizeWhitespace(node, offset); + + // "Let (node, offset) be the active range's start." + node = getActiveRange().startContainer; + offset = getActiveRange().startOffset; + + // "If node is a Text node:" + if (node.nodeType == Node.TEXT_NODE) { + // "Call insertData(offset, value) on node." + node.insertData(offset, value); + + // "Call collapse(node, offset) on the context object's Selection." + getSelection().collapse(node, offset); + getActiveRange().setStart(node, offset); + + // "Call extend(node, offset + 1) on the context object's + // Selection." + // + // Work around WebKit bug: the extend() can throw if the text we're + // adding is trailing whitespace. + try { getSelection().extend(node, offset + 1); } catch(e) {} + getActiveRange().setEnd(node, offset + 1); + + // "Otherwise:" + } else { + // "If node has only one child, which is a collapsed line break, + // remove its child from it." + // + // FIXME: IE incorrectly returns false here instead of true + // sometimes? + if (node.childNodes.length == 1 + && isCollapsedLineBreak(node.firstChild)) { + node.removeChild(node.firstChild); + } + + // "Let text be the result of calling createTextNode(value) on the + // context object." + var text = document.createTextNode(value); + + // "Call insertNode(text) on the active range." + getActiveRange().insertNode(text); + + // "Call collapse(text, 0) on the context object's Selection." + getSelection().collapse(text, 0); + getActiveRange().setStart(text, 0); + + // "Call extend(text, 1) on the context object's Selection." + getSelection().extend(text, 1); + getActiveRange().setEnd(text, 1); + } + + // "Restore states and values from overrides." + restoreStatesAndValues(overrides); + + // "Canonicalize whitespace at the active range's start, with fix + // collapsed space false." + canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset, false); + + // "Canonicalize whitespace at the active range's end, with fix + // collapsed space false." + canonicalizeWhitespace(getActiveRange().endContainer, getActiveRange().endOffset, false); + + // "If value is a space character, autolink the active range's start." + if (/^[ \t\n\f\r]$/.test(value)) { + autolink(getActiveRange().startContainer, getActiveRange().startOffset); + } + + // "Call collapseToEnd() on the context object's Selection." + // + // Work around WebKit bug: sometimes it blows up the selection and + // throws, which we don't want. + try { getSelection().collapseToEnd(); } catch(e) {} + getActiveRange().collapse(false); + + // "Return true." + return true; + } +}; + +//@} +///// The insertUnorderedList command ///// +//@{ +commands.insertunorderedlist = { + preservesOverrides: true, + // "Toggle lists with tag name "ul", then return true." + action: function() { toggleLists("ul"); return true }, + // "True if the selection's list state is "mixed" or "mixed ul", false + // otherwise." + indeterm: function() { return /^mixed( ul)?$/.test(getSelectionListState()) }, + // "True if the selection's list state is "ul", false otherwise." + state: function() { return getSelectionListState() == "ul" }, +}; + +//@} +///// The justifyCenter command ///// +//@{ +commands.justifycenter = { + preservesOverrides: true, + // "Justify the selection with alignment "center", then return true." + action: function() { justifySelection("center"); return true }, + indeterm: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if among visible editable nodes that + // are contained in the result and have no children, at least one has + // alignment value "center" and at least one does not. Otherwise return + // false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.some(function(node) { return getAlignmentValue(node) == "center" }) + && nodes.some(function(node) { return getAlignmentValue(node) != "center" }); + }, state: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if there is at least one visible + // editable node that is contained in the result and has no children, + // and all such nodes have alignment value "center". Otherwise return + // false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.length + && nodes.every(function(node) { return getAlignmentValue(node) == "center" }); + }, value: function() { + // "Return the empty string if the active range is null. Otherwise, + // block-extend the active range, and return the alignment value of the + // first visible editable node that is contained in the result and has + // no children. If there is no such node, return "left"." + if (!getActiveRange()) { + return ""; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + if (nodes.length) { + return getAlignmentValue(nodes[0]); + } else { + return "left"; + } + }, +}; + +//@} +///// The justifyFull command ///// +//@{ +commands.justifyfull = { + preservesOverrides: true, + // "Justify the selection with alignment "justify", then return true." + action: function() { justifySelection("justify"); return true }, + indeterm: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if among visible editable nodes that + // are contained in the result and have no children, at least one has + // alignment value "justify" and at least one does not. Otherwise + // return false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.some(function(node) { return getAlignmentValue(node) == "justify" }) + && nodes.some(function(node) { return getAlignmentValue(node) != "justify" }); + }, state: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if there is at least one visible + // editable node that is contained in the result and has no children, + // and all such nodes have alignment value "justify". Otherwise return + // false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.length + && nodes.every(function(node) { return getAlignmentValue(node) == "justify" }); + }, value: function() { + // "Return the empty string if the active range is null. Otherwise, + // block-extend the active range, and return the alignment value of the + // first visible editable node that is contained in the result and has + // no children. If there is no such node, return "left"." + if (!getActiveRange()) { + return ""; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + if (nodes.length) { + return getAlignmentValue(nodes[0]); + } else { + return "left"; + } + }, +}; + +//@} +///// The justifyLeft command ///// +//@{ +commands.justifyleft = { + preservesOverrides: true, + // "Justify the selection with alignment "left", then return true." + action: function() { justifySelection("left"); return true }, + indeterm: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if among visible editable nodes that + // are contained in the result and have no children, at least one has + // alignment value "left" and at least one does not. Otherwise return + // false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.some(function(node) { return getAlignmentValue(node) == "left" }) + && nodes.some(function(node) { return getAlignmentValue(node) != "left" }); + }, state: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if there is at least one visible + // editable node that is contained in the result and has no children, + // and all such nodes have alignment value "left". Otherwise return + // false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.length + && nodes.every(function(node) { return getAlignmentValue(node) == "left" }); + }, value: function() { + // "Return the empty string if the active range is null. Otherwise, + // block-extend the active range, and return the alignment value of the + // first visible editable node that is contained in the result and has + // no children. If there is no such node, return "left"." + if (!getActiveRange()) { + return ""; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + if (nodes.length) { + return getAlignmentValue(nodes[0]); + } else { + return "left"; + } + }, +}; + +//@} +///// The justifyRight command ///// +//@{ +commands.justifyright = { + preservesOverrides: true, + // "Justify the selection with alignment "right", then return true." + action: function() { justifySelection("right"); return true }, + indeterm: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if among visible editable nodes that + // are contained in the result and have no children, at least one has + // alignment value "right" and at least one does not. Otherwise return + // false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.some(function(node) { return getAlignmentValue(node) == "right" }) + && nodes.some(function(node) { return getAlignmentValue(node) != "right" }); + }, state: function() { + // "Return false if the active range is null. Otherwise, block-extend + // the active range. Return true if there is at least one visible + // editable node that is contained in the result and has no children, + // and all such nodes have alignment value "right". Otherwise return + // false." + if (!getActiveRange()) { + return false; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + return nodes.length + && nodes.every(function(node) { return getAlignmentValue(node) == "right" }); + }, value: function() { + // "Return the empty string if the active range is null. Otherwise, + // block-extend the active range, and return the alignment value of the + // first visible editable node that is contained in the result and has + // no children. If there is no such node, return "left"." + if (!getActiveRange()) { + return ""; + } + var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) { + return isEditable(node) && isVisible(node) && !node.hasChildNodes(); + }); + if (nodes.length) { + return getAlignmentValue(nodes[0]); + } else { + return "left"; + } + }, +}; + +//@} +///// The outdent command ///// +//@{ +commands.outdent = { + preservesOverrides: true, + action: function() { + // "Let items be a list of all lis that are ancestor containers of the + // range's start and/or end node." + // + // It's annoying to get this in tree order using functional stuff + // without doing getDescendants(document), which is slow, so I do it + // imperatively. + var items = []; + (function(){ + for ( + var ancestorContainer = getActiveRange().endContainer; + ancestorContainer != getActiveRange().commonAncestorContainer; + ancestorContainer = ancestorContainer.parentNode + ) { + if (isHtmlElement(ancestorContainer, "li")) { + items.unshift(ancestorContainer); + } + } + for ( + var ancestorContainer = getActiveRange().startContainer; + ancestorContainer; + ancestorContainer = ancestorContainer.parentNode + ) { + if (isHtmlElement(ancestorContainer, "li")) { + items.unshift(ancestorContainer); + } + } + })(); + + // "For each item in items, normalize sublists of item." + items.forEach(normalizeSublists); + + // "Block-extend the active range, and let new range be the result." + var newRange = blockExtend(getActiveRange()); + + // "Let node list be a list of nodes, initially empty." + // + // "For each node node contained in new range, append node to node list + // if the last member of node list (if any) is not an ancestor of node; + // node is editable; and either node has no editable descendants, or is + // an ol or ul, or is an li whose parent is an ol or ul." + var nodeList = getContainedNodes(newRange, function(node) { + return isEditable(node) + && (!getDescendants(node).some(isEditable) + || isHtmlElement(node, ["ol", "ul"]) + || (isHtmlElement(node, "li") && isHtmlElement(node.parentNode, ["ol", "ul"]))); + }); + + // "While node list is not empty:" + while (nodeList.length) { + // "While the first member of node list is an ol or ul or is not + // the child of an ol or ul, outdent it and remove it from node + // list." + while (nodeList.length + && (isHtmlElement(nodeList[0], ["OL", "UL"]) + || !isHtmlElement(nodeList[0].parentNode, ["OL", "UL"]))) { + outdentNode(nodeList.shift()); + } + + // "If node list is empty, break from these substeps." + if (!nodeList.length) { + break; + } + + // "Let sublist be a list of nodes, initially empty." + var sublist = []; + + // "Remove the first member of node list and append it to sublist." + sublist.push(nodeList.shift()); + + // "While the first member of node list is the nextSibling of the + // last member of sublist, and the first member of node list is not + // an ol or ul, remove the first member of node list and append it + // to sublist." + while (nodeList.length + && nodeList[0] == sublist[sublist.length - 1].nextSibling + && !isHtmlElement(nodeList[0], ["OL", "UL"])) { + sublist.push(nodeList.shift()); + } + + // "Record the values of sublist, and let values be the result." + var values = recordValues(sublist); + + // "Split the parent of sublist, with new parent null." + splitParent(sublist); + + // "Fix disallowed ancestors of each member of sublist." + sublist.forEach(fixDisallowedAncestors); + + // "Restore the values from values." + restoreValues(values); + } + + // "Return true." + return true; + } +}; + +//@} + +////////////////////////////////// +///// Miscellaneous commands ///// +////////////////////////////////// + +///// The defaultParagraphSeparator command ///// +//@{ +commands.defaultparagraphseparator = { + action: function(value) { + // "Let value be converted to ASCII lowercase. If value is then equal + // to "p" or "div", set the context object's default single-line + // container name to value and return true. Otherwise, return false." + value = value.toLowerCase(); + if (value == "p" || value == "div") { + defaultSingleLineContainerName = value; + return true; + } + return false; + }, value: function() { + // "Return the context object's default single-line container name." + return defaultSingleLineContainerName; + }, +}; + +//@} +///// The selectAll command ///// +//@{ +commands.selectall = { + // Note, this ignores the whole globalRange/getActiveRange() thing and + // works with actual selections. Not suitable for autoimplementation.html. + action: function() { + // "Let target be the body element of the context object." + var target = document.body; + + // "If target is null, let target be the context object's + // documentElement." + if (!target) { + target = document.documentElement; + } + + // "If target is null, call getSelection() on the context object, and + // call removeAllRanges() on the result." + if (!target) { + getSelection().removeAllRanges(); + + // "Otherwise, call getSelection() on the context object, and call + // selectAllChildren(target) on the result." + } else { + getSelection().selectAllChildren(target); + } + + // "Return true." + return true; + } +}; + +//@} +///// The styleWithCSS command ///// +//@{ +commands.stylewithcss = { + action: function(value) { + // "If value is an ASCII case-insensitive match for the string + // "false", set the CSS styling flag to false. Otherwise, set the + // CSS styling flag to true. Either way, return true." + cssStylingFlag = String(value).toLowerCase() != "false"; + return true; + }, state: function() { return cssStylingFlag } +}; + +//@} +///// The useCSS command ///// +//@{ +commands.usecss = { + action: function(value) { + // "If value is an ASCII case-insensitive match for the string "false", + // set the CSS styling flag to true. Otherwise, set the CSS styling + // flag to false. Either way, return true." + cssStylingFlag = String(value).toLowerCase() == "false"; + return true; + } +}; +//@} + +// Some final setup +//@{ +(function() { +// Opera 11.50 doesn't implement Object.keys, so I have to make an explicit +// temporary, which means I need an extra closure to not leak the temporaries +// into the global namespace. >:( +var commandNames = []; +for (var command in commands) { + commandNames.push(command); +} +commandNames.forEach(function(command) { + // "If a command does not have a relevant CSS property specified, it + // defaults to null." + if (!("relevantCssProperty" in commands[command])) { + commands[command].relevantCssProperty = null; + } + + // "If a command has inline command activated values defined but nothing + // else defines when it is indeterminate, it is indeterminate if among + // formattable nodes effectively contained in the active range, there is at + // least one whose effective command value is one of the given values and + // at least one whose effective command value is not one of the given + // values." + if ("inlineCommandActivatedValues" in commands[command] + && !("indeterm" in commands[command])) { + commands[command].indeterm = function() { + if (!getActiveRange()) { + return false; + } + + var values = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode) + .map(function(node) { return getEffectiveCommandValue(node, command) }); + + var matchingValues = values.filter(function(value) { + return commands[command].inlineCommandActivatedValues.indexOf(value) != -1; + }); + + return matchingValues.length >= 1 + && values.length - matchingValues.length >= 1; + }; + } + + // "If a command has inline command activated values defined, its state is + // true if either no formattable node is effectively contained in the + // active range, and the active range's start node's effective command + // value is one of the given values; or if there is at least one + // formattable node effectively contained in the active range, and all of + // them have an effective command value equal to one of the given values." + if ("inlineCommandActivatedValues" in commands[command]) { + commands[command].state = function() { + if (!getActiveRange()) { + return false; + } + + var nodes = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode); + + if (nodes.length == 0) { + return commands[command].inlineCommandActivatedValues + .indexOf(getEffectiveCommandValue(getActiveRange().startContainer, command)) != -1; + } else { + return nodes.every(function(node) { + return commands[command].inlineCommandActivatedValues + .indexOf(getEffectiveCommandValue(node, command)) != -1; + }); + } + }; + } + + // "If a command is a standard inline value command, it is indeterminate if + // among formattable nodes that are effectively contained in the active + // range, there are two that have distinct effective command values. Its + // value is the effective command value of the first formattable node that + // is effectively contained in the active range; or if there is no such + // node, the effective command value of the active range's start node; or + // if that is null, the empty string." + if ("standardInlineValueCommand" in commands[command]) { + commands[command].indeterm = function() { + if (!getActiveRange()) { + return false; + } + + var values = getAllEffectivelyContainedNodes(getActiveRange()) + .filter(isFormattableNode) + .map(function(node) { return getEffectiveCommandValue(node, command) }); + for (var i = 1; i < values.length; i++) { + if (values[i] != values[i - 1]) { + return true; + } + } + return false; + }; + + commands[command].value = function() { + if (!getActiveRange()) { + return ""; + } + + var refNode = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode)[0]; + + if (typeof refNode == "undefined") { + refNode = getActiveRange().startContainer; + } + + var ret = getEffectiveCommandValue(refNode, command); + if (ret === null) { + return ""; + } + return ret; + }; + } + + // "If a command preserves overrides, then before taking its action, the + // user agent must record current overrides. After taking the action, if + // the active range is collapsed, it must restore states and values from + // the recorded list." + if ("preservesOverrides" in commands[command]) { + var oldAction = commands[command].action; + + commands[command].action = function(value) { + var overrides = recordCurrentOverrides(); + var ret = oldAction(value); + if (getActiveRange().collapsed) { + restoreStatesAndValues(overrides); + } + return ret; + }; + } +}); +})(); +//@} + +// vim: foldmarker=@{,@} foldmethod=marker diff --git a/testing/web-platform/tests/editing/include/manualtest.js b/testing/web-platform/tests/editing/include/manualtest.js new file mode 100644 index 0000000000..504fdae4cc --- /dev/null +++ b/testing/web-platform/tests/editing/include/manualtest.js @@ -0,0 +1,225 @@ +// Initial setup +//@{ +var globalValue; +if (globalValue === undefined) { + globalValue = command in defaultValues ? defaultValues[command] : ""; +} +var keyPrefix = globalValue == "" + ? "manualtest-" + command + "-" + : "manualtest-" + command + "-" + globalValue + "-"; +(function(){ + var manualTests = tests[command] + .map(function(test) { return normalizeTest(command, test) }) + .filter(function(test) { return test[1][1] == globalValue }); + var relevantMultiTests = tests.multitest + .map(function(test) { return normalizeTest("multitest", test) }) + .filter(function(test) { + // We only want multitests if there's exactly one occurrence of the + // command we're testing for, and the value is correct, and that's + // the last command we're testing. Some of these limitations could + // be removed in the future. + return test[test.length - 1][0] === command + && test[test.length - 1][1] === globalValue; + }); + + tests = manualTests.concat(relevantMultiTests); +})(); +//@} + +function clearCachedResults() { +//@{ + for (var key in localStorage) { + if (key.indexOf(keyPrefix) === 0) { + localStorage.removeItem(key); + } + } +} +//@} + +var numManualTests = 0; +var currentTestIdx = null; + +// Make sure styleWithCss is always reset to false at the start of a test run +// (I'm looking at you, Firefox) +try { document.execCommand("stylewithcss", false, "false") } catch(e) {} + +function runTests() { +//@{ + // We don't ask the user to hit a key on all tests, so make sure not to + // claim more tests are going to be run than actually are. + for (var i = 0; i < tests.length; i++) { + if (localStorage.getItem(keyPrefix + JSON.stringify(tests[i])) === null) { + numManualTests++; + } + } + + currentTestIdx = 0; + + var runTestsButton = document.querySelector("#tests input[type=button]"); + runTestsButton.parentNode.removeChild(runTestsButton); + + var addTestButton = document.querySelector("#tests input[type=button]"); + var input = document.querySelector("#tests label input"); + // This code actually focuses and clicks everything because for some + // reason, anything else doesn't work in IE9 . . . + input.value = JSON.stringify(tests[0]); + input.focus(); + addTestButton.click(); +} +//@} + +function addTest() { +//@{ + var tr = doSetup("#tests table", 0); + var input = document.querySelector("#tests label input"); + var test = JSON.parse(input.value); + doInputCell(tr, test, test.length == 2 ? command : "multitest"); + doSpecCell(tr, test, test.length == 2 ? command : "multitest"); + if (localStorage.getItem(keyPrefix + JSON.stringify(test)) !== null) { + // Yay, I get to cheat. Remove the overlay div so the user doesn't + // keep hitting the key, in case it takes a while. + var browserCell = document.createElement("td"); + tr.appendChild(browserCell); + browserCell.innerHTML = localStorage[keyPrefix + JSON.stringify(test)]; + doBrowserCellButton(browserCell, test); + document.getElementById("overlay").style.display = ""; + doSameCell(tr); + runNextTest(test); + } else { + doBrowserCell(tr, test, function() { + doSameCell(tr); + runNextTest(); + }); + } +} +//@} + +function runNextTest() { +//@{ + doTearDown(); + var input = document.querySelector("#tests label input"); + if (currentTestIdx === null + || currentTestIdx + 1 >= tests.length) { + currentTestIdx = null; + document.getElementById("overlay").style.display = ""; + input.value = ""; + return; + } + currentTestIdx++; + input.value = JSON.stringify(tests[currentTestIdx]); + input.focus(); + addTest(); +} +//@} + +function doBrowserCell(tr, test, callback) { +//@{ + var browserCell = document.createElement("td"); + tr.appendChild(browserCell); + + try { + var points = setupCell(browserCell, test[0]); + + var testDiv = browserCell.firstChild; + // Work around weird Firefox bug: + // https://bugzilla.mozilla.org/show_bug.cgi?id=649138 + document.body.appendChild(testDiv); + testDiv.onkeyup = function() { + continueBrowserCell(test, testDiv, browserCell); + callback(); + }; + testDiv.contentEditable = "true"; + testDiv.spellcheck = false; + if (currentTestIdx === null) { + document.getElementById("testcount").style.display = "none"; + } else { + document.getElementById("testcount").style.display = ""; + document.querySelector("#testcount > span").textContent = numManualTests; + numManualTests--; + } + document.getElementById("overlay").style.display = "block"; + testDiv.focus(); + setSelection(points[0], points[1], points[2], points[3]); + // Execute any extra commands beforehand, for multitests + for (var i = 1; i < test.length - 1; i++) { + document.execCommand(test[i][0], false, test[i][1]); + } + } catch (e) { + browserCellException(e, testDiv, browserCell); + callback(); + } +} +//@} + +function continueBrowserCell(test, testDiv, browserCell) { +//@{ + try { + testDiv.contentEditable = "inherit"; + testDiv.removeAttribute("spellcheck"); + var compareDiv1 = testDiv.cloneNode(true); + + if (getSelection().rangeCount) { + addBrackets(getSelection().getRangeAt(0)); + } + browserCell.insertBefore(testDiv, browserCell.firstChild); + + if (!browserCell.childNodes.length == 2) { + throw "The cell didn't have two children. Did something spill outside the test div?"; + } + + compareDiv1.normalize(); + // Sigh, Gecko is crazy + var treeWalker = document.createTreeWalker(compareDiv1, NodeFilter.SHOW_ELEMENT, null, null); + while (treeWalker.nextNode()) { + var remove = [].filter.call(treeWalker.currentNode.attributes, function(attrib) { + return /^_moz_/.test(attrib.name) || attrib.value == "_moz"; + }); + for (var i = 0; i < remove.length; i++) { + treeWalker.currentNode.removeAttribute(remove[i].name); + } + } + var compareDiv2 = compareDiv1.cloneNode(false); + compareDiv2.innerHTML = compareDiv1.innerHTML; + if (!compareDiv1.isEqualNode(compareDiv2) + && compareDiv1.innerHTML != compareDiv2.innerHTML) { + throw "DOM does not round-trip through serialization! " + + compareDiv1.innerHTML + " vs. " + compareDiv2.innerHTML; + } + if (!compareDiv1.isEqualNode(compareDiv2)) { + throw "DOM does not round-trip through serialization (although innerHTML is the same)! " + + compareDiv1.innerHTML; + } + + browserCell.lastChild.textContent = browserCell.firstChild.innerHTML; + } catch (e) { + browserCellException(e, testDiv, browserCell); + } + + localStorage[keyPrefix + JSON.stringify(test)] = browserCell.innerHTML; + + doBrowserCellButton(browserCell, test); +} +//@} + +function doBrowserCellButton(browserCell, test) { +//@{ + var button = document.createElement("button"); + browserCell.lastChild.appendChild(button); + button.textContent = "Redo browser output"; + button.onclick = function() { + localStorage.removeItem(keyPrefix + JSON.stringify(test)); + var tr = browserCell.parentNode; + while (browserCell.nextSibling) { + tr.removeChild(browserCell.nextSibling); + } + tr.removeChild(browserCell); + doBrowserCell(tr, test, function() { + doSameCell(tr); + doTearDown(); + document.getElementById("overlay").style.display = ""; + tr.scrollIntoView(); + }); + }; +} +//@} +// vim: foldmarker=@{,@} foldmethod=marker diff --git a/testing/web-platform/tests/editing/include/reset.css b/testing/web-platform/tests/editing/include/reset.css new file mode 100644 index 0000000000..b711d724c3 --- /dev/null +++ b/testing/web-platform/tests/editing/include/reset.css @@ -0,0 +1,27 @@ +/* Make sure various CSS values are what are expected, so that tests work + * right. */ +body { font-family: serif } +/* http://www.w3.org/Bugs/Public/show_bug.cgi?id=12154 + * https://bugzilla.mozilla.org/show_bug.cgi?id=589124 + * https://bugs.webkit.org/show_bug.cgi?id=56400 */ +b, strong { font-weight: bold } +.bold { font-weight: bold } +.notbold { font-weight: normal } +.underline { text-decoration: underline } +.line-through { text-decoration: line-through } +.underline-and-line-through { text-decoration: underline line-through } +#purple { color: purple } +/* https://bugs.webkit.org/show_bug.cgi?id=56670 */ +dfn { font-style: italic } +/* Opera has weird default blockquote style */ +blockquote { margin: 1em 40px } +/* Some tests assume links are blue, for the sake of argument, but they aren't + * blue in any browser. And :visited definitely isn't blue, except in engines + * like Gecko that lie. + * + * This should really be #00e, probably. See: + * http://www.w3.org/Bugs/Public/show_bug.cgi?id=13330 */ +:link, :visited { color: blue } +/* http://www.w3.org/Bugs/Public/show_bug.cgi?id=14066 + * https://bugs.webkit.org/show_bug.cgi?id=68392 */ +quasit { text-align: inherit } diff --git a/testing/web-platform/tests/editing/include/tests.css b/testing/web-platform/tests/editing/include/tests.css new file mode 100644 index 0000000000..e72f338085 --- /dev/null +++ b/testing/web-platform/tests/editing/include/tests.css @@ -0,0 +1,84 @@ +@import "reset.css"; +.yes { color: green } +.no { color: red } +.maybe { color: orange } +.yes, .no, .maybe { + text-align: center; + vertical-align: middle; + font-size: 3em; + /* Somehow Opera doesn't render the X's if the font is serif, on my + * machine. */ + font-family: sans-serif; + border-color: black; +} +div.alert { + color: red; + font-weight: bold; +} +.extra-results { font-size: small } +.good-result { color: green } +.bad-result { color: red } +body > div > table > tbody > tr > td > div:first-child { + padding-bottom: 0.2em; +} +body > div > table > tbody > tr > td > div:last-child { + padding-top: 0.2em; + border-top: 1px solid black; +} +/* Workaround for browsers that don't treat <wbr> as a line-break opportunity + * (activated via JS feature-detection) */ +body.wbr-workaround > div > table > tbody > tr > td > div:last-child { + word-wrap: break-word; +} +body > div > table > tbody > tr > td > div:last-child { + white-space: pre-wrap; +} +/* Let the rendered HTML line up so it's easier to compare whitespace */ +body > div > table > tbody > tr > td { vertical-align: top } +/* We don't want test cells to not wrap */ +listing, plaintext, pre, xmp { white-space: pre-wrap } +img, video { width: 50px } +body > div > table { + width: 100%; + table-layout: fixed; +} +body > div > table > tbody > tr > td, +body > div > table > tbody > tr > th { + width: 30%; +} +body > div > table > tbody > tr > td:last-child, +body > div > table > tbody > tr > th:last-child { + width: 10%; +} +body > div > p > label > input { width: 30% } +#toolbar { + position: fixed; + top: 0; + left: 0; + right: 0; + height: 1.5em; + background: white; + border-bottom: 2px solid gray; +} +body { + /* So the toolbar doesn't block it */ + margin-top: 2em; +} +/* For easy visibility of nesting */ +ol ol { list-style-type: lower-alpha } +ol ol ol { list-style-type: lower-roman } +/* For manual tests */ +#overlay { + display: none; + position: fixed; + top: 0; + bottom: 0; + left: 0; + right: 0; + color: red; + background: yellow; + font-size: 4em; + font-weight: bold; + text-align: center; + padding: 2em; +} diff --git a/testing/web-platform/tests/editing/include/tests.js b/testing/web-platform/tests/editing/include/tests.js new file mode 100644 index 0000000000..c18aef136b --- /dev/null +++ b/testing/web-platform/tests/editing/include/tests.js @@ -0,0 +1,5756 @@ +// For the original (development) tests, we want to make a bunch of changes to +// the page as it loads. We don't want this for the conformance tests, so let +// them opt out. +if (typeof testsJsLibraryOnly == "undefined" || !testsJsLibraryOnly) { + // Alert the reader of egregious Opera bug that will make the specced + // implementation horribly buggy + //@{ + (function() { + var div = document.createElement("div"); + div.appendChild(document.createElement("br")); + document.body.insertBefore(div, document.body.firstChild); + var range = document.createRange(); + range.setStart(div, 1); + div.insertBefore(document.createElement("p"), div.firstChild); + if (range.startOffset > range.startContainer.childNodes.length) { + var warningDiv = document.createElement("p"); + document.body.insertBefore(warningDiv, document.body.firstChild); + warningDiv.style.fontWeight = "bold"; + warningDiv.style.fontSize = "2em"; + warningDiv.style.color = "red"; + warningDiv.innerHTML = 'Your browser suffers from an <a href="http://software.hixie.ch/utilities/js/live-dom-viewer/saved/1028">egregious bug</a> in range mutation that will give incorrect results for the spec columns in many cases. To ensure that the spec column contains the output actually required by the spec, use a different browser.'; + } + div.parentNode.removeChild(div); + })(); + //@} + + // Insert the toolbar thingie as soon as the script file is loaded + //@{ + (function() { + var toolbarDiv = document.createElement("div"); + toolbarDiv.id = "toolbar"; + // Note: this is completely not a hack at all. + toolbarDiv.innerHTML = "<style id=alerts>body > div > table > tbody > tr:not(.alert):not(:first-child):not(.active) { display: none }</style>" + + "<label><input id=alert-checkbox type=checkbox accesskey=a checked onclick='updateAlertRowStyle()'> Display rows without spec <u>a</u>lerts</label>" + + "<label><input id=browser-checkbox type=checkbox accesskey=b checked onclick='localStorage[\"display-browser-tests\"] = event.target.checked'> Run <u>b</u>rowser tests as well as spec tests</label>"; + + document.body.appendChild(toolbarDiv); + })(); + //@} + + // Confusingly, we're storing a string here, not a boolean. + document.querySelector("#alert-checkbox").checked = localStorage["display-alerts"] != "false"; + document.querySelector("#browser-checkbox").checked = localStorage["display-browser-tests"] != "false"; + + function updateAlertRowStyle() { + //@{ + var checked = document.querySelector("#alert-checkbox").checked; + document.querySelector("#alerts").disabled = checked; + localStorage["display-alerts"] = checked; + } + //@} + updateAlertRowStyle(); + + // Feature-test whether the browser wraps at <wbr> or not, and set word-wrap: + // break-word where necessary if not. (IE and Opera don't wrap, Gecko and + // WebKit do.) word-wrap: break-word will break anywhere at all, so it looks + // significantly uglier. + //@{ + (function() { + var wordWrapTestDiv = document.createElement("div"); + wordWrapTestDiv.style.width = "5em"; + document.body.appendChild(wordWrapTestDiv); + wordWrapTestDiv.innerHTML = "abc"; + var height1 = getComputedStyle(wordWrapTestDiv).height; + wordWrapTestDiv.innerHTML = "abc<wbr>abc<wbr>abc<wbr>abc<wbr>abc<wbr>abc"; + var height2 = getComputedStyle(wordWrapTestDiv).height; + document.body.removeChild(wordWrapTestDiv); + if (height1 == height2) { + document.body.className = (document.body.className + " wbr-workaround").trim(); + } + })(); + //@} +} + +// Now for the meat of the file. +var tests = { + backcolor: [ + //@{ Same as hilitecolor (set below) + ], + //@} + bold: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + 'foo<span contenteditable=false>[bar]</span>baz', + 'fo[o<span contenteditable=false>bar</span>b]az', + 'foo<span contenteditable=false>ba[r</span>b]az', + 'fo[o<span contenteditable=false>b]ar</span>baz', + 'fo[<b>o</b><span contenteditable=false>bar</span><b>b</b>]az', + '<span contenteditable=false>foo<span contenteditable=true>[bar]</span>baz</span>', + '<span contenteditable=false>fo[o<span contenteditable=true>bar</span>b]az</span>', + '<span contenteditable=false>foo<span contenteditable=true>ba[r</span>b]az</span>', + '<span contenteditable=false>fo[o<span contenteditable=true>b]ar</span>baz</span>', + '<span contenteditable=false>fo[<b>o<span contenteditable=true>bar</span>b</b>]az</span>', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<span style="font-weight: bold">[bar]</span>baz', + 'foo<b>[bar]</b>baz', + 'foo<b>bar</b>[baz]', + '[foo]<b>bar</b>baz', + '<b>foo</b>[bar]<b>baz</b>', + 'foo<strong>bar</strong>[baz]', + '[foo]<strong>bar</strong>baz', + '<strong>foo</strong>[bar]<strong>baz</strong>', + '<b>foo</b>[bar]<strong>baz</strong>', + '<strong>foo</strong>[bar]<b>baz</b>', + 'foo[<b>bar</b>]baz', + 'foo[<b>bar]</b>baz', + 'foo<b>[bar</b>]baz', + + 'foo{<b></b>}baz', + 'foo{<i></i>}baz', + 'foo{<b><i></i></b>}baz', + 'foo{<i><b></b></i>}baz', + + 'foo<strong>[bar]</strong>baz', + 'foo[<strong>bar</strong>]baz', + 'foo[<strong>bar]</strong>baz', + 'foo<strong>[bar</strong>]baz', + 'foo[<span style="font-weight: bold">bar</span>]baz', + 'foo[<span style="font-weight: bold">bar]</span>baz', + 'foo<span style="font-weight: bold">[bar</span>]baz', + + '<b>{<p>foo</p><p>bar</p>}<p>baz</p></b>', + '<b><p>foo[<i>bar</i>}</p><p>baz</p></b>', + + 'foo [bar <b>baz] qoz</b> quz sic', + 'foo bar <b>baz [qoz</b> quz] sic', + + '<b id=purple>bar [baz] qoz</b>', + + 'foo<span style="font-weight: 100">[bar]</span>baz', + 'foo<span style="font-weight: 200">[bar]</span>baz', + 'foo<span style="font-weight: 300">[bar]</span>baz', + 'foo<span style="font-weight: 400">[bar]</span>baz', + 'foo<span style="font-weight: 500">[bar]</span>baz', + 'foo<span style="font-weight: 600">[bar]</span>baz', + 'foo<span style="font-weight: 700">[bar]</span>baz', + 'foo<span style="font-weight: 800">[bar]</span>baz', + 'foo<span style="font-weight: 900">[bar]</span>baz', + 'foo<span style="font-weight: 400">[bar</span>]baz', + 'foo<span style="font-weight: 700">[bar</span>]baz', + 'foo[<span style="font-weight: 400">bar]</span>baz', + 'foo[<span style="font-weight: 700">bar]</span>baz', + 'foo[<span style="font-weight: 400">bar</span>]baz', + 'foo[<span style="font-weight: 700">bar</span>]baz', + '<span style="font-weight: 100">foo[bar]baz</span>', + '<span style="font-weight: 400">foo[bar]baz</span>', + '<span style="font-weight: 700">foo[bar]baz</span>', + '<span style="font-weight: 900">foo[bar]baz</span>', + '{<span style="font-weight: 100">foobar]baz</span>', + '{<span style="font-weight: 400">foobar]baz</span>', + '{<span style="font-weight: 700">foobar]baz</span>', + '{<span style="font-weight: 900">foobar]baz</span>', + '<span style="font-weight: 100">foo[barbaz</span>}', + '<span style="font-weight: 400">foo[barbaz</span>}', + '<span style="font-weight: 700">foo[barbaz</span>}', + '<span style="font-weight: 900">foo[barbaz</span>}', + + '<h3>foo[bar]baz</h3>', + '{<h3>foobar]baz</h3>', + '<h3>foo[barbaz</h3>}', + '<h3>[foobarbaz]</h3>', + '{<h3>foobarbaz]</h3>', + '<h3>[foobarbaz</h3>}', + '{<h3>foobarbaz</h3>}', + + '<b>foo<span style="font-weight: normal">bar<b>[baz]</b>quz</span>qoz</b>', + '<b>foo<span style="font-weight: normal">[bar]</span>baz</b>', + + '{<b>foo</b> <b>bar</b>}', + '{<h3>foo</h3><b>bar</b>}', + + '<i><b>foo</b></i>[bar]<i><b>baz</b></i>', + '<i><b>foo</b></i>[bar]<b>baz</b>', + '<b>foo</b>[bar]<i><b>baz</b></i>', + '<font color=blue face=monospace><b>foo</b></font>[bar]', + + 'foo<span style="font-weight: normal"><b>{bar}</b></span>baz', + '[foo<span class=notbold>bar</span>baz]', + '<b><span class=notbold>[foo]</span></b>', + '<b><span class=notbold>foo[bar]baz</span></b>', + + '<p style="font-weight: bold">foo[bar]baz</p>', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<b>b]ar</b>baz', + 'foo<b>ba[r</b>b]az', + 'fo[o<b>bar</b>b]az', + 'foo[<b>b]ar</b>baz', + 'foo<b>ba[r</b>]baz', + 'foo{<b>bar</b>}baz', + 'fo[o<span style=font-weight:bold>b]ar</span>baz', + '<span style=font-weight:800>fo[o</span><span style=font-weight:900>b]ar</span>', + '<span style=font-weight:700>fo[o</span><span style=font-weight:800>b]ar</span>', + '<span style=font-weight:600>fo[o</span><span style=font-weight:700>b]ar</span>', + '<span style=font-weight:500>fo[o</span><span style=font-weight:600>b]ar</span>', + '<span style=font-weight:400>fo[o</span><span style=font-weight:500>b]ar</span>', + '<span style=font-weight:300>fo[o</span><span style=font-weight:400>b]ar</span>', + '<span style=font-weight:200>fo[o</span><span style=font-weight:300>b]ar</span>', + '<span style=font-weight:100>fo[o</span><span style=font-weight:200>b]ar</span>', + ], + //@} + createlink: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + '<a href=http://www.google.com/>foo[bar]baz</a>', + '<a href=http://www.google.com/>foo[barbaz</a>}', + '{<a href=http://www.google.com/>foobar]baz</a>', + '{<a href=http://www.google.com/>foobarbaz</a>}', + '<a href=http://www.google.com/>[foobarbaz]</a>', + + 'foo<a href=http://www.google.com/>[bar]</a>baz', + '[foo]<a href=http://www.google.com/>bar</a>baz', + 'foo<a href=http://www.google.com/>bar</a>[baz]', + 'foo[<a href=http://www.google.com/>bar</a>]baz', + 'foo<a href=http://www.google.com/>[bar</a>baz]', + '[foo<a href=http://www.google.com/>bar]</a>baz', + '[foo<a href=http://www.google.com/>bar</a>baz]', + + '<a href=otherurl>foo[bar]baz</a>', + '<a href=otherurl>foo[barbaz</a>}', + '{<a href=otherurl>foobar]baz</a>', + '{<a href=otherurl>foobarbaz</a>}', + '<a href=otherurl>[foobarbaz]</a>', + + 'foo<a href=otherurl>[bar]</a>baz', + 'foo[<a href=otherurl>bar</a>]baz', + 'foo<a href=otherurl>[bar</a>baz]', + '[foo<a href=otherurl>bar]</a>baz', + '[foo<a href=otherurl>bar</a>baz]', + + '<a href=otherurl><b>foo[bar]baz</b></a>', + '<a href=otherurl><b>foo[barbaz</b></a>}', + '{<a href=otherurl><b>foobar]baz</b></a>', + '<a href=otherurl><b>[foobarbaz]</b></a>', + + '<a name=abc>foo[bar]baz</a>', + '<a name=abc><b>foo[bar]baz</b></a>', + + ['', 'foo[bar]baz'], + ], + //@} + // Opera requires this to be quoted, contrary to ES5 11.1.5 which allows + // PropertyName to be any IdentifierName, and see 7.6 which defines + // IdentifierName to include ReservedWord; Identifier excludes it. + "delete": [ + //@{ + // Collapsed selection + // + // These three commented-out test call Firefox 5.0a2 to blow up, not + // just throwing exceptions on the tests themselves but on many + // subsequent tests too. + //'[]foo', + //'<span>[]foo</span>', + //'<p>[]foo</p>', + 'foo[]bar', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo<span style=display:none>bar</span>[]baz', + 'foo<script>bar</script>[]baz', + + 'foö[]bar', + 'foö[]bar', + 'foö̧[]bar', + 'ö[]bar', + 'ö[]bar', + 'ö̧[]bar', + + 'שָׁ[]לוֹם', + 'שָׁלוֹ[]ם', + + '<p>foo</p><p>[]bar</p>', + '<p>foo</p>[]bar', + 'foo<p>[]bar</p>', + '<p>foo<br></p><p>[]bar</p>', + '<p>foo<br></p>[]bar', + 'foo<br><p>[]bar</p>', + '<p>foo<br><br></p><p>[]bar</p>', + '<p>foo<br><br></p>[]bar', + 'foo<br><br><p>[]bar</p>', + + '<div><p>foo</p></div><p>[]bar</p>', + '<p>foo</p><div><p>[]bar</p></div>', + '<div><p>foo</p></div><div><p>[]bar</p></div>', + '<div><p>foo</p></div>[]bar', + 'foo<div><p>[]bar</p></div>', + + '<div>foo</div><div>[]bar</div>', + '<pre>foo</pre>[]bar', + + 'foo<br>[]bar', + 'foo<br><b>[]bar</b>', + 'foo<hr>[]bar', + '<p>foo<hr><p>[]bar', + '<p>foo</p><br><p>[]bar</p>', + '<p>foo</p><br><br><p>[]bar</p>', + '<p>foo</p><img src=/img/lion.svg><p>[]bar', + 'foo<img src=/img/lion.svg>[]bar', + + '<a>foo</a>[]bar', + '<a href=/>foo</a>[]bar', + '<a name=abc>foo</a>[]bar', + '<a href=/ name=abc>foo</a>[]bar', + '<span><a>foo</a></span>[]bar', + '<span><a href=/>foo</a></span>[]bar', + '<span><a name=abc>foo</a></span>[]bar', + '<span><a href=/ name=abc>foo</a></span>[]bar', + 'foo<a>[]bar</a>', + 'foo<a href=/>[]bar</a>', + 'foo<a name=abc>[]bar</a>', + 'foo<a href=/ name=abc>[]bar</a>', + + 'foo []', + ' [] foo', + 'foo []bar', + 'foo []bar', + 'foo []bar', + 'foo []bar', + 'foo [] bar', + 'foo [] bar', + 'foo []bar', + 'foo []<span> </span> bar', + 'foo <span> </span>[] bar', + 'foo <span> </span> []bar', + '<b>foo </b> []bar', + '<b>foo </b> []bar', + '<b>foo </b> []bar', + '<b>foo </b> []bar', + '<p>foo </p><p>[] bar</p>', + + '<pre>foo []</pre>', + '<pre> [] foo</pre>', + '<pre>foo []bar</pre>', + '<pre>foo []bar</pre>', + '<pre>foo []bar</pre>', + + '<div style=white-space:pre>foo []</div>', + '<div style=white-space:pre> [] foo</div>', + '<div style=white-space:pre>foo []bar</div>', + '<div style=white-space:pre>foo []bar</div>', + '<div style=white-space:pre>foo []bar</div>', + + '<div style=white-space:pre-wrap>foo []</div>', + '<div style=white-space:pre-wrap> [] foo</div>', + '<div style=white-space:pre-wrap>foo []bar</div>', + '<div style=white-space:pre-wrap>foo []bar</div>', + '<div style=white-space:pre-wrap>foo []bar</div>', + + '<div style=white-space:pre-line>foo []</div>', + '<div style=white-space:pre-line> [] foo</div>', + '<div style=white-space:pre-line>foo []bar</div>', + '<div style=white-space:pre-line>foo []bar</div>', + '<div style=white-space:pre-line>foo []bar</div>', + + '<div style=white-space:nowrap>foo []</div>', + '<div style=white-space:nowrap> [] foo</div>', + '<div style=white-space:nowrap>foo []bar</div>', + '<div style=white-space:nowrap>foo []bar</div>', + '<div style=white-space:nowrap>foo []bar</div>', + + // Tables with collapsed selection + 'foo<table><tr><td>[]bar</table>baz', + 'foo<table><tr><td>bar</table>[]baz', + '<p>foo<table><tr><td>[]bar</table><p>baz', + '<p>foo<table><tr><td>bar</table><p>[]baz', + '<table><tr><td>foo<td>[]bar</table>', + '<table><tr><td>foo<tr><td>[]bar</table>', + + 'foo<br><table><tr><td>[]bar</table>baz', + 'foo<table><tr><td>bar<br></table>[]baz', + '<p>foo<br><table><tr><td>[]bar</table><p>baz', + '<p>foo<table><tr><td>bar<br></table><p>[]baz', + '<table><tr><td>foo<br><td>[]bar</table>', + '<table><tr><td>foo<br><tr><td>[]bar</table>', + + 'foo<br><br><table><tr><td>[]bar</table>baz', + 'foo<table><tr><td>bar<br><br></table>[]baz', + '<p>foo<br><br><table><tr><td>[]bar</table><p>baz', + '<p>foo<table><tr><td>bar<br><br></table><p>[]baz', + '<table><tr><td>foo<br><br><td>[]bar</table>', + '<table><tr><td>foo<br><br><tr><td>[]bar</table>', + + 'foo<hr><table><tr><td>[]bar</table>baz', + 'foo<table><tr><td>bar<hr></table>[]baz', + '<table><tr><td>foo<hr><td>[]bar</table>', + '<table><tr><td>foo<hr><tr><td>[]bar</table>', + + // Lists with collapsed selection + 'foo<ol><li>[]bar<li>baz</ol>', + 'foo<br><ol><li>[]bar<li>baz</ol>', + 'foo<br><br><ol><li>[]bar<li>baz</ol>', + '<ol><li>foo<li>[]bar</ol>', + '<ol><li>foo<br><li>[]bar</ol>', + '<ol><li>foo<br><br><li>[]bar</ol>', + '<ol><li>foo<li>[]bar<br>baz</ol>', + '<ol><li>foo<br>bar<li>[]baz</ol>', + + '<ol><li><p>foo</p>{}bar</ol>', + + '<ol><li><p>foo<li>[]bar</ol>', + '<ol><li>foo<li><p>[]bar</ol>', + '<ol><li><p>foo<li><p>[]bar</ol>', + + '<ol><li>foo<ul><li>[]bar</ul></ol>', + 'foo<ol><ol><li>[]bar</ol></ol>', + 'foo<div><ol><li>[]bar</ol></div>', + + 'foo<dl><dt>[]bar<dd>baz</dl>', + 'foo<dl><dd>[]bar</dl>', + '<dl><dt>foo<dd>[]bar</dl>', + '<dl><dt>foo<dt>[]bar<dd>baz</dl>', + '<dl><dt>foo<dd>bar<dd>[]baz</dl>', + + '<ol><li>foo</ol>[]bar', + '<ol><li>foo<br></ol>[]bar', + '<ol><li>foo<br><br></ol>[]bar', + '<ol><li><br></ol>[]bar', + '<ol><li>foo<li><br></ol>[]bar', + + '<ol><li>foo</ol><p>[]bar', + '<ol><li>foo<br></ol><p>[]bar', + '<ol><li>foo<br><br></ol><p>[]bar', + '<ol><li><br></ol><p>[]bar', + '<ol><li>foo<li><br></ol><p>[]bar', + + '<ol><li>foo</ol>{}<br>', + '<ol><li>foo<br></ol>{}<br>', + '<ol><li>foo<br><br></ol>{}<br>', + '<ol><li><br></ol>{}<br>', + '<ol><li>foo<li><br></ol>{}<br>', + + '<ol><li>foo</ol><p>{}<br>', + '<ol><li>foo<br></ol><p>{}<br>', + '<ol><li>foo<br><br></ol><p>{}<br>', + '<ol><li><br></ol><p>{}<br>', + '<ol><li>foo<li><br></ol><p>{}<br>', + + // Indented stuff with collapsed selection + 'foo<blockquote>[]bar</blockquote>', + 'foo<blockquote><blockquote>[]bar</blockquote></blockquote>', + 'foo<blockquote><div>[]bar</div></blockquote>', + 'foo<blockquote style="color: blue">[]bar</blockquote>', + + 'foo<blockquote><blockquote><p>[]bar<p>baz</blockquote></blockquote>', + 'foo<blockquote><div><p>[]bar<p>baz</div></blockquote>', + 'foo<blockquote style="color: blue"><p>[]bar<p>baz</blockquote>', + + 'foo<blockquote><p><b>[]bar</b><p>baz</blockquote>', + 'foo<blockquote><p><strong>[]bar</strong><p>baz</blockquote>', + 'foo<blockquote><p><span>[]bar</span><p>baz</blockquote>', + + 'foo<blockquote><ol><li>[]bar</ol></blockquote><p>extra', + 'foo<blockquote>bar<ol><li>[]baz</ol>quz</blockquote><p>extra', + 'foo<blockquote><ol><li>bar</li><ol><li>[]baz</ol><li>quz</ol></blockquote><p>extra', + + // Invisible stuff with collapsed selection + 'foo<span></span>[]bar', + 'foo<span><span></span></span>[]bar', + 'foo<quasit></quasit>[]bar', + 'foo<br><span></span>[]bar', + '<span>foo<span></span></span>[]bar', + 'foo<span></span><span>[]bar</span>', + 'foo<div><div><p>[]bar</div></div>', + 'foo<div><div><p><!--abc-->[]bar</div></div>', + 'foo<div><div><!--abc--><p>[]bar</div></div>', + 'foo<div><!--abc--><div><p>[]bar</div></div>', + 'foo<!--abc--><div><div><p>[]bar</div></div>', + '<div><div><p>foo</div></div>[]bar', + '<div><div><p>foo</div></div><!--abc-->[]bar', + '<div><div><p>foo</div><!--abc--></div>[]bar', + '<div><div><p>foo</p><!--abc--></div></div>[]bar', + '<div><div><p>foo<!--abc--></div></div>[]bar', + '<div><div><p>foo</p></div></div><div><div><div>[]bar</div></div></div>', + '<div><div><p>foo<!--abc--></p></div></div><div><div><div>[]bar</div></div></div>', + '<div><div><p>foo</p><!--abc--></div></div><div><div><div>[]bar</div></div></div>', + '<div><div><p>foo</p></div><!--abc--></div><div><div><div>[]bar</div></div></div>', + '<div><div><p>foo</p></div></div><!--abc--><div><div><div>[]bar</div></div></div>', + '<div><div><p>foo</p></div></div><div><!--abc--><div><div>[]bar</div></div></div>', + '<div><div><p>foo</p></div></div><div><div><!--abc--><div>[]bar</div></div></div>', + '<div><div><p>foo</p></div></div><div><div><div><!--abc-->[]bar</div></div></div>', + + // Styled stuff with collapsed selection + '<p style=color:blue>foo<p>[]bar', + '<p style=color:blue>foo<p style=color:brown>[]bar', + '<p style=color:blue>foo<p style=color:rgba(0,0,255,1)>[]bar', + '<p style=color:transparent>foo<p style=color:rgba(0,0,0,0)>[]bar', + '<p>foo<p style=color:brown>[]bar', + '<p><font color=blue>foo</font><p>[]bar', + '<p><font color=blue>foo</font><p><font color=brown>[]bar</font>', + '<p>foo<p><font color=brown>[]bar</font>', + '<p><span style=color:blue>foo</font><p>[]bar', + '<p><span style=color:blue>foo</font><p><span style=color:brown>[]bar</font>', + '<p>foo<p><span style=color:brown>[]bar</font>', + + '<p style=background-color:aqua>foo<p>[]bar', + '<p style=background-color:aqua>foo<p style=background-color:tan>[]bar', + '<p>foo<p style=background-color:tan>[]bar', + '<p><span style=background-color:aqua>foo</font><p>[]bar', + '<p><span style=background-color:aqua>foo</font><p><span style=background-color:tan>[]bar</font>', + '<p>foo<p><span style=background-color:tan>[]bar</font>', + + '<p style=text-decoration:underline>foo<p>[]bar', + '<p style=text-decoration:underline>foo<p style=text-decoration:line-through>[]bar', + '<p>foo<p style=text-decoration:line-through>[]bar', + '<p><u>foo</u><p>[]bar', + '<p><u>foo</u><p><s>[]bar</s>', + '<p>foo<p><s>[]bar</s>', + + '<p style=color:blue>foo</p>[]bar', + 'foo<p style=color:brown>[]bar', + '<div style=color:blue><p style=color:green>foo</div>[]bar', + '<div style=color:blue><p style=color:green>foo</div><p style=color:brown>[]bar', + '<p style=color:blue>foo<div style=color:brown><p style=color:green>[]bar', + + // Uncollapsed selection + 'foo[bar]baz', + '<p>foo<span style=color:#aBcDeF>[bar]</span>baz', + '<p>foo<span style=color:#aBcDeF>{bar}</span>baz', + '<p>foo{<span style=color:#aBcDeF>bar</span>}baz', + '<p>[foo<span style=color:#aBcDeF>bar]</span>baz', + '<p>{foo<span style=color:#aBcDeF>bar}</span>baz', + '<p>foo<span style=color:#aBcDeF>[bar</span>baz]', + '<p>foo<span style=color:#aBcDeF>{bar</span>baz}', + '<p>foo<span style=color:#aBcDeF>[bar</span><span style=color:#fEdCbA>baz]</span>quz', + + 'foo<b>[bar]</b>baz', + 'foo<b>{bar}</b>baz', + 'foo{<b>bar</b>}baz', + 'foo<span>[bar]</span>baz', + 'foo<span>{bar}</span>baz', + 'foo{<span>bar</span>}baz', + '<b>foo[bar</b><i>baz]quz</i>', + '<p>foo</p><p>[bar]</p><p>baz</p>', + '<p>foo</p><p>{bar}</p><p>baz</p>', + '<p>foo</p><p>{bar</p>}<p>baz</p>', + '<p>foo</p>{<p>bar}</p><p>baz</p>', + '<p>foo</p>{<p>bar</p>}<p>baz</p>', + + '<p>foo[bar<p>baz]quz', + '<p>foo[bar<div>baz]quz</div>', + '<p>foo[bar<h1>baz]quz</h1>', + '<div>foo[bar</div><p>baz]quz', + '<blockquote>foo[bar</blockquote><pre>baz]quz</pre>', + + '<p><b>foo[bar</b><p>baz]quz', + '<div><p>foo[bar</div><p>baz]quz', + '<p>foo[bar<blockquote><p>baz]quz<p>qoz</blockquote', + '<p>foo[bar<p style=color:blue>baz]quz', + '<p>foo[bar<p><b>baz]quz</b>', + + '<div><p>foo<p>[bar<p>baz]</div>', + + 'foo[<br>]bar', + '<p>foo[</p><p>]bar</p>', + '<p>foo[</p><p>]bar<br>baz</p>', + 'foo[<p>]bar</p>', + 'foo{<p>}bar</p>', + 'foo[<p>]bar<br>baz</p>', + 'foo[<p>]bar</p>baz', + 'foo{<p>bar</p>}baz', + 'foo<p>{bar</p>}baz', + 'foo{<p>bar}</p>baz', + '<p>foo[</p>]bar', + '<p>foo{</p>}bar', + '<p>foo[</p>]bar<br>baz', + '<p>foo[</p>]bar<p>baz</p>', + 'foo[<div><p>]bar</div>', + '<div><p>foo[</p></div>]bar', + 'foo[<div><p>]bar</p>baz</div>', + 'foo[<div>]bar<p>baz</p></div>', + '<div><p>foo</p>bar[</div>]baz', + '<div>foo<p>bar[</p></div>]baz', + + '<p>foo<br>{</p>]bar', + '<p>foo<br><br>{</p>]bar', + 'foo<br>{<p>]bar</p>', + 'foo<br><br>{<p>]bar</p>', + '<p>foo<br>{</p><p>}bar</p>', + '<p>foo<br><br>{</p><p>}bar</p>', + + '<table><tbody><tr><th>foo<th>[bar]<th>baz<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>foo<th>ba[r<th>b]az<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>fo[o<th>bar<th>b]az<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>foo<th>bar<th>ba[z<tr><td>q]uz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>[foo<th>bar<th>baz]<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>[foo<th>bar<th>baz<tr><td>quz<td>qoz<td>qiz]</table>', + '{<table><tbody><tr><th>foo<th>bar<th>baz<tr><td>quz<td>qoz<td>qiz</table>}', + '<table><tbody><tr><td>foo<td>ba[r<tr><td>baz<td>quz<tr><td>q]oz<td>qiz</table>', + '<p>fo[o<table><tr><td>b]ar</table><p>baz', + '<p>foo<table><tr><td>ba[r</table><p>b]az', + '<p>fo[o<table><tr><td>bar</table><p>b]az', + + '<p>foo<ol><li>ba[r<li>b]az</ol><p>quz', + '<p>foo<ol><li>bar<li>[baz]</ol><p>quz', + '<p>fo[o<ol><li>b]ar<li>baz</ol><p>quz', + '<p>foo<ol><li>bar<li>ba[z</ol><p>q]uz', + '<p>fo[o<ol><li>bar<li>b]az</ol><p>quz', + '<p>fo[o<ol><li>bar<li>baz</ol><p>q]uz', + + '<ol><li>fo[o</ol><ol><li>b]ar</ol>', + '<ol><li>fo[o</ol><ul><li>b]ar</ul>', + + 'foo[<ol><li>]bar</ol>', + '<ol><li>foo[<li>]bar</ol>', + 'foo[<dl><dt>]bar<dd>baz</dl>', + 'foo[<dl><dd>]bar</dl>', + '<dl><dt>foo[<dd>]bar</dl>', + '<dl><dt>foo[<dt>]bar<dd>baz</dl>', + '<dl><dt>foo<dd>bar[<dd>]baz</dl>', + + '<b>foo [ </b>bar]', + 'foo<b> [ bar]</b>', + '<b>[foo ] </b>bar', + '[foo<b> ] bar</b>', + + // Do we merge based on element names or the display property? + '<p style=display:inline>fo[o<p style=display:inline>b]ar', + '<span style=display:block>fo[o</span><span style=display:block>b]ar</span>', + '<span style=display:inline-block>fo[o</span><span style=display:inline-block>b]ar</span>', + '<span style=display:inline-table>fo[o</span><span style=display:inline-table>b]ar</span>', + '<span style=display:none>fo[o</span><span style=display:none>b]ar</span>', + '<quasit style=display:block>fo[o</quasit><quasit style=display:block>b]ar</quasit>', + + // https://bugs.webkit.org/show_bug.cgi?id=35281 + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=13976 + '<ol><li>foo</ol>{}<br><ol><li>bar</ol>', + '<ol><li>foo</ol><p>{}<br></p><ol><li>bar</ol>', + '<ol><li><p>foo</ol><p>{}<br></p><ol><li>bar</ol>', + '<ol id=a><li>foo</ol>{}<br><ol><li>bar</ol>', + '<ol><li>foo</ol>{}<br><ol id=b><li>bar</ol>', + '<ol id=a><li>foo</ol>{}<br><ol id=b><li>bar</ol>', + '<ol class=a><li>foo</ol>{}<br><ol class=b><li>bar</ol>', + // Broken test: http://www.w3.org/Bugs/Public/show_bug.cgi?id=14727 + '!<ol><ol><li>foo</ol><li>{}<br><ol><li>bar</ol></ol>', + '<ol><ol><li>foo</ol><li>{}<br></li><ol><li>bar</ol></ol>', + '<ol><li>foo[</ol>bar]<ol><li>baz</ol>', + '<ol><li>foo[</ol><p>bar]<ol><li>baz</ol>', + '<ol><li><p>foo[</ol><p>bar]<ol><li>baz</ol>', + '<ol><li>foo[]</ol><ol><li>bar</ol>', + '<ol><li>foo</ol>[bar<ol><li>]baz</ol>', + '<ol><li>foo</ol><p>[bar<ol><li>]baz</ol>', + '<ol><li>foo</ol><p>[bar<ol><li><p>]baz</ol>', + '<ol><li>foo</ol><ol><li>b[]ar</ol>', + '<ol><ol><li>foo[</ol><li>bar</ol>baz]<ol><li>quz</ol>', + '<ul><li>foo</ul>{}<br><ul><li>bar</ul>', + '<ul><li>foo</ul><p>{}<br></p><ul><li>bar</ul>', + '<ol><li>foo[<li>bar]</ol><ol><li>baz</ol><ol><li>quz</ol>', + '<ol><li>foo</ol>{}<br><ul><li>bar</ul>', + '<ol><li>foo</ol><p>{}<br></p><ul><li>bar</ul>', + '<ul><li>foo</ul>{}<br><ol><li>bar</ol>', + '<ul><li>foo</ul><p>{}<br></p><ol><li>bar</ol>', + + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=13831 + '<p><b>[foo]</b>', + '<p><quasit>[foo]</quasit>', + '<p><b><i>[foo]</i></b>', + '<p><b>{foo}</b>', + '<p>{<b>foo</b>}', + '<p><b>f[]</b>', + '<b>[foo]</b>', + '<div><b>[foo]</b></div>', + ], + //@} + fontname: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<code>[bar]</code>baz', + 'foo<kbd>[bar]</kbd>baz', + 'foo<listing>[bar]</listing>baz', + 'foo<pre>[bar]</pre>baz', + 'foo<samp>[bar]</samp>baz', + 'foo<tt>[bar]</tt>baz', + + 'foo<code>b[a]r</code>baz', + 'foo<kbd>b[a]r</kbd>baz', + 'foo<listing>b[a]r</listing>baz', + 'foo<pre>b[a]r</pre>baz', + 'foo<samp>b[a]r</samp>baz', + 'foo<tt>b[a]r</tt>baz', + + '[foo<code>bar</code>baz]', + '[foo<kbd>bar</kbd>baz]', + '[foo<listing>bar</listing>baz]', + '[foo<pre>bar</pre>baz]', + '[foo<samp>bar</samp>baz]', + '[foo<tt>bar</tt>baz]', + + '[foo<code>ba]r</code>baz', + '[foo<kbd>ba]r</kbd>baz', + '[foo<listing>ba]r</listing>baz', + '[foo<pre>ba]r</pre>baz', + '[foo<samp>ba]r</samp>baz', + '[foo<tt>ba]r</tt>baz', + + 'foo<code>b[ar</code>baz]', + 'foo<kbd>b[ar</kbd>baz]', + 'foo<listing>b[ar</listing>baz]', + 'foo<pre>b[ar</pre>baz]', + 'foo<samp>b[ar</samp>baz]', + 'foo<tt>b[ar</tt>baz]', + + 'foo<span style="font-family: sans-serif">[bar]</span>baz', + 'foo<span style="font-family: sans-serif">b[a]r</span>baz', + 'foo<span style="font-family: monospace">[bar]</span>baz', + 'foo<span style="font-family: monospace">b[a]r</span>baz', + + 'foo<tt contenteditable=false>ba[r</tt>b]az', + 'fo[o<tt contenteditable=false>b]ar</tt>baz', + 'foo<tt>{}<br></tt>bar', + 'foo<tt>{<br></tt>}bar', + 'foo<tt>{<br></tt>b]ar', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<span style=font-family:monospace>b]ar</span>baz', + 'foo<span style=font-family:monospace>ba[r</span>b]az', + 'fo[o<span style=font-family:monospace>bar</span>b]az', + 'foo[<span style=font-family:monospace>b]ar</span>baz', + 'foo<span style=font-family:monospace>ba[r</span>]baz', + 'foo[<span style=font-family:monospace>bar</span>]baz', + 'foo<span style=font-family:monospace>[bar]</span>baz', + 'foo{<span style=font-family:monospace>bar</span>}baz', + 'fo[o<code>b]ar</code>', + 'fo[o<kbd>b]ar</kbd>', + 'fo[o<listing>b]ar</listing>', + 'fo[o<pre>b]ar</pre>', + 'fo[o<samp>b]ar</samp>', + 'fo[o<tt>b]ar</tt>', + '<tt>fo[o</tt><code>b]ar</code>', + '<pre>fo[o</pre><samp>b]ar</samp>', + '<span style=font-family:monospace>fo[o</span><kbd>b]ar</kbd>', + ], + //@} + fontsize: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + ["1", 'foo[bar]baz'], + ["0", 'foo[bar]baz'], + ["-5", 'foo[bar]baz'], + ["6", 'foo[bar]baz'], + ["7", 'foo[bar]baz'], + ["8", 'foo[bar]baz'], + ["100", 'foo[bar]baz'], + ["2em", 'foo[bar]baz'], + ["20pt", 'foo[bar]baz'], + ["xx-large", 'foo[bar]baz'], + [" 1 ", 'foo[bar]baz'], + ["1.", 'foo[bar]baz'], + ["1.0", 'foo[bar]baz'], + ["1.0e2", 'foo[bar]baz'], + ["1.1", 'foo[bar]baz'], + ["1.9", 'foo[bar]baz'], + ["+0", 'foo[bar]baz'], + ["+1", 'foo[bar]baz'], + ["+9", 'foo[bar]baz'], + ["-0", 'foo[bar]baz'], + ["-1", 'foo[bar]baz'], + ["-9", 'foo[bar]baz'], + ["", 'foo[bar]baz'], + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<font size=1>[bar]</font>baz', + '<font size=1>foo[bar]baz</font>', + 'foo<font size=3>[bar]</font>baz', + '<font size=3>foo[bar]baz</font>', + 'foo<font size=4>[bar]</font>baz', + '<font size=4>foo[bar]baz</font>', + 'foo<font size=+1>[bar]</font>baz', + '<font size=+1>foo[bar]baz</font>', + '<font size=4>foo<font size=1>b[a]r</font>baz</font>', + + 'foo<span style="font-size: xx-small">[bar]</span>baz', + '<span style="font-size: xx-small">foo[bar]baz</span>', + 'foo<span style="font-size: medium">[bar]</span>baz', + '<span style="font-size: medium">foo[bar]baz</span>', + 'foo<span style="font-size: large">[bar]</span>baz', + '<span style="font-size: large">foo[bar]baz</span>', + '<span style="font-size: large">foo<span style="font-size: xx-small">b[a]r</span>baz</span>', + + 'foo<span style="font-size: 2em">[bar]</span>baz', + '<span style="font-size: 2em">foo[bar]baz</span>', + + '<p style="font-size: xx-small">foo[bar]baz</p>', + '<p style="font-size: medium">foo[bar]baz</p>', + '<p style="font-size: large">foo[bar]baz</p>', + '<p style="font-size: 2em">foo[bar]baz</p>', + + ["3", '<p style="font-size: xx-small">foo[bar]baz</p>'], + ["3", '<p style="font-size: medium">foo[bar]baz</p>'], + ["3", '<p style="font-size: large">foo[bar]baz</p>'], + ["3", '<p style="font-size: 2em">foo[bar]baz</p>'], + + // Minor algorithm bug: this changes the size of the "b" and "r" in + // "bar" when we pull down styles + ["3", '<font size=6>foo <span style="font-size: 2em">b[a]r</span> baz</font>'], + + ["3", 'foo<big>[bar]</big>baz'], + ["3", 'foo<big>b[a]r</big>baz'], + ["3", 'foo<small>[bar]</small>baz'], + ["3", 'foo<small>b[a]r</small>baz'], + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<font size=2>b]ar</font>baz', + 'foo<font size=2>ba[r</font>b]az', + 'fo[o<font size=2>bar</font>b]az', + 'foo[<font size=2>b]ar</font>baz', + 'foo<font size=2>ba[r</font>]baz', + 'foo[<font size=2>bar</font>]baz', + 'foo<font size=2>[bar]</font>baz', + 'foo{<font size=2>bar</font>}baz', + '<font size=1>fo[o</font><span style=font-size:xx-small>b]ar</span>', + '<font size=2>fo[o</font><span style=font-size:small>b]ar</span>', + '<font size=3>fo[o</font><span style=font-size:medium>b]ar</span>', + '<font size=4>fo[o</font><span style=font-size:large>b]ar</span>', + '<font size=5>fo[o</font><span style=font-size:x-large>b]ar</span>', + '<font size=6>fo[o</font><span style=font-size:xx-large>b]ar</span>', + + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=13829 + ["!6", '<span style=background-color:aqua>[foo]</span>'], + ["!6", '<span style=background-color:aqua>foo[bar]baz</span>'], + ["!6", '[foo<span style=background-color:aqua>bar</span>baz]'], + ], + //@} + forecolor: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + ['blue', 'foo[bar]baz'], + ['f', 'foo[bar]baz'], + ['#f', 'foo[bar]baz'], + ['00f', 'foo[bar]baz'], + ['#00f', 'foo[bar]baz'], + ['0000ff', 'foo[bar]baz'], + ['#0000ff', 'foo[bar]baz'], + ['000000fff', 'foo[bar]baz'], + ['#000000fff', 'foo[bar]baz'], + ['rgb(0, 0, 255)', 'foo[bar]baz'], + ['rgb(0%, 0%, 100%)', 'foo[bar]baz'], + ['rgb( 0 ,0 ,255)', 'foo[bar]baz'], + ['rgba(0, 0, 255, 0.0)', 'foo[bar]baz'], + ['rgb(15, -10, 375)', 'foo[bar]baz'], + ['rgba(0, 0, 0, 1)', 'foo[bar]baz'], + ['rgba(255, 255, 255, 1)', 'foo[bar]baz'], + ['rgba(0, 0, 255, 0.5)', 'foo[bar]baz'], + ['hsl(240, 100%, 50%)', 'foo[bar]baz'], + ['cornsilk', 'foo[bar]baz'], + ['potato quiche', 'foo[bar]baz'], + ['transparent', 'foo[bar]baz'], + ['currentColor', 'foo[bar]baz'], + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<font color=blue>[bar]</font>baz', + 'foo{<font color=blue>bar</font>}baz', + '<span style="color: blue">foo<span style="color: brown">[bar]</span>baz</span>', + '<span style="color: #00f">foo<span style="color: brown">[bar]</span>baz</span>', + '<span style="color: #0000ff">foo<span style="color: brown">[bar]</span>baz</span>', + '<span style="color: rgb(0, 0, 255)">foo<span style="color: brown">[bar]</span>baz</span>', + '<font color=blue>foo<font color=brown>[bar]</font>baz</font>', + '<span style="color: rgb(0, 0, 255)">foo<span style="color: brown">b[ar]</span>baz</span>', + 'foo<span id=purple>ba[r</span>ba]z', + '<span style="color: rgb(0, 0, 255)">foo<span id=purple>b[a]r</span>baz</span>', + + ['blue', '<a href=http://www.google.com>foo[bar]baz</a>'], + ['#0000ff', '<a href=http://www.google.com>foo[bar]baz</a>'], + ['rgb(0,0,255)', '<a href=http://www.google.com>foo[bar]baz</a>'], + + // Tests for queryCommandValue() + '<font color="blue">[foo]</font>', + '<font color="0000ff">[foo]</font>', + '<font color="#0000ff">[foo]</font>', + '<span style="color: blue">[foo]</span>', + '<span style="color: #0000ff">[foo]</span>', + '<span style="color: rgb(0, 0, 255)">[foo]</span>', + '<span style="color: rgb(0%, 0%, 100%)">[foo]</span>', + '<span style="color: rgb( 0 ,0 ,255)">[foo]</span>', + '<span style="color: rgba(0, 0, 255, 0.0)">[foo]</span>', + '<span style="color: rgb(15, -10, 375)">[foo]</span>', + '<span style="color: rgba(0, 0, 0, 1)">[foo]</span>', + '<span style="color: rgba(255, 255, 255, 1)">[foo]</span>', + '<span style="color: rgba(0, 0, 255, 0.5)">[foo]</span>', + '<span style="color: hsl(240, 100%, 50%)">[foo]</span>', + '<span style="color: cornsilk">[foo]</span>', + '<span style="color: transparent">[foo]</span>', + '<span style="color: currentColor">[foo]</span>', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<font color=brown>b]ar</font>baz', + 'foo<font color=brown>ba[r</font>b]az', + 'fo[o<font color=brown>bar</font>b]az', + 'foo[<font color=brown>b]ar</font>baz', + 'foo<font color=brown>ba[r</font>]baz', + 'foo[<font color=brown>bar</font>]baz', + 'foo<font color=brown>[bar]</font>baz', + 'foo{<font color=brown>bar</font>}baz', + '<font color=brown>fo[o</font><span style=color:brown>b]ar</span>', + '<span style=color:brown>fo[o</span><span style=color:#0000ff>b]ar</span>', + ], + //@} + formatblock: [ + //@{ + 'foo[]bar<p>extra', + '<span>foo</span>{}<span>bar</span><p>extra', + '<span>foo[</span><span>]bar</span><p>extra', + 'foo[bar]baz<p>extra', + 'foo]bar[baz<p>extra', + '{<p><p> <p>foo</p>}', + 'foo[bar<i>baz]qoz</i>quz<p>extra', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + '<div>[foobar]</div>', + '<p>[foobar]</p>', + '<blockquote>[foobar]</blockquote>', + '<h1>[foobar]</h1>', + '<h2>[foobar]</h2>', + '<h3>[foobar]</h3>', + '<h4>[foobar]</h4>', + '<h5>[foobar]</h5>', + '<h6>[foobar]</h6>', + '<dl><dt>[foo]<dd>bar</dl>', + '<dl><dt>foo<dd>[bar]</dl>', + '<dl><dt>[foo<dd>bar]</dl>', + '<ol><li>[foobar]</ol>', + '<ul><li>[foobar]</ul>', + '<address>[foobar]</address>', + '<pre>[foobar]</pre>', + '<article>[foobar]</article>', + '<ins>[foobar]</ins>', + '<del>[foobar]</del>', + '<quasit>[foobar]</quasit>', + '<quasit style="display: block">[foobar]</quasit>', + + ['<p>', 'foo[]bar<p>extra'], + ['<p>', '<span>foo</span>{}<span>bar</span><p>extra'], + ['<p>', '<span>foo[</span><span>]bar</span><p>extra'], + ['<p>', 'foo[bar]baz<p>extra'], + ['<p>', 'foo]bar[baz<p>extra'], + ['<p>', '{<p><p> <p>foo</p>}'], + ['<p>', 'foo[bar<i>baz]qoz</i>quz<p>extra'], + + ['<p>', '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>'], + ['<p>', '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>'], + ['<p>', '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>'], + ['<p>', '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>'], + ['<p>', '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>'], + ['<p>', '{<table><tr><td>foo<td>bar<td>baz</table>}'], + + ['<p>', '<div>[foobar]</div>'], + ['<p>', '<p>[foobar]</p>'], + ['<p>', '<blockquote>[foobar]</blockquote>'], + ['<p>', '<h1>[foobar]</h1>'], + ['<p>', '<h2>[foobar]</h2>'], + ['<p>', '<h3>[foobar]</h3>'], + ['<p>', '<h4>[foobar]</h4>'], + ['<p>', '<h5>[foobar]</h5>'], + ['<p>', '<h6>[foobar]</h6>'], + ['<p>', '<dl><dt>[foo]<dd>bar</dl>'], + ['<p>', '<dl><dt>foo<dd>[bar]</dl>'], + ['<p>', '<dl><dt>[foo<dd>bar]</dl>'], + ['<p>', '<ol><li>[foobar]</ol>'], + ['<p>', '<ul><li>[foobar]</ul>'], + ['<p>', '<address>[foobar]</address>'], + ['<p>', '<pre>[foobar]</pre>'], + ['<p>', '<listing>[foobar]</listing>'], + ['<p>', '<xmp>[foobar]</xmp>'], + ['<p>', '<article>[foobar]</article>'], + ['<p>', '<ins>[foobar]</ins>'], + ['<p>', '<del>[foobar]</del>'], + ['<p>', '<quasit>[foobar]</quasit>'], + ['<p>', '<quasit style="display: block">[foobar]</quasit>'], + + ['<blockquote>', '<blockquote>[foo]</blockquote><p>extra'], + ['<blockquote>', '<blockquote><p>[foo]<p>bar</blockquote><p>extra'], + ['<blockquote>', '[foo]<blockquote>bar</blockquote><p>extra'], + ['<blockquote>', '<p>[foo<p>bar]<p>baz'], + ['<blockquote>', '<section>[foo]</section>'], + ['<blockquote>', '<section><p>[foo]</section>'], + ['<blockquote>', '<section><hgroup><h1>[foo]</h1><h2>bar</h2></hgroup><p>baz</section>'], + ['<article>', '<section>[foo]</section>'], + + ['<address>', '<div>[foobar]</div>'], + ['<article>', '<div>[foobar]</div>'], + ['<blockquote>', '<div>[foobar]</div>'], + ['<dd>', '<div>[foobar]</div>'], + ['<del>', '<div>[foobar]</div>'], + ['<dl>', '<div>[foobar]</div>'], + ['<dt>', '<div>[foobar]</div>'], + ['<h1>', '<div>[foobar]</div>'], + ['<h2>', '<div>[foobar]</div>'], + ['<h3>', '<div>[foobar]</div>'], + ['<h4>', '<div>[foobar]</div>'], + ['<h5>', '<div>[foobar]</div>'], + ['<h6>', '<div>[foobar]</div>'], + ['<ins>', '<div>[foobar]</div>'], + ['<li>', '<div>[foobar]</div>'], + ['<ol>', '<div>[foobar]</div>'], + ['<pre>', '<div>[foobar]</div>'], + ['<ul>', '<div>[foobar]</div>'], + ['<quasit>', '<div>[foobar]</div>'], + + ['<address>', '<p>[foobar]</p>'], + ['<article>', '<p>[foobar]</p>'], + ['<aside>', '<p>[foobar]</p>'], + ['<blockquote>', '<p>[foobar]</p>'], + ['<body>', '<p>[foobar]</p>'], + ['<dd>', '<p>[foobar]</p>'], + ['<del>', '<p>[foobar]</p>'], + ['<details>', '<p>[foobar]</p>'], + ['<dir>', '<p>[foobar]</p>'], + ['<dl>', '<p>[foobar]</p>'], + ['<dt>', '<p>[foobar]</p>'], + ['<fieldset>', '<p>[foobar]</p>'], + ['<figcaption>', '<p>[foobar]</p>'], + ['<figure>', '<p>[foobar]</p>'], + ['<footer>', '<p>[foobar]</p>'], + ['<form>', '<p>[foobar]</p>'], + ['<h1>', '<p>[foobar]</p>'], + ['<h2>', '<p>[foobar]</p>'], + ['<h3>', '<p>[foobar]</p>'], + ['<h4>', '<p>[foobar]</p>'], + ['<h5>', '<p>[foobar]</p>'], + ['<h6>', '<p>[foobar]</p>'], + ['<header>', '<p>[foobar]</p>'], + ['<head>', '<p>[foobar]</p>'], + ['<hgroup>', '<p>[foobar]</p>'], + ['<hr>', '<p>[foobar]</p>'], + ['<html>', '<p>[foobar]</p>'], + ['<ins>', '<p>[foobar]</p>'], + ['<li>', '<p>[foobar]</p>'], + ['<listing>', '<p>[foobar]</p>'], + ['<menu>', '<p>[foobar]</p>'], + ['<nav>', '<p>[foobar]</p>'], + ['<ol>', '<p>[foobar]</p>'], + ['<plaintext>', '<p>[foobar]</p>'], + ['<pre>', '<p>[foobar]</p>'], + ['<section>', '<p>[foobar]</p>'], + ['<ul>', '<p>[foobar]</p>'], + ['<xmp>', '<p>[foobar]</p>'], + ['<quasit>', '<p>[foobar]</p>'], + + ['<address>', '<p>[foo<p>bar]'], + ['<article>', '<p>[foo<p>bar]'], + ['<aside>', '<p>[foo<p>bar]'], + ['<blockquote>', '<p>[foo<p>bar]'], + ['<body>', '<p>[foo<p>bar]'], + ['<dd>', '<p>[foo<p>bar]'], + ['<del>', '<p>[foo<p>bar]'], + ['<details>', '<p>[foo<p>bar]'], + ['<dir>', '<p>[foo<p>bar]'], + ['<div>', '<p>[foo<p>bar]'], + ['<dl>', '<p>[foo<p>bar]'], + ['<dt>', '<p>[foo<p>bar]'], + ['<fieldset>', '<p>[foo<p>bar]'], + ['<figcaption>', '<p>[foo<p>bar]'], + ['<figure>', '<p>[foo<p>bar]'], + ['<footer>', '<p>[foo<p>bar]'], + ['<form>', '<p>[foo<p>bar]'], + ['<h1>', '<p>[foo<p>bar]'], + ['<h2>', '<p>[foo<p>bar]'], + ['<h3>', '<p>[foo<p>bar]'], + ['<h4>', '<p>[foo<p>bar]'], + ['<h5>', '<p>[foo<p>bar]'], + ['<h6>', '<p>[foo<p>bar]'], + ['<header>', '<p>[foo<p>bar]'], + ['<head>', '<p>[foo<p>bar]'], + ['<hgroup>', '<p>[foo<p>bar]'], + ['<hr>', '<p>[foo<p>bar]'], + ['<html>', '<p>[foo<p>bar]'], + ['<ins>', '<p>[foo<p>bar]'], + ['<li>', '<p>[foo<p>bar]'], + ['<listing>', '<p>[foo<p>bar]'], + ['<menu>', '<p>[foo<p>bar]'], + ['<nav>', '<p>[foo<p>bar]'], + ['<ol>', '<p>[foo<p>bar]'], + ['<p>', '<p>[foo<p>bar]'], + ['<plaintext>', '<p>[foo<p>bar]'], + ['<pre>', '<p>[foo<p>bar]'], + ['<section>', '<p>[foo<p>bar]'], + ['<ul>', '<p>[foo<p>bar]'], + ['<xmp>', '<p>[foo<p>bar]'], + ['<quasit>', '<p>[foo<p>bar]'], + + ['p', '<div>[foobar]</div>'], + + '<ol><li>[foo]<li>bar</ol>', + + ['<p>', '<h1>[foo]<br>bar</h1>'], + ['<p>', '<h1>foo<br>[bar]</h1>'], + ['<p>', '<h1>[foo<br>bar]</h1>'], + ['<address>', '<h1>[foo]<br>bar</h1>'], + ['<address>', '<h1>foo<br>[bar]</h1>'], + ['<address>', '<h1>[foo<br>bar]</h1>'], + ['<pre>', '<h1>[foo]<br>bar</h1>'], + ['<pre>', '<h1>foo<br>[bar]</h1>'], + ['<pre>', '<h1>[foo<br>bar]</h1>'], + ['<h2>', '<h1>[foo]<br>bar</h1>'], + ['<h2>', '<h1>foo<br>[bar]</h1>'], + ['<h2>', '<h1>[foo<br>bar]</h1>'], + + ['<h1>', '<p>[foo]<br>bar</p>'], + ['<h1>', '<p>foo<br>[bar]</p>'], + ['<h1>', '<p>[foo<br>bar]</p>'], + ['<address>', '<p>[foo]<br>bar</p>'], + ['<address>', '<p>foo<br>[bar]</p>'], + ['<address>', '<p>[foo<br>bar]</p>'], + ['<pre>', '<p>[foo]<br>bar</p>'], + ['<pre>', '<p>foo<br>[bar]</p>'], + ['<pre>', '<p>[foo<br>bar]</p>'], + + ['<p>', '<address>[foo]<br>bar</address>'], + ['<p>', '<address>foo<br>[bar]</address>'], + ['<p>', '<address>[foo<br>bar]</address>'], + ['<pre>', '<address>[foo]<br>bar</address>'], + ['<pre>', '<address>foo<br>[bar]</address>'], + ['<pre>', '<address>[foo<br>bar]</address>'], + ['<h1>', '<address>[foo]<br>bar</address>'], + ['<h1>', '<address>foo<br>[bar]</address>'], + ['<h1>', '<address>[foo<br>bar]</address>'], + + ['<p>', '<pre>[foo]<br>bar</pre>'], + ['<p>', '<pre>foo<br>[bar]</pre>'], + ['<p>', '<pre>[foo<br>bar]</pre>'], + ['<address>', '<pre>[foo]<br>bar</pre>'], + ['<address>', '<pre>foo<br>[bar]</pre>'], + ['<address>', '<pre>[foo<br>bar]</pre>'], + ['<h1>', '<pre>[foo]<br>bar</pre>'], + ['<h1>', '<pre>foo<br>[bar]</pre>'], + ['<h1>', '<pre>[foo<br>bar]</pre>'], + + ['<h1>', '<p>[foo</p>bar]'], + ['<h1>', '[foo<p>bar]</p>'], + ['<p>', '<div>[foo<p>bar]</p></div>'], + ['<p>', '<xmp>[foo]</xmp>'], + ['<div>', '<xmp>[foo]</xmp>'], + + '<div><ol><li>[foo]</ol></div>', + '<div><table><tr><td>[foo]</table></div>', + '<p>[foo<h1>bar]</h1>', + '<h1>[foo</h1><h2>bar]</h2>', + '<div>[foo</div>bar]', + + // https://bugs.webkit.org/show_bug.cgi?id=47054 + ['<p>', '<div style=color:blue>[foo]</div>'], + // https://bugs.webkit.org/show_bug.cgi?id=47574 + ['<h1>', '{<p>foo</p>ba]r'], + ['<pre>', ' [foo<p>bar]</p>'], + // From https://bugs.webkit.org/show_bug.cgi?id=47300 + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=14009 + ['!<p>', '{<pre> foo bar </pre>}'], + ], + //@} + forwarddelete: [ + //@{ + // Collapsed selection + 'foo[]', + '<span>foo[]</span>', + '<p>foo[]</p>', + 'foo[]bar', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[]<span style=display:none>bar</span>baz', + 'foo[]<script>bar</script>baz', + 'fo[]öbar', + 'fo[]öbar', + 'fo[]ö̧bar', + '[]öbar', + '[]öbar', + '[]ö̧bar', + + '[]שָׁלוֹם', + 'שָׁל[]וֹם', + + '<p>foo[]</p><p>bar</p>', + '<p>foo[]</p>bar', + 'foo[]<p>bar</p>', + '<p>foo[]<br></p><p>bar</p>', + '<p>foo[]<br></p>bar', + 'foo[]<br><p>bar</p>', + + '<p>{}<br></p>foo', + '<p>{}<span><br></span></p>foo', + 'foo{}<p><br>', + 'foo{}<p><span><br></span>', + 'foo{}<br><p><br>', + 'foo{}<span><br></span><p><br>', + 'foo{}<br><p><span><br></span>', + 'foo{}<span><br></span><p><span><br></span>', + 'foo{}<p>', + '<table><tr><td>{}</table>foo', + '<table><tr><td>{}<br></table>foo', + '<table><tr><td>{}<span><br></span></table>foo', + + '<div><p>foo[]</p></div><p>bar</p>', + '<p>foo[]</p><div><p>bar</p></div>', + '<div><p>foo[]</p></div><div><p>bar</p></div>', + '<div><p>foo[]</p></div>bar', + 'foo[]<div><p>bar</p></div>', + + '<div>foo[]</div><div>bar</div>', + '<pre>foo[]</pre>bar', + + 'foo[]<br>bar', + '<b>foo[]</b><br>bar', + 'foo[]<hr>bar', + '<p>foo[]<hr><p>bar', + '<p>foo[]</p><br><p>bar</p>', + '<p>foo[]</p><br><br><p>bar</p>', + '<p>foo[]</p><img src=/img/lion.svg><p>bar', + 'foo[]<img src=/img/lion.svg>bar', + + 'foo[]<a>bar</a>', + 'foo[]<a href=/>bar</a>', + 'foo[]<a name=abc>bar</a>', + 'foo[]<a href=/ name=abc>bar</a>', + 'foo[]<span><a>bar</a></span>', + 'foo[]<span><a href=/>bar</a></span>', + 'foo[]<span><a name=abc>bar</a></span>', + 'foo[]<span><a href=/ name=abc>bar</a></span>', + '<a>foo[]</a>bar', + '<a href=/>foo[]</a>bar', + '<a name=abc>foo[]</a>bar', + '<a href=/ name=abc>foo[]</a>bar', + + 'foo [] ', + '[] foo', + 'foo[] bar', + 'foo[] bar', + 'foo[] bar', + 'foo[] bar', + 'foo[] bar', + 'foo [] bar', + 'foo [] bar', + 'foo[] <span> </span> bar', + 'foo []<span> </span> bar', + 'foo <span> </span>[] bar', + '<b>foo[] </b> bar', + '<b>foo[] </b> bar', + '<b>foo[] </b> bar', + '<b>foo[] </b> bar', + + '<pre>foo [] </pre>', + '<pre>[] foo</pre>', + '<pre>foo[] bar</pre>', + '<pre>foo[] bar</pre>', + '<pre>foo[] bar</pre>', + + '<div style=white-space:pre>foo [] </div>', + '<div style=white-space:pre>[] foo</div>', + '<div style=white-space:pre>foo[] bar</div>', + '<div style=white-space:pre>foo[] bar</div>', + '<div style=white-space:pre>foo[] bar</div>', + + '<div style=white-space:pre-wrap>foo [] </div>', + '<div style=white-space:pre-wrap>[] foo</div>', + '<div style=white-space:pre-wrap>foo[] bar</div>', + '<div style=white-space:pre-wrap>foo[] bar</div>', + '<div style=white-space:pre-wrap>foo[] bar</div>', + + '<div style=white-space:pre-line>foo [] </div>', + '<div style=white-space:pre-line>[] foo</div>', + '<div style=white-space:pre-line>foo[] bar</div>', + '<div style=white-space:pre-line>foo[] bar</div>', + '<div style=white-space:pre-line>foo[] bar</div>', + + '<div style=white-space:nowrap>foo [] </div>', + '<div style=white-space:nowrap>[] foo</div>', + '<div style=white-space:nowrap>foo[] bar</div>', + '<div style=white-space:nowrap>foo[] bar</div>', + '<div style=white-space:nowrap>foo[] bar</div>', + + // Tables with collapsed selection + 'foo[]<table><tr><td>bar</table>baz', + 'foo<table><tr><td>bar[]</table>baz', + '<p>foo[]<table><tr><td>bar</table><p>baz', + '<table><tr><td>foo[]<td>bar</table>', + '<table><tr><td>foo[]<tr><td>bar</table>', + + 'foo[]<br><table><tr><td>bar</table>baz', + 'foo<table><tr><td>bar[]<br></table>baz', + '<p>foo[]<br><table><tr><td>bar</table><p>baz', + '<p>foo<table><tr><td>bar[]<br></table><p>baz', + '<table><tr><td>foo[]<br><td>bar</table>', + '<table><tr><td>foo[]<br><tr><td>bar</table>', + + 'foo<table><tr><td>bar[]</table><br>baz', + 'foo[]<table><tr><td><hr>bar</table>baz', + '<table><tr><td>foo[]<td><hr>bar</table>', + '<table><tr><td>foo[]<tr><td><hr>bar</table>', + + // Lists with collapsed selection + 'foo[]<ol><li>bar<li>baz</ol>', + 'foo[]<br><ol><li>bar<li>baz</ol>', + '<ol><li>foo[]<li>bar</ol>', + '<ol><li>foo[]<br><li>bar</ol>', + '<ol><li>foo[]<li>bar<br>baz</ol>', + + '<ol><li><p>foo[]<li>bar</ol>', + '<ol><li>foo[]<li><p>bar</ol>', + '<ol><li><p>foo[]<li><p>bar</ol>', + + '<ol><li>foo[]<ul><li>bar</ul></ol>', + 'foo[]<ol><ol><li>bar</ol></ol>', + 'foo[]<div><ol><li>bar</ol></div>', + + 'foo[]<dl><dt>bar<dd>baz</dl>', + 'foo[]<dl><dd>bar</dl>', + '<dl><dt>foo[]<dd>bar</dl>', + '<dl><dt>foo[]<dt>bar<dd>baz</dl>', + '<dl><dt>foo<dd>bar[]<dd>baz</dl>', + + '<ol><li>foo[]</ol>bar', + '<ol><li>foo[]<br></ol>bar', + '<ol><li>{}<br></ol>bar', + '<ol><li>foo<li>{}<br></ol>bar', + + '<ol><li>foo[]</ol><p>bar', + '<ol><li>foo[]<br></ol><p>bar', + '<ol><li>{}<br></ol><p>bar', + '<ol><li>foo<li>{}<br></ol><p>bar', + + '<ol><li>foo[]</ol><br>', + '<ol><li>foo[]<br></ol><br>', + '<ol><li>{}<br></ol><br>', + '<ol><li>foo<li>{}<br></ol><br>', + + '<ol><li>foo[]</ol><p><br>', + '<ol><li>foo[]<br></ol><p><br>', + '<ol><li>{}<br></ol><p><br>', + '<ol><li>foo<li>{}<br></ol><p><br>', + + // Indented stuff with collapsed selection + 'foo[]<blockquote>bar</blockquote>', + 'foo[]<blockquote><blockquote>bar</blockquote></blockquote>', + 'foo[]<blockquote><div>bar</div></blockquote>', + 'foo[]<blockquote style="color: blue">bar</blockquote>', + + 'foo[]<blockquote><blockquote><p>bar<p>baz</blockquote></blockquote>', + 'foo[]<blockquote><div><p>bar<p>baz</div></blockquote>', + 'foo[]<blockquote style="color: blue"><p>bar<p>baz</blockquote>', + + 'foo[]<blockquote><p><b>bar</b><p>baz</blockquote>', + 'foo[]<blockquote><p><strong>bar</strong><p>baz</blockquote>', + 'foo[]<blockquote><p><span>bar</span><p>baz</blockquote>', + + 'foo[]<blockquote><ol><li>bar</ol></blockquote><p>extra', + 'foo[]<blockquote>bar<ol><li>baz</ol>quz</blockquote><p>extra', + 'foo<blockquote><ol><li>bar[]</li><ol><li>baz</ol><li>quz</ol></blockquote><p>extra', + + // Invisible stuff with collapsed selection + 'foo[]<span></span>bar', + 'foo[]<span><span></span></span>bar', + 'foo[]<quasit></quasit>bar', + 'foo[]<span></span><br>bar', + '<span>foo[]<span></span></span>bar', + 'foo[]<span></span><span>bar</span>', + 'foo[]<div><div><p>bar</div></div>', + 'foo[]<div><div><p><!--abc-->bar</div></div>', + 'foo[]<div><div><!--abc--><p>bar</div></div>', + 'foo[]<div><!--abc--><div><p>bar</div></div>', + 'foo[]<!--abc--><div><div><p>bar</div></div>', + '<div><div><p>foo[]</div></div>bar', + '<div><div><p>foo[]</div></div><!--abc-->bar', + '<div><div><p>foo[]</div><!--abc--></div>bar', + '<div><div><p>foo[]</p><!--abc--></div></div>bar', + '<div><div><p>foo[]<!--abc--></div></div>bar', + '<div><div><p>foo[]</p></div></div><div><div><div>bar</div></div></div>', + '<div><div><p>foo[]<!--abc--></p></div></div><div><div><div>bar</div></div></div>', + '<div><div><p>foo[]</p><!--abc--></div></div><div><div><div>bar</div></div></div>', + '<div><div><p>foo[]</p></div><!--abc--></div><div><div><div>bar</div></div></div>', + '<div><div><p>foo[]</p></div></div><!--abc--><div><div><div>bar</div></div></div>', + '<div><div><p>foo[]</p></div></div><div><!--abc--><div><div>bar</div></div></div>', + '<div><div><p>foo[]</p></div></div><div><div><!--abc--><div>bar</div></div></div>', + '<div><div><p>foo[]</p></div></div><div><div><div><!--abc-->bar</div></div></div>', + + // Styled stuff with collapsed selection + '<p style=color:blue>foo[]<p>bar', + '<p style=color:blue>foo[]<p style=color:brown>bar', + '<p>foo[]<p style=color:brown>bar', + '<p><font color=blue>foo[]</font><p>bar', + '<p><font color=blue>foo[]</font><p><font color=brown>bar</font>', + '<p>foo[]<p><font color=brown>bar</font>', + '<p><span style=color:blue>foo[]</font><p>bar', + '<p><span style=color:blue>foo[]</font><p><span style=color:brown>bar</font>', + '<p>foo[]<p><span style=color:brown>bar</font>', + + '<p style=background-color:aqua>foo[]<p>bar', + '<p style=background-color:aqua>foo[]<p style=background-color:tan>bar', + '<p>foo[]<p style=background-color:tan>bar', + '<p><span style=background-color:aqua>foo[]</font><p>bar', + '<p><span style=background-color:aqua>foo[]</font><p><span style=background-color:tan>bar</font>', + '<p>foo[]<p><span style=background-color:tan>bar</font>', + + '<p style=text-decoration:underline>foo[]<p>bar', + '<p style=text-decoration:underline>foo[]<p style=text-decoration:line-through>bar', + '<p>foo[]<p style=text-decoration:line-through>bar', + '<p><u>foo[]</u><p>bar', + '<p><u>foo[]</u><p><s>bar</s>', + '<p>foo[]<p><s>bar</s>', + + '<p style=color:blue>foo[]</p>bar', + 'foo[]<p style=color:brown>bar', + '<div style=color:blue><p style=color:green>foo[]</div>bar', + '<div style=color:blue><p style=color:green>foo[]</div><p style=color:brown>bar', + '<p style=color:blue>foo[]<div style=color:brown><p style=color:green>bar', + + // Uncollapsed selection (should be same as delete command) + 'foo[bar]baz', + '<p>foo<span style=color:#aBcDeF>[bar]</span>baz', + '<p>foo<span style=color:#aBcDeF>{bar}</span>baz', + '<p>foo{<span style=color:#aBcDeF>bar</span>}baz', + '<p>[foo<span style=color:#aBcDeF>bar]</span>baz', + '<p>{foo<span style=color:#aBcDeF>bar}</span>baz', + '<p>foo<span style=color:#aBcDeF>[bar</span>baz]', + '<p>foo<span style=color:#aBcDeF>{bar</span>baz}', + '<p>foo<span style=color:#aBcDeF>[bar</span><span style=color:#fEdCbA>baz]</span>quz', + + 'foo<b>[bar]</b>baz', + 'foo<b>{bar}</b>baz', + 'foo{<b>bar</b>}baz', + 'foo<span>[bar]</span>baz', + 'foo<span>{bar}</span>baz', + 'foo{<span>bar</span>}baz', + '<b>foo[bar</b><i>baz]quz</i>', + '<p>foo</p><p>[bar]</p><p>baz</p>', + '<p>foo</p><p>{bar}</p><p>baz</p>', + '<p>foo</p><p>{bar</p>}<p>baz</p>', + '<p>foo</p>{<p>bar}</p><p>baz</p>', + '<p>foo</p>{<p>bar</p>}<p>baz</p>', + + '<p>foo[bar<p>baz]quz', + '<p>foo[bar<div>baz]quz</div>', + '<p>foo[bar<h1>baz]quz</h1>', + '<div>foo[bar</div><p>baz]quz', + '<blockquote>foo[bar</blockquote><pre>baz]quz</pre>', + + '<p><b>foo[bar</b><p>baz]quz', + '<div><p>foo[bar</div><p>baz]quz', + '<p>foo[bar<blockquote><p>baz]quz<p>qoz</blockquote', + '<p>foo[bar<p style=color:blue>baz]quz', + '<p>foo[bar<p><b>baz]quz</b>', + + '<div><p>foo<p>[bar<p>baz]</div>', + + 'foo[<br>]bar', + '<p>foo[</p><p>]bar</p>', + '<p>foo[</p><p>]bar<br>baz</p>', + 'foo[<p>]bar</p>', + 'foo{<p>}bar</p>', + 'foo[<p>]bar<br>baz</p>', + 'foo[<p>]bar</p>baz', + 'foo{<p>bar</p>}baz', + 'foo<p>{bar</p>}baz', + 'foo{<p>bar}</p>baz', + '<p>foo[</p>]bar', + '<p>foo{</p>}bar', + '<p>foo[</p>]bar<br>baz', + '<p>foo[</p>]bar<p>baz</p>', + 'foo[<div><p>]bar</div>', + '<div><p>foo[</p></div>]bar', + 'foo[<div><p>]bar</p>baz</div>', + 'foo[<div>]bar<p>baz</p></div>', + '<div><p>foo</p>bar[</div>]baz', + '<div>foo<p>bar[</p></div>]baz', + + '<p>foo<br>{</p>]bar', + '<p>foo<br><br>{</p>]bar', + 'foo<br>{<p>]bar</p>', + 'foo<br><br>{<p>]bar</p>', + '<p>foo<br>{</p><p>}bar</p>', + '<p>foo<br><br>{</p><p>}bar</p>', + + '<table><tbody><tr><th>foo<th>[bar]<th>baz<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>foo<th>ba[r<th>b]az<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>fo[o<th>bar<th>b]az<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>foo<th>bar<th>ba[z<tr><td>q]uz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>[foo<th>bar<th>baz]<tr><td>quz<td>qoz<td>qiz</table>', + '<table><tbody><tr><th>[foo<th>bar<th>baz<tr><td>quz<td>qoz<td>qiz]</table>', + '{<table><tbody><tr><th>foo<th>bar<th>baz<tr><td>quz<td>qoz<td>qiz</table>}', + '<table><tbody><tr><td>foo<td>ba[r<tr><td>baz<td>quz<tr><td>q]oz<td>qiz</table>', + '<p>fo[o<table><tr><td>b]ar</table><p>baz', + '<p>foo<table><tr><td>ba[r</table><p>b]az', + '<p>fo[o<table><tr><td>bar</table><p>b]az', + + '<p>foo<ol><li>ba[r<li>b]az</ol><p>quz', + '<p>foo<ol><li>bar<li>[baz]</ol><p>quz', + '<p>fo[o<ol><li>b]ar<li>baz</ol><p>quz', + '<p>foo<ol><li>bar<li>ba[z</ol><p>q]uz', + '<p>fo[o<ol><li>bar<li>b]az</ol><p>quz', + '<p>fo[o<ol><li>bar<li>baz</ol><p>q]uz', + + '<ol><li>fo[o</ol><ol><li>b]ar</ol>', + '<ol><li>fo[o</ol><ul><li>b]ar</ul>', + + 'foo[<ol><li>]bar</ol>', + '<ol><li>foo[<li>]bar</ol>', + 'foo[<dl><dt>]bar<dd>baz</dl>', + 'foo[<dl><dd>]bar</dl>', + '<dl><dt>foo[<dd>]bar</dl>', + '<dl><dt>foo[<dt>]bar<dd>baz</dl>', + '<dl><dt>foo<dd>bar[<dd>]baz</dl>', + + // https://bugs.webkit.org/show_bug.cgi?id=35281 + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=13976 + '<ol><li>foo</ol>{}<br><ol><li>bar</ol>', + '<ol><li>foo</ol><p>{}<br></p><ol><li>bar</ol>', + '<ol><li><p>foo</ol><p>{}<br></p><ol><li>bar</ol>', + '<ol id=a><li>foo</ol>{}<br><ol><li>bar</ol>', + '<ol><li>foo</ol>{}<br><ol id=b><li>bar</ol>', + '<ol id=a><li>foo</ol>{}<br><ol id=b><li>bar</ol>', + '<ol class=a><li>foo</ol>{}<br><ol class=b><li>bar</ol>', + '<ol><ol><li>foo</ol><li>{}<br><ol><li>bar</ol></ol>', + '<ol><ol><li>foo</ol><li>{}<br></li><ol><li>bar</ol></ol>', + '<ol><li>foo[</ol>bar]<ol><li>baz</ol>', + '<ol><li>foo[</ol><p>bar]<ol><li>baz</ol>', + '<ol><li><p>foo[</ol><p>bar]<ol><li>baz</ol>', + '<ol><li>fo[]o</ol><ol><li>bar</ol>', + '<ol><li>foo</ol>[bar<ol><li>]baz</ol>', + '<ol><li>foo</ol><p>[bar<ol><li>]baz</ol>', + '<ol><li>foo</ol><p>[bar<ol><li><p>]baz</ol>', + '<ol><li>foo</ol><ol><li>[]bar</ol>', + '<ol><ol><li>foo[</ol><li>bar</ol>baz]<ol><li>quz</ol>', + '<ul><li>foo</ul>{}<br><ul><li>bar</ul>', + '<ul><li>foo</ul><p>{}<br></p><ul><li>bar</ul>', + '<ol><li>foo[<li>bar]</ol><ol><li>baz</ol><ol><li>quz</ol>', + '<ol><li>foo</ol>{}<br><ul><li>bar</ul>', + '<ol><li>foo</ol><p>{}<br></p><ul><li>bar</ul>', + '<ul><li>foo</ul>{}<br><ol><li>bar</ol>', + '<ul><li>foo</ul><p>{}<br></p><ol><li>bar</ol>', + + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=13831 + '<p><b>[foo]</b>', + '<p><quasit>[foo]</quasit>', + '<p><b><i>[foo]</i></b>', + '<p><b>{foo}</b>', + '<p>{<b>foo</b>}', + '<p><b>[]f</b>', + '<b>[foo]</b>', + '<div><b>[foo]</b></div>', + ], + //@} + hilitecolor: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + '<p style="background-color: rgb(0, 255, 255)">foo[bar]baz</p>', + '<p style="background-color: #00ffff">foo[bar]baz</p>', + '<p style="background-color: aqua">foo[bar]baz</p>', + '{<p style="background-color: aqua">foo</p><p>bar</p>}', + '<span style="background-color: aqua">foo<span style="background-color: tan">[bar]</span>baz</span>', + '<span style="background-color: #00ffff">foo<span style="background-color: tan">[bar]</span>baz</span>', + '<span style="background-color: #0ff">foo<span style="background-color: tan">[bar]</span>baz</span>', + '<span style="background-color: rgb(0, 255, 255)">foo<span style="background-color: tan">[bar]</span>baz</span>', + '<span style="background-color: aqua">foo<span style="background-color: tan">b[ar]</span>baz</span>', + '<p style="background-color: aqua">foo<span style="background-color: tan">b[ar]</span>baz</p>', + '<div style="background-color: aqua"><p style="background-color: tan">b[ar]</p></div>', + '<span style="display: block; background-color: aqua"><span style="display: block; background-color: tan">b[ar]</span></span>', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<span style=background-color:tan>b]ar</span>baz', + 'foo<span style=background-color:tan>ba[r</span>b]az', + 'fo[o<span style=background-color:tan>bar</span>b]az', + 'foo[<span style=background-color:tan>b]ar</span>baz', + 'foo<span style=background-color:tan>ba[r</span>]baz', + 'foo[<span style=background-color:tan>bar</span>]baz', + 'foo<span style=background-color:tan>[bar]</span>baz', + 'foo{<span style=background-color:tan>bar</span>}baz', + '<span style=background-color:tan>fo[o</span><span style=background-color:yellow>b]ar</span>', + '<span style=background-color:tan>fo[o</span><span style=background-color:tan>b]ar</span>', + '<span style=background-color:tan>fo[o<span style=background-color:transparent>b]ar</span></span>', + + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=13829 + '!<font size=6>[foo]</font>', + '!<span style=font-size:xx-large>[foo]</span>', + '!<font size=6>foo[bar]baz</font>', + '!<span style=font-size:xx-large>foo[bar]baz</span>', + '![foo<font size=6>bar</font>baz]', + '![foo<span style=font-size:xx-large>bar</span>baz]', + ], + //@} + indent: [ + //@{ + // All these have a trailing unselected paragraph, because otherwise + // Gecko is unhappy: it throws exceptions in non-CSS mode, and in CSS + // mode it adds the indentation invisibly to the wrapper div in many + // cases. + 'foo[]bar<p>extra', + '<span>foo</span>{}<span>bar</span><p>extra', + '<span>foo[</span><span>]bar</span><p>extra', + 'foo[bar]baz<p>extra', + '<p dir=rtl>פו[בר]בז<p dir=rtl>נוםף', + '<p dir=rtl>פו[ברבז<p>Foobar]baz<p>Extra', + '<p>Foo[barbaz<p dir=rtl>פובר]בז<p>Extra', + '<div><p>Foo[barbaz<p dir=rtl>פובר]בז</div><p>Extra', + 'foo]bar[baz<p>extra', + '{<p><p> <p>foo</p>}<p>extra', + 'foo[bar<i>baz]qoz</i>quz<p>extra', + '[]foo<p>extra', + 'foo[]<p>extra', + '<p>[]foo<p>extra', + '<p>foo[]<p>extra', + '<p>{}<br>foo</p><p>extra', + '<p>foo<br>{}</p><p>extra', + '<span>{}<br>foo</span>bar<p>extra', + '<span>foo<br>{}</span>bar<p>extra', + '<p>foo</p>{}<p>bar</p>', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<p>foo[bar]</p><p>baz</p><p>extra', + '<p>[foobar</p><p>ba]z</p><p>extra', + 'foo[bar]<br>baz<p>extra', + 'foo[bar]<br><br><br><br>baz<p>extra', + 'foobar<br>[ba]z<p>extra', + 'foobar<br><br><br><br>[ba]z<p>extra', + 'foo[bar<br>ba]z<p>extra', + '<div>foo<p>[bar]</p>baz</div><p>extra', + + // These mimic existing indentation in various browsers, to see how + // they cope with indenting twice. This is spec, Gecko non-CSS, and + // Opera: + '<blockquote><p>foo[bar]</p><p>baz</p></blockquote><p>extra', + '<blockquote><p>foo[bar</p><p>b]az</p></blockquote><p>extra', + '<blockquote><p>foo[bar]</p></blockquote><p>baz</p><p>extra', + '<blockquote><p>foo[bar</p></blockquote><p>b]az</p><p>extra', + '<p>[foo]<blockquote><p>bar</blockquote><p>extra', + '<p>[foo<blockquote><p>b]ar</blockquote><p>extra', + '<p>foo<blockquote><p>bar</blockquote><p>[baz]<p>extra', + '<p>foo<blockquote><p>[bar</blockquote><p>baz]<p>extra', + '<p>[foo<blockquote><p>bar</blockquote><p>baz]<p>extra', + '<blockquote><p>foo</blockquote><p>[bar]<blockquote><p>baz</blockquote><p>extra', + + '<blockquote>foo[bar]<br>baz</blockquote><p>extra', + '<blockquote>foo[bar<br>b]az</blockquote><p>extra', + '<blockquote>foo[bar]</blockquote>baz<p>extra', + '<blockquote>foo[bar</blockquote>b]az<p>extra', + '[foo]<blockquote>bar</blockquote><p>extra', + '[foo<blockquote>b]ar</blockquote><p>extra', + 'foo<blockquote>bar</blockquote>[baz]<p>extra', + '[foo<blockquote>bar</blockquote>baz]<p>extra', + '<blockquote>foo</blockquote>[bar]<blockquote>baz</blockquote><p>extra', + + // IE: + '<blockquote style="margin-right: 0" dir="ltr"><p>foo[bar]</p><p>baz</p></blockquote><p>extra', + '<blockquote style="margin-right: 0" dir="ltr"><p>foo[bar</p><p>b]az</p></blockquote><p>extra', + '<blockquote style="margin-right: 0" dir="ltr"><p>foo[bar]</p></blockquote><p>baz</p><p>extra', + '<blockquote style="margin-right: 0" dir="ltr"><p>foo[bar</p></blockquote><p>b]az</p><p>extra', + '<p>[foo]<blockquote style="margin-right: 0" dir="ltr"><p>bar</blockquote><p>extra', + '<p>[foo<blockquote style="margin-right: 0" dir="ltr"><p>b]ar</blockquote><p>extra', + '<p>foo<blockquote style="margin-right: 0" dir="ltr"><p>bar</blockquote><p>[baz]<p>extra', + '<p>foo<blockquote style="margin-right: 0" dir="ltr"><p>[bar</blockquote><p>baz]<p>extra', + '<p>[foo<blockquote style="margin-right: 0" dir="ltr"><p>bar</blockquote><p>baz]<p>extra', + '<blockquote style="margin-right: 0" dir="ltr"><p>foo</blockquote><p>[bar]<blockquote style="margin-right: 0" dir="ltr"><p>baz</blockquote><p>extra', + + // Firefox CSS mode: + '<p style="margin-left: 40px">foo[bar]</p><p style="margin-left: 40px">baz</p><p>extra', + '<p style="margin-left: 40px">foo[bar</p><p style="margin-left: 40px">b]az</p><p>extra', + '<p style="margin-left: 40px">foo[bar]</p><p>baz</p><p>extra', + '<p style="margin-left: 40px">foo[bar</p><p>b]az</p><p>extra', + '<p>[foo]<p style="margin-left: 40px">bar<p>extra', + '<p>[foo<p style="margin-left: 40px">b]ar<p>extra', + '<p>foo<p style="margin-left: 40px">bar<p>[baz]<p>extra', + '<p>foo<p style="margin-left: 40px">[bar<p>baz]<p>extra', + '<p>[foo<p style="margin-left: 40px">bar<p>baz]<p>extra', + '<p style="margin-left: 40px">foo<p>[bar]<p style="margin-left: 40px">baz<p>extra', + + // WebKit: + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>foo[bar]</p><p>baz</p></blockquote><p>extra', + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>foo[bar</p><p>b]az</p></blockquote><p>extra', + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>foo[bar]</p></blockquote><p>baz</p><p>extra', + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>foo[bar</p></blockquote><p>b]az</p><p>extra', + '<p>[foo]<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>bar</blockquote><p>extra', + '<p>[foo<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>b]ar</blockquote><p>extra', + '<p>foo<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>bar</blockquote><p>[baz]<p>extra', + '<p>foo<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>[bar</blockquote><p>baz]<p>extra', + '<p>[foo<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>bar</blockquote><p>baz]<p>extra', + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>foo</blockquote><p>[bar]<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px"><p>baz</blockquote><p>extra', + + // MDC says "In Firefox, if the selection spans multiple lines at + // different levels of indentation, only the least indented lines in + // the selection will be indented." Let's test that. + '<blockquote>f[oo<blockquote>b]ar</blockquote></blockquote><p>extra', + + // Lists! + '<ol><li>foo<li>[bar]<li>baz</ol>', + '<ol data-start=1 data-end=2><li>foo<li>bar<li>baz</ol>', + '<ol><li>foo</ol>[bar]', + '<ol><li>[foo]<br>bar<li>baz</ol>', + '<ol><li>foo<br>[bar]<li>baz</ol>', + '<ol><li><div>[foo]</div>bar<li>baz</ol>', + '<ol><li>foo<ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo</li><ol data-start=0 data-end=1><li>bar<li>baz</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol data-start=1 data-end=2><li>bar<li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>b[a]r</ol><li>baz</ol>', + '<ol><li>foo</li><ol><li>b[a]r</ol><li>baz</ol>', + '<ol><li>foo{<ol><li>bar</ol>}<li>baz</ol>', + '<ol><li>foo</li>{<ol><li>bar</ol>}<li>baz</ol>', + '<ol><li>[foo]<ol><li>bar</ol><li>baz</ol>', + '<ol><li>[foo]</li><ol><li>bar</ol><li>baz</ol>', + '<ol><li>foo<li>[bar]<ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<li>[bar]</li><ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>baz</ol><li>[quz]</ol>', + '<ol><li>foo</li><ol><li>bar<li>baz</ol><li>[quz]</ol>', + + // Lists with id's: + // http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2009-July/020721.html + '<ol><ol id=u1><li id=i1>foo</ol><li id=i2>[bar]</li><ol id=u3><li id=i3>baz</ol></ol>', + '<ol><ol><li id=i1>foo</ol><li id=i2>[bar]</li><ol id=u3><li id=i3>baz</ol></ol>', + '<ol><ol id=u1><li id=i1>foo</ol><li id=i2>[bar]</li><ol><li id=i3>baz</ol></ol>', + '<ol><li id=i2>[bar]</li><ol id=u3><li id=i3>baz</ol></ol>', + '<ol><ol id=u1><li id=i1>foo</ol><li id=i2>[bar]</ol>', + + // Try indenting multiple items at once. + '<ol><li>foo<li>b[ar<li>baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol><li>baz</ol>', + '<ol><li>[foo</li><ol><li>bar]</ol><li>baz</ol>', + '<ol><li>foo<ol><li>b[ar</ol><li>b]az</ol>', + '<ol><li>foo</li><ol><li>b[ar</ol><li>b]az</ol>', + '<ol><li>[foo<ol><li>bar</ol><li>baz]</ol><p>extra', + '<ol><li>[foo</li><ol><li>bar</ol><li>baz]</ol><p>extra', + + // We probably can't actually get this DOM . . . + '<ol><li>[foo]<ol><li>bar</ol>baz</ol>', + '<ol><li>foo<ol><li>[bar]</ol>baz</ol>', + '<ol><li>foo<ol><li>bar</ol>[baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol>baz</ol>', + + 'foo<!--bar-->[baz]<p>extra', + '[foo]<!--bar-->baz<p>extra', + '<p>foo<!--bar-->{}<p>extra', + '<p>{}<!--foo-->bar<p>extra', + + // Whitespace nodes + '<blockquote><p>foo</blockquote> <p>[bar]', + '<p>[foo]</p> <blockquote><p>bar</blockquote>', + '<blockquote><p>foo</blockquote> <p>[bar]</p> <blockquote><p>baz</blockquote>', + '<ol><li>foo</li><ol><li>bar</li> </ol><li>[baz]</ol>', + '<ol><li>foo</li><ol><li>bar</li></ol> <li>[baz]</ol>', + '<ol><li>foo</li><ol><li>bar</li> </ol> <li>[baz]</ol>', + '<ol><li>foo<ol><li>bar</li> </ol></li><li>[baz]</ol>', + '<ol><li>foo<ol><li>bar</li></ol></li> <li>[baz]</ol>', + '<ol><li>foo<ol><li>bar</li> </ol></li> <li>[baz]</ol>', + '<ol><li>foo<li>[bar]</li> <ol><li>baz</ol></ol>', + '<ol><li>foo<li>[bar]</li><ol> <li>baz</ol></ol>', + '<ol><li>foo<li>[bar]</li> <ol> <li>baz</ol></ol>', + '<ol><li>foo<li>[bar] <ol><li>baz</ol></ol>', + '<ol><li>foo<li>[bar]<ol> <li>baz</ol></ol>', + '<ol><li>foo<li>[bar] <ol> <li>baz</ol></ol>', + + // https://bugs.webkit.org/show_bug.cgi?id=32003 + '<ul><li>a<br>{<br>}</li><li>b</li></ul>', + ], + //@} + inserthorizontalrule: [ + //@{ + 'foo[]bar', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + '<p>foo[bar<p>baz]quz', + '<div><b>foo</b>{}<b>bar</b></div>', + '<div><b>foo[</b><b>]bar</b></div>', + '<div><b>foo</b>{<b>bar</b>}<b>baz</b></div>', + '<b>foo[]bar</b>', + '<b id=abc>foo[]bar</b>', + ["abc", 'foo[bar]baz'], + 'foo[bar]baz', + + 'foo<b>[bar]</b>baz', + 'foo<b>{bar}</b>baz', + 'foo{<b>bar</b>}baz', + '<p>foo<p>[bar]<p>baz', + '<p>foo<p>{bar}<p>baz', + '<p>foo{<p>bar</p>}<p>baz', + + '<p>foo[bar]baz</p>', + '<p id=abc>foo[bar]baz</p>', + '<h1>foo[bar]baz</h1>', + '<p>foo<b>b[a]r</b>baz</p>', + + '<a>foo[bar]baz</a>', + '<a href=/>foo[bar]baz</a>', + '<abbr>foo[bar]baz</abbr>', + '<address>foo[bar]baz</address>', + '<article>foo[bar]baz</article>', + '<aside>foo[bar]baz</aside>', + '<b>foo[bar]baz</b>', + '<bdi>foo[bar]baz</bdi>', + '<bdo dir=rtl>foo[bar]baz</bdo>', + '<blockquote>foo[bar]baz</blockquote>', + '<table><caption>foo[bar]baz</caption><tr><td>quz</table>', + '<cite>foo[bar]baz</cite>', + '<code>foo[bar]baz</code>', + '<dl><dd>foo[bar]baz</dd></dl>', + '<del>foo[bar]baz</del>', + '<details>foo[bar]baz</details>', + '<dfn>foo[bar]baz</dfn>', + '<div>foo[bar]baz</div>', + '<dl><dt>foo[bar]baz</dt></dl>', + '<em>foo[bar]baz</em>', + '<figure><figcaption>foo[bar]baz</figcaption>quz</figure>', + '<figure>foo[bar]baz</figure>', + '<footer>foo[bar]baz</footer>', + '<h1>foo[bar]baz</h1>', + '<h2>foo[bar]baz</h2>', + '<h3>foo[bar]baz</h3>', + '<h4>foo[bar]baz</h4>', + '<h5>foo[bar]baz</h5>', + '<h6>foo[bar]baz</h6>', + '<header>foo[bar]baz</header>', + '<hgroup>foo[bar]baz</hgroup>', + '<hgroup><h1>foo[bar]baz</h1></hgroup>', + '<i>foo[bar]baz</i>', + '<ins>foo[bar]baz</ins>', + '<kbd>foo[bar]baz</kbd>', + '<mark>foo[bar]baz</mark>', + '<nav>foo[bar]baz</nav>', + '<ol><li>foo[bar]baz</li></ol>', + '<p>foo[bar]baz</p>', + '<pre>foo[bar]baz</pre>', + '<q>foo[bar]baz</q>', + '<ruby>foo[bar]baz<rt>quz</rt></ruby>', + '<ruby>foo<rt>bar[baz]quz</rt></ruby>', + '<ruby>foo<rp>bar[baz]quz</rp><rt>qoz</rt><rp>qiz</rp></ruby>', + '<s>foo[bar]baz</s>', + '<samp>foo[bar]baz</samp>', + '<section>foo[bar]baz</section>', + '<small>foo[bar]baz</small>', + '<span>foo[bar]baz</span>', + '<strong>foo[bar]baz</strong>', + '<sub>foo[bar]baz</sub>', + '<sup>foo[bar]baz</sup>', + '<table><tr><td>foo[bar]baz</td></table>', + '<table><tr><th>foo[bar]baz</th></table>', + '<u>foo[bar]baz</u>', + '<ul><li>foo[bar]baz</li></ul>', + '<var>foo[bar]baz</var>', + + '<acronym>foo[bar]baz</acronym>', + '<big>foo[bar]baz</big>', + '<blink>foo[bar]baz</blink>', + '<center>foo[bar]baz</center>', + '<dir>foo[bar]baz</dir>', + '<dir><li>foo[bar]baz</li></dir>', + '<font>foo[bar]baz</font>', + '<listing>foo[bar]baz</listing>', + '<marquee>foo[bar]baz</marquee>', + '<nobr>foo[bar]baz</nobr>', + '<strike>foo[bar]baz</strike>', + '<tt>foo[bar]baz</tt>', + '<xmp>foo[bar]baz</xmp>', + + '<quasit>foo[bar]baz</quasit>', + + '<table><tr><td>fo[o<td>b]ar</table>', + 'fo[o<span contenteditable=false>bar</span>b]az', + ], + //@} + inserthtml: [ + //@{ + 'foo[]bar', + 'foo[bar]baz', + 'foo<span style=color:#aBcDeF>[bar]</span>baz', + 'foo<span style=color:#aBcDeF>{bar}</span>baz', + 'foo{<span style=color:#aBcDeF>bar</span>}baz', + '[foo<span style=color:#aBcDeF>bar]</span>baz', + '{foo<span style=color:#aBcDeF>bar}</span>baz', + 'foo<span style=color:#aBcDeF>[bar</span>baz]', + 'foo<span style=color:#aBcDeF>{bar</span>baz}', + 'foo<span style=color:#aBcDeF>[bar</span><span style=color:#fEdCbA>baz]</span>quz', + + ['', 'foo[bar]baz'], + ['\0', 'foo[bar]baz'], + ['\x07', 'foo[bar]baz'], + // The following line makes Firefox 7.0a2 go into an infinite loop on + // my machine. + //['\ud800', 'foo[bar]baz'], + + ['<b>', 'foo[bar]baz'], + ['<b>abc', 'foo[bar]baz'], + ['<p>abc', '<p>foo[bar]baz'], + ['<li>abc', '<p>foo[bar]baz'], + ['<p>abc', '<ol>{<li>foo</li>}<li>bar</ol>'], + ['<p>abc', '<ol><li>foo</li>{<li>bar</li>}<li>baz</ol>'], + ['<p>abc', '<ol><li>[foo]</li><li>bar</ol>'], + + ['abc', '<xmp>f[o]o</xmp>'], + ['<b>abc</b>', '<xmp>f[o]o</xmp>'], + ['abc', '<script>f[o]o</script>bar'], + ['<b>abc</b>', '<script>f[o]o</script>bar'], + + ['<a>abc</a>', '<a>f[o]o</a>'], + ['<a href=/>abc</a>', '<a href=.>f[o]o</a>'], + ['<hr>', '<p>f[o]o'], + ['<hr>', '<b>f[o]o</b>'], + ['<h2>abc</h2>', '<h1>f[o]o</h1>'], + ['<td>abc</td>', '<table><tr><td>f[o]o</table>'], + ['<td>abc</td>', 'f[o]o'], + + ['<dt>abc</dt>', '<dl><dt>f[o]o<dd>bar</dl>'], + ['<dt>abc</dt>', '<dl><dt>foo<dd>b[a]r</dl>'], + ['<dd>abc</dd>', '<dl><dt>f[o]o<dd>bar</dl>'], + ['<dd>abc</dd>', '<dl><dt>foo<dd>b[a]r</dl>'], + ['<dt>abc</dt>', 'f[o]o'], + ['<dt>abc</dt>', '<ol><li>f[o]o</ol>'], + ['<dd>abc</dd>', 'f[o]o'], + ['<dd>abc</dd>', '<ol><li>f[o]o</ol>'], + + ['<li>abc</li>', '<dir><li>f[o]o</dir>'], + ['<li>abc</li>', '<ol><li>f[o]o</ol>'], + ['<li>abc</li>', '<ul><li>f[o]o</ul>'], + ['<dir><li>abc</dir>', '<dir><li>f[o]o</dir>'], + ['<dir><li>abc</dir>', '<ol><li>f[o]o</ol>'], + ['<dir><li>abc</dir>', '<ul><li>f[o]o</ul>'], + ['<ol><li>abc</ol>', '<dir><li>f[o]o</dir>'], + ['<ol><li>abc</ol>', '<ol><li>f[o]o</ol>'], + ['<ol><li>abc</ol>', '<ul><li>f[o]o</ul>'], + ['<ul><li>abc</ul>', '<dir><li>f[o]o</dir>'], + ['<ul><li>abc</ul>', '<ol><li>f[o]o</ol>'], + ['<ul><li>abc</ul>', '<ul><li>f[o]o</ul>'], + ['<li>abc</li>', 'f[o]o'], + + ['<nobr>abc</nobr>', '<nobr>f[o]o</nobr>'], + ['<nobr>abc</nobr>', 'f[o]o'], + + ['<p>abc', '<font color=blue>foo[]bar</font>'], + ['<p>abc', '<span style=color:blue>foo[]bar</span>'], + ['<p>abc', '<span style=font-variant:small-caps>foo[]bar</span>'], + [' ', '<p>[foo]</p>'], + ['<span style=display:none></span>', '<p>[foo]</p>'], + ['<!--abc-->', '<p>[foo]</p>'], + + ['abc', '<p>{}<br></p>'], + ['<!--abc-->', '<p>{}<br></p>'], + ['abc', '<p><!--foo-->{}<span><br></span><!--bar--></p>'], + ['<!--abc-->', '<p><!--foo-->{}<span><br></span><!--bar--></p>'], + ['abc', '<p>{}<span><!--foo--><br><!--bar--></span></p>'], + ['<!--abc-->', '<p>{}<span><!--foo--><br><!--bar--></span></p>'], + + ['abc', '<p><br>{}</p>'], + ['<!--abc-->', '<p><br>{}</p>'], + ['abc', '<p><!--foo--><span><br></span>{}<!--bar--></p>'], + ['<!--abc-->', '<p><!--foo--><span><br></span>{}<!--bar--></p>'], + ['abc', '<p><span><!--foo--><br><!--bar--></span>{}</p>'], + ['<!--abc-->', '<p><span><!--foo--><br><!--bar--></span>{}</p>'], + ], + //@} + insertimage: [ + //@{ + 'foo[]bar', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + ["", 'foo[bar]baz'], + 'foo[bar]baz', + 'foo<span style=color:#aBcDeF>[bar]</span>baz', + 'foo<span style=color:#aBcDeF>{bar}</span>baz', + 'foo{<span style=color:#aBcDeF>bar</span>}baz', + '[foo<span style=color:#aBcDeF>bar]</span>baz', + '{foo<span style=color:#aBcDeF>bar}</span>baz', + 'foo<span style=color:#aBcDeF>[bar</span>baz]', + 'foo<span style=color:#aBcDeF>{bar</span>baz}', + 'foo<span style=color:#aBcDeF>[bar</span><span style=color:#fEdCbA>baz]</span>quz', + + 'foo<b>[bar]</b>baz', + 'foo<b>{bar}</b>baz', + 'foo{<b>bar</b>}baz', + 'foo<span>[bar]</span>baz', + 'foo<span>{bar}</span>baz', + 'foo{<span>bar</span>}baz', + '<b>foo[bar</b><i>baz]quz</i>', + '<p>foo</p><p>[bar]</p><p>baz</p>', + '<p>foo</p><p>{bar}</p><p>baz</p>', + '<p>foo</p>{<p>bar</p>}<p>baz</p>', + + '<p>foo[bar<p>baz]quz', + '<p>foo[bar<div>baz]quz</div>', + '<p>foo[bar<h1>baz]quz</h1>', + '<div>foo[bar</div><p>baz]quz', + '<blockquote>foo[bar</blockquote><pre>baz]quz</pre>', + + '<p><b>foo[bar</b><p>baz]quz', + '<div><p>foo[bar</div><p>baz]quz', + '<p>foo[bar<blockquote><p>baz]quz<p>qoz</blockquote', + '<p>foo[bar<p style=color:blue>baz]quz', + '<p>foo[bar<p><b>baz]quz</b>', + + '<div><p>foo<p>[bar<p>baz]</div>', + + 'foo[<br>]bar', + '<p>foo[</p><p>]bar</p>', + '<p>foo[</p><p>]bar<br>baz</p>', + 'foo[<p>]bar</p>', + 'foo[<p>]bar<br>baz</p>', + 'foo[<p>]bar</p>baz', + '<p>foo[</p>]bar', + '<p>foo[</p>]bar<br>baz', + '<p>foo[</p>]bar<p>baz</p>', + 'foo[<div><p>]bar</div>', + '<div><p>foo[</p></div>]bar', + 'foo[<div><p>]bar</p>baz</div>', + 'foo[<div>]bar<p>baz</p></div>', + '<div><p>foo</p>bar[</div>]baz', + '<div>foo<p>bar[</p></div>]baz', + ], + //@} + insertlinebreak: [ + //@{ Same as insertparagraph (set below) + ], + //@} + insertorderedlist: [ + //@{ + 'foo[]bar', + 'foo[bar]baz', + 'foo<br>[bar]', + 'f[oo<br>b]ar<br>baz', + '<p>[foo]<br>bar</p>', + '[foo<ol><li>bar]</ol>baz', + 'foo<ol><li>[bar</ol>baz]', + '[foo<ul><li>bar]</ul>baz', + 'foo<ul><li>[bar</ul>baz]', + 'foo<ul><li>[bar</ul><ol><li>baz]</ol>quz', + 'foo<ol><li>[bar</ol><ul><li>baz]</ul>quz', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr><td>fo[o<td>b]ar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + '<p>foo<p>[bar]<p>baz', + '<p>foo<blockquote>[bar]</blockquote><p>baz', + '<dl><dt>foo<dd>[bar]<dt>baz<dd>quz</dl>', + '<dl><dt>foo<dd>bar<dt>[baz]<dd>quz</dl>', + + '<p>[foo<p>bar]<p>baz', + '<p>[foo<blockquote>bar]</blockquote><p>baz', + '<dl><dt>[foo<dd>bar]<dt>baz<dd>quz</dl>', + '<dl><dt>foo<dd>[bar<dt>baz]<dd>quz</dl>', + + '<p>[foo<blockquote><p>bar]<p>baz</blockquote>', + + + // Various <ol> stuff + '<ol><li>foo<li>[bar]<li>baz</ol>', + '<ol><li>foo</ol>[bar]', + '[foo]<ol><li>bar</ol>', + '<ol><li>foo</ol>[bar]<ol><li>baz</ol>', + '<ol><ol><li>[foo]</ol></ol>', + '<ol><li>[foo]<br>bar<li>baz</ol>', + '<ol><li>foo<br>[bar]<li>baz</ol>', + '<ol><li><div>[foo]</div>bar<li>baz</ol>', + '<ol><li>foo<ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>[foo]<ol><li>bar</ol><li>baz</ol>', + '<ol><li>[foo]</li><ol><li>bar</ol><li>baz</ol>', + '<ol><li>foo<li>[bar]<ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<li>[bar]</li><ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>baz</ol><li>[quz]</ol>', + '<ol><li>foo</li><ol><li>bar<li>baz</ol><li>[quz]</ol>', + + // Multiple items at once. + '<ol><li>foo<li>[bar<li>baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol><li>baz</ol>', + '<ol><li>foo<ol><li>b[ar</ol><li>b]az</ol>', + '<ol><li>[foo<ol><li>bar</ol><li>baz]</ol><p>extra', + + // We probably can't actually get this DOM . . . + '<ol><li>[foo]<ol><li>bar</ol>baz</ol>', + '<ol><li>foo<ol><li>[bar]</ol>baz</ol>', + '<ol><li>foo<ol><li>bar</ol>[baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol>baz</ol>', + + + // Same stuff but with <ul> + '<ul><li>foo<li>[bar]<li>baz</ul>', + '<ul><li>foo</ul>[bar]', + '[foo]<ul><li>bar</ul>', + '<ul><li>foo</ul>[bar]<ul><li>baz</ul>', + '<ul><ul><li>[foo]</ul></ul>', + '<ul><li>[foo]<br>bar<li>baz</ul>', + '<ul><li>foo<br>[bar]<li>baz</ul>', + '<ul><li><div>[foo]</div>bar<li>baz</ul>', + '<ul><li>foo<ul><li>[bar]<li>baz</ul><li>quz</ul>', + '<ul><li>foo<ul><li>bar<li>[baz]</ul><li>quz</ul>', + '<ul><li>foo</li><ul><li>[bar]<li>baz</ul><li>quz</ul>', + '<ul><li>foo</li><ul><li>bar<li>[baz]</ul><li>quz</ul>', + '<ul><li>[foo]<ul><li>bar</ul><li>baz</ul>', + '<ul><li>[foo]</li><ul><li>bar</ul><li>baz</ul>', + '<ul><li>foo<li>[bar]<ul><li>baz</ul><li>quz</ul>', + '<ul><li>foo<li>[bar]</li><ul><li>baz</ul><li>quz</ul>', + '<ul><li>foo<ul><li>bar<li>baz</ul><li>[quz]</ul>', + '<ul><li>foo</li><ul><li>bar<li>baz</ul><li>[quz]</ul>', + + // Multiple items at once. + '<ul><li>foo<li>[bar<li>baz]</ul>', + '<ul><li>[foo<ul><li>bar]</ul><li>baz</ul>', + '<ul><li>foo<ul><li>b[ar</ul><li>b]az</ul>', + '<ul><li>[foo<ul><li>bar</ul><li>baz]</ul><p>extra', + + // We probably can't actually get this DOM . . . + '<ul><li>[foo]<ul><li>bar</ul>baz</ul>', + '<ul><li>foo<ul><li>[bar]</ul>baz</ul>', + '<ul><li>foo<ul><li>bar</ul>[baz]</ul>', + '<ul><li>[foo<ul><li>bar]</ul>baz</ul>', + + + // Mix of <ol> and <ul> + 'foo<ol><li>bar</ol><ul><li>[baz]</ul>quz', + 'foo<ol><li>bar</ol><ul><li>[baz</ul>quz]', + 'foo<ul><li>[bar]</ul><ol><li>baz</ol>quz', + '[foo<ul><li>bar]</ul><ol><li>baz</ol>quz', + + // Interaction with indentation + '[foo]<blockquote>bar</blockquote>baz', + 'foo<blockquote>[bar]</blockquote>baz', + '[foo<blockquote>bar]</blockquote>baz', + '<ol><li>foo</ol><blockquote>[bar]</blockquote>baz', + '[foo]<blockquote><ol><li>bar</ol></blockquote>baz', + 'foo<blockquote>[bar]<br>baz</blockquote>', + '[foo<blockquote>bar]<br>baz</blockquote>', + '<ol><li>foo</ol><blockquote>[bar]<br>baz</blockquote>', + + '<p>[foo]<blockquote><p>bar</blockquote><p>baz', + '<p>foo<blockquote><p>[bar]</blockquote><p>baz', + '<p>[foo<blockquote><p>bar]</blockquote><p>baz', + '<ol><li>foo</ol><blockquote><p>[bar]</blockquote><p>baz', + + // Attributes + '<ul id=abc><li>foo<li>[bar]<li>baz</ul>', + '<ul style=color:blue><li>foo<li>[bar]<li>baz</ul>', + '<ul style=text-indent:1em><li>foo<li>[bar]<li>baz</ul>', + '<ul id=abc><li>[foo]<li>bar<li>baz</ul>', + '<ul style=color:blue><li>[foo]<li>bar<li>baz</ul>', + '<ul style=text-indent:1em><li>[foo]<li>bar<li>baz</ul>', + '<ul id=abc><li>foo<li>bar<li>[baz]</ul>', + '<ul style=color:blue><li>foo<li>bar<li>[baz]</ul>', + '<ul style=text-indent:1em><li>foo<li>bar<li>[baz]</ul>', + + // Whitespace nodes + '<ol><li>foo</ol> <p>[bar]', + '<p>[foo]</p> <ol><li>bar</ol>', + '<ol><li>foo</ol> <p>[bar]</p> <ol><li>baz</ol>', + + // This caused an infinite loop at one point due to a bug in "fix + // disallowed ancestors". Disabled because I'm not sure how we want it + // to behave: + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=14578 + '!<span contenteditable=true>foo[]</span>', + ], + //@} + insertparagraph: [ + //@{ + 'foo[bar]baz', + 'fo[o<table><tr><td>b]ar</table>', + '<table><tr><td>[foo<td>bar]<tr><td>baz<td>quz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<tr><td>baz<td>quz</table>', + '<table><tr><td>fo[o</table>b]ar', + '<table><tr><td>fo[o<td>b]ar<td>baz</table>', + '{<table><tr><td>foo</table>}', + '<table><tr><td>[foo]</table>', + '<ol><li>[foo]<li>bar</ol>', + '<ol><li>f[o]o<li>bar</ol>', + + '[]foo', + 'foo[]', + '<span>foo[]</span>', + 'foo[]<br>', + 'foo[]bar', + '<address>[]foo</address>', + '<address>foo[]</address>', + '<address>foo[]<br></address>', + '<address>foo[]bar</address>', + '<div>[]foo</div>', + '<div>foo[]</div>', + '<div>foo[]<br></div>', + '<div>foo[]bar</div>', + '<dl><dt>[]foo<dd>bar</dl>', + '<dl><dt>foo[]<dd>bar</dl>', + '<dl><dt>foo[]<br><dd>bar</dl>', + '<dl><dt>foo[]bar<dd>baz</dl>', + '<dl><dt>foo<dd>[]bar</dl>', + '<dl><dt>foo<dd>bar[]</dl>', + '<dl><dt>foo<dd>bar[]<br></dl>', + '<dl><dt>foo<dd>bar[]baz</dl>', + '<h1>[]foo</h1>', + '<h1>foo[]</h1>', + '<h1>foo[]<br></h1>', + '<h1>foo[]bar</h1>', + '<ol><li>[]foo</ol>', + '<ol><li>foo[]</ol>', + '<ol><li>foo[]<br></ol>', + '<ol><li>foo[]bar</ol>', + '<p>[]foo</p>', + '<p>foo[]</p>', + '<p>foo[]<br></p>', + '<p>foo[]bar</p>', + '<pre>[]foo</pre>', + '<pre>foo[]</pre>', + '<pre>foo[]<br></pre>', + '<pre>foo[]bar</pre>', + + '<pre>foo[]<br><br></pre>', + '<pre>foo<br>{}<br></pre>', + '<pre>foo []</pre>', + '<pre>foo[] </pre>', + '<pre>foo [] </pre>', + + '<xmp>foo[]bar</xmp>', + '<script>foo[]bar</script>baz', + '<div style=display:none>foo[]bar</div>baz', + '<listing>foo[]bar</listing>', + + '<ol><li>{}<br></li></ol>', + 'foo<ol><li>{}<br></li></ol>', + '<ol><li>{}<br></li></ol>foo', + '<ol><li>foo<li>{}<br></ol>', + '<ol><li>{}<br><li>bar</ol>', + '<ol><li>foo</li><ul><li>{}<br></ul></ol>', + + '<dl><dt>{}<br></dt></dl>', + '<dl><dt>foo<dd>{}<br></dl>', + '<dl><dt>{}<br><dd>bar</dl>', + '<dl><dt>foo<dd>bar<dl><dt>{}<br><dd>baz</dl></dl>', + '<dl><dt>foo<dd>bar<dl><dt>baz<dd>{}<br></dl></dl>', + + '<h1>foo[bar</h1><p>baz]quz</p>', + '<p>foo[bar</p><h1>baz]quz</h1>', + '<p>foo</p>{}<br>', + '{}<br><p>foo</p>', + '<p>foo</p>{}<br><h1>bar</h1>', + '<h1>foo</h1>{}<br><p>bar</p>', + '<h1>foo</h1>{}<br><h2>bar</h2>', + '<p>foo</p><h1>[bar]</h1><p>baz</p>', + '<p>foo</p>{<h1>bar</h1>}<p>baz</p>', + + '<table><tr><td>foo[]bar</table>', + '<table><tr><td><p>foo[]bar</table>', + + '<blockquote>[]foo</blockquote>', + '<blockquote>foo[]</blockquote>', + '<blockquote>foo[]<br></blockquote>', + '<blockquote>foo[]bar</blockquote>', + '<blockquote><p>[]foo</blockquote>', + '<blockquote><p>foo[]</blockquote>', + '<blockquote><p>foo[]bar</blockquote>', + '<blockquote><p>foo[]<p>bar</blockquote>', + '<blockquote><p>foo[]bar<p>baz</blockquote>', + + '<span>foo[]bar</span>', + '<span>foo[]bar</span>baz', + '<b>foo[]bar</b>', + '<b>foo[]bar</b>baz', + '<b>foo[]</b>bar', + 'foo<b>[]bar</b>', + '<b>foo[]</b><i>bar</i>', + '<b id=x class=y>foo[]bar</b>', + '<i><b>foo[]bar</b>baz</i>', + + '<p><b>foo[]bar</b></p>', + '<p><b>[]foo</b></p>', + '<p><b id=x class=y>foo[]bar</b></p>', + '<div><b>foo[]bar</b></div>', + + '<a href=foo>foo[]bar</a>', + '<a href=foo>foo[]bar</a>baz', + '<a href=foo>foo[]</a>bar', + 'foo<a href=foo>[]bar</a>', + + '<p>foo[]<!--bar-->', + '<p><!--foo-->[]bar', + + '<p>foo<span style=color:#aBcDeF>[bar]</span>baz', + '<p>foo<span style=color:#aBcDeF>{bar}</span>baz', + '<p>foo{<span style=color:#aBcDeF>bar</span>}baz', + '<p>[foo<span style=color:#aBcDeF>bar]</span>baz', + '<p>{foo<span style=color:#aBcDeF>bar}</span>baz', + '<p>foo<span style=color:#aBcDeF>[bar</span>baz]', + '<p>foo<span style=color:#aBcDeF>{bar</span>baz}', + '<p>foo<span style=color:#aBcDeF>[bar</span><span style=color:#fEdCbA>baz]</span>quz', + + // https://bugs.webkit.org/show_bug.cgi?id=5036 + '<ul contenteditable><li>{}<br></ul>', + '<ul contenteditable><li>foo[]</ul>', + '<div contenteditable=false><ul contenteditable><li>{}<br></ul></div>', + '<div contenteditable=false><ul contenteditable><li>foo[]</ul></div>', + + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=13841 + // https://bugs.webkit.org/show_bug.cgi?id=23507 + '<address><p>foo[]</address>', + '<dl><dt><p>foo[]</dl>', + '<dl><dd><p>foo[]</dl>', + '<ol><li><p>foo[]</ol>', + '<ul><li><p>foo[]</ul>', + '<address><div>foo[]</address>', + '<dl><dt><div>foo[]</dl>', + '<dl><dd><div>foo[]</dl>', + '<ol><li><div>foo[]</ol>', + '<ul><li><div>foo[]</ul>', + '<div><p>foo[]</div>', + '<div><div>foo[]</div>', + + '<address><p>[]foo</address>', + '<dl><dt><p>[]foo</dl>', + '<dl><dd><p>[]foo</dl>', + '<ol><li><p>[]foo</ol>', + '<ul><li><p>[]foo</ul>', + '<address><div>[]foo</address>', + '<dl><dt><div>[]foo</dl>', + '<dl><dd><div>[]foo</dl>', + '<ol><li><div>[]foo</ol>', + '<ul><li><div>[]foo</ul>', + '<div><p>[]foo</div>', + '<div><div>[]foo</div>', + + '<address><p>foo[]bar</address>', + '<dl><dt><p>foo[]bar</dl>', + '<dl><dd><p>foo[]bar</dl>', + '<ol><li><p>foo[]bar</ol>', + '<ul><li><p>foo[]bar</ul>', + '<address><div>foo[]bar</address>', + '<dl><dt><div>foo[]bar</dl>', + '<dl><dd><div>foo[]bar</dl>', + '<ol><li><div>foo[]bar</ol>', + '<ul><li><div>foo[]bar</ul>', + '<div><p>foo[]bar</div>', + '<div><div>foo[]bar</div>', + + '<ol><li class=a id=x><p class=b id=y>foo[]</ol>', + '<div class=a id=x><div class=b id=y>foo[]</div></div>', + '<div class=a id=x><p class=b id=y>foo[]</div>', + '<ol><li class=a id=x><p class=b id=y>[]foo</ol>', + '<div class=a id=x><div class=b id=y>[]foo</div></div>', + '<div class=a id=x><p class=b id=y>[]foo</div>', + '<ol><li class=a id=x><p class=b id=y>foo[]bar</ol>', + '<div class=a id=x><div class=b id=y>foo[]bar</div></div>', + '<div class=a id=x><p class=b id=y>foo[]bar</div>', + ], + //@} + inserttext: [ + //@{ + 'foo[bar]baz', + ['', 'foo[bar]baz'], + + ['\t', 'foo[]bar'], + ['&', 'foo[]bar'], + ['\n', 'foo[]bar'], + ['abc\ndef', 'foo[]bar'], + ['\x07', 'foo[]bar'], + + ['<b>hi</b>', 'foo[]bar'], + ['<', 'foo[]bar'], + ['&', 'foo[]bar'], + + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=14254 + ['!\r', 'foo[]bar'], + ['!\r\n', 'foo[]bar'], + ['!\0', 'foo[]bar'], + ['!\ud800', 'foo[]bar'], + + // Whitespace tests! The following two bugs are relevant to some of + // these: + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=14119 + // https://bugzilla.mozilla.org/show_bug.cgi?id=681626 + [' ', 'foo[]bar'], + [' ', 'foo []bar'], + [' ', 'foo[] bar'], + [' ', 'foo []bar'], + [' ', 'foo [] bar'], + [' ', 'foo[] bar'], + [' ', 'foo []bar'], + [' ', 'foo [] bar'], + [' ', 'foo[] bar'], + [' ', 'foo []bar'], + [' ', 'foo [] bar'], + [' ', 'foo[] bar'], + [' ', 'foo [] bar'], + [' ', 'foo []bar'], + [' ', 'foo [] bar'], + + [' ', '[]foo'], + [' ', '{}foo'], + [' ', 'foo[]'], + [' ', 'foo{}'], + [' ', 'foo []'], + [' ', 'foo {}'], + [' ', 'foo []'], + [' ', 'foo {}'], + [' ', '<b>foo[]</b>bar'], + [' ', 'foo[]<b>bar</b>'], + + [' ', 'foo[] '], + [' ', ' foo [] '], + [' ', 'foo[]<span> </span>'], + [' ', 'foo[]<span> </span> '], + [' ', ' []foo'], + [' ', ' [] foo '], + [' ', '<span> </span>[]foo'], + [' ', ' <span> </span>[]foo'], + + [' ', '{}<br>'], + [' ', '<p>{}<br>'], + + [' ', '<p>foo[]<p>bar'], + [' ', '<p>foo []<p>bar'], + [' ', '<p>foo[]<p> bar'], + + // Some of the same tests as above, repeated with various values of + // white-space. + [' ', '<pre>foo[]bar</pre>'], + [' ', '<pre>foo []bar</pre>'], + [' ', '<pre>foo[] bar</pre>'], + [' ', '<pre>foo []bar</pre>'], + [' ', '<pre>[]foo</pre>'], + [' ', '<pre>foo[]</pre>'], + [' ', '<pre>foo []</pre>'], + [' ', '<pre> foo [] </pre>'], + + [' ', '<div style=white-space:pre>foo[]bar</div>'], + [' ', '<div style=white-space:pre>foo []bar</div>'], + [' ', '<div style=white-space:pre>foo[] bar</div>'], + [' ', '<div style=white-space:pre>foo []bar</div>'], + [' ', '<div style=white-space:pre>[]foo</div>'], + [' ', '<div style=white-space:pre>foo[]</div>'], + [' ', '<div style=white-space:pre>foo []</div>'], + [' ', '<div style=white-space:pre> foo [] </div>'], + + [' ', '<div style=white-space:pre-wrap>foo[]bar</div>'], + [' ', '<div style=white-space:pre-wrap>foo []bar</div>'], + [' ', '<div style=white-space:pre-wrap>foo[] bar</div>'], + [' ', '<div style=white-space:pre-wrap>foo []bar</div>'], + [' ', '<div style=white-space:pre-wrap>[]foo</div>'], + [' ', '<div style=white-space:pre-wrap>foo[]</div>'], + [' ', '<div style=white-space:pre-wrap>foo []</div>'], + [' ', '<div style=white-space:pre-wrap> foo [] </div>'], + + [' ', '<div style=white-space:pre-line>foo[]bar</div>'], + [' ', '<div style=white-space:pre-line>foo []bar</div>'], + [' ', '<div style=white-space:pre-line>foo[] bar</div>'], + [' ', '<div style=white-space:pre-line>foo []bar</div>'], + [' ', '<div style=white-space:pre-line>[]foo</div>'], + [' ', '<div style=white-space:pre-line>foo[]</div>'], + [' ', '<div style=white-space:pre-line>foo []</div>'], + [' ', '<div style=white-space:pre-line> foo [] </div>'], + + [' ', '<div style=white-space:nowrap>foo[]bar</div>'], + [' ', '<div style=white-space:nowrap>foo []bar</div>'], + [' ', '<div style=white-space:nowrap>foo[] bar</div>'], + [' ', '<div style=white-space:nowrap>foo []bar</div>'], + [' ', '<div style=white-space:nowrap>[]foo</div>'], + [' ', '<div style=white-space:nowrap>foo[]</div>'], + [' ', '<div style=white-space:nowrap>foo []</div>'], + [' ', '<div style=white-space:nowrap> foo [] </div>'], + + // End whitespace tests + + // Autolinking tests + [' ', 'http://a[]'], + [' ', 'ftp://a[]'], + [' ', 'quasit://a[]'], + [' ', '.x-++-.://a[]'], + [' ', '(http://a)[]'], + [' ', '<http://a>[]'], + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=14744 + ['! ', '[http://a][]'], + ['! ', '{http://a}[]'], + [' ', 'http://a![]'], + [' ', '!"#$%&\'()*+,-./:;<=>?\^_`|~http://a!"#$%&\'()*+,-./:;<=>?\^_`|~[]'], + [' ', 'http://a!"\'(),-.:;<>`[]'], + [' ', 'http://a#$%&*+/=?\^_|~[]'], + [' ', 'mailto:a[]'], + [' ', 'a@b[]'], + [' ', 'a@[]'], + [' ', '@b[]'], + [' ', '#@x[]'], + [' ', 'a@.[]'], + [' ', '!"#$%&\'()*+,-./:;<=>?\^_`|~a@b!"#$%&\'()*+,-./:;<=>?\^_`|~[]'], + [' ', '<b>a@b</b>{}'], + [' ', '<b>a</b><i>@</i><u>b</u>{}'], + [' ', 'a@b<b>[]c</b>'], + [' ', '<p>a@b</p><p>[]c</p>'], + ['a', 'http://a[]'], + ['\t', 'http://a[]'], + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=14254 + ['!\r', 'http://a[]'], + // http://www.w3.org/Bugs/Public/show_bug.cgi?id=14745 + ['!\n', 'http://a[]'], + ['\f', 'http://a[]'], + ['\u00A0', 'http://a[]'], + + [' ', 'foo[]'], + + 'foo[]bar', + 'foo []', + 'foo\xa0[]', + '<p>foo[]', + '<p>foo</p>{}', + '<p>[]foo', + '<p>{}foo', + '{}<p>foo', + '<p>foo</p>{}<p>bar</p>', + '<b>foo[]</b>bar', + '<b>foo</b>[]bar', + 'foo<b>{}</b>bar', + '<a>foo[]</a>bar', + '<a>foo</a>[]bar', + '<a href=/>foo[]</a>bar', + '<a href=/>foo</a>[]bar', + '<p>fo[o<p>b]ar', + '<p>fo[o<p>bar<p>b]az', + '{}<br>', + '<p>{}<br>', + '<p><span>{}<br></span>', + '<p>foo<span style=color:#aBcDeF>[bar]</span>baz', + '<p>foo<span style=color:#aBcDeF>{bar}</span>baz', + '<p>foo{<span style=color:#aBcDeF>bar</span>}baz', + '<p>[foo<span style=color:#aBcDeF>bar]</span>baz', + '<p>{foo<span style=color:#aBcDeF>bar}</span>baz', + '<p>foo<span style=color:#aBcDeF>[bar</span>baz]', + '<p>foo<span style=color:#aBcDeF>{bar</span>baz}', + '<p>foo<span style=color:#aBcDeF>[bar</span><span style=color:#fEdCbA>baz]</span>quz', + + + // These are like the corresponding tests in the multitest section, but + // because the selection isn't collapsed, we don't need to do + // multitests to set overrides. + 'foo<b>[bar]</b>baz', + 'foo<i>[bar]</i>baz', + 'foo<s>[bar]</s>baz', + 'foo<sub>[bar]</sub>baz', + 'foo<sup>[bar]</sup>baz', + 'foo<u>[bar]</u>baz', + 'foo<a href=http://www.google.com>[bar]</a>baz', + 'foo<font face=sans-serif>[bar]</font>baz', + 'foo<font size=4>[bar]</font>baz', + 'foo<font color=#0000FF>[bar]</font>baz', + 'foo<span style=background-color:#00FFFF>[bar]</span>baz', + 'foo<a href=http://www.google.com><font color=blue>[bar]</font></a>baz', + 'foo<font color=blue><a href=http://www.google.com>[bar]</a></font>baz', + 'foo<a href=http://www.google.com><font color=brown>[bar]</font></a>baz', + 'foo<font color=brown><a href=http://www.google.com>[bar]</a></font>baz', + 'foo<a href=http://www.google.com><font color=black>[bar]</font></a>baz', + 'foo<a href=http://www.google.com><u>[bar]</u></a>baz', + 'foo<u><a href=http://www.google.com>[bar]</a></u>baz', + 'foo<sub><font size=2>[bar]</font></sub>baz', + 'foo<font size=2><sub>[bar]</sub></font>baz', + 'foo<sub><font size=3>[bar]</font></sub>baz', + 'foo<font size=3><sub>[bar]</sub></font>baz', + + // Now repeat but with different selections. + '[foo<b>bar]</b>baz', + '[foo<i>bar]</i>baz', + '[foo<s>bar]</s>baz', + '[foo<sub>bar]</sub>baz', + '[foo<sup>bar]</sup>baz', + '[foo<u>bar]</u>baz', + '[foo<a href=http://www.google.com>bar]</a>baz', + '[foo<font face=sans-serif>bar]</font>baz', + '[foo<font size=4>bar]</font>baz', + '[foo<font color=#0000FF>bar]</font>baz', + '[foo<span style=background-color:#00FFFF>bar]</span>baz', + '[foo<a href=http://www.google.com><font color=blue>bar]</font></a>baz', + '[foo<font color=blue><a href=http://www.google.com>bar]</a></font>baz', + '[foo<a href=http://www.google.com><font color=brown>bar]</font></a>baz', + '[foo<font color=brown><a href=http://www.google.com>bar]</a></font>baz', + '[foo<a href=http://www.google.com><font color=black>bar]</font></a>baz', + '[foo<a href=http://www.google.com><u>bar]</u></a>baz', + '[foo<u><a href=http://www.google.com>bar]</a></u>baz', + '[foo<sub><font size=2>bar]</font></sub>baz', + '[foo<font size=2><sub>bar]</sub></font>baz', + '[foo<sub><font size=3>bar]</font></sub>baz', + '[foo<font size=3><sub>bar]</sub></font>baz', + + 'foo<b>[bar</b>baz]', + 'foo<i>[bar</i>baz]', + 'foo<s>[bar</s>baz]', + 'foo<sub>[bar</sub>baz]', + 'foo<sup>[bar</sup>baz]', + 'foo<u>[bar</u>baz]', + 'foo<a href=http://www.google.com>[bar</a>baz]', + 'foo<font face=sans-serif>[bar</font>baz]', + 'foo<font size=4>[bar</font>baz]', + 'foo<font color=#0000FF>[bar</font>baz]', + 'foo<span style=background-color:#00FFFF>[bar</span>baz]', + 'foo<a href=http://www.google.com><font color=blue>[bar</font></a>baz]', + 'foo<font color=blue><a href=http://www.google.com>[bar</a></font>baz]', + 'foo<a href=http://www.google.com><font color=brown>[bar</font></a>baz]', + 'foo<font color=brown><a href=http://www.google.com>[bar</a></font>baz]', + 'foo<a href=http://www.google.com><font color=black>[bar</font></a>baz]', + 'foo<a href=http://www.google.com><u>[bar</u></a>baz]', + 'foo<u><a href=http://www.google.com>[bar</a></u>baz]', + 'foo<sub><font size=2>[bar</font></sub>baz]', + 'foo<font size=2><sub>[bar</sub></font>baz]', + 'foo<sub><font size=3>[bar</font></sub>baz]', + 'foo<font size=3><sub>[bar</sub></font>baz]', + + // https://bugs.webkit.org/show_bug.cgi?id=19702 + '<blockquote><font color=blue>[foo]</font></blockquote>', + ], + //@} + insertunorderedlist: [ + //@{ + 'foo[]bar', + 'foo[bar]baz', + 'foo<br>[bar]', + 'f[oo<br>b]ar<br>baz', + '<p>[foo]<br>bar</p>', + '[foo<ol><li>bar]</ol>baz', + 'foo<ol><li>[bar</ol>baz]', + '[foo<ul><li>bar]</ul>baz', + 'foo<ul><li>[bar</ul>baz]', + 'foo<ul><li>[bar</ul><ol><li>baz]</ol>quz', + 'foo<ol><li>[bar</ol><ul><li>baz]</ul>quz', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr><td>fo[o<td>b]ar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + '<p>foo<p>[bar]<p>baz', + '<p>foo<blockquote>[bar]</blockquote><p>baz', + '<dl><dt>foo<dd>[bar]<dt>baz<dd>quz</dl>', + '<dl><dt>foo<dd>bar<dt>[baz]<dd>quz</dl>', + + '<p>[foo<p>bar]<p>baz', + '<p>[foo<blockquote>bar]</blockquote><p>baz', + '<dl><dt>[foo<dd>bar]<dt>baz<dd>quz</dl>', + '<dl><dt>foo<dd>[bar<dt>baz]<dd>quz</dl>', + + '<p>[foo<blockquote><p>bar]<p>baz</blockquote>', + + + // Various <ol> stuff + '<ol><li>foo<li>[bar]<li>baz</ol>', + '<ol><li>foo</ol>[bar]', + '[foo]<ol><li>bar</ol>', + '<ol><li>foo</ol>[bar]<ol><li>baz</ol>', + '<ol><ol><li>[foo]</ol></ol>', + '<ol><li>[foo]<br>bar<li>baz</ol>', + '<ol><li>foo<br>[bar]<li>baz</ol>', + '<ol><li><div>[foo]</div>bar<li>baz</ol>', + '<ol><li>foo<ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>[foo]<ol><li>bar</ol><li>baz</ol>', + '<ol><li>[foo]</li><ol><li>bar</ol><li>baz</ol>', + '<ol><li>foo<li>[bar]<ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<li>[bar]</li><ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>baz</ol><li>[quz]</ol>', + '<ol><li>foo</li><ol><li>bar<li>baz</ol><li>[quz]</ol>', + + // Multiple items at once. + '<ol><li>foo<li>[bar<li>baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol><li>baz</ol>', + '<ol><li>foo<ol><li>b[ar</ol><li>b]az</ol>', + '<ol><li>[foo<ol><li>bar</ol><li>baz]</ol><p>extra', + + // We probably can't actually get this DOM . . . + '<ol><li>[foo]<ol><li>bar</ol>baz</ol>', + '<ol><li>foo<ol><li>[bar]</ol>baz</ol>', + '<ol><li>foo<ol><li>bar</ol>[baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol>baz</ol>', + + + // Same stuff but with <ul> + '<ul><li>foo<li>[bar]<li>baz</ul>', + '<ul><li>foo</ul>[bar]', + '[foo]<ul><li>bar</ul>', + '<ul><li>foo</ul>[bar]<ul><li>baz</ul>', + '<ul><ul><li>[foo]</ul></ul>', + '<ul><li>[foo]<br>bar<li>baz</ul>', + '<ul><li>foo<br>[bar]<li>baz</ul>', + '<ul><li><div>[foo]</div>bar<li>baz</ul>', + '<ul><li>foo<ul><li>[bar]<li>baz</ul><li>quz</ul>', + '<ul><li>foo<ul><li>bar<li>[baz]</ul><li>quz</ul>', + '<ul><li>foo</li><ul><li>[bar]<li>baz</ul><li>quz</ul>', + '<ul><li>foo</li><ul><li>bar<li>[baz]</ul><li>quz</ul>', + '<ul><li>[foo]<ul><li>bar</ul><li>baz</ul>', + '<ul><li>[foo]</li><ul><li>bar</ul><li>baz</ul>', + '<ul><li>foo<li>[bar]<ul><li>baz</ul><li>quz</ul>', + '<ul><li>foo<li>[bar]</li><ul><li>baz</ul><li>quz</ul>', + '<ul><li>foo<ul><li>bar<li>baz</ul><li>[quz]</ul>', + '<ul><li>foo</li><ul><li>bar<li>baz</ul><li>[quz]</ul>', + + // Multiple items at once. + '<ul><li>foo<li>[bar<li>baz]</ul>', + '<ul><li>[foo<ul><li>bar]</ul><li>baz</ul>', + '<ul><li>foo<ul><li>b[ar</ul><li>b]az</ul>', + '<ul><li>[foo<ul><li>bar</ul><li>baz]</ul><p>extra', + + // We probably can't actually get this DOM . . . + '<ul><li>[foo]<ul><li>bar</ul>baz</ul>', + '<ul><li>foo<ul><li>[bar]</ul>baz</ul>', + '<ul><li>foo<ul><li>bar</ul>[baz]</ul>', + '<ul><li>[foo<ul><li>bar]</ul>baz</ul>', + + + // Mix of <ol> and <ul> + 'foo<ol><li>bar</ol><ul><li>[baz]</ul>quz', + 'foo<ol><li>bar</ol><ul><li>[baz</ul>quz]', + 'foo<ul><li>[bar]</ul><ol><li>baz</ol>quz', + '[foo<ul><li>bar]</ul><ol><li>baz</ol>quz', + + // Interaction with indentation + '[foo]<blockquote>bar</blockquote>baz', + 'foo<blockquote>[bar]</blockquote>baz', + '[foo<blockquote>bar]</blockquote>baz', + '<ol><li>foo</ol><blockquote>[bar]</blockquote>baz', + '[foo]<blockquote><ol><li>bar</ol></blockquote>baz', + 'foo<blockquote>[bar]<br>baz</blockquote>', + '[foo<blockquote>bar]<br>baz</blockquote>', + '<ol><li>foo</ol><blockquote>[bar]<br>baz</blockquote>', + + '<p>[foo]<blockquote><p>bar</blockquote><p>baz', + '<p>foo<blockquote><p>[bar]</blockquote><p>baz', + '<p>[foo<blockquote><p>bar]</blockquote><p>baz', + '<ol><li>foo</ol><blockquote><p>[bar]</blockquote><p>baz', + + // Attributes + '<ul id=abc><li>foo<li>[bar]<li>baz</ul>', + '<ul style=color:blue><li>foo<li>[bar]<li>baz</ul>', + '<ul style=text-indent:1em><li>foo<li>[bar]<li>baz</ul>', + '<ul id=abc><li>[foo]<li>bar<li>baz</ul>', + '<ul style=color:blue><li>[foo]<li>bar<li>baz</ul>', + '<ul style=text-indent:1em><li>[foo]<li>bar<li>baz</ul>', + '<ul id=abc><li>foo<li>bar<li>[baz]</ul>', + '<ul style=color:blue><li>foo<li>bar<li>[baz]</ul>', + '<ul style=text-indent:1em><li>foo<li>bar<li>[baz]</ul>', + + // Whitespace nodes + '<ul><li>foo</ul> <p>[bar]', + '<p>[foo]</p> <ul><li>bar</ul>', + '<ul><li>foo</ul> <p>[bar]</p> <ul><li>baz</ul>', + + // https://bugs.webkit.org/show_bug.cgi?id=24167 + '{<div style="font-size: 1.3em">1</div><div style="font-size: 1.1em">2</div>}', + ], + //@} + italic: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<span style="font-style: italic">[bar]</span>baz', + 'foo<address>[bar]</address>baz', + 'foo<cite>[bar]</cite>baz', + 'foo<dfn>[bar]</dfn>baz', + 'foo<em>[bar]</em>baz', + 'foo<i>[bar]</i>baz', + 'foo<var>[bar]</var>baz', + + 'foo{<address>bar</address>}baz', + 'foo{<cite>bar</cite>}baz', + 'foo{<dfn>bar</dfn>}baz', + 'foo{<em>bar</em>}baz', + 'foo{<i>bar</i>}baz', + 'foo{<var>bar</var>}baz', + + 'foo<address>b[a]r</address>baz', + 'foo<cite>b[a]r</cite>baz', + 'foo<dfn>b[a]r</dfn>baz', + 'foo<em>b[a]r</em>baz', + 'foo<i>b[a]r</i>baz', + 'foo<var>b[a]r</var>baz', + + 'fo[o<address>bar</address>b]az', + 'fo[o<cite>bar</cite>b]az', + 'fo[o<dfn>bar</dfn>b]az', + 'fo[o<em>bar</em>b]az', + 'fo[o<i>bar</i>b]az', + 'fo[o<var>bar</var>b]az', + + 'foo[<address>bar</address>baz]', + 'foo[<cite>bar</cite>baz]', + 'foo[<dfn>bar</dfn>baz]', + 'foo[<em>bar</em>baz]', + 'foo[<i>bar</i>baz]', + 'foo[<var>bar</var>baz]', + + '[foo<address>bar</address>]baz', + '[foo<cite>bar</cite>]baz', + '[foo<dfn>bar</dfn>]baz', + '[foo<em>bar</em>]baz', + '[foo<i>bar</i>]baz', + '[foo<var>bar</var>]baz', + + 'foo<span style="font-style: italic">[bar]</span>baz', + 'foo<span style="font-style: oblique">[bar]</span>baz', + 'foo<span style="font-style: oblique">b[a]r</span>baz', + + '<i>{<p>foo</p><p>bar</p>}<p>baz</p></i>', + '<i><p>foo[<b>bar</b>}</p><p>baz</p></i>', + 'foo [bar <b>baz] qoz</b> quz sic', + 'foo bar <b>baz [qoz</b> quz] sic', + 'foo [bar <i>baz] qoz</i> quz sic', + 'foo bar <i>baz [qoz</i> quz] sic', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<i>b]ar</i>baz', + 'foo<i>ba[r</i>b]az', + 'fo[o<i>bar</i>b]az', + 'foo[<i>b]ar</i>baz', + 'foo<i>ba[r</i>]baz', + 'foo[<i>bar</i>]baz', + 'foo<i>[bar]</i>baz', + 'foo{<i>bar</i>}baz', + 'fo[o<span style=font-style:italic>b]ar</span>baz', + 'fo[o<span style=font-style:oblique>b]ar</span>baz', + '<span style=font-style:italic>fo[o</span><span style=font-style:oblique>b]ar</span>', + '<span style=font-style:oblique>fo[o</span><span style=font-style:italic>b]ar</span>', + '<i>fo[o</i><address>b]ar</address>', + ], + //@} + justifycenter: [ + //@{ + 'foo[]bar<p>extra', + '<span>foo</span>{}<span>bar</span><p>extra', + '<span>foo[</span><span>]bar</span><p>extra', + 'foo[bar]baz<p>extra', + 'foo[bar<b>baz]qoz</b>quz<p>extra', + '<p>foo[]bar<p>extra', + '<p>foo[bar]baz<p>extra', + '<h1>foo[bar]baz</h1><p>extra', + '<pre>foo[bar]baz</pre><p>extra', + '<xmp>foo[bar]baz</xmp><p>extra', + '<center><p>[foo]<p>bar</center><p>extra', + '<center><p>[foo<p>bar]</center><p>extra', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table align=center><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table align=center><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=center><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=center><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table align=center data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table align=center><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody align=center><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody align=center><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=center><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=center data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody align=center><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tbody align=center><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody><tr align=center><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr align=center data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr align=center data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr align=center><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr align=center><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr align=center><td>foo<td>bar<td>baz</table>}<p>extra', + + '<div align=center><p>[foo]<p>bar</div><p>extra', + '<div align=center><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:center><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:center><p>[foo<p>bar]</div><p>extra', + + '<div align=justify><p>[foo]<p>bar</div><p>extra', + '<div align=justify><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:justify><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:justify><p>[foo<p>bar]</div><p>extra', + + '<div align=left><p>[foo]<p>bar</div><p>extra', + '<div align=left><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:left><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:left><p>[foo<p>bar]</div><p>extra', + + '<div align=right><p>[foo]<p>bar</div><p>extra', + '<div align=right><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:right><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:right><p>[foo<p>bar]</div><p>extra', + + '<center>foo</center>[bar]<p>extra', + '[foo]<center>bar</center><p>extra', + '<center>foo</center>[bar]<center>baz</center><p>extra', + '<div align=center>foo</div>[bar]<p>extra', + '[foo]<div align=center>bar</div><p>extra', + '<div align=center>foo</div>[bar]<div align=center>baz</div><p>extra', + '<div align=center><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div align=center><p>bar</div><p>extra', + '<div align=center><p>foo</div><p>[bar]<div align=center><p>baz</div><p>extra', + '<div style=text-align:center>foo</div>[bar]<p>extra', + '[foo]<div style=text-align:center>bar</div><p>extra', + '<div style=text-align:center>foo</div>[bar]<div style=text-align:center>baz</div><p>extra', + '<div style=text-align:center><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div style=text-align:center><p>bar</div><p>extra', + '<div style=text-align:center><p>foo</div><p>[bar]<div style=text-align:center><p>baz</div><p>extra', + '<p align=center>foo<p>[bar]<p>extra', + '<p>[foo]<p align=center>bar<p>extra', + '<p align=center>foo<p>[bar]<p align=center>baz<p>extra', + + '<center>[foo</center>bar]<p>extra', + '<center>fo[o</center>b]ar<p>extra', + '<div align=center>[foo</div>bar]<p>extra', + '<div align=center>fo[o</div>b]ar<p>extra', + '<div style=text-align:center>[foo</div>bar]<p>extra', + '<div style=text-align:center>fo[o</div>b]ar<p>extra', + '<span style=text-align:center>[foo]</span><p>extra', + '<span style=text-align:center>f[o]o</span><p>extra', + + '<div style=text-align:center>[foo<div style=text-align:left contenteditable=false>bar</div>baz]</div><p>extra', + + '<div align=nonsense><p>[foo]</div><p>extra', + '<div style=text-align:inherit><p>[foo]</div><p>extra', + '<quasit align=right><p>[foo]</p></quasit><p>extra', + + '<div align=center>{<div align=left>foo</div>}</div>', + '<div align=left>{<div align=center>foo</div>}</div>', + '<div align=center>{<div align=left>foo</div>bar}</div>', + '<div align=left>{<div align=center>foo</div>bar}</div>', + '<div align=center>{<div align=left>foo</div><img src=/img/lion.svg>}</div>', + '<div align=left>{<div align=center>foo</div><img src=/img/lion.svg>}</div>', + '<div align=center>{<div align=left>foo</div><!-- bar -->}</div>', + '<div align=left>{<div align=center>foo</div><!-- bar -->}</div>', + + '<div style=text-align:start>[foo]</div><p>extra', + '<div style=text-align:end>[foo]</div><p>extra', + '<div dir=rtl style=text-align:start>[foo]</div><p>extra', + '<div dir=rtl style=text-align:end>[foo]</div><p>extra', + + // Whitespace nodes + '<div style=text-align:center><p>foo</div> <p>[bar]', + '<div align=center><p>foo</div> <p>[bar]', + '<center><p>foo</center> <p>[bar]', + '<p>[foo]</p> <div style=text-align:center><p>bar</div>', + '<p>[foo]</p> <div align=center><p>bar</div>', + '<p>[foo]</p> <center><p>bar</center>', + '<div style=text-align:center><p>foo</div> <p>[bar]</p> <div style=text-align:center><p>baz</div>', + '<div align=center><p>foo</div> <p>[bar]</p> <div align=center><p>baz</div>', + '<center><p>foo</center> <p>[bar]</p> <center><p>baz</center>', + ], + //@} + justifyfull: [ + //@{ + 'foo[]bar<p>extra', + '<span>foo</span>{}<span>bar</span><p>extra', + '<span>foo[</span><span>]bar</span><p>extra', + 'foo[bar]baz<p>extra', + 'foo[bar<b>baz]qoz</b>quz<p>extra', + '<p>foo[]bar<p>extra', + '<p>foo[bar]baz<p>extra', + '<h1>foo[bar]baz</h1><p>extra', + '<pre>foo[bar]baz</pre><p>extra', + '<xmp>foo[bar]baz</xmp><p>extra', + '<center><p>[foo]<p>bar</center><p>extra', + '<center><p>[foo<p>bar]</center><p>extra', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table align=justify><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table align=justify><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=justify><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=justify><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table align=justify data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table align=justify><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody align=justify><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody align=justify><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=justify><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=justify data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody align=justify><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tbody align=justify><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody><tr align=justify><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr align=justify data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr align=justify data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr align=justify><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr align=justify><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr align=justify><td>foo<td>bar<td>baz</table>}<p>extra', + + '<div align=center><p>[foo]<p>bar</div><p>extra', + '<div align=center><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:center><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:center><p>[foo<p>bar]</div><p>extra', + + '<div align=justify><p>[foo]<p>bar</div><p>extra', + '<div align=justify><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:justify><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:justify><p>[foo<p>bar]</div><p>extra', + + '<div align=left><p>[foo]<p>bar</div><p>extra', + '<div align=left><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:left><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:left><p>[foo<p>bar]</div><p>extra', + + '<div align=right><p>[foo]<p>bar</div><p>extra', + '<div align=right><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:right><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:right><p>[foo<p>bar]</div><p>extra', + + '<div align=justify>foo</div>[bar]<p>extra', + '[foo]<div align=justify>bar</div><p>extra', + '<div align=justify>foo</div>[bar]<div align=justify>baz</div><p>extra', + '<div align=justify><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div align=justify><p>bar</div><p>extra', + '<div align=justify><p>foo</div><p>[bar]<div align=justify><p>baz</div><p>extra', + '<div style=text-align:justify>foo</div>[bar]<p>extra', + '[foo]<div style=text-align:justify>bar</div><p>extra', + '<div style=text-align:justify>foo</div>[bar]<div style=text-align:justify>baz</div><p>extra', + '<div style=text-align:justify><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div style=text-align:justify><p>bar</div><p>extra', + '<div style=text-align:justify><p>foo</div><p>[bar]<div style=text-align:justify><p>baz</div><p>extra', + '<p align=justify>foo<p>[bar]<p>extra', + '<p>[foo]<p align=justify>bar<p>extra', + '<p align=justify>foo<p>[bar]<p align=justify>baz<p>extra', + + '<div align=justify>[foo</div>bar]<p>extra', + '<div align=justify>fo[o</div>b]ar<p>extra', + '<div style=text-align:justify>[foo</div>bar]<p>extra', + '<div style=text-align:justify>fo[o</div>b]ar<p>extra', + '<span style=text-align:justify>[foo]</span><p>extra', + '<span style=text-align:justify>f[o]o</span><p>extra', + + '<div style=text-align:justify>[foo<div style=text-align:left contenteditable=false>bar</div>baz]</div><p>extra', + + '<div align=nonsense><p>[foo]</div><p>extra', + '<div style=text-align:inherit><p>[foo]</div><p>extra', + '<quasit align=center><p>[foo]</p></quasit><p>extra', + + '<div style=text-align:start>[foo]</div><p>extra', + '<div style=text-align:end>[foo]</div><p>extra', + '<div dir=rtl style=text-align:start>[foo]</div><p>extra', + '<div dir=rtl style=text-align:end>[foo]</div><p>extra', + + // Whitespace nodes + '<div style=text-align:justify><p>foo</div> <p>[bar]', + '<div align=justify><p>foo</div> <p>[bar]', + '<p>[foo]</p> <div style=text-align:justify><p>bar</div>', + '<p>[foo]</p> <div align=justify><p>bar</div>', + '<div style=text-align:justify><p>foo</div> <p>[bar]</p> <div style=text-align:justify><p>baz</div>', + '<div align=justify><p>foo</div> <p>[bar]</p> <div align=justify><p>baz</div>', + ], + //@} + justifyleft: [ + //@{ + 'foo[]bar<p>extra', + '<span>foo</span>{}<span>bar</span><p>extra', + '<span>foo[</span><span>]bar</span><p>extra', + 'foo[bar]baz<p>extra', + 'foo[bar<b>baz]qoz</b>quz<p>extra', + '<p>foo[]bar<p>extra', + '<p>foo[bar]baz<p>extra', + '<h1>foo[bar]baz</h1><p>extra', + '<pre>foo[bar]baz</pre><p>extra', + '<xmp>foo[bar]baz</xmp><p>extra', + '<center><p>[foo]<p>bar</center><p>extra', + '<center><p>[foo<p>bar]</center><p>extra', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table align=left><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table align=left><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=left><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=left><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table align=left data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table align=left><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody align=left><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody align=left><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=left><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=left data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody align=left><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tbody align=left><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody><tr align=left><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr align=left data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr align=left data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr align=left><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr align=left><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr align=left><td>foo<td>bar<td>baz</table>}<p>extra', + + '<div align=center><p>[foo]<p>bar</div><p>extra', + '<div align=center><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:center><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:center><p>[foo<p>bar]</div><p>extra', + + '<div align=justify><p>[foo]<p>bar</div><p>extra', + '<div align=justify><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:justify><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:justify><p>[foo<p>bar]</div><p>extra', + + '<div align=left><p>[foo]<p>bar</div><p>extra', + '<div align=left><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:left><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:left><p>[foo<p>bar]</div><p>extra', + + '<div align=right><p>[foo]<p>bar</div><p>extra', + '<div align=right><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:right><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:right><p>[foo<p>bar]</div><p>extra', + + '<div align=left>foo</div>[bar]<p>extra', + '[foo]<div align=left>bar</div><p>extra', + '<div align=left>foo</div>[bar]<div align=left>baz</div><p>extra', + '<div align=left><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div align=left><p>bar</div><p>extra', + '<div align=left><p>foo</div><p>[bar]<div align=left><p>baz</div><p>extra', + '<div style=text-align:left>foo</div>[bar]<p>extra', + '[foo]<div style=text-align:left>bar</div><p>extra', + '<div style=text-align:left>foo</div>[bar]<div style=text-align:left>baz</div><p>extra', + '<div style=text-align:left><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div style=text-align:left><p>bar</div><p>extra', + '<div style=text-align:left><p>foo</div><p>[bar]<div style=text-align:left><p>baz</div><p>extra', + '<p align=left>foo<p>[bar]<p>extra', + '<p>[foo]<p align=left>bar<p>extra', + '<p align=left>foo<p>[bar]<p align=left>baz<p>extra', + + '<div align=left>[foo</div>bar]<p>extra', + '<div align=left>fo[o</div>b]ar<p>extra', + '<div style=text-align:left>[foo</div>bar]<p>extra', + '<div style=text-align:left>fo[o</div>b]ar<p>extra', + '<span style=text-align:left>[foo]</span><p>extra', + '<span style=text-align:left>f[o]o</span><p>extra', + + '<div style=text-align:left>[foo<div style=text-align:left contenteditable=false>bar</div>baz]</div><p>extra', + + '<div align=nonsense><p>[foo]</div><p>extra', + '<div style=text-align:inherit><p>[foo]</div><p>extra', + '<quasit align=center><p>[foo]</p></quasit><p>extra', + + '<div style=text-align:start>[foo]</div><p>extra', + '<div style=text-align:end>[foo]</div><p>extra', + '<div dir=rtl style=text-align:start>[foo]</div><p>extra', + '<div dir=rtl style=text-align:end>[foo]</div><p>extra', + + // Whitespace nodes + '<div style=text-align:left><p>foo</div> <p>[bar]', + '<div align=left><p>foo</div> <p>[bar]', + '<p>[foo]</p> <div style=text-align:left><p>bar</div>', + '<p>[foo]</p> <div align=left><p>bar</div>', + '<div style=text-align:left><p>foo</div> <p>[bar]</p> <div style=text-align:left><p>baz</div>', + '<div align=left><p>foo</div> <p>[bar]</p> <div align=left><p>baz</div>', + ], + //@} + justifyright: [ + //@{ + 'foo[]bar<p>extra', + '<span>foo</span>{}<span>bar</span><p>extra', + '<span>foo[</span><span>]bar</span><p>extra', + 'foo[bar]baz<p>extra', + 'foo[bar<b>baz]qoz</b>quz<p>extra', + '<p>foo[]bar<p>extra', + '<p>foo[bar]baz<p>extra', + '<h1>foo[bar]baz</h1><p>extra', + '<pre>foo[bar]baz</pre><p>extra', + '<xmp>foo[bar]baz</xmp><p>extra', + '<center><p>[foo]<p>bar</center><p>extra', + '<center><p>[foo<p>bar]</center><p>extra', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table align=right><tbody><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table align=right><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=right><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table align=right><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table align=right data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table align=right><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody align=right><tr><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody align=right><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=right><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody align=right data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody align=right><tr><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tbody align=right><tr><td>foo<td>bar<td>baz</table>}<p>extra', + + '<table><tbody><tr align=right><td>foo<td>b[a]r<td>baz</table><p>extra', + '<table><tbody><tr align=right data-start=1 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody><tr align=right data-start=0 data-end=2><td>foo<td>bar<td>baz</table><p>extra', + '<table><tbody data-start=0 data-end=1><tr align=right><td>foo<td>bar<td>baz</table><p>extra', + '<table data-start=0 data-end=1><tbody><tr align=right><td>foo<td>bar<td>baz</table><p>extra', + '{<table><tr align=right><td>foo<td>bar<td>baz</table>}<p>extra', + + '<div align=center><p>[foo]<p>bar</div><p>extra', + '<div align=center><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:center><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:center><p>[foo<p>bar]</div><p>extra', + + '<div align=justify><p>[foo]<p>bar</div><p>extra', + '<div align=justify><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:justify><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:justify><p>[foo<p>bar]</div><p>extra', + + '<div align=left><p>[foo]<p>bar</div><p>extra', + '<div align=left><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:left><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:left><p>[foo<p>bar]</div><p>extra', + + '<div align=right><p>[foo]<p>bar</div><p>extra', + '<div align=right><p>[foo<p>bar}</div><p>extra', + '<div style=text-align:right><p>[foo]<p>bar</div><p>extra', + '<div style=text-align:right><p>[foo<p>bar]</div><p>extra', + + '<div align=right>foo</div>[bar]<p>extra', + '[foo]<div align=right>bar</div><p>extra', + '<div align=right>foo</div>[bar]<div align=right>baz</div><p>extra', + '<div align=right><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div align=right><p>bar</div><p>extra', + '<div align=right><p>foo</div><p>[bar]<div align=right><p>baz</div><p>extra', + '<div style=text-align:right>foo</div>[bar]<p>extra', + '[foo]<div style=text-align:right>bar</div><p>extra', + '<div style=text-align:right>foo</div>[bar]<div style=text-align:right>baz</div><p>extra', + '<div style=text-align:right><p>foo</div><p>[bar]<p>extra', + '<p>[foo]<div style=text-align:right><p>bar</div><p>extra', + '<div style=text-align:right><p>foo</div><p>[bar]<div style=text-align:right><p>baz</div><p>extra', + '<p align=right>foo<p>[bar]<p>extra', + '<p>[foo]<p align=right>bar<p>extra', + '<p align=right>foo<p>[bar]<p align=right>baz<p>extra', + + '<div align=right>[foo</div>bar]<p>extra', + '<div align=right>fo[o</div>b]ar<p>extra', + '<div style=text-align:right>[foo</div>bar]<p>extra', + '<div style=text-align:right>fo[o</div>b]ar<p>extra', + '<span style=text-align:right>[foo]</span><p>extra', + '<span style=text-align:right>f[o]o</span><p>extra', + + '<div style=text-align:right>[foo<div style=text-align:left contenteditable=false>bar</div>baz]</div><p>extra', + + '<div align=nonsense><p>[foo]</div><p>extra', + '<div style=text-align:inherit><p>[foo]</div><p>extra', + '<quasit align=center><p>[foo]</p></quasit><p>extra', + + '<div style=text-align:start>[foo]</div><p>extra', + '<div style=text-align:end>[foo]</div><p>extra', + '<div dir=rtl style=text-align:start>[foo]</div><p>extra', + '<div dir=rtl style=text-align:end>[foo]</div><p>extra', + + // Whitespace nodes + '<div style=text-align:right><p>foo</div> <p>[bar]', + '<div align=right><p>foo</div> <p>[bar]', + '<p>[foo]</p> <div style=text-align:right><p>bar</div>', + '<p>[foo]</p> <div align=right><p>bar</div>', + '<div style=text-align:right><p>foo</div> <p>[bar]</p> <div style=text-align:right><p>baz</div>', + '<div align=right><p>foo</div> <p>[bar]</p> <div align=right><p>baz</div>', + ], + //@} + outdent: [ + //@{ + // These mimic existing indentation in various browsers, to see how + // they cope with outdenting various things. This is spec, Gecko + // non-CSS, and Opera: + '<blockquote><p>foo[bar]</p><p>baz</p></blockquote><p>extra', + '<blockquote><p>foo[bar</p><p>b]az</p></blockquote><p>extra', + '<blockquote><p>foo[bar]</p></blockquote><p>baz</p><p>extra', + '<blockquote><p>foo[bar</p></blockquote><p>b]az</p><p>extra', + + // IE: + '<blockquote style="margin-right: 0px;" dir="ltr"><p>foo[bar]</p><p>baz</p></blockquote><p>extra', + '<blockquote style="margin-right: 0px;" dir="ltr"><p>foo[bar</p><p>b]az</p></blockquote><p>extra', + '<blockquote style="margin-right: 0px;" dir="ltr"><p>foo[bar]</p></blockquote><p>baz</p><p>extra', + '<blockquote style="margin-right: 0px;" dir="ltr"><p>foo[bar</p></blockquote><p>b]az</p><p>extra', + + // Firefox CSS mode: + '<p style="margin-left: 40px">foo[bar]</p><p style="margin-left: 40px">baz</p><p>extra', + '<p style="margin-left: 40px">foo[bar</p><p style="margin-left: 40px">b]az</p><p>extra', + '<p style="margin-left: 40px">foo[bar]</p><p>baz</p><p>extra', + '<p style="margin-left: 40px">foo[bar</p><p>b]az</p><p>extra', + + // WebKit: + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><p>foo[bar]</p><p>baz</p></blockquote><p>extra', + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><p>foo[bar</p><p>b]az</p></blockquote><p>extra', + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><p>foo[bar]</p></blockquote><p>baz</p><p>extra', + '<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><p>foo[bar</p></blockquote><p>b]az</p><p>extra', + + // Now let's try nesting lots of stuff and see what happens. + '<blockquote><blockquote>foo[bar]baz</blockquote></blockquote>', + '<blockquote><blockquote data-abc=def>foo[bar]baz</blockquote></blockquote>', + '<blockquote data-abc=def><blockquote>foo[bar]baz</blockquote></blockquote>', + '<blockquote><div>foo[bar]baz</div></blockquote>', + '<blockquote><div id=abc>foo[bar]baz</div></blockquote>', + '<blockquote id=abc>foo[bar]baz</blockquote>', + '<blockquote style="color: blue">foo[bar]baz</blockquote>', + + '<blockquote><blockquote><p>foo[bar]<p>baz</blockquote></blockquote>', + '<blockquote><blockquote data-abc=def><p>foo[bar]<p>baz</blockquote></blockquote>', + '<blockquote data-abc=def><blockquote><p>foo[bar]<p>baz</blockquote></blockquote>', + '<blockquote><div><p>foo[bar]<p>baz</div></blockquote>', + '<blockquote><div id=abc><p>foo[bar]<p>baz</div></blockquote>', + '<blockquote id=abc><p>foo[bar]<p>baz</blockquote>', + '<blockquote style="color: blue"><p>foo[bar]<p>baz</blockquote>', + + '<blockquote><p><b>foo[bar]</b><p>baz</blockquote>', + '<blockquote><p><strong>foo[bar]</strong><p>baz</blockquote>', + '<blockquote><p><span>foo[bar]</span><p>baz</blockquote>', + '<blockquote><blockquote style="color: blue"><p>foo[bar]</blockquote><p>baz</blockquote>', + '<blockquote style="color: blue"><blockquote><p>foo[bar]</blockquote><p>baz</blockquote>', + + // Lists! + '<ol><li>foo<li>[bar]<li>baz</ol>', + '<ol data-start=1 data-end=2><li>foo<li>bar<li>baz</ol>', + '<ol><li>foo</ol>[bar]', + '<ol><li>[foo]<br>bar<li>baz</ol>', + '<ol><li>foo<br>[bar]<li>baz</ol>', + '<ol><li><div>[foo]</div>bar<li>baz</ol>', + '<ol><li>foo<ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>[bar]<li>baz</ol><li>quz</ol>', + '<ol><li>foo</li><ol data-start=0 data-end=1><li>bar<li>baz</ol><li>quz</ol>', + '<ol><li>foo</li><ol><li>bar<li>[baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol data-start=1 data-end=2><li>bar<li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>b[a]r</ol><li>baz</ol>', + '<ol><li>foo</li><ol><li>b[a]r</ol><li>baz</ol>', + '<ol><li>foo{<ol><li>bar</ol>}<li>baz</ol>', + '<ol><li>foo</li>{<ol><li>bar</ol>}<li>baz</ol>', + '<ol><li>[foo]<ol><li>bar</ol><li>baz</ol>', + '<ol><li>[foo]</li><ol><li>bar</ol><li>baz</ol>', + '<ol><li>foo<li>[bar]<ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<li>[bar]</li><ol><li>baz</ol><li>quz</ol>', + '<ol><li>foo<ol><li>bar<li>baz</ol><li>[quz]</ol>', + '<ol><li>foo</li><ol><li>bar<li>baz</ol><li>[quz]</ol>', + + // Try outdenting multiple items at once. + '<ol><li>foo<li>b[ar<li>baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol><li>baz</ol>', + '<ol><li>[foo</li><ol><li>bar]</ol><li>baz</ol>', + '<ol><li>foo<ol><li>b[ar</ol><li>b]az</ol>', + '<ol><li>foo</li><ol><li>b[ar</ol><li>b]az</ol>', + '<ol><li>[foo<ol><li>bar</ol><li>baz]</ol><p>extra', + '<ol><li>[foo</li><ol><li>bar</ol><li>baz]</ol><p>extra', + + // We probably can't actually get this DOM . . . + '<ol><li>[foo]<ol><li>bar</ol>baz</ol>', + '<ol><li>foo<ol><li>[bar]</ol>baz</ol>', + '<ol><li>foo<ol><li>bar</ol>[baz]</ol>', + '<ol><li>[foo<ol><li>bar]</ol>baz</ol>', + + // Attribute handling on lists + 'foo<ol start=5><li>[bar]</ol>baz', + 'foo<ol id=abc><li>[bar]</ol>baz', + 'foo<ol style=color:blue><li>[bar]</ol>baz', + 'foo<ol><li value=5>[bar]</ol>baz', + 'foo<ol><li id=abc>[bar]</ol>baz', + 'foo<ol><li style=color:blue>[bar]</ol>baz', + '<ol><li>foo</li><ol><li value=5>[bar]</ol></ol>', + '<ul><li>foo</li><ol><li value=5>[bar]</ol></ul>', + '<ol><li>foo</li><ol start=5><li>[bar]</ol><li>baz</ol>', + '<ol><li>foo</li><ol id=abc><li>[bar]</ol><li>baz</ol>', + '<ol><li>foo</li><ol style=color:blue><li>[bar]</ol><li>baz</ol>', + '<ol><li>foo</li><ol style=text-indent:1em><li>[bar]</ol><li>baz</ol>', + '<ol><li>foo</li><ol start=5><li>[bar<li>baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol id=abc><li>[bar<li>baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol style=color:blue><li>[bar<li>baz]</ol><li>quz</ol>', + '<ol><li>foo</li><ol style=text-indent:1em><li>[bar<li>baz]</ol><li>quz</ol>', + + // List inside indentation element + '<blockquote><ol><li>[foo]</ol></blockquote><p>extra', + '<blockquote>foo<ol><li>[bar]</ol>baz</blockquote><p>extra', + '<blockquote><ol><li>foo</li><ol><li>[bar]</ol><li>baz</ol></blockquote><p>extra', + + '<ol><li><h1>[foo]</h1></ol>', + '<ol><li><xmp>[foo]</xmp></li></ol>', + '<blockquote><ol><li>foo<div><ol><li>[bar]</ol></div><li>baz</ol></blockquote>', + + // Whitespace nodes + '<blockquote> <p>[foo]</p></blockquote>', + '<blockquote><p>[foo]</p> </blockquote>', + '<blockquote> <p>[foo]</p> </blockquote>', + '<ol> <li>[foo]</li></ol>', + '<ol><li>[foo]</li> </ol>', + '<ol> <li>[foo]</li> </ol>', + '<ul> <li>[foo]</li></ul>', + '<ul><li>[foo]</li> </ul>', + '<ul> <li>[foo]</li> </ul>', + '<blockquote> <p>[foo]</p> <p>bar</p> <p>baz</p></blockquote>', + '<blockquote> <p>foo</p> <p>[bar]</p> <p>baz</p></blockquote>', + '<blockquote> <p>foo</p> <p>bar</p> <p>[baz]</p></blockquote>', + '<ol> <li>[foo]</li> <li>bar</li> <li>baz</li></ol>', + '<ol> <li>foo</li> <li>[bar]</li> <li>baz</li></ol>', + '<ol> <li>foo</li> <li>bar</li> <li>[baz]</li></ol>', + '<ul> <li>[foo]</li> <li>bar</li> <li>baz</li></ul>', + '<ul> <li>foo</li> <li>[bar]</li> <li>baz</li></ul>', + '<ul> <li>foo</li> <li>bar</li> <li>[baz]</li></ul>', + + // https://bugs.webkit.org/show_bug.cgi?id=24249 + '<ol><li>[]a<table><tr><td><br></table></ol>', + // https://bugs.webkit.org/show_bug.cgi?id=43447 + '<blockquote><span>foo<br>[bar]</span></blockquote>', + ], + //@} + removeformat: [ + //@{ + 'foo[]bar', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + '[foo<b>bar</b>baz]', + 'foo[<b>bar</b>baz]', + 'foo[<b>bar</b>]baz', + 'foo<b>[bar]</b>baz', + 'foo<b>b[a]r</b>baz', + '[foo<strong>bar</strong>baz]', + '[foo<span style="font-weight: bold">bar</span>baz]', + 'foo<span style="font-weight: bold">b[a]r</span>baz', + '[foo<span style="font-variant: small-caps">bar</span>baz]', + 'foo<span style="font-variant: small-caps">b[a]r</span>baz', + '[foo<b id=foo>bar</b>baz]', + 'foo<b id=foo>b[a]r</b>baz', + + // HTML has lots of inline elements, doesn't it? + '[foo<a>bar</a>baz]', + 'foo<a>b[a]r</a>baz', + '[foo<a href=foo>bar</a>baz]', + 'foo<a href=foo>b[a]r</a>baz', + '[foo<abbr>bar</abbr>baz]', + 'foo<abbr>b[a]r</abbr>baz', + '[foo<acronym>bar</acronym>baz]', + 'foo<acronym>b[a]r</acronym>baz', + '[foo<b>bar</b>baz]', + 'foo<b>b[a]r</b>baz', + '[foo<bdi dir=rtl>bar</bdi>baz]', + 'foo<bdi dir=rtl>b[a]r</bdi>baz', + '[foo<bdo dir=rtl>bar</bdo>baz]', + 'foo<bdo dir=rtl>b[a]r</bdo>baz', + '[foo<big>bar</big>baz]', + 'foo<big>b[a]r</big>baz', + '[foo<blink>bar</blink>baz]', + 'foo<blink>b[a]r</blink>baz', + '[foo<cite>bar</cite>baz]', + 'foo<cite>b[a]r</cite>baz', + '[foo<code>bar</code>baz]', + 'foo<code>b[a]r</code>baz', + '[foo<del>bar</del>baz]', + 'foo<del>b[a]r</del>baz', + '[foo<dfn>bar</dfn>baz]', + 'foo<dfn>b[a]r</dfn>baz', + '[foo<em>bar</em>baz]', + 'foo<em>b[a]r</em>baz', + '[foo<font>bar</font>baz]', + 'foo<font>b[a]r</font>baz', + '[foo<font color=blue>bar</font>baz]', + 'foo<font color=blue>b[a]r</font>baz', + '[foo<i>bar</i>baz]', + 'foo<i>b[a]r</i>baz', + '[foo<ins>bar</ins>baz]', + 'foo<ins>b[a]r</ins>baz', + '[foo<kbd>bar</kbd>baz]', + 'foo<kbd>b[a]r</kbd>baz', + '[foo<mark>bar</mark>baz]', + 'foo<mark>b[a]r</mark>baz', + '[foo<nobr>bar</nobr>baz]', + 'foo<nobr>b[a]r</nobr>baz', + '[foo<q>bar</q>baz]', + 'foo<q>b[a]r</q>baz', + '[foo<samp>bar</samp>baz]', + 'foo<samp>b[a]r</samp>baz', + '[foo<s>bar</s>baz]', + 'foo<s>b[a]r</s>baz', + '[foo<small>bar</small>baz]', + 'foo<small>b[a]r</small>baz', + '[foo<span>bar</span>baz]', + 'foo<span>b[a]r</span>baz', + '[foo<strike>bar</strike>baz]', + 'foo<strike>b[a]r</strike>baz', + '[foo<strong>bar</strong>baz]', + 'foo<strong>b[a]r</strong>baz', + '[foo<sub>bar</sub>baz]', + 'foo<sub>b[a]r</sub>baz', + '[foo<sup>bar</sup>baz]', + 'foo<sup>b[a]r</sup>baz', + '[foo<tt>bar</tt>baz]', + 'foo<tt>b[a]r</tt>baz', + '[foo<u>bar</u>baz]', + 'foo<u>b[a]r</u>baz', + '[foo<var>bar</var>baz]', + 'foo<var>b[a]r</var>baz', + + // Empty and replaced elements + '[foo<br>bar]', + '[foo<hr>bar]', + '[foo<wbr>bar]', + '[foo<img>bar]', + '[foo<img src=abc>bar]', + '[foo<video></video>bar]', + '[foo<video src=abc></video>bar]', + '[foo<svg><circle fill=blue r=20 cx=20 cy=20 /></svg>bar]', + + // Unrecognized elements + '[foo<nonexistentelement>bar</nonexistentelement>baz]', + 'foo<nonexistentelement>b[a]r</nonexistentelement>baz', + '[foo<nonexistentelement style="display: block">bar</nonexistentelement>baz]', + 'foo<nonexistentelement style="display: block">b[a]r</nonexistentelement>baz', + + // Random stuff + '[foo<span id=foo>bar</span>baz]', + 'foo<span id=foo>b[a]r</span>baz', + '[foo<span class=foo>bar</span>baz]', + 'foo<span class=foo>b[a]r</span>baz', + '[foo<b style="font-weight: normal">bar</b>baz]', + 'foo<b style="font-weight: normal">b[a]r</b>baz', + '<p style="background-color: aqua">foo[bar]baz</p>', + '<p><span style="background-color: aqua">foo[bar]baz</span></p>', + '<p style="font-weight: bold">foo[bar]baz</p>', + '<b><p style="font-weight: bold">foo[bar]baz</p></b>', + '<p style="font-variant: small-caps">foo[bar]baz</p>', + '{<p style="font-variant: small-caps">foobarbaz</p>}', + '<p style="text-indent: 2em">foo[bar]baz</p>', + '{<p style="text-indent: 2em">foobarbaz</p>}', + + // https://bugzilla.mozilla.org/show_bug.cgi?id=649138 + // Chrome 15 dev fails this for some unclear reason. + '<table data-start=0 data-end=1><tr><td><b>foo</b></table>', + ], + //@} + strikethrough: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<u>[bar]</u>baz', + 'foo<span style="text-decoration: underline">[bar]</span>baz', + '<u>foo[bar]baz</u>', + '<u>foo[b<span style="color:blue">ar]ba</span>z</u>', + '<u>foo[b<span style="color:blue" id=foo>ar]ba</span>z</u>', + '<u>foo[b<span style="font-size:3em">ar]ba</span>z</u>', + '<u>foo[b<i>ar]ba</i>z</u>', + '<p style="text-decoration: underline">foo[bar]baz</p>', + + 'foo<s>[bar]</s>baz', + 'foo<span style="text-decoration: line-through">[bar]</span>baz', + '<s>foo[bar]baz</s>', + '<s>foo[b<span style="color:blue">ar]ba</span>z</s>', + '<s>foo[b<span style="color:blue" id=foo>ar]ba</span>z</s>', + '<s>foo[b<span style="font-size:3em">ar]ba</span>z</s>', + '<s>foo[b<i>ar]ba</i>z</s>', + '<p style="text-decoration: line-through">foo[bar]baz</p>', + + 'foo<strike>[bar]</strike>baz', + '<strike>foo[bar]baz</strike>', + '<strike>foo[b<span style="color:blue">ar]ba</span>z</strike>', + '<strike>foo[b<span style="color:blue" id=foo>ar]ba</span>z</strike>', + '<strike>foo[b<span style="font-size:3em">ar]ba</span>z</strike>', + '<strike>foo[b<i>ar]ba</i>z</strike>', + + 'foo<ins>[bar]</ins>baz', + '<ins>foo[bar]baz</ins>', + '<ins>foo[b<span style="color:blue">ar]ba</span>z</ins>', + '<ins>foo[b<span style="color:blue" id=foo>ar]ba</span>z</ins>', + '<ins>foo[b<span style="font-size:3em">ar]ba</span>z</ins>', + '<ins>foo[b<i>ar]ba</i>z</ins>', + + 'foo<del>[bar]</del>baz', + '<del>foo[bar]baz</del>', + '<del>foo[b<span style="color:blue">ar]ba</span>z</del>', + '<del>foo[b<span style="color:blue" id=foo>ar]ba</span>z</del>', + '<del>foo[b<span style="font-size:3em">ar]ba</span>z</del>', + '<del>foo[b<i>ar]ba</i>z</del>', + + 'foo<span style="text-decoration: underline line-through">[bar]</span>baz', + 'foo<span style="text-decoration: underline line-through">b[a]r</span>baz', + 'foo<s style="text-decoration: underline">[bar]</s>baz', + 'foo<s style="text-decoration: underline">b[a]r</s>baz', + 'foo<u style="text-decoration: line-through">[bar]</u>baz', + 'foo<u style="text-decoration: line-through">b[a]r</u>baz', + 'foo<s style="text-decoration: overline">[bar]</s>baz', + 'foo<s style="text-decoration: overline">b[a]r</s>baz', + 'foo<u style="text-decoration: overline">[bar]</u>baz', + 'foo<u style="text-decoration: overline">b[a]r</u>baz', + + '<p style="text-decoration: line-through">foo[bar]baz</p>', + '<p style="text-decoration: overline">foo[bar]baz</p>', + + 'foo<span class="underline">[bar]</span>baz', + 'foo<span class="underline">b[a]r</span>baz', + 'foo<span class="line-through">[bar]</span>baz', + 'foo<span class="line-through">b[a]r</span>baz', + 'foo<span class="underline-and-line-through">[bar]</span>baz', + 'foo<span class="underline-and-line-through">b[a]r</span>baz', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<s>b]ar</s>baz', + 'foo<s>ba[r</s>b]az', + 'fo[o<s>bar</s>b]az', + 'foo[<s>b]ar</s>baz', + 'foo<s>ba[r</s>]baz', + 'foo[<s>bar</s>]baz', + 'foo<s>[bar]</s>baz', + 'foo{<s>bar</s>}baz', + 'fo[o<span style=text-decoration:line-through>b]ar</span>baz', + '<strike>fo[o</strike><s>b]ar</s>', + '<s>fo[o</s><del>b]ar</del>', + ], + //@} + subscript: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<sub>[bar]</sub>baz', + 'foo<sub>b[a]r</sub>baz', + 'foo<sup>[bar]</sup>baz', + 'foo<sup>b[a]r</sup>baz', + + 'foo<span style=vertical-align:sub>[bar]</span>baz', + 'foo<span style=vertical-align:super>[bar]</span>baz', + + 'foo<sub><sub>[bar]</sub></sub>baz', + 'foo<sub><sub>b[a]r</sub></sub>baz', + 'foo<sub>b<sub>[a]</sub>r</sub>baz', + 'foo<sup><sup>[bar]</sup></sup>baz', + 'foo<sup><sup>b[a]r</sup></sup>baz', + 'foo<sup>b<sup>[a]</sup>r</sup>baz', + 'foo<sub><sup>[bar]</sup></sub>baz', + 'foo<sub><sup>b[a]r</sup></sub>baz', + 'foo<sub>b<sup>[a]</sup>r</sub>baz', + 'foo<sup><sub>[bar]</sub></sup>baz', + 'foo<sup><sub>b[a]r</sub></sup>baz', + 'foo<sup>b<sub>[a]</sub>r</sup>baz', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<sub>b]ar</sub>baz', + 'foo<sub>ba[r</sub>b]az', + 'fo[o<sub>bar</sub>b]az', + 'foo[<sub>b]ar</sub>baz', + 'foo<sub>ba[r</sub>]baz', + 'foo[<sub>bar</sub>]baz', + 'foo<sub>[bar]</sub>baz', + 'foo{<sub>bar</sub>}baz', + '<sub>fo[o</sub><sup>b]ar</sup>', + '<sub>fo[o</sub><span style=vertical-align:sub>b]ar</span>', + 'foo<span style=vertical-align:top>[bar]</span>baz', + '<sub>fo[o</sub><span style=vertical-align:top>b]ar</span>', + ], + //@} + superscript: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<sub>[bar]</sub>baz', + 'foo<sub>b[a]r</sub>baz', + 'foo<sup>[bar]</sup>baz', + 'foo<sup>b[a]r</sup>baz', + + 'foo<span style=vertical-align:sub>[bar]</span>baz', + 'foo<span style=vertical-align:super>[bar]</span>baz', + + 'foo<sub><sub>[bar]</sub></sub>baz', + 'foo<sub><sub>b[a]r</sub></sub>baz', + 'foo<sub>b<sub>[a]</sub>r</sub>baz', + 'foo<sup><sup>[bar]</sup></sup>baz', + 'foo<sup><sup>b[a]r</sup></sup>baz', + 'foo<sup>b<sup>[a]</sup>r</sup>baz', + 'foo<sub><sup>[bar]</sup></sub>baz', + 'foo<sub><sup>b[a]r</sup></sub>baz', + 'foo<sub>b<sup>[a]</sup>r</sub>baz', + 'foo<sup><sub>[bar]</sub></sup>baz', + 'foo<sup><sub>b[a]r</sub></sup>baz', + 'foo<sup>b<sub>[a]</sub>r</sup>baz', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<sup>b]ar</sup>baz', + 'foo<sup>ba[r</sup>b]az', + 'fo[o<sup>bar</sup>b]az', + 'foo[<sup>b]ar</sup>baz', + 'foo<sup>ba[r</sup>]baz', + 'foo[<sup>bar</sup>]baz', + 'foo<sup>[bar]</sup>baz', + 'foo{<sup>bar</sup>}baz', + '<sup>fo[o</sup><sub>b]ar</sub>', + '<sup>fo[o</sup><span style=vertical-align:super>b]ar</span>', + 'foo<span style=vertical-align:bottom>[bar]</span>baz', + '<sup>fo[o</sup><span style=vertical-align:bottom>b]ar</span>', + + // https://bugs.webkit.org/show_bug.cgi?id=28472 + 'foo<sup>[bar]<br></sup>', + ], + //@} + underline: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<p>[foo<p><br><p>bar]', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<table><tbody><tr><td>foo<td>b[a]r<td>baz</table>', + '<table><tbody><tr data-start=1 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody><tr data-start=0 data-end=2><td>foo<td>bar<td>baz</table>', + '<table><tbody data-start=0 data-end=1><tr><td>foo<td>bar<td>baz</table>', + '<table data-start=0 data-end=1><tbody><tr><td>foo<td>bar<td>baz</table>', + '{<table><tr><td>foo<td>bar<td>baz</table>}', + + 'foo<u>[bar]</u>baz', + 'foo<span style="text-decoration: underline">[bar]</span>baz', + '<u>foo[bar]baz</u>', + '<u>foo[b<span style="color:blue">ar]ba</span>z</u>', + '<u>foo[b<span style="color:blue" id=foo>ar]ba</span>z</u>', + '<u>foo[b<span style="font-size:3em">ar]ba</span>z</u>', + '<u>foo[b<i>ar]ba</i>z</u>', + '<p style="text-decoration: underline">foo[bar]baz</p>', + + 'foo<s>[bar]</s>baz', + 'foo<span style="text-decoration: line-through">[bar]</span>baz', + '<s>foo[bar]baz</s>', + '<s>foo[b<span style="color:blue">ar]ba</span>z</s>', + '<s>foo[b<span style="color:blue" id=foo>ar]ba</span>z</s>', + '<s>foo[b<span style="font-size:3em">ar]ba</span>z</s>', + '<s>foo[b<i>ar]ba</i>z</s>', + '<p style="text-decoration: line-through">foo[bar]baz</p>', + + 'foo<strike>[bar]</strike>baz', + '<strike>foo[bar]baz</strike>', + '<strike>foo[b<span style="color:blue">ar]ba</span>z</strike>', + '<strike>foo[b<span style="color:blue" id=foo>ar]ba</span>z</strike>', + '<strike>foo[b<span style="font-size:3em">ar]ba</span>z</strike>', + '<strike>foo[b<i>ar]ba</i>z</strike>', + + 'foo<ins>[bar]</ins>baz', + '<ins>foo[bar]baz</ins>', + '<ins>foo[b<span style="color:blue">ar]ba</span>z</ins>', + '<ins>foo[b<span style="color:blue" id=foo>ar]ba</span>z</ins>', + '<ins>foo[b<span style="font-size:3em">ar]ba</span>z</ins>', + '<ins>foo[b<i>ar]ba</i>z</ins>', + + 'foo<del>[bar]</del>baz', + '<del>foo[bar]baz</del>', + '<del>foo[b<span style="color:blue">ar]ba</span>z</del>', + '<del>foo[b<span style="color:blue" id=foo>ar]ba</span>z</del>', + '<del>foo[b<span style="font-size:3em">ar]ba</span>z</del>', + '<del>foo[b<i>ar]ba</i>z</del>', + + 'foo<span style="text-decoration: underline line-through">[bar]</span>baz', + 'foo<span style="text-decoration: underline line-through">b[a]r</span>baz', + 'foo<s style="text-decoration: underline">[bar]</s>baz', + 'foo<s style="text-decoration: underline">b[a]r</s>baz', + 'foo<u style="text-decoration: line-through">[bar]</u>baz', + 'foo<u style="text-decoration: line-through">b[a]r</u>baz', + 'foo<s style="text-decoration: overline">[bar]</s>baz', + 'foo<s style="text-decoration: overline">b[a]r</s>baz', + 'foo<u style="text-decoration: overline">[bar]</u>baz', + 'foo<u style="text-decoration: overline">b[a]r</u>baz', + + '<p style="text-decoration: line-through">foo[bar]baz</p>', + '<p style="text-decoration: overline">foo[bar]baz</p>', + + 'foo<span class="underline">[bar]</span>baz', + 'foo<span class="underline">b[a]r</span>baz', + 'foo<span class="line-through">[bar]</span>baz', + 'foo<span class="line-through">b[a]r</span>baz', + 'foo<span class="underline-and-line-through">[bar]</span>baz', + 'foo<span class="underline-and-line-through">b[a]r</span>baz', + + // Tests for queryCommandIndeterm() and queryCommandState() + 'fo[o<u>b]ar</u>baz', + 'foo<u>ba[r</u>b]az', + 'fo[o<u>bar</u>b]az', + 'foo[<u>b]ar</u>baz', + 'foo<u>ba[r</u>]baz', + 'foo[<u>bar</u>]baz', + 'foo<u>[bar]</u>baz', + 'foo{<u>bar</u>}baz', + 'fo[o<span style=text-decoration:underline>b]ar</span>baz', + '<ins>fo[o</ins><u>b]ar</u>', + '<u>fo[o</u><ins>b]ar</ins>', + ], + //@} + unlink: [ + //@{ + 'foo[]bar', + '<p>[foo</p> <p>bar]</p>', + '<span>[foo</span> <span>bar]</span>', + '<p>[foo</p><p> <span>bar</span> </p><p>baz]</p>', + '<b>foo[]bar</b>', + '<i>foo[]bar</i>', + '<span>foo</span>{}<span>bar</span>', + '<span>foo[</span><span>]bar</span>', + 'foo[bar]baz', + 'foo[bar<b>baz]qoz</b>quz', + 'foo[bar<i>baz]qoz</i>quz', + '{<p><p> <p>foo</p>}', + + '<a href=http://www.google.com/>foo[bar]baz</a>', + '<a href=http://www.google.com/>foo[barbaz</a>}', + '{<a href=http://www.google.com/>foobar]baz</a>', + '{<a href=http://www.google.com/>foobarbaz</a>}', + '<a href=http://www.google.com/>[foobarbaz]</a>', + + 'foo<a href=http://www.google.com/>b[]ar</a>baz', + 'foo<a href=http://www.google.com/>[bar]</a>baz', + 'foo[<a href=http://www.google.com/>bar</a>]baz', + 'foo<a href=http://www.google.com/>[bar</a>baz]', + '[foo<a href=http://www.google.com/>bar]</a>baz', + '[foo<a href=http://www.google.com/>bar</a>baz]', + + '<a id=foo href=http://www.google.com/>foobar[]baz</a>', + '<a id=foo href=http://www.google.com/>foo[bar]baz</a>', + '<a id=foo href=http://www.google.com/>[foobarbaz]</a>', + 'foo<a id=foo href=http://www.google.com/>[bar]</a>baz', + 'foo[<a id=foo href=http://www.google.com/>bar</a>]baz', + '[foo<a id=foo href=http://www.google.com/>bar</a>baz]', + + '<a name=foo>foobar[]baz</a>', + '<a name=foo>foo[bar]baz</a>', + '<a name=foo>[foobarbaz]</a>', + 'foo<a name=foo>[bar]</a>baz', + 'foo[<a name=foo>bar</a>]baz', + '[foo<a name=foo>bar</a>baz]', + ], + //@} + copy: ['!foo[bar]baz'], + cut: ['!foo[bar]baz'], + defaultparagraphseparator: [ + //@{ + ['', 'foo[bar]baz'], + ['div', 'foo[bar]baz'], + ['p', 'foo[bar]baz'], + ['DIV', 'foo[bar]baz'], + ['P', 'foo[bar]baz'], + [' div ', 'foo[bar]baz'], + [' p ', 'foo[bar]baz'], + ['<div>', 'foo[bar]baz'], + ['<p>', 'foo[bar]baz'], + ['li', 'foo[bar]baz'], + ['blockquote', 'foo[bar]baz'], + ], + //@} + paste: ['!foo[bar]baz'], + selectall: ['foo[bar]baz'], + stylewithcss: [ + //@{ + ['true', 'foo[bar]baz'], + ['TRUE', 'foo[bar]baz'], + ['TrUe', 'foo[bar]baz'], + ['true ', 'foo[bar]baz'], + [' true', 'foo[bar]baz'], + ['truer', 'foo[bar]baz'], + [' true ', 'foo[bar]baz'], + [' TrUe', 'foo[bar]baz'], + ['', 'foo[bar]baz'], + [' ', 'foo[bar]baz'], + ['false', 'foo[bar]baz'], + ['FALSE', 'foo[bar]baz'], + ['FaLsE', 'foo[bar]baz'], + [' false', 'foo[bar]baz'], + ['false ', 'foo[bar]baz'], + ['falser', 'foo[bar]baz'], + ['falsé', 'foo[bar]baz'], + ], + //@} + usecss: [ + //@{ + ['true', 'foo[bar]baz'], + ['TRUE', 'foo[bar]baz'], + ['TrUe', 'foo[bar]baz'], + ['true ', 'foo[bar]baz'], + [' true', 'foo[bar]baz'], + ['truer', 'foo[bar]baz'], + [' true ', 'foo[bar]baz'], + [' TrUe', 'foo[bar]baz'], + ['', 'foo[bar]baz'], + [' ', 'foo[bar]baz'], + ['false', 'foo[bar]baz'], + ['FALSE', 'foo[bar]baz'], + ['FaLsE', 'foo[bar]baz'], + [' false', 'foo[bar]baz'], + ['false ', 'foo[bar]baz'], + ['falser', 'foo[bar]baz'], + ['falsé', 'foo[bar]baz'], + ], + //@} + quasit: ['foo[bar]baz'], + multitest: [ + //@{ + // Insertion-affecting state. Test that insertText works right, and + // test that various block commands preserve (or don't preserve) the + // state. + ['foo[]bar', 'bold', 'inserttext'], + ['foo[]bar', 'bold', 'delete'], + ['foo[]bar', 'bold', 'delete', 'inserttext'], + ['foo[]bar', 'bold', 'formatblock'], + ['foo[]bar', 'bold', 'formatblock', 'inserttext'], + ['foo[]bar', 'bold', 'forwarddelete'], + ['foo[]bar', 'bold', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'bold', 'indent'], + ['foo[]bar', 'bold', 'indent', 'inserttext'], + ['foo[]bar', 'bold', 'inserthorizontalrule'], + ['foo[]bar', 'bold', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'bold', 'inserthtml'], + ['foo[]bar', 'bold', 'inserthtml', 'inserttext'], + ['foo[]bar', 'bold', 'insertimage'], + ['foo[]bar', 'bold', 'insertimage', 'inserttext'], + ['foo[]bar', 'bold', 'insertlinebreak'], + ['foo[]bar', 'bold', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'bold', 'insertorderedlist'], + ['foo[]bar', 'bold', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'bold', 'insertparagraph'], + ['foo[]bar', 'bold', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'bold', 'insertunorderedlist'], + ['foo[]bar', 'bold', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'bold', 'justifycenter'], + ['foo[]bar', 'bold', 'justifycenter', 'inserttext'], + ['foo[]bar', 'bold', 'justifyfull'], + ['foo[]bar', 'bold', 'justifyfull', 'inserttext'], + ['foo[]bar', 'bold', 'justifyleft'], + ['foo[]bar', 'bold', 'justifyleft', 'inserttext'], + ['foo[]bar', 'bold', 'justifyright'], + ['foo[]bar', 'bold', 'justifyright', 'inserttext'], + ['foo[]bar', 'bold', 'outdent'], + ['foo[]bar', 'bold', 'outdent', 'inserttext'], + + ['foo[]bar', 'italic', 'inserttext'], + ['foo[]bar', 'italic', 'delete'], + ['foo[]bar', 'italic', 'delete', 'inserttext'], + ['foo[]bar', 'italic', 'formatblock'], + ['foo[]bar', 'italic', 'formatblock', 'inserttext'], + ['foo[]bar', 'italic', 'forwarddelete'], + ['foo[]bar', 'italic', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'italic', 'indent'], + ['foo[]bar', 'italic', 'indent', 'inserttext'], + ['foo[]bar', 'italic', 'inserthorizontalrule'], + ['foo[]bar', 'italic', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'italic', 'inserthtml'], + ['foo[]bar', 'italic', 'inserthtml', 'inserttext'], + ['foo[]bar', 'italic', 'insertimage'], + ['foo[]bar', 'italic', 'insertimage', 'inserttext'], + ['foo[]bar', 'italic', 'insertlinebreak'], + ['foo[]bar', 'italic', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'italic', 'insertorderedlist'], + ['foo[]bar', 'italic', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'italic', 'insertparagraph'], + ['foo[]bar', 'italic', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'italic', 'insertunorderedlist'], + ['foo[]bar', 'italic', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'italic', 'justifycenter'], + ['foo[]bar', 'italic', 'justifycenter', 'inserttext'], + ['foo[]bar', 'italic', 'justifyfull'], + ['foo[]bar', 'italic', 'justifyfull', 'inserttext'], + ['foo[]bar', 'italic', 'justifyleft'], + ['foo[]bar', 'italic', 'justifyleft', 'inserttext'], + ['foo[]bar', 'italic', 'justifyright'], + ['foo[]bar', 'italic', 'justifyright', 'inserttext'], + ['foo[]bar', 'italic', 'outdent'], + ['foo[]bar', 'italic', 'outdent', 'inserttext'], + + ['foo[]bar', 'strikethrough', 'inserttext'], + ['foo[]bar', 'strikethrough', 'delete'], + ['foo[]bar', 'strikethrough', 'delete', 'inserttext'], + ['foo[]bar', 'strikethrough', 'formatblock'], + ['foo[]bar', 'strikethrough', 'formatblock', 'inserttext'], + ['foo[]bar', 'strikethrough', 'forwarddelete'], + ['foo[]bar', 'strikethrough', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'strikethrough', 'indent'], + ['foo[]bar', 'strikethrough', 'indent', 'inserttext'], + ['foo[]bar', 'strikethrough', 'inserthorizontalrule'], + ['foo[]bar', 'strikethrough', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'strikethrough', 'inserthtml'], + ['foo[]bar', 'strikethrough', 'inserthtml', 'inserttext'], + ['foo[]bar', 'strikethrough', 'insertimage'], + ['foo[]bar', 'strikethrough', 'insertimage', 'inserttext'], + ['foo[]bar', 'strikethrough', 'insertlinebreak'], + ['foo[]bar', 'strikethrough', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'strikethrough', 'insertorderedlist'], + ['foo[]bar', 'strikethrough', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'strikethrough', 'insertparagraph'], + ['foo[]bar', 'strikethrough', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'strikethrough', 'insertunorderedlist'], + ['foo[]bar', 'strikethrough', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'strikethrough', 'justifycenter'], + ['foo[]bar', 'strikethrough', 'justifycenter', 'inserttext'], + ['foo[]bar', 'strikethrough', 'justifyfull'], + ['foo[]bar', 'strikethrough', 'justifyfull', 'inserttext'], + ['foo[]bar', 'strikethrough', 'justifyleft'], + ['foo[]bar', 'strikethrough', 'justifyleft', 'inserttext'], + ['foo[]bar', 'strikethrough', 'justifyright'], + ['foo[]bar', 'strikethrough', 'justifyright', 'inserttext'], + ['foo[]bar', 'strikethrough', 'outdent'], + ['foo[]bar', 'strikethrough', 'outdent', 'inserttext'], + + ['foo[]bar', 'subscript', 'inserttext'], + ['foo[]bar', 'subscript', 'delete'], + ['foo[]bar', 'subscript', 'delete', 'inserttext'], + ['foo[]bar', 'subscript', 'formatblock'], + ['foo[]bar', 'subscript', 'formatblock', 'inserttext'], + ['foo[]bar', 'subscript', 'forwarddelete'], + ['foo[]bar', 'subscript', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'subscript', 'indent'], + ['foo[]bar', 'subscript', 'indent', 'inserttext'], + ['foo[]bar', 'subscript', 'inserthorizontalrule'], + ['foo[]bar', 'subscript', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'subscript', 'inserthtml'], + ['foo[]bar', 'subscript', 'inserthtml', 'inserttext'], + ['foo[]bar', 'subscript', 'insertimage'], + ['foo[]bar', 'subscript', 'insertimage', 'inserttext'], + ['foo[]bar', 'subscript', 'insertlinebreak'], + ['foo[]bar', 'subscript', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'subscript', 'insertorderedlist'], + ['foo[]bar', 'subscript', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'subscript', 'insertparagraph'], + ['foo[]bar', 'subscript', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'subscript', 'insertunorderedlist'], + ['foo[]bar', 'subscript', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'subscript', 'justifycenter'], + ['foo[]bar', 'subscript', 'justifycenter', 'inserttext'], + ['foo[]bar', 'subscript', 'justifyfull'], + ['foo[]bar', 'subscript', 'justifyfull', 'inserttext'], + ['foo[]bar', 'subscript', 'justifyleft'], + ['foo[]bar', 'subscript', 'justifyleft', 'inserttext'], + ['foo[]bar', 'subscript', 'justifyright'], + ['foo[]bar', 'subscript', 'justifyright', 'inserttext'], + ['foo[]bar', 'subscript', 'outdent'], + ['foo[]bar', 'subscript', 'outdent', 'inserttext'], + + ['foo[]bar', 'superscript', 'inserttext'], + ['foo[]bar', 'superscript', 'delete'], + ['foo[]bar', 'superscript', 'delete', 'inserttext'], + ['foo[]bar', 'superscript', 'formatblock'], + ['foo[]bar', 'superscript', 'formatblock', 'inserttext'], + ['foo[]bar', 'superscript', 'forwarddelete'], + ['foo[]bar', 'superscript', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'superscript', 'indent'], + ['foo[]bar', 'superscript', 'indent', 'inserttext'], + ['foo[]bar', 'superscript', 'inserthorizontalrule'], + ['foo[]bar', 'superscript', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'superscript', 'inserthtml'], + ['foo[]bar', 'superscript', 'inserthtml', 'inserttext'], + ['foo[]bar', 'superscript', 'insertimage'], + ['foo[]bar', 'superscript', 'insertimage', 'inserttext'], + ['foo[]bar', 'superscript', 'insertlinebreak'], + ['foo[]bar', 'superscript', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'superscript', 'insertorderedlist'], + ['foo[]bar', 'superscript', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'superscript', 'insertparagraph'], + ['foo[]bar', 'superscript', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'superscript', 'insertunorderedlist'], + ['foo[]bar', 'superscript', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'superscript', 'justifycenter'], + ['foo[]bar', 'superscript', 'justifycenter', 'inserttext'], + ['foo[]bar', 'superscript', 'justifyfull'], + ['foo[]bar', 'superscript', 'justifyfull', 'inserttext'], + ['foo[]bar', 'superscript', 'justifyleft'], + ['foo[]bar', 'superscript', 'justifyleft', 'inserttext'], + ['foo[]bar', 'superscript', 'justifyright'], + ['foo[]bar', 'superscript', 'justifyright', 'inserttext'], + ['foo[]bar', 'superscript', 'outdent'], + ['foo[]bar', 'superscript', 'outdent', 'inserttext'], + + ['foo[]bar', 'underline', 'inserttext'], + ['foo[]bar', 'underline', 'delete'], + ['foo[]bar', 'underline', 'delete', 'inserttext'], + ['foo[]bar', 'underline', 'formatblock'], + ['foo[]bar', 'underline', 'formatblock', 'inserttext'], + ['foo[]bar', 'underline', 'forwarddelete'], + ['foo[]bar', 'underline', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'underline', 'indent'], + ['foo[]bar', 'underline', 'indent', 'inserttext'], + ['foo[]bar', 'underline', 'inserthorizontalrule'], + ['foo[]bar', 'underline', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'underline', 'inserthtml'], + ['foo[]bar', 'underline', 'inserthtml', 'inserttext'], + ['foo[]bar', 'underline', 'insertimage'], + ['foo[]bar', 'underline', 'insertimage', 'inserttext'], + ['foo[]bar', 'underline', 'insertlinebreak'], + ['foo[]bar', 'underline', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'underline', 'insertorderedlist'], + ['foo[]bar', 'underline', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'underline', 'insertparagraph'], + ['foo[]bar', 'underline', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'underline', 'insertunorderedlist'], + ['foo[]bar', 'underline', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'underline', 'justifycenter'], + ['foo[]bar', 'underline', 'justifycenter', 'inserttext'], + ['foo[]bar', 'underline', 'justifyfull'], + ['foo[]bar', 'underline', 'justifyfull', 'inserttext'], + ['foo[]bar', 'underline', 'justifyleft'], + ['foo[]bar', 'underline', 'justifyleft', 'inserttext'], + ['foo[]bar', 'underline', 'justifyright'], + ['foo[]bar', 'underline', 'justifyright', 'inserttext'], + ['foo[]bar', 'underline', 'outdent'], + ['foo[]bar', 'underline', 'outdent', 'inserttext'], + + // Insertion-affecting value. Test that insertText works right, and + // test that various block commands preserve (or don't preserve) the + // value. + ['foo[]bar', 'backcolor', 'inserttext'], + ['foo[]bar', 'backcolor', 'delete'], + ['foo[]bar', 'backcolor', 'delete', 'inserttext'], + ['foo[]bar', 'backcolor', 'formatblock'], + ['foo[]bar', 'backcolor', 'formatblock', 'inserttext'], + ['foo[]bar', 'backcolor', 'forwarddelete'], + ['foo[]bar', 'backcolor', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'backcolor', 'indent'], + ['foo[]bar', 'backcolor', 'indent', 'inserttext'], + ['foo[]bar', 'backcolor', 'inserthorizontalrule'], + ['foo[]bar', 'backcolor', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'backcolor', 'inserthtml'], + ['foo[]bar', 'backcolor', 'inserthtml', 'inserttext'], + ['foo[]bar', 'backcolor', 'insertimage'], + ['foo[]bar', 'backcolor', 'insertimage', 'inserttext'], + ['foo[]bar', 'backcolor', 'insertlinebreak'], + ['foo[]bar', 'backcolor', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'backcolor', 'insertorderedlist'], + ['foo[]bar', 'backcolor', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'backcolor', 'insertparagraph'], + ['foo[]bar', 'backcolor', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'backcolor', 'insertunorderedlist'], + ['foo[]bar', 'backcolor', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'backcolor', 'justifycenter'], + ['foo[]bar', 'backcolor', 'justifycenter', 'inserttext'], + ['foo[]bar', 'backcolor', 'justifyfull'], + ['foo[]bar', 'backcolor', 'justifyfull', 'inserttext'], + ['foo[]bar', 'backcolor', 'justifyleft'], + ['foo[]bar', 'backcolor', 'justifyleft', 'inserttext'], + ['foo[]bar', 'backcolor', 'justifyright'], + ['foo[]bar', 'backcolor', 'justifyright', 'inserttext'], + ['foo[]bar', 'backcolor', 'outdent'], + ['foo[]bar', 'backcolor', 'outdent', 'inserttext'], + + ['foo[]bar', 'createlink', 'inserttext'], + ['foo[]bar', 'createlink', 'delete'], + ['foo[]bar', 'createlink', 'delete', 'inserttext'], + ['foo[]bar', 'createlink', 'formatblock'], + ['foo[]bar', 'createlink', 'formatblock', 'inserttext'], + ['foo[]bar', 'createlink', 'forwarddelete'], + ['foo[]bar', 'createlink', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'createlink', 'indent'], + ['foo[]bar', 'createlink', 'indent', 'inserttext'], + ['foo[]bar', 'createlink', 'inserthorizontalrule'], + ['foo[]bar', 'createlink', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'createlink', 'inserthtml'], + ['foo[]bar', 'createlink', 'inserthtml', 'inserttext'], + ['foo[]bar', 'createlink', 'insertimage'], + ['foo[]bar', 'createlink', 'insertimage', 'inserttext'], + ['foo[]bar', 'createlink', 'insertlinebreak'], + ['foo[]bar', 'createlink', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'createlink', 'insertorderedlist'], + ['foo[]bar', 'createlink', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'createlink', 'insertparagraph'], + ['foo[]bar', 'createlink', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'createlink', 'insertunorderedlist'], + ['foo[]bar', 'createlink', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'createlink', 'justifycenter'], + ['foo[]bar', 'createlink', 'justifycenter', 'inserttext'], + ['foo[]bar', 'createlink', 'justifyfull'], + ['foo[]bar', 'createlink', 'justifyfull', 'inserttext'], + ['foo[]bar', 'createlink', 'justifyleft'], + ['foo[]bar', 'createlink', 'justifyleft', 'inserttext'], + ['foo[]bar', 'createlink', 'justifyright'], + ['foo[]bar', 'createlink', 'justifyright', 'inserttext'], + ['foo[]bar', 'createlink', 'outdent'], + ['foo[]bar', 'createlink', 'outdent', 'inserttext'], + + ['foo[]bar', 'fontname', 'inserttext'], + ['foo[]bar', 'fontname', 'delete'], + ['foo[]bar', 'fontname', 'delete', 'inserttext'], + ['foo[]bar', 'fontname', 'formatblock'], + ['foo[]bar', 'fontname', 'formatblock', 'inserttext'], + ['foo[]bar', 'fontname', 'forwarddelete'], + ['foo[]bar', 'fontname', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'fontname', 'indent'], + ['foo[]bar', 'fontname', 'indent', 'inserttext'], + ['foo[]bar', 'fontname', 'inserthorizontalrule'], + ['foo[]bar', 'fontname', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'fontname', 'inserthtml'], + ['foo[]bar', 'fontname', 'inserthtml', 'inserttext'], + ['foo[]bar', 'fontname', 'insertimage'], + ['foo[]bar', 'fontname', 'insertimage', 'inserttext'], + ['foo[]bar', 'fontname', 'insertlinebreak'], + ['foo[]bar', 'fontname', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'fontname', 'insertorderedlist'], + ['foo[]bar', 'fontname', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'fontname', 'insertparagraph'], + ['foo[]bar', 'fontname', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'fontname', 'insertunorderedlist'], + ['foo[]bar', 'fontname', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'fontname', 'justifycenter'], + ['foo[]bar', 'fontname', 'justifycenter', 'inserttext'], + ['foo[]bar', 'fontname', 'justifyfull'], + ['foo[]bar', 'fontname', 'justifyfull', 'inserttext'], + ['foo[]bar', 'fontname', 'justifyleft'], + ['foo[]bar', 'fontname', 'justifyleft', 'inserttext'], + ['foo[]bar', 'fontname', 'justifyright'], + ['foo[]bar', 'fontname', 'justifyright', 'inserttext'], + ['foo[]bar', 'fontname', 'outdent'], + ['foo[]bar', 'fontname', 'outdent', 'inserttext'], + + ['foo[]bar', 'fontsize', 'inserttext'], + ['foo[]bar', 'fontsize', 'delete'], + ['foo[]bar', 'fontsize', 'delete', 'inserttext'], + ['foo[]bar', 'fontsize', 'formatblock'], + ['foo[]bar', 'fontsize', 'formatblock', 'inserttext'], + ['foo[]bar', 'fontsize', 'forwarddelete'], + ['foo[]bar', 'fontsize', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'fontsize', 'indent'], + ['foo[]bar', 'fontsize', 'indent', 'inserttext'], + ['foo[]bar', 'fontsize', 'inserthorizontalrule'], + ['foo[]bar', 'fontsize', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'fontsize', 'inserthtml'], + ['foo[]bar', 'fontsize', 'inserthtml', 'inserttext'], + ['foo[]bar', 'fontsize', 'insertimage'], + ['foo[]bar', 'fontsize', 'insertimage', 'inserttext'], + ['foo[]bar', 'fontsize', 'insertlinebreak'], + ['foo[]bar', 'fontsize', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'fontsize', 'insertorderedlist'], + ['foo[]bar', 'fontsize', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'fontsize', 'insertparagraph'], + ['foo[]bar', 'fontsize', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'fontsize', 'insertunorderedlist'], + ['foo[]bar', 'fontsize', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'fontsize', 'justifycenter'], + ['foo[]bar', 'fontsize', 'justifycenter', 'inserttext'], + ['foo[]bar', 'fontsize', 'justifyfull'], + ['foo[]bar', 'fontsize', 'justifyfull', 'inserttext'], + ['foo[]bar', 'fontsize', 'justifyleft'], + ['foo[]bar', 'fontsize', 'justifyleft', 'inserttext'], + ['foo[]bar', 'fontsize', 'justifyright'], + ['foo[]bar', 'fontsize', 'justifyright', 'inserttext'], + ['foo[]bar', 'fontsize', 'outdent'], + ['foo[]bar', 'fontsize', 'outdent', 'inserttext'], + + ['foo[]bar', 'forecolor', 'inserttext'], + ['foo[]bar', 'forecolor', 'delete'], + ['foo[]bar', 'forecolor', 'delete', 'inserttext'], + ['foo[]bar', 'forecolor', 'formatblock'], + ['foo[]bar', 'forecolor', 'formatblock', 'inserttext'], + ['foo[]bar', 'forecolor', 'forwarddelete'], + ['foo[]bar', 'forecolor', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'forecolor', 'indent'], + ['foo[]bar', 'forecolor', 'indent', 'inserttext'], + ['foo[]bar', 'forecolor', 'inserthorizontalrule'], + ['foo[]bar', 'forecolor', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'forecolor', 'inserthtml'], + ['foo[]bar', 'forecolor', 'inserthtml', 'inserttext'], + ['foo[]bar', 'forecolor', 'insertimage'], + ['foo[]bar', 'forecolor', 'insertimage', 'inserttext'], + ['foo[]bar', 'forecolor', 'insertlinebreak'], + ['foo[]bar', 'forecolor', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'forecolor', 'insertorderedlist'], + ['foo[]bar', 'forecolor', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'forecolor', 'insertparagraph'], + ['foo[]bar', 'forecolor', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'forecolor', 'insertunorderedlist'], + ['foo[]bar', 'forecolor', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'forecolor', 'justifycenter'], + ['foo[]bar', 'forecolor', 'justifycenter', 'inserttext'], + ['foo[]bar', 'forecolor', 'justifyfull'], + ['foo[]bar', 'forecolor', 'justifyfull', 'inserttext'], + ['foo[]bar', 'forecolor', 'justifyleft'], + ['foo[]bar', 'forecolor', 'justifyleft', 'inserttext'], + ['foo[]bar', 'forecolor', 'justifyright'], + ['foo[]bar', 'forecolor', 'justifyright', 'inserttext'], + ['foo[]bar', 'forecolor', 'outdent'], + ['foo[]bar', 'forecolor', 'outdent', 'inserttext'], + + ['foo[]bar', 'hilitecolor', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'delete'], + ['foo[]bar', 'hilitecolor', 'delete', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'formatblock'], + ['foo[]bar', 'hilitecolor', 'formatblock', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'forwarddelete'], + ['foo[]bar', 'hilitecolor', 'forwarddelete', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'indent'], + ['foo[]bar', 'hilitecolor', 'indent', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'inserthorizontalrule'], + ['foo[]bar', 'hilitecolor', 'inserthorizontalrule', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'inserthtml'], + ['foo[]bar', 'hilitecolor', 'inserthtml', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'insertimage'], + ['foo[]bar', 'hilitecolor', 'insertimage', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'insertlinebreak'], + ['foo[]bar', 'hilitecolor', 'insertlinebreak', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'insertorderedlist'], + ['foo[]bar', 'hilitecolor', 'insertorderedlist', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'insertparagraph'], + ['foo[]bar', 'hilitecolor', 'insertparagraph', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'insertunorderedlist'], + ['foo[]bar', 'hilitecolor', 'insertunorderedlist', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'justifycenter'], + ['foo[]bar', 'hilitecolor', 'justifycenter', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'justifyfull'], + ['foo[]bar', 'hilitecolor', 'justifyfull', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'justifyleft'], + ['foo[]bar', 'hilitecolor', 'justifyleft', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'justifyright'], + ['foo[]bar', 'hilitecolor', 'justifyright', 'inserttext'], + ['foo[]bar', 'hilitecolor', 'outdent'], + ['foo[]bar', 'hilitecolor', 'outdent', 'inserttext'], + + // Test things that interfere with each other + ['foo[]bar', 'superscript', 'subscript', 'inserttext'], + ['foo[]bar', 'subscript', 'superscript', 'inserttext'], + + ['foo[]bar', 'createlink', ['forecolor', '#0000FF'], 'inserttext'], + ['foo[]bar', ['forecolor', '#0000FF'], 'createlink', 'inserttext'], + ['foo[]bar', 'createlink', ['forecolor', 'blue'], 'inserttext'], + ['foo[]bar', ['forecolor', 'blue'], 'createlink', 'inserttext'], + ['foo[]bar', 'createlink', ['forecolor', 'brown'], 'inserttext'], + ['foo[]bar', ['forecolor', 'brown'], 'createlink', 'inserttext'], + ['foo[]bar', 'createlink', ['forecolor', 'black'], 'inserttext'], + ['foo[]bar', ['forecolor', 'black'], 'createlink', 'inserttext'], + ['foo[]bar', 'createlink', 'underline', 'inserttext'], + ['foo[]bar', 'underline', 'createlink', 'inserttext'], + ['foo[]bar', 'createlink', 'underline', 'underline', 'inserttext'], + ['foo[]bar', 'underline', 'underline', 'createlink', 'inserttext'], + + ['foo[]bar', 'subscript', ['fontsize', '2'], 'inserttext'], + ['foo[]bar', ['fontsize', '2'], 'subscript', 'inserttext'], + ['foo[]bar', 'subscript', ['fontsize', '3'], 'inserttext'], + ['foo[]bar', ['fontsize', '3'], 'subscript', 'inserttext'], + + ['foo[]bar', ['hilitecolor', 'aqua'], ['backcolor', 'tan'], 'inserttext'], + ['foo[]bar', ['backcolor', 'tan'], ['hilitecolor', 'aqua'], 'inserttext'], + + + // The following are all just inserttext tests that we took from there, + // but we first backspace the selected text instead of letting + // inserttext handle it. This tests that deletion correctly sets + // overrides. + ['foo<b>[bar]</b>baz', 'delete', 'inserttext'], + ['foo<i>[bar]</i>baz', 'delete', 'inserttext'], + ['foo<s>[bar]</s>baz', 'delete', 'inserttext'], + ['foo<sub>[bar]</sub>baz', 'delete', 'inserttext'], + ['foo<sup>[bar]</sup>baz', 'delete', 'inserttext'], + ['foo<u>[bar]</u>baz', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com>[bar]</a>baz', 'delete', 'inserttext'], + ['foo<font face=sans-serif>[bar]</font>baz', 'delete', 'inserttext'], + ['foo<font size=4>[bar]</font>baz', 'delete', 'inserttext'], + ['foo<font color=#0000FF>[bar]</font>baz', 'delete', 'inserttext'], + ['foo<span style=background-color:#00FFFF>[bar]</span>baz', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><font color=blue>[bar]</font></a>baz', 'delete', 'inserttext'], + ['foo<font color=blue><a href=http://www.google.com>[bar]</a></font>baz', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><font color=brown>[bar]</font></a>baz', 'delete', 'inserttext'], + ['foo<font color=brown><a href=http://www.google.com>[bar]</a></font>baz', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><font color=black>[bar]</font></a>baz', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><u>[bar]</u></a>baz', 'delete', 'inserttext'], + ['foo<u><a href=http://www.google.com>[bar]</a></u>baz', 'delete', 'inserttext'], + ['foo<sub><font size=2>[bar]</font></sub>baz', 'delete', 'inserttext'], + ['foo<font size=2><sub>[bar]</sub></font>baz', 'delete', 'inserttext'], + ['foo<sub><font size=3>[bar]</font></sub>baz', 'delete', 'inserttext'], + ['foo<font size=3><sub>[bar]</sub></font>baz', 'delete', 'inserttext'], + + // Now repeat but with different selections. + ['[foo<b>bar]</b>baz', 'delete', 'inserttext'], + ['[foo<i>bar]</i>baz', 'delete', 'inserttext'], + ['[foo<s>bar]</s>baz', 'delete', 'inserttext'], + ['[foo<sub>bar]</sub>baz', 'delete', 'inserttext'], + ['[foo<sup>bar]</sup>baz', 'delete', 'inserttext'], + ['[foo<u>bar]</u>baz', 'delete', 'inserttext'], + ['[foo<a href=http://www.google.com>bar]</a>baz', 'delete', 'inserttext'], + ['[foo<font face=sans-serif>bar]</font>baz', 'delete', 'inserttext'], + ['[foo<font size=4>bar]</font>baz', 'delete', 'inserttext'], + ['[foo<font color=#0000FF>bar]</font>baz', 'delete', 'inserttext'], + ['[foo<span style=background-color:#00FFFF>bar]</span>baz', 'delete', 'inserttext'], + ['[foo<a href=http://www.google.com><font color=blue>bar]</font></a>baz', 'delete', 'inserttext'], + ['[foo<font color=blue><a href=http://www.google.com>bar]</a></font>baz', 'delete', 'inserttext'], + ['[foo<a href=http://www.google.com><font color=brown>bar]</font></a>baz', 'delete', 'inserttext'], + ['[foo<font color=brown><a href=http://www.google.com>bar]</a></font>baz', 'delete', 'inserttext'], + ['[foo<a href=http://www.google.com><font color=black>bar]</font></a>baz', 'delete', 'inserttext'], + ['[foo<a href=http://www.google.com><u>bar]</u></a>baz', 'delete', 'inserttext'], + ['[foo<u><a href=http://www.google.com>bar]</a></u>baz', 'delete', 'inserttext'], + ['[foo<sub><font size=2>bar]</font></sub>baz', 'delete', 'inserttext'], + ['[foo<font size=2><sub>bar]</sub></font>baz', 'delete', 'inserttext'], + ['[foo<sub><font size=3>bar]</font></sub>baz', 'delete', 'inserttext'], + ['[foo<font size=3><sub>bar]</sub></font>baz', 'delete', 'inserttext'], + + ['foo<b>[bar</b>baz]', 'delete', 'inserttext'], + ['foo<i>[bar</i>baz]', 'delete', 'inserttext'], + ['foo<s>[bar</s>baz]', 'delete', 'inserttext'], + ['foo<sub>[bar</sub>baz]', 'delete', 'inserttext'], + ['foo<sup>[bar</sup>baz]', 'delete', 'inserttext'], + ['foo<u>[bar</u>baz]', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com>[bar</a>baz]', 'delete', 'inserttext'], + ['foo<font face=sans-serif>[bar</font>baz]', 'delete', 'inserttext'], + ['foo<font size=4>[bar</font>baz]', 'delete', 'inserttext'], + ['foo<font color=#0000FF>[bar</font>baz]', 'delete', 'inserttext'], + ['foo<span style=background-color:#00FFFF>[bar</span>baz]', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><font color=blue>[bar</font></a>baz]', 'delete', 'inserttext'], + ['foo<font color=blue><a href=http://www.google.com>[bar</a></font>baz]', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><font color=brown>[bar</font></a>baz]', 'delete', 'inserttext'], + ['foo<font color=brown><a href=http://www.google.com>[bar</a></font>baz]', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><font color=black>[bar</font></a>baz]', 'delete', 'inserttext'], + ['foo<a href=http://www.google.com><u>[bar</u></a>baz]', 'delete', 'inserttext'], + ['foo<u><a href=http://www.google.com>[bar</a></u>baz]', 'delete', 'inserttext'], + ['foo<sub><font size=2>[bar</font></sub>baz]', 'delete', 'inserttext'], + ['foo<font size=2><sub>[bar</sub></font>baz]', 'delete', 'inserttext'], + ['foo<sub><font size=3>[bar</font></sub>baz]', 'delete', 'inserttext'], + ['foo<font size=3><sub>[bar</sub></font>baz]', 'delete', 'inserttext'], + + // https://bugs.webkit.org/show_bug.cgi?id=19702 + ['<blockquote><font color=blue>[foo]</font></blockquote>', 'delete', 'inserttext'], + ], + //@} +}; +tests.backcolor = tests.hilitecolor; +tests.insertlinebreak = tests.insertparagraph; + +// Tests that start with "!" are believed to have bogus results and should be +// skipped until the relevant bugs are fixed. +var badTests = {}; +(function(){ + for (var command in tests) { + badTests[command] = []; + for (var i = 0; i < tests[command].length; i++) { + var test = tests[command][i]; + if (typeof test == "string" && test[0] == "!") { + test = test.slice(1); + tests[command][i] = test; + badTests[command].push(test); + } + if (typeof test == "object" && test[0][0] == "!") { + test = [test[0].slice(1)].concat(test.slice(1)); + tests[command][i] = test; + badTests[command].push(test); + } + } + } +})(); + +var defaultValues = { +//@{ + backcolor: "#00FFFF", + createlink: "http://www.google.com/", + fontname: "sans-serif", + fontsize: "4", + forecolor: "#0000FF", + formatblock: "<div>", + hilitecolor: "#00FFFF", + inserthorizontalrule: "", + inserthtml: "ab<b>c</b>d", + insertimage: "/img/lion.svg", + inserttext: "a", + defaultparagraphseparator: "div", + stylewithcss: "true", + usecss: "true", +}; +//@} + +var notes = { +//@{ + fontname: 'Note that the body\'s font-family is "serif".', +}; +//@} + +var doubleTestingCommands = [ +//@{ + "backcolor", + "bold", + "fontname", + "fontsize", + "forecolor", + "italic", + "justifycenter", + "justifyfull", + "justifyleft", + "justifyright", + "strikethrough", + "stylewithcss", + "subscript", + "superscript", + "underline", + "usecss", +]; +//@} + +function prettyPrint(value) { +//@{ + // Partly stolen from testharness.js + if (typeof value != "string") { + return String(value); + } + + value = value.replace(/\\/g, "\\\\") + .replace(/"/g, '\\"'); + + for (var i = 0; i < 32; i++) { + var replace = "\\"; + switch (i) { + case 0: replace += "0"; break; + case 1: replace += "x01"; break; + case 2: replace += "x02"; break; + case 3: replace += "x03"; break; + case 4: replace += "x04"; break; + case 5: replace += "x05"; break; + case 6: replace += "x06"; break; + case 7: replace += "x07"; break; + case 8: replace += "b"; break; + case 9: replace += "t"; break; + case 10: replace += "n"; break; + case 11: replace += "v"; break; + case 12: replace += "f"; break; + case 13: replace += "r"; break; + case 14: replace += "x0e"; break; + case 15: replace += "x0f"; break; + case 16: replace += "x10"; break; + case 17: replace += "x11"; break; + case 18: replace += "x12"; break; + case 19: replace += "x13"; break; + case 20: replace += "x14"; break; + case 21: replace += "x15"; break; + case 22: replace += "x16"; break; + case 23: replace += "x17"; break; + case 24: replace += "x18"; break; + case 25: replace += "x19"; break; + case 26: replace += "x1a"; break; + case 27: replace += "x1b"; break; + case 28: replace += "x1c"; break; + case 29: replace += "x1d"; break; + case 30: replace += "x1e"; break; + case 31: replace += "x1f"; break; + } + value = value.replace(new RegExp(String.fromCharCode(i), "g"), replace); + } + return '"' + value + '"'; +} +//@} + +function doSetup(selector, idx) { +//@{ + var table = document.querySelectorAll(selector)[idx]; + + var tr = document.createElement("tr"); + table.firstChild.appendChild(tr); + tr.className = (tr.className + " active").trim(); + + return tr; +} +//@} + +function queryOutputHelper(beforeIndeterm, beforeState, beforeValue, + afterIndeterm, afterState, afterValue, + command, value) { +//@{ + var frag = document.createDocumentFragment(); + var beforeDiv = document.createElement("div"); + var afterDiv = document.createElement("div"); + frag.appendChild(beforeDiv); + frag.appendChild(afterDiv); + beforeDiv.className = afterDiv.className = "extra-results"; + beforeDiv.textContent = "Before: "; + afterDiv.textContent = "After: "; + + beforeDiv.appendChild(document.createElement("span")); + afterDiv.appendChild(document.createElement("span")); + if ("indeterm" in commands[command]) { + // We only know it has to be either true or false. + if (beforeIndeterm !== true && beforeIndeterm !== false) { + beforeDiv.lastChild.className = "bad-result"; + } + } else { + // It always has to be false. + beforeDiv.lastChild.className = beforeIndeterm === false + ? "good-result" + : "bad-result"; + } + // After running the command, indeterminate must always be false, except if + // it's an exception, or if it's insert*list and the state was true to + // begin with. And we can't help strikethrough/underline. + if ((/^insert(un)?orderedlist$/.test(command) && beforeState) + || command == "strikethrough" + || command == "underline") { + if (afterIndeterm !== true && afterIndeterm !== false) { + afterDiv.lastChild.className = "bad-result"; + } + } else { + afterDiv.lastChild.className = + afterIndeterm === false + ? "good-result" + : "bad-result"; + } + beforeDiv.lastChild.textContent = "indeterm " + prettyPrint(beforeIndeterm); + afterDiv.lastChild.textContent = "indeterm " + prettyPrint(afterIndeterm); + + beforeDiv.appendChild(document.createTextNode(", ")); + afterDiv.appendChild(document.createTextNode(", ")); + + beforeDiv.appendChild(document.createElement("span")); + afterDiv.appendChild(document.createElement("span")); + if (/^insert(un)?orderedlist$/.test(command)) { + // If the before state is true, the after state could be either true or + // false. But if the before state is false, the after state has to be + // true. + if (beforeState !== true && beforeState !== false) { + beforeDiv.lastChild.className = "bad-result"; + } + if (!beforeState) { + afterDiv.lastChild.className = afterState === true + ? "good-result" + : "bad-result"; + } else if (afterState !== true && afterState !== false) { + afterDiv.lastChild.className = "bad-result"; + } + } else if (/^justify(center|full|left|right)$/.test(command)) { + // We don't know about the before state, but the after state is always + // supposed to be true. + if (beforeState !== true && beforeState !== false) { + beforeDiv.lastChild.className = "bad-result"; + } + afterDiv.lastChild.className = afterState === true + ? "good-result" + : "bad-result"; + } else if (command == "strikethrough" || command == "underline") { + // The only thing we can say is the before/after states need to be + // either true or false. + if (beforeState !== true && beforeState !== false) { + beforeDiv.lastChild.className = "bad-result"; + } + if (afterState !== true && afterState !== false) { + afterDiv.lastChild.className = "bad-result"; + } + } else { + // The general rule is it must flip the state, unless there's no state + // defined, in which case it should always be false. + beforeDiv.lastChild.className = + afterDiv.lastChild.className = + ("state" in commands[command] && typeof beforeState == "boolean" && typeof afterState == "boolean" && beforeState === !afterState) + || (!("state" in commands[command]) && beforeState === false && afterState === false) + ? "good-result" + : "bad-result"; + } + beforeDiv.lastChild.textContent = "state " + prettyPrint(beforeState); + afterDiv.lastChild.textContent = "state " + prettyPrint(afterState); + + beforeDiv.appendChild(document.createTextNode(", ")); + afterDiv.appendChild(document.createTextNode(", ")); + + beforeDiv.appendChild(document.createElement("span")); + afterDiv.appendChild(document.createElement("span")); + + // Direct equality comparison doesn't make sense in a bunch of cases. + if (command == "backcolor" || command == "forecolor" || command == "hilitecolor") { + if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) { + value = "#" + value; + } + } else if (command == "fontsize") { + value = normalizeFontSize(value); + if (value !== null) { + value = String(cssSizeToLegacy(value)); + } + } else if (command == "formatblock") { + value = value.replace(/^<(.*)>$/, "$1").toLowerCase(); + } else if (command == "defaultparagraphseparator") { + value = value.toLowerCase(); + if (value != "p" && value != "div") { + value = ""; + } + } + + if (((command == "backcolor" || command == "forecolor" || command == "hilitecolor") && value.toLowerCase() == "currentcolor") + || (command == "fontsize" && value === null) + || (command == "formatblock" && formattableBlockNames.indexOf(value.replace(/^<(.*)>$/, "$1").trim()) == -1) + || (command == "defaultparagraphseparator" && value == "")) { + afterDiv.lastChild.className = beforeValue === afterValue + ? "good-result" + : "bad-result"; + } else if (/^justify(center|full|left|right)$/.test(command)) { + // We know there are only four correct values beforehand, and afterward + // the value has to be the one we set. + if (!/^(center|justify|left|right)$/.test(beforeValue)) { + beforeDiv.lastChild.className = "bad-result"; + } + var expectedValue = command == "justifyfull" + ? "justify" + : command.replace("justify", ""); + afterDiv.lastChild.className = afterValue === expectedValue + ? "good-result" + : "bad-result"; + } else if (!("value" in commands[command])) { + // If it's not defined we want "". + beforeDiv.lastChild.className = beforeValue === "" + ? "good-result" + : "bad-result"; + afterDiv.lastChild.className = afterValue === "" + ? "good-result" + : "bad-result"; + } else { + // And in all other cases, the value afterwards has to be the one we + // set. + afterDiv.lastChild.className = + areEquivalentValues(command, afterValue, value) + ? "good-result" + : "bad-result"; + } + beforeDiv.lastChild.textContent = "value " + prettyPrint(beforeValue); + afterDiv.lastChild.textContent = "value " + prettyPrint(afterValue); + + return frag; +} +//@} + +function normalizeTest(command, test, styleWithCss) { +//@{ + // Our standard format for test processing is: + // [input HTML, + // [command1, value1, optional_name_mod], + // [command2, value2, optional_name_mod], ...] + // Where `optional_name_mod` is an optionally-specified string used when + // generating test names (necessary to ensure uniqueness for command + // sequences that use the same command multiple times). This format is + // verbose, so we actually use three different formats in the tests and + // multiTests arrays: + // + // 1) Plain string giving the input HTML. The command is implicit from the + // key of the tests array. If the command takes values, the value is given + // by defaultValues, otherwise it's "". Has to be converted to + // [input HTML, [command, value]. + // + // 2) Two-element array [value, input HTML]. Has to be converted to + // [input HTML, [command, value]]. + // + // 3) An element of multiTests. This just has to have values filled in. + // + // Optionally, a styleWithCss argument can be passed, either true or false. + // If it is, we'll prepend a styleWithCss invocation. + if (command == "multitest") { + if (typeof test == "string") { + test = JSON.parse(test); + } + for (var i = 1; i < test.length; i++) { + if (typeof test[i] == "string" + && test[i] in defaultValues) { + test[i] = [test[i], defaultValues[test[i]]]; + } else if (typeof test[i] == "string") { + test[i] = [test[i], ""]; + } + } + return test; + } + + if (typeof test == "string") { + if (command in defaultValues) { + test = [test, [command, defaultValues[command]]]; + } else { + test = [test, [command, ""]]; + } + } else if (test.length == 2) { + test = [test[1], [command, String(test[0])]]; + } + + if (styleWithCss !== undefined) { + test.splice(1, 0, ["stylewithcss", String(styleWithCss)]); + } + + return test; +} +//@} + +function doInputCell(tr, test, command) { +//@{ + var testHtml = test[0]; + + var msg = null; + if (command in defaultValues) { + // Single command with a value, possibly with a styleWithCss stuck + // before. We don't need to specify the command itself, since this + // presumably isn't in multiTests, so the command is already given by + // the section header. + msg = 'value: ' + prettyPrint(test[test.length - 1][1]); + } else if (command == "multitest") { + // Uses a different input format + msg = JSON.stringify(test); + } + var inputCell = document.createElement("td"); + inputCell.innerHTML = "<div></div><div></div>"; + inputCell.firstChild.innerHTML = testHtml; + inputCell.lastChild.textContent = inputCell.firstChild.innerHTML; + if (msg !== null) { + inputCell.lastChild.textContent += " (" + msg + ")"; + } + + tr.appendChild(inputCell); +} +//@} + +function doSpecCell(tr, test, command) { +//@{ + var specCell = document.createElement("td"); + tr.appendChild(specCell); + try { + var points = setupCell(specCell, test[0]); + var range = document.createRange(); + range.setStart(points[0], points[1]); + range.setEnd(points[2], points[3]); + // The points might be backwards + if (range.collapsed) { + range.setEnd(points[0], points[1]); + } + specCell.firstChild.contentEditable = "true"; + specCell.firstChild.spellcheck = false; + + if (command != "multitest") { + try { var beforeIndeterm = myQueryCommandIndeterm(command, range) } + catch(e) { beforeIndeterm = "Exception" } + try { var beforeState = myQueryCommandState(command, range) } + catch(e) { beforeState = "Exception" } + try { var beforeValue = myQueryCommandValue(command, range) } + catch(e) { beforeValue = "Exception" } + } + + for (var i = 1; i < test.length; i++) { + myExecCommand(test[i][0], false, test[i][1], range); + } + + if (command != "multitest") { + try { var afterIndeterm = myQueryCommandIndeterm(command, range) } + catch(e) { afterIndeterm = "Exception" } + try { var afterState = myQueryCommandState(command, range) } + catch(e) { afterState = "Exception" } + try { var afterValue = myQueryCommandValue(command, range) } + catch(e) { afterValue = "Exception" } + } + + specCell.firstChild.contentEditable = "inherit"; + specCell.firstChild.removeAttribute("spellcheck"); + var compareDiv1 = specCell.firstChild.cloneNode(true); + + // Now do various sanity checks, and throw if they're violated. First + // just count children: + if (specCell.childNodes.length != 2) { + throw "The cell didn't have two children. Did something spill outside the test div?"; + } + + // Now verify that the DOM serializes. + compareDiv1.normalize(); + var compareDiv2 = compareDiv1.cloneNode(false); + compareDiv2.innerHTML = compareDiv1.innerHTML; + // Oddly, IE9 sometimes produces two nodes that return true for + // isEqualNode but have different innerHTML (omitting closing tags vs. + // not). + if (!compareDiv1.isEqualNode(compareDiv2) + && compareDiv1.innerHTML != compareDiv2.innerHTML) { + throw "DOM does not round-trip through serialization! " + + compareDiv1.innerHTML + " vs. " + compareDiv2.innerHTML; + } + if (!compareDiv1.isEqualNode(compareDiv2)) { + throw "DOM does not round-trip through serialization (although innerHTML is the same)! " + + compareDiv1.innerHTML; + } + + // Check for attributes + if (specCell.firstChild.attributes.length) { + throw "Wrapper div has attributes! " + + specCell.innerHTML.replace(/<div><\/div>$/, ""); + } + + // Final sanity check: make sure everything isAllowedChild() of its + // parent. + getDescendants(specCell.firstChild).forEach(function(descendant) { + if (!isAllowedChild(descendant, descendant.parentNode)) { + throw "Something here is not an allowed child of its parent: " + descendant; + } + }); + + addBrackets(range); + + specCell.lastChild.textContent = specCell.firstChild.innerHTML; + if (command != "multitest") { + specCell.lastChild.appendChild(queryOutputHelper( + beforeIndeterm, beforeState, beforeValue, + afterIndeterm, afterState, afterValue, + command, test[test.length - 1][1])); + if (specCell.querySelector(".bad-result")) { + specCell.parentNode.className = "alert"; + } + } + } catch (e) { + specCell.firstChild.contentEditable = "inherit"; + specCell.firstChild.removeAttribute("spellcheck"); + specCell.lastChild.textContent = "Exception: " + formatException(e); + + specCell.parentNode.className = "alert"; + specCell.lastChild.className = "alert"; + + // Don't bother comparing to localStorage, this is always wrong no + // matter what. + return; + } + + if (command != "multitest") { + // Old storage format + var key = "execcommand-" + command + + "-" + (test.length == 2 || test[1][1] == "false" ? "0" : "1") + + "-" + tr.firstChild.lastChild.textContent; + } else { + var key = "execcommand-" + JSON.stringify(test); + } + + // Use getItem() instead of direct property access to work around Firefox + // bug: https://bugzilla.mozilla.org/show_bug.cgi?id=532062 + var oldValue = localStorage.getItem(key); + var newValue = specCell.lastChild.firstChild.textContent; + + // Ignore differences between {} and []. + if (oldValue === null + || oldValue.replace("{}", "[]") !== newValue.replace("{}", "[]")) { + specCell.parentNode.className = "alert"; + var alertDiv = document.createElement("div"); + specCell.lastChild.appendChild(alertDiv); + alertDiv.className = "alert"; + if (oldValue === null) { + alertDiv.textContent = "Newly added test result"; + } else if (oldValue.replace(/[\[\]{}]/g, "") == newValue.replace(/[\[\]{}]/g, "")) { + alertDiv.textContent = "Last run produced a different selection: " + oldValue; + } else { + alertDiv.textContent = "Last run produced different markup: " + oldValue; + } + + var button = document.createElement("button"); + alertDiv.appendChild(button); + button.textContent = "Store new result"; + button.className = "store-new-result"; + button.onclick = (function(key, val, alertDiv) { return function() { + localStorage[key] = val; + // Make it easier to do mass updates, and also to jump to the next + // new result + var buttons = document.getElementsByClassName("store-new-result"); + for (var i = 0; i < buttons.length; i++) { + if (isDescendant(buttons[i], alertDiv) + && i + 1 < buttons.length) { + buttons[i + 1].focus(); + break; + } + } + var td = alertDiv; + while (td.tagName != "TD") { + td = td.parentNode; + } + alertDiv.parentNode.removeChild(alertDiv); + if (!td.querySelector(".alert")) { + td.parentNode.className = (" " + td.parentNode.className + " ") + .replace(/ alert /g, "") + .replace(/^ | $/g, ""); + } + } })(key, newValue, alertDiv); + } +} +//@} + +function browserCellException(e, testDiv, browserCell) { +//@{ + if (testDiv) { + testDiv.contenteditable = "inherit"; + testDiv.removeAttribute("spellcheck"); + } + browserCell.lastChild.className = "alert"; + browserCell.lastChild.textContent = "Exception: " + formatException(e); + if (testDiv && testDiv.parentNode != browserCell) { + browserCell.insertBefore(testDiv, browserCell.firstChild); + } +} +//@} + +function formatException(e) { +//@{ + if (typeof e == "object" && "stack" in e) { + return e + " (stack: " + e.stack + ")"; + } + return String(e); +} +//@} + +function doSameCell(tr) { +//@{ + tr.className = (" " + tr.className + " ").replace(" active ", "").trim(); + if (tr.className == "") { + tr.removeAttribute("class"); + } + + var sameCell = document.createElement("td"); + if (!document.querySelector("#browser-checkbox").checked) { + sameCell.className = "maybe"; + sameCell.textContent = "?"; + } else { + var exception = false; + try { + // Ad hoc normalization to avoid basically spurious mismatches. For + // now this includes ignoring where the selection goes. + var normalizedSpecCell = tr.childNodes[1].lastChild.firstChild.textContent + .replace(/[[\]{}]/g, "") + .replace(/ style="margin: 0 0 0 40px; border: none; padding: 0px;"/g, '') + .replace(/ style="margin-right: 0px;" dir="ltr"/g, '') + .replace(/ style="margin-left: 0px;" dir="rtl"/g, '') + .replace(/ style="margin-(left|right): 40px;"/g, '') + .replace(/: /g, ":") + .replace(/;? ?"/g, '"') + .replace(/<(\/?)strong/g, '<$1b') + .replace(/<(\/?)strike/g, '<$1s') + .replace(/<(\/?)em/g, '<$1i') + .replace(/#[0-9a-fA-F]{6}/g, function(match) { return match.toUpperCase(); }); + var normalizedBrowserCell = tr.childNodes[2].lastChild.firstChild.textContent + .replace(/[[\]{}]/g, "") + .replace(/ style="margin: 0 0 0 40px; border: none; padding: 0px;"/g, '') + .replace(/ style="margin-right: 0px;" dir="ltr"/g, '') + .replace(/ style="margin-left: 0px;" dir="rtl"/g, '') + .replace(/ style="margin-(left|right): 40px;"/g, '') + .replace(/: /g, ":") + .replace(/;? ?"/g, '"') + .replace(/<(\/?)strong/g, '<$1b') + .replace(/<(\/?)strike/g, '<$1s') + .replace(/<(\/?)em/g, '<$1i') + .replace(/#[0-9a-fA-F]{6}/g, function(match) { return match.toUpperCase(); }) + .replace(/ size="2" width="100%"/g, ''); + if (navigator.userAgent.indexOf("MSIE") != -1) { + // IE produces <font style> instead of <span style>, so let's + // translate all <span>s to <font>s. + normalizedSpecCell = normalizedSpecCell + .replace(/<(\/?)span/g, '<$1font'); + normalizedBrowserCell = normalizedBrowserCell + .replace(/<(\/?)span/g, '<$1font'); + } + } catch (e) { + exception = true; + } + if (!exception && normalizedSpecCell == normalizedBrowserCell) { + sameCell.className = "yes"; + sameCell.textContent = "\u2713"; + } else { + sameCell.className = "no"; + sameCell.textContent = "\u2717"; + } + } + tr.appendChild(sameCell); + + for (var i = 0; i <= 2; i++) { + // Insert <wbr> so IE doesn't stretch the screen. This is considerably + // more complicated than it has to be, thanks to Firefox's lack of + // support for outerHTML. + var div = tr.childNodes[i].lastChild; + if (div.firstChild) { + var text = div.firstChild.textContent; + div.removeChild(div.firstChild); + div.insertBefore(document.createElement("div"), div.firstChild); + div.firstChild.innerHTML = text + .replace(/&/g, "&") + .replace(/</g, "<") + .replace(/>/g, "><wbr>") + .replace(/</g, "<wbr><"); + while (div.firstChild.hasChildNodes()) { + div.insertBefore(div.firstChild.lastChild, div.firstChild.nextSibling); + } + div.removeChild(div.firstChild); + } + + // Add position: absolute span to not affect vertical layout + getDescendants(tr.childNodes[i].firstChild) + .filter(function(node) { + return node.nodeType == Node.TEXT_NODE + && /^(\{\}?|\})$/.test(node.data); + }).forEach(function(node) { + var span = document.createElement("span"); + span.style.position = "absolute"; + span.textContent = node.data; + node.parentNode.insertBefore(span, node); + node.parentNode.removeChild(node); + }); + } +} +//@} + +function doTearDown(command) { +//@{ + getSelection().removeAllRanges(); +} +//@} + +function setupCell(cell, html) { +//@{ + cell.innerHTML = "<div></div><div></div>"; + + return setupDiv(cell.firstChild, html); +} +//@} + +function setupDiv(node, html) { +//@{ + // A variety of checks to avoid simple errors. Not foolproof, of course. + var re = /\{|\[|data-start/g; + var markers = []; + var marker; + while (marker = re.exec(html)) { + markers.push(marker); + } + if (markers.length != 1) { + throw "Need exactly one start marker ([ or { or data-start), found " + markers.length; + } + + var re = /\}|\]|data-end/g; + var markers = []; + var marker; + while (marker = re.exec(html)) { + markers.push(marker); + } + if (markers.length != 1) { + throw "Need exactly one end marker (] or } or data-end), found " + markers.length; + } + + node.innerHTML = html; + + var startNode, startOffset, endNode, endOffset; + + // For braces that don't lie inside text nodes, we can't just set + // innerHTML, because that might disturb the DOM. For instance, if the + // brace is right before a <tr>, it could get moved outside the table + // entirely, which messes everything up pretty badly. So we instead + // allow using data attributes: data-start and data-end on the start and + // end nodes, with a numeric value indicating the offset. This format + // doesn't allow the parent div to be a start or end node, but in that case + // you can always use the curly braces. + if (node.querySelector("[data-start]")) { + startNode = node.querySelector("[data-start]"); + startOffset = startNode.getAttribute("data-start"); + startNode.removeAttribute("data-start"); + } + if (node.querySelector("[data-end]")) { + endNode = node.querySelector("[data-end]"); + endOffset = endNode.getAttribute("data-end"); + endNode.removeAttribute("data-end"); + } + + var cur = node; + while (true) { + if (!cur || (cur != node && !(cur.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINS))) { + break; + } + + if (cur.nodeType != Node.TEXT_NODE) { + cur = nextNode(cur); + continue; + } + + var data = cur.data.replace(/\]/g, ""); + var startIdx = data.indexOf("["); + + data = cur.data.replace(/\[/g, ""); + var endIdx = data.indexOf("]"); + + cur.data = cur.data.replace(/[\[\]]/g, ""); + + if (startIdx != -1) { + startNode = cur; + startOffset = startIdx; + } + + if (endIdx != -1) { + endNode = cur; + endOffset = endIdx; + } + + // These are only legal as the first or last + data = cur.data.replace(/\}/g, ""); + var elStartIdx = data.indexOf("{"); + + data = cur.data.replace(/\{/g, ""); + var elEndIdx = data.indexOf("}"); + + if (elStartIdx == 0) { + startNode = cur.parentNode; + startOffset = getNodeIndex(cur); + } else if (elStartIdx != -1) { + startNode = cur.parentNode; + startOffset = getNodeIndex(cur) + 1; + } + if (elEndIdx == 0) { + endNode = cur.parentNode; + endOffset = getNodeIndex(cur); + } else if (elEndIdx != -1) { + endNode = cur.parentNode; + endOffset = getNodeIndex(cur) + 1; + } + + cur.data = cur.data.replace(/[{}]/g, ""); + if (!cur.data.length) { + if (cur == startNode || cur == endNode) { + throw "You put a square bracket where there was no text node . . ."; + } + var oldCur = cur; + cur = nextNode(cur); + oldCur.parentNode.removeChild(oldCur); + } else { + cur = nextNode(cur); + } + } + + return [startNode, startOffset, endNode, endOffset]; +} +//@} + +function setSelection(startNode, startOffset, endNode, endOffset) { +//@{ + if (navigator.userAgent.indexOf("Opera") != -1) { + // Yes, browser sniffing is evil, but I can't be bothered to debug + // Opera. + var range = document.createRange(); + range.setStart(startNode, startOffset); + range.setEnd(endNode, endOffset); + if (range.collapsed) { + range.setEnd(startNode, startOffset); + } + getSelection().removeAllRanges(); + getSelection().addRange(range); + } else if ("extend" in getSelection()) { + // WebKit behaves unreasonably for collapse(), so do that manually. + /* + var range = document.createRange(); + range.setStart(startNode, startOffset); + getSelection().removeAllRanges(); + getSelection().addRange(range); + */ + getSelection().collapse(startNode, startOffset); + getSelection().extend(endNode, endOffset); + } else { + // IE9. Selections have no direction, so we just make the selection + // always forwards. + var range; + if (getSelection().rangeCount) { + range = getSelection().getRangeAt(0); + } else { + range = document.createRange(); + } + range.setStart(startNode, startOffset); + range.setEnd(endNode, endOffset); + if (range.collapsed) { + // Phooey, we got them backwards. + range.setEnd(startNode, startOffset); + } + if (!getSelection().rangeCount) { + getSelection().addRange(range); + } + } +} +//@} + +/** + * Add brackets at the start and end points of the given range, so that they're + * visible. + */ +function addBrackets(range) { +//@{ + // Handle the collapsed case specially, to avoid confusingly getting the + // markers backwards in some cases + if (range.startContainer.nodeType == Node.TEXT_NODE + || range.startContainer.nodeType == Node.COMMENT_NODE) { + if (range.collapsed) { + range.startContainer.insertData(range.startOffset, "[]"); + } else { + range.startContainer.insertData(range.startOffset, "["); + } + } else { + var marker = range.collapsed ? "{}" : "{"; + if (range.startOffset != range.startContainer.childNodes.length + && range.startContainer.childNodes[range.startOffset].nodeType == Node.TEXT_NODE) { + range.startContainer.childNodes[range.startOffset].insertData(0, marker); + } else if (range.startOffset != 0 + && range.startContainer.childNodes[range.startOffset - 1].nodeType == Node.TEXT_NODE) { + range.startContainer.childNodes[range.startOffset - 1].appendData(marker); + } else { + // Seems to serialize as I'd want even for tables . . . IE doesn't + // allow undefined to be passed as the second argument (it throws + // an exception), so we have to explicitly check the number of + // children and pass null. + range.startContainer.insertBefore(document.createTextNode(marker), + range.startContainer.childNodes.length == range.startOffset + ? null + : range.startContainer.childNodes[range.startOffset]); + } + } + if (range.collapsed) { + return; + } + if (range.endContainer.nodeType == Node.TEXT_NODE + || range.endContainer.nodeType == Node.COMMENT_NODE) { + range.endContainer.insertData(range.endOffset, "]"); + } else { + if (range.endOffset != range.endContainer.childNodes.length + && range.endContainer.childNodes[range.endOffset].nodeType == Node.TEXT_NODE) { + range.endContainer.childNodes[range.endOffset].insertData(0, "}"); + } else if (range.endOffset != 0 + && range.endContainer.childNodes[range.endOffset - 1].nodeType == Node.TEXT_NODE) { + range.endContainer.childNodes[range.endOffset - 1].appendData("}"); + } else { + range.endContainer.insertBefore(document.createTextNode("}"), + range.endContainer.childNodes.length == range.endOffset + ? null + : range.endContainer.childNodes[range.endOffset]); + } + } +} +//@} + +function normalizeSerializedStyle(wrapper) { +//@{ + // Inline CSS attribute serialization has terrible interop, so we fix + // things up a bit to avoid spurious mismatches. This needs to be removed + // once CSSOM defines this stuff properly, but for now there's just no + // standard for any of it. This only normalizes descendants of wrapper, + // not wrapper itself. + [].forEach.call(wrapper.querySelectorAll("[style]"), function(node) { + if (node.style.color != "") { + var newColor = normalizeColor(node.style.color); + node.style.color = ""; + node.style.color = newColor; + } + if (node.style.backgroundColor != "") { + var newBackgroundColor = normalizeColor(node.style.backgroundColor); + node.style.backgroundColor = ""; + node.style.backgroundColor = newBackgroundColor; + } + node.setAttribute("style", node.getAttribute("style") + // Random spacing differences + .replace(/; ?$/, "") + .replace(/: /g, ":") + // Gecko likes "transparent" + .replace(/transparent/g, "rgba(0, 0, 0, 0)") + // WebKit likes to look overly precise + .replace(/, 0.496094\)/g, ", 0.5)") + // Gecko converts anything with full alpha to "transparent" which + // then becomes "rgba(0, 0, 0, 0)", so we have to make other + // browsers match + .replace(/rgba\([0-9]+, [0-9]+, [0-9]+, 0\)/g, "rgba(0, 0, 0, 0)") + ); + }); +} +//@} + +/** + * Input is in the following format: + * [input HTML, + * array of commands, + * expected output HTML, + * array of expected execCommand() return values, + * object of expected indeterm/state/value]. + * The array of commands is [[command, value, optionalDesc], [command, value, + * optionalDesc], ...]. optionalDesc is appended to the description of the + * test in the generated test name. + * + * The + * array of expected execCommand() return values is [true|false, true|false, + * ...], where the indices match those in the array of commands. The + * indeterm/state/value object is of the form + * {command: [expected indeterm before, expected state before, + * expected value before, expected indeterm after, + * expected state after, expected value after], + * command: ... } + * null for any of the last six entries means an INVALID_ACCESS_ERR must be + * raised. + */ +function runConformanceTest(browserTest) { +//@{ + document.getElementById("test-container").innerHTML = "<div contenteditable></div><p>test"; + var testName = JSON.stringify(browserTest[1]) + " " + format_value(browserTest[0]); + var testDiv = document.querySelector("div[contenteditable]"); + var originalRootElement, newRootElement; + var exception = null; + var expectedExecCommandReturnValues = browserTest[3]; + var expectedQueryResults = browserTest[4]; + var actualQueryResults = {}; + var actualQueryExceptions = {}; + var subtestName; + + try { + var points = setupDiv(testDiv, browserTest[0]); + + var range = document.createRange(); + range.setStart(points[0], points[1]); + range.setEnd(points[2], points[3]); + // The points might be backwards + if (range.collapsed) { + range.setEnd(points[0], points[1]); + } + getSelection().removeAllRanges(); + getSelection().addRange(range); + + var originalRootElement = document.documentElement.cloneNode(true); + originalRootElement.querySelector("[contenteditable]").parentNode + .removeChild(originalRootElement.querySelector("[contenteditable]")); + originalRootElement.querySelector("#log").parentNode + .removeChild(originalRootElement.querySelector("#log")); + + for (var command in expectedQueryResults) { + var results = []; + var exceptions = {}; + try { results[0] = document.queryCommandIndeterm(command) } + catch(e) { exceptions[0] = e } + try { results[1] = document.queryCommandState(command) } + catch(e) { exceptions[1] = e } + try { results[2] = document.queryCommandValue(command) } + catch(e) { exceptions[2] = e } + actualQueryResults[command] = results; + actualQueryExceptions[command] = exceptions; + } + } catch(e) { + exception = e; + } + + for (var i = 0; i < browserTest[1].length; i++) { + subtestName = testName + ": execCommand(" + + format_value(browserTest[1][i][0]) + ", false, " + + format_value(browserTest[1][i][1]) + ") " + + (browserTest[1][i][2] ? browserTest[1][i][2] + " " : "") + + "return value" + subsetTest(test, function() { + assert_equals(exception, null, "Setup must not throw an exception"); + + assert_equals(document.execCommand(browserTest[1][i][0], false, browserTest[1][i][1]), + expectedExecCommandReturnValues[i]); + }, subtestName); + } + + if (exception === null) { + try { + for (var command in expectedQueryResults) { + var results = actualQueryResults[command]; + var exceptions = actualQueryExceptions[command]; + try { results[3] = document.queryCommandIndeterm(command) } + catch(e) { exceptions[3] = e } + try { results[4] = document.queryCommandState(command) } + catch(e) { exceptions[4] = e } + try { results[5] = document.queryCommandValue(command) } + catch(e) { exceptions[5] = e } + } + + var newRootElement = document.documentElement.cloneNode(true); + newRootElement.querySelector("[contenteditable]").parentNode + .removeChild(newRootElement.querySelector("[contenteditable]")); + newRootElement.querySelector("#log").parentNode + .removeChild(newRootElement.querySelector("#log")); + + normalizeSerializedStyle(testDiv); + } catch(e) { + exception = e; + } + } + + subsetTest(test, function() { + assert_equals(exception, null, "Setup must not throw an exception"); + + // Now test for modifications to non-editable content. First just + // count children: + assert_equals(testDiv.parentNode.childNodes.length, 2, + "The parent div must have two children. Did something spill outside the test div?"); + + // Check for attributes + assert_equals(testDiv.attributes.length, 1, + 'Wrapper div must have only one attribute (<div contenteditable="">), but has more (' + + formatStartTag(testDiv) + ")"); + + assert_equals(document.body.attributes.length, 0, + "Body element must have no attributes (<body>), but has more (" + + formatStartTag(document.body) + ")"); + + // Check that in general, nothing outside the test div was modified. + // TODO: Less verbose error reporting, the way some of the range tests + // do? + assert_equals(newRootElement.innerHTML, originalRootElement.innerHTML, + "Everything outside the editable div must be unchanged, but some change did occur"); + }, testName + " checks for modifications to non-editable content"); + + subsetTest(test, function() { + assert_equals(exception, null, "Setup must not throw an exception"); + + if (Array.isArray(browserTest[2])) { + var expectedInnerHTMLArray = []; + browserTest[2].forEach(function (expectedInnerHTML) { + expectedInnerHTMLArray.push(expectedInnerHTML.replace(/[\[\]{}]/g, "")); + }); + assert_in_array(testDiv.innerHTML, + expectedInnerHTMLArray, + "Unexpected innerHTML (after normalizing inline style)"); + } else { + assert_equals(testDiv.innerHTML, + browserTest[2].replace(/[\[\]{}]/g, ""), + "Unexpected innerHTML (after normalizing inline style)"); + } + }, testName + " compare innerHTML"); + + for (var command in expectedQueryResults) { + var descriptions = [ + 'queryCommandIndeterm("' + command + '") before', + 'queryCommandState("' + command + '") before', + 'queryCommandValue("' + command + '") before', + 'queryCommandIndeterm("' + command + '") after', + 'queryCommandState("' + command + '") after', + 'queryCommandValue("' + command + '") after', + ]; + for (var i = 0; i < 6; i++) { + subsetTest(test, function() { + assert_equals(exception, null, "Setup must not throw an exception"); + + if (expectedQueryResults[command][i] === null) { + // Some ad hoc tests to verify that we have a real + // DOMException. FIXME: This should be made more rigorous, + // with clear steps specified for checking that something + // is really a DOMException. + assert_true(i in actualQueryExceptions[command], + "An exception must be thrown in this case"); + var e = actualQueryExceptions[command][i]; + assert_equals(typeof e, "object", + "typeof thrown object"); + assert_idl_attribute(e, "code", + "Thrown object must be a DOMException"); + assert_idl_attribute(e, "INVALID_ACCESS_ERR", + "Thrown object must be a DOMException"); + assert_equals(e.code, e.INVALID_ACCESS_ERR, + "Thrown object must be an INVALID_ACCESS_ERR, so its .code and .INVALID_ACCESS_ERR attributes must be equal"); + } else if ((i == 2 || i == 5) + && (command == "backcolor" || command == "forecolor" || command == "hilitecolor") + && typeof actualQueryResults[command][i] == "string") { + assert_false(i in actualQueryExceptions[command], + "An exception must not be thrown in this case"); + // We don't return the format that the color should be in: + // that's up to CSSOM. Thus we normalize before comparing. + assert_equals(normalizeColor(actualQueryResults[command][i]), + expectedQueryResults[command][i], + "Wrong result returned (after color normalization)"); + } else { + assert_false(i in actualQueryExceptions[command], + "An exception must not be thrown in this case"); + assert_equals(actualQueryResults[command][i], + expectedQueryResults[command][i], + "Wrong result returned"); + } + }, testName + " " + descriptions[i]); + } + } + + // Silly Firefox + document.body.removeAttribute("bgcolor"); +} +//@} + +/** + * Return a string like '<body bgcolor="#FFFFFF">'. + */ +function formatStartTag(el) { +//@{ + var ret = "<" + el.tagName.toLowerCase(); + for (var i = 0; i < el.attributes.length; i++) { + ret += " " + el.attributes[i].name + '="'; + ret += el.attributes[i].value.replace(/\&/g, "&") + .replace(/"/g, """); + ret += '"'; + } + return ret + ">"; +} +//@} + +// vim: foldmarker=@{,@} foldmethod=marker |