Class: A11y::Lint::SlimNode

Inherits:
Object
  • Object
show all
Includes:
BlockInspection
Defined in:
lib/a11y/lint/slim_node.rb

Overview

Wraps a Slim AST s-expression as a queryable node for lint rules.

Constant Summary

Constants included from BlockInspection

BlockInspection::ICON_HELPERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BlockInspection

#block_has_only_icon_helpers?

Constructor Details

#initialize(sexp, line:) ⇒ SlimNode

Returns a new instance of SlimNode.



11
12
13
14
# File 'lib/a11y/lint/slim_node.rb', line 11

def initialize(sexp, line:)
  @sexp = sexp
  @line = line
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



9
10
11
# File 'lib/a11y/lint/slim_node.rb', line 9

def line
  @line
end

Instance Method Details

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/a11y/lint/slim_node.rb', line 32

def attribute?(name)
  attributes.key?(name)
end

#attributesObject



36
37
38
# File 'lib/a11y/lint/slim_node.rb', line 36

def attributes
  @attributes ||= extract_attributes
end

#block_body_codesObject

Returns ruby_code strings from child :slim :output nodes inside a block body. Only meaningful for output nodes (e.g. ‘= button_tag(…) do`).



54
55
56
57
58
# File 'lib/a11y/lint/slim_node.rb', line 54

def block_body_codes
  return unless slim_output?

  collect_output_codes(@sexp[4])
end

#block_has_text_children?Boolean

Returns true when the block body contains visible text or HTML tag children (i.e. content that provides an accessible name).

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/a11y/lint/slim_node.rb', line 62

def block_has_text_children?
  return false unless slim_output?

  text_content?(@sexp[4])
end

#call_nodeObject



26
27
28
29
30
# File 'lib/a11y/lint/slim_node.rb', line 26

def call_node
  return unless slim_output?

  @call_node ||= parse_call_node
end

#childrenObject

Returns direct HTML element children as SlimNode objects. Walks through [:multi] and [:slim, :control] wrappers so that tags nested inside control flow are still treated as direct children. Opaque [:slim, :output] blocks are skipped.



44
45
46
47
48
49
# File 'lib/a11y/lint/slim_node.rb', line 44

def children
  return [] unless html_tag?

  body = @sexp[4]
  collect_children(body)
end

#ruby_codeObject



20
21
22
23
24
# File 'lib/a11y/lint/slim_node.rb', line 20

def ruby_code
  return unless slim_output?

  @sexp[3]
end

#tag_nameObject



16
17
18
# File 'lib/a11y/lint/slim_node.rb', line 16

def tag_name
  @sexp[2]
end