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)
-
- (String) [](attr_name)
Attribute accessor.
-
- (Hexp::Node) add_class(klass)
Add a CSS class to the element.
-
- (String|Hexp::Node) attr(*args)
private
Attribute getter/setter.
-
- (Boolean) class?(klass)
Check for the presence of a class.
-
- (Array<String>) class_list
The CSS classes of this element as an array.
-
- (Boolean) has_attr?(name)
Is an attribute present.
-
- (Hexp::Node) merge_attrs(node_or_hash)
Merge attributes into this Hexp.
-
- (Hexp::Node) remove_attr(name)
Remove an attribute by name.
-
- (Hexp::Node) remove_class(klass)
Remove a CSS class from this element.
-
- (Hexp::Node) set_attrs(attrs)
(also: #%, #add_attributes)
Set or override multiple attributes using a hash syntax.
Instance Method Details
- (String) [](attr_name)
Attribute accessor
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
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.
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
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.
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.
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.
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
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.
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
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 |