Skip to content

Commit

Permalink
Be a bit more conservative with the smiley detection
Browse files Browse the repository at this point in the history
  • Loading branch information
emanchado committed Aug 16, 2011
1 parent 7df99ea commit 161cde0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
12 changes: 10 additions & 2 deletions includes/smileyParser.js
Expand Up @@ -10,9 +10,17 @@ var SmileyParser = function(smileyTable) { this.init(smileyTable) };
var result = string;
for (var smiley in this._smileyTable) {
var self = this;
result = result.replace(new RegExp('(")?' + smiley, "g"),
var regExp = new RegExp('(["a-zA-Z0-9;#])?' + smiley, "g");
result = result.replace(regExp,
function ($0, $1) {
return $1 ? $0 : ("<img alt=\"" + $0 + "\" title=\"" + $0 + "\" src=\"" + self._smileyTable[smiley] + "\" />");
return $1 ?
$0
:
("<img alt=\"" + $0 +
"\" title=\"" + $0 +
"\" src=\"" +
self._smileyTable[smiley] +
"\" />");
});
}
return result;
Expand Down
29 changes: 29 additions & 0 deletions test/spec/smileyParserSpec.js
Expand Up @@ -112,4 +112,33 @@ describe("SmileyParser", function() {
expect(actualOnePass).toEqual(expected);
expect(actualTwoPasses).toEqual(expected);
});

it("should be a bit more picky when detecting smilies", function() {
var myParser = new SmileyParser({"[:;]-?P+\\b": 'wink.png',
":-?C": 'frown.png',
"\\blol\\b|\\bLOL\\b": 'lol.png'});
var source1 = "Newsflash:Catastrophic day for S&amp;P's #lol";
expect(myParser.parseSmileys(source1)).toEqual(source1);
var source2 = "Smilies: :C ;P lol";
var expected2 = "Smilies: <img alt=\":C\" title=\":C\" " +
"src=\"frown.png\" /> <img alt=\";P\" title=\";P\" " +
"src=\"wink.png\" /> <img alt=\"lol\" title=\"lol\" " +
"src=\"lol.png\" />";
expect(myParser.parseSmileys(source2)).toEqual(expected2);
});

it("should detect smilies at the start of the string", function() {
var myParser = new SmileyParser({"[:;]-?P+\\b": 'wink.png',
":-?C": 'frown.png',
"\\blol\\b|\\bLOL\\b": 'lol.png'});
var source2 = ":C";
var source3 = ";P";
var source4 = "lol";
var expected2 = "<img alt=\":C\" title=\":C\" src=\"frown.png\" />";
var expected3 = "<img alt=\";P\" title=\";P\" src=\"wink.png\" />";
var expected4 = "<img alt=\"lol\" title=\"lol\" src=\"lol.png\" />";
expect(myParser.parseSmileys(source2)).toEqual(expected2);
expect(myParser.parseSmileys(source3)).toEqual(expected3);
expect(myParser.parseSmileys(source4)).toEqual(expected4);
});
});

0 comments on commit 161cde0

Please sign in to comment.