Module: Ayril::XMLElement::ElementClassnameManipulation

Included in:
Ayril::XMLElement
Defined in:
lib/ayril/xml_element/element_classname_manipulation.rb

Instance Method Summary collapse

Instance Method Details

#add_class_name(class_name) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ayril/xml_element/element_classname_manipulation.rb', line 10

def add_class_name(class_name)
  if not self.has_class_name? class_name
    current = if not self.has_attribute? "class" then ''
              else self.read_attribute("class") end
  end
  self.write_attribute "class", (current + ((current == '') ? '' : ' ') + class_name)
  self
end

#has_class_name?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
# File 'lib/ayril/xml_element/element_classname_manipulation.rb', line 4

def has_class_name?(class_name)
  classes = self.read_attribute "class"
  !!(classes.length > 0 and (classes == class_name or 
     classes =~ /(^|\s)#{class_name}(\s|$)/))
end

#remove_class_name(class_name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ayril/xml_element/element_classname_manipulation.rb', line 19

def remove_class_name(class_name)
  return self if not self.has_attribute? "class"
  string_value = self.read_attribute("class").sub(/(^|\s+)#{class_name}(\s+|$)/, ' ').strip
  string_value == '' ? self.remove_attribute("class")
                     : self.write_attribute("class", string_value)
  self.tap { |s| s.attribute.sync; s.class_names.sync }
end

#toggle_class_name(class_name) ⇒ Object



27
28
29
30
31
# File 'lib/ayril/xml_element/element_classname_manipulation.rb', line 27

def toggle_class_name(class_name)
  if self.has_class_name? class_name
  then self.remove_class_name class_name
  else self.add_class_name class_name end
end