r2 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +5 -5
  2. data/lib/r2/version.rb +1 -1
  3. data/lib/r2.rb +39 -20
  4. data/spec/r2_spec.rb +13 -0
  5. metadata +6 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d1fa57a5b8f4000ba8d6e91882b7eda70ef205b3
4
- data.tar.gz: 34e72abffc40f8b95266805d6e5275bf830eaf1a
2
+ SHA256:
3
+ metadata.gz: 790a1c3403b43e80a07f3353d0f683483aff2f50367d95b0c55759202e02626c
4
+ data.tar.gz: af54fe9e6ee0d53be9b88683af39cae8f5ad40adb8c6a39a3e46f3356d3ce050
5
5
  SHA512:
6
- metadata.gz: 78b0b6a518f92747338fb1e0dbe32089ca82679ffca5dd7de2c867ae4691da8282926cb39fe08c6dc2f7ca16ec102f443935fba82561be3f22eb1110d7df7942
7
- data.tar.gz: ac1f527bbb0a97dafb880e2d849f7a7e4f91137ab7af58997caef3ecd6995c545f556d7c5a7bef3a7311be96d0c2296e4770440943663836c5b0dbdda04446ed
6
+ metadata.gz: 274cd78b0fd61476c37dcc87c474587ef60e57ad2cf2cdd3aeb803cf08c3abed8226b940ff563b5f98c87c7a55108264194f292663658f19bcfd7ec85a84b7d6
7
+ data.tar.gz: 6aa114c12414912ad7c1d4f26f93548e1c9eb85565e4b5f27599105c4c47e416b3138dbbce8df6dd14f152848ba9dea19dd92cb7dde5c0ee7f3f32d4eb8786a2
data/lib/r2/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module R2
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
data/lib/r2.rb CHANGED
@@ -13,6 +13,9 @@ module R2
13
13
  # A string that indicates this block should be skipped
14
14
  SKIP_TOKEN = 'SKIP_R2'
15
15
 
16
+ # Important declarations are removed and then re-added after swapping
17
+ IMPORTANT_DECLARATION = '!important'
18
+
16
19
  # Short cut method for providing a one-time CSS change
17
20
  def self.r2(css)
18
21
  ::R2::Swapper.new.r2(css)
@@ -165,14 +168,17 @@ module R2
165
168
  # Given a 4-argument CSS declaration value (like that of <tt>padding</tt> or <tt>margin</tt>) return the opposing
166
169
  # value. The opposing value swaps the left and right but not the top or bottom. Any unrecognized argument is returned
167
170
  # unmolested (for example, 2-argument values)
168
- def quad_swap(val)
171
+ def quad_swap(css_value)
172
+ points_str, is_important = extract_important(css_value)
169
173
  # 1px 2px 3px 4px => 1px 4px 3px 2px
170
- points = val.to_s.split(/\s+/)
174
+ points = points_str.to_s.split(/\s+/)
171
175
 
172
- if points && points.length == 4
173
- [points[0], points[3], points[2], points[1]].join(' ')
174
- else
175
- 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
176
182
  end
177
183
  end
178
184
 
@@ -212,22 +218,25 @@ module R2
212
218
  # Border radius uses top-left, top-right, bottom-left, bottom-right, so all values need to be swapped. Additionally,
213
219
  # two and three value border-radius declarations need to be swapped as well. Vertical radius, specified with a /,
214
220
  # should be left alone.
215
- def border_radius_swap(val)
221
+ def border_radius_swap(css_value)
216
222
  # 1px 2px 3px 4px => 1px 4px 3px 2px
217
- points = val.to_s.split(/\s+/)
218
-
219
- if points && points.length > 1 && !val.to_s.include?('/')
220
- case points.length
221
- when 4
222
- [points[1], points[0], points[3], points[2]].join(' ')
223
- when 3
224
- [points[1], points[0], points[1], points[2]].join(' ')
225
- when 2
226
- [points[1], points[0]].join(' ')
227
- 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
228
239
  end
229
- else
230
- val
231
240
  end
232
241
  end
233
242
 
@@ -255,6 +264,16 @@ module R2
255
264
 
256
265
  val
257
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
258
277
  end
259
278
 
260
279
  end
data/spec/r2_spec.rb CHANGED
@@ -210,6 +210,11 @@ describe R2::Swapper do
210
210
  it "should skip a pair value" do
211
211
  expect(r2.quad_swap("1px 2px")).to eq("1px 2px")
212
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
213
218
  end
214
219
 
215
220
  describe "#shadow_swap" do
@@ -249,6 +254,9 @@ describe R2::Swapper do
249
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")
250
255
  end
251
256
 
257
+ it "should handle !important correctly" do
258
+ expect(r2.shadow_swap("1px 2px !important")).to eq("-1px 2px !important")
259
+ end
252
260
  end
253
261
 
254
262
  describe "#border_radius_swap" do
@@ -263,6 +271,11 @@ describe R2::Swapper do
263
271
  it "should skip a pair value" do
264
272
  expect(r2.border_radius_swap("1px 2px")).to eq("2px 1px")
265
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
266
279
  end
267
280
 
268
281
  describe "#background_position_swap" 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.7
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: 2017-07-17 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.5.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: