Class: ORB::AST::TagNode

Inherits:
AbstractNode show all
Defined in:
lib/orb/ast/tag_node.rb

Overview

Represents a tag node in the AST. A tag node is used to represent an HTML tag or a component tag. The tag node contains information about the tag name, attributes, and directives.

It is created by the parser from a Token object produced by the tokenizer.

Constant Summary collapse

SLOT_SEPARATOR =
':'

Instance Attribute Summary collapse

Attributes inherited from AbstractNode

#children, #errors

Instance Method Summary collapse

Methods inherited from AbstractNode

#==, #add_child

Constructor Details

#initialize(token) ⇒ TagNode

Create a new TagNode from the given token

Parameters:

  • token (Token)

    the token to create the node from



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/orb/ast/tag_node.rb', line 22

def initialize(token)
  super
  @tag = token.value
  @meta = token.meta

  # Parse attributes from the metadata
  @raw_attributes = @meta.fetch(:attributes, []).map do |attr|
    name, type, value = attr
    Attribute.new(name, type, value)
  end
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



12
13
14
# File 'lib/orb/ast/tag_node.rb', line 12

def meta
  @meta
end

#tagObject (readonly)

Returns the value of attribute tag.



12
13
14
# File 'lib/orb/ast/tag_node.rb', line 12

def tag
  @tag
end

Instance Method Details

#attributesArray<Attribute>

Retrieve the attributes for the tag. Parses the internal representation of attributes in the metadata payload and constructs an array of Attribute objects.

Use this method rather than attempting to parse the meta object directly.

Returns:

  • (Array<Attribute>)

    the attributes for the tag



64
65
66
67
# File 'lib/orb/ast/tag_node.rb', line 64

def attributes
  @attributes ||= @raw_attributes.reject(&:directive?)
  @attributes
end

#clear_directivesObject

Clear all directives from the tag



91
92
93
# File 'lib/orb/ast/tag_node.rb', line 91

def clear_directives
  @directives = {}
end

#compiler_directives?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/orb/ast/tag_node.rb', line 195

def compiler_directives?
  directives.any? { |k, _v| k == :if || k == :for }
end

#componentString

Retrieve the component name from the tag

Returns:

  • (String)

    the component name



147
148
149
# File 'lib/orb/ast/tag_node.rb', line 147

def component
  @tag.split(SLOT_SEPARATOR).first
end

#component_moduleString

Retrieve the module name from the component name The module name is the first part of the component name separated by a period.

For example: MyApp::UI::Button would return MyApp.UI

Returns:

  • (String)

    the module name



167
168
169
# File 'lib/orb/ast/tag_node.rb', line 167

def component_module
  component.rsplit('.').first
end

#component_slot_tag?Boolean

Determine whether the TagNode represents a component slot tag A component slot tag is of the form Component:slot and is used to render a slot within a component.

Returns:

  • (Boolean)

    true if the tag is a component slot tag, false otherwise



139
140
141
# File 'lib/orb/ast/tag_node.rb', line 139

def component_slot_tag?
  @tag.start_with?(/[A-Z]/) && @tag.include?(SLOT_SEPARATOR)
end

#component_tag?Boolean

Determine whether the TagNode represents a component tag A component tag is one that starts with an uppercase letter and may contain a slot call on the component.

Returns:

  • (Boolean)

    true if the tag is a component tag, false otherwise



129
130
131
# File 'lib/orb/ast/tag_node.rb', line 129

def component_tag?
  @tag.start_with?(/[A-Z]/) && @tag.exclude?(SLOT_SEPARATOR)
end

#directivesArray<Array<Attribute>>

Retrieve the directives for the tag

Returns:

  • (Array<Array<Attribute>>)

    the directives for the tag



73
74
75
76
77
78
79
# File 'lib/orb/ast/tag_node.rb', line 73

def directives
  @directives ||= @raw_attributes
    .select(&:directive?)
    .to_h { |attr| [attr.name[1..], attr.value] }
    .transform_keys(&:to_sym)
  @directives
end

#directives?Boolean

Check whether the node has any directives

Returns:

  • (Boolean)

    true if the directives are present, false otherwise



191
192
193
# File 'lib/orb/ast/tag_node.rb', line 191

def directives?
  directives.any?
end

#dynamic?Boolean

Determine whether the tag needs to be compiled as dynamic HTML

Returns:

  • (Boolean)

    true if the tag needs to be compiled as dynamic HTML, false otherwise



183
184
185
# File 'lib/orb/ast/tag_node.rb', line 183

def dynamic?
  splat_attributes.any?
end

#dynamic_attributesArray<Attribute>

Retrieve all the dynamic attributes for the tag. A dynamic attribute is one that has an expression value.

Returns:

  • (Array<Attribute>)

    the dynamic attributes for the tag



109
110
111
# File 'lib/orb/ast/tag_node.rb', line 109

def dynamic_attributes
  attributes.select(&:dynamic?)
end

#html_tag?Boolean

Determine whether the TagNode represents an HTML tag

Returns:

  • (Boolean)

    true if the tag is an HTML tag, false otherwise



45
46
47
# File 'lib/orb/ast/tag_node.rb', line 45

def html_tag?
  @tag.start_with?(/[a-z]/)
end

#remove_directive(name) ⇒ Object

Remove a directive from the tag, i.e. when it is consumed by the compiler



84
85
86
# File 'lib/orb/ast/tag_node.rb', line 84

def remove_directive(name)
  @directives.delete(name)
end

#render(_context) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Render the node to a string



37
38
39
# File 'lib/orb/ast/tag_node.rb', line 37

def render(_context)
  raise "Not implemented"
end

#self_closing?Boolean

Determine whether the TagNode represents a self-closing (void) tag

Returns:

  • (Boolean)

    true if the tag is self-closing, false otherwise



53
54
55
# File 'lib/orb/ast/tag_node.rb', line 53

def self_closing?
  @meta.fetch(:self_closing, false)
end

#slotString

Retrieve the slot name from the tag

Returns:

  • (String)

    the slot name



155
156
157
# File 'lib/orb/ast/tag_node.rb', line 155

def slot
  @tag.split(SLOT_SEPARATOR).last.underscore
end

#splat_attributesArray<Attribute>

Retrieve all the splat attributes for the tag. A splat attribute is one that has a splat type. The splat attribute is used to pass a hash of attributes to the tag at runtime.

Returns:

  • (Array<Attribute>)

    the splat attributes for the tag



119
120
121
# File 'lib/orb/ast/tag_node.rb', line 119

def splat_attributes
  attributes.select(&:splat?)
end

#static?Boolean

Determine whether the tag can be compiled as static HTML

Returns:

  • (Boolean)

    true if the tag can be compiled as static HTML, false otherwise



175
176
177
# File 'lib/orb/ast/tag_node.rb', line 175

def static?
  splat_attributes.empty?
end

#static_attributesArray<Attribute>

Retrieve all the static attributes for the tag. A static attribute is one that has a string or boolean value.

Returns:

  • (Array<Attribute>)

    the static attributes for the tag



100
101
102
# File 'lib/orb/ast/tag_node.rb', line 100

def static_attributes
  attributes.select(&:static?)
end

#verbatim?Boolean

Determine whether the tag content should be escaped or treated as verbatim

Returns:

  • (Boolean)

    true if the tag content should be escaped, false otherwise



203
204
205
# File 'lib/orb/ast/tag_node.rb', line 203

def verbatim?
  @meta.fetch(:verbatim, false)
end