Class: CssTidy::MediaSet

Inherits:
CssBase show all
Defined in:
lib/modules/css_media_set.rb

Overview

media sets live as nodes in a stylesheet when a set is added to a style sheet, the sheet adds all new nodes to the last mediaset

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CssBase

#inspect

Constructor Details

#initialize(at_media) ⇒ MediaSet

Returns a new instance of MediaSet.



10
11
12
13
14
15
16
17
# File 'lib/modules/css_media_set.rb', line 10

def initialize(at_media)
  # nodes can contain:
  # * ruleset
  # * comment
  @nodes = []
  @at_media = at_media
  self
end

Instance Attribute Details

#at_mediaObject

Returns the value of attribute at_media.



8
9
10
# File 'lib/modules/css_media_set.rb', line 8

def at_media
  @at_media
end

Instance Method Details

#<<(object) ⇒ Object



19
20
21
# File 'lib/modules/css_media_set.rb', line 19

def << (object)
  @nodes << object
end

#clearObject



81
82
83
84
# File 'lib/modules/css_media_set.rb', line 81

def clear
  @nodes = []
  @at_media = ''
end

#optimize(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/modules/css_media_set.rb', line 32

def optimize(options)
  return nil if @nodes.empty? || @at_media.empty?
  # clean up self first
    @at_media.gsub!(/\*\/\s+\/\*/, '*//*')

  # then do kids
  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]
        node.text = '\\'  # replace it
        keep_next_comment = true
      elsif keep_next_comment
        node.text = ''  # replace it
        keep_next_comment = false
      else
        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, indent = '') ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/modules/css_media_set.rb', line 23

def to_s(format=:one_line, indent='')
  css = "#{@at_media}{" + ((format == :multi_line) ? "\n" : '')
  @nodes.each do |node|
    css << indent + node.to_s(format) + ((format == :multi_line) ? "\n" : '')
  end
  css << '}' + ((format == :multi_line) ? "\n" : '')
  css
end