From ff00eb07fbd53b9b56b151c8f5800f1ca6abdbb0 Mon Sep 17 00:00:00 2001 From: Mantas Date: Tue, 2 Feb 2021 07:57:11 +0200 Subject: [PATCH] Fixes #3344 - Allow mailto links in knowledge base (KB) and email signatures --- .../richtext_additions/popup_link.coffee | 15 +++++-- .../knowledge_base/locale/answer/edit_spec.rb | 39 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_ui_element/richtext_additions/popup_link.coffee b/app/assets/javascripts/app/controllers/_ui_element/richtext_additions/popup_link.coffee index a73581623..aa3799855 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/richtext_additions/popup_link.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/richtext_additions/popup_link.coffee @@ -21,10 +21,10 @@ class App.UiElement.richtext.additions.RichTextToolPopupLink extends App.UiEleme ensureProtocol: (input) -> input = input.trim() - if !input.match(/^\S+\:\/\//) and input[0] isnt '/' - 'http://' + input - else + if @isWithProtocol(input) || @isRelativePath(input) || @isMailto(input) input + else + 'http://' + input apply: (callback) -> input = @el.find('input').val() @@ -57,3 +57,12 @@ class App.UiElement.richtext.additions.RichTextToolPopupLink extends App.UiEleme switch @selection.type when 'existing' $(@selection.dom).contents().unwrap() + + isWithProtocol: (input) -> + /^\S+\:\/\//.test(input) + + isMailto: (input) -> + /^mailto\:\S+@\S/.test(input) + + isRelativePath: (input) -> + input[0] is '/' diff --git a/spec/system/knowledge_base/locale/answer/edit_spec.rb b/spec/system/knowledge_base/locale/answer/edit_spec.rb index dc27a6e5c..1f42b8eab 100644 --- a/spec/system/knowledge_base/locale/answer/edit_spec.rb +++ b/spec/system/knowledge_base/locale/answer/edit_spec.rb @@ -20,6 +20,45 @@ RSpec.describe 'Knowledge Base Locale Answer Edit', type: :system, authenticated end end + context 'add weblink' do + def open_editor_and_add_link(input) + visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{draft_answer.id}/edit" + + sleep 3 # wait for popover killer to pass + + find('a[data-action="link"]').click + + within('.popover-content') do + find('input').fill_in with: input + find('[type=submit]').click + end + end + + it 'allows mailto links' do + open_editor_and_add_link 'mailto:test@example.com' + + expect(page).to have_selector('a[href="mailto:test@example.com"]') + end + + it 'allows link with a protocol' do + open_editor_and_add_link 'protocol://example.org' + + expect(page).to have_selector('a[href="protocol://example.org"]') + end + + it 'allows relative link' do + open_editor_and_add_link '/path' + + expect(page).to have_selector('a[href="/path"]') + end + + it 'allows non-protocol URL and prepends default protocol' do + open_editor_and_add_link 'example.com' + + expect(page).to have_selector('a[href="http://example.com"]') + end + end + context 'embedded video' do it 'has adding functionality' do