sprockets-media_query_combiner 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,8 @@ if defined?(Rails)
4
4
  module Sprockets
5
5
  module MediaQueryCombiner
6
6
  class Engine < Rails::Engine
7
- initializer :setup_media_query_combiner, after: 'sprockets.environment', group: :assets do |app|
7
+ initializer :setup_media_query_combiner, after: 'sprockets.environment', group: :all do |app|
8
+ app.assets.register_postprocessor 'text/css', Sprockets::MediaQueryCombiner::Processor
8
9
  app.assets.register_bundle_processor 'text/css', Sprockets::MediaQueryCombiner::Processor
9
10
  end
10
11
  end
@@ -8,11 +8,17 @@ module Sprockets
8
8
 
9
9
  def evaluate(context, locals, &block)
10
10
  queries = Hash.new { |hash, key| hash[key] = '' }
11
- filtered_data = data.gsub(/@media(?<query>[^{]+){(?<body>(?<braces>(?:[^{}]+)|({\g<braces>}))*)}\n?/m) do |match|
12
- queries[$1.strip] << $2.strip; ''
11
+ pretty = true
12
+
13
+ filtered_data = data.gsub(/(?<query>\n?@media[^{]+){(?<body>(?<braces>(?:[^{}]+)|({\g<braces>}))*)}\n?/m) do |match|
14
+ queries[$1] << $2
15
+ pretty &&= /\n$/m === match
16
+ ''
13
17
  end
14
18
 
15
- "#{filtered_data}#{queries.map { |query, body| "@media #{query}{#{body}}" }.join}\n"
19
+ combined = queries.map { |query, body| "#{query}{#{body}}" }.
20
+ join(("\n" if pretty))
21
+ "#{filtered_data}#{combined}\n"
16
22
  end
17
23
  end
18
24
  end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module MediaQueryCombiner
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -3,42 +3,69 @@ require 'sprockets/media_query_combiner/processor'
3
3
 
4
4
  module Sprockets::MediaQueryCombiner
5
5
  describe Processor do
6
- it "should work" do
6
+ it "should with pretty css" do
7
7
  combiner = Processor.new do
8
8
  <<CSS
9
9
  h3 {
10
10
  color: orange
11
11
  }
12
+
12
13
  @media (max-width: 480px) {
13
14
  h1 {
14
15
  color: red
15
16
  }
16
17
  }
18
+
17
19
  @media (max-width: 980px) {
18
20
  h4 {
19
21
  color: black
20
22
  }
21
23
  }
24
+
22
25
  @media (max-width: 480px) {
23
26
  h2 {
24
27
  color: blue
25
28
  }
26
29
  }
30
+
31
+ b {
32
+ color: yellow
33
+ }
27
34
  CSS
28
35
  end
29
36
  combiner.evaluate(nil, nil).should == <<CSS
30
37
  h3 {
31
38
  color: orange
32
39
  }
33
- @media (max-width: 480px){h1 {
40
+
41
+ b {
42
+ color: yellow
43
+ }
44
+
45
+ @media (max-width: 480px) {
46
+ h1 {
34
47
  color: red
35
- }h2 {
48
+ }
49
+
50
+ h2 {
36
51
  color: blue
37
- }}@media (max-width: 980px){h4 {
52
+ }
53
+ }
54
+
55
+ @media (max-width: 980px) {
56
+ h4 {
38
57
  color: black
39
- }}
58
+ }
59
+ }
40
60
  CSS
41
61
  end
42
62
 
63
+ it "should work with ugly css" do
64
+ combiner = Processor.new do
65
+ "h3{color:orange}@media (max-width:480px){h1{color:red}}@media (max-width:980px){h4{color:black}}@media (max-width:480px){h2{color:blue}}\n"
66
+ end
67
+ combiner.evaluate(nil, nil).should ==
68
+ "h3{color:orange}@media (max-width:480px){h1{color:red}h2{color:blue}}@media (max-width:980px){h4{color:black}}\n"
69
+ end
43
70
  end
44
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-media_query_combiner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: