Class: HamlLint::Tree::TagNode
- Defined in:
- lib/haml_lint/tree/tag_node.rb
Overview
Represents a tag node in a HAML document.
Instance Attribute Summary
Attributes inherited from Node
#children, #line, #parent, #type
Instance Method Summary collapse
-
#attributes_source ⇒ Hash
Returns the source code for the static and dynamic attributes of a tag.
-
#contains_script? ⇒ true, false
Returns whether this tag contains executable script (e.g. is followed by a ‘=`).
-
#dynamic_attributes_source ⇒ Hash
Returns the source code for the dynamic attributes defined in ‘…`, `(…)`, or `[…]` after a tag name.
-
#dynamic_attributes_sources ⇒ Array<String>
Computed set of attribute hashes code.
-
#has_hash_attribute?(attribute) ⇒ true, false
Returns whether this tag has a specified attribute.
-
#hash_attributes? ⇒ true, false
Whether this tag node has a set of hash attributes defined via the curly brace syntax (e.g. ‘%tag{ lang: ’en’ }‘).
-
#hash_attributes_source ⇒ String
Attributes defined after the tag name in Ruby hash brackets (‘{}`).
-
#html_attributes? ⇒ true, false
Whether this tag node has a set of HTML attributes defined via the parentheses syntax (e.g. ‘%tag(lang=en)`).
-
#html_attributes_source ⇒ String?
Attributes defined after the tag name in parentheses (‘()`).
-
#object_reference? ⇒ true, false
Whether this tag node has a set of square brackets (e.g. ‘%tag`) following it that indicates its class and ID will be to the value of the given object’s #to_key or #id method (in that order).
-
#object_reference_source ⇒ String?
Source code for the contents of the node’s object reference.
-
#remove_inner_whitespace? ⇒ true, false
Whether this node had a ‘<` after it signifying that outer whitespace should be removed.
-
#remove_outer_whitespace? ⇒ true, false
Whether this node had a ‘>` after it signifying that outer whitespace should be removed.
-
#script ⇒ String
Returns the script source that will be evaluated to produce this tag’s inner content, if any.
-
#static_attributes_source ⇒ String
Static element attributes defined after the tag name.
-
#static_classes ⇒ Array<String>
List of classes statically defined for this tag.
-
#static_ids ⇒ Array<String>
List of ids statically defined for this tag.
-
#tag_name ⇒ String
Name of the HTML tag.
Methods inherited from Node
#find, #initialize, #inspect, #next_node, #source_code, #successor, #text
Constructor Details
This class inherits a constructor from HamlLint::Tree::Node
Instance Method Details
#attributes_source ⇒ Hash
Returns the source code for the static and dynamic attributes of a tag.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/haml_lint/tree/tag_node.rb', line 87 def attributes_source @attr_source ||= begin _explicit_tag, static_attrs, rest = source_code.scan(/\A\s*(%[-:\w]+)?([-:\w\.\#]*)(.*)/m)[0] attr_types = { '{' => [:hash, %w[{ }]], '(' => [:html, %w[( )]], '[' => [:object_ref, %w[[ ]]], } attr_source = { static: static_attrs } while rest type, chars = attr_types[rest[0]] break unless type # Not an attribute opening character, so we're done # Can't define multiple of the same attribute type (e.g. two {...}) break if attr_source[type] attr_source[type], rest = Haml::Util.balance(rest, *chars) end attr_source end end |
#contains_script? ⇒ true, false
Returns whether this tag contains executable script (e.g. is followed by a ‘=`).
19 20 21 |
# File 'lib/haml_lint/tree/tag_node.rb', line 19 def contains_script? @value[:parse] && !@value[:value].strip.empty? end |
#dynamic_attributes_source ⇒ Hash
Returns the source code for the dynamic attributes defined in ‘…`, `(…)`, or `[…]` after a tag name.
75 76 77 78 |
# File 'lib/haml_lint/tree/tag_node.rb', line 75 def dynamic_attributes_source @dynamic_attributes_source ||= attributes_source.reject { |key| key == :static } end |
#dynamic_attributes_sources ⇒ Array<String>
Computed set of attribute hashes code.
This is a combination of all dynamically calculated attributes from the different attribute setting syntaxes (‘…`/`(…)`), converted into Ruby code.
11 12 13 |
# File 'lib/haml_lint/tree/tag_node.rb', line 11 def dynamic_attributes_sources @value[:attributes_hashes] end |
#has_hash_attribute?(attribute) ⇒ true, false
Returns whether this tag has a specified attribute.
26 27 28 |
# File 'lib/haml_lint/tree/tag_node.rb', line 26 def has_hash_attribute?(attribute) hash_attributes? && existing_attributes.include?(attribute) end |
#hash_attributes? ⇒ true, false
Whether this tag node has a set of hash attributes defined via the curly brace syntax (e.g. ‘%tag{ lang: ’en’ }‘).
118 119 120 |
# File 'lib/haml_lint/tree/tag_node.rb', line 118 def hash_attributes? !dynamic_attributes_source[:hash].nil? end |
#hash_attributes_source ⇒ String
Attributes defined after the tag name in Ruby hash brackets (‘{}`).
128 129 130 |
# File 'lib/haml_lint/tree/tag_node.rb', line 128 def hash_attributes_source dynamic_attributes_source[:hash] end |
#html_attributes? ⇒ true, false
Whether this tag node has a set of HTML attributes defined via the parentheses syntax (e.g. ‘%tag(lang=en)`).
136 137 138 |
# File 'lib/haml_lint/tree/tag_node.rb', line 136 def html_attributes? !dynamic_attributes_source[:html].nil? end |
#html_attributes_source ⇒ String?
Attributes defined after the tag name in parentheses (‘()`).
147 148 149 |
# File 'lib/haml_lint/tree/tag_node.rb', line 147 def html_attributes_source dynamic_attributes_source[:html][/\A\((.*)\)\z/, 1] if html_attributes? end |
#object_reference? ⇒ true, false
Whether this tag node has a set of square brackets (e.g. ‘%tag`) following it that indicates its class and ID will be to the value of the given object’s #to_key or #id method (in that order).
163 164 165 |
# File 'lib/haml_lint/tree/tag_node.rb', line 163 def object_reference? @value[:object_ref].to_s != 'nil' end |
#object_reference_source ⇒ String?
Source code for the contents of the node’s object reference.
172 173 174 |
# File 'lib/haml_lint/tree/tag_node.rb', line 172 def object_reference_source @value[:object_ref][/\A\[(.*)\]\z/, 1] if object_reference? end |
#remove_inner_whitespace? ⇒ true, false
Whether this node had a ‘<` after it signifying that outer whitespace should be removed.
180 181 182 |
# File 'lib/haml_lint/tree/tag_node.rb', line 180 def remove_inner_whitespace? @value[:nuke_inner_whitespace] end |
#remove_outer_whitespace? ⇒ true, false
Whether this node had a ‘>` after it signifying that outer whitespace should be removed.
188 189 190 |
# File 'lib/haml_lint/tree/tag_node.rb', line 188 def remove_outer_whitespace? @value[:nuke_outer_whitespace] end |
#script ⇒ String
Returns the script source that will be evaluated to produce this tag’s inner content, if any.
196 197 198 |
# File 'lib/haml_lint/tree/tag_node.rb', line 196 def script (@value[:value] if @value[:parse]) || '' end |
#static_attributes_source ⇒ String
Static element attributes defined after the tag name.
64 65 66 |
# File 'lib/haml_lint/tree/tag_node.rb', line 64 def static_attributes_source attributes_source[:static] || '' end |
#static_classes ⇒ Array<String>
List of classes statically defined for this tag.
37 38 39 40 41 42 |
# File 'lib/haml_lint/tree/tag_node.rb', line 37 def static_classes @static_classes ||= begin static_attributes_source.scan(/\.([-:\w]+)/) end end |
#static_ids ⇒ Array<String>
List of ids statically defined for this tag.
51 52 53 54 55 56 |
# File 'lib/haml_lint/tree/tag_node.rb', line 51 def static_ids @static_ids ||= begin static_attributes_source.scan(/#([-:\w]+)/) end end |
#tag_name ⇒ String
Name of the HTML tag.
154 155 156 |
# File 'lib/haml_lint/tree/tag_node.rb', line 154 def tag_name @value[:name] end |