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 (‘()`).
-
#inline_marker_source ⇒ String
Source that follows the tag name and attributes.
-
#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.
-
#parsed_attributes ⇒ ParsedRuby
The attributes given to the tag parsed into a Ruby syntax tree.
-
#parsed_script ⇒ ParsedRuby
The Ruby script contents of a tag parsed into a syntax tree.
-
#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_id ⇒ String
ID of the HTML tag.
-
#tag_name ⇒ String
Name of the HTML tag.
-
#unescape_html? ⇒ true, false
Whether this tag outputs unescaped HTML via a ‘!` marker, e.g.
Methods inherited from Node
#comment_configuration, #directives, #disabled?, #each, #initialize, #inspect, #keyword, #line_numbers, #lines, #next_node, #predecessor, #source_code, #subsequents, #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.
106 107 108 |
# File 'lib/haml_lint/tree/tag_node.rb', line 106 def attributes_source parsed_attributes_source[:attributes] end |
#contains_script? ⇒ true, false
Returns whether this tag contains executable script (e.g. is followed by a ‘=`).
32 33 34 |
# File 'lib/haml_lint/tree/tag_node.rb', line 32 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.
94 95 96 97 |
# File 'lib/haml_lint/tree/tag_node.rb', line 94 def dynamic_attributes_source @dynamic_attributes_source ||= attributes_source.reject { |key| key == :static } end |
#dynamic_attributes_sources ⇒ Array<String>
This has to be memoized because of a design decision in Haml 5. When
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.
calling ‘DynamicAttributes#to_literal`, they mutate the “old” parameter using `String#sub!` instead of returning a new string. This means that any subsequent calls can return a nil instead of a string for that attribute, which causes any subsequent calls to the method to raise an error.
19 20 21 22 23 24 25 26 |
# File 'lib/haml_lint/tree/tag_node.rb', line 19 def dynamic_attributes_sources @dynamic_attributes_sources ||= if Gem::Version.new(Haml::VERSION) < Gem::Version.new('5') @value[:attributes_hashes] else Array(@value[:dynamic_attributes].to_literal).reject(&:empty?) end end |
#has_hash_attribute?(attribute) ⇒ true, false
Returns whether this tag has a specified attribute.
49 50 51 |
# File 'lib/haml_lint/tree/tag_node.rb', line 49 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’ }‘).
125 126 127 |
# File 'lib/haml_lint/tree/tag_node.rb', line 125 def hash_attributes? !dynamic_attributes_source[:hash].nil? end |
#hash_attributes_source ⇒ String
Attributes defined after the tag name in Ruby hash brackets (‘{}`).
135 136 137 |
# File 'lib/haml_lint/tree/tag_node.rb', line 135 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)`).
143 144 145 |
# File 'lib/haml_lint/tree/tag_node.rb', line 143 def html_attributes? !dynamic_attributes_source[:html].nil? end |
#html_attributes_source ⇒ String?
Attributes defined after the tag name in parentheses (‘()`).
154 155 156 |
# File 'lib/haml_lint/tree/tag_node.rb', line 154 def html_attributes_source dynamic_attributes_source[:html][/\A\((.*)\)\z/, 1] if html_attributes? end |
#inline_marker_source ⇒ String
Source that follows the tag name and attributes. It begins with the content marker (e.g. ‘=`, `!=`, `~`, `!~`, or plain text).
117 118 119 |
# File 'lib/haml_lint/tree/tag_node.rb', line 117 def inline_marker_source parsed_attributes_source[:marker] 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).
177 178 179 |
# File 'lib/haml_lint/tree/tag_node.rb', line 177 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.
186 187 188 |
# File 'lib/haml_lint/tree/tag_node.rb', line 186 def object_reference_source @value[:object_ref][/\A\[(.*)\]\z/, 1] if object_reference? end |
#parsed_attributes ⇒ ParsedRuby
The attributes given to the tag parsed into a Ruby syntax tree.
193 194 195 |
# File 'lib/haml_lint/tree/tag_node.rb', line 193 def parsed_attributes HamlLint::ParsedRuby.new(HamlLint::RubyParser.new.parse(hash_attributes_source || '')) end |
#parsed_script ⇒ ParsedRuby
The Ruby script contents of a tag parsed into a syntax tree.
200 201 202 |
# File 'lib/haml_lint/tree/tag_node.rb', line 200 def parsed_script HamlLint::ParsedRuby.new(HamlLint::RubyParser.new.parse(script || '')) end |
#remove_inner_whitespace? ⇒ true, false
Whether this node had a ‘<` after it signifying that outer whitespace should be removed.
208 209 210 |
# File 'lib/haml_lint/tree/tag_node.rb', line 208 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.
216 217 218 |
# File 'lib/haml_lint/tree/tag_node.rb', line 216 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.
224 225 226 |
# File 'lib/haml_lint/tree/tag_node.rb', line 224 def script (@value[:value] if @value[:parse]) || '' end |
#static_attributes_source ⇒ String
Static element attributes defined after the tag name.
83 84 85 |
# File 'lib/haml_lint/tree/tag_node.rb', line 83 def static_attributes_source attributes_source[:static] || '' end |
#static_classes ⇒ Array<String>
List of classes statically defined for this tag.
60 61 62 63 |
# File 'lib/haml_lint/tree/tag_node.rb', line 60 def static_classes @static_classes ||= static_attributes_source.scan(/\.([-:\w]+)/) end |
#static_ids ⇒ Array<String>
List of ids statically defined for this tag.
72 73 74 75 |
# File 'lib/haml_lint/tree/tag_node.rb', line 72 def static_ids @static_ids ||= static_attributes_source.scan(/#([-:\w]+)/) end |
#tag_id ⇒ String
ID of the HTML tag.
161 162 163 |
# File 'lib/haml_lint/tree/tag_node.rb', line 161 def tag_id @value[:attributes]['id'] end |
#tag_name ⇒ String
Name of the HTML tag.
168 169 170 |
# File 'lib/haml_lint/tree/tag_node.rb', line 168 def tag_name @value[:name] end |
#unescape_html? ⇒ true, false
Whether this tag outputs unescaped HTML via a ‘!` marker, e.g. `%tag!=` or `%tag!~`.
40 41 42 43 44 |
# File 'lib/haml_lint/tree/tag_node.rb', line 40 def unescape_html? return false unless contains_script? /\A\s*[<>]*\s*!/.match?(inline_marker_source) end |