Sha256: 85e1ac1b8974ce7dc167b99019eee9ebec407c68928ff5e34385876cb95edbbc

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require "heartml"

module Heartml
  module Petite
    # @param klass [Class]
    # @return [void]
    def self.included(klass)
      klass.attribute_binding "v-for", :_petite_for_binding, only: :template
      klass.attribute_binding "v-text", :_petite_text_binding
      klass.attribute_binding "v-html", :_petite_html_binding
      klass.attribute_binding "v-bind", :_petite_bound_attribute
      klass.attribute_binding %r{^:}, :_petite_bound_attribute
    end

    protected

    def _petite_for_binding(attribute:, node:)
      delimiter = node["v-for"].include?(" of ") ? " of " : " in "
      expression = node["v-for"].split(delimiter)

      process_list(
        attribute: attribute,
        node: node,
        item_node: node.children[0].first_element_child,
        for_in: expression
      )
    end

    def _petite_text_binding(attribute:, node:)
      node.content = evaluate_attribute_expression(attribute).to_s
    end

    def _petite_html_binding(attribute:, node:)
      node.inner_html = evaluate_attribute_expression(attribute).to_s
    end

    def _petite_bound_attribute(attribute:, node:) # rubocop:disable Metrics
      return if attribute.name == ":key"

      real_attribute = if attribute.name.start_with?(":")
                         attribute.name.delete_prefix(":")
                       elsif attribute.name.start_with?("v-bind:")
                         attribute.name.delete_prefix("v-bind:")
                       end

      obj = evaluate_attribute_expression(attribute)

      if real_attribute == "class"
        node[real_attribute] = class_list_for(obj)
      elsif real_attribute != "style" # style bindings aren't SSRed
        node[real_attribute] = obj if obj
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
heartml-1.0.0.beta7 lib/heartml/petite.rb
heartml-1.0.0.beta6 lib/heartml/petite.rb
heartml-1.0.0.beta5 lib/heartml/petite.rb
heartml-1.0.0.beta4 lib/heartml/petite.rb
heartml-1.0.0.beta3 lib/heartml/petite.rb
heartml-1.0.0.beta2 lib/heartml/petite.rb
heartml-1.0.0.beta1 lib/heartml/petite.rb