r2 0.2.6 → 0.2.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
- SHA1:
3
- metadata.gz: f8550ffae3d9358c1257e8163f6a5d03d945d09f
4
- data.tar.gz: 3a0e2808776280f8c3a87de5d1cf7baa5a18af8d
2
+ SHA256:
3
+ metadata.gz: 790a1c3403b43e80a07f3353d0f683483aff2f50367d95b0c55759202e02626c
4
+ data.tar.gz: af54fe9e6ee0d53be9b88683af39cae8f5ad40adb8c6a39a3e46f3356d3ce050
5
5
  SHA512:
6
- metadata.gz: d1de570c1fc501a5e278d2757bba45aacce956ef3d39660e9e3f661de8bbf7812391729407363882d9a98bb386bbff37ec8de588f85e9b49592679d3175aac1c
7
- data.tar.gz: 6c80710ffed22c49310fec1766d478e4df07c10af0a9bed75ec3a1ea1c02c18f3da29d311517355fcff44c374fab99c183ca9fc39c034a18300c922938f83f96
6
+ metadata.gz: 274cd78b0fd61476c37dcc87c474587ef60e57ad2cf2cdd3aeb803cf08c3abed8226b940ff563b5f98c87c7a55108264194f292663658f19bcfd7ec85a84b7d6
7
+ data.tar.gz: 6aa114c12414912ad7c1d4f26f93548e1c9eb85565e4b5f27599105c4c47e416b3138dbbce8df6dd14f152848ba9dea19dd92cb7dde5c0ee7f3f32d4eb8786a2
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
2
  language: ruby
3
3
  rvm:
4
- - 1.9.3
4
+ - 2.2
5
5
  - jruby-19mode # JRuby in 1.9 mode
data/README.md CHANGED
@@ -23,6 +23,7 @@ Report bugs in the github project at http://github.com/mzsanford/r2rb
23
23
 
24
24
  ## Change Log
25
25
 
26
+ * v0.2.7 - Add an option to skip R2 processing on a CSS block (error report from [@mapmeld](https://github.com/mapmeld))
26
27
  * v0.2.6 - Handle multi-row selectors (fix from [@n0nick](https://github.com/n0nick))
27
28
  * v0.2.5 - Handle `background:` shorthand
28
29
  * v0.2.4 - Handle `url()` properties better
data/lib/r2/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module R2
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.8"
3
3
  end
data/lib/r2.rb CHANGED
@@ -10,6 +10,12 @@ require 'r2/shadow_flipper'
10
10
 
11
11
  module R2
12
12
 
13
+ # A string that indicates this block should be skipped
14
+ SKIP_TOKEN = 'SKIP_R2'
15
+
16
+ # Important declarations are removed and then re-added after swapping
17
+ IMPORTANT_DECLARATION = '!important'
18
+
13
19
  # Short cut method for providing a one-time CSS change
14
20
  def self.r2(css)
15
21
  ::R2::Swapper.new.r2(css)
@@ -77,9 +83,13 @@ module R2
77
83
  # it is a selector with "{" or a closing "}", insert as it is. This is
78
84
  # things like ".foo {" and its matching "}"
79
85
  rule_str = rule
86
+ elsif rule.match(/#{SKIP_TOKEN}/)
87
+ # A body that is being skipped
88
+ rule_str = rule.sub(SKIP_TOKEN, '')
80
89
  else
81
90
  # It is a declaration body, like "padding-left:4px;margin-left:5px;"
82
91
  rule_str = ""
92
+
83
93
  # Split up the individual rules in the body and process each swap. To handle the
84
94
  # possible ";" in the url() definitions, like
85
95
  # url("data;base64") and url("data:image/svg+xml;charset=...")
@@ -109,11 +119,12 @@ module R2
109
119
  def minimize(css)
110
120
  return '' unless css
111
121
 
112
- css.gsub(/\/\*[\s\S]+?\*\//, ''). # comments
113
- gsub(/[\n\r]+/, ' '). # line breaks and carriage returns
114
- gsub(/\s*([:;,\{\}])\s*/, '\1'). # space between selectors, declarations, properties and values
115
- gsub(/\s+/, ' '). # replace multiple spaces with single spaces
116
- gsub(/(\A\s+|\s+\z)/, '') # leading or trailing spaces
122
+ css.gsub(/\/\*\s*no-r2\s*\*\//, SKIP_TOKEN). # special skip comment
123
+ gsub(/\/\*[\s\S]+?\*\//, ''). # comments
124
+ gsub(/[\n\r]+/, ' '). # line breaks and carriage returns
125
+ gsub(/\s*([:;,\{\}])\s*/, '\1'). # space between selectors, declarations, properties and values
126
+ gsub(/\s+/, ' '). # replace multiple spaces with single spaces
127
+ gsub(/(\A\s+|\s+\z)/, '') # leading or trailing spaces
117
128
  end
118
129
 
119
130
  # 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)
@@ -157,14 +168,17 @@ module R2
157
168
  # Given a 4-argument CSS declaration value (like that of <tt>padding</tt> or <tt>margin</tt>) return the opposing
158
169
  # value. The opposing value swaps the left and right but not the top or bottom. Any unrecognized argument is returned
159
170
  # unmolested (for example, 2-argument values)
160
- def quad_swap(val)
171
+ def quad_swap(css_value)
172
+ points_str, is_important = extract_important(css_value)
161
173
  # 1px 2px 3px 4px => 1px 4px 3px 2px
162
- points = val.to_s.split(/\s+/)
174
+ points = points_str.to_s.split(/\s+/)
163
175
 
164
- if points && points.length == 4
165
- [points[0], points[3], points[2], points[1]].join(' ')
166
- else
167
- val
176
+ append_important(is_important) do
177
+ if points && points.length == 4
178
+ [points[0], points[3], points[2], points[1]].join(' ')
179
+ else
180
+ points_str
181
+ end
168
182
  end
169
183
  end
170
184
 
@@ -204,22 +218,25 @@ module R2
204
218
  # Border radius uses top-left, top-right, bottom-left, bottom-right, so all values need to be swapped. Additionally,
205
219
  # two and three value border-radius declarations need to be swapped as well. Vertical radius, specified with a /,
206
220
  # should be left alone.
207
- def border_radius_swap(val)
221
+ def border_radius_swap(css_value)
208
222
  # 1px 2px 3px 4px => 1px 4px 3px 2px
209
- points = val.to_s.split(/\s+/)
210
-
211
- if points && points.length > 1 && !val.to_s.include?('/')
212
- case points.length
213
- when 4
214
- [points[1], points[0], points[3], points[2]].join(' ')
215
- when 3
216
- [points[1], points[0], points[1], points[2]].join(' ')
217
- when 2
218
- [points[1], points[0]].join(' ')
219
- else val
223
+ points_str, is_important = extract_important(css_value)
224
+ points = points_str.to_s.split(/\s+/)
225
+
226
+ append_important(is_important) do
227
+ if points && points.length > 1 && !points_str.to_s.include?('/')
228
+ case points.length
229
+ when 4
230
+ [points[1], points[0], points[3], points[2]].join(' ')
231
+ when 3
232
+ [points[1], points[0], points[1], points[2]].join(' ')
233
+ when 2
234
+ [points[1], points[0]].join(' ')
235
+ else points_str
236
+ end
237
+ else
238
+ points_str
220
239
  end
221
- else
222
- val
223
240
  end
224
241
  end
225
242
 
@@ -241,12 +258,22 @@ module R2
241
258
  end
242
259
 
243
260
  # If first point is a unit-value
244
- if match = points[0].match(/^(\d+[a-z]{2,3})/)
261
+ if match = points[0].match(/^(-?\d+[a-z]{2,3})/)
245
262
  val = ["right", match[1], points[1] || "center"].compact.join(' ')
246
263
  end
247
264
 
248
265
  val
249
266
  end
267
+
268
+ def extract_important(css)
269
+ is_important = css =~ /#{IMPORTANT_DECLARATION}/
270
+ return css.sub(/ #{IMPORTANT_DECLARATION}/, ''), is_important
271
+ end
272
+
273
+ def append_important(is_important, &block)
274
+ css_value = yield
275
+ css_value + (is_important ? " #{IMPORTANT_DECLARATION}" : '')
276
+ end
250
277
  end
251
278
 
252
279
  end
data/spec/r2_spec.rb CHANGED
@@ -107,6 +107,19 @@ describe R2::Swapper do
107
107
 
108
108
  expect(r2.r2(css)).to eq(expected)
109
109
  end
110
+
111
+ it "handles no-r2 comment" do
112
+ css = <<-EOS
113
+ .sprite.foo {
114
+ /* no-r2 */
115
+ background-position: 0px -50px;
116
+ }
117
+ EOS
118
+
119
+ expected = ".sprite.foo{ background-position:0px -50px;}"
120
+
121
+ expect(r2.r2(css)).to eq(expected)
122
+ end
110
123
  end
111
124
 
112
125
  describe "#declaration_swap" do
@@ -197,6 +210,11 @@ describe R2::Swapper do
197
210
  it "should skip a pair value" do
198
211
  expect(r2.quad_swap("1px 2px")).to eq("1px 2px")
199
212
  end
213
+
214
+ it "should handle !important" do
215
+ expect(r2.quad_swap("1px 2px 3px 4px !important")).to eq("1px 4px 3px 2px !important")
216
+ expect(r2.quad_swap("1px 2px !important")).to eq("1px 2px !important")
217
+ end
200
218
  end
201
219
 
202
220
  describe "#shadow_swap" do
@@ -236,6 +254,9 @@ describe R2::Swapper do
236
254
  expect(r2.shadow_swap("inset 1px 2px rgba(0,0,0,0.2), 1px 2px #000")).to eq("-1px 2px rgba(0,0,0,0.2) inset, -1px 2px #000")
237
255
  end
238
256
 
257
+ it "should handle !important correctly" do
258
+ expect(r2.shadow_swap("1px 2px !important")).to eq("-1px 2px !important")
259
+ end
239
260
  end
240
261
 
241
262
  describe "#border_radius_swap" do
@@ -250,6 +271,11 @@ describe R2::Swapper do
250
271
  it "should skip a pair value" do
251
272
  expect(r2.border_radius_swap("1px 2px")).to eq("2px 1px")
252
273
  end
274
+
275
+ it "should corectly handle !important" do
276
+ expect(r2.border_radius_swap("1px !important")).to eq("1px !important")
277
+ expect(r2.border_radius_swap("1px 2px !important")).to eq("2px 1px !important")
278
+ end
253
279
  end
254
280
 
255
281
  describe "#background_position_swap" do
@@ -274,6 +300,7 @@ describe R2::Swapper do
274
300
  it "should convert a unit value" do
275
301
  expect(r2.background_position_swap('25px')).to eq('right 25px center')
276
302
  end
303
+
277
304
  end
278
305
 
279
306
  context "with a pair of values" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Sanford
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2023-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -88,7 +88,7 @@ files:
88
88
  homepage: https://github.com/mzsanford/R2rb
89
89
  licenses: []
90
90
  metadata: {}
91
- post_install_message:
91
+ post_install_message:
92
92
  rdoc_options: []
93
93
  require_paths:
94
94
  - lib
@@ -103,9 +103,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
- rubyforge_project: r2
107
- rubygems_version: 2.2.2
108
- signing_key:
106
+ rubygems_version: 3.1.6
107
+ signing_key:
109
108
  specification_version: 4
110
109
  summary: CSS flipper for right-to-left processing
111
110
  test_files: