Sha256: e2a7c70920a11d693c045d98bdb1fd1fe1327d9625c827aacb0ed7497a5315ff

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require "hot_module"

module HoTModuLe
  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

5 entries across 5 versions & 1 rubygems

Version Path
hot_module-1.0.0.alpha14 lib/hot_module/petite.rb
hot_module-1.0.0.alpha13 lib/hot_module/petite.rb
hot_module-1.0.0.alpha12 lib/hot_module/petite.rb
hot_module-1.0.0.alpha11 lib/hot_module/petite.rb
hot_module-1.0.0.alpha10 lib/hot_module/petite.rb