Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Using media types instead of filename extension

  • Loading branch information...
commit 1cd1265b4c43bf64419229c4d892c1b5ab7cfab4 1 parent 3fadd57
@trygve-lie trygve-lie authored
View
17 lib/css-b64-images.js
@@ -3,7 +3,20 @@ var fs = require('fs'),
/* Adapted from https://gist.github.com/2594980 */
imgRegex = /url\s?\(['"]?(.*?)(?=['"]?\))/gi,
absoluteUrlRegex = /^\//,
- externalUrlRegex = /http/;
+ externalUrlRegex = /http/,
+ mediatypes = {
+ 'eot' : 'application/vnd.ms-fontobject',
+ 'gif' : 'image/gif',
+ 'ico' : 'image/vnd.microsoft.icon',
+ 'jpg' : 'image/jpeg',
+ 'jpeg' : 'image/jpeg',
+ 'otf' : 'application/x-font-opentype',
+ 'png' : 'image/png',
+ 'svg' : 'image/svg+xml',
+ 'ttf' : 'application/x-font-ttf',
+ 'webp' : 'image/webp',
+ 'woff' : 'application/x-font-woff'
+ };
module.exports = {
fromFile: fromFile,
@@ -57,7 +70,7 @@ function replaceUrlByB64(imageUrl, imagePath, css, cb){
fs.readFile(imagePath, 'base64', function(err, img){
if(err) return cb(err, css);
var ext = imagePath.substr(imagePath.lastIndexOf('.') + 1);
- var newCss = css.replace(imageUrl, 'data:image/' + ext + ';base64,' + img);
+ var newCss = css.replace(imageUrl, 'data:' + mediatypes[ext] + ';base64,' + img);
cb(null, newCss);
});
});
View
2  test/css-b64-images-test.js
@@ -33,4 +33,6 @@ function cssShouldBeCorrect(css){
css.should.include(".external {\n background: url('http");
css.should.include(".tooBig {\n background: url('../img");
css.should.include(".not-found {\n background: url('../img");
+
+ css.should.include(".mediatype {\n background: url('data:image/svg+xml;base64,");
}
View
4 test/fixture/css/style.css
@@ -46,3 +46,7 @@
.not-found {
background: url('../img/nlabal.png');
}
+
+.mediatype {
+ background: url('../img/dots.svg');
+}
View
6 test/fixture/img/dots.svg
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg viewBox="0 0 32 32" height="100%" width="100%" version="1.1" xmlns="http://www.w3.org/2000/svg" xml:space="preserve">
+<rect id="rect4" width="32" fill="#8f5902" height="32"/>
+<circle id="circle6" cy="8" cx="8" r="8" fill="#e9b96e"/>
+<circle id="circle8" cy="24" cx="24" r="8" fill="#c17d11"/>
+</svg>
Please sign in to comment.
Something went wrong with that request. Please try again.