Archive for April, 2014

Manipulating text relative to the caret in a contenteditable div

I wanted to play around a bit with dynamically modifying text as you type. The following is a simple auto-correct demo that makes use of the Selection and Range interfaces to replace text (read: text preceding to the caret) within a contenteditable div.

$(document).on('keydown', '.ia-txt', function (e) {
// check if space bar was hit
if(e.keyCode == 32) {
// we'll check for the string "hwat"; incorrect form of "what"
var incorrectTxt = "hwat";
// Get selection and range based on position of caret
        // (we assume nothing is selected, and range points to the position of the caret)
var sel = window.getSelection();
var range = sel.getRangeAt(0);
// check that we have at least incorrectTxt.length characters in our container
if(range.startOffset - incorrectTxt.length >= 0) {
// clone the range, so we can alter the start and end
var clone = range.cloneRange();
// alter start and end of cloned ranged, so it selects incorrectTxt.length characters
clone.setStart(range.startContainer, range.startOffset - incorrectTxt.length);
            clone.setEnd(range.startContainer, range.startOffset);

// get contents of cloned range
var contents = clone.toString();                    
// check if the contents of the cloned range is equal to our incorrectTxt string
if(contents == incorrectTxt) {
// delete the contents of the range ("hwat")
// create a text node with the corrected text ("what") and insert it where we deleted the incorrect text
var txtNode = document.createTextNode("what");
// set the start of the range after the inserted node, so we have the caret after the inserted text
// Chrome fix



You can see the code in action in the frame below. Every time you press the space-bar and the string “hwat” is detected, preceding the position of the caret, it is removed and replaced with the string “what”:

This is an incredibly trivial example (note that it doesn’t even check that the string “hwat” is surrounded by whitespace on both sides), but it does serve as a template for more advanced functionality. That said, be very aware of minor differences in the behavior of Range methods when working across browsers, I stumbled across a few:

  • The code above breaks under certain conditions in Internet Explorer. If you move the caret to a position between 2 works, type “hwat” + space (the string is auto-corrected to “what”), then type “hwat” + space again, the auto-correct doesn’t work. The range.startOffset variable seems incorrect (too small) and subtracting incorrectTxt.length (4) yields a negative start offset.
  • Using a keyup event instead of a keydown event, and checking for the string “hwat ” instead yields different behaviors in Firefox and Chrome. Firefox preserves the space after the corrected string, and the caret is at the position after the space. However, Chrome strips the space and the caret is after the corrected string.
  • After the selection’s range is altered after auto-correcting, Chrome requires the removeAllRanges(), addRange() calls to replace the selection’s range, but Firefox does not.