r2 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11915f30f055af7962245e50b8bb3acd299d72e8
4
+ data.tar.gz: a536b287f02596d20774cce78c37e81a24eb32fd
5
+ SHA512:
6
+ metadata.gz: 6915646f994723a8ea9755e19ea764d00afe85c6f50f8ec356c373778729b08ccf07c0cf3ef1b4342d161902e3bce3365c1e2639fad435d4b97f8d96ab125666
7
+ data.tar.gz: 36b1614dd763a3c4f5534858f549c97e2fd06a467089437dcf082dfbc1ec0a748bf6121826bbe30e1f382f5ede46b6e331d7630c33dc8a5df6a92bb932e4bcb8
@@ -0,0 +1,9 @@
1
+
2
+ language: ruby
3
+ rvm:
4
+ - 1.9.3
5
+ - jruby-18mode # JRuby in 1.8 mode
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ - 1.8.7
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in r2.gemspec
4
4
  gemspec
5
+
6
+ gem 'guard-rspec'
7
+ gem 'rb-fsevent', '~> 0.9'
@@ -0,0 +1,6 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
data/README.md CHANGED
@@ -13,13 +13,20 @@ You can use the handy static method for flipping any CSS string via:
13
13
  > R2.r2("/* Comment */\nbody { direction: rtl; }")
14
14
  #=> "body{direction:ltr;}"
15
15
 
16
+ ## Build Status
17
+
18
+ [![Build Status](https://travis-ci.org/mzsanford/R2rb.png)](https://travis-ci.org/mzsanford/R2rb)
19
+
16
20
  ## Reporting bugs
17
21
 
18
22
  Report bugs in the github project at http://github.com/mzsanford/r2rb
19
23
 
20
24
  ## Change Log
21
25
 
22
- * v0.1.0 – @fractious[https://github.com/fractious] updates
26
+ * v0.2.0 – CSS3 additions
27
+ * [FEATURE] Support `@media` queries by ignoring them (fix from [@haimlankry](https://github.com/haimlankry))
28
+ * [FEATURE] Correctly flip `box-shadow` values (bug report from [@aviaron](https://github.com/aviaron))
29
+ * v0.1.0 – [@fractious](https://github.com/fractious) updates
23
30
  * [CLEANUP] Added rspec dev dependency
24
31
  * [CLEANUP] Fixed typo in internal method name
25
32
  * [FEATURE] Added support for background-position
data/Rakefile CHANGED
@@ -1,14 +1,9 @@
1
1
  require 'bundler'
2
2
  require 'rake'
3
- require 'spec/rake/spectask'
3
+ require 'rspec/core/rake_task'
4
4
 
5
5
  Bundler::GemHelper.install_tasks
6
6
 
7
- task :default => [:test]
8
-
9
- desc "Run all examples"
10
- Spec::Rake::SpecTask.new('test') do |t|
11
- t.spec_files = FileList['spec/*_spec.rb']
12
- end
13
-
7
+ RSpec::Core::RakeTask.new(:spec)
14
8
 
9
+ task :default => :spec
data/lib/r2.rb CHANGED
@@ -54,9 +54,9 @@ module R2
54
54
  '-webkit-border-radius' => lambda {|obj,val| obj.border_radius_swap(val) },
55
55
  'text-align' => lambda {|obj,val| obj.side_swap(val) },
56
56
  'float' => lambda {|obj,val| obj.side_swap(val) },
57
- 'box-shadow' => lambda {|obj,val| obj.quad_swap(val) },
58
- '-webkit-box-shadow' => lambda {|obj,val| obj.quad_swap(val) },
59
- '-moz-box-shadow' => lambda {|obj,val| obj.quad_swap(val) },
57
+ 'box-shadow' => lambda {|obj,val| obj.shadow_swap(val) },
58
+ '-webkit-box-shadow' => lambda {|obj,val| obj.shadow_swap(val) },
59
+ '-moz-box-shadow' => lambda {|obj,val| obj.shadow_swap(val) },
60
60
  'direction' => lambda {|obj,val| obj.direction_swap(val) },
61
61
  'clear' => lambda {|obj,val| obj.side_swap(val) },
62
62
  'background-position' => lambda {|obj,val| obj.background_position_swap(val) }
@@ -66,20 +66,19 @@ module R2
66
66
  def r2(original_css)
67
67
  css = minimize(original_css)
68
68
 
69
- result = css.gsub(/([^\{]+\{[^\}]+\})+?/) do |rule|
70
- # break rule into selector|declaration parts
71
- parts = rule.match(/([^\{]+)\{([^\}]+)/)
72
- selector = parts[1]
73
- declarations = parts[2]
74
-
75
- rule_str = selector + '{'
76
- declarations.split(/;(?!base64)/).each do |decl|
77
- rule_str << declaration_swap(decl)
69
+ result = css.gsub(/([^\{\}]+[^\}]|[\}])+?/) do |rule|
70
+ if rule.match(/[\{\}]/)
71
+ #it is a selector with "{" or a closing "}", insert as it is
72
+ rule_str = rule
73
+ else
74
+ #It is a decleration
75
+ rule_str = ""
76
+ rule.split(/;(?!base64)/).each do |decl|
77
+ rule_str << declaration_swap(decl)
78
+ end
78
79
  end
79
- rule_str << "}"
80
80
  rule_str
81
81
  end
82
-
83
82
  return result
84
83
  end
85
84
 
@@ -90,7 +89,8 @@ module R2
90
89
  css.gsub(/\/\*[\s\S]+?\*\//, ''). # comments
91
90
  gsub(/[\n\r]/, ''). # line breaks and carriage returns
92
91
  gsub(/\s*([:;,\{\}])\s*/, '\1'). # space between selectors, declarations, properties and values
93
- gsub(/\s+/, ' ') # replace multiple spaces with single spaces
92
+ gsub(/\s+/, ' '). # replace multiple spaces with single spaces
93
+ gsub(/(\A\s+|\s+\z)/, '') # leading or trailing spaces
94
94
  end
95
95
 
96
96
  # Given a single CSS declaration rule (e.g. <tt>padding-left: 4px</tt>) return the opposing rule (so, <tt>padding-right:4px;</tt> in this example)
@@ -144,6 +144,20 @@ module R2
144
144
  val
145
145
  end
146
146
  end
147
+
148
+ # Given the 2-6 variable declaration for box-shadow convert the direction. Conversion requires inverting the
149
+ # horizontal measure only.
150
+ def shadow_swap(val)
151
+ args = val.to_s.split(/\s+/)
152
+
153
+ matched = args && args[0].match(/^([-+]?\d+)(\w*)$/)
154
+ if matched
155
+ return (["#{(-1 * matched[1].to_i)}#{matched[2]}"] + Array(args.values_at(1..5))).compact.join(' ')
156
+ else
157
+ return val
158
+ end
159
+ end
160
+
147
161
  # Border radius uses top-left, top-right, bottom-left, bottom-right, so all values need to be swapped. Additionally,
148
162
  # two and three value border-radius declarations need to be swapped as well. Vertical radius, specified with a /,
149
163
  # should be left alone.
@@ -1,3 +1,3 @@
1
1
  module R2
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/r2.gemspec CHANGED
@@ -14,7 +14,8 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.rubyforge_project = "r2"
16
16
 
17
- s.add_development_dependency 'rspec', '~> 1.3.0'
17
+ s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'rspec', '~> 2.13.0'
18
19
 
19
20
  s.files = `git ls-files`.split("\n")
20
21
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -2,137 +2,192 @@
2
2
  require 'r2'
3
3
 
4
4
  describe R2 do
5
- before(:each) do
6
- @r2 = R2::Swapper.new
5
+
6
+ describe ".r2" do
7
+ let(:r2) { double("r2") }
8
+ let(:css) { "body { direction: rtl; }" }
9
+
10
+ it "provides a shortcut to .new#r2" do
11
+ R2::Swapper.should_receive(:new).and_return(r2)
12
+ r2.should_receive(:r2).with(css)
13
+ R2.r2(css)
14
+ end
7
15
  end
8
16
 
9
- context ".r2 static call" do
17
+ end
18
+
19
+ describe R2::Swapper do
20
+ subject(:r2) { R2::Swapper.new }
21
+
22
+ describe "#r2" do
10
23
  it "processes CSS" do
11
- R2.r2("/* comment */\nbody { direction: rtl; }\nimg { padding: 4px;}").should == "body{direction:ltr;}img{padding:4px;}"
24
+ r2.r2("/* comment */\nbody { direction: rtl; }\nimg { padding: 4px;}").should == "body{direction:ltr;}img{padding:4px;}"
25
+ end
26
+
27
+ it "handles media queries" do
28
+ css = <<-EOS
29
+ @media all and (max-width: 222px) {
30
+ p {
31
+ padding-left: 2px;
32
+ }
33
+ }
34
+ EOS
35
+
36
+ expected_result = "@media all and (max-width:222px){p{padding-right:2px;}}"
37
+
38
+ flipped_css = r2.r2(css)
39
+
40
+ flipped_css.should == expected_result
12
41
  end
13
42
  end
14
43
 
15
- context "declaration_swap" do
44
+ describe "#declaration_swap" do
16
45
  it "should handle nil" do
17
- @r2.declaration_swap(nil).should == ''
46
+ r2.declaration_swap(nil).should == ''
18
47
  end
19
48
 
20
49
  it "should handle invalid declarations" do
21
- @r2.declaration_swap("not a decl").should == ''
50
+ r2.declaration_swap("not a decl").should == ''
22
51
  end
23
52
 
24
53
  it "should swap a swappable parameter" do
25
- @r2.declaration_swap("padding-right:4px").should == 'padding-left:4px;'
54
+ r2.declaration_swap("padding-right:4px").should == 'padding-left:4px;'
26
55
  end
27
56
 
28
57
  it "should swap a swappable quad parameter" do
29
- @r2.declaration_swap("padding:1px 2px 3px 4px").should == 'padding:1px 4px 3px 2px;'
58
+ r2.declaration_swap("padding:1px 2px 3px 4px").should == 'padding:1px 4px 3px 2px;'
30
59
  end
31
60
 
32
61
  it "should ignore other parameters" do
33
- @r2.declaration_swap("foo:bar").should == 'foo:bar;'
62
+ r2.declaration_swap("foo:bar").should == 'foo:bar;'
34
63
  end
35
64
  end
36
65
 
37
- context "minimize" do
66
+ describe "#minimize" do
38
67
  it "should handle nil" do
39
- @r2.minimize(nil).should == ""
68
+ r2.minimize(nil).should == ""
40
69
  end
41
70
 
42
71
  it "should strip comments" do
43
- @r2.minimize("/* comment */foo").should == "foo"
72
+ r2.minimize("/* comment */foo").should == "foo"
44
73
  end
45
74
 
46
75
  it "should remove newlines" do
47
- @r2.minimize("foo\nbar").should == "foobar"
76
+ r2.minimize("foo\nbar").should == "foobar"
48
77
  end
49
78
 
50
79
  it "should remove carriage returns" do
51
- @r2.minimize("foo\rbar").should == "foobar"
80
+ r2.minimize("foo\rbar").should == "foobar"
52
81
  end
53
82
 
54
83
  it "should collapse multiple spaces into one" do
55
- @r2.minimize("foo bar").should == "foo bar"
84
+ r2.minimize("foo bar").should == "foo bar"
56
85
  end
57
86
  end
58
87
 
59
- context "direction_swap" do
88
+ describe "#direction_swap" do
60
89
  it "should swap 'rtl' to 'ltr'" do
61
- @r2.direction_swap('rtl').should == 'ltr'
90
+ r2.direction_swap('rtl').should == 'ltr'
62
91
  end
63
92
 
64
93
  it "should swap 'ltr' to 'rtl'" do
65
- @r2.direction_swap('ltr').should == 'rtl'
94
+ r2.direction_swap('ltr').should == 'rtl'
66
95
  end
67
96
 
68
97
  it "should ignore values other than 'ltr' and 'rtl'" do
69
98
  [nil, '', 'foo'].each do |val|
70
- @r2.direction_swap(val).should == val
99
+ r2.direction_swap(val).should == val
71
100
  end
72
101
  end
73
102
  end
74
103
 
75
- context "side_swap" do
104
+ describe "#side_swap" do
76
105
  it "should swap 'right' to 'left'" do
77
- @r2.side_swap('right').should == 'left'
106
+ r2.side_swap('right').should == 'left'
78
107
  end
79
108
 
80
109
  it "should swap 'left' to 'right'" do
81
- @r2.side_swap('left').should == 'right'
110
+ r2.side_swap('left').should == 'right'
82
111
  end
83
112
 
84
113
  it "should ignore values other than 'left' and 'right'" do
85
114
  [nil, '', 'foo'].each do |val|
86
- @r2.side_swap(val).should == val
115
+ r2.side_swap(val).should == val
87
116
  end
88
117
  end
89
118
  end
90
119
 
91
- context "quad_swap" do
120
+ describe "#quad_swap" do
92
121
  it "should swap a valid quad value" do
93
- @r2.quad_swap("1px 2px 3px 4px").should == "1px 4px 3px 2px"
122
+ r2.quad_swap("1px 2px 3px 4px").should == "1px 4px 3px 2px"
94
123
  end
95
124
 
96
125
  it "should skip a pair value" do
97
- @r2.quad_swap("1px 2px").should == "1px 2px"
126
+ r2.quad_swap("1px 2px").should == "1px 2px"
98
127
  end
99
128
  end
100
129
 
101
- context "border_radius_swap" do
130
+ describe "#shadow_swap" do
131
+ it "should swap a 2 arg value" do
132
+ r2.shadow_swap("1px 2px").should == "-1px 2px"
133
+ end
134
+
135
+ it "should swap a 2 arg value from rtl to ltr" do
136
+ r2.shadow_swap("-1px 2px").should == "1px 2px"
137
+ end
138
+
139
+ it "should swap a 3 arg value" do
140
+ r2.shadow_swap("1px 2px #000").should == "-1px 2px #000"
141
+ end
142
+
143
+ it "should swap a 4 arg value" do
144
+ r2.shadow_swap("1px 2px 3px 4px").should == "-1px 2px 3px 4px"
145
+ end
146
+
147
+ it "should swap a 5 arg value" do
148
+ r2.shadow_swap("1px 2px 3px 4px #000").should == "-1px 2px 3px 4px #000"
149
+ end
150
+
151
+ it "should swap a 6 arg value" do
152
+ r2.shadow_swap("1px 2px 3px 4px #000 inset").should == "-1px 2px 3px 4px #000 inset"
153
+ end
154
+ end
155
+
156
+ describe "#border_radius_swap" do
102
157
  it "should swap a valid quad value" do
103
- @r2.border_radius_swap("1px 2px 3px 4px").should == "2px 1px 4px 3px"
158
+ r2.border_radius_swap("1px 2px 3px 4px").should == "2px 1px 4px 3px"
104
159
  end
105
160
 
106
161
  it "should skip a triple value" do
107
- @r2.border_radius_swap("1px 2px 3px").should == "2px 1px 2px 3px"
162
+ r2.border_radius_swap("1px 2px 3px").should == "2px 1px 2px 3px"
108
163
  end
109
164
 
110
165
  it "should skip a pair value" do
111
- @r2.border_radius_swap("1px 2px").should == "2px 1px"
166
+ r2.border_radius_swap("1px 2px").should == "2px 1px"
112
167
  end
113
168
  end
114
169
 
115
- context "background_position_swap" do
170
+ describe "#background_position_swap" do
116
171
 
117
172
  context "with a single value" do
118
173
  it "should ignore a named-vertical" do
119
- @r2.background_position_swap('top').should == 'top'
174
+ r2.background_position_swap('top').should == 'top'
120
175
  end
121
176
 
122
177
  it "should swap a named-horizontal 'left'" do
123
- @r2.background_position_swap('left').should == 'right'
178
+ r2.background_position_swap('left').should == 'right'
124
179
  end
125
180
 
126
181
  it "should swap a named-horizontal 'right'" do
127
- @r2.background_position_swap('right').should == 'left'
182
+ r2.background_position_swap('right').should == 'left'
128
183
  end
129
184
 
130
185
  it "should invert a percentage" do
131
- @r2.background_position_swap('25%').should == '75%'
186
+ r2.background_position_swap('25%').should == '75%'
132
187
  end
133
188
 
134
189
  it "should convert a unit value" do
135
- @r2.background_position_swap('25px').should == 'right 25px center'
190
+ r2.background_position_swap('25px').should == 'right 25px center'
136
191
  end
137
192
  end
138
193
 
@@ -143,36 +198,37 @@ describe R2 do
143
198
  # See: http://dev.w3.org/csswg/css3-background/#background-position
144
199
 
145
200
  it "should swap named-horizontal and ignore named-vertical" do
146
- @r2.background_position_swap('right bottom').should == 'left bottom'
201
+ r2.background_position_swap('right bottom').should == 'left bottom'
147
202
  end
148
203
 
149
204
  it "should swap named-horizontal and ignore unit-vertical" do
150
- @r2.background_position_swap('left 100px').should == 'right 100px'
205
+ r2.background_position_swap('left 100px').should == 'right 100px'
151
206
  end
152
207
 
153
208
  it "should convert unit-horizontal" do
154
- @r2.background_position_swap('100px center').should == 'right 100px center'
209
+ r2.background_position_swap('100px center').should == 'right 100px center'
155
210
  end
156
211
 
157
212
  it "should swap named-horizontal and ignore percentage-vertical" do
158
- @r2.background_position_swap('left 0%').should == 'right 0%'
213
+ r2.background_position_swap('left 0%').should == 'right 0%'
159
214
  end
160
215
 
161
216
  it "should invert first percentage-horizontal value in a pair" do
162
- @r2.background_position_swap('25% 100%').should == '75% 100%'
217
+ r2.background_position_swap('25% 100%').should == '75% 100%'
163
218
  end
164
219
  end
165
220
 
166
221
  context "with a triplet of values" do
167
222
  it "should swap named-horizontal" do
168
- @r2.background_position_swap('left 20px center').should == 'right 20px center'
223
+ r2.background_position_swap('left 20px center').should == 'right 20px center'
169
224
  end
170
225
  end
171
226
 
172
227
  context "with a quad of values" do
173
228
  it "should swap named-horizontal value" do
174
- @r2.background_position_swap('bottom 10px left 20px').should == 'bottom 10px right 20px'
229
+ r2.background_position_swap('bottom 10px left 20px').should == 'bottom 10px right 20px'
175
230
  end
176
231
  end
177
232
  end
233
+
178
234
  end
metadata CHANGED
@@ -1,32 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Matt Sanford
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-05 00:00:00.000000000 Z
11
+ date: 2013-05-23 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: rspec
16
29
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
30
  requirements:
19
31
  - - ~>
20
32
  - !ruby/object:Gem::Version
21
- version: 1.3.0
33
+ version: 2.13.0
22
34
  type: :development
23
35
  prerelease: false
24
36
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
37
  requirements:
27
38
  - - ~>
28
39
  - !ruby/object:Gem::Version
29
- version: 1.3.0
40
+ version: 2.13.0
30
41
  description: CSS flipper for right-to-left processing. A Ruby port of https://github.com/ded/r2
31
42
  email:
32
43
  - matt@twitter.com
@@ -36,7 +47,9 @@ extensions: []
36
47
  extra_rdoc_files: []
37
48
  files:
38
49
  - .gitignore
50
+ - .travis.yml
39
51
  - Gemfile
52
+ - Guardfile
40
53
  - LICENSE
41
54
  - README.md
42
55
  - Rakefile
@@ -47,27 +60,26 @@ files:
47
60
  - spec/r2_spec.rb
48
61
  homepage: ''
49
62
  licenses: []
63
+ metadata: {}
50
64
  post_install_message:
51
65
  rdoc_options: []
52
66
  require_paths:
53
67
  - lib
54
68
  required_ruby_version: !ruby/object:Gem::Requirement
55
- none: false
56
69
  requirements:
57
- - - ! '>='
70
+ - - '>='
58
71
  - !ruby/object:Gem::Version
59
72
  version: '0'
60
73
  required_rubygems_version: !ruby/object:Gem::Requirement
61
- none: false
62
74
  requirements:
63
- - - ! '>='
75
+ - - '>='
64
76
  - !ruby/object:Gem::Version
65
77
  version: '0'
66
78
  requirements: []
67
79
  rubyforge_project: r2
68
- rubygems_version: 1.8.23
80
+ rubygems_version: 2.0.3
69
81
  signing_key:
70
- specification_version: 3
82
+ specification_version: 4
71
83
  summary: CSS flipper for right-to-left processing
72
84
  test_files:
73
85
  - spec/r2_spec.rb