Module: Hexp::Node::Attributes

Included in:
Hexp::Node
Defined in:
lib/hexp/node/attributes.rb

Overview

Node API methods that deal with attributes

Instance Method Summary collapse

Instance Method Details

#[](attr_name) ⇒ String

Attribute accessor

Returns:

  • (String)

    The value of the attribute



158
159
160
# File 'lib/hexp/node/attributes.rb', line 158

def [](attr_name)
  self.attributes[attr_name.to_s]
end

#add_class(klass) ⇒ Hexp::Node

Add a CSS class to the element

Examples:

H[:div].add_class('foo') #=> H[:div, class: 'foo']

Parameters:

  • klass (#to_s)

    The class to add

Returns:



79
80
81
# File 'lib/hexp/node/attributes.rb', line 79

def add_class(klass)
  attr('class', [attr('class'), klass].compact.join(' '))
end

#attr(*args) ⇒ String|Hexp::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attribute getter/setter

When called with one argument : return the attribute value with that name. When called with two arguments : return a new Node with the attribute set. When the second argument is nil : return a new Node with the attribute unset.

Examples:

H[:p, class: 'hello'].attr('class')       # => "hello"
H[:p, class: 'hello'].attr('id', 'para1') # => H[:p, {"class"=>"hello", "id"=>"para1"}]
H[:p, class: 'hello'].attr('class', nil)  # => H[:p]

Parameters:

  • args (Array<#to_s>)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hexp/node/attributes.rb', line 21

def attr(*args)
  arity     = args.count
  attr_name = args[0].to_s

  case arity
  when 1
    attributes[attr_name]
  when 2
    set_attr(*args)
  else
    raise ArgumentError, "wrong number of arguments(#{arity} for 1..2)"
  end
end

#class?(klass) ⇒ Boolean

Check for the presence of a class

Examples:

H[:span, class: "banner strong"].class?("strong") #=> true

Parameters:

  • klass (String)

    The name of the class to check for

Returns:

  • (Boolean)

    True if the class is present, false otherwise



64
65
66
# File 'lib/hexp/node/attributes.rb', line 64

def class?(klass)
  attr('class') && attr('class').split(' ').include?(klass.to_s)
end

#class_listArray<String>

The CSS classes of this element as an array

Convenience method so you don’t have to split the class list yourself.

Returns:

  • (Array<String>)


90
91
92
# File 'lib/hexp/node/attributes.rb', line 90

def class_list
  @class_list ||= (attr('class') || '').split(' ')
end

#has_attr?(name) ⇒ true, false

Is an attribute present

This will also return true if the attribute is present but empty.

Examples:

H[:option].has_attr?('selected') #=> false

Parameters:

  • name (String|Symbol)

    The name of the attribute

Returns:

  • (true, false)


48
49
50
# File 'lib/hexp/node/attributes.rb', line 48

def has_attr?(name)
  attributes.has_key? name.to_s
end

#merge_attrs(node_or_hash) ⇒ Hexp::Node Also known as: %

Merge attributes into this Hexp

Class attributes are treated special : the class lists are merged, rather than being overwritten. See #set_attrs for a more basic version.

This method is analoguous with ‘Hash#merge`. As argument it can take a Hash, or another Hexp element, in which case that element’s attributes are used.

Returns:



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/hexp/node/attributes.rb', line 176

def merge_attrs(node_or_hash)
  hash = node_or_hash.respond_to?(:to_hexp) ?
           node_or_hash.to_hexp.attributes : node_or_hash
  result = self
  hash.each do |key,value|
    result = if key.to_s == 'class'
               result.add_class(value)
             else
               result.attr(key, value)
             end
  end
  result
end

#remove_attr(name) ⇒ Hexp::Node

Remove an attribute by name

Parameters:

  • name (#to_s)

    The attribute to be removed

Returns:

  • (Hexp::Node)

    A new node with the attribute removed



141
142
143
144
145
146
147
# File 'lib/hexp/node/attributes.rb', line 141

def remove_attr(name)
  H[
    self.tag,
    self.attributes.reject {|key,_| key == name.to_s},
    self.children
  ]
end

#remove_class(klass) ⇒ Hexp::Node

Remove a CSS class from this element

If the resulting class list is empty, the class attribute will be removed. If the class is present several times all instances will be removed. If it’s not present at all, the class list will be unmodified.

Calling this on a node with a class attribute that is equal to an empty string will result in the class attribute being removed.

Parameters:

  • klass (#to_s)

    The class to be removed

Returns:

  • (Hexp::Node)

    A node that is identical to this one, but with the given class removed



110
111
112
113
114
115
# File 'lib/hexp/node/attributes.rb', line 110

def remove_class(klass)
  return self unless has_attr?('class')
  new_list = class_list - [klass.to_s]
  return remove_attr('class') if new_list.empty?
  attr('class', new_list.join(' '))
end

#set_attrs(attrs) ⇒ Hexp::Node

Set or override multiple attributes using a hash syntax

Parameters:

Returns:



124
125
126
127
128
129
130
# File 'lib/hexp/node/attributes.rb', line 124

def set_attrs(attrs)
  H[
    self.tag,
    Hash[*attrs.flat_map{|k,v| [k.to_s, v]}],
    self.children
  ]
end