Skip to content
This repository has been archived by the owner on May 30, 2018. It is now read-only.

Commit

Permalink
fix bug with RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
marexandre committed Nov 7, 2015
1 parent b8b98d7 commit 754e55a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-textarea-highlighter",
"version": "0.6.7",
"version": "0.6.8",
"main": "jquery.textarea-highlighter.js",
"ignore": [
"screenshot.png",
Expand Down
5 changes: 5 additions & 0 deletions demo/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ hr {
opacity: .6;
}

.tagsHighlight {
background-color: #99f;
opacity: .6;
}

.match.added,
.hoge.added,
.fuga.added {
Expand Down
15 changes: 9 additions & 6 deletions demo/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ $(function(){
var brackets = ['[[[text here too]]]'];
var glossary = ['here for', 'for they', 'for me', 'here too'];
var misspelling = ['for', 'for them', 'here'];
var tags = /\{\/?\d+\}/g;

$('.translation').find('.target')
.textareaHighlighter({
matches: [
{'matchClass': 'matchHighlight', 'match': brackets, 'priority': 2 },
{'matchClass': 'hogeHighlight', 'match': misspelling, 'priority': 0 },
{'matchClass': 'fugaHighlight', 'match': glossary, 'priority': 1 }
{'matchClass': 'tagsHighlight', 'match': tags, 'priority': 3},
{'matchClass': 'matchHighlight', 'match': brackets, 'priority': 2},
{'matchClass': 'fugaHighlight', 'match': glossary, 'priority': 1},
{'matchClass': 'hogeHighlight', 'match': misspelling, 'priority': 0}
]
});

Expand All @@ -19,9 +21,10 @@ $(function(){
$translationMax.find('.target')
.textareaHighlighter({
matches: [
{'matchClass': 'matchHighlight', 'match': brackets, 'priority': 2 },
{'matchClass': 'hogeHighlight', 'match': misspelling, 'priority': 0 },
{'matchClass': 'fugaHighlight', 'match': glossary, 'priority': 1 }
{'matchClass': 'tagsHighlight', 'match': tags, 'priority': 3},
{'matchClass': 'matchHighlight', 'match': brackets, 'priority': 2},
{'matchClass': 'fugaHighlight', 'match': glossary, 'priority': 1},
{'matchClass': 'hogeHighlight', 'match': misspelling, 'priority': 0}
],
maxlength: 150,
maxlengthWarning: 'warning',
Expand Down
38 changes: 28 additions & 10 deletions jquery.textarea-highlighter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* jquery.textarea-highlighter.js - jQuery plugin for highlighting text in textarea.
* @version v0.6.7
* @version v0.6.8
* @link https://github.com/marexandre/jquery.textarea-highlighter.js
* @author alexandre.kirillov@gmail.com
* @license MIT license. http://opensource.org/licenses/MIT
Expand Down Expand Up @@ -251,6 +251,10 @@ var marexandre;
return '<span class="' + className + '">' + text + '</span>';
};

Helper.prototype.isRegExp = function(v) {
return v instanceof RegExp;
};

/**
* browser returns an Object containing browser information
* @return {Object}
Expand Down Expand Up @@ -607,24 +611,25 @@ var marexandre;
var list = _this.settings.matches;
var indeciesList = [];
var item, trieIndecies;
var matches;
var matches = [];

for (var i = 0, imax = list.length; i < imax; i++) {
item = list[i];

if (!item._trie) {
item._trie = new marexandre.Trie();
if (item.match instanceof RegExp) {
matches = helper.getUniqueArray(text.match(item.match));
} else {

// Add none RegExp matches once, when the trie is initialized
if (!helper.isRegExp(item.match)) {
matches = item.match;
_this.addMatchesToTrie(item._trie, matches);
}
}

// HTML escape matching words
for (var j = 0, jmax = matches.length; j < jmax; j++) {
var m = _this.settings.caseSensitive ? matches[j] : matches[j].toLowerCase();
item._trie.add(helper.escapeHTML(m));
}
// For RegExp matches we need to add them to the trie object
if (helper.isRegExp(item.match)) {
matches = helper.getUniqueArray(text.match(item.match) || []);
_this.addMatchesToTrie(item._trie, matches);
}

var t = _this.settings.caseSensitive ? text : text.toLowerCase();
Expand All @@ -642,6 +647,19 @@ var marexandre;
return helper.createHTML( helper.makeTokenized(text, flattened) );
};

/**
* addMatchesToTrie addes given matches to a given trie
* @param {Trie} trie Trie object
* @param {Array} matches Array of string mathces
*/
TextareaHighlighter.prototype.addMatchesToTrie = function(trie, matches) {
var _this = this;
for (var j = 0, jmax = matches.length; j < jmax; j++) {
var m = _this.settings.caseSensitive ? matches[j] : matches[j].toLowerCase();
trie.add(helper.escapeHTML(m));
}
};

/**
* updateCharLimitElement updates the max chars element with the latest data
* @param {String} text
Expand Down
4 changes: 2 additions & 2 deletions jquery.textarea-highlighter.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jquery.textarea-highlighter.js",
"description": "jQuery plugin for highlighting text in textarea.",
"version": "0.6.7",
"version": "0.6.8",
"homepage": "https://github.com/marexandre/jquery.textarea-highlighter.js",
"license": "MIT license. http://opensource.org/licenses/MIT",
"author": {
Expand Down
17 changes: 17 additions & 0 deletions spec/javascripts/jquery.textarea-highlighter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ describe('jquery.textarea-highlighter', function() {
expect( $segment.find('.background-div').html() ).toBe(html);
});

it('should not error with RegExp as matches, and when there is no matching text in the target', function() {
var $target = $segment.find('#target-fixture');

$target
.val('Hi ')
.textareaHighlighter({
matches: [
{ 'matchClass': 'tags', 'match': /\{\/?\d+\}/g }
]
});

var html = '';
html += 'Hi ';

expect( $segment.find('.background-div').html() ).toBe(html);
});

it('should highlight case sensitive content correctly', function() {
var $target = $segment.find('#target-fixture');

Expand Down
4 changes: 4 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ var marexandre;
return '<span class="' + className + '">' + text + '</span>';
};

Helper.prototype.isRegExp = function(v) {
return v instanceof RegExp;
};

/**
* browser returns an Object containing browser information
* @return {Object}
Expand Down
32 changes: 23 additions & 9 deletions src/jquery.textarea-highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,25 @@ var marexandre;
var list = _this.settings.matches;
var indeciesList = [];
var item, trieIndecies;
var matches;
var matches = [];

for (var i = 0, imax = list.length; i < imax; i++) {
item = list[i];

if (!item._trie) {
item._trie = new marexandre.Trie();
if (item.match instanceof RegExp) {
matches = helper.getUniqueArray(text.match(item.match));
} else {

// Add none RegExp matches once, when the trie is initialized
if (!helper.isRegExp(item.match)) {
matches = item.match;
_this.addMatchesToTrie(item._trie, matches);
}
}

// HTML escape matching words
for (var j = 0, jmax = matches.length; j < jmax; j++) {
var m = _this.settings.caseSensitive ? matches[j] : matches[j].toLowerCase();
item._trie.add(helper.escapeHTML(m));
}
// For RegExp matches we need to add them to the trie object
if (helper.isRegExp(item.match)) {
matches = helper.getUniqueArray(text.match(item.match) || []);
_this.addMatchesToTrie(item._trie, matches);
}

var t = _this.settings.caseSensitive ? text : text.toLowerCase();
Expand All @@ -216,6 +217,19 @@ var marexandre;
return helper.createHTML( helper.makeTokenized(text, flattened) );
};

/**
* addMatchesToTrie addes given matches to a given trie
* @param {Trie} trie Trie object
* @param {Array} matches Array of string mathces
*/
TextareaHighlighter.prototype.addMatchesToTrie = function(trie, matches) {
var _this = this;
for (var j = 0, jmax = matches.length; j < jmax; j++) {
var m = _this.settings.caseSensitive ? matches[j] : matches[j].toLowerCase();
trie.add(helper.escapeHTML(m));
}
};

/**
* updateCharLimitElement updates the max chars element with the latest data
* @param {String} text
Expand Down

0 comments on commit 754e55a

Please sign in to comment.