Class: HamlLint::Tree::TagNode

Inherits:
Node
  • Object
show all
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

Methods inherited from Node

#comment_configuration, #directives, #disabled?, #each, #initialize, #inspect, #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_sourceHash

Returns the source code for the static and dynamic attributes of a tag.

Examples:

For ‘%tag.class{ id: ’hello’ }(lang=en)‘, this returns:

{ :static => '.class', :hash => " id: 'hello' ", :html => "lang=en" }

Returns:

  • (Hash)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/haml_lint/tree/tag_node.rb', line 100

def attributes_source
  @attributes_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 ‘=`).

Returns:

  • (true, false)


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_sourceHash

Returns the source code for the dynamic attributes defined in ‘…`, `(…)`, or `[…]` after a tag name.

Examples:

For ‘%tag.class{ id: ’hello’ }(lang=en)‘, this returns:

{ :hash => " id: 'hello' ", :html => "lang=en" }

Returns:

  • (Hash)


88
89
90
91
# File 'lib/haml_lint/tree/tag_node.rb', line 88

def dynamic_attributes_source
  @dynamic_attributes_source ||=
    attributes_source.reject { |key| key == :static }
end

#dynamic_attributes_sourcesArray<String>

Note:

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.

Returns:

  • (Array<String>)


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.

Returns:

  • (true, false)


39
40
41
# File 'lib/haml_lint/tree/tag_node.rb', line 39

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’ }‘).

Returns:

  • (true, false)


131
132
133
# File 'lib/haml_lint/tree/tag_node.rb', line 131

def hash_attributes?
  !dynamic_attributes_source[:hash].nil?
end

#hash_attributes_sourceString

Attributes defined after the tag name in Ruby hash brackets (‘{}`).

Examples:

For ‘%tag.class{ lang: ’en’ }‘, this returns:

" lang: 'en' "

Returns:

  • (String)

    source without the surrounding curly braces



141
142
143
# File 'lib/haml_lint/tree/tag_node.rb', line 141

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)`).

Returns:

  • (true, false)


149
150
151
# File 'lib/haml_lint/tree/tag_node.rb', line 149

def html_attributes?
  !dynamic_attributes_source[:html].nil?
end

#html_attributes_sourceString?

Attributes defined after the tag name in parentheses (‘()`).

Examples:

For ‘%tag.class(lang=en)`, this returns:

"lang=en"

Returns:

  • (String, nil)

    source without the surrounding parentheses, or ‘nil` if it has not been defined



160
161
162
# File 'lib/haml_lint/tree/tag_node.rb', line 160

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).

Returns:

  • (true, false)


183
184
185
# File 'lib/haml_lint/tree/tag_node.rb', line 183

def object_reference?
  @value[:object_ref].to_s != 'nil'
end

#object_reference_sourceString?

Source code for the contents of the node’s object reference.

Returns:

  • (String, nil)

    string source of object reference or ‘nil` if it has not been defined

See Also:



192
193
194
# File 'lib/haml_lint/tree/tag_node.rb', line 192

def object_reference_source
  @value[:object_ref][/\A\[(.*)\]\z/, 1] if object_reference?
end

#parsed_attributesParsedRuby

The attributes given to the tag parsed into a Ruby syntax tree.

Returns:

  • (ParsedRuby)

    syntax tree in the form returned by Parser gem



199
200
201
# File 'lib/haml_lint/tree/tag_node.rb', line 199

def parsed_attributes
  HamlLint::ParsedRuby.new(HamlLint::RubyParser.new.parse(hash_attributes_source || ''))
end

#parsed_scriptParsedRuby

The Ruby script contents of a tag parsed into a syntax tree.

Returns:

  • (ParsedRuby)

    syntax tree in the form returned by Parser gem



206
207
208
# File 'lib/haml_lint/tree/tag_node.rb', line 206

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.

Returns:

  • (true, false)


214
215
216
# File 'lib/haml_lint/tree/tag_node.rb', line 214

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.

Returns:

  • (true, false)


222
223
224
# File 'lib/haml_lint/tree/tag_node.rb', line 222

def remove_outer_whitespace?
  !!@value[:nuke_outer_whitespace] # rubocop:disable Style/DoubleNegation
end

#scriptString

Returns the script source that will be evaluated to produce this tag’s inner content, if any.

Returns:

  • (String)


230
231
232
# File 'lib/haml_lint/tree/tag_node.rb', line 230

def script
  (@value[:value] if @value[:parse]) || ''
end

#static_attributes_sourceString

Static element attributes defined after the tag name.

Examples:

For ‘%tag.button#start-button`, this returns:

'.button#start-button'

Returns:

  • (String)


77
78
79
# File 'lib/haml_lint/tree/tag_node.rb', line 77

def static_attributes_source
  attributes_source[:static] || ''
end

#static_classesArray<String>

List of classes statically defined for this tag.

Examples:

For ‘%tag.button.button-info{ class: status }`, this returns:

['button', 'button-info']

Returns:

  • (Array<String>)

    list of statically defined classes with leading dot removed



50
51
52
53
54
55
# File 'lib/haml_lint/tree/tag_node.rb', line 50

def static_classes
  @static_classes ||=
    begin
      static_attributes_source.scan(/\.([-:\w]+)/)
    end
end

#static_idsArray<String>

List of ids statically defined for this tag.

Examples:

For ‘%tag.button#start-button{ id: special_id }`, this returns:

['start-button']

Returns:

  • (Array<String>)

    list of statically defined ids with leading ‘#` removed



64
65
66
67
68
69
# File 'lib/haml_lint/tree/tag_node.rb', line 64

def static_ids
  @static_ids ||=
    begin
      static_attributes_source.scan(/#([-:\w]+)/)
    end
end

#tag_idString

ID of the HTML tag.

Returns:

  • (String)


167
168
169
# File 'lib/haml_lint/tree/tag_node.rb', line 167

def tag_id
  @value[:attributes]['id']
end

#tag_nameString

Name of the HTML tag.

Returns:

  • (String)


174
175
176
# File 'lib/haml_lint/tree/tag_node.rb', line 174

def tag_name
  @value[:name]
end