Class: BetterHtml::Tree::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/better_html/tree/attribute.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Attribute

Returns a new instance of Attribute.



16
17
18
19
# File 'lib/better_html/tree/attribute.rb', line 16

def initialize(node)
  @node = node
  @name_node, @equal_node, @value_node = *node if @node.type == :attribute
end

Instance Attribute Details

#equal_nodeObject (readonly)

Returns the value of attribute equal_node.



8
9
10
# File 'lib/better_html/tree/attribute.rb', line 8

def equal_node
  @equal_node
end

#name_nodeObject (readonly)

Returns the value of attribute name_node.



8
9
10
# File 'lib/better_html/tree/attribute.rb', line 8

def name_node
  @name_node
end

#nodeObject (readonly)

Returns the value of attribute node.



8
9
10
# File 'lib/better_html/tree/attribute.rb', line 8

def node
  @node
end

#value_nodeObject (readonly)

Returns the value of attribute value_node.



8
9
10
# File 'lib/better_html/tree/attribute.rb', line 8

def value_node
  @value_node
end

Class Method Details

.from_node(node) ⇒ Object



11
12
13
# File 'lib/better_html/tree/attribute.rb', line 11

def from_node(node)
  new(node)
end

Instance Method Details

#erb?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/better_html/tree/attribute.rb', line 21

def erb?
  @node.type == :erb
end

#locObject



25
26
27
# File 'lib/better_html/tree/attribute.rb', line 25

def loc
  @node.loc
end

#nameObject



29
30
31
# File 'lib/better_html/tree/attribute.rb', line 29

def name
  @name_node&.loc&.source&.downcase
end

#valueObject



33
34
35
36
# File 'lib/better_html/tree/attribute.rb', line 33

def value
  parts = value_node.to_a.reject { |node| node.is_a?(::AST::Node) && node.type == :quote }
  parts.map { |s| s.is_a?(::AST::Node) ? s.loc.source : CGI.unescapeHTML(s) }.join
end