css_parser 1.3.4 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/css_parser.rb +0 -1
- data/lib/css_parser/parser.rb +57 -37
- data/lib/css_parser/rule_set.rb +3 -2
- data/lib/css_parser/version.rb +1 -1
- metadata +4 -4
data/lib/css_parser.rb
CHANGED
data/lib/css_parser/parser.rb
CHANGED
@@ -24,7 +24,7 @@ module CssParser
|
|
24
24
|
|
25
25
|
# Array of CSS files that have been loaded.
|
26
26
|
attr_reader :loaded_uris
|
27
|
-
|
27
|
+
|
28
28
|
#--
|
29
29
|
# Class variable? see http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_variab_1.html
|
30
30
|
#++
|
@@ -38,10 +38,10 @@ module CssParser
|
|
38
38
|
|
39
39
|
# array of RuleSets
|
40
40
|
@rules = []
|
41
|
-
|
42
|
-
|
41
|
+
|
42
|
+
|
43
43
|
@loaded_uris = []
|
44
|
-
|
44
|
+
|
45
45
|
# unprocessed blocks of CSS
|
46
46
|
@blocks = []
|
47
47
|
reset!
|
@@ -77,7 +77,7 @@ module CssParser
|
|
77
77
|
rule_sets = []
|
78
78
|
|
79
79
|
selectors.each do |selector|
|
80
|
-
each_rule_set(media_types) do |rule_set|
|
80
|
+
each_rule_set(media_types) do |rule_set, media_type|
|
81
81
|
if !rule_sets.member?(rule_set) && rule_set.selectors.member?(selector)
|
82
82
|
rule_sets << rule_set
|
83
83
|
end
|
@@ -117,7 +117,7 @@ module CssParser
|
|
117
117
|
if options[:base_uri] and @options[:absolute_paths]
|
118
118
|
block = CssParser.convert_uris(block, options[:base_uri])
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
# Load @imported CSS
|
122
122
|
block.scan(RE_AT_IMPORT_RULE).each do |import_rule|
|
123
123
|
media_types = []
|
@@ -128,22 +128,22 @@ module CssParser
|
|
128
128
|
else
|
129
129
|
media_types = [:all]
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
next unless options[:only_media_types].include?(:all) or media_types.length < 1 or (media_types & options[:only_media_types]).length > 0
|
133
133
|
|
134
134
|
import_path = import_rule[0].to_s.gsub(/['"]*/, '').strip
|
135
|
-
|
135
|
+
|
136
136
|
if options[:base_uri]
|
137
137
|
import_uri = Addressable::URI.parse(options[:base_uri].to_s) + Addressable::URI.parse(import_path)
|
138
138
|
load_uri!(import_uri, options[:base_uri], media_types)
|
139
139
|
elsif options[:base_dir]
|
140
140
|
load_file!(import_path, options[:base_dir], media_types)
|
141
|
-
end
|
141
|
+
end
|
142
142
|
end
|
143
143
|
|
144
144
|
# Remove @import declarations
|
145
145
|
block.gsub!(RE_AT_IMPORT_RULE, '')
|
146
|
-
|
146
|
+
|
147
147
|
parse_block_into_rule_sets!(block, options)
|
148
148
|
end
|
149
149
|
|
@@ -169,13 +169,13 @@ module CssParser
|
|
169
169
|
# Iterate through RuleSet objects.
|
170
170
|
#
|
171
171
|
# +media_types+ can be a symbol or an array of symbols.
|
172
|
-
def each_rule_set(media_types = :all) # :yields: rule_set
|
172
|
+
def each_rule_set(media_types = :all) # :yields: rule_set, media_types
|
173
173
|
media_types = [:all] if media_types.nil?
|
174
174
|
media_types = [media_types].flatten.collect { |mt| CssParser.sanitize_media_query(mt)}
|
175
175
|
|
176
176
|
@rules.each do |block|
|
177
177
|
if media_types.include?(:all) or block[:media_types].any? { |mt| media_types.include?(mt) }
|
178
|
-
yield
|
178
|
+
yield(block[:rules], block[:media_types])
|
179
179
|
end
|
180
180
|
end
|
181
181
|
end
|
@@ -184,10 +184,10 @@ module CssParser
|
|
184
184
|
#
|
185
185
|
# +media_types+ can be a symbol or an array of symbols.
|
186
186
|
# See RuleSet#each_selector for +options+.
|
187
|
-
def each_selector(media_types = :all, options = {}) # :yields: selectors, declarations, specificity
|
188
|
-
each_rule_set(media_types) do |rule_set|
|
187
|
+
def each_selector(media_types = :all, options = {}) # :yields: selectors, declarations, specificity, media_types
|
188
|
+
each_rule_set(media_types) do |rule_set, media_types|
|
189
189
|
rule_set.each_selector(options) do |selectors, declarations, specificity|
|
190
|
-
yield selectors, declarations, specificity
|
190
|
+
yield selectors, declarations, specificity, media_types
|
191
191
|
end
|
192
192
|
end
|
193
193
|
end
|
@@ -195,12 +195,32 @@ module CssParser
|
|
195
195
|
# Output all CSS rules as a single stylesheet.
|
196
196
|
def to_s(media_types = :all)
|
197
197
|
out = ''
|
198
|
-
|
199
|
-
|
198
|
+
styles_by_media_types = {}
|
199
|
+
each_selector(media_types) do |selectors, declarations, specificity, media_types|
|
200
|
+
media_types.each do |media_type|
|
201
|
+
styles_by_media_types[media_type] ||= []
|
202
|
+
styles_by_media_types[media_type] << [selectors, declarations]
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
styles_by_media_types.each_pair do |media_type, media_styles|
|
207
|
+
media_block = (media_type != :all)
|
208
|
+
out += "@media #{media_type} {\n" if media_block
|
209
|
+
|
210
|
+
media_styles.each do |media_style|
|
211
|
+
if media_block
|
212
|
+
out += " #{media_style[0]} {\n #{media_style[1]}\n }\n"
|
213
|
+
else
|
214
|
+
out += "#{media_style[0]} {\n#{media_style[1]}\n}\n"
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
out += "}\n" if media_block
|
200
219
|
end
|
220
|
+
|
201
221
|
out
|
202
222
|
end
|
203
|
-
|
223
|
+
|
204
224
|
# A hash of { :media_query => rule_sets }
|
205
225
|
def rules_by_media_query
|
206
226
|
rules_by_media = {}
|
@@ -212,7 +232,7 @@ module CssParser
|
|
212
232
|
rules_by_media[mt] << block[:rules]
|
213
233
|
end
|
214
234
|
end
|
215
|
-
|
235
|
+
|
216
236
|
rules_by_media
|
217
237
|
end
|
218
238
|
|
@@ -246,7 +266,7 @@ module CssParser
|
|
246
266
|
|
247
267
|
if token =~ /\A"/ # found un-escaped double quote
|
248
268
|
in_string = !in_string
|
249
|
-
end
|
269
|
+
end
|
250
270
|
|
251
271
|
if in_declarations > 0
|
252
272
|
# too deep, malformed declaration block
|
@@ -254,17 +274,17 @@ module CssParser
|
|
254
274
|
in_declarations -= 1 if token =~ /\}/
|
255
275
|
next
|
256
276
|
end
|
257
|
-
|
277
|
+
|
258
278
|
if token =~ /\{/
|
259
279
|
in_declarations += 1
|
260
280
|
next
|
261
281
|
end
|
262
|
-
|
282
|
+
|
263
283
|
current_declarations += token
|
264
284
|
|
265
285
|
if token =~ /\}/ and not in_string
|
266
286
|
current_declarations.gsub!(/\}[\s]*$/, '')
|
267
|
-
|
287
|
+
|
268
288
|
in_declarations -= 1
|
269
289
|
|
270
290
|
unless current_declarations.strip.empty?
|
@@ -318,7 +338,7 @@ module CssParser
|
|
318
338
|
end
|
319
339
|
end
|
320
340
|
|
321
|
-
# check for unclosed braces
|
341
|
+
# check for unclosed braces
|
322
342
|
if in_declarations > 0
|
323
343
|
add_rule!(current_selectors, current_declarations, current_media_queries)
|
324
344
|
end
|
@@ -343,7 +363,7 @@ module CssParser
|
|
343
363
|
opts[:base_uri] = options if options.is_a? String
|
344
364
|
opts[:media_types] = deprecated if deprecated
|
345
365
|
end
|
346
|
-
|
366
|
+
|
347
367
|
if uri.scheme == 'file' or uri.scheme.nil?
|
348
368
|
uri.path = File.expand_path(uri.path)
|
349
369
|
uri.scheme = 'file'
|
@@ -356,7 +376,7 @@ module CssParser
|
|
356
376
|
add_block!(src, opts)
|
357
377
|
end
|
358
378
|
end
|
359
|
-
|
379
|
+
|
360
380
|
# Load a local CSS file.
|
361
381
|
def load_file!(file_name, base_dir = nil, media_types = :all)
|
362
382
|
file_name = File.expand_path(file_name, base_dir)
|
@@ -368,18 +388,18 @@ module CssParser
|
|
368
388
|
|
369
389
|
add_block!(src, {:media_types => media_types, :base_dir => base_dir})
|
370
390
|
end
|
371
|
-
|
391
|
+
|
372
392
|
# Load a local CSS string.
|
373
393
|
def load_string!(src, base_dir = nil, media_types = :all)
|
374
394
|
add_block!(src, {:media_types => media_types, :base_dir => base_dir})
|
375
395
|
end
|
376
|
-
|
377
|
-
|
396
|
+
|
397
|
+
|
378
398
|
|
379
399
|
protected
|
380
400
|
# Check that a path hasn't been loaded already
|
381
401
|
#
|
382
|
-
# Raises a CircularReferenceError exception if io_exceptions are on,
|
402
|
+
# Raises a CircularReferenceError exception if io_exceptions are on,
|
383
403
|
# otherwise returns true/false.
|
384
404
|
def circular_reference_check(path)
|
385
405
|
path = path.to_s
|
@@ -391,7 +411,7 @@ module CssParser
|
|
391
411
|
return true
|
392
412
|
end
|
393
413
|
end
|
394
|
-
|
414
|
+
|
395
415
|
# Strip comments and clean up blank lines from a block of CSS.
|
396
416
|
#
|
397
417
|
# Returns a string.
|
@@ -399,7 +419,7 @@ module CssParser
|
|
399
419
|
# Strip CSS comments
|
400
420
|
block.gsub!(STRIP_CSS_COMMENTS_RX, '')
|
401
421
|
|
402
|
-
# Strip HTML comments - they shouldn't really be in here but
|
422
|
+
# Strip HTML comments - they shouldn't really be in here but
|
403
423
|
# some people are just crazy...
|
404
424
|
block.gsub!(STRIP_HTML_COMMENTS_RX, '')
|
405
425
|
|
@@ -416,12 +436,12 @@ module CssParser
|
|
416
436
|
# TODO: add option to fail silently or throw and exception on a 404
|
417
437
|
#++
|
418
438
|
def read_remote_file(uri) # :nodoc:
|
419
|
-
return nil, nil unless circular_reference_check(uri.to_s)
|
439
|
+
return nil, nil unless circular_reference_check(uri.to_s)
|
420
440
|
|
421
441
|
src = '', charset = nil
|
422
442
|
|
423
443
|
begin
|
424
|
-
uri = Addressable::URI.parse(uri.to_s)
|
444
|
+
uri = Addressable::URI.parse(uri.to_s)
|
425
445
|
|
426
446
|
if uri.scheme == 'file'
|
427
447
|
# local file
|
@@ -433,13 +453,13 @@ module CssParser
|
|
433
453
|
if uri.scheme == 'https'
|
434
454
|
uri.port = 443 unless uri.port
|
435
455
|
http = Net::HTTP.new(uri.host, uri.port)
|
436
|
-
http.use_ssl = true
|
456
|
+
http.use_ssl = true
|
437
457
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
438
458
|
else
|
439
459
|
http = Net::HTTP.new(uri.host, uri.port)
|
440
460
|
end
|
441
461
|
|
442
|
-
res = http.get(uri.
|
462
|
+
res = http.get(uri.request_uri, {'User-Agent' => USER_AGENT, 'Accept-Encoding' => 'gzip'})
|
443
463
|
src = res.body
|
444
464
|
charset = fh.respond_to?(:charset) ? fh.charset : 'utf-8'
|
445
465
|
|
@@ -471,7 +491,7 @@ module CssParser
|
|
471
491
|
return nil, nil
|
472
492
|
end
|
473
493
|
|
474
|
-
return src, charset
|
494
|
+
return src, charset
|
475
495
|
end
|
476
496
|
|
477
497
|
private
|
data/lib/css_parser/rule_set.rb
CHANGED
@@ -366,8 +366,9 @@ module CssParser
|
|
366
366
|
'border-style' => 'border-%s-style',
|
367
367
|
'border-width' => 'border-%s-width'}.each do |property, expanded|
|
368
368
|
|
369
|
-
|
370
|
-
|
369
|
+
top, right, bottom, left = ['top', 'right', 'bottom', 'left'].map { |side| expanded % side }
|
370
|
+
foldable = @declarations.select do |dim, val|
|
371
|
+
dim == top or dim == right or dim == bottom or dim == left
|
371
372
|
end
|
372
373
|
# All four dimensions must be present
|
373
374
|
if foldable.length == 4
|
data/lib/css_parser/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: css_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-08-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: addressable
|
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
53
|
version: '0'
|
54
54
|
segments:
|
55
55
|
- 0
|
56
|
-
hash:
|
56
|
+
hash: 4192577183181421877
|
57
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
58
|
none: false
|
59
59
|
requirements:
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
version: '0'
|
63
63
|
segments:
|
64
64
|
- 0
|
65
|
-
hash:
|
65
|
+
hash: 4192577183181421877
|
66
66
|
requirements: []
|
67
67
|
rubyforge_project:
|
68
68
|
rubygems_version: 1.8.25
|