Skip to content

Commit 3c9b3d9

Browse files
fix: Ignore emoji shorthand codes in URIs (#1847)
* fix: Ignore emoji shorthand codes in URIs Fixes: #1823 * test: Add test for emoji in anchor body * fix: Handle support for URIs used in additional contexts Examples: - Without explicit scheme (i.e. starting with `//`) - In single and double quote strings - Within unquoted HTML tag attributes - In css `url()` values Co-authored-by: John Hildenbiddle <[email protected]>
1 parent 19e40c2 commit 3c9b3d9

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/core/render/emojify.js

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export function emojify(text, useNativeEmoji) {
3535
)
3636
// Mark colons in comments
3737
.replace(/<!--[\s\S]+?-->/g, m => m.replace(/:/g, '__colon__'))
38+
// Mark colons in URIs
39+
.replace(/([a-z]{2,}:)?\/\/[^\s'">)]+/gi, m =>
40+
m.replace(/:/g, '__colon__')
41+
)
3842
// Replace emoji shorthand codes
3943
.replace(/:([a-z0-9_\-+]+?):/g, (m, $1) =>
4044
replaceEmojiShorthand(m, $1, useNativeEmoji)

test/integration/__snapshots__/emoji.test.js.snap

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
exports[`Emoji Ignores all emoji shorthand codes (noEmoji:true) 1`] = `"<p>:smile:</p><p>:smile::smile:</p><p>:smile: :smile:</p><p>:smile::smile::smile:</p><p>:smile: :smile: :smile:</p><p>text:smile:</p><p>:smile:text</p><p>text:smile:text</p>"`;
44

5+
exports[`Emoji Ignores emoji shorthand codes in URIs 1`] = `"<p>Url <a href=\\"https://docsify.js.org/:foo:/\\" target=\\"_blank\\" rel=\\"noopener\\">https://docsify.js.org/:foo:/</a> <a href=\\"http://docsify.js.org/:100:/\\" target=\\"_blank\\" rel=\\"noopener\\">http://docsify.js.org/:100:/</a> <a href=\\"ftp://docsify.js.org/:smile:/\\" target=\\"_blank\\" rel=\\"noopener\\">ftp://docsify.js.org/:smile:/</a></p>"`;
6+
7+
exports[`Emoji Ignores emoji shorthand codes in URIs while handling anchor content 1`] = `"<p>Achor tags <a href=\\"http://docsify.js.org/:100:/\\" target=\\"_blank\\" rel=\\"noopener\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f4af.png?v8.png\\" alt=\\"100\\" class=\\"emoji\\" loading=\\"lazy\\"></a></p>"`;
8+
59
exports[`Emoji Ignores emoji shorthand codes in code, pre, script, and template tags 1`] = `
610
"<pre>:100:</pre>
711
@@ -16,6 +20,10 @@ exports[`Emoji Ignores emoji shorthand codes in code, pre, script, and template
1620
1721
exports[`Emoji Ignores emoji shorthand codes in comments 1`] = `"<p>Text <!-- :foo: :100: --></p>"`;
1822
23+
exports[`Emoji Ignores emoji shorthand codes in html attributes 1`] = `"<p><a href=\\"http://domain.com/:smile:/\\"> <img src=\\"http://domain.com/:smile:/file.png\\"> <script src=\\"http://domain.com/:smile:/file.js\\"></script></a></p>"`;
24+
25+
exports[`Emoji Ignores emoji shorthand codes in style url() values 1`] = `"<style>@import url(http://domain.com/:smile/file.css);</style>"`;
26+
1927
exports[`Emoji Ignores unmatched emoji shorthand codes 1`] = `"<p>hh:mm</p><p>hh:mm:ss</p><p>Namespace::SubNameSpace</p><p>Namespace::SubNameSpace::Class</p><p>2014-12-29T16:11:20+00:00</p>"`;
2028
2129
exports[`Emoji Renders GitHub emoji images (nativeEmoji:false) 1`] = `"<p><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"></p><p><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"></p><p><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"> <img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"></p><p><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"></p><p><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"> <img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"> <img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"></p><p>text<img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\"></p><p><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\">text</p><p>text<img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f604.png?v8.png\\" alt=\\"smile\\" class=\\"emoji\\" loading=\\"lazy\\">text</p>"`;

test/integration/emoji.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,59 @@ describe('Emoji', function () {
107107
expect(mainElm.innerHTML).toMatchSnapshot();
108108
});
109109

110+
test('Ignores emoji shorthand codes in URIs', async () => {
111+
await docsifyInit({
112+
markdown: {
113+
homepage:
114+
'Url https://docsify.js.org/:foo:/ http://docsify.js.org/:100:/ ftp://docsify.js.org/:smile:/',
115+
},
116+
// _logHTML: true,
117+
});
118+
119+
const mainElm = document.querySelector('#main');
120+
121+
expect(mainElm.innerHTML).toMatchSnapshot();
122+
});
123+
124+
test('Ignores emoji shorthand codes in URIs while handling anchor content', async () => {
125+
await docsifyInit({
126+
markdown: {
127+
homepage: 'Achor tags [:100:](http://docsify.js.org/:100:/)',
128+
},
129+
// _logHTML: true,
130+
});
131+
132+
const mainElm = document.querySelector('#main');
133+
134+
expect(mainElm.innerHTML).toMatchSnapshot();
135+
});
136+
137+
test('Ignores emoji shorthand codes in html attributes', async () => {
138+
await docsifyInit({
139+
markdown: {
140+
homepage: `<a href="http://domain.com/:smile:/"> <img src='http://domain.com/:smile:/file.png'> <script src=http://domain.com/:smile:/file.js></script>`,
141+
},
142+
// _logHTML: true,
143+
});
144+
145+
const mainElm = document.querySelector('#main');
146+
147+
expect(mainElm.innerHTML).toMatchSnapshot();
148+
});
149+
150+
test('Ignores emoji shorthand codes in style url() values', async () => {
151+
await docsifyInit({
152+
markdown: {
153+
homepage: `<style>@import url(http://domain.com/:smile/file.css);</style>`,
154+
},
155+
// _logHTML: true,
156+
});
157+
158+
const mainElm = document.querySelector('#main');
159+
160+
expect(mainElm.innerHTML).toMatchSnapshot();
161+
});
162+
110163
test('Ignores emoji shorthand codes in code, pre, script, and template tags', async () => {
111164
await docsifyInit({
112165
markdown: {

0 commit comments

Comments
 (0)