sprockets_relative_url 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.simplecov +7 -0
- data/.travis.yml +8 -0
- data/README.md +74 -10
- data/Rakefile +7 -0
- data/lib/sprockets_relative_url/processor.rb +18 -10
- data/lib/sprockets_relative_url/version.rb +1 -1
- data/spec/sprockets_relative_url/processor_spec.rb +28 -3
- data/sprockets_relative_url.gemspec +1 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f178dd2d77344289848a57b1ad03e3fda57ed14
|
4
|
+
data.tar.gz: c66cc99fd140581195a2b974e24674b09991c929
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74be7143490d14c696eee9e5a57e746011b58a74963856e6fff290a829176cf4ec7e2aef939cf64ad2d493579bd447c2c37b1af0991eb63ea8440b29591582e0
|
7
|
+
data.tar.gz: 2383006eab8ca1236d8fe2a7086f9d24d366c58e87a699457cbc686bff43f8ef6906964d5c42fdb1ed339c6f763bf0a7b0c713529062b8936457bba15ff4e722
|
data/.rubocop.yml
CHANGED
data/.simplecov
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
# Sprockets Relative URL support
|
2
2
|
|
3
|
+
[![Gem Version][GV img]][Gem Version]
|
4
|
+
[![Build Status][BS img]][Build Status]
|
5
|
+
[![Dependency Status][DS img]][Dependency Status]
|
6
|
+
[![Code Climate][CC img]][Code Climate]
|
7
|
+
[![Coverage Status][CS img]][Coverage Status]
|
8
|
+
[![Semantic Versioning][SV img]][Semantic Versioning]
|
9
|
+
|
10
|
+
[Gem Version]: https://rubygems.org/gems/sprockets_relative_url
|
11
|
+
[Build Status]: https://travis-ci.org/smangelsdorf/sprockets_relative_url
|
12
|
+
[Dependency Status]: https://gemnasium.com/smangelsdorf/sprockets_relative_url
|
13
|
+
[Code Climate]: https://codeclimate.com/github/smangelsdorf/sprockets_relative_url
|
14
|
+
[Coverage Status]: https://coveralls.io/r/smangelsdorf/sprockets_relative_url
|
15
|
+
[Semantic Versioning]: http://semver.org
|
16
|
+
|
17
|
+
[GV img]: https://img.shields.io/gem/v/sprockets_relative_url.svg
|
18
|
+
[BS img]: https://img.shields.io/travis/smangelsdorf/sprockets_relative_url.svg
|
19
|
+
[DS img]: https://img.shields.io/gemnasium/smangelsdorf/sprockets_relative_url.svg
|
20
|
+
[CC img]: https://img.shields.io/codeclimate/github/smangelsdorf/sprockets_relative_url.svg
|
21
|
+
[CS img]: https://img.shields.io/coveralls/smangelsdorf/sprockets_relative_url.svg
|
22
|
+
[SV img]: http://img.shields.io/badge/semver-%E2%9C%94-brightgreen.svg
|
23
|
+
|
3
24
|
Fixes relative URLs in CSS assets using Sprockets.
|
4
25
|
|
5
26
|
Each asset is resolved from Sprockets, and its relative URL will be rewritten to
|
@@ -7,16 +28,19 @@ use a precompiled version. By adding this preprocessor, CSS frameworks can be
|
|
7
28
|
minified and combined into your `application.css` file without any need to
|
8
29
|
rewrite the `url()` values in their CSS.
|
9
30
|
|
10
|
-
Tested
|
31
|
+
Tested Rubies:
|
11
32
|
|
12
|
-
-
|
13
|
-
-
|
33
|
+
- MRI 1.9.3, 2.0.0, 2.1.x
|
34
|
+
- JRuby 1.7.x
|
35
|
+
- Rubinius 2.2.x
|
14
36
|
|
15
37
|
## Installation
|
16
38
|
|
17
39
|
Add this line to your application's Gemfile:
|
18
40
|
|
19
|
-
|
41
|
+
```ruby
|
42
|
+
gem 'sprockets_relative_url'
|
43
|
+
```
|
20
44
|
|
21
45
|
## Usage
|
22
46
|
|
@@ -24,17 +48,57 @@ Add this line to your application's Gemfile:
|
|
24
48
|
|
25
49
|
Add the processor to the Rails asset pipeline:
|
26
50
|
|
27
|
-
|
28
|
-
|
51
|
+
```ruby
|
52
|
+
# config/initializers/sprockets_relative_url.rb
|
53
|
+
Rails.application.assets
|
54
|
+
.register_postprocessor('text/css', SprocketsRelativeUrl::Processor)
|
55
|
+
```
|
56
|
+
|
57
|
+
Ensure referenced assets are being precompiled. For example:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# config/application.rb
|
61
|
+
config.assets.precompile << /(fonts|images)\/.*/
|
62
|
+
```
|
63
|
+
|
64
|
+
### Standalone Sprockets
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
env = Sprockets::Environment.new
|
68
|
+
env.append_path('app/assets')
|
69
|
+
env.register_postprocessor('text/css', SprocketsRelativeUrl::Processor)
|
70
|
+
```
|
71
|
+
|
72
|
+
Similar to the Rails setup above, you will need to ensure that referenced assets
|
73
|
+
are being precompiled.
|
74
|
+
|
75
|
+
## Caveats
|
76
|
+
|
77
|
+
This library uses a regular expression to grab `url(...)` values out of CSS,
|
78
|
+
which has some drawbacks:
|
79
|
+
|
80
|
+
* No support for escaped parentheses inside URLs:
|
81
|
+
|
82
|
+
```css
|
83
|
+
background: url(images/mainbg\(white\).png);
|
84
|
+
```
|
85
|
+
|
86
|
+
This issue is addressed by ignoring any URL which contains a backslash
|
87
|
+
character, but this obviously means that escaped characters in a URL are
|
88
|
+
prohibited. Special characters can be URL encoded to work around this.
|
29
89
|
|
30
|
-
|
90
|
+
* Deliberately malformed CSS can probably trick the regular expression into
|
91
|
+
matching something it shouldn't. This should never be an issue, but it's
|
92
|
+
worth mentioning.
|
31
93
|
|
32
|
-
|
94
|
+
These could be fixed properly by building a full CSS parser into the processor,
|
95
|
+
but the additional complexity is prohibitive.
|
33
96
|
|
34
97
|
## Contributing
|
35
98
|
|
36
99
|
1. Fork it ( https://github.com/smangelsdorf/sprockets_relative_url/fork )
|
37
100
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
101
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
-
4.
|
40
|
-
5.
|
102
|
+
4. Verify project tests and style (`rake`)
|
103
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
104
|
+
6. Create a new Pull Request
|
data/Rakefile
CHANGED
@@ -1,37 +1,45 @@
|
|
1
1
|
module SprocketsRelativeUrl
|
2
2
|
class Processor < Sprockets::Processor
|
3
3
|
CSS_URL_REGEXP = /
|
4
|
-
(
|
4
|
+
(?<=[^a-zA-Z0-9_-]url\() # Match url\( prefix
|
5
5
|
\s*
|
6
|
-
(?<q>['"]?)
|
7
|
-
(?<path>[^\)]+?)
|
8
|
-
\k<q>
|
6
|
+
(?<q>['"]?) # Opening quote
|
7
|
+
(?<path>[^\)]+?) # Capture actual path
|
8
|
+
\k<q> # Backref for closing quote
|
9
9
|
\s*
|
10
|
-
(?=\))
|
10
|
+
(?=\)) # Match \) terminator
|
11
11
|
/x
|
12
12
|
|
13
|
+
private_constant :CSS_URL_REGEXP
|
14
|
+
|
13
15
|
def evaluate(context, *)
|
14
16
|
data.gsub(CSS_URL_REGEXP) do
|
15
17
|
path = Regexp.last_match[:path]
|
16
|
-
|
17
18
|
next path if should_skip?(path)
|
18
|
-
|
19
|
+
|
20
|
+
uri = URI.parse(path)
|
21
|
+
next path if uri.absolute?
|
22
|
+
|
23
|
+
uri.path = root_relative_path(context, uri.path)
|
24
|
+
uri.to_s
|
19
25
|
end
|
20
26
|
end
|
21
27
|
|
22
28
|
private
|
23
29
|
|
24
30
|
def root_relative_path(context, path)
|
25
|
-
|
31
|
+
decoded = URI.decode(path)
|
32
|
+
asset_full_path = context.pathname.parent.join(decoded)
|
26
33
|
asset = context.environment.find_asset(asset_full_path)
|
27
34
|
|
28
35
|
return path if asset.nil?
|
29
36
|
|
30
|
-
|
37
|
+
encoded = URI.encode(asset.logical_path)
|
38
|
+
context.asset_path(encoded)
|
31
39
|
end
|
32
40
|
|
33
41
|
def should_skip?(path)
|
34
|
-
path.start_with?('/') || path.include?('\\')
|
42
|
+
path.start_with?('/') || path.include?('\\')
|
35
43
|
end
|
36
44
|
end
|
37
45
|
end
|
@@ -16,8 +16,8 @@ describe SprocketsRelativeUrl::Processor do
|
|
16
16
|
end
|
17
17
|
let(:asset_prefix) { '/assets' }
|
18
18
|
|
19
|
-
let(:files) { %w(images/background.gif css/extra/test.gif) }
|
20
|
-
let(:hash) {
|
19
|
+
let(:files) { %w(images/background.gif css/extra/test.gif images/\.gif) }
|
20
|
+
let(:hash) { Time.now.to_i.to_s }
|
21
21
|
|
22
22
|
subject { SprocketsRelativeUrl::Processor.new { data } }
|
23
23
|
|
@@ -25,7 +25,7 @@ describe SprocketsRelativeUrl::Processor do
|
|
25
25
|
files.each do |file|
|
26
26
|
pathname = base_path.join(file)
|
27
27
|
pathname.parent.mkpath
|
28
|
-
pathname.
|
28
|
+
pathname.open('w').close unless pathname.exist?
|
29
29
|
end
|
30
30
|
|
31
31
|
allow(context).to receive(:asset_path) do |s|
|
@@ -116,6 +116,31 @@ describe SprocketsRelativeUrl::Processor do
|
|
116
116
|
include_examples 'rewrite the url'
|
117
117
|
end
|
118
118
|
|
119
|
+
context 'with url encoding' do
|
120
|
+
let(:data) { 'background: url(../images/%5c.gif)' }
|
121
|
+
let(:expected) { %r{background: url\(/assets/images/%5[cC]-\h+\.gif\)} }
|
122
|
+
|
123
|
+
include_examples 'rewrite the url'
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'with query parameters' do
|
127
|
+
let(:data) { 'background: url(../images/background.gif?1)' }
|
128
|
+
let(:expected) do
|
129
|
+
%r{background: url\(/assets/images/background-\h+\.gif\?1\)}
|
130
|
+
end
|
131
|
+
|
132
|
+
include_examples 'rewrite the url'
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'with a url fragment' do
|
136
|
+
let(:data) { 'background: url(../images/background.gif#1)' }
|
137
|
+
let(:expected) do
|
138
|
+
%r{background: url\(/assets/images/background-\h+\.gif#1\)}
|
139
|
+
end
|
140
|
+
|
141
|
+
include_examples 'rewrite the url'
|
142
|
+
end
|
143
|
+
|
119
144
|
context 'with a root-relative url' do
|
120
145
|
let(:data) { 'background: url(/images/background.gif)' }
|
121
146
|
include_examples 'leave the url alone'
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
25
25
|
spec.add_development_dependency 'rake'
|
26
26
|
spec.add_development_dependency 'simplecov'
|
27
|
+
spec.add_development_dependency 'coveralls'
|
27
28
|
|
28
29
|
spec.add_runtime_dependency 'sprockets', '~> 2.12'
|
29
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets_relative_url
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shaun Mangelsdorf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: guard
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: coveralls
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: sprockets
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -133,6 +147,7 @@ files:
|
|
133
147
|
- ".rspec"
|
134
148
|
- ".rubocop.yml"
|
135
149
|
- ".simplecov"
|
150
|
+
- ".travis.yml"
|
136
151
|
- Gemfile
|
137
152
|
- Guardfile
|
138
153
|
- LICENSE.txt
|
@@ -171,3 +186,4 @@ summary: Fixes relative URLs in CSS assets using Sprockets.
|
|
171
186
|
test_files:
|
172
187
|
- spec/spec_helper.rb
|
173
188
|
- spec/sprockets_relative_url/processor_spec.rb
|
189
|
+
has_rdoc:
|