Sha256: bc3a1c0cca06408f35cfb74c48591f7a6fccd4c9903a3685d5e6198400471640

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module HoTModuLe
  class Fragment
    def initialize(fragment, attribute_bindings, html_module:)
      @fragment = fragment
      @attribute_bindings = attribute_bindings
      @html_module = html_module
    end

    # NOTE: for some reason, the Nokogiri traverse method yields node children first, then the
    # parent node. That doesn't work for our case. We want to go strictly in source order. So this
    # is our own implementation of that.
    def traverse(node, &block)
      yield(node)
      node.children.each { |child| traverse(child, &block) }
    end

    def process(fragment = @fragment)
      traverse(fragment) do |node|
        process_attribute_bindings(node)
      end
    end

    def process_attribute_bindings(node) # rubocop:todo Metrics
      node.attributes.each do |name, attr_node|
        @attribute_bindings.each do |attribute_binding|
          next if attribute_binding.only_for_tag && node.name != attribute_binding.only_for_tag.to_s
          next unless attribute_binding.matcher.match?(name)
          next if attribute_binding.method.receiver._check_stack(node)

          break unless attribute_binding.method.(attribute: attr_node, node: node)
        end
      rescue Exception => e # rubocop:disable Lint/RescueException
        raise e.class, e.message.lines.first, ["#{@html_module}:#{attr_node}", *e.backtrace]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hot_module-1.0.0.alpha9 lib/hot_module/fragment.rb
hot_module-1.0.0.alpha8 lib/hot_module/fragment.rb
hot_module-1.0.0.alpha7 lib/hot_module/fragment.rb
hot_module-1.0.0.alpha6 lib/hot_module/fragment.rb
hot_module-1.0.0.alpha5 lib/hot_module/fragment.rb
hot_module-1.0.0.alpha4 lib/hot_module/fragment.rb
hot_module-1.0.0.alpha3 lib/hot_module/fragment.rb
hot_module-1.0.0.alpha2 lib/hot_module/fragment.rb