From 5ff3a9c4bbe23afe8d22baf7c41d0458798dd7af Mon Sep 17 00:00:00 2001 From: Ayke Halder Date: Wed, 9 Feb 2022 12:18:59 +0100 Subject: [PATCH] DEV: add native lazy loading for emojis (#15830) --- .../discourse/tests/acceptance/emoji-test.js | 4 +- .../acceptance/user-drafts-stream-test.js | 2 +- .../tests/unit/lib/pretty-text-test.js | 18 ++-- .../engines/discourse-markdown/emoji.js | 6 ++ .../engines/discourse-markdown/quotes.js | 1 + app/models/emoji.rb | 4 +- plugins/poll/spec/lib/pretty_text_spec.rb | 6 +- spec/components/cooked_post_processor_spec.rb | 14 +-- spec/components/pretty_text_spec.rb | 94 +++++++++---------- spec/fabricators/post_fabricator.rb | 2 +- .../helpers/user_notifications_helper_spec.rb | 2 +- spec/jobs/rebake_custom_emoji_posts_spec.rb | 2 +- spec/models/emoji_spec.rb | 4 +- spec/models/user_profile_spec.rb | 2 +- spec/services/search_indexer_spec.rb | 2 +- 15 files changed, 85 insertions(+), 78 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/acceptance/emoji-test.js b/app/assets/javascripts/discourse/tests/acceptance/emoji-test.js index fed440044be..a5209010155 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/emoji-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/emoji-test.js @@ -13,7 +13,7 @@ acceptance("Emoji", function (needs) { await fillIn(".d-editor-input", "this is an emoji :blonde_woman:"); assert.strictEqual( queryAll(".d-editor-preview:visible").html().trim(), - `

this is an emoji :blonde_woman:

` + `

this is an emoji :blonde_woman:

` ); }); @@ -24,7 +24,7 @@ acceptance("Emoji", function (needs) { await fillIn(".d-editor-input", "this is an emoji :blonde_woman:t5:"); assert.strictEqual( queryAll(".d-editor-preview:visible").html().trim(), - `

this is an emoji :blonde_woman:t5:

` + `

this is an emoji :blonde_woman:t5:

` ); }); }); diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-drafts-stream-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-drafts-stream-test.js index cbed14814f6..ace7abce1de 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/user-drafts-stream-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/user-drafts-stream-test.js @@ -55,7 +55,7 @@ acceptance("User Drafts", function (needs) { ); assert.strictEqual( query(".user-stream-item:nth-child(3) .excerpt").innerHTML.trim(), - `here goes a reply to a PM :slight_smile:` + `here goes a reply to a PM :slight_smile:` ); }); }); diff --git a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js index e3166ee7e29..827b01f5dea 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js @@ -1506,7 +1506,7 @@ var bar = 'bar'; assert.cookedOptions( ":grin: @sam", { featuresOverride: ["emoji"] }, - `

:grin: @sam

`, + `

:grin: @sam

`, "cooks emojis when only the emoji markdown engine is enabled" ); @@ -1521,15 +1521,15 @@ var bar = 'bar'; test("emoji", function (assert) { assert.cooked( ":smile:", - `

:smile:

` + `

:smile:

` ); assert.cooked( ":(", - `

:frowning:

` + `

:frowning:

` ); assert.cooked( "8-)", - `

:sunglasses:

` + `

:sunglasses:

` ); }); @@ -1543,7 +1543,7 @@ var bar = 'bar'; assert.cookedOptions( "test:smile:test", { siteSettings: { enable_inline_emoji_translation: true } }, - `

test:smile:test

` + `

test:smile:test

` ); }); @@ -1551,7 +1551,7 @@ var bar = 'bar'; assert.cookedOptions( ":smile:", { siteSettings: { emoji_set: "twitter" } }, - `

:smile:

` + `

:smile:

` ); }); @@ -1564,7 +1564,7 @@ var bar = 'bar'; external_emoji_url: "https://emoji.hosting.service", }, }, - `

:smile:

` + `

:smile:

` ); }); @@ -1574,7 +1574,7 @@ var bar = 'bar'; assert.cookedOptions( ":foo:", {}, - `

:foo:

` + `

:foo:

` ); registerEmoji("bar", "/images/avatar.png", "baz"); @@ -1582,7 +1582,7 @@ var bar = 'bar'; assert.cookedOptions( ":bar:", {}, - `

:bar:

` + `

:bar:

` ); }); diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js index 0806a97b33a..6a68b19d9f2 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js @@ -118,6 +118,9 @@ function getEmojiTokenByName(name, state) { ["title", info.title], ["class", info.classes], ["alt", info.title], + ["loading", "lazy"], + ["width", "20"], + ["height", "20"], ]; return token; @@ -345,5 +348,8 @@ export function setup(helper) { "img[class=emoji emoji-custom]", "img[class=emoji emoji-custom only-emoji]", "img[class=emoji only-emoji]", + "img[loading=lazy]", + "img[width=20]", + "img[height=20]", ]); } diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js index 8eaf6dd4060..3a12846ee2b 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js @@ -127,6 +127,7 @@ const rule = { emojiCDNUrl: options.emojiCDNUrl, enableEmojiShortcuts: options.enableEmojiShortcuts, inlineEmoji: options.inlineEmoji, + lazy: true, }); } diff --git a/app/models/emoji.rb b/app/models/emoji.rb index 8e7d8fd6bed..36bdea93be3 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -240,9 +240,9 @@ class Emoji if code && Emoji.custom?(code) emoji = Emoji[code] - "\"#{code}\"" + "\"#{code}\"" elsif code && Emoji.exists?(code) - "\"#{code}\"" + "\"#{code}\"" else name end diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rb index 5770f804f3f..0f6f870fe7c 100644 --- a/plugins/poll/spec/lib/pretty_text_spec.rb +++ b/plugins/poll/spec/lib/pretty_text_spec.rb @@ -100,7 +100,7 @@ describe PrettyText do
    -
  1. test 1 :slight_smile: test +
  2. test 1 :slight_smile: test
  3. test 2
@@ -165,7 +165,7 @@ describe PrettyText do MD expect(cooked).to include(<<~HTML) -
What’s your favorite berry? :wink: https://google.com/ +
What’s your favorite berry? :wink: https://google.com/
HTML end @@ -185,7 +185,7 @@ describe PrettyText do MD expect(cooked).to include(<<~HTML) -
What’s your favorite berry? :wink: https://google.com/ +
What’s your favorite berry? :wink: https://google.com/
HTML diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 77f9cb4b3a4..809b63555ac 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -1246,7 +1246,7 @@ describe CookedPostProcessor do Google

text.txt (20 Bytes)
- :smile:

+ :smile:

HTML end @@ -1261,7 +1261,7 @@ describe CookedPostProcessor do Google

text.txt (20 Bytes)
- :smile:

+ :smile:

HTML end @@ -1274,7 +1274,7 @@ describe CookedPostProcessor do Google

text.txt (20 Bytes)
- :smile:

+ :smile:

HTML end @@ -1288,7 +1288,7 @@ describe CookedPostProcessor do Google

text.txt (20 Bytes)
- :smile:

+ :smile:

HTML end @@ -1302,7 +1302,7 @@ describe CookedPostProcessor do Google

text.txt (20 Bytes)
- :smile:

+ :smile:

HTML end @@ -1336,7 +1336,7 @@ describe CookedPostProcessor do cpp.optimize_urls expect(cpp.html).to match_html <<~HTML -

This post has a local emoji :+1: and an external upload

+

This post has a local emoji :+1: and an external upload

smallest.png

HTML end @@ -1354,7 +1354,7 @@ describe CookedPostProcessor do cpp.optimize_urls expect(cpp.html).to match_html <<~HTML -

This post has a local emoji :+1: and an external upload

+

This post has a local emoji :+1: and an external upload

smallest.png

HTML end diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 6db1a86e350..76d522d60cd 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -37,7 +37,7 @@ describe PrettyText do