Skip to content

Commit ca342c1

Browse files
committed
Initial - basic buffer-based version. No frills
0 parents  commit ca342c1

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
var through = require('through2');
3+
var gutil = require('gulp-util');
4+
5+
const PLUGIN_NAME = 'gulp-css-urlencode-inline-svgs';
6+
const delimiter = '}\n';
7+
const detect = /data:image\/svg\+xml\;utf8/g;
8+
const target = /<svg[^;]+\<\/svg\>/g;
9+
10+
function urlencodeSvgInText (input) {
11+
var lines = input.toString().split(delimiter);
12+
13+
for (var i in lines) {
14+
if (lines[i].match(detect)) {
15+
lines[i] = lines[i].replace(target, encodeURIComponent);
16+
}
17+
}
18+
19+
return lines.join(delimiter);
20+
}
21+
22+
module.exports = function gulpCssUrlencodeInlineSvgs () {
23+
return through.obj(function(file, enc, cb) {
24+
if (file.isNull()) {
25+
return cb(null, file);
26+
}
27+
28+
if (file.isStream()) {
29+
cb(new gutil.PluginError(PLUGIN_NAME, 'No support for streams.'));
30+
}
31+
32+
if (file.isBuffer()) {
33+
file.contents = new Buffer( urlencodeSvgInText(file.contents) );
34+
}
35+
36+
cb(null, file);
37+
});
38+
}

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "gulp-css-urlencode-inline-svgs",
3+
"version": "0.0.5",
4+
"description": "Makes CSS-inlined data:image/xml+svg compatible with Firefox and IE",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "lakmeer (github.com/lakmeer)",
10+
"license": "MIT",
11+
"dependencies": {
12+
"gulp-util": "^3.0.6",
13+
"through2": "^2.0.0"
14+
}
15+
}

readme.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# `gulp-css-urlencode-inline-svgs`
3+
4+
Data URIs make sprites easier. SVGs make resolution independence easier.
5+
Putting them together seems like an obvious win. You might think that because
6+
the contents of an SVG is just XML text, you could just paste them right into
7+
your stylesheets! But the spec says that `data:image/xml+svg` must be URL
8+
encoded. So you still need to pass them through an encoding stage like you
9+
would for base64'ing other image types.
10+
11+
Here's a gulp plugin that does that. It works on plain CSS so put it in your
12+
pipe after your compiler and autoprefixer, but before your minifier.
13+
14+
15+
## Example Usage
16+
17+
``` javascript
18+
var urlencode = require('gulp-css-urlencode-inline-svgs');
19+
20+
gulp.task('styles', function () {
21+
gulp.src('src/styles/index.scss')
22+
.pipe(sass())
23+
.pipe(prefix())
24+
.pipe(urlencode()) // New hotness
25+
.pipe(cssmin())
26+
.pipe(gulp.dest('public'));
27+
});
28+
```
29+
30+
#### Input
31+
32+
``` css
33+
background-image: url('data:image/svg+xml;utf8, <svg version="1.1" ... etc
34+
```
35+
36+
#### Output
37+
38+
``` css
39+
background-image: url('data:image/svg+xml;utf8, %3Csvg%20version%3D%22 ... etc
40+
```
41+
42+
## Features
43+
44+
- Enables cross-browser compatibility for inlining SVG data-URIs
45+
- That's the only thing it does, because that's what gulp plugins are supposed to do.
46+
- It also has the most unwieldy name of any gulp plugin, so there's also that.
47+
48+
## TODO
49+
50+
- Support streams? I guess
51+
- Default line delimiter is `}\n`. Might want to provide a different one.
52+
- Needs tests for proper plugin compliance.
53+

0 commit comments

Comments
 (0)