Class: CssTidy::StyleSheet

Inherits:
Object
  • Object
show all
Defined in:
lib/modules/css_stylesheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyleSheet

Returns a new instance of StyleSheet.



7
8
9
10
11
12
13
14
15
# File 'lib/modules/css_stylesheet.rb', line 7

def initialize
  # nodes can contain any kind of object:
  # * charset
  # * media (which can contain media or rulesets)
  # * rulesets
  @nodes = []
  @charset = ''
  @in_media = false
end

Instance Attribute Details

#in_mediaObject Also known as: end_at_block

Returns the value of attribute in_media.



4
5
6
# File 'lib/modules/css_stylesheet.rb', line 4

def in_media
  @in_media
end

Instance Method Details

#<<(object) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/modules/css_stylesheet.rb', line 17

def << (object)
  # if we are in media block, then add object to the last node
  if @in_media
    if object.class == CssTidy::MediaSet
      @nodes << object
    else
      @nodes.last << object
    end
  else
    @nodes << object
    if object.class == CssTidy::MediaSet
      @in_media = true
    end
  end
end

#charsetObject



54
55
56
# File 'lib/modules/css_stylesheet.rb', line 54

def charset
  @charset
end

#charset=(charset) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/modules/css_stylesheet.rb', line 45

def charset=(charset)
  if @charset.empty?
    @charset = charset.strip
    return true
  else
    return false
  end
end

#inspectObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/modules/css_stylesheet.rb', line 102

def inspect
  indent = '  '
  puts "Stylesheet"
  @nodes.each_with_index do |node, idx|
    case node.class.to_s
    when 'CssTidy::RuleSet'
      puts " + RuleSet"
    when 'CssTidy::Comment'
      puts " + Comment"
    end
    node.inspect(indent)
#       puts node.class
  end
end

#optimize(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/modules/css_stylesheet.rb', line 58

def optimize(options={})
  keep_next_comment = false

  @nodes.each_with_index do |node, idx|
    if node.class == CssTidy::Comment
      if node.is_special? && options[:keep_special_comments]
        next # do nothing
      elsif node.is_ie5_hack? && options[:keep_ie5_comment_hack] && ! options[:optimize_selectors]
        node.text = '\\'  # replace it
        keep_next_comment = true
      elsif keep_next_comment
        node.text = ''  # replace it
        keep_next_comment = false
      elsif ! options[:keep_comments]
        node.printable = false # don't print this one
      end
    end
    node.optimize(options)
  end

  if options[:optimize_selectors]
    nodes_to_remove = []
    length = @nodes.length
    @nodes.each_with_index do |node, index|
      if node.class == CssTidy::RuleSet
        idx = index
        # Check if properties also exist in another RuleSet
        while idx < length -1
          idx += 1 # start at the next one
          # just Rulsets
          if @nodes[idx].class == CssTidy::RuleSet
            if ! node.empty? && node == @nodes[idx]
              node += @nodes[idx]
              nodes_to_remove << idx
              @nodes[idx].clear
            end
           end
          end
      end
    end
  end

end

#to_s(format = :one_line) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/modules/css_stylesheet.rb', line 33

def to_s(format=:one_line)
  css = ''
  if ! @charset.empty?
    css << "@charset #{@charset};" + ((format == :multi_line) ? "\n" : '')
  end

  @nodes.each do |node|
    css << node.to_s(format) + ((format == :multi_line) ? "\n" : '')
  end
  css
end