Class: Crm::ArticleRevisionDiffRowComponent::FragmentMarker

Inherits:
Object
  • Object
show all
Defined in:
app/components/crm/article_revision_diff_row_component/fragment_marker.rb

Overview

Applies one side's semantic ins/del nodes to an already-sanitized fragment.

Instance Method Summary collapse

Constructor Details

#initialize(fragment:, segments:, element:, css_class:) ⇒ FragmentMarker

Returns a new instance of FragmentMarker.

Parameters:

  • fragment (Nokogiri::HTML::DocumentFragment)

    sanitized fragment whose
    text nodes are replaced by marked-up nodes

  • segments (Array<Array(String, Boolean)>)

    ordered diff segments as
    [text, changed] pairs covering the fragment's text

  • element (Symbol, String)

    wrapper tag name (e.g. :ins, :del) for
    changed text

  • css_class (String)

    CSS class applied to each wrapper element



12
13
14
15
16
17
# File 'app/components/crm/article_revision_diff_row_component/fragment_marker.rb', line 12

def initialize(fragment:, segments:, element:, css_class:)
  @fragment = fragment
  @cursor = Crm::ArticleRevisionDiffRowComponent::SegmentCursor.new(segments)
  @element = element
  @css_class = css_class
end

Instance Method Details

#callNokogiri::HTML::DocumentFragment

Rebuilds every text node in the fragment, wrapping changed runs in the
configured element.

Returns:

  • (Nokogiri::HTML::DocumentFragment)

    the mutated fragment



22
23
24
25
26
27
28
29
30
# File 'app/components/crm/article_revision_diff_row_component/fragment_marker.rb', line 22

def call
  fragment.xpath(".//text()").each do |text_node|
    cursor.take(text_node.text).each do |text, changed|
      text_node.add_previous_sibling(build_node(text, changed))
    end
    text_node.remove
  end
  fragment
end