inline_svg 1.3.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/workflows/integration_test.yml +47 -0
- data/.github/workflows/rails_6_webpacker_integration_tests.yaml +62 -0
- data/.github/workflows/ruby.yml +20 -0
- data/CHANGELOG.md +94 -5
- data/README.md +42 -16
- data/inline_svg.gemspec +1 -1
- data/lib/inline_svg/action_view/helpers.rb +38 -4
- data/lib/inline_svg/cached_asset_file.rb +3 -12
- data/lib/inline_svg/finds_asset_paths.rb +1 -1
- data/lib/inline_svg/id_generator.rb +3 -1
- data/lib/inline_svg/propshaft_asset_finder.rb +16 -0
- data/lib/inline_svg/railtie.rb +3 -2
- data/lib/inline_svg/static_asset_finder.rb +5 -2
- data/lib/inline_svg/transform_pipeline/transformations/view_box.rb +9 -0
- data/lib/inline_svg/transform_pipeline/transformations.rb +8 -3
- data/lib/inline_svg/transform_pipeline.rb +1 -1
- data/lib/inline_svg/version.rb +1 -1
- data/lib/inline_svg/webpack_asset_finder.rb +60 -0
- data/lib/inline_svg.rb +15 -0
- data/spec/finds_asset_paths_spec.rb +45 -0
- data/spec/helpers/inline_svg_spec.rb +83 -62
- data/spec/id_generator_spec.rb +1 -1
- data/spec/inline_svg_spec.rb +1 -1
- data/spec/propshaft_asset_finder_spec.rb +23 -0
- data/spec/static_asset_finder_spec.rb +25 -0
- data/spec/transformation_pipeline/transformations/view_box_spec.rb +13 -0
- data/spec/transformation_pipeline/transformations_spec.rb +2 -0
- data/spec/webpack_asset_finder_spec.rb +23 -0
- metadata +22 -10
- data/circle.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 66214539304b60e87e9263afb26531212492ca77957e9ce8cf89d015cef86239
|
4
|
+
data.tar.gz: 6ab9cb3d75a9f268728600edb48bf27b3503145c80f1addabe8bc484c88af545
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f8d4642dff97eb5bfc358fd1ce2257d5113a6f232c02f4ecd847007ee861b2537748539c689e5e2e67297951fdd158594740a754d49469cbd8c13c72002c290
|
7
|
+
data.tar.gz: 1b5cb2dfd5e7067d64ec0cc3449c2aeeead3a39cab42f8d51db33609a296478511fa9a24a965e32b10d7f01c98e5cb4b2ef8a6268fb055a7ad968068499eccd6
|
@@ -0,0 +1,47 @@
|
|
1
|
+
name: Integration Tests
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
test-branch: [rails5, rails6, rails7]
|
11
|
+
timeout-minutes: 20
|
12
|
+
steps:
|
13
|
+
- name: Checkout
|
14
|
+
uses: actions/checkout@v2
|
15
|
+
- name: Checkout test app
|
16
|
+
uses: actions/checkout@v2
|
17
|
+
with:
|
18
|
+
repository: jamesmartin/inline_svg_test_app
|
19
|
+
ref: ${{ matrix.test-branch }}
|
20
|
+
path: test_app
|
21
|
+
- name: Set up Ruby 2.7
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: 2.7.7
|
25
|
+
- name: Build local gem
|
26
|
+
run: |
|
27
|
+
gem install bundler
|
28
|
+
bundle install --jobs 4 --retry 3
|
29
|
+
bundle exec rake build
|
30
|
+
- name: Use the local gem in the test App
|
31
|
+
id: uselocalgem
|
32
|
+
uses: jacobtomlinson/gha-find-replace@0.1.1
|
33
|
+
with:
|
34
|
+
find: "gem 'inline_svg'"
|
35
|
+
replace: "gem 'inline_svg', path: '${{github.workspace}}'"
|
36
|
+
- name: Check local gem in use
|
37
|
+
run: |
|
38
|
+
test "${{ steps.uselocalgem.outputs.modifiedFiles }}" != "0"
|
39
|
+
grep "inline_svg" $GITHUB_WORKSPACE/test_app/Gemfile
|
40
|
+
- name: Bundle
|
41
|
+
run: |
|
42
|
+
cd $GITHUB_WORKSPACE/test_app
|
43
|
+
bundle install --jobs 4 --retry 3
|
44
|
+
- name: Test
|
45
|
+
run: |
|
46
|
+
cd $GITHUB_WORKSPACE/test_app
|
47
|
+
bundle exec rake test
|
@@ -0,0 +1,62 @@
|
|
1
|
+
name: Rails 6 Webpacker Integration Tests (unreliable)
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
strategy:
|
10
|
+
matrix:
|
11
|
+
test-branch: [rails6-webpacker]
|
12
|
+
steps:
|
13
|
+
- name: Checkout
|
14
|
+
uses: actions/checkout@v2
|
15
|
+
- name: Checkout test app
|
16
|
+
uses: actions/checkout@v2
|
17
|
+
with:
|
18
|
+
repository: jamesmartin/inline_svg_test_app
|
19
|
+
ref: ${{ matrix.test-branch }}
|
20
|
+
path: test_app
|
21
|
+
- name: Set up Ruby 3.1
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: 3.1
|
25
|
+
- name: Build local gem
|
26
|
+
run: |
|
27
|
+
gem install bundler
|
28
|
+
bundle install --jobs 4 --retry 3
|
29
|
+
bundle exec rake build
|
30
|
+
- name: Use the local gem in the test App
|
31
|
+
id: uselocalgem
|
32
|
+
uses: jacobtomlinson/gha-find-replace@0.1.1
|
33
|
+
with:
|
34
|
+
find: "gem 'inline_svg'"
|
35
|
+
replace: "gem 'inline_svg', path: '${{github.workspace}}'"
|
36
|
+
- name: Check local gem in use
|
37
|
+
run: |
|
38
|
+
test "${{ steps.uselocalgem.outputs.modifiedFiles }}" != "0"
|
39
|
+
grep "inline_svg" $GITHUB_WORKSPACE/test_app/Gemfile
|
40
|
+
- name: Bundle
|
41
|
+
run: |
|
42
|
+
cd $GITHUB_WORKSPACE/test_app
|
43
|
+
bundle install --jobs 4 --retry 3
|
44
|
+
- name: Set up Node.js 16.x
|
45
|
+
uses: actions/setup-node@v2
|
46
|
+
with:
|
47
|
+
node-version: 16
|
48
|
+
if: matrix.test-branch == 'rails6-webpacker'
|
49
|
+
- name: Set up Python 2.7
|
50
|
+
uses: actions/setup-python@v4
|
51
|
+
with:
|
52
|
+
python-version: '2.7'
|
53
|
+
- name: Generate Webpacker config
|
54
|
+
run: |
|
55
|
+
cd $GITHUB_WORKSPACE/test_app
|
56
|
+
CXXFLAGS="--std=c++17" yarn install --check-files
|
57
|
+
bundle exec rake webpacker:compile
|
58
|
+
if: matrix.test-branch == 'rails6-webpacker'
|
59
|
+
- name: Test
|
60
|
+
run: |
|
61
|
+
cd $GITHUB_WORKSPACE/test_app
|
62
|
+
bundle exec rake test
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v2
|
12
|
+
- name: Set up Ruby 2.7
|
13
|
+
uses: ruby/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.7.7
|
16
|
+
- name: Build and test with Rake
|
17
|
+
run: |
|
18
|
+
gem install bundler
|
19
|
+
bundle install --jobs 4 --retry 3
|
20
|
+
bundle exec rake
|
data/CHANGELOG.md
CHANGED
@@ -3,9 +3,87 @@ All notable changes to this project will be documented in this file.
|
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
4
|
|
5
5
|
## [Unreleased][unreleased]
|
6
|
-
- Nothing
|
7
6
|
|
8
|
-
|
7
|
+
Nothing.
|
8
|
+
|
9
|
+
## [1.10.0] - 2024-09-03
|
10
|
+
### Added
|
11
|
+
- Support for Shakapacker. [#158](https://github.com/jamesmartin/inline_svg/pull/158). Thanks, [@tagliala](https://github.com/tagliala)
|
12
|
+
|
13
|
+
### Fixed
|
14
|
+
- Fixed documentation typos. [#157](https://github.com/jamesmartin/inline_svg/pull/157). Thanks, [@tagliala](https://github.com/tagliala)
|
15
|
+
- Fixed missing ActiveSupport require. [#152](https://github.com/jamesmartin/inline_svg/pull/152). Thanks, [@xymbol](https://github.com/xymbol)
|
16
|
+
- Remove wrapping whitespace from SVG tags. [#150](https://github.com/jamesmartin/inline_svg/pull/150). Thanks, [@fredboyle](https://github.com/fredboyle)
|
17
|
+
|
18
|
+
## [1.9.0] - 2023-03-29
|
19
|
+
### Added
|
20
|
+
- A new option: `view_box` adds a `viewBox` attribute to the SVG. [#142](https://github.com/jamesmartin/inline_svg/pull/142). Thanks [@sunny](https://github.com/sunny)
|
21
|
+
|
22
|
+
### Fixed
|
23
|
+
- Allow Propshaft assets to use fallbacks. [#140](https://github.com/jamesmartin/inline_svg/pull/140). Thanks, [@ohrite](https://github.com/ohrite)
|
24
|
+
- Handling missing file when using static assets. [#141](https://github.com/jamesmartin/inline_svg/pull/141). Thanks, [@leighhalliday](https://github.com/leighhalliday)
|
25
|
+
- Handle missing file when using Webpacker assets.
|
26
|
+
|
27
|
+
## [1.8.0] - 2022-01-09
|
28
|
+
### Added
|
29
|
+
- Remove deprecation warning for `inline_svg`, as we intend to keep it in 2.0. [#131](https://github.com/jamesmartin/inline_svg/pull/131). Thanks [@DanielJackson-Oslo](https://github.com/DanielJackson-Oslo)
|
30
|
+
- Add support for Webpacker 6 beta. [#129](https://github.com/jamesmartin/inline_svg/pull/129). Thanks [@Intrepidd](https://github.com/Intrepidd) and [@tessi](https://github.com/tessi)
|
31
|
+
- Add support for Propshaft assets in Rails 7. [#134](https://github.com/jamesmartin/inline_svg/pull/134). Thanks, [@martinzamuner](https://github.com/martinzamuner)
|
32
|
+
|
33
|
+
## [1.7.2] - 2020-12-07
|
34
|
+
### Fixed
|
35
|
+
- Improve performance of `CachedAssetFile`. [#118](https://github.com/jamesmartin/inline_svg/pull/118). Thanks [@stevendaniels](https://github.com/stevendaniels)
|
36
|
+
- Avoid XSS by preventing malicious input of filenames. [#117](https://github.com/jamesmartin/inline_svg/pull/117). Thanks [@pbyrne](https://github.com/pbyrne).
|
37
|
+
|
38
|
+
## [1.7.1] - 2020-03-17
|
39
|
+
### Fixed
|
40
|
+
- Static Asset Finder uses pathname for compatibility with Sprockets 4+. [#106](https://github.com/jamesmartin/inline_svg/pull/106). Thanks [@subdigital](https://github.com/subdigital)
|
41
|
+
|
42
|
+
## [1.7.0] - 2020-02-13
|
43
|
+
### Added
|
44
|
+
- WebpackAssetFinder serves files from dev server if one is running. [#111](https://github.com/jamesmartin/inline_svg/pull/111). Thanks, [@connorshea](https://github.com/connorshea)
|
45
|
+
|
46
|
+
### Fixed
|
47
|
+
- Using Webpacker and Asset Pipeline in a single App could result in SVGs not being found because the wrong `AssetFinder` was used. [#114](https://github.com/jamesmartin/inline_svg/pull/114). Thanks, [@kylefox](https://github.com/kylefox)
|
48
|
+
- Prevent "EOFError error" when using webpack dev server over HTTPS [#113](https://github.com/jamesmartin/inline_svg/pull/113). Thanks, [@kylefox](https://github.com/kylefox)
|
49
|
+
|
50
|
+
## [1.6.0] - 2019-11-13
|
51
|
+
### Added
|
52
|
+
- Support Webpack via the new `inline_svg_pack_tag` helper and deprecate `inline_svg` helper in preparation for v2.0.
|
53
|
+
[#103](https://github.com/jamesmartin/inline_svg/pull/103)
|
54
|
+
Thanks, [@kylefox](https://github.com/kylefox)
|
55
|
+
|
56
|
+
## [1.5.2] - 2019-06-20
|
57
|
+
### Fixed
|
58
|
+
- Revert automatic Webpack asset finder behavior. Make Webpack "opt-in".
|
59
|
+
[#98](https://github.com/jamesmartin/inline_svg/issues/98)
|
60
|
+
|
61
|
+
## [1.5.1] - 2019-06-18
|
62
|
+
### Fixed
|
63
|
+
- Prevent nil asset finder when neither Sprockets or Webpacker are available
|
64
|
+
[#97](https://github.com/jamesmartin/inline_svg/issues/97)
|
65
|
+
|
66
|
+
## [1.5.0] - 2019-06-17
|
67
|
+
### Added
|
68
|
+
- Support for finding assets bundled by Webpacker
|
69
|
+
[#96](https://github.com/jamesmartin/inline_svg/pull/96)
|
70
|
+
|
71
|
+
## [1.4.0] - 2019-04-19
|
72
|
+
### Fixed
|
73
|
+
- Prevent invalid XML names being generated via IdGenerator
|
74
|
+
[#87](https://github.com/jamesmartin/inline_svg/issues/87)
|
75
|
+
Thanks, [@endorfin](https://github.com/endorfin)
|
76
|
+
|
77
|
+
### Added
|
78
|
+
- Raise error on file not found (if configured)
|
79
|
+
[#93](https://github.com/jamesmartin/inline_svg/issues/93)
|
80
|
+
|
81
|
+
## [1.3.1] - 2017-12-14
|
82
|
+
### Fixed
|
83
|
+
- Allow Ruby < 2.1 to work with `CachedAssetFile`
|
84
|
+
[#80](https://github.com/jamesmartin/inline_svg/pull/80)
|
85
|
+
|
86
|
+
## [1.3.0] - 2017-10-30
|
9
87
|
### Added
|
10
88
|
- Aria hidden attribute
|
11
89
|
[#78](https://github.com/jamesmartin/inline_svg/pull/78)
|
@@ -143,7 +221,7 @@ transformations](https://github.com/jamesmartin/inline_svg/blob/master/README.md
|
|
143
221
|
|
144
222
|
## [0.5.1] - 2015-03-30
|
145
223
|
### Warning
|
146
|
-
** This version is NOT
|
224
|
+
** This version is NOT compatible with Sprockets >= 3. **
|
147
225
|
|
148
226
|
### Fixed
|
149
227
|
- Support for ActiveSupport (and hence, Rails) 4.2.x. Thanks, @jmarceli.
|
@@ -184,8 +262,19 @@ transformations](https://github.com/jamesmartin/inline_svg/blob/master/README.md
|
|
184
262
|
### Added
|
185
263
|
- Basic Railtie and view helper to inline SVG documents to Rails views.
|
186
264
|
|
187
|
-
[unreleased]: https://github.com/jamesmartin/inline_svg/compare/v1.
|
188
|
-
[1.
|
265
|
+
[unreleased]: https://github.com/jamesmartin/inline_svg/compare/v1.9.0...HEAD
|
266
|
+
[1.9.0]: https://github.com/jamesmartin/inline_svg/compare/v1.8.0...v1.9.0
|
267
|
+
[1.8.0]: https://github.com/jamesmartin/inline_svg/compare/v1.7.2...v1.8.0
|
268
|
+
[1.7.2]: https://github.com/jamesmartin/inline_svg/compare/v1.7.1...v1.7.2
|
269
|
+
[1.7.1]: https://github.com/jamesmartin/inline_svg/compare/v1.7.0...v1.7.1
|
270
|
+
[1.7.0]: https://github.com/jamesmartin/inline_svg/compare/v1.6.0...v1.7.0
|
271
|
+
[1.6.0]: https://github.com/jamesmartin/inline_svg/compare/v1.5.2...v1.6.0
|
272
|
+
[1.5.2]: https://github.com/jamesmartin/inline_svg/compare/v1.5.1...v1.5.2
|
273
|
+
[1.5.1]: https://github.com/jamesmartin/inline_svg/compare/v1.5.0...v1.5.1
|
274
|
+
[1.5.0]: https://github.com/jamesmartin/inline_svg/compare/v1.4.0...v1.5.0
|
275
|
+
[1.4.0]: https://github.com/jamesmartin/inline_svg/compare/v1.3.1...v1.4.0
|
276
|
+
[1.3.1]: https://github.com/jamesmartin/inline_svg/compare/v1.3.0...v1.3.1
|
277
|
+
[1.3.0]: https://github.com/jamesmartin/inline_svg/compare/v1.2.3...v1.3.0
|
189
278
|
[1.2.3]: https://github.com/jamesmartin/inline_svg/compare/v1.2.2...v1.2.3
|
190
279
|
[1.2.2]: https://github.com/jamesmartin/inline_svg/compare/v1.2.1...v1.2.2
|
191
280
|
[1.2.1]: https://github.com/jamesmartin/inline_svg/compare/v1.2.0...v1.2.1
|
data/README.md
CHANGED
@@ -1,15 +1,22 @@
|
|
1
1
|
# Inline SVG
|
2
2
|
|
3
|
-
|
3
|
+

|
4
|
+

|
4
5
|
|
5
6
|
Styling a SVG document with CSS for use on the web is most reliably achieved by
|
6
7
|
[adding classes to the document and
|
7
8
|
embedding](http://css-tricks.com/using-svg/) it inline in the HTML.
|
8
9
|
|
9
|
-
This gem adds
|
10
|
+
This gem adds Rails helper methods (`inline_svg_tag` and `inline_svg_pack_tag`) that read an SVG document (via Sprockets or Webpacker, so works with the Rails Asset Pipeline), applies a CSS class attribute to the root of the document and
|
10
11
|
then embeds it into a view.
|
11
12
|
|
12
|
-
Inline SVG supports
|
13
|
+
Inline SVG supports:
|
14
|
+
|
15
|
+
- [Rails 5](http://weblog.rubyonrails.org/2016/6/30/Rails-5-0-final/) (from [v0.10.0](https://github.com/jamesmartin/inline_svg/releases/tag/v0.10.0))
|
16
|
+
- [Rails 6](https://weblog.rubyonrails.org/2019/4/24/Rails-6-0-rc1-released/) with Sprockets or Webpacker (from [v1.5.2](https://github.com/jamesmartin/inline_svg/releases/tag/v1.5.2)).
|
17
|
+
- [Rails 7](https://weblog.rubyonrails.org/2021/12/6/Rails-7-0-rc-1-released/)
|
18
|
+
|
19
|
+
Inline SVG no longer officially supports Rails 3 or Rails 4 (although they may still work). In order to reduce the maintenance cost of this project we now follow the [Rails Maintenance Policy](https://guides.rubyonrails.org/maintenance_policy.html).
|
13
20
|
|
14
21
|
## Changelog
|
15
22
|
|
@@ -32,9 +39,15 @@ Or install it yourself as:
|
|
32
39
|
|
33
40
|
## Usage
|
34
41
|
|
42
|
+
```ruby
|
43
|
+
# Sprockets
|
44
|
+
inline_svg_tag(file_name, options={})
|
45
|
+
|
46
|
+
# Webpacker
|
47
|
+
inline_svg_pack_tag(file_name, options={})
|
35
48
|
```
|
36
|
-
|
37
|
-
|
49
|
+
|
50
|
+
_**Note:** The remainder of this README uses `inline_svg_tag` for examples, but the exact same principles work for `inline_svg_pack_tag`._
|
38
51
|
|
39
52
|
The `file_name` can be a full path to a file, the file's basename or an `IO`
|
40
53
|
object. The
|
@@ -52,7 +65,7 @@ Here's an example of embedding an SVG document and applying a 'class' attribute:
|
|
52
65
|
<body>
|
53
66
|
<h1>Embedded SVG Documents</h1>
|
54
67
|
<div>
|
55
|
-
<%=
|
68
|
+
<%= inline_svg_tag "some-document.svg", class: 'some-class' %>
|
56
69
|
</div>
|
57
70
|
</body>
|
58
71
|
</html>
|
@@ -84,13 +97,15 @@ key | description
|
|
84
97
|
`desc` | add a \<desc\> node inside the top level of the SVG document
|
85
98
|
`nocomment` | remove comment tags from the SVG document
|
86
99
|
`preserve_aspect_ratio` | adds a `preserveAspectRatio` attribute to the SVG
|
100
|
+
`view_box` | adds a `viewBox` attribute to the SVG
|
87
101
|
`aria` | adds common accessibility attributes to the SVG (see [PR #34](https://github.com/jamesmartin/inline_svg/pull/34#issue-152062674) for details)
|
88
102
|
`aria_hidden` | adds the `aria-hidden=true` attribute to the SVG
|
103
|
+
`fallback` | set fallback SVG document
|
89
104
|
|
90
105
|
Example:
|
91
106
|
|
92
107
|
```ruby
|
93
|
-
|
108
|
+
inline_svg_tag(
|
94
109
|
"some-document.svg",
|
95
110
|
id: 'some-id',
|
96
111
|
class: 'some-class',
|
@@ -100,14 +115,16 @@ inline_svg(
|
|
100
115
|
desc: 'Some description',
|
101
116
|
nocomment: true,
|
102
117
|
preserve_aspect_ratio: 'xMaxYMax meet',
|
118
|
+
view_box: '0 0 100 100',
|
103
119
|
aria: true,
|
104
|
-
aria_hidden: true
|
120
|
+
aria_hidden: true,
|
121
|
+
fallback: 'fallback-document.svg'
|
105
122
|
)
|
106
123
|
```
|
107
124
|
|
108
125
|
## Accessibility
|
109
126
|
|
110
|
-
Use the `aria: true` option to make `
|
127
|
+
Use the `aria: true` option to make `inline_svg_tag` add the following
|
111
128
|
accessibility (a11y) attributes to your embedded SVG:
|
112
129
|
|
113
130
|
* Adds a `role="img"` attribute to the root SVG element
|
@@ -118,7 +135,7 @@ Here's an example:
|
|
118
135
|
|
119
136
|
```erb
|
120
137
|
<%=
|
121
|
-
|
138
|
+
inline_svg_tag('iconmonstr-glasses-12-icon.svg',
|
122
139
|
aria: true, title: 'An SVG',
|
123
140
|
desc: 'This is my SVG. There are many like it. You get the picture')
|
124
141
|
%>
|
@@ -132,11 +149,11 @@ Here's an example:
|
|
132
149
|
</svg>
|
133
150
|
```
|
134
151
|
|
135
|
-
***Note:*** The title and desc `id` attributes generated for, and referenced by, `aria-labelled-by` are one-way digests based on the value of the title and desc elements and an optional "salt" value using the SHA1 algorithm. This reduces the chance of `
|
152
|
+
***Note:*** The title and desc `id` attributes generated for, and referenced by, `aria-labelled-by` are one-way digests based on the value of the title and desc elements and an optional "salt" value using the SHA1 algorithm. This reduces the chance of `inline_svg_tag` embedding elements inside the SVG with `id` attributes that clash with other elements elsewhere on the page.
|
136
153
|
|
137
154
|
## Custom Transformations
|
138
155
|
|
139
|
-
The transformation behavior of `
|
156
|
+
The transformation behavior of `inline_svg_tag` can be customized by creating custom transformation classes.
|
140
157
|
|
141
158
|
For example, inherit from `InlineSvg::CustomTransformation` and implement the `#transform` method:
|
142
159
|
|
@@ -165,7 +182,7 @@ end
|
|
165
182
|
The custom transformation can then be called like so:
|
166
183
|
```haml
|
167
184
|
%div
|
168
|
-
=
|
185
|
+
= inline_svg_tag "some-document.svg", my_custom_attribute: 'some value'
|
169
186
|
```
|
170
187
|
|
171
188
|
In this example, the following transformation would be applied to a SVG document:
|
@@ -186,8 +203,8 @@ end
|
|
186
203
|
The custom transformation will be triggered even if you don't pass any attribute value
|
187
204
|
```haml
|
188
205
|
%div
|
189
|
-
=
|
190
|
-
=
|
206
|
+
= inline_svg_tag "some-document.svg"
|
207
|
+
= inline_svg_tag "some-document.svg", my_custom_attribute: 'some value'
|
191
208
|
```
|
192
209
|
|
193
210
|
In this example, the following transformation would be applied to a SVG document:
|
@@ -267,7 +284,7 @@ end
|
|
267
284
|
|
268
285
|
**Note:** Paths are read recursively, so think about keeping your SVG assets
|
269
286
|
restricted to as few paths as possible, and using the filter option to further
|
270
|
-
restrict assets to only those likely to be used by `
|
287
|
+
restrict assets to only those likely to be used by `inline_svg_tag`.
|
271
288
|
|
272
289
|
## Missing SVG Files
|
273
290
|
|
@@ -294,6 +311,15 @@ Which would instead render:
|
|
294
311
|
<svg class='svg-not-found'><!-- SVG file not found: 'some-missing-file.svg' --></svg>
|
295
312
|
```
|
296
313
|
|
314
|
+
Alternatively, `inline_svg_tag` can be configured to raise an exception when a file
|
315
|
+
is not found:
|
316
|
+
|
317
|
+
```ruby
|
318
|
+
InlineSvg.configure do |config|
|
319
|
+
config.raise_on_file_not_found = true
|
320
|
+
end
|
321
|
+
```
|
322
|
+
|
297
323
|
## Contributing
|
298
324
|
|
299
325
|
1. Fork it ( [http://github.com/jamesmartin/inline_svg/fork](http://github.com/jamesmartin/inline_svg/fork) )
|
data/inline_svg.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency "bundler", "~>
|
21
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec", "~> 3.2"
|
24
24
|
spec.add_development_dependency "rspec_junit_formatter", "0.2.2"
|
@@ -4,10 +4,38 @@ require 'action_view/context' if defined?(Rails)
|
|
4
4
|
module InlineSvg
|
5
5
|
module ActionView
|
6
6
|
module Helpers
|
7
|
+
def inline_svg_tag(filename, transform_params={})
|
8
|
+
with_asset_finder(InlineSvg.configuration.asset_finder) do
|
9
|
+
render_inline_svg(filename, transform_params)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def inline_svg_pack_tag(filename, transform_params={})
|
14
|
+
with_asset_finder(InlineSvg::WebpackAssetFinder) do
|
15
|
+
render_inline_svg(filename, transform_params)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
7
19
|
def inline_svg(filename, transform_params={})
|
20
|
+
render_inline_svg(filename, transform_params)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def backwards_compatible_html_escape(filename)
|
26
|
+
# html_escape_once was introduced in newer versions of Rails.
|
27
|
+
if ERB::Util.respond_to?(:html_escape_once)
|
28
|
+
ERB::Util.html_escape_once(filename)
|
29
|
+
else
|
30
|
+
ERB::Util.html_escape(filename)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def render_inline_svg(filename, transform_params={})
|
8
35
|
begin
|
9
36
|
svg_file = read_svg(filename)
|
10
|
-
rescue InlineSvg::AssetFile::FileNotFound
|
37
|
+
rescue InlineSvg::AssetFile::FileNotFound => error
|
38
|
+
raise error if InlineSvg.configuration.raise_on_file_not_found?
|
11
39
|
return placeholder(filename) unless transform_params[:fallback].present?
|
12
40
|
|
13
41
|
if transform_params[:fallback].present?
|
@@ -22,8 +50,6 @@ module InlineSvg
|
|
22
50
|
InlineSvg::TransformPipeline.generate_html_from(svg_file, transform_params).html_safe
|
23
51
|
end
|
24
52
|
|
25
|
-
private
|
26
|
-
|
27
53
|
def read_svg(filename)
|
28
54
|
if InlineSvg::IOResource === filename
|
29
55
|
InlineSvg::IOResource.read filename
|
@@ -34,7 +60,7 @@ module InlineSvg
|
|
34
60
|
|
35
61
|
def placeholder(filename)
|
36
62
|
css_class = InlineSvg.configuration.svg_not_found_css_class
|
37
|
-
not_found_message = "'#{filename}' #{extension_hint(filename)}"
|
63
|
+
not_found_message = "'#{backwards_compatible_html_escape(filename)}' #{extension_hint(filename)}"
|
38
64
|
|
39
65
|
if css_class.nil?
|
40
66
|
return "<svg><!-- SVG file not found: #{not_found_message}--></svg>".html_safe
|
@@ -47,6 +73,14 @@ module InlineSvg
|
|
47
73
|
InlineSvg.configuration.asset_file
|
48
74
|
end
|
49
75
|
|
76
|
+
def with_asset_finder(asset_finder)
|
77
|
+
Thread.current[:inline_svg_asset_finder] = asset_finder
|
78
|
+
output = yield
|
79
|
+
Thread.current[:inline_svg_asset_finder] = nil
|
80
|
+
|
81
|
+
output
|
82
|
+
end
|
83
|
+
|
50
84
|
def extension_hint(filename)
|
51
85
|
filename.ends_with?(".svg") ? "" : "(Try adding .svg to your filename) "
|
52
86
|
end
|
@@ -14,10 +14,11 @@ module InlineSvg
|
|
14
14
|
# Note: Specifying no filters will cache every file found in
|
15
15
|
# paths.
|
16
16
|
#
|
17
|
-
def initialize(paths
|
17
|
+
def initialize(paths: [], filters: [])
|
18
18
|
@paths = Array(paths).compact.map { |p| Pathname.new(p) }
|
19
19
|
@filters = Array(filters).map { |f| Regexp.new(f) }
|
20
20
|
@assets = @paths.reduce({}) { |assets, p| assets.merge(read_assets(assets, p)) }
|
21
|
+
@sorted_asset_keys = assets.keys.sort { |a, b| a.size <=> b.size }
|
21
22
|
end
|
22
23
|
|
23
24
|
# Public: Finds the named asset and returns the contents as a string.
|
@@ -39,17 +40,7 @@ module InlineSvg
|
|
39
40
|
# Returns a String representing the key for the named asset or nil if there
|
40
41
|
# is no match.
|
41
42
|
def key_for_asset(asset_name)
|
42
|
-
|
43
|
-
a.string.size <=> b.string.size
|
44
|
-
end.first
|
45
|
-
match && match.string
|
46
|
-
end
|
47
|
-
|
48
|
-
# Internal: Find all potential asset keys matching the given asset name.
|
49
|
-
#
|
50
|
-
# Returns an array of MatchData objects for keys matching the asset name.
|
51
|
-
def all_keys_matching(asset_name)
|
52
|
-
assets.keys.map { |k| /(#{asset_name})/.match(k.to_s) }.compact
|
43
|
+
@sorted_asset_keys.find { |k| k.include?(asset_name) }
|
53
44
|
end
|
54
45
|
|
55
46
|
# Internal: Recursively descends through current_paths reading each file it
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
1
3
|
module InlineSvg
|
2
4
|
class IdGenerator
|
3
5
|
class Randomness
|
@@ -9,7 +11,7 @@ module InlineSvg
|
|
9
11
|
|
10
12
|
def self.generate(base, salt, randomness: Randomness)
|
11
13
|
bytes = Digest::SHA1.digest("#{base}-#{salt}-#{randomness.call}")
|
12
|
-
Digest.hexencode(bytes).to_i(16).to_s(36)
|
14
|
+
'a' + Digest.hexencode(bytes).to_i(16).to_s(36)
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module InlineSvg
|
2
|
+
class PropshaftAssetFinder
|
3
|
+
def self.find_asset(filename)
|
4
|
+
new(filename)
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(filename)
|
8
|
+
@filename = filename
|
9
|
+
end
|
10
|
+
|
11
|
+
def pathname
|
12
|
+
asset_path = ::Rails.application.assets.load_path.find(@filename)
|
13
|
+
asset_path.path unless asset_path.nil?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/inline_svg/railtie.rb
CHANGED
@@ -10,11 +10,12 @@ module InlineSvg
|
|
10
10
|
|
11
11
|
config.after_initialize do |app|
|
12
12
|
InlineSvg.configure do |config|
|
13
|
-
#
|
14
|
-
# Sprockets::Environment instance
|
13
|
+
# Configure the asset_finder:
|
15
14
|
# Only set this when a user-configured asset finder has not been
|
16
15
|
# configured already.
|
17
16
|
if config.asset_finder.nil?
|
17
|
+
# In default Rails apps, this will be a fully operational
|
18
|
+
# Sprockets::Environment instance
|
18
19
|
config.asset_finder = app.instance_variable_get(:@assets)
|
19
20
|
end
|
20
21
|
end
|
@@ -1,7 +1,9 @@
|
|
1
|
+
require "pathname"
|
2
|
+
|
1
3
|
# Naive fallback asset finder for when sprockets >= 3.0 &&
|
2
4
|
# config.assets.precompile = false
|
3
5
|
# Thanks to @ryanswood for the original code:
|
4
|
-
# https://github.com/
|
6
|
+
# https://github.com/jamesmartin/inline_svg/commit/661bbb3bef7d1b4bd6ccd63f5f018305797b9509
|
5
7
|
module InlineSvg
|
6
8
|
class StaticAssetFinder
|
7
9
|
def self.find_asset(filename)
|
@@ -14,7 +16,8 @@ module InlineSvg
|
|
14
16
|
|
15
17
|
def pathname
|
16
18
|
if ::Rails.application.config.assets.compile
|
17
|
-
::Rails.application.assets[@filename]
|
19
|
+
asset = ::Rails.application.assets[@filename]
|
20
|
+
Pathname.new(asset.filename) if asset.present?
|
18
21
|
else
|
19
22
|
manifest = ::Rails.application.assets_manifest
|
20
23
|
asset_path = manifest.assets[@filename]
|
@@ -10,11 +10,12 @@ module InlineSvg::TransformPipeline::Transformations
|
|
10
10
|
class: { transform: ClassAttribute },
|
11
11
|
style: { transform: StyleAttribute },
|
12
12
|
data: { transform: DataAttributes },
|
13
|
-
height: { transform: Height },
|
14
13
|
nocomment: { transform: NoComment },
|
15
14
|
preserve_aspect_ratio: { transform: PreserveAspectRatio },
|
16
15
|
size: { transform: Size },
|
17
16
|
width: { transform: Width },
|
17
|
+
height: { transform: Height },
|
18
|
+
view_box: { transform: ViewBox },
|
18
19
|
}
|
19
20
|
end
|
20
21
|
|
@@ -40,8 +41,11 @@ module InlineSvg::TransformPipeline::Transformations
|
|
40
41
|
end
|
41
42
|
|
42
43
|
def self.lookup(transform_params)
|
44
|
+
return [] unless transform_params.any? || custom_transformations.any?
|
45
|
+
|
46
|
+
transform_params_with_defaults = params_with_defaults(transform_params)
|
43
47
|
all_transformations.map { |name, definition|
|
44
|
-
value =
|
48
|
+
value = transform_params_with_defaults[name]
|
45
49
|
definition.fetch(:transform, no_transform).create_with_value(value) if value
|
46
50
|
}.compact
|
47
51
|
end
|
@@ -80,8 +84,9 @@ require 'inline_svg/transform_pipeline/transformations/description'
|
|
80
84
|
require 'inline_svg/transform_pipeline/transformations/size'
|
81
85
|
require 'inline_svg/transform_pipeline/transformations/height'
|
82
86
|
require 'inline_svg/transform_pipeline/transformations/width'
|
87
|
+
require 'inline_svg/transform_pipeline/transformations/view_box'
|
83
88
|
require 'inline_svg/transform_pipeline/transformations/id_attribute'
|
84
89
|
require 'inline_svg/transform_pipeline/transformations/data_attributes'
|
85
90
|
require 'inline_svg/transform_pipeline/transformations/preserve_aspect_ratio'
|
86
91
|
require 'inline_svg/transform_pipeline/transformations/aria_attributes'
|
87
|
-
require "inline_svg/transform_pipeline/transformations/aria_hidden_attribute"
|
92
|
+
require "inline_svg/transform_pipeline/transformations/aria_hidden_attribute"
|