-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
css.rb
390 lines (353 loc) · 10.9 KB
/
css.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
require File.dirname(__FILE__) + '/../sass'
require 'sass/tree/node'
require 'sass/scss/css_parser'
module Sass
# This class converts CSS documents into Sass or SCSS templates.
# It works by parsing the CSS document into a {Sass::Tree} structure,
# and then applying various transformations to the structure
# to produce more concise and idiomatic Sass/SCSS.
#
# Example usage:
#
# Sass::CSS.new("p { color: blue }").render(:sass) #=> "p\n color: blue"
# Sass::CSS.new("p { color: blue }").render(:scss) #=> "p {\n color: blue; }"
class CSS
# @param template [String] The CSS stylesheet.
# This stylesheet can be encoded using any encoding
# that can be converted to Unicode.
# If the stylesheet contains an `@charset` declaration,
# that overrides the Ruby encoding
# (see {file:SASS_REFERENCE.md#encodings the encoding documentation})
# @option options :old [Boolean] (false)
# Whether or not to output old property syntax
# (`:color blue` as opposed to `color: blue`).
# This is only meaningful when generating Sass code,
# rather than SCSS.
def initialize(template, options = {})
if template.is_a? IO
template = template.read
end
@options = options.dup
# Backwards compatibility
@options[:old] = true if @options[:alternate] == false
@template = template
end
# Converts the CSS template into Sass or SCSS code.
#
# @param fmt [Symbol] `:sass` or `:scss`, designating the format to return.
# @return [String] The resulting Sass or SCSS code
# @raise [Sass::SyntaxError] if there's an error parsing the CSS template
def render(fmt = :sass)
check_encoding!
build_tree.send("to_#{fmt}", @options).strip + "\n"
rescue Sass::SyntaxError => err
err.modify_backtrace(:filename => @options[:filename] || '(css)')
raise err
end
# Returns the original encoding of the document,
# or `nil` under Ruby 1.8.
#
# @return [Encoding, nil]
# @raise [Encoding::UndefinedConversionError] if the source encoding
# cannot be converted to UTF-8
# @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
def source_encoding
check_encoding!
@original_encoding
end
private
def check_encoding!
return if @checked_encoding
@checked_encoding = true
@template, @original_encoding = Sass::Util.check_sass_encoding(@template) do |msg, line|
raise Sass::SyntaxError.new(msg, :line => line)
end
end
# Parses the CSS template and applies various transformations
#
# @return [Tree::Node] The root node of the parsed tree
def build_tree
root = Sass::SCSS::CssParser.new(@template, @options[:filename]).parse
parse_selectors root
expand_commas root
nest_seqs root
parent_ref_rules root
flatten_rules root
fold_commas root
dump_selectors root
root
end
# Parse all the selectors in the document and assign them to
# {Sass::Tree::RuleNode#parsed_rules}.
#
# @param root [Tree::Node] The parent node
def parse_selectors(root)
root.children.each do |child|
next parse_selectors(child) if child.is_a?(Tree::DirectiveNode)
next unless child.is_a?(Tree::RuleNode)
parser = Sass::SCSS::CssParser.new(child.rule.first, child.filename, child.line)
child.parsed_rules = parser.parse_selector
end
end
# Transform
#
# foo, bar, baz
# color: blue
#
# into
#
# foo
# color: blue
# bar
# color: blue
# baz
# color: blue
#
# @param root [Tree::Node] The parent node
def expand_commas(root)
root.children.map! do |child|
# child.parsed_rules.members.size > 1 iff the rule contains a comma
unless child.is_a?(Tree::RuleNode) && child.parsed_rules.members.size > 1
expand_commas(child) if child.is_a?(Tree::DirectiveNode)
next child
end
child.parsed_rules.members.map do |seq|
node = Tree::RuleNode.new([])
node.parsed_rules = make_cseq(seq)
node.children = child.children
node
end
end
root.children.flatten!
end
# Make rules use nesting so that
#
# foo
# color: green
# foo bar
# color: red
# foo baz
# color: blue
#
# becomes
#
# foo
# color: green
# bar
# color: red
# baz
# color: blue
#
# @param root [Tree::Node] The parent node
def nest_seqs(root)
current_rule = nil
root.children.map! do |child|
unless child.is_a?(Tree::RuleNode)
nest_seqs(child) if child.is_a?(Tree::DirectiveNode)
next child
end
seq = first_seq(child)
seq.members.reject! {|sseq| sseq == "\n"}
first, rest = seq.members.first, seq.members[1..-1]
if current_rule.nil? || first_sseq(current_rule) != first
current_rule = Tree::RuleNode.new([])
current_rule.parsed_rules = make_seq(first)
end
unless rest.empty?
child.parsed_rules = make_seq(*rest)
current_rule << child
else
current_rule.children += child.children
end
current_rule
end
root.children.compact!
root.children.uniq!
root.children.each {|v| nest_seqs(v)}
end
# Make rules use parent refs so that
#
# foo
# color: green
# foo.bar
# color: blue
#
# becomes
#
# foo
# color: green
# &.bar
# color: blue
#
# @param root [Tree::Node] The parent node
def parent_ref_rules(root)
current_rule = nil
root.children.map! do |child|
unless child.is_a?(Tree::RuleNode)
parent_ref_rules(child) if child.is_a?(Tree::DirectiveNode)
next child
end
sseq = first_sseq(child)
next child unless sseq.is_a?(Sass::Selector::SimpleSequence)
firsts, rest = [sseq.members.first], sseq.members[1..-1]
firsts.push rest.shift if firsts.first.is_a?(Sass::Selector::Parent)
if current_rule.nil? || first_sseq(current_rule).members != firsts
current_rule = Tree::RuleNode.new([])
current_rule.parsed_rules = make_sseq(*firsts)
end
unless rest.empty?
rest.unshift Sass::Selector::Parent.new
child.parsed_rules = make_sseq(*rest)
current_rule << child
else
current_rule.children += child.children
end
current_rule
end
root.children.compact!
root.children.uniq!
root.children.each {|v| parent_ref_rules(v)}
end
# Flatten rules so that
#
# foo
# bar
# color: red
#
# becomes
#
# foo bar
# color: red
#
# and
#
# foo
# &.bar
# color: blue
#
# becomes
#
# foo.bar
# color: blue
#
# @param root [Tree::Node] The parent node
def flatten_rules(root)
root.children.each do |child|
case child
when Tree::RuleNode
flatten_rule(child)
when Tree::DirectiveNode
flatten_rules(child)
end
end
end
# Flattens a single rule.
#
# @param rule [Tree::RuleNode] The candidate for flattening
# @see #flatten_rules
def flatten_rule(rule)
while rule.children.size == 1 && rule.children.first.is_a?(Tree::RuleNode)
child = rule.children.first
if first_simple_sel(child).is_a?(Sass::Selector::Parent)
rule.parsed_rules = child.parsed_rules.resolve_parent_refs(rule.parsed_rules)
else
rule.parsed_rules = make_seq(first_sseq(rule), *first_seq(child).members)
end
rule.children = child.children
end
flatten_rules(rule)
end
# Transform
#
# foo
# bar
# color: blue
# baz
# color: blue
#
# into
#
# foo
# bar, baz
# color: blue
#
# @param rule [Tree::RuleNode] The candidate for flattening
def fold_commas(root)
prev_rule = nil
root.children.map! do |child|
unless child.is_a?(Tree::RuleNode)
fold_commas(child) if child.is_a?(Tree::DirectiveNode)
next child
end
if prev_rule && prev_rule.children == child.children
prev_rule.parsed_rules.members << first_seq(child)
next nil
end
fold_commas(child)
prev_rule = child
child
end
root.children.compact!
end
# Dump all the parsed {Sass::Tree::RuleNode} selectors to strings.
#
# @param root [Tree::Node] The parent node
def dump_selectors(root)
root.children.each do |child|
next dump_selectors(child) if child.is_a?(Tree::DirectiveNode)
next unless child.is_a?(Tree::RuleNode)
child.rule = child.parsed_rules.to_s
dump_selectors(child)
end
end
# Create a {Sass::Selector::CommaSequence}.
#
# @param seqs [Array<Sass::Selector::Sequence>]
# @return [Sass::Selector::CommaSequence]
def make_cseq(*seqs)
Sass::Selector::CommaSequence.new(seqs)
end
# Create a {Sass::Selector::CommaSequence} containing only a single
# {Sass::Selector::Sequence}.
#
# @param sseqs [Array<Sass::Selector::Sequence, String>]
# @return [Sass::Selector::CommaSequence]
def make_seq(*sseqs)
make_cseq(Sass::Selector::Sequence.new(sseqs))
end
# Create a {Sass::Selector::CommaSequence} containing only a single
# {Sass::Selector::Sequence} which in turn contains only a single
# {Sass::Selector::SimpleSequence}.
#
# @param sseqs [Array<Sass::Selector::Sequence, String>]
# @return [Sass::Selector::CommaSequence]
def make_sseq(*sseqs)
make_seq(Sass::Selector::SimpleSequence.new(sseqs))
end
# Return the first {Sass::Selector::Sequence} in a {Sass::Tree::RuleNode}.
#
# @param rule [Sass::Tree::RuleNode]
# @return [Sass::Selector::Sequence]
def first_seq(rule)
rule.parsed_rules.members.first
end
# Return the first {Sass::Selector::SimpleSequence} in a
# {Sass::Tree::RuleNode}.
#
# @param rule [Sass::Tree::RuleNode]
# @return [Sass::Selector::SimpleSequence, String]
def first_sseq(rule)
first_seq(rule).members.first
end
# Return the first {Sass::Selector::Simple} in a {Sass::Tree::RuleNode},
# unless the rule begins with a combinator.
#
# @param rule [Sass::Tree::RuleNode]
# @return [Sass::Selector::Simple?]
def first_simple_sel(rule)
sseq = first_sseq(rule)
return unless sseq.is_a?(Sass::Selector::SimpleSequence)
sseq.members.first
end
end
end