Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make original text smilies selectable (closes #2)

* Make smilies a background image of an <abbrev> element with transparent text.
  This leaves the original text in place, so copying and pasting will keep the
  original text smiley
* Updated tests. Now they're probably a bit more fragile than they were, but it
  shouldn't be too bad.
  • Loading branch information...
commit bcdba4a19c6d2bc9a11ca9b470b2c3a825b5233e 1 parent f55a571
Esteban Manchado Velázquez authored
69 includes/smileyParser.js
... ... @@ -1,33 +1,46 @@
1 1 var SmileyParser = function(smileyTable) { this.init(smileyTable); };
2 2 (function ()
3 3 {
4   - this._smileyTable = {};
5   - this.init = function(smileyTable) {
6   - this._smileyTable = smileyTable || defaultSmileyTable;
7   - this._handlers = [];
8   - for (var smiley in this._smileyTable) {
9   - if (this._smileyTable.hasOwnProperty(smiley)) {
10   - var self = this;
11   - this._handlers.push({pattern: new RegExp('(["a-zA-Z0-9#])?' + smiley, "g"),
12   - replacement: (function (s) {
13   - return function(text, lb) {
14   - if (lb) return document.createTextNode(text);
15   - var img =
16   - document.createElement('img');
17   - img.src = self._smileyTable[s];
18   - img.alt = img.title =
19   - text.replace(/&/g, '&amp;').
20   - replace(/</g, '&lt;').
21   - replace(/>/g, '&gt;');
22   - return img;
23   - };
24   - })(smiley)});
25   - }
26   - }
27   - };
  4 + this._smileyTable = {};
  5 + this.init = function(smileyTable) {
  6 + /* Returns a function that receives the matched text and the
  7 + first matched group in the regular expression. The first group
  8 + is always a fake lookbehind. If there's anything, we should
  9 + return the text back, without creating any graphical smiley */
  10 + function replaceFunction(s) {
  11 + return function(text, lb) {
  12 + if (lb) return document.createTextNode(text);
  13 + var smileyInfo = self._smileyTable[s];
  14 + var smileyDom = document.createElement('abbrev');
  15 + smileyDom.innerHTML = text.replace(/&/g, '&amp;').
  16 + replace(/</g, '&lt;').
  17 + replace(/>/g, '&gt;');
  18 + var url = "data:image/png;base64," + smileyInfo.imageDataBase64;
  19 + smileyDom.style.backgroundImage = "url(" + url + ")";
  20 + if (smileyInfo.height !== undefined)
  21 + smileyDom.style.height = smileyInfo.height + "px";
  22 + if (smileyInfo.width !== undefined)
  23 + smileyDom.style.width = smileyInfo.width + "px";
  24 + smileyDom.style.display = 'inline-block';
  25 + smileyDom.style.color = 'transparent';
  26 + smileyDom.style.overflow = 'hidden';
  27 + return smileyDom;
  28 + };
  29 + }
  30 + this._smileyTable = smileyTable || defaultSmileyTable;
  31 + this._handlers = [];
  32 + for (var smiley in this._smileyTable) {
  33 + if (this._smileyTable.hasOwnProperty(smiley)) {
  34 + var self = this;
  35 + this._handlers.push({pattern: new RegExp('(["a-zA-Z0-9#])?' +
  36 + smiley, "g"),
  37 + replacement: replaceFunction(smiley)});
  38 + }
  39 + }
  40 + };
28 41
29   - this.parseSmileys = function(element) {
30   - replaceTextWithElements(element, {excludedTags: /textarea/,
31   - handlers: this._handlers});
32   - };
  42 + this.parseSmileys = function(element) {
  43 + replaceTextWithElements(element, {excludedTags: /textarea/,
  44 + handlers: this._handlers});
  45 + };
33 46 }).call(SmileyParser.prototype);
16 test.html
@@ -3,14 +3,6 @@
3 3 <head>
4 4 <title>Smiley parsing test</title>
5 5 <meta charset='utf-8'>
6   - <script src="includes/defaultSmileyTable.js" />
7   - <script src="includes/smileyParser.js" />
8   - <script>
9   - window.addEventListener('load', function() {
10   - var myOwnParser = new SmileyParser();
11   - // alert(myOwnParser.parseSmileys('lol'));
12   - }, false);
13   - </script>
14 6 </head>
15 7 <body style="background-color: #ddd">
16 8 <div class="commentContent">o_o O_O O_o o_O / foo fOo food oO Oo oo OO o-o</div>
@@ -21,10 +13,16 @@
21 13 <div class="commentContent">&lt;/troll&gt; &lt;/trolling&gt; :-7 / -troll- *troll* troll</div>
22 14 <div class="commentContent">^_^ ;) ;-)) :-P ;-P :P ;P :) :-) :))) :( :(((( :-( ;p ;P ;d ;D / ;proper ;Proper ;dice ;Dice</div>
23 15 <div class="commentContent">:? :-? :??? :-??? ;'( :'( :'(( :C :-C / :c :-c</div>
24   - <div class="commentContent">X'D X-D XD x'D x-D xD :D :-D :DDD / :d :-d</div>
  16 + <div class="commentContent">X'D X-D XD x'D x-D xD :D :-D :DDD :-DDDDDDDDDDDDDDDDDDD / :d :-d</div>
25 17 <div class="commentContent">lol LOL лол / LoL lOl trolol trolololo лоласо тролололо трололол</div>
26 18 <div class="commentContent"><a href=" lol ">link lol</a></div>
27 19
  20 + <h1>Random test</h1>
  21 +
  22 +<div class="comment">
  23 + /me doesn't think @ErikRose is a follower on twitter … I can't dm him ;-) See you in the tomorrow with bike shorts on
  24 +</div>
  25 +
28 26 <h1>Addons.opera.com comment test</h1>
29 27
30 28 <div class="comment">
62 test/lib/htmlComparison.js
... ... @@ -1,28 +1,52 @@
  1 +function canonicalElementHtml(element) {
  2 + var result = "<" + element.nodeName.toLowerCase();
  3 + var attrPairs = [];
  4 + for (var ai = 0, alen = element.attributes.length; ai < alen; ++ai) {
  5 + attrPairs.push([ element.attributes[ai].name,
  6 + element.attributes[ai].value ]);
  7 + }
  8 + // Order the attributes
  9 + var orderedAttrPairs = attrPairs.sort(function(a, b) {
  10 + if (a[0] === b[0]) return 0;
  11 + return (a[0] > b[0]) ? 1 : -1;
  12 + });
  13 + for (var ai2 = 0, alen2 = orderedAttrPairs.length; ai2 < alen2; ++ai2) {
  14 + result += " " + orderedAttrPairs[ai2][0] + '="' + orderedAttrPairs[ai2][1] + '"';
  15 + }
  16 + if (element.childNodes.length > 0) {
  17 + result += ">";
  18 + result += canonicalHtml(element) +
  19 + "</" + element.nodeName.toLowerCase() + ">";
  20 + } else {
  21 + result += " />";
  22 + }
  23 + return result;
  24 +}
  25 +
  26 +
  27 +function canonicalSmiley(element) {
  28 + console.log(element.textContent);
  29 + return "<smiley url=\"" + element.style.backgroundImage + "\"" +
  30 + ((element.style.height !== undefined) ?
  31 + " height=\"" + element.style.height + "\"" : "") +
  32 + ((element.style.width !== undefined) ?
  33 + " width=\"" + element.style.width + "\"" : "") +
  34 + ">" + element.textContent.replace(/&/g, '&amp;').
  35 + replace(/</g, '&lt;').
  36 + replace(/>/g, '&gt;') +
  37 + "</smiley>";
  38 +}
  39 +
  40 +
1 41 function canonicalHtml(element) {
2 42 var result = "";
3 43 var cn = element.childNodes;
4 44 for (var i = 0, len = cn.length; i < len; ++i) {
5 45 if (cn[i] instanceof Element) {
6   - result += "<" + cn[i].nodeName.toLowerCase();
7   - var attrPairs = [];
8   - for (var ai = 0, alen = cn[i].attributes.length; ai < alen; ++ai) {
9   - attrPairs.push([ cn[i].attributes[ai].name,
10   - cn[i].attributes[ai].value ]);
11   - }
12   - // Order the attributes
13   - var orderedAttrPairs = attrPairs.sort(function(a, b) {
14   - if (a[0] === b[0]) return 0;
15   - return (a[0] > b[0]) ? 1 : -1;
16   - });
17   - for (var ai2 = 0, alen2 = orderedAttrPairs.length; ai2 < alen2; ++ai2) {
18   - result += " " + orderedAttrPairs[ai2][0] + '="' + orderedAttrPairs[ai2][1] + '"';
19   - }
20   - if (cn[i].childNodes.length > 0) {
21   - result += ">";
22   - result += canonicalHtml(cn[i]) +
23   - "</" + cn[i].nodeName.toLowerCase() + ">";
  46 + if (cn[i].nodeName.toLowerCase() === 'abbrev') {
  47 + result += canonicalSmiley(cn[i]);
24 48 } else {
25   - result += " />";
  49 + result += canonicalElementHtml(cn[i]);
26 50 }
27 51 } else {
28 52 result += cn[i].nodeValue.replace(/&/g, '&amp;').
91 test/spec/htmlComparisonSpec.js
... ... @@ -1,41 +1,68 @@
  1 +function isHtmlEquivalent(one, two) {
  2 + var domElement = document.createElement('div');
  3 + var domElement2 = document.createElement('div');
  4 + domElement.innerHTML = one;
  5 + domElement2.innerHTML = two;
  6 + return canonicalHtml(domElement) === canonicalHtml(domElement2);
  7 +}
  8 +
1 9 describe("canonicalHtml", function() {
2   - var domElement;
  10 + beforeEach(function() {
  11 + this.addMatchers({
  12 + toBeCanonically: function(expected) {
  13 + return isHtmlEquivalent(this.actual, expected);
  14 + },
  15 + toNotBeCanonically: function(expected) {
  16 + return ! isHtmlEquivalent(this.actual, expected);
  17 + }
  18 + });
  19 + });
3 20
4   - beforeEach(function() {
5   - domElement = document.createElement('div');
  21 + it("should return the same string when there isn't any markup", function() {
  22 + var someString = "Some string without any markup";
  23 + expect(someString).toBeCanonically(someString);
  24 + });
6 25
7   - this.addMatchers({
8   - toBeCanonically: function(expected) {
9   - domElement.innerHTML = this.actual;
10   - return this.env.equals_(canonicalHtml(domElement), expected);
11   - }
12   - });
13   - });
  26 + it("should return the same string when there are simple elements", function() {
  27 + var someString = "Some string with <strong>some</strong> markup";
  28 + expect(someString).toBeCanonically(someString);
  29 + });
14 30
15   - it("should return the same string when there isn't any markup", function() {
16   - var someString = "Some string without any markup";
17   - expect(someString).toBeCanonically(someString);
18   - });
  31 + it("should return attributes in alphabetical order", function() {
  32 + var source = 'Some <a title="foo" href="http://example.com">link</a>';
  33 + var expected = 'Some <a href="http://example.com" title="foo">link</a>';
  34 + expect(source).toBeCanonically(expected);
  35 + });
19 36
20   - it("should return the same string when there are simple elements", function() {
21   - var someString = "Some string with <strong>some</strong> markup";
22   - expect(someString).toBeCanonically(someString);
23   - });
  37 + it("should return self-closing tags correctly", function() {
  38 + var source = 'An <img src="image.png">';
  39 + var expected = 'An <img src="image.png" />';
  40 + expect(source).toBeCanonically(expected);
  41 + });
24 42
25   - it("should return attributes in alphabetical order", function() {
26   - var source = 'Some <a title="foo" href="http://example.com">link</a>';
27   - var expected = 'Some <a href="http://example.com" title="foo">link</a>';
28   - expect(source).toBeCanonically(expected);
29   - });
  43 + it("should return HTML entities correctly", function() {
  44 + var source = 'A Smith &amp; Wesson weapon';
  45 + expect(source).toBeCanonically(source);
  46 + });
30 47
31   - it("should return self-closing tags correctly", function() {
32   - var source = 'An <img src="image.png">';
33   - var expected = 'An <img src="image.png" />';
34   - expect(source).toBeCanonically(expected);
35   - });
  48 + it("should recognise a simple smiley correctly", function() {
  49 + var source1 = 'A smiley: <abbrev style="background-image: url(foo.png); width: 30px; height: 25px">some text</abbrev>';
  50 + var source2 = 'A smiley: <abbrev style="background-image: url(foo.png); height: 25px; width: 30px">some text</abbrev>';
  51 + var source3 = 'A smiley: <abbrev style="background-image: url(foo.png); height: 25px; width: 30px">some other text</abbrev>';
  52 + expect(source1).toBeCanonically(source2);
  53 + expect(source1).toNotBeCanonically(source3);
  54 + expect(source2).toNotBeCanonically(source3);
  55 + });
36 56
37   - it("should return HTML entities correctly", function() {
38   - var source = 'A Smith &amp; Wesson weapon';
39   - expect(source).toBeCanonically(source);
40   - });
  57 + it("should recognise a smiley w/ extra properties as different", function() {
  58 + var source1 = 'A smiley: <abbrev style="background-image: url(foo.png); width: 30px">some text</abbrev>';
  59 + var source2 = 'A smiley: <abbrev style="background-image: url(foo.png); width: 30px; height: 25px">some text</abbrev>';
  60 + expect(source1).toNotBeCanonically(source2);
  61 + });
  62 +
  63 + it("should recognise a smiley w/ angle brackets", function() {
  64 + var source1 = 'A smiley: <abbrev style="background-image: url(foo.png); height: 25px; width: 30px">&lt;/troll&gt;</abbrev>';
  65 + var source2 = 'A smiley: <abbrev style="background-image: url(foo.png); width: 30px; height: 25px">&lt;/troll&gt;</abbrev>';
  66 + expect(source1).toBeCanonically(source2);
  67 + });
41 68 });
322 test/spec/smileyParserSpec.js
... ... @@ -1,173 +1,179 @@
  1 +function smileyMarkup(text, url) {
  2 + return "<abbrev style=\"background-image: url(data:image/png;base64," +
  3 + url + ")\">" + text + "</abbrev>";
  4 +}
  5 +
  6 +
1 7 describe("SmileyParser", function() {
2   - var parser, domElement;
3   -
4   - beforeEach(function() {
5   - parser = new SmileyParser({"\\bxD+\\b": "lol.png",
6   - ":-?\\)": "smile.png"});
7   - domElement = document.createElement('div');
8   -
9   - this.addMatchers({
10   - toParseTo: function(expected, p) {
11   - if (p === undefined)
12   - p = parser;
13   - domElement.innerHTML = this.actual;
14   - p.parseSmileys(domElement);
15   - return this.env.equals_(canonicalHtml(domElement), expected);
16   - }
17   - });
18   - });
  8 + var parser, domElement, domElement2;
19 9
20   - it("should return the same string when there aren't any smileys", function() {
21   - var someString = "Some string without any smileys";
22   - expect(someString).toParseTo(someString);
23   - });
  10 + beforeEach(function() {
  11 + parser = new SmileyParser({"\\bxD+\\b": {imageDataBase64: "lol"},
  12 + ":-?\\)": {imageDataBase64: "smile"}});
  13 + domElement = document.createElement('div');
  14 + domElement2 = document.createElement('div');
24 15
25   - it("should not be confused with text that looks like a smiley", function() {
26   - var someString = "FedEx or FexDex?";
27   - expect(someString).toParseTo(someString);
  16 + this.addMatchers({
  17 + toParseTo: function(expected, p) {
  18 + if (p === undefined)
  19 + p = parser;
  20 + domElement.innerHTML = this.actual;
  21 + domElement2.innerHTML = expected;
  22 + p.parseSmileys(domElement);
  23 + console.log('Initially it\'s ' + domElement.innerHTML + ', to be compared to ', expected);
  24 + console.log('Expected to parse to ' + canonicalHtml(domElement2) + ', was ', canonicalHtml(domElement));
  25 + return this.env.equals_(canonicalHtml(domElement), canonicalHtml(domElement2));
  26 + }
28 27 });
  28 + });
29 29
30   - it("should replace simple smileys", function() {
31   - var source = "I LOLed xD";
32   - var expected = "I LOLed <img alt=\"xD\" src=\"lol.png\" " +
33   - "title=\"xD\" />";
34   - expect(source).toParseTo(expected);
35   - });
  30 + it("should return the same string when there aren't any smileys", function() {
  31 + var someString = "Some string without any smileys";
  32 + expect(someString).toParseTo(someString);
  33 + });
36 34
37   - it("should allow smileys to be configured", function() {
38   - var myParser = new SmileyParser({":-?\\(": "frown.png"});
39   - var source = "I didn't LOL (xD) :-(";
40   - var expectedStandard = "I didn't LOL (<img alt=\"xD\" " +
41   - "src=\"lol.png\" title=\"xD\" />) :-(";
42   - var expectedConfigured = "I didn't LOL (xD) <img alt=\":-(\" " +
43   - "src=\"frown.png\" title=\":-(\" />";
44   - expect(source).toParseTo(expectedStandard);
45   - expect(source).toParseTo(expectedConfigured, myParser);
46   - });
  35 + it("should not be confused with text that looks like a smiley", function() {
  36 + var someString = "FedEx or FexDex?";
  37 + expect(someString).toParseTo(someString);
  38 + });
47 39
48   - it("should replace many smileys", function() {
49   - var source = "I LOLed xD :-) xDD";
50   - var expected = "I LOLed <img alt=\"xD\" src=\"lol.png\" " +
51   - "title=\"xD\" /> " +
52   - "<img alt=\":-)\" src=\"smile.png\" title=\":-)\" /> " +
53   - "<img alt=\"xDD\" src=\"lol.png\" title=\"xDD\" />";
54   - expect(source).toParseTo(expected);
55   - });
  40 + it("should replace simple smileys", function() {
  41 + var source = "I LOLed xD";
  42 + var expected = "I LOLed " + smileyMarkup("xD", "lol");
  43 + expect(source).toParseTo(expected);
  44 + });
56 45
57   - it("should be idempotent", function() {
58   - var source = "I LOLed xD :-) xDD";
59   - var expected = "I LOLed <img alt=\"xD\" src=\"lol.png\" " +
60   - "title=\"xD\" /> " +
61   - "<img alt=\":-)\" src=\"smile.png\" title=\":-)\" /> " +
62   - "<img alt=\"xDD\" src=\"lol.png\" title=\"xDD\" />";
63   - var tmpDiv = document.createElement('div');
64   - tmpDiv.innerHTML = source;
65   - parser.parseSmileys(tmpDiv);
66   - var actualOnePass = tmpDiv.innerHTML;
67   - expect(source).toParseTo(expected);
68   - expect(actualOnePass).toParseTo(expected);
69   - });
  46 + it("should allow smileys to be configured", function() {
  47 + var myParser = new SmileyParser({":-?\\(": {imageDataBase64: "frown"}});
  48 + var source = "I didn't LOL (xD) :-(";
  49 + var expectedStandard = "I didn't LOL (" + smileyMarkup("xD", "lol") +
  50 + ") :-(";
  51 + var expectedConfigured = "I didn't LOL (xD) " +
  52 + smileyMarkup(":-(", "frown");
  53 + expect(source).toParseTo(expectedStandard);
  54 + expect(source).toParseTo(expectedConfigured, myParser);
  55 + });
70 56
71   - it("should play well with ;))", function() {
72   - var winkParser = new SmileyParser({"\\bxD+\\b": "lol.png",
73   - ";-?\\)+": "wink.png"});
74   - var source = "Jajaja si, puede ser una solución, pero admito mas ideas... ;))";
75   - var expected = "Jajaja si, puede ser una solución, pero admito mas ideas... <img alt=\";))\" src=\"wink.png\" " +
76   - "title=\";))\" />";
77   - var tmpDiv = document.createElement('div');
78   - tmpDiv.innerHTML = source;
79   - winkParser.parseSmileys(tmpDiv);
80   - var actualOnePass = tmpDiv.innerHTML;
81   - expect(source).toParseTo(expected, winkParser);
82   - expect(actualOnePass).toParseTo(expected, winkParser);
83   - });
  57 + it("should replace many smileys", function() {
  58 + var source = "I LOLed xD :-) xDD";
  59 + var expected = "I LOLed " + smileyMarkup("xD", "lol") + " " +
  60 + smileyMarkup(":-)", "smile") + " " +
  61 + smileyMarkup("xDD", "lol");
  62 + expect(source).toParseTo(expected);
  63 + });
84 64
85   - it("should play well with character repetitions", function() {
86   - var winkParser = new SmileyParser({
87   - ":-?\\)+": 'imgHappy.png',
88   - ":-?\\(\\(+": 'imgOhNo.png',
89   - "[:;]-?P+\\b": 'imgLick.png',
90   - "[:;]'\\(+": 'imgCry.png',
91   - ";-?\\)+": 'imgWink.png',
92   - ":-?D+\\b": 'imgSoMuchWin.png',
93   - "[xX][-']?D+\\b": 'imgGrin.png',
94   - ":-/+": 'imgErr.png',
95   - ":-?\\?+": 'imgHmmm.png',
96   - "\\^_\\^": 'imgCyoot.png',
97   - "\\b[oO]_[oO]\\b": 'imgStareDad.png',
98   - "\\b[fF]+[uU][uU]+\\b": 'imgFuu.png',
99   - "\\bY U NO\\b": 'imgYUNo.png',
100   - "\\bfap fap( fap)+\\b": 'imgFap.png',
101   - "-troll-": 'imgTroll.png'});
102   - var source = "FFUUUU :-((( :))) ;'(( ;-)) xDDD ffuuu";
103   - var expected = "<img alt=\"FFUUUU\" src=\"imgFuu.png\" " +
104   - "title=\"FFUUUU\" /> <img alt=\":-(((\" src=\"imgOhNo.png\" " +
105   - "title=\":-(((\" /> <img alt=\":)))\" src=\"imgHappy.png\" " +
106   - "title=\":)))\" /> <img alt=\";'((\" src=\"imgCry.png\" " +
107   - "title=\";'((\" /> <img alt=\";-))\" src=\"imgWink.png\" " +
108   - "title=\";-))\" /> <img alt=\"xDDD\" src=\"imgGrin.png\" " +
109   - "title=\"xDDD\" /> <img alt=\"ffuuu\" src=\"imgFuu.png\" " +
110   - "title=\"ffuuu\" />";
111   - var tmpDiv = document.createElement('div');
112   - tmpDiv.innerHTML = source;
113   - winkParser.parseSmileys(tmpDiv);
114   - var actualOnePass = tmpDiv.innerHTML;
115   - expect(source).toParseTo(expected, winkParser);
116   - expect(actualOnePass).toParseTo(expected, winkParser);
117   - });
  65 + it("should be idempotent", function() {
  66 + var source = "I LOLed xD :-) xDD";
  67 + var expected = "I LOLed " + smileyMarkup("xD", "lol") + " " +
  68 + smileyMarkup(":-)", "smile") + " " +
  69 + smileyMarkup("xDD", "lol");
  70 + var tmpDiv = document.createElement('div');
  71 + tmpDiv.innerHTML = source;
  72 + parser.parseSmileys(tmpDiv);
  73 + var actualOnePass = tmpDiv.innerHTML;
  74 + expect(source).toParseTo(expected);
  75 + expect(actualOnePass).toParseTo(expected);
  76 + });
118 77
119   - it("should play well with word repetitions", function() {
120   - var winkParser = new SmileyParser({"\\bfap fap( fap)+\\b":
121   - 'imgFap.png'});
122   - var source = "fap fap fap mumble mumble fap fap fap fap";
123   - var expected = "<img alt=\"fap fap fap\" src=\"imgFap.png\" " +
124   - "title=\"fap fap fap\" /> mumble mumble " +
125   - "<img alt=\"fap fap fap fap\" src=\"imgFap.png\" " +
126   - "title=\"fap fap fap fap\" />";
127   - var tmpDiv = document.createElement('div');
128   - tmpDiv.innerHTML = source;
129   - winkParser.parseSmileys(tmpDiv);
130   - var actualOnePass = tmpDiv.innerHTML;
131   - expect(source).toParseTo(expected, winkParser);
132   - expect(actualOnePass).toParseTo(expected, winkParser);
133   - });
  78 + it("should play well with ;))", function() {
  79 + var winkParser = new SmileyParser({"\\bxD+\\b": {imageDataBase64: "lol"},
  80 + ";-?\\)+": {imageDataBase64: "wink"}});
  81 + var source = "Jajaja si, puede ser una solución, pero admito mas ideas... ;))";
  82 + var expected =
  83 + "Jajaja si, puede ser una solución, pero admito mas ideas... " +
  84 + smileyMarkup(";))", "wink");
  85 + var tmpDiv = document.createElement('div');
  86 + tmpDiv.innerHTML = source;
  87 + winkParser.parseSmileys(tmpDiv);
  88 + var actualOnePass = tmpDiv.innerHTML;
  89 + expect(source).toParseTo(expected, winkParser);
  90 + expect(actualOnePass).toParseTo(expected, winkParser);
  91 + });
134 92
135   - it("should be a bit more picky when detecting smilies", function() {
136   - var myParser = new SmileyParser({"[:;]-?P+\\b": 'wink.png',
137   - ":-?C": 'frown.png',
138   - "\\blol\\b|\\bLOL\\b": 'lol.png'});
139   - var source1 = "Newsflash:Catastrophic day for S&amp;P's #lol";
140   - expect(source1).toParseTo(source1, myParser);
141   - var source2 = "Smilies: :C ;P lol";
142   - var expected2 = "Smilies: <img alt=\":C\" src=\"frown.png\" " +
143   - "title=\":C\" /> <img alt=\";P\" src=\"wink.png\" " +
144   - "title=\";P\" /> <img alt=\"lol\" src=\"lol.png\" " +
145   - "title=\"lol\" />";
146   - expect(source2).toParseTo(expected2, myParser);
147   - });
  93 + it("should play well with character repetitions", function() {
  94 + var winkParser = new SmileyParser({
  95 + ":-?\\)+": {imageDataBase64: 'imgHappy'},
  96 + ":-?\\(\\(+": {imageDataBase64: 'imgOhNo'},
  97 + "[:;]-?P+\\b": {imageDataBase64: 'imgLick'},
  98 + "[:;]'\\(+": {imageDataBase64: 'imgCry'},
  99 + ";-?\\)+": {imageDataBase64: 'imgWink'},
  100 + ":-?D+\\b": {imageDataBase64: 'imgSoMuchWin'},
  101 + "[xX][-']?D+\\b": {imageDataBase64: 'imgGrin'},
  102 + ":-/+": {imageDataBase64: 'imgErr'},
  103 + ":-?\\?+": {imageDataBase64: 'imgHmmm'},
  104 + "\\^_\\^": {imageDataBase64: 'imgCyoot'},
  105 + "\\b[oO]_[oO]\\b": {imageDataBase64: 'imgStareDad'},
  106 + "\\b[fF]+[uU][uU]+\\b": {imageDataBase64: 'imgFuu'},
  107 + "\\bY U NO\\b": {imageDataBase64: 'imgYUNo'},
  108 + "\\bfap fap( fap)+\\b": {imageDataBase64: 'imgFap'},
  109 + "-troll-": {imageDataBase64: 'imgTroll'}});
  110 + var source = "FFUUUU :-((( :))) ;'(( ;-)) xDDD ffuuu";
  111 + var expected = smileyMarkup("FFUUUU", "imgFuu") + " " +
  112 + smileyMarkup(":-(((", "imgOhNo") + " " +
  113 + smileyMarkup(":)))", "imgHappy") + " " +
  114 + smileyMarkup(";'((", "imgCry") + " " +
  115 + smileyMarkup(";-))", "imgWink") + " " +
  116 + smileyMarkup("xDDD", "imgGrin") + " " +
  117 + smileyMarkup("ffuuu", "imgFuu");
  118 + var tmpDiv = document.createElement('div');
  119 + tmpDiv.innerHTML = source;
  120 + winkParser.parseSmileys(tmpDiv);
  121 + var actualOnePass = tmpDiv.innerHTML;
  122 + expect(source).toParseTo(expected, winkParser);
  123 + expect(actualOnePass).toParseTo(expected, winkParser);
  124 + });
148 125
149   - it("should detect smilies at the start of the string", function() {
150   - var myParser = new SmileyParser({"[:;]-?P+\\b": 'wink.png',
151   - ":-?C": 'frown.png',
152   - "\\blol\\b|\\bLOL\\b": 'lol.png'});
153   - var source2 = ":C";
154   - var source3 = ";P";
155   - var source4 = "lol";
156   - var expected2 = "<img alt=\":C\" src=\"frown.png\" title=\":C\" />";
157   - var expected3 = "<img alt=\";P\" src=\"wink.png\" title=\";P\" />";
158   - var expected4 = "<img alt=\"lol\" src=\"lol.png\" title=\"lol\" />";
159   - expect(source2).toParseTo(expected2, myParser);
160   - expect(source3).toParseTo(expected3, myParser);
161   - expect(source4).toParseTo(expected4, myParser);
162   - });
  126 + it("should play well with word repetitions", function() {
  127 + var winkParser = new SmileyParser({"\\bfap fap( fap)+\\b":
  128 + {imageDataBase64: 'imgFap'}});
  129 + var source = "fap fap fap mumble mumble fap fap fap fap";
  130 + var expected = smileyMarkup("fap fap fap", "imgFap") +
  131 + " mumble mumble " +
  132 + smileyMarkup("fap fap fap fap", "imgFap");
  133 + var tmpDiv = document.createElement('div');
  134 + tmpDiv.innerHTML = source;
  135 + winkParser.parseSmileys(tmpDiv);
  136 + var actualOnePass = tmpDiv.innerHTML;
  137 + expect(source).toParseTo(expected, winkParser);
  138 + expect(actualOnePass).toParseTo(expected, winkParser);
  139 + });
163 140
164   - it("should detect smilies with angle brackets", function() {
165   - var myParser = new SmileyParser({"</troll(ing)?>": 'troll.png'});
166   - var source = "&lt;/troll&gt; &lt;/trolling&gt;";
167   - var expected = "<img alt=\"&lt;/troll&gt;\" src=\"troll.png\" "
168   - + "title=\"&lt;/troll&gt;\" /> "
169   - + "<img alt=\"&lt;/trolling&gt;\" src=\"troll.png\" "
170   - + "title=\"&lt;/trolling&gt;\" />";
171   - expect(source).toParseTo(expected, myParser);
172   - });
  141 + it("should be a bit more picky when detecting smilies", function() {
  142 + var myParser = new SmileyParser(
  143 + {"[:;]-?P+\\b": {imageDataBase64: 'wink'},
  144 + ":-?C": {imageDataBase64: 'frown'},
  145 + "\\blol\\b|\\bLOL\\b": {imageDataBase64: 'lol'}});
  146 + var source1 = "Newsflash:Catastrophic day for S&amp;P's #lol";
  147 + expect(source1).toParseTo(source1, myParser);
  148 + var source2 = "Smilies: :C ;P lol";
  149 + var expected2 = "Smilies: " + smileyMarkup(":C", "frown") + " " +
  150 + smileyMarkup(";P", "wink") + " " +
  151 + smileyMarkup("lol", "lol");
  152 + expect(source2).toParseTo(expected2, myParser);
  153 + });
  154 +
  155 + it("should detect smilies at the start of the string", function() {
  156 + var myParser = new SmileyParser(
  157 + {"[:;]-?P+\\b": {imageDataBase64: 'wink'},
  158 + ":-?C": {imageDataBase64: 'frown'},
  159 + "\\blol\\b|\\bLOL\\b": {imageDataBase64: 'lol'}});
  160 + var source2 = ":C";
  161 + var source3 = ";P";
  162 + var source4 = "lol";
  163 + var expected2 = smileyMarkup(":C", "frown");
  164 + var expected3 = smileyMarkup(";P", "wink");
  165 + var expected4 = smileyMarkup("lol", "lol");
  166 + expect(source2).toParseTo(expected2, myParser);
  167 + expect(source3).toParseTo(expected3, myParser);
  168 + expect(source4).toParseTo(expected4, myParser);
  169 + });
  170 +
  171 + it("should detect smilies with angle brackets", function() {
  172 + var myParser = new SmileyParser(
  173 + {"</troll(ing)?>": {imageDataBase64: 'troll'}});
  174 + var source = "&lt;/troll&gt; &lt;/trolling&gt;";
  175 + var expected = smileyMarkup("&lt;/troll&gt;", "troll") + " " +
  176 + smileyMarkup("&lt;/trolling&gt;", "troll");
  177 + expect(source).toParseTo(expected, myParser);
  178 + });
173 179 });

0 comments on commit bcdba4a

Please sign in to comment.
Something went wrong with that request. Please try again.