mirror of
https://gitee.com/docsifyjs/docsify.git
synced 2024-11-29 18:48:14 +08:00
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 <jhildenbiddle@users.noreply.github.com>
This commit is contained in:
parent
19e40c2075
commit
3c9b3d9702
@ -35,6 +35,10 @@ export function emojify(text, useNativeEmoji) {
|
||||
)
|
||||
// Mark colons in comments
|
||||
.replace(/<!--[\s\S]+?-->/g, m => m.replace(/:/g, '__colon__'))
|
||||
// Mark colons in URIs
|
||||
.replace(/([a-z]{2,}:)?\/\/[^\s'">)]+/gi, m =>
|
||||
m.replace(/:/g, '__colon__')
|
||||
)
|
||||
// Replace emoji shorthand codes
|
||||
.replace(/:([a-z0-9_\-+]+?):/g, (m, $1) =>
|
||||
replaceEmojiShorthand(m, $1, useNativeEmoji)
|
||||
|
@ -2,6 +2,10 @@
|
||||
|
||||
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>"`;
|
||||
|
||||
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>"`;
|
||||
|
||||
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>"`;
|
||||
|
||||
exports[`Emoji Ignores emoji shorthand codes in code, pre, script, and template tags 1`] = `
|
||||
"<pre>:100:</pre>
|
||||
|
||||
@ -16,6 +20,10 @@ exports[`Emoji Ignores emoji shorthand codes in code, pre, script, and template
|
||||
|
||||
exports[`Emoji Ignores emoji shorthand codes in comments 1`] = `"<p>Text <!-- :foo: :100: --></p>"`;
|
||||
|
||||
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>"`;
|
||||
|
||||
exports[`Emoji Ignores emoji shorthand codes in style url() values 1`] = `"<style>@import url(http://domain.com/:smile/file.css);</style>"`;
|
||||
|
||||
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>"`;
|
||||
|
||||
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>"`;
|
||||
|
@ -107,6 +107,59 @@ describe('Emoji', function () {
|
||||
expect(mainElm.innerHTML).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('Ignores emoji shorthand codes in URIs', async () => {
|
||||
await docsifyInit({
|
||||
markdown: {
|
||||
homepage:
|
||||
'Url https://docsify.js.org/:foo:/ http://docsify.js.org/:100:/ ftp://docsify.js.org/:smile:/',
|
||||
},
|
||||
// _logHTML: true,
|
||||
});
|
||||
|
||||
const mainElm = document.querySelector('#main');
|
||||
|
||||
expect(mainElm.innerHTML).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('Ignores emoji shorthand codes in URIs while handling anchor content', async () => {
|
||||
await docsifyInit({
|
||||
markdown: {
|
||||
homepage: 'Achor tags [:100:](http://docsify.js.org/:100:/)',
|
||||
},
|
||||
// _logHTML: true,
|
||||
});
|
||||
|
||||
const mainElm = document.querySelector('#main');
|
||||
|
||||
expect(mainElm.innerHTML).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('Ignores emoji shorthand codes in html attributes', async () => {
|
||||
await docsifyInit({
|
||||
markdown: {
|
||||
homepage: `<a href="http://domain.com/:smile:/"> <img src='http://domain.com/:smile:/file.png'> <script src=http://domain.com/:smile:/file.js></script>`,
|
||||
},
|
||||
// _logHTML: true,
|
||||
});
|
||||
|
||||
const mainElm = document.querySelector('#main');
|
||||
|
||||
expect(mainElm.innerHTML).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('Ignores emoji shorthand codes in style url() values', async () => {
|
||||
await docsifyInit({
|
||||
markdown: {
|
||||
homepage: `<style>@import url(http://domain.com/:smile/file.css);</style>`,
|
||||
},
|
||||
// _logHTML: true,
|
||||
});
|
||||
|
||||
const mainElm = document.querySelector('#main');
|
||||
|
||||
expect(mainElm.innerHTML).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('Ignores emoji shorthand codes in code, pre, script, and template tags', async () => {
|
||||
await docsifyInit({
|
||||
markdown: {
|
||||
|
Loading…
Reference in New Issue
Block a user