Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextRange module's findText fix for text with non-breaking space. #405

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions lib/rangy-textrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,13 @@
currentChar = currentChar.toLowerCase();
}

// If there is a non-breaking space in the innerText, then replace with empty space " " so that it
// matches with the searchTerm. Based on the documentation, search should be performed on
// the visible text of the document
if (currentChar == "\u00a0") {
currentChar = " ";
}

if (backward) {
chars.unshift(pos);
text = currentChar + text;
Expand Down
7 changes: 7 additions & 0 deletions src/modules/rangy-textrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,13 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
currentChar = currentChar.toLowerCase();
}

// If there is a non-breaking space in the innerText, then replace with empty space " " so that it
// matches with the searchTerm. Based on the documentation, search should be performed on
// the visible text of the document
if (currentChar == "\u00a0") {
currentChar = " ";
}

if (backward) {
chars.unshift(pos);
text = currentChar + text;
Expand Down
36 changes: 36 additions & 0 deletions test/textrangetests.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,42 @@ xn.test.suite("Text Range module tests", function(s) {
t.assertFalse(range.findText("Two", options));
});

s.test("findText text with non-breaking space", function(t) {
t.el.innerHTML = 'One Two three';
var textNode = t.el.firstChild;
var range = rangy.createRange();
range.collapseToPoint(textNode, 0);

var scopeRange = rangy.createRange();
scopeRange.selectNodeContents(t.el);
var options = {
withinRange: scopeRange
};

t.assert(range.findText("Two ", options));
testRangeBoundaries(t, range, textNode, 4, textNode, 8);
range.collapse(false);
t.assertFalse(range.findText("Two", options));
});

s.test("findText text with non-breaking space and normal space", function(t) {
t.el.innerHTML = 'One Two  three';
var textNode = t.el.firstChild;
var range = rangy.createRange();
range.collapseToPoint(textNode, 0);

var scopeRange = rangy.createRange();
scopeRange.selectNodeContents(t.el);
var options = {
withinRange: scopeRange
};

t.assert(range.findText("Two three", options));
testRangeBoundaries(t, range, textNode, 4, textNode, 14);
range.collapse(false);
t.assertFalse(range.findText("Two", options));
});

s.test("findText simple text no wrap", function(t) {
t.el.innerHTML = 'Two One Two three';
var textNode = t.el.firstChild;
Expand Down