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

- (String) [](attr_name)

Attribute accessor

Parameters:

  • attr_name (#to_s)

    The name of the attribute

Returns:

  • (String)

    The value of the attribute



144
145
146
# File 'lib/hexp/node/attributes.rb', line 144

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

- (Hexp::Node) add_class(klass)

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:



71
72
73
# File 'lib/hexp/node/attributes.rb', line 71

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

- (String|Hexp::Node) attr(*args)

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]

Returns:



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

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

- (Boolean) class?(klass)

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



58
59
60
# File 'lib/hexp/node/attributes.rb', line 58

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

- (Array<String>) class_list

The CSS classes of this element as an array

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

Returns:



82
83
84
# File 'lib/hexp/node/attributes.rb', line 82

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

- (Boolean) has_attr?(name)

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:

  • (Boolean)


45
46
47
# File 'lib/hexp/node/attributes.rb', line 45

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

- (Hexp::Node) merge_attrs(node_or_hash)

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.

Parameters:

  • node_or_hash (#to_hexp|Hash)

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hexp/node/attributes.rb', line 161

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

- (Hexp::Node) remove_attr(name)

Remove an attribute by name

Parameters:

  • name (#to_s)

    The attribute to be removed

Returns:

  • (Hexp::Node)

    a new node with the attribute removed



130
131
132
133
134
135
136
# File 'lib/hexp/node/attributes.rb', line 130

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

- (Hexp::Node) remove_class(klass)

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



101
102
103
104
105
106
# File 'lib/hexp/node/attributes.rb', line 101

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

- (Hexp::Node) set_attrs(attrs) Also known as: %, add_attributes

Set or override multiple attributes using a hash syntax

Parameters:

  • attrs (Hash)

Returns:



114
115
116
117
118
119
120
# File 'lib/hexp/node/attributes.rb', line 114

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