mirror of
https://github.com/webodf/WebODF.git
synced 2026-11-30 16:47:58 +01:00
Rewrite DomUtils.getNodesInRange to improve performance
By changing how boundaries are being checked, iteration performance is boosted quite significantly for text node retrieval. * Replace @returns with @return in affected files
This commit is contained in:
+80
-48
@@ -37,7 +37,6 @@
|
||||
*/
|
||||
|
||||
/*global Node, core, ops, runtime, NodeFilter, Range*/
|
||||
/*jslint bitwise: true*/
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
@@ -48,7 +47,7 @@
|
||||
* Detect various browser quirks
|
||||
* unscaledRangeClientRects - Firefox doesn't apply parent css transforms to any range client rectangles
|
||||
* rangeBCRIgnoresElementBCR - Internet explorer returns 0 client rects for an empty element that has fixed dimensions
|
||||
* @returns {!{unscaledRangeClientRects: !boolean, rangeBCRIgnoresElementBCR: !boolean}}
|
||||
* @return {!{unscaledRangeClientRects: !boolean, rangeBCRIgnoresElementBCR: !boolean}}
|
||||
*/
|
||||
function getBrowserQuirks() {
|
||||
var range,
|
||||
@@ -136,7 +135,7 @@
|
||||
* to the provided container and offset.
|
||||
* @param {!Node} container
|
||||
* @param {!number} offset
|
||||
* @returns {{container: Node, offset: !number}}
|
||||
* @return {{container: Node, offset: !number}}
|
||||
*/
|
||||
function findStablePoint(container, offset) {
|
||||
var c = container;
|
||||
@@ -189,7 +188,7 @@
|
||||
* only the completely selected text node:
|
||||
* "A" "|BCD|" "E"
|
||||
* @param {!Range} range
|
||||
* @returns {!Array.<!Node>} Return a list of nodes modified as a result
|
||||
* @return {!Array.<!Node>} Return a list of nodes modified as a result
|
||||
* of this split operation. These are often
|
||||
* processed through
|
||||
* DomUtils.normalizeTextNodes after all
|
||||
@@ -264,7 +263,7 @@
|
||||
* Aligned boundaries are counted as inclusion
|
||||
* @param {!Range} container
|
||||
* @param {!Range} insideRange
|
||||
* @returns {boolean}
|
||||
* @return {boolean}
|
||||
*/
|
||||
function containsRange(container, insideRange) {
|
||||
return container.compareBoundaryPoints(Range.START_TO_START, insideRange) <= 0
|
||||
@@ -276,7 +275,7 @@
|
||||
* Returns true if there is any intersection between range1 and range2
|
||||
* @param {!Range} range1
|
||||
* @param {!Range} range2
|
||||
* @returns {boolean}
|
||||
* @return {boolean}
|
||||
*/
|
||||
function rangesIntersect(range1, range2) {
|
||||
return range1.compareBoundaryPoints(Range.END_TO_START, range2) <= 0
|
||||
@@ -284,42 +283,90 @@
|
||||
}
|
||||
this.rangesIntersect = rangesIntersect;
|
||||
|
||||
/**
|
||||
* Returns the maximum available offset for the node. If this is a text
|
||||
* node, this will be node.length, or for an element node, childNodes.length
|
||||
* @param {!Node} node
|
||||
* @return {!number}
|
||||
*/
|
||||
function maximumOffset(node) {
|
||||
return node.nodeType === Node.TEXT_NODE ? /**@type{!Text}*/(node).length : node.childNodes.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all nodes within a supplied range that pass the required filter
|
||||
* @param {!Range} range
|
||||
* @param {!function(!Node) : number} nodeFilter
|
||||
* @returns {!Array.<Node>}
|
||||
* @param {!number} whatToShow
|
||||
* @return {!Array.<Node>}
|
||||
*/
|
||||
function getNodesInRange(range, nodeFilter) {
|
||||
/*jslint bitwise:true*/
|
||||
function getNodesInRange(range, nodeFilter, whatToShow) {
|
||||
var document = range.startContainer.ownerDocument,
|
||||
elements = [],
|
||||
rangeRoot = range.commonAncestorContainer,
|
||||
root = /**@type{!Node}*/(rangeRoot.nodeType === Node.TEXT_NODE ? rangeRoot.parentNode : rangeRoot),
|
||||
n,
|
||||
filterResult,
|
||||
treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_ALL, nodeFilter, false);
|
||||
treeWalker = document.createTreeWalker(root, whatToShow, nodeFilter, false),
|
||||
currentNode,
|
||||
lastNodeInRange,
|
||||
endNodeCompareFlags,
|
||||
comparePositionResult;
|
||||
|
||||
treeWalker.currentNode = range.startContainer;
|
||||
n = range.startContainer;
|
||||
while (n) {
|
||||
filterResult = nodeFilter(n);
|
||||
if (filterResult === NodeFilter.FILTER_ACCEPT) {
|
||||
elements.push(n);
|
||||
} else if (filterResult === NodeFilter.FILTER_REJECT) {
|
||||
if (range.endContainer.childNodes[range.endOffset - 1]) {
|
||||
// This is the last node completely contained in the range
|
||||
lastNodeInRange = /**@type{!Node}*/(range.endContainer.childNodes[range.endOffset - 1]);
|
||||
// Accept anything preceding or contained by this node.
|
||||
endNodeCompareFlags = Node.DOCUMENT_POSITION_PRECEDING | Node.DOCUMENT_POSITION_CONTAINED_BY;
|
||||
} else {
|
||||
// Either no child nodes (e.g., TEXT_NODE) or endOffset = 0
|
||||
lastNodeInRange = /**@type{!Node}*/(range.endContainer);
|
||||
// Don't accept things contained within this node, as the range ends before this node's children.
|
||||
// This is the last node touching the range though, so the node is still accepted into the results.
|
||||
endNodeCompareFlags = Node.DOCUMENT_POSITION_PRECEDING;
|
||||
}
|
||||
|
||||
if (range.startContainer.childNodes[range.startOffset]) {
|
||||
// The range starts within startContainer, so this child node is the first node in the range
|
||||
currentNode = /**@type{!Node}*/(range.startContainer.childNodes[range.startOffset]);
|
||||
treeWalker.currentNode = currentNode;
|
||||
} else if (range.startOffset === maximumOffset(range.startContainer)) {
|
||||
// This condition will be true if the range starts beyond the last position of a node
|
||||
// E.g., (text, text.length) or (div, div.childNodes.length)
|
||||
currentNode = /**@type{!Node}*/(range.startContainer);
|
||||
treeWalker.currentNode = currentNode;
|
||||
// In this case, move to the last child (if the node has children)
|
||||
treeWalker.lastChild(); // May return null if the current node has no children
|
||||
// And navigate onto the next node in sequence
|
||||
currentNode = treeWalker.nextNode();
|
||||
} else {
|
||||
// This will only be hit for a text node that is partially overlapped by the range start
|
||||
currentNode = /**@type{!Node}*/(range.startContainer);
|
||||
treeWalker.currentNode = currentNode;
|
||||
}
|
||||
|
||||
if (currentNode && nodeFilter(currentNode) === NodeFilter.FILTER_ACCEPT) {
|
||||
// The first call to nextNode will return the next node *after* walker.currentNode
|
||||
// Therefore, need to manually check if currentNode should be included in the elements array
|
||||
// and save it if it passes the filter
|
||||
elements.push(currentNode);
|
||||
}
|
||||
|
||||
currentNode = treeWalker.nextNode();
|
||||
while (currentNode) {
|
||||
comparePositionResult = lastNodeInRange.compareDocumentPosition(currentNode);
|
||||
if (comparePositionResult !== 0 && (comparePositionResult & endNodeCompareFlags) === 0) {
|
||||
// comparePositionResult === 0 if currentNode === lastNodeInRange. This is considered within the range
|
||||
// comparePositionResult & endNodeCompareFlags would be non-zero if n precedes lastNodeInRange
|
||||
// If either of these statements are false, currentNode is past the end of the range
|
||||
break;
|
||||
}
|
||||
n = n.parentNode;
|
||||
elements.push(currentNode);
|
||||
currentNode = treeWalker.nextNode();
|
||||
}
|
||||
// The expected sequence is outer-most to inner-most element, thus, the array just built needs to be reversed
|
||||
elements.reverse();
|
||||
|
||||
n = treeWalker.nextNode();
|
||||
while (n) {
|
||||
elements.push(n);
|
||||
n = treeWalker.nextNode();
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
/*jslint bitwise:false*/
|
||||
this.getNodesInRange = getNodesInRange;
|
||||
|
||||
/**
|
||||
@@ -375,9 +422,9 @@
|
||||
|
||||
/**
|
||||
* Checks if the provided limits fully encompass the passed in node
|
||||
* @param {{startContainer: Node, startOffset: !number, endContainer: Node, endOffset: !number}} limits
|
||||
* @param {!Range|{startContainer: Node, startOffset: !number, endContainer: Node, endOffset: !number}} limits
|
||||
* @param {!Node} node
|
||||
* @returns {boolean} Returns true if the node is fully contained within
|
||||
* @return {boolean} Returns true if the node is fully contained within
|
||||
* the range
|
||||
*/
|
||||
function rangeContainsNode(limits, node) {
|
||||
@@ -439,7 +486,7 @@
|
||||
* @param {!Element|!Document} node
|
||||
* @param {!string} namespace
|
||||
* @param {!string} tagName
|
||||
* @returns {!Array.<!Element>}
|
||||
* @return {!Array.<!Element>}
|
||||
*/
|
||||
function getElementsByTagNameNS(node, namespace, tagName) {
|
||||
var e = [], list, i, l;
|
||||
@@ -452,23 +499,6 @@
|
||||
}
|
||||
this.getElementsByTagNameNS = getElementsByTagNameNS;
|
||||
|
||||
/**
|
||||
* @param {!Range} range
|
||||
* @param {!Node} node
|
||||
* @return {!boolean}
|
||||
*/
|
||||
function rangeIntersectsNode(range, node) {
|
||||
var nodeRange = node.ownerDocument.createRange(),
|
||||
result;
|
||||
|
||||
nodeRange.selectNodeContents(node);
|
||||
result = rangesIntersect(range, nodeRange);
|
||||
nodeRange.detach();
|
||||
|
||||
return result;
|
||||
}
|
||||
this.rangeIntersectsNode = rangeIntersectsNode;
|
||||
|
||||
/**
|
||||
* Whether a node contains another node
|
||||
* Wrapper around Node.contains
|
||||
@@ -491,12 +521,14 @@
|
||||
* @param {?Node} descendant The node to test presence of
|
||||
* @return {!boolean}
|
||||
*/
|
||||
/*jslint bitwise:true*/
|
||||
function containsNodeForBrokenWebKit(parent, descendant) {
|
||||
// the contains function is not reliable on safari/webkit so use
|
||||
// compareDocumentPosition instead
|
||||
return parent === descendant ||
|
||||
Boolean(parent.compareDocumentPosition(descendant) & Node.DOCUMENT_POSITION_CONTAINED_BY);
|
||||
}
|
||||
/*jslint bitwise:false*/
|
||||
|
||||
/**
|
||||
* Return a number > 0 when point 1 precedes point 2. Return 0 if the points
|
||||
@@ -543,7 +575,7 @@
|
||||
* a property on two range-sourced client
|
||||
* rectangles (e.g., rect1.top - rect2.top)
|
||||
* @param {!number} zoomLevel Current canvas zoom level
|
||||
* @returns {!number}
|
||||
* @return {!number}
|
||||
*/
|
||||
function adaptRangeDifferenceToZoomLevel(inputNumber, zoomLevel) {
|
||||
if (getBrowserQuirks().unscaledRangeClientRects) {
|
||||
@@ -563,7 +595,7 @@
|
||||
* transformed in the same way.
|
||||
* See https://bugzilla.mozilla.org/show_bug.cgi?id=863618
|
||||
* @param {!Node} node
|
||||
* @returns {?ClientRect}
|
||||
* @return {?ClientRect}
|
||||
*/
|
||||
function getBoundingClientRect(node) {
|
||||
var doc = /**@type{!Document}*/(node.ownerDocument),
|
||||
|
||||
+104
-86
@@ -176,7 +176,7 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* @param {Node} node Node to start searching from
|
||||
* @param {!Node} container Root container to stop searching at. This helps set the boundary of the current
|
||||
* search and will usually be the root level element node (e.g., office:text, office:presentation, etc.)
|
||||
* @returns {!boolean}
|
||||
* @return {!boolean}
|
||||
*/
|
||||
this.isWithinTrackedChanges = function (node, container) {
|
||||
while (node && node !== container) {
|
||||
@@ -501,7 +501,7 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* All other whitespace elements are considered insignificant
|
||||
* @param {!Text} textNode
|
||||
* @param {!number} offset
|
||||
* @returns {!boolean}
|
||||
* @return {!boolean}
|
||||
*/
|
||||
function isSignificantWhitespace(textNode, offset) {
|
||||
var text = textNode.data,
|
||||
@@ -542,7 +542,7 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* a downgradeable whitespace element is a space element that is immediately preceded by something other than a space
|
||||
* and has at least one non-space character after it
|
||||
* @param {!Node} node
|
||||
* @returns {!boolean}
|
||||
* @return {!boolean}
|
||||
*/
|
||||
this.isDowngradableSpaceElement = function(node) {
|
||||
if (node.namespaceURI === textns && node.localName === "s") {
|
||||
@@ -554,7 +554,7 @@ odf.OdfUtils = function OdfUtils() {
|
||||
/**
|
||||
* Returns the first non-whitespace-only child of a given node
|
||||
* @param {Node|undefined} node
|
||||
* @returns {Node|undefined}
|
||||
* @return {Node|undefined}
|
||||
*/
|
||||
function getFirstNonWhitespaceChild(node) {
|
||||
var child = node && node.firstChild;
|
||||
@@ -646,7 +646,7 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* Adapted from instructions on how to generate plain text from an ODT document.
|
||||
* See algorithm at http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1415196_253892949
|
||||
* @param {!Node} node
|
||||
* @returns {boolean}
|
||||
* @return {boolean}
|
||||
*/
|
||||
function isTextContentContainingNode(node) {
|
||||
switch (node.namespaceURI) {
|
||||
@@ -692,7 +692,7 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* significant whitespace characters (as defined by the ODF standards).
|
||||
* WARNING this method is likely to be quite slow, so should be used as little as possible
|
||||
* @param {!Text} textNode
|
||||
* @returns {!boolean}
|
||||
* @return {!boolean}
|
||||
*/
|
||||
function isSignificantTextContent(textNode) {
|
||||
return Boolean(getParagraphElement(textNode)
|
||||
@@ -700,16 +700,19 @@ odf.OdfUtils = function OdfUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the supplied nodeRange should be included in the parent range according
|
||||
* to the setting of the includePartial flag.
|
||||
* @param {!Range} range Selection range
|
||||
* @param {!Range} nodeRange Node character range
|
||||
* @param {!boolean} includePartial flag indicating whether partial intersections are acceptable
|
||||
* @returns {!boolean}
|
||||
* Remove any nodes that aren't fully contained within the supplied range. This function assumes
|
||||
* the nodes appear in document order.
|
||||
* @param {!Range} range
|
||||
* @param {!Array.<!Node>} nodes
|
||||
* @return {undefined}
|
||||
*/
|
||||
function includeNode(range, nodeRange, includePartial) {
|
||||
return (includePartial && domUtils.rangesIntersect(range, nodeRange))
|
||||
|| domUtils.containsRange(range, nodeRange);
|
||||
function removePartiallyContainedNodes(range, nodes) {
|
||||
while (nodes.length > 0 && !domUtils.rangeContainsNode(range, /**@type{!Node}*/(nodes[0]))) {
|
||||
nodes.shift();
|
||||
}
|
||||
while (nodes.length > 0 && !domUtils.rangeContainsNode(range, /**@type{!Node}*/(nodes[nodes.length - 1]))) {
|
||||
nodes.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -719,37 +722,34 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* @param {!Range} range Range to search for nodes within
|
||||
* @param {boolean} includePartial Include partially intersecting text nodes
|
||||
* in the result.
|
||||
* @returns {!Array.<Node>}
|
||||
* @return {!Array.<Node>}
|
||||
*/
|
||||
function getTextNodes(range, includePartial) {
|
||||
var document = range.startContainer.ownerDocument,
|
||||
nodeRange = document.createRange(),
|
||||
textNodes;
|
||||
var textNodes;
|
||||
|
||||
/**
|
||||
* @param {!Node} node
|
||||
* @return {number}
|
||||
*/
|
||||
function nodeFilter(node) {
|
||||
nodeRange.selectNodeContents(node);
|
||||
|
||||
var result = NodeFilter.FILTER_REJECT;
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
if (includeNode(range, nodeRange, includePartial)) {
|
||||
return isSignificantTextContent(/**@type{!Text}*/(node))
|
||||
? NodeFilter.FILTER_ACCEPT
|
||||
: NodeFilter.FILTER_REJECT;
|
||||
}
|
||||
} else if (domUtils.rangesIntersect(range, nodeRange)) {
|
||||
if (isTextContentContainingNode(node)) {
|
||||
return NodeFilter.FILTER_SKIP;
|
||||
if (isSignificantTextContent(/**@type{!Text}*/(node))) {
|
||||
result = NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
} else if (isTextContentContainingNode(node)) {
|
||||
result = NodeFilter.FILTER_SKIP;
|
||||
}
|
||||
return NodeFilter.FILTER_REJECT;
|
||||
return result;
|
||||
}
|
||||
|
||||
textNodes = domUtils.getNodesInRange(range, nodeFilter);
|
||||
/*jslint bitwise:true*/
|
||||
textNodes = domUtils.getNodesInRange(range, nodeFilter, NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT);
|
||||
/*jslint bitwise:false*/
|
||||
if (!includePartial) {
|
||||
removePartiallyContainedNodes(range, textNodes);
|
||||
}
|
||||
|
||||
nodeRange.detach();
|
||||
return textNodes;
|
||||
}
|
||||
this.getTextNodes = getTextNodes;
|
||||
@@ -771,55 +771,73 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* content. This includes whitespace only elements
|
||||
* used in pretty-formatted xml as LibreOffice
|
||||
* produces in flat ODT files.
|
||||
* @returns {!Array.<Node>}
|
||||
* @return {!Array.<Node>}
|
||||
*/
|
||||
function getTextElements(range, includePartial,
|
||||
includeInsignificantWhitespace) {
|
||||
var document = range.startContainer.ownerDocument,
|
||||
nodeRange = document.createRange(),
|
||||
elements;
|
||||
function getTextElements(range, includePartial, includeInsignificantWhitespace) {
|
||||
var elements;
|
||||
|
||||
/**
|
||||
* @param {!Node} node
|
||||
* @return {number}
|
||||
*/
|
||||
function nodeFilter(node) {
|
||||
nodeRange.selectNodeContents(node);
|
||||
var result = NodeFilter.FILTER_REJECT;
|
||||
// do not return anything inside an character element or an inline root such as an annotation
|
||||
if (isCharacterElement(node.parentNode) || isInlineRoot(node.parentNode)) {
|
||||
return NodeFilter.FILTER_REJECT;
|
||||
}
|
||||
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
if (includeNode(range, nodeRange, includePartial)) {
|
||||
if (includeInsignificantWhitespace
|
||||
|| isSignificantTextContent(
|
||||
/**@type{!Text}*/(node)
|
||||
)) {
|
||||
// Text nodes should only be returned if they are
|
||||
// fully contained within the range.
|
||||
return NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
result = NodeFilter.FILTER_REJECT;
|
||||
} else if (node.nodeType === Node.TEXT_NODE) {
|
||||
if (includeInsignificantWhitespace || isSignificantTextContent(/**@type{!Text}*/(node))) {
|
||||
// Text nodes should only be returned if they are
|
||||
// fully contained within the range.
|
||||
result = NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
} else if (isAnchoredAsCharacterElement(node)) {
|
||||
if (includeNode(range, nodeRange, includePartial)) {
|
||||
// Character elements should only be returned if they are
|
||||
// fully contained within the range.
|
||||
return NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
// Character elements should only be returned if they are
|
||||
// fully contained within the range.
|
||||
result = NodeFilter.FILTER_ACCEPT;
|
||||
} else if (isTextContentContainingNode(node) || isGroupingElement(node)) {
|
||||
return NodeFilter.FILTER_SKIP;
|
||||
result = NodeFilter.FILTER_SKIP;
|
||||
}
|
||||
return NodeFilter.FILTER_REJECT;
|
||||
return result;
|
||||
}
|
||||
|
||||
elements = domUtils.getNodesInRange(range, nodeFilter);
|
||||
nodeRange.detach();
|
||||
/*jslint bitwise:true*/
|
||||
elements = domUtils.getNodesInRange(range, nodeFilter, NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT);
|
||||
/*jslint bitwise:false*/
|
||||
if (!includePartial) {
|
||||
removePartiallyContainedNodes(range, elements);
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
this.getTextElements = getTextElements;
|
||||
|
||||
/**
|
||||
* Crawl parent nodes starting at the startContainer until a matching node is found,
|
||||
* or the first inline root is met. If a node is accepted by the supplied filter, and is
|
||||
* not already the first element in the elements array, this node is prepended to the start
|
||||
* of the elements array.
|
||||
*
|
||||
* @param {!Node} startContainer Container to start search from (inclusive)
|
||||
* @param {!Array.<!Node>} elements Existing elements already discovered
|
||||
* @param {!function(!Node):!boolean} filter
|
||||
*/
|
||||
function prependParentContainers(startContainer, elements, filter) {
|
||||
var container = startContainer;
|
||||
while (container) {
|
||||
if (filter(container)) {
|
||||
if (elements[0] !== container) {
|
||||
elements.unshift(container);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (isInlineRoot(container)) {
|
||||
break;
|
||||
}
|
||||
container = container.parentNode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all paragraph elements that intersect the supplied range in document
|
||||
* order.
|
||||
@@ -830,32 +848,33 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* this function would return the following array:
|
||||
* [text:p{id="A"}, text:p{id="B"}]
|
||||
* @param {!Range} range
|
||||
* @returns {!Array.<!Element>}
|
||||
* @return {!Array.<!Element>}
|
||||
*/
|
||||
this.getParagraphElements = function (range) {
|
||||
var document = range.startContainer.ownerDocument,
|
||||
nodeRange = document.createRange(),
|
||||
elements;
|
||||
|
||||
var elements;
|
||||
/**
|
||||
* @param {!Node} node
|
||||
* @return {number}
|
||||
*/
|
||||
function nodeFilter(node) {
|
||||
nodeRange.selectNodeContents(node);
|
||||
var result = NodeFilter.FILTER_REJECT;
|
||||
if (isParagraph(node)) {
|
||||
if (domUtils.rangesIntersect(range, nodeRange)) {
|
||||
return NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
result = NodeFilter.FILTER_ACCEPT;
|
||||
} else if (isTextContentContainingNode(node) || isGroupingElement(node)) {
|
||||
return NodeFilter.FILTER_SKIP;
|
||||
result = NodeFilter.FILTER_SKIP;
|
||||
}
|
||||
return NodeFilter.FILTER_REJECT;
|
||||
return result;
|
||||
}
|
||||
|
||||
elements = domUtils.getNodesInRange(range, nodeFilter);
|
||||
nodeRange.detach();
|
||||
elements = domUtils.getNodesInRange(range, nodeFilter, NodeFilter.SHOW_ELEMENT);
|
||||
// getNodesInRange will only return nodes it enters during the iteration.
|
||||
// However, we desire all paragraph nodes either contained OR containing this range,
|
||||
// so we crawl the parentNodes of the start container until a root is found.
|
||||
|
||||
// Note, this isn't necessary for the end container because iteration crosses the
|
||||
// node boundary when entering towards the end container, meaning all paragraphs in
|
||||
// the end container's parentNodes will be reported by getNodesInRange.
|
||||
prependParentContainers(/**@type{!Node}*/(range.startContainer), elements, isParagraph);
|
||||
return elements;
|
||||
};
|
||||
|
||||
@@ -863,28 +882,27 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* Get all image elements that fully contained within the supplied range in
|
||||
* document order.
|
||||
* @param {!Range} range
|
||||
* @returns {!Array.<Node>}
|
||||
* @return {!Array.<Node>}
|
||||
*/
|
||||
this.getImageElements = function (range) {
|
||||
var document = range.startContainer.ownerDocument,
|
||||
nodeRange = document.createRange(),
|
||||
elements;
|
||||
var elements;
|
||||
|
||||
/**
|
||||
* @param {!Node} node
|
||||
* @return {number}
|
||||
*/
|
||||
function nodeFilter(node) {
|
||||
nodeRange.selectNodeContents(node);
|
||||
if (isImage(node) && domUtils.containsRange(range, nodeRange)) {
|
||||
return NodeFilter.FILTER_ACCEPT;
|
||||
var result = NodeFilter.FILTER_SKIP;
|
||||
if (isImage(node)) {
|
||||
result = NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
return NodeFilter.FILTER_SKIP;
|
||||
return result;
|
||||
}
|
||||
|
||||
elements = domUtils.getNodesInRange(range, nodeFilter);
|
||||
nodeRange.detach();
|
||||
|
||||
elements = domUtils.getNodesInRange(range, nodeFilter, NodeFilter.SHOW_ELEMENT);
|
||||
// See description in getParagraphElements as to why this is necessary
|
||||
// Short summary: want to include images that completely contain this range
|
||||
prependParentContainers(/**@type{!Node}*/(range.startContainer), elements, isImage);
|
||||
return elements;
|
||||
};
|
||||
|
||||
@@ -918,7 +936,7 @@ odf.OdfUtils = function OdfUtils() {
|
||||
* this function would return the following array:
|
||||
* [text:a{xlink:href="google"}, text:a{xlink:href="apple"}]
|
||||
* @param {!Range} range
|
||||
* @returns {!Array.<Node>}
|
||||
* @return {!Array.<Node>}
|
||||
*/
|
||||
this.getHyperlinkElements = function (range) {
|
||||
var links = [],
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
* @source: http://www.webodf.org/
|
||||
* @source: https://github.com/kogmbh/WebODF/
|
||||
*/
|
||||
/*global core, runtime*/
|
||||
/*global core, runtime, NodeFilter*/
|
||||
runtime.loadClass("core.DomUtils");
|
||||
|
||||
/**
|
||||
@@ -59,6 +59,31 @@ core.DomUtilsTests = function DomUtilsTests(runner) {
|
||||
core.UnitTest.cleanupTestAreaDiv();
|
||||
};
|
||||
|
||||
function ignoreSpans(node) {
|
||||
return node.localName === "span" ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the specified HTML nodes to the test area's div
|
||||
* @param {!string} html XML html string
|
||||
* @returns {undefined}
|
||||
*/
|
||||
function createNodes(html) {
|
||||
var /**@type{!string}*/
|
||||
xmlDoc = "<?xml version='1.0' encoding='UTF-8'?>",
|
||||
doc;
|
||||
|
||||
xmlDoc += "<div xmlns='http://www.w3.org/1999/xhtml'>";
|
||||
xmlDoc += html;
|
||||
xmlDoc += "</div>";
|
||||
|
||||
doc = runtime.parseXML(xmlDoc);
|
||||
document.importNode(doc.firstChild, true);
|
||||
while (doc.firstChild.firstChild) {
|
||||
t.doc.appendChild(doc.firstChild.firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeTextNodes_TextWithTextSilblings() {
|
||||
t.doc.appendChild(document.createTextNode("a"));
|
||||
t.doc.appendChild(document.createTextNode("b"));
|
||||
@@ -529,6 +554,64 @@ core.DomUtilsTests = function DomUtilsTests(runner) {
|
||||
r.shouldBe(t, "t.parent.firstChild.childNodes[2].firstChild.textContent", "'test'");
|
||||
}
|
||||
|
||||
function getNodesInRange_EndContainerSkippedByFilter_CompletesIteration() {
|
||||
var range = document.createRange();
|
||||
createNodes("before<span/>middle<span/>after");
|
||||
|
||||
range.setStart(t.doc.childNodes[0], 0);
|
||||
range.setEnd(t.doc.childNodes[3], 0);
|
||||
|
||||
t.nodes = t.utils.getNodesInRange(range, ignoreSpans, NodeFilter.SHOW_ALL);
|
||||
|
||||
r.shouldBe(t, "t.nodes.shift()", "t.doc.childNodes[0]");
|
||||
r.shouldBe(t, "t.nodes.shift()", "t.doc.childNodes[2]");
|
||||
r.shouldBe(t, "t.nodes.shift()", "undefined");
|
||||
range.detach();
|
||||
}
|
||||
|
||||
function getNodesInRange_NodeStartToNodeEnd_ReturnsNode() {
|
||||
var range = document.createRange();
|
||||
createNodes("before<span/>middle<span/>after");
|
||||
|
||||
range.setStart(t.doc.childNodes[0], 0);
|
||||
range.setEnd(t.doc.childNodes[0], t.doc.childNodes[0].length);
|
||||
|
||||
t.nodes = t.utils.getNodesInRange(range, ignoreSpans, NodeFilter.SHOW_ALL);
|
||||
|
||||
r.shouldBe(t, "t.nodes.shift()", "t.doc.childNodes[0]");
|
||||
r.shouldBe(t, "t.nodes.shift()", "undefined");
|
||||
range.detach();
|
||||
}
|
||||
|
||||
function getNodesInRange_NodeEndToNodeStart_ReturnsTouchedNode() {
|
||||
var range = document.createRange();
|
||||
createNodes("before<span/>middle<span/>after");
|
||||
|
||||
range.setStart(t.doc.childNodes[0], t.doc.childNodes[0].length);
|
||||
range.setEnd(t.doc.childNodes[4], 0);
|
||||
|
||||
t.nodes = t.utils.getNodesInRange(range, ignoreSpans, NodeFilter.SHOW_ALL);
|
||||
|
||||
r.shouldBe(t, "t.nodes.shift()", "t.doc.childNodes[2]");
|
||||
r.shouldBe(t, "t.nodes.shift()", "t.doc.childNodes[4]");
|
||||
r.shouldBe(t, "t.nodes.shift()", "undefined");
|
||||
range.detach();
|
||||
}
|
||||
|
||||
function getNodesInRange_NodeEndToNodeEnd_ReturnsBracketedNode() {
|
||||
var range = document.createRange();
|
||||
createNodes("before<span/>middle<span>ignored</span>after");
|
||||
|
||||
range.setStart(t.doc.childNodes[0], t.doc.childNodes[0].length);
|
||||
range.setEnd(t.doc.childNodes[3], 1);
|
||||
|
||||
t.nodes = t.utils.getNodesInRange(range, ignoreSpans, NodeFilter.SHOW_ALL);
|
||||
|
||||
r.shouldBe(t, "t.nodes.shift()", "t.doc.childNodes[2]");
|
||||
r.shouldBe(t, "t.nodes.shift()", "undefined");
|
||||
range.detach();
|
||||
}
|
||||
|
||||
this.tests = function () {
|
||||
return r.name([
|
||||
normalizeTextNodes_TextWithTextSilblings,
|
||||
@@ -560,7 +643,12 @@ core.DomUtilsTests = function DomUtilsTests(runner) {
|
||||
rangeIntersectsRange_RightTouch_ReturnsTrue,
|
||||
|
||||
removeUnwantedNodes_DiscardAll,
|
||||
removeUnwantedNodes_DiscardSpanOnly
|
||||
removeUnwantedNodes_DiscardSpanOnly,
|
||||
|
||||
getNodesInRange_EndContainerSkippedByFilter_CompletesIteration,
|
||||
getNodesInRange_NodeStartToNodeEnd_ReturnsNode,
|
||||
getNodesInRange_NodeEndToNodeStart_ReturnsTouchedNode,
|
||||
getNodesInRange_NodeEndToNodeEnd_ReturnsBracketedNode
|
||||
]);
|
||||
};
|
||||
this.asyncTests = function () {
|
||||
|
||||
Reference in New Issue
Block a user