diff --git a/app/helpers/quill_js_helper.rb b/app/helpers/quill_js_helper.rb new file mode 100644 index 000000000..5097b36b4 --- /dev/null +++ b/app/helpers/quill_js_helper.rb @@ -0,0 +1,17 @@ +module QuillJsHelper + def sanitize_quill_js_input(input) + require "#{Rails.root}/app/utilities/scrubbers/quill_js_scrubber" + + # We need to disable formatting to prevent unwanted \n + # symbols from creeping into sanitized HTML (which + # cause unwanted new lines when rendered in Quill.js) + disable_formatting = + Nokogiri::XML::Node::SaveOptions::DEFAULT_HTML ^ + Nokogiri::XML::Node::SaveOptions::FORMAT + + Loofah + .fragment(input) + .scrub!(QuillJsScrubber.new) + .to_html(save_with: disable_formatting) + end +end diff --git a/app/utilities/protocols_importer.rb b/app/utilities/protocols_importer.rb index 7f1e9e59a..383883e17 100644 --- a/app/utilities/protocols_importer.rb +++ b/app/utilities/protocols_importer.rb @@ -1,6 +1,5 @@ module ProtocolsImporter - require 'scrubbers/quill_js_scrubber' - include RenamingUtil + include RenamingUtil, QuillJsHelper def import_new_protocol(protocol_json, organization, type, user) remove_empty_inputs(protocol_json) @@ -55,9 +54,8 @@ module ProtocolsImporter step = Step.create!( name: step_json["name"], description: # Sanitize description HTML - ActionController::Base.helpers.sanitize( - step_json['description'], - scrubber: QuillJsScrubber.new + sanitize_quill_js_input( + step_json['description'] ), position: step_pos, completed: false,