Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/rangy-textrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* - Ability to move range boundaries by character or word offsets
* - Customizable word tokenizer
* - Ignores text nodes inside <script> or <style> elements or those hidden by CSS display and visibility properties
* - Ignores text nodes inside <script> or <style> elements
* - Range findText method to search for text or regex within the page or within a range. Flags for whole words and case
* sensitivity
* - Selection and range save/restore as text offsets within a node
Expand Down Expand Up @@ -101,6 +101,9 @@
var trailingSpaceBeforeBlockCollapses = false;
var trailingSpaceBeforeLineBreakInPreLineCollapses = true;

// This test code runs at load time, but generates side effects
// on the selection, which is bad. Hence, we disable it.
/*
(function() {
var el = dom.createTestElement(document, "<p>1 </p><p></p>", true);
var p = el.firstChild;
Expand All @@ -122,6 +125,7 @@
dom.removeNode(el);
sel.removeAllRanges();
})();
*/

/*----------------------------------------------------------------------------------------------------------------*/

Expand Down Expand Up @@ -468,6 +472,14 @@
isCollapsedWhitespaceNode(node);
}

function isIgnorableTextContainer(node) {
var type = node.nodeType;
return type == 7 /* PROCESSING_INSTRUCTION */ ||
type == 8 /* COMMENT */ ||
/^(script|style)$/i.test(node.nodeName) ||
isCollapsedWhitespaceNode(node);
}

function isIgnoredNode(node, win) {
var type = node.nodeType;
return type == 7 /* PROCESSING_INSTRUCTION */ ||
Expand Down Expand Up @@ -553,6 +565,7 @@
getComputedDisplay: createCachingGetter("computedDisplay", getComputedDisplay, "node"),
isCollapsed: createCachingGetter("collapsed", isCollapsedNode, "node"),
isIgnored: createCachingGetter("ignored", isIgnoredNode, "node"),
isIgnorableTextContainer: createCachingGetter("isIgnorableTextContainer", isIgnorableTextContainer, "node"),
next: createCachingGetter("nextPos", nextNode, "node"),
previous: createCachingGetter("previous", previousNode, "node"),

Expand Down Expand Up @@ -1002,8 +1015,9 @@
/*
Next and previous position moving functions that filter out

- Hidden (CSS visibility/display) elements
- Comment nodes and processing nodes
- Script and style elements
- nodes that contain only whitespace
*/
nextVisible: createCachingGetter("nextVisible", function(pos) {
var next = pos.next();
Expand All @@ -1012,7 +1026,7 @@
}
var nodeWrapper = next.nodeWrapper, node = next.node;
var newPos = next;
if (nodeWrapper.isCollapsed()) {
if (nodeWrapper.isIgnorableTextContainer()) {
// We're skipping this node and all its descendants
newPos = nodeWrapper.session.getPosition(node.parentNode, nodeWrapper.getNodeIndex() + 1);
}
Expand All @@ -1037,7 +1051,7 @@
}
var nodeWrapper = previous.nodeWrapper, node = previous.node;
var newPos = previous;
if (nodeWrapper.isCollapsed()) {
if (nodeWrapper.isIgnorableTextContainer()) {
// We're skipping this node and all its descendants
newPos = nodeWrapper.session.getPosition(node.parentNode, nodeWrapper.getNodeIndex());
}
Expand Down Expand Up @@ -1710,7 +1724,7 @@
// Create a range representing the search scope if none was provided
var searchScopeRange = findOptions.withinRange;
if (!searchScopeRange) {
searchScopeRange = api.createRange();
searchScopeRange = api.createRange(this.getDocument());
searchScopeRange.selectNodeContents(this.getDocument());
}

Expand Down Expand Up @@ -1927,4 +1941,4 @@
});

return rangy;
}, this);
}, this);
10 changes: 8 additions & 2 deletions src/modules/rangy-textrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
var trailingSpaceBeforeBlockCollapses = false;
var trailingSpaceBeforeLineBreakInPreLineCollapses = true;

// This test code runs at load time, but generates side effects
// on the selection, which is bad. Hence, we disable it.
/*
(function() {
// This test code runs at load time, but generates side effects
// on the selection, which is bad. Hence, we disable it.
var el = dom.createTestElement(document, "<p>1 </p><p></p>", true);
var p = el.firstChild;
var sel = api.getSelection();
Expand All @@ -112,6 +117,7 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
dom.removeNode(el);
sel.removeAllRanges();
})();
*/

/*----------------------------------------------------------------------------------------------------------------*/

Expand Down Expand Up @@ -1776,7 +1782,7 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
// Create a range representing the search scope if none was provided
var searchScopeRange = findOptions.withinRange;
if (!searchScopeRange) {
searchScopeRange = api.createRange();
searchScopeRange = api.createRange(this.getDocument());
searchScopeRange.selectNodeContents(this.getDocument());
}

Expand Down Expand Up @@ -1996,4 +2002,4 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
)
};
});
/* build:modularizeEnd */
/* build:modularizeEnd */