css_parser 1.4.7 → 1.4.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dececf056d9418c517224b0041be332f1a0e132
4
- data.tar.gz: 06a21e39d76c9979887b35fa2fbeb834f035e8a0
3
+ metadata.gz: f5f0a57b79a2c3074f6e8a4f83f9c33c86af5f8d
4
+ data.tar.gz: 70c358a8a37da0b69c7a752fdf845f5b57decaa4
5
5
  SHA512:
6
- metadata.gz: 21810de13f346dae79a69933b91a907e8430fca03ff64d682120912aa81d65357a42e1e8ef47c4e62a0ce8738a27c2fd4828e344b801fceb9d8d1a35498662e8
7
- data.tar.gz: 677aa6fa9fe06183b3fd97aab252f130cbc96056820a274500a54f98677647a54d80054f3cef320551f275fc0362c2666b0876d0506644c1f8abb7543c998856
6
+ metadata.gz: c3cb1b5bff4cf81a54a3f2ed091b8119c6c47ec3e3278e110ac2ead3da8685d7504f2affeb62d8956128c165fe1273b3c89ed25c23e18722215f0e66293d9b5a
7
+ data.tar.gz: 179f6d26055efe2340cbebc9bb551000cbe466dadae3c16ceb00708b8f3f7283f364d89d164e3509ae70d65c3c4e17d431a95627acc250411d5ab89685f44f95
@@ -187,6 +187,27 @@ module CssParser
187
187
  end
188
188
  end
189
189
 
190
+ # Output all CSS rules as a Hash
191
+ def to_h(media_types = :all)
192
+ out = {}
193
+ styles_by_media_types = {}
194
+ each_selector(media_types) do |selectors, declarations, specificity, media_types|
195
+ media_types.each do |media_type|
196
+ styles_by_media_types[media_type] ||= []
197
+ styles_by_media_types[media_type] << [selectors, declarations]
198
+ end
199
+ end
200
+
201
+ styles_by_media_types.each_pair do |media_type, media_styles|
202
+ ms = {}
203
+ media_styles.each do |media_style|
204
+ ms = css_node_to_h(ms, media_style[0], media_style[1])
205
+ end
206
+ out[media_type.to_s] = ms
207
+ end
208
+ out
209
+ end
210
+
190
211
  # Iterate through CSS selectors.
191
212
  #
192
213
  # +media_types+ can be a symbol or an array of symbols.
@@ -424,16 +445,17 @@ module CssParser
424
445
  # Returns a string.
425
446
  def cleanup_block(block) # :nodoc:
426
447
  # Strip CSS comments
427
- block.gsub!(STRIP_CSS_COMMENTS_RX, '')
448
+ utf8_block = block.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
449
+ utf8_block.gsub!(STRIP_CSS_COMMENTS_RX, '')
428
450
 
429
451
  # Strip HTML comments - they shouldn't really be in here but
430
452
  # some people are just crazy...
431
- block.gsub!(STRIP_HTML_COMMENTS_RX, '')
453
+ utf8_block.gsub!(STRIP_HTML_COMMENTS_RX, '')
432
454
 
433
455
  # Strip lines containing just whitespace
434
- block.gsub!(/^\s+$/, "")
456
+ utf8_block.gsub!(/^\s+$/, "")
435
457
 
436
- block
458
+ utf8_block
437
459
  end
438
460
 
439
461
  # Download a file into a string.
@@ -539,5 +561,23 @@ module CssParser
539
561
  @css_rules = []
540
562
  @css_warnings = []
541
563
  end
564
+
565
+ # recurse through nested nodes and return them as Hashes nested in
566
+ # passed hash
567
+ def css_node_to_h(hash, key, val)
568
+ hash[key.strip] = '' and return hash if val.nil?
569
+ lines = val.split(';')
570
+ nodes = {}
571
+ lines.each do |line|
572
+ parts = line.split(':', 2)
573
+ if (parts[1] =~ /:/)
574
+ nodes[parts[0]] = css_node_to_h(hash, parts[0], parts[1])
575
+ else
576
+ nodes[parts[0].to_s.strip] =parts[1].to_s.strip
577
+ end
578
+ end
579
+ hash[key.strip] = nodes
580
+ hash
581
+ end
542
582
  end
543
583
  end
@@ -1,3 +1,3 @@
1
1
  module CssParser
2
- VERSION = "1.4.7".freeze
2
+ VERSION = "1.4.8".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: css_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.7
4
+ version: 1.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dunae
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-29 00:00:00.000000000 Z
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project:
59
- rubygems_version: 2.4.5.1
59
+ rubygems_version: 2.6.8
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Ruby CSS parser.