Module: FrontCompiler::CssSource::NestedStyles

Included in:
FrontCompiler::CssSource
Defined in:
lib/front_compiler/css_source/nested_styles.rb

Constant Summary collapse

CSS_CONSTRUCTION_RE =

converts the nested styles constructions in the source

/((\A|\}|;)\s*?([^\}\{;]+?)\s*?)\{.*?\}/m

Instance Method Summary collapse

Instance Method Details

#convert_nested_styles!Object



18
19
20
21
22
23
24
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
# File 'lib/front_compiler/css_source/nested_styles.rb', line 18

def convert_nested_styles!
  # loop though the blocks
  offset = 0
  while pos = index(CSS_CONSTRUCTION_RE, offset)
    pos += $1.size # <- getting the actual block position
    block = find_block("{}", pos)
    block = block[1, block.size-1]; pos+=1 # <- remove the container
    block_size = block.size
    
    parent_rules = clean_rules_from $3 # <- the block rules list

    block_sub_styles = []
    
    # looking for a nested construction
    while block_pos = block.index(CSS_CONSTRUCTION_RE)
      trail_char = $2.dup
      
      block_start = $1.dup
      block_start = block_start[trail_char.size, block_start.size]
      block_start_size = block_start.size
      
      block_pos += trail_char.size # <- updating the sub-block position
      
      # update the sub-bolock rules
      clean_rules_from($3).each do |block_rule|
        block_start.gsub! block_rule, parent_rules.collect{ |p_rule|
          "#{p_rule} #{block_rule}"
        }.join(", ")
      end
      
      # getting the construction body
      block_body = find_block("{}", block_pos + block_start_size, block)
      
      # removing the construction out of the block
      block[block_pos, block_start_size + block_body.size] = ''
      block_sub_styles << block_start + block_body
    end
    
    # replacing the block
    self[pos, block_size] = block + block_sub_styles.join('')
    
    offset = pos + block.size - 1
  end
end

#initialize(src, keep_as_is = false) ⇒ Object

overloads the constructor, to convert the styles on fly



11
12
13
14
# File 'lib/front_compiler/css_source/nested_styles.rb', line 11

def initialize(src, keep_as_is=false)
  super src
  convert_nested_styles! unless keep_as_is
end