Sha256: e1feb8e0a46ac38f6481b610be730cfea3b013747ffc75222cbda0320755d7f3

Contents?: true

Size: 1.74 KB

Versions: 1

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].children.find(&:element?),
        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

1 entries across 1 versions & 1 rubygems

Version Path
hot_module-1.0.0.alpha2 lib/hot_module/petite.rb