class ComponentEmbeddedRuby::Parser::AttributeParser

Internal: Parses an HTML tag attributes into a hash of key values

This class parses HTML attributes into a hash of key values, keys are always strings but since values can be dynamic, they will either be a string or an instance of `Eval`.

Given how we parse these attributes, they are intentionally either a string or Ruby, not a combination of the two.

Valid attributes may look like `id=“document” class={my_classes}`

The following is invalid `class=“mb-0 {my_classes}”`

Attributes

token_reader[R]

Public Instance Methods

call() click to toggle source
# File lib/component_embedded_ruby/parser/attribute_parser.rb, line 19
def call
  attributes = {}

  attributes.merge!(parse_attribute) while current_token.type == :identifier

  attributes
end

Private Instance Methods

parse_attribute() click to toggle source
# File lib/component_embedded_ruby/parser/attribute_parser.rb, line 31
def parse_attribute
  key = expect(:identifier).value
  expect(:equals)
  value = parse_value

  { key => value }
end
parse_value() click to toggle source
# File lib/component_embedded_ruby/parser/attribute_parser.rb, line 39
def parse_value
  value_token = expect_any(:string, :ruby, expected_message: "a string or ruby code")

  if value_token.type == :string
    value_token.value
  else
    Eval.new(value_token.value)
  end
end