diff --git a/lib/rangy-textrange.js b/lib/rangy-textrange.js index 6e380a12..f2768911 100644 --- a/lib/rangy-textrange.js +++ b/lib/rangy-textrange.js @@ -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; diff --git a/src/modules/rangy-textrange.js b/src/modules/rangy-textrange.js index f166d570..2244e4e6 100644 --- a/src/modules/rangy-textrange.js +++ b/src/modules/rangy-textrange.js @@ -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; diff --git a/test/textrangetests.js b/test/textrangetests.js index 60e0000a..4af0e056 100644 --- a/test/textrangetests.js +++ b/test/textrangetests.js @@ -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;