badsyntax / jquery-spellchecker

[not maintained] A lightweight jQuery plugin that can check the spelling of text within a form field or DOM tree.
http://jquery-spellchecker.badsyntax.co
MIT License
257 stars 113 forks source link

Highlight incorrect words on the wrong position #61

Closed jptannus closed 10 years ago

jptannus commented 10 years ago

I'm using the jquery spellchecker with CKEditor. When I have a lot of empty lines between paragraphs the incorrect word highlight is inserted in a wrong position according to the number of empty lines above it.

The first and right behavior: spellcheckerbug

If I add one empty line before it: spellcheckerbug02

If I add two empty lines before it: spellcheckerbug03

I think it is something in "_stepThroughMatches" method but I'm not sure yet.

jptannus commented 10 years ago

I changed the _stepThroughMatches function that was like this:

if (curNode.nodeType === 3) {
  if (!endNode && curNode.length + atIndex >= matchLocation[1]) {
    // We've found the ending
    endNode = curNode;
    endNodeIndex = matchLocation[1] - atIndex;
  } else if (startNode) {
    // Intersecting node
    innerNodes.push(curNode);
  }
  if (!startNode && curNode.length + atIndex > matchLocation[0]) {
    // We've found the match start
    startNode = curNode;
    startNodeIndex = matchLocation[0] - atIndex;
  }
  atIndex += curNode.length;
}

to this:

if (curNode.nodeType === 3) {
  if (startNode) {
    // Intersecting node
    innerNodes.push(curNode);
  }
  if (!startNode && curNode.length + atIndex > matchLocation[0]) {

    // We've found the match start
    startNode = curNode;
    startNodeIndex = $(curNode).text().indexOf(matchLocation[2][0]);

    endNode = curNode;
    endNodeIndex = startNodeIndex + matchLocation[2][0].length;
  }
  atIndex += curNode.length;
}