Module: SC::Helpers::SplitCSS

Defined in:
lib/sproutcore/helpers/css_split.rb

Constant Summary collapse

UNTIL_SINGLE_QUOTE =
/(?!\\)'/
UNTIL_DOUBLE_QUOTE =
/(?!\\)"/
BEGIN_SCOPE =
/\{/
END_SCOPE =
/\}/
NORMAL_SCAN_UNTIL =
/[^{},]+/
MAX_SELECTOR_COUNT =
4000

Class Method Summary collapse

Class Method Details

.handle_comment(scanner) ⇒ Object



174
175
176
177
178
179
# File 'lib/sproutcore/helpers/css_split.rb', line 174

def self.handle_comment(scanner)
  str = '/*'
  
  scanner.pos += 2
  str << scanner.scan_until(/\*\//)
end

.handle_content(scanner) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sproutcore/helpers/css_split.rb', line 139

def self.handle_content(scanner)
  str = ""
  
  res = scanner.scan(NORMAL_SCAN_UNTIL)
  if res.nil?
    str << scanner.getch
  else
    str << res
  end
  
  str
end

.handle_scope(scanner) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sproutcore/helpers/css_split.rb', line 115

def self.handle_scope(scanner)
  str = ""
  
  # Consume the begin scope we matched
  str << scanner.scan(BEGIN_SCOPE)
  
  while not scanner.eos?
    str << handle_skip(scanner)
    
    if scanner.match? BEGIN_SCOPE
      str << self.handle_scope(scanner)
    end
    
    if scanner.match? END_SCOPE
      str << scanner.scan(END_SCOPE)
      return str
    end
    
    str << handle_content(scanner)
  end
  
  str
end

.handle_skip(scanner) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sproutcore/helpers/css_split.rb', line 152

def self.handle_skip(scanner)
  str = ""

  while true do
    if scanner.match?(/\s+/)
      str << scanner.scan(/\s+/)
      next
    end
    if scanner.match?(/\/\*/)
      str << handle_comment(scanner)
      next
    end
    if scanner.match?(/["']/)
      str << handle_string(scanner)
      next
    end
    break
  end
  
  str
end

.handle_string(scanner) ⇒ Object



181
182
183
184
185
186
# File 'lib/sproutcore/helpers/css_split.rb', line 181

def self.handle_string(scanner)
  str = scanner.getch
  str += scanner.scan_until(str == "'" ? UNTIL_SINGLE_QUOTE : UNTIL_DOUBLE_QUOTE)
  
  str
end

.split_css(input) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sproutcore/helpers/css_split.rb', line 49

def self.split_css(input)
  scanner = StringScanner.new(input)
  
  current = ""
  ret = [current]
  
  selectors = ""
  in_selector = false
  selector_count = 0
  
  while not scanner.eos?
    # Handle any strings, etc. This also handles whitespace, strings, blah.
    # Basically, unless we see a scope, we know we are in a selector.
    selectors << handle_skip(scanner)
    
    # Handle scope
    if scanner.match?(BEGIN_SCOPE)
      # Push the current selectors
      current << selectors
      selectors = ""
      in_selector = false
      
      current << handle_scope(scanner)
      
      # For readability and reliability in certain browsers, add a newline at the end
      # of each rule.
      current << "\n"
      next
    end
    
    # Handle , -- which would mean we are finishing a selector
    if scanner.match? /,/
      selectors << scanner.scan(/,/)
      in_selector = false
      
      next
    end
    
    if not in_selector
      # At this point we MUST be in a selector.
      in_selector = true
      selector_count += 1
      
      if selector_count > MAX_SELECTOR_COUNT
        current = ""
        ret << current
        
        selector_count = 0
      end
    end
    
    # skip over anything that our tokens do not start with. We implement this differently
    # since these are ONLY selectors, and therefore we must add them to selector instead of output.
    res = scanner.scan(NORMAL_SCAN_UNTIL)
    break if scanner.eos?
    
    if res.nil?
      selectors << scanner.getch
    else
      selectors << res
    end
  end
  
  ret
end