Skip to content

Commit

Permalink
Merge ea32abd into 813fcdd
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Sep 1, 2018
2 parents 813fcdd + ea32abd commit b2862ec
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
Expand Up @@ -11,7 +11,7 @@ exports[`emoticons 1`] = `
<p><img src=\\"/static/smileys/heureux.png\\" alt=\\":D\\" class=\\"foo bar\\"> This is not a caption</p>
<p>This is not a smiley:)</p>
<p>Not :)a smiley either.</p>
<p>Smiley after another node: <a href=\\"#foo\\">link</a><img src=\\"/static/smileys/smile.png\\" alt=\\":)\\" class=\\"foo bar\\"></p>
<p>Smiley after another node: <a href=\\"#foo\\">link</a> <img src=\\"/static/smileys/smile.png\\" alt=\\":)\\" class=\\"foo bar\\"></p>
<p>Smiley after another node w/2 spaces: <a href=\\"#foo\\">link</a> <img src=\\"/static/smileys/smile.png\\" alt=\\":)\\" class=\\"foo bar\\"></p>
<p>Smiley after another node w/3 spaces: <a href=\\"#foo\\">link</a> <img src=\\"/static/smileys/smile.png\\" alt=\\":)\\" class=\\"foo bar\\"></p>"
`;
Expand All @@ -27,7 +27,7 @@ exports[`emoticons without class 1`] = `
<p><img src=\\"/static/smileys/heureux.png\\" alt=\\":D\\"> This is not a caption</p>
<p>This is not a smiley:)</p>
<p>Not :)a smiley either.</p>
<p>Smiley after another node: <a href=\\"#foo\\">link</a><img src=\\"/static/smileys/smile.png\\" alt=\\":)\\"></p>
<p>Smiley after another node: <a href=\\"#foo\\">link</a> <img src=\\"/static/smileys/smile.png\\" alt=\\":)\\"></p>
<p>Smiley after another node w/2 spaces: <a href=\\"#foo\\">link</a> <img src=\\"/static/smileys/smile.png\\" alt=\\":)\\"></p>
<p>Smiley after another node w/3 spaces: <a href=\\"#foo\\">link</a> <img src=\\"/static/smileys/smile.png\\" alt=\\":)\\"></p>"
`;
Expand All @@ -49,7 +49,7 @@ This is not a smiley:)
Not :)a smiley either.
Smiley after another node: [link](#foo):)
Smiley after another node: [link](#foo) :)
Smiley after another node w/2 spaces: [link](#foo) :)
Expand Down
10 changes: 10 additions & 0 deletions packages/remark-emoticons/__tests__/index.js
Expand Up @@ -80,6 +80,16 @@ test('emoticons', () => {
expect(contents).toMatchSnapshot()
})

test('does not eat spaces leading to a smiley', () => {
const {contents} = render(dedent`
*hey* :)
[ho](#) :D let's go
`)
expect(contents).not.toContain('</em><img')
expect(contents).not.toContain('</a><img')
})

test('emoticons without class', () => {
const render = text => unified()
.use(reParse)
Expand Down
18 changes: 10 additions & 8 deletions packages/remark-emoticons/dist/index.js
Expand Up @@ -6,8 +6,6 @@ function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); // eslint-disable-line no-useless-escape
}

var SPACE = ' ';

module.exports = function inlinePlugin(ctx) {
var emoticonClasses = ctx && ctx.classes;
var emoticons = ctx && ctx.emoticons;
Expand Down Expand Up @@ -48,26 +46,30 @@ module.exports = function inlinePlugin(ctx) {

var pattern = Object.keys(emoticons).map(escapeRegExp).join('|');

var regex = new RegExp('(\\s|^)(' + pattern + ')(\\s|$)', 'i');
var regex = new RegExp('(?:\\s|^)(' + pattern + ')(?:\\s|$)', 'i');

function locator(value, fromIndex) {
var keep = regex.exec(value);
if (keep && value[keep.index] === SPACE) return keep.index + 1;
if (keep) {
var index = keep.index;
while (/^\s/.test(value.charAt(index))) {
index++;
}
return index;
}
return -1;
}

function inlineTokenizer(eat, value, silent) {
var keep = regex.exec(value);
if (keep) {
if (keep.index !== 0) return true;
if (!keep[0].startsWith(keep[1])) return true;

/* istanbul ignore if - never used (yet) */
if (silent) return true;

var toEat = keep[0];
if (toEat.charAt(toEat.length - 1) === SPACE) {
toEat = toEat.substring(0, toEat.length - 1);
}
var toEat = keep[1];
var emoticon = toEat.trim();
var src = emoticons[emoticon.toLowerCase()];

Expand Down
18 changes: 10 additions & 8 deletions packages/remark-emoticons/src/index.js
Expand Up @@ -2,8 +2,6 @@ function escapeRegExp (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&') // eslint-disable-line no-useless-escape
}

const SPACE = ' '

module.exports = function inlinePlugin (ctx) {
const emoticonClasses = ctx && ctx.classes
const emoticons = ctx && ctx.emoticons
Expand All @@ -18,26 +16,30 @@ module.exports = function inlinePlugin (ctx) {

const pattern = Object.keys(emoticons).map(escapeRegExp).join('|')

const regex = new RegExp(`(\\s|^)(${pattern})(\\s|$)`, 'i')
const regex = new RegExp(`(?:\\s|^)(${pattern})(?:\\s|$)`, 'i')

function locator (value, fromIndex) {
const keep = regex.exec(value)
if (keep && value[keep.index] === SPACE) return keep.index + 1
if (keep) {
let index = keep.index
while (/^\s/.test(value.charAt(index))) {
index++
}
return index
}
return -1
}

function inlineTokenizer (eat, value, silent) {
const keep = regex.exec(value)
if (keep) {
if (keep.index !== 0) return true
if (!keep[0].startsWith(keep[1])) return true

/* istanbul ignore if - never used (yet) */
if (silent) return true

let toEat = keep[0]
if (toEat.charAt(toEat.length - 1) === SPACE) {
toEat = toEat.substring(0, toEat.length - 1)
}
const toEat = keep[1]
const emoticon = toEat.trim()
const src = emoticons[emoticon.toLowerCase()]

Expand Down

0 comments on commit b2862ec

Please sign in to comment.