class Motion::MarkupTransformer

Attributes

key_attribute[R]
serializer[R]
state_attribute[R]

Public Class Methods

new( serializer: Motion.serializer, key_attribute: Motion.config.key_attribute, state_attribute: Motion.config.state_attribute ) click to toggle source
# File lib/motion/markup_transformer.rb, line 14
def initialize(
  serializer: Motion.serializer,
  key_attribute: Motion.config.key_attribute,
  state_attribute: Motion.config.state_attribute
)
  @serializer = serializer
  @key_attribute = key_attribute
  @state_attribute = state_attribute
end

Public Instance Methods

add_state_to_html(component, html) click to toggle source
# File lib/motion/markup_transformer.rb, line 24
def add_state_to_html(component, html)
  return if html.blank?

  key, state = serializer.serialize(component)

  transform_root(component, html) do |root|
    root[key_attribute] = key
    root[state_attribute] = state
  end
end

Private Instance Methods

transform_root(component, html) { |root| ... } click to toggle source
# File lib/motion/markup_transformer.rb, line 37
def transform_root(component, html)
  fragment = Nokogiri::HTML::DocumentFragment.parse(html)
  root, *unexpected_others = fragment.children

  if !root || unexpected_others.any?(&:present?)
    raise MultipleRootsError, component
  end

  yield root

  fragment.to_html.html_safe
end