Class: Hpricot::Styles

Inherits:
Object show all
Defined in:
lib/hpricot_ext.rb

Instance Method Summary collapse

Constructor Details

#initialize(e) ⇒ Styles

Returns a new instance of Styles.



23
24
25
# File 'lib/hpricot_ext.rb', line 23

def initialize e
  @element = e
end

Instance Method Details

#[](key) ⇒ Object



34
35
36
# File 'lib/hpricot_ext.rb', line 34

def [] key
  properties[key]
end

#[]=(k, v) ⇒ Object



38
39
40
41
# File 'lib/hpricot_ext.rb', line 38

def []= k, v 
  s = properties.map {|pty,val| "#{pty}:#{val}"}.join(";")
  @element.set_attribute("style", "#{s.chomp(";")};#{k}:#{v}".sub(/^\;/, ""))
end

#delete(key) ⇒ Object



27
28
29
30
31
32
# File 'lib/hpricot_ext.rb', line 27

def delete(key)
  p = properties.dup
  if p.delete key
    @element.set_attribute("style", "#{p.map {|pty,val| "#{pty}:#{val}"}.join(";")}")
  end
end

#empty?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/hpricot_ext.rb', line 43

def empty?
  return true if properties.size == 0
end

#has_style?(key) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/hpricot_ext.rb', line 47

def has_style?(key)
  properties.has_key?(key)
end

#propertiesObject



59
60
61
62
63
64
65
# File 'lib/hpricot_ext.rb', line 59

def properties
  return {} if not @element.has_attribute?("style")
  @element.get_attribute("style").split(";").inject({}) do |hash,v|
    v = v.split(":")
    hash.update v.first.strip => v.last.strip
  end
end

#to_hObject



55
56
57
# File 'lib/hpricot_ext.rb', line 55

def to_h
  properties
end

#to_sObject



51
52
53
# File 'lib/hpricot_ext.rb', line 51

def to_s
  properties.to_s
end