Sha256: 60303d511c5aeaf26412cea3f7b3fe711485f30d93c56c8faf2f348817ea753f

Contents?: true

Size: 1.51 KB

Versions: 38

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Decidim
  # Extending Diffy gem to accomodate the needs of app/cells/decidim/diff_cell.rb
  module DiffyExtension
    # HtmlFormatter that returns basic html output (no inline highlighting)
    # and does not escape HTML tags.
    class UnescapedHtmlFormatter < Diffy::HtmlFormatter
      # We exclude the tags `del` and `ins` so the diffy styling does not apply.
      TAGS = (UserInputScrubber.new.tags.to_a - %w(del ins)).freeze

      def to_s
        str = wrap_lines(@diff.map { |line| wrap_line(line) })
        ActionView::Base.new(ActionView::LookupContext.new(nil), {}, nil).sanitize(str, tags: TAGS)
      end
    end

    # Adding a new method to Diffy::Format so we can pass the
    # `:unescaped_html` option when calling Diffy::Diff#to_s.
    Diffy::Format.module_eval do
      def unescaped_html
        UnescapedHtmlFormatter.new(self, options).to_s
      end
    end

    # The private "split" method SplitDiff needs to be overriden to take into
    # account the new :unescaped_html format, and the fact that the tags
    # <ins> <del> are not there anymore
    Diffy::SplitDiff.module_eval do
      private

      def split
        return [split_left, split_right] unless @format == :unescaped_html

        [unescaped_split_left, unescaped_split_right]
      end

      def unescaped_split_left
        @diff.gsub(%r{<li class="ins">([\s\S]*?)</li>}, "")
      end

      def unescaped_split_right
        @diff.gsub(%r{<li class="del">([\s\S]*?)</li>}, "")
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
decidim-core-0.28.4 lib/decidim/diffy_extension.rb
decidim-core-0.27.9 lib/decidim/diffy_extension.rb
decidim-core-0.28.3 lib/decidim/diffy_extension.rb
decidim-core-0.27.8 lib/decidim/diffy_extension.rb
decidim-core-0.28.2 lib/decidim/diffy_extension.rb
decidim-core-0.27.7 lib/decidim/diffy_extension.rb
decidim-core-0.28.1 lib/decidim/diffy_extension.rb
decidim-core-0.27.6 lib/decidim/diffy_extension.rb
decidim-core-0.26.10 lib/decidim/diffy_extension.rb
decidim-core-0.26.9 lib/decidim/diffy_extension.rb
decidim-core-0.28.0 lib/decidim/diffy_extension.rb
decidim-core-0.27.5 lib/decidim/diffy_extension.rb
decidim-core-0.28.0.rc5 lib/decidim/diffy_extension.rb
decidim-core-0.28.0.rc4 lib/decidim/diffy_extension.rb
decidim-core-0.26.8 lib/decidim/diffy_extension.rb
decidim-core-0.27.4 lib/decidim/diffy_extension.rb
decidim-core-0.27.3 lib/decidim/diffy_extension.rb
decidim-core-0.26.7 lib/decidim/diffy_extension.rb
decidim-core-0.26.5 lib/decidim/diffy_extension.rb
decidim-core-0.27.2 lib/decidim/diffy_extension.rb