When working with a javascript range object, Internet Explorer offers a moveEnd
method that can shift the end of a range by a specified number of sentence units.
How can a similar functionality be achieved in Chrome?
Below is the code I have written that seems to work, but it also highlights beyond the end of the sentence.
var range = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : new Range();
var startNode = document.getElementsByTagName("p")[0];
var endNode = document.getElementsByTagName("p")[0];
var endOffset = endNode.childNodes.length;
range.setStart(startNode, 0);
for (i = 0; i <= endNode.childNodes.length; i++) {
range.setEnd(endNode, i);
if (/.+\./.test(range.toString()))
break;
}
var newNode = document.createElement("span");
newNode.setAttribute('class', 'highlight');
range.surroundContents(newNode);
.highlight {
background-color:yellow;
}
<p><b>John Fitzgerald</b> "<b>Jack</b>" <b>Kennedy</b> (May 29, 1917 – November 22, 1963), commonly referred to by his initials <b>JFK</b>, was an American politician who served as the <a href="/wiki/List_of_Presidents_of_the_United_States" title="List of Presidents of the United States">35th President of the United States</a> from January 1961 until <a href="/wiki/Assassination_of_John_F._Kennedy" title="Assassination of John F. Kennedy">his assassination</a> in November 1963. The <a href="/wiki/Cuban_Missile_Crisis" title="Cuban Missile Crisis">Cuban Missile Crisis</a>, <a href="/wiki/The_Bay_of_Pigs_Invasion" class="mw-redirect" title="The Bay of Pigs Invasion">The Bay of Pigs Invasion</a>, the <a href="/wiki/Partial_Nuclear_Test_Ban_Treaty" title="Partial Nuclear Test Ban Treaty">Nuclear Test Ban Treaty</a>, the establishment of the <a href="/wiki/Peace_Corps" title="Peace Corps">Peace Corps</a>, developments in the <a href="/wiki/Space_Race" title="Space Race">Space Race</a>, the building of the <a href="/wiki/Berlin_Wall" title="Berlin Wall">Berlin Wall</a>, the <a href="/wiki/Trade_Expansion_Act" title="Trade Expansion Act">Trade Expansion Act</a> to lower tariffs, and the <a href="/wiki/African-American_Civil_Rights_Movement_(1955%E2%80%931968)" class="mw-redirect" title="African-American Civil Rights Movement (1955–1968)">Civil Rights Movement</a> all took place during his presidency. A member of the <a href="/wiki/Democratic_Party_(United_States)" title="Democratic Party (United States)">Democratic Party</a>, his <a href="/wiki/New_Frontier" title="New Frontier">New Frontier</a> domestic program was largely enacted as a memorial to him after his death.</p>