Class: Condenser::CSSMediaCombinerProcessor

Inherits:
Object
  • Object
show all
Includes:
ParseHelpers
Defined in:
lib/condenser/processors/css_media_combiner_processor.rb

Instance Attribute Summary

Attributes included from ParseHelpers

#matched

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseHelpers

#current_line, #cursor, #eos?, #forward, #gobble, #next_word, #peek, #pre_match, #rewind, #scan_until, #seek

Class Method Details

.call(environment, input) ⇒ Object



10
11
12
# File 'lib/condenser/processors/css_media_combiner_processor.rb', line 10

def self.call(environment, input)
  new.call(environment, input)
end

.setup(env) ⇒ Object



7
8
# File 'lib/condenser/processors/css_media_combiner_processor.rb', line 7

def self.setup(env)
end

Instance Method Details

#call(environment, input) ⇒ Object



27
28
29
30
31
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
80
81
# File 'lib/condenser/processors/css_media_combiner_processor.rb', line 27

def call(environment, input)
  seek(0)
  @sourcefile = input[:source_file]
  @source = input[:source]
  @stack =  []
  @selectors = []
  @media_queries = {}

  input[:source] = String.new
  while !eos?
    output = if @selectors.empty?
      input[:source]
    else
      (@selectors[0...-1].reduce(@media_queries) { |hash, selector| hash[selector] ||= {} }[@selectors.last] ||= String.new)
    end
    
    case @stack.last
    when :media_query
      scan_until(/(@media[^\{]*{|\{|\})/)
      case matched
      when '{'
        output << pre_match << matched
        @stack << :statement
      when '}'
        output << pre_match
        @stack.pop
        @selectors.pop
      else
        output << pre_match
        @selectors << matched.squish
        @stack << :media_query
      end
    when :statement
      scan_until(/(\{|\})/)
      output << pre_match << matched
      case matched
      when '{'
        @stack << :statement
      when '}'
        @stack.pop
      end
    else
      case scan_until(/(@media[^\{]*{|\z)/)
      when ''
        output << pre_match
      else
        output << pre_match.rstrip
        @selectors << matched.squish
        @stack << :media_query
      end
    end
  end

  input[:source] << reduce_media_query(@media_queries)
end

#reduce_media_query(queries) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/condenser/processors/css_media_combiner_processor.rb', line 14

def reduce_media_query(queries)
  output = String.new
  queries.each do |query, contents|
    output << query if query
    output << if contents.is_a?(Hash)
      reduce_media_query(contents)
    else
      contents + '}'
    end
  end
  output
end