Class: RSAC::StyleSheet

Inherits:
DocumentHandler show all
Defined in:
lib/antisamy/csspool/rsac/stylesheet/rule.rb,
lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb

Defined Under Namespace

Classes: Rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DocumentHandler

#comment, #end_document, #end_font_face, #end_media, #end_page, #ignorable_at_rule, #import_style, #namespace_declaration, #start_document, #start_font_face, #start_media, #start_page

Constructor Details

#initialize(sac) ⇒ StyleSheet

Returns a new instance of StyleSheet.



5
6
7
8
9
10
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 5

def initialize(sac)
  @sac   = sac
  @rules = []
  @current_rules = []
  @selector_index = 0
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



3
4
5
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 3

def rules
  @rules
end

Instance Method Details

#create_rule(rule) ⇒ Object



31
32
33
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 31

def create_rule(rule)
  Rule.new(@sac.parse_rule(rule).first, @selector_index += 1)
end

#end_selector(selectors) ⇒ Object



18
19
20
21
22
23
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 18

def end_selector(selectors)
  @rules += @current_rules
  @current_rules = []
  @selector_index += 1
  reduce!
end

#find_rule(rule) ⇒ Object Also known as: []



25
26
27
28
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 25

def find_rule(rule)
  rule = self.create_rule(rule) if rule.is_a?(String)
  rules.find { |x| x.selector == rule.selector }
end

#property(name, value, important) ⇒ Object



35
36
37
38
39
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 35

def property(name, value, important)
  @current_rules.each { |selector|
    selector.properties << [name, value, important]
  }
end

#rules_by_propertyObject

Get a hash of rules by property



42
43
44
45
46
47
48
49
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 42

def rules_by_property
  rules_by_property = Hash.new { |h,k| h[k] = [] }
  @rules.each { |sel|
    props = sel.properties.to_a.sort_by { |x| x.hash } # HACK?
    rules_by_property[props] << sel
  }
  rules_by_property
end

#start_selector(selectors) ⇒ Object



12
13
14
15
16
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 12

def start_selector(selectors)
  selectors.each { |selector|
    @current_rules << Rule.new(selector, @selector_index)
  }
end

#to_cssObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/antisamy/csspool/rsac/stylesheet/stylesheet.rb', line 51

def to_css
  rules_by_property.map do |properties, rules|
    rules.map { |rule| rule.selector.to_css }.sort.join(', ') + " {\n" +
      properties.map { |key,value,important|
        # Super annoying.  If the property is font-family, its supposed to
        # be commas
        join_val = ('font-family' == key) ? ', ' : ' '
        values = [value].flatten.join(join_val)
        "#{key}:#{values}#{important ? ' !important' : ''};"
      }.join("\n") + "\n}"
  end.sort.join("\n")
end