diff --git a/src/js/jquery.spellchecker.js b/src/js/jquery.spellchecker.js index 2253051..f07cf64 100644 --- a/src/js/jquery.spellchecker.js +++ b/src/js/jquery.spellchecker.js @@ -450,7 +450,14 @@ text = '' + text; // Typecast to string text = decode(text); // Decode HTML characters text = text.replace(/\xA0|\s+|( )/mg, ' '); // Convert whitespace - text = text.replace(new RegExp('<[^>]+>', 'g'), ''); // Strip HTML tags + + var wordBreakingTags = '(p|li)'; + text = text.replace(new RegExp('<' + wordBreakingTags + '[^>]*>', 'gi'), ' '); // Strip word breaking tags + text = text.replace(new RegExp('<' + wordBreakingTags + '>', 'gi'), ' '); // Strip word breaking tags + text = text.replace(new RegExp('' + wordBreakingTags + ' [^>]*>', 'gi'), ' '); // Strip word breaking tags + text = text.replace(new RegExp('' + wordBreakingTags + '>', 'gi'), ' '); // Strip word breaking tags + + text = text.replace(new RegExp('<[^>]+>', 'g'), ''); // Strip other HTML tags var puncExpr = [ '(^|\\s+)[' + punctuationChars + ']+', // punctuation(s) with leading whitespace(s) @@ -885,7 +892,9 @@ window.findAndReplaceDOMText = (function() { var txt = ''; if (!!(node = node.firstChild)) do { - txt += _getText(node); + var wordBreakingNode = (node.tagName === 'P' || node.tagName === 'LI'); + txt += ((wordBreakingNode ? ' ' : '') + _getText(node)); + } while (!!(node = node.nextSibling)); return txt; @@ -926,6 +935,8 @@ window.findAndReplaceDOMText = (function() { startNodeIndex = matchLocation[0] - atIndex; } atIndex += curNode.length; + } else if (curNode.tagName === 'P' || curNode.tagName === 'LI') { + atIndex += 1; } if (startNode && endNode) { diff --git a/tests/javascript/spec/spellchecker.js b/tests/javascript/spec/spellchecker.js index 66c1bf1..0e56c6f 100644 --- a/tests/javascript/spec/spellchecker.js +++ b/tests/javascript/spec/spellchecker.js @@ -289,6 +289,67 @@ describe("SpellChecker", function() { expect(replaced).toBe('Привет, ты в хорошо? Хотели бы Вы немного кокса? Нет, спасибо, я в хорошо!'); }); }); +describe('Html parser', function() { + + var spellchecker, a, parser; + + beforeEach(function () { + a = $('').appendTo('body'); + spellchecker = newSpellChecker('html', a); + parser = spellchecker.parser; + }); + + afterEach(function() { + spellchecker.destroy(); + a.remove(); + }); + + it('Removes punctuation from text with tags', function() { + + var text1 = '
Hello, this "is" a-test.
How \'are\' you today?
'; + var text2 = 'Hello,
123,
this is a-test. \'456\' How are you today?
'; + var text2 = 'ok 1?
ok 2!
Привет, ты в порядке? Хотели бы Вы немного кокса? Нет, спасибо, я в порядке!