Method: Nokogiri::XML::Node#add_class
- Defined in:
- lib/nokogiri/xml/node.rb
permalink #add_class(names) ⇒ Object
:call-seq: add_class(names) → self
Ensure HTML CSS classes are present on self
. Any CSS classes in names
that already exist in the “class” attribute are not added. Note that any existing duplicates in the “class” attribute are not removed. Compare with #append_class.
This is a convenience function and is equivalent to:
node.kwattr_add("class", names)
See related: #kwattr_add, #classes, #append_class, #remove_class
- Parameters
-
names
(String, Array<String>)CSS class names to be added to the Node’s “class” attribute. May be a string containing whitespace-delimited names, or an Array of String names. Any class names already present will not be added. Any class names not present will be added. If no “class” attribute exists, one is created.
- Returns
-
self
(Node) for ease of chaining method calls.
Example: Ensure that the node has CSS class “section”
node # => <div></div>
node.add_class("section") # => <div class="section"></div>
node.add_class("section") # => <div class="section"></div> # duplicate not added
Example: Ensure that the node has CSS classes “section” and “header”, via a String argument
Note that the CSS class “section” is not added because it is already present. Note also that the pre-existing duplicate CSS class “section” is not removed.
node # => <div class="section section"></div>
node.add_class("section header") # => <div class="section section header"></div>
Example: Ensure that the node has CSS classes “section” and “header”, via an Array argument
node # => <div></div>
node.add_class(["section", "header"]) # => <div class="section header"></div>
790 791 792 |
# File 'lib/nokogiri/xml/node.rb', line 790 def add_class(names) kwattr_add("class", names) end |