Skip to content

#26, words did not get highlighted correctly in rich editors. #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/js/jquery.spellchecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
61 changes: 61 additions & 0 deletions tests/javascript/spec/spellchecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,67 @@ describe("SpellChecker", function() {
expect(replaced).toBe('Привет, ты в хорошо? Хотели бы Вы немного кокса? Нет, спасибо, я в хорошо!');
});
});
describe('Html parser', function() {

var spellchecker, a, parser;

beforeEach(function () {
a = $('<a id="test1" />').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 = '<p><b>Hello</b>, this "is" a-test.</p><P>How \'are\' you today?</P>';
var text2 = '<ul><li>test!</li><li>test.</li></ul>';
var cleaned1 = parser.clean(text1);
var cleaned2 = parser.clean(text2);

expect(cleaned1).toBe('Hello this is a-test How are you today');
expect(cleaned2).toBe('test test');
});

it('Removes numbers from text with tags', function() {

var text1 = '<p><b>Hello</b>,</p><p>123,</p><p>this is a-test. \'456\' How are you today?</p>';
var text2 = '<ul><li>test 1</li><li>test 2</li><ul>';
var cleaned1 = parser.clean(text1);
var cleaned2 = parser.clean(text2);

expect(cleaned1).toBe('Hello this is a-test How are you today');
expect(cleaned2).toBe('test test');
});

it('Replaces a word multiple times in a simple node', function() {
var text = $('<div>are you ok?</div>');
parser.replaceWord('ok', 'good', text);
var replaced = text.text();

expect(replaced).toBe('are you good?');
});

it('Replaces a word multiple times in a node', function () {
var text = $('<div><p class="someClass">o<b>k</b> 1?</p><P>ok 2!</P><ul><li someAttribute=true>ok 3?</li><LI><u>ok</u> 4!</LI></ul></div>');
parser.replaceWord('ok', 'good', text);
var replaced = text.text();

expect(replaced).toBe('good 1?good 2!good 3?good 4!');
});

it('Replaces a Unicode word multiple times in a node', function() {
var text = $('<div><p>Привет, ты в порядке? Хотели бы Вы немного кокса? Нет, спасибо, я в порядке!</p></div>');
parser.replaceWord('порядке', 'хорошо', text);
var replaced = text.text();

expect(replaced).toBe('Привет, ты в хорошо? Хотели бы Вы немного кокса? Нет, спасибо, я в хорошо!');
});
});

describe('Public methods', function() {

Expand Down