Class: ORB::AST::TagNode
- Inherits:
-
AbstractNode
- Object
- AbstractNode
- ORB::AST::TagNode
- 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
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Attributes inherited from AbstractNode
Instance Method Summary collapse
-
#attributes ⇒ Array<Attribute>
Retrieve the attributes for the tag.
-
#clear_directives ⇒ Object
Clear all directives from the tag.
- #compiler_directives? ⇒ Boolean
-
#component ⇒ String
Retrieve the component name from the tag.
-
#component_module ⇒ String
Retrieve the module name from the component name The module name is the first part of the component name separated by a period.
-
#component_slot_tag? ⇒ Boolean
Determine whether the TagNode represents a component slot tag A component slot tag is of the form
Component:slotand is used to render a slot within a component. -
#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.
-
#directives ⇒ Array<Array<Attribute>>
Retrieve the directives for the tag.
-
#directives? ⇒ Boolean
Check whether the node has any directives.
-
#dynamic? ⇒ Boolean
Determine whether the tag needs to be compiled as dynamic HTML.
-
#dynamic_attributes ⇒ Array<Attribute>
Retrieve all the dynamic attributes for the tag.
-
#html_tag? ⇒ Boolean
Determine whether the TagNode represents an HTML tag.
-
#initialize(token) ⇒ TagNode
constructor
Create a new TagNode from the given token.
-
#remove_directive(name) ⇒ Object
Remove a directive from the tag, i.e.
-
#render(_context) ⇒ Object
private
Render the node to a string.
-
#self_closing? ⇒ Boolean
Determine whether the TagNode represents a self-closing (void) tag.
-
#slot ⇒ String
Retrieve the slot name from the tag.
-
#splat_attributes ⇒ Array<Attribute>
Retrieve all the splat attributes for the tag.
-
#static? ⇒ Boolean
Determine whether the tag can be compiled as static HTML.
-
#static_attributes ⇒ Array<Attribute>
Retrieve all the static attributes for the tag.
-
#verbatim? ⇒ Boolean
Determine whether the tag content should be escaped or treated as verbatim.
Methods inherited from AbstractNode
Constructor Details
#initialize(token) ⇒ TagNode
Create a new TagNode from the given token
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 = token. # Parse attributes from the metadata @raw_attributes = .fetch(:attributes, []).map do |attr| name, type, value = attr Attribute.new(name, type, value) end end |
Instance Attribute Details
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
12 13 14 |
# File 'lib/orb/ast/tag_node.rb', line 12 def end |
#tag ⇒ Object (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
#attributes ⇒ Array<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.
64 65 66 67 |
# File 'lib/orb/ast/tag_node.rb', line 64 def attributes @attributes ||= @raw_attributes.reject(&:directive?) @attributes end |
#clear_directives ⇒ Object
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
195 196 197 |
# File 'lib/orb/ast/tag_node.rb', line 195 def compiler_directives? directives.any? { |k, _v| k == :if || k == :for } end |
#component ⇒ String
Retrieve the component name from the tag
147 148 149 |
# File 'lib/orb/ast/tag_node.rb', line 147 def component @tag.split(SLOT_SEPARATOR).first end |
#component_module ⇒ String
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
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.
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.
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 |
#directives ⇒ Array<Array<Attribute>>
Retrieve 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
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
183 184 185 |
# File 'lib/orb/ast/tag_node.rb', line 183 def dynamic? splat_attributes.any? end |
#dynamic_attributes ⇒ Array<Attribute>
Retrieve all the dynamic attributes for the tag. A dynamic attribute is one that has an expression value.
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
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
53 54 55 |
# File 'lib/orb/ast/tag_node.rb', line 53 def self_closing? .fetch(:self_closing, false) end |
#slot ⇒ String
Retrieve the slot name from the tag
155 156 157 |
# File 'lib/orb/ast/tag_node.rb', line 155 def slot @tag.split(SLOT_SEPARATOR).last.underscore end |
#splat_attributes ⇒ Array<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.
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
175 176 177 |
# File 'lib/orb/ast/tag_node.rb', line 175 def static? splat_attributes.empty? end |
#static_attributes ⇒ Array<Attribute>
Retrieve all the static attributes for the tag. A static attribute is one that has a string or boolean value.
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
203 204 205 |
# File 'lib/orb/ast/tag_node.rb', line 203 def verbatim? .fetch(:verbatim, false) end |