remi-css3-now 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/css3-now.rb +16 -8
  3. data/spec/css3-now_spec.rb +28 -0
  4. metadata +1 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -1,34 +1,42 @@
1
1
  # ...
2
2
  class CSS3Now
3
3
 
4
- Substitutions = [
4
+ class << self
5
+ attr_accessor :substitutions
6
+ end
7
+
8
+ @substitutions = [
5
9
  lambda {|css| replace_browser_specific css, 'border-radius' },
6
10
  lambda {|css| replace_browser_specific css, 'box-shadow' },
7
11
  lambda {|css| css.gsub(/opacity: ([\w\.]+);/){ "filter:alpha(opacity=#{ ($1.to_f * 100).to_i }); opacity: #{ $1 };" } }
8
12
  ]
9
13
 
14
+ def self.substitution &block
15
+ @substitutions << block
16
+ end
17
+
10
18
  def initialize original_css
11
19
  @original_css = original_css
12
20
  end
13
21
 
14
22
  def to_css
15
- Substitutions.inject(@original_css) do |css, substitution|
23
+ CSS3Now.substitutions.inject(@original_css) do |css, substitution|
16
24
  css = substitution.call css
17
25
  end
18
26
  end
19
27
 
28
+ def self.replace_browser_specific css, attribute, browsers = nil
29
+ css.gsub /#{ attribute }:([^;]+);/, browser_specific(attribute, '\1', browsers)
30
+ end
31
+
20
32
  private
21
33
 
22
34
  def self.browser_specific attribute, value, browsers = nil
23
35
  browsers = [ :moz, :webkit, :khtml ] if browsers.nil?
24
36
  browsers.inject("") do |all, browser|
25
- all << "-#{ browser }-#{ attribute }: #{ value }; "
37
+ all << "-#{ browser }-#{ attribute }:#{ value }; "
26
38
  all
27
- end + "#{ attribute }: #{ value };"
28
- end
29
-
30
- def self.replace_browser_specific css, attribute, browsers = nil
31
- css.gsub /#{ attribute }: ([-\.#\w ]+);/, browser_specific(attribute, '\1', browsers)
39
+ end + "#{ attribute }:#{ value };"
32
40
  end
33
41
 
34
42
  end
@@ -20,6 +20,12 @@ describe CSS3Now do
20
20
  "-khtml-border-radius: 5em 10px -3.4em #fff; border-radius: 5em 10px -3.4em #fff;\n }"
21
21
  end
22
22
 
23
+ it 'should grab anything before the semicolon as a value' do
24
+ css = CSS3Now.new("#header {\n border-radius:( !imporant *Awesomeness* !@\#$% #123 );\n }").to_css
25
+ css.should == "#header {\n -moz-border-radius:( !imporant *Awesomeness* !@\#$% #123 ); -webkit-border-radius:( !imporant *Awesomeness* !@\#$% #123 ); " +
26
+ "-khtml-border-radius:( !imporant *Awesomeness* !@\#$% #123 ); border-radius:( !imporant *Awesomeness* !@\#$% #123 );\n }"
27
+ end
28
+
23
29
  it 'should work with multiple instances of an attibute' do
24
30
  CSS3Now.new("#x {\n opacity: 0.5;\n } #y {\n opacity: 0.4;\n }").to_css.should ==
25
31
  "#x {\n filter:alpha(opacity=50); opacity: 0.5;\n }" +
@@ -42,4 +48,26 @@ describe CSS3Now do
42
48
  CSS3Now.new("#x {\n opacity: 0.5;\n }").to_css.should == "#x {\n filter:alpha(opacity=50); opacity: 0.5;\n }"
43
49
  end
44
50
 
51
+ it 'should be able to add your own substitutions' do
52
+ CSS3Now.new("#x { color: white; }").to_css.should == "#x { color: white; }"
53
+
54
+ lambda {
55
+ CSS3Now.substitution do |css|
56
+ CSS3Now.replace_browser_specific css, :color, [ :moz ]
57
+ end
58
+ }.should change(CSS3Now.substitutions, :count)
59
+
60
+ CSS3Now.new("#x { color: white; }").to_css.should == "#x { -moz-color: white; color: white; }"
61
+ end
62
+
63
+ it 'should be able to add your own substitutions (2)' do
64
+ CSS3Now.new("#x { foo: white; }").to_css.should == "#x { foo: white; }"
65
+
66
+ CSS3Now.substitution do |css|
67
+ css.gsub 'white', 'black'
68
+ end
69
+
70
+ CSS3Now.new("#x { foo: white; }").to_css.should == "#x { foo: black; }"
71
+ end
72
+
45
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remi-css3-now
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - remi