From 9dd879bc7099d988b8eb8b0e7ed0fde7d7b3c6c4 Mon Sep 17 00:00:00 2001 From: Harish Ramachandran Date: Wed, 2 Oct 2019 10:46:24 -0400 Subject: [PATCH 1/5] Wrap emojis in links when needed --- app/helpers/markup_helper.rb | 10 ++++++---- spec/helpers/markup_helper_spec.rb | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/helpers/markup_helper.rb b/app/helpers/markup_helper.rb index e2524938e10f16..b98f1079c53a8c 100644 --- a/app/helpers/markup_helper.rb +++ b/app/helpers/markup_helper.rb @@ -51,12 +51,14 @@ def link_to_html(redacted, url, html_options = {}) text = fragment.children[0].text fragment.children[0].replace(link_to(text, url, html_options)) else - # Traverse the fragment's first generation of children looking for pure - # text, wrapping anything found in the requested link + # Traverse the fragment's first generation of children looking for + # either pure text or emojis, wrapping anything found in the + # requested link fragment.children.each do |node| - next unless node.text? + next unless node.text? || node.name.match?('gl-emoji') - node.replace(link_to(node.text, url, html_options)) + node.replace(link_to(node.text, url, html_options)) if node.text? + node.replace(link_to(node.to_html.html_safe, url, html_options)) if node.name.match?('gl-emoji') end end diff --git a/spec/helpers/markup_helper_spec.rb b/spec/helpers/markup_helper_spec.rb index 32851249b2e25e..a8e43930fab628 100644 --- a/spec/helpers/markup_helper_spec.rb +++ b/spec/helpers/markup_helper_spec.rb @@ -210,7 +210,7 @@ it 'replaces commit message with emoji to link' do actual = link_to_markdown(':book: Book', '/foo') expect(actual) - .to eq '📖 Book' + .to eq '📖 Book' end end -- GitLab From 41a688527b0e512a0c1754c58f21cfbae7f7f353 Mon Sep 17 00:00:00 2001 From: Harish Ramachandran Date: Wed, 2 Oct 2019 19:20:10 -0400 Subject: [PATCH 2/5] Changelog for emoji links --- changelogs/unreleased/harishsr-emoticon-commit-links.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelogs/unreleased/harishsr-emoticon-commit-links.yml diff --git a/changelogs/unreleased/harishsr-emoticon-commit-links.yml b/changelogs/unreleased/harishsr-emoticon-commit-links.yml new file mode 100644 index 00000000000000..0ad9dd1e101e48 --- /dev/null +++ b/changelogs/unreleased/harishsr-emoticon-commit-links.yml @@ -0,0 +1,5 @@ +--- +title: Allow emojis to be linkable +merge_request: 18014 +author: +type: fixed -- GitLab From e3fa2b736171e1c9c6323c0f356cfed226ccfa31 Mon Sep 17 00:00:00 2001 From: Harish Ramachandran Date: Mon, 28 Oct 2019 17:15:17 -0400 Subject: [PATCH 3/5] Better identify emoji tags for HTML links --- app/helpers/markup_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/markup_helper.rb b/app/helpers/markup_helper.rb index b98f1079c53a8c..8a97945bbfb0a4 100644 --- a/app/helpers/markup_helper.rb +++ b/app/helpers/markup_helper.rb @@ -58,7 +58,7 @@ def link_to_html(redacted, url, html_options = {}) next unless node.text? || node.name.match?('gl-emoji') node.replace(link_to(node.text, url, html_options)) if node.text? - node.replace(link_to(node.to_html.html_safe, url, html_options)) if node.name.match?('gl-emoji') + node.replace(link_to(node.to_html.html_safe, url, html_options)) if node.name == 'gl-emoji' end end -- GitLab From 457edc7d93f693c1891d74dbaa20328db1803fef Mon Sep 17 00:00:00 2001 From: Harish Ramachandran Date: Mon, 28 Oct 2019 17:43:57 -0400 Subject: [PATCH 4/5] Replace one more match method with an equality --- app/helpers/markup_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/markup_helper.rb b/app/helpers/markup_helper.rb index 8a97945bbfb0a4..4330c64568b94a 100644 --- a/app/helpers/markup_helper.rb +++ b/app/helpers/markup_helper.rb @@ -55,7 +55,7 @@ def link_to_html(redacted, url, html_options = {}) # either pure text or emojis, wrapping anything found in the # requested link fragment.children.each do |node| - next unless node.text? || node.name.match?('gl-emoji') + next unless node.text? || node.name == 'gl-emoji' node.replace(link_to(node.text, url, html_options)) if node.text? node.replace(link_to(node.to_html.html_safe, url, html_options)) if node.name == 'gl-emoji' -- GitLab From e93258428506abf6fc8ed43314dfa94237752f47 Mon Sep 17 00:00:00 2001 From: Harish Ramachandran Date: Tue, 29 Oct 2019 17:44:54 -0400 Subject: [PATCH 5/5] Refactor changes and test for passed-in HTML --- app/helpers/markup_helper.rb | 9 +++++---- spec/helpers/markup_helper_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/helpers/markup_helper.rb b/app/helpers/markup_helper.rb index 4330c64568b94a..e1e756c2f4ce6c 100644 --- a/app/helpers/markup_helper.rb +++ b/app/helpers/markup_helper.rb @@ -55,10 +55,11 @@ def link_to_html(redacted, url, html_options = {}) # either pure text or emojis, wrapping anything found in the # requested link fragment.children.each do |node| - next unless node.text? || node.name == 'gl-emoji' - - node.replace(link_to(node.text, url, html_options)) if node.text? - node.replace(link_to(node.to_html.html_safe, url, html_options)) if node.name == 'gl-emoji' + if node.text? + node.replace(link_to(node.text, url, html_options)) + elsif node.name == 'gl-emoji' + node.replace(link_to(node.to_html.html_safe, url, html_options)) + end end end diff --git a/spec/helpers/markup_helper_spec.rb b/spec/helpers/markup_helper_spec.rb index a8e43930fab628..683c04428abdc0 100644 --- a/spec/helpers/markup_helper_spec.rb +++ b/spec/helpers/markup_helper_spec.rb @@ -232,6 +232,12 @@ expect(doc.css('a')[0].attr('href')).to eq link expect(doc.css('a')[0].text).to eq 'This should finally fix ' end + + it "escapes HTML passed as an emoji" do + rendered = '<div class="test">test</div>' + expect(helper.link_to_html(rendered, '/foo')) + .to eq '<div class="test">test</div>' + end end describe '#render_wiki_content' do -- GitLab