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, #pre_match, #rewind, #scan_until, #seek

Class Method Details

.call(environment, input) ⇒ Object



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

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

.setup(env) ⇒ Object



5
6
# File 'lib/condenser/processors/css_media_combiner_processor.rb', line 5

def self.setup(env)
end

Instance Method Details

#call(environment, input) ⇒ Object



25
26
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
# File 'lib/condenser/processors/css_media_combiner_processor.rb', line 25

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

  input[:source] = ''
  while !eos?
    output = if @selectors.empty?
      input[:source]
    else
      (@selectors[0...-1].reduce(@media_queries) { |hash, selector| hash[selector] ||= {} }[@selectors.last] ||= '')
    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
        @selectors << matched.squish
        @stack << :media_query
      end
    end
  end

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

#reduce_media_query(queries) ⇒ Object



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

def reduce_media_query(queries)
  output = ''
  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