From 9920551536bb4f78dffeaaf3a194b92f54c34a47 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Thu, 6 Oct 2016 23:01:42 +0100 Subject: [PATCH] Enable CacheMarkdownField for the remaining models This commit alters views for the following models to use the markdown cache if present: * AbuseReport * Appearance * ApplicationSetting * BroadcastMessage * Group * Issue * Label * MergeRequest * Milestone * Project At the same time, calls to `escape_once` have been moved into the `single_line` Banzai pipeline, so they can't be missed out by accident and the work is done at save, rather than render, time. --- .../admin/broadcast_messages_controller.rb | 2 +- app/helpers/appearances_helper.rb | 2 +- app/helpers/application_settings_helper.rb | 12 ------------ app/helpers/broadcast_messages_helper.rb | 6 +++--- app/helpers/gitlab_markdown_helper.rb | 14 ++++++-------- .../admin/abuse_reports/_abuse_report.html.haml | 2 +- app/views/admin/broadcast_messages/_form.html.haml | 5 ++++- app/views/admin/broadcast_messages/preview.js.haml | 2 +- app/views/admin/groups/_group.html.haml | 2 +- app/views/admin/labels/_label.html.haml | 2 +- app/views/admin/projects/index.html.haml | 2 +- app/views/devise/confirmations/almost_there.haml | 4 ++-- app/views/groups/show.html.haml | 2 +- app/views/help/index.html.haml | 2 +- app/views/layouts/devise.html.haml | 4 ++-- app/views/projects/_home_panel.html.haml | 2 +- app/views/projects/commit/_commit_box.html.haml | 4 ++-- app/views/projects/commits/_commit.html.haml | 2 +- app/views/projects/issues/show.html.haml | 4 ++-- .../projects/merge_requests/show/_mr_box.html.haml | 4 ++-- app/views/projects/milestones/show.html.haml | 4 ++-- app/views/projects/pipelines/_info.html.haml | 4 ++-- app/views/projects/repositories/_feed.html.haml | 2 +- .../projects/runners/_shared_runners.html.haml | 4 ++-- app/views/projects/tags/_tag.html.haml | 2 +- app/views/projects/tags/show.html.haml | 2 +- app/views/shared/_label_row.html.haml | 2 +- app/views/shared/groups/_group.html.haml | 2 +- app/views/shared/milestones/_labels_tab.html.haml | 2 +- app/views/shared/milestones/_top.html.haml | 3 +-- app/views/shared/projects/_project.html.haml | 2 +- app/views/shared/snippets/_blob.html.haml | 7 +++++-- app/views/shared/snippets/_header.html.haml | 2 +- lib/banzai/filter/html_entity_filter.rb | 12 ++++++++++++ lib/banzai/pipeline/single_line_pipeline.rb | 1 + spec/helpers/broadcast_messages_helper_spec.rb | 4 ++-- spec/lib/banzai/filter/html_entity_filter_spec.rb | 14 ++++++++++++++ 37 files changed, 83 insertions(+), 65 deletions(-) create mode 100644 lib/banzai/filter/html_entity_filter.rb create mode 100644 spec/lib/banzai/filter/html_entity_filter_spec.rb diff --git a/app/controllers/admin/broadcast_messages_controller.rb b/app/controllers/admin/broadcast_messages_controller.rb index 82055006ac0..762e36ee2e9 100644 --- a/app/controllers/admin/broadcast_messages_controller.rb +++ b/app/controllers/admin/broadcast_messages_controller.rb @@ -37,7 +37,7 @@ class Admin::BroadcastMessagesController < Admin::ApplicationController end def preview - @message = broadcast_message_params[:message] + @broadcast_message = BroadcastMessage.new(broadcast_message_params) end protected diff --git a/app/helpers/appearances_helper.rb b/app/helpers/appearances_helper.rb index de13e7a1fc2..16136d02530 100644 --- a/app/helpers/appearances_helper.rb +++ b/app/helpers/appearances_helper.rb @@ -16,7 +16,7 @@ module AppearancesHelper end def brand_text - markdown(brand_item.description) + markdown_field(brand_item, :description) end def brand_item diff --git a/app/helpers/application_settings_helper.rb b/app/helpers/application_settings_helper.rb index 6de25bea654..6229384817b 100644 --- a/app/helpers/application_settings_helper.rb +++ b/app/helpers/application_settings_helper.rb @@ -11,18 +11,6 @@ module ApplicationSettingsHelper current_application_settings.signin_enabled? end - def extra_sign_in_text - current_application_settings.sign_in_text - end - - def after_sign_up_text - current_application_settings.after_sign_up_text - end - - def shared_runners_text - current_application_settings.shared_runners_text - end - def user_oauth_applications? current_application_settings.user_oauth_applications end diff --git a/app/helpers/broadcast_messages_helper.rb b/app/helpers/broadcast_messages_helper.rb index 43a29c96bca..eb03ced67eb 100644 --- a/app/helpers/broadcast_messages_helper.rb +++ b/app/helpers/broadcast_messages_helper.rb @@ -3,7 +3,7 @@ module BroadcastMessagesHelper return unless message.present? content_tag :div, class: 'broadcast-message', style: broadcast_message_style(message) do - icon('bullhorn') << ' ' << render_broadcast_message(message.message) + icon('bullhorn') << ' ' << render_broadcast_message(message) end end @@ -32,7 +32,7 @@ module BroadcastMessagesHelper end end - def render_broadcast_message(message) - Banzai.render(message, pipeline: :broadcast_message).html_safe + def render_broadcast_message(broadcast_message) + Banzai.render_field(broadcast_message, :message).html_safe end end diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb index d24680b8617..0772d848289 100644 --- a/app/helpers/gitlab_markdown_helper.rb +++ b/app/helpers/gitlab_markdown_helper.rb @@ -13,14 +13,12 @@ module GitlabMarkdownHelper def link_to_gfm(body, url, html_options = {}) return "" if body.blank? - escaped_body = if body.start_with?(' + class HTMLEntityFilter < HTML::Pipeline::TextFilter + def call + ERB::Util.html_escape(text) + end + end + end +end diff --git a/lib/banzai/pipeline/single_line_pipeline.rb b/lib/banzai/pipeline/single_line_pipeline.rb index ba2555df98d..30bc035d085 100644 --- a/lib/banzai/pipeline/single_line_pipeline.rb +++ b/lib/banzai/pipeline/single_line_pipeline.rb @@ -3,6 +3,7 @@ module Banzai class SingleLinePipeline < GfmPipeline def self.filters @filters ||= FilterArray[ + Filter::HTMLEntityFilter, Filter::SanitizationFilter, Filter::EmojiFilter, diff --git a/spec/helpers/broadcast_messages_helper_spec.rb b/spec/helpers/broadcast_messages_helper_spec.rb index 157cc4665a2..c6e3c5c2368 100644 --- a/spec/helpers/broadcast_messages_helper_spec.rb +++ b/spec/helpers/broadcast_messages_helper_spec.rb @@ -7,7 +7,7 @@ describe BroadcastMessagesHelper do end it 'includes the current message' do - current = double(message: 'Current Message') + current = BroadcastMessage.new(message: 'Current Message') allow(helper).to receive(:broadcast_message_style).and_return(nil) @@ -15,7 +15,7 @@ describe BroadcastMessagesHelper do end it 'includes custom style' do - current = double(message: 'Current Message') + current = BroadcastMessage.new(message: 'Current Message') allow(helper).to receive(:broadcast_message_style).and_return('foo') diff --git a/spec/lib/banzai/filter/html_entity_filter_spec.rb b/spec/lib/banzai/filter/html_entity_filter_spec.rb new file mode 100644 index 00000000000..6dc4a970071 --- /dev/null +++ b/spec/lib/banzai/filter/html_entity_filter_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe Banzai::Filter::HTMLEntityFilter, lib: true do + include FilterSpecHelper + + let(:unescaped) { 'foo &&&' } + let(:escaped) { 'foo <strike attr="foo">&&&</strike>' } + + it 'converts common entities to their HTML-escaped equivalents' do + output = filter(unescaped) + + expect(output).to eq(escaped) + end +end