Skip to content

Commit

Permalink
add fromString
Browse files Browse the repository at this point in the history
  • Loading branch information
Filirom1 committed Jun 18, 2012
1 parent 29d589c commit 2107d0b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 39 deletions.
22 changes: 20 additions & 2 deletions README.md
Expand Up @@ -42,14 +42,32 @@ Do not work with (a warning is shown, but the process continue)

## As a library

optimizeFile(cssFile, root, cb)
### From File

fromFile(cssFile, root, cb)

You must specify the `root` path for absolute URLs to work.

var b64img = require('css-b64-images');

b64img('/your/www/root/dir/css/your-stylesheet.css', '/your/www/root/dir/', function(err, css){
b64img.fromFile('/your/www/root/dir/css/your-stylesheet.css', '/your/www/root/dir/', function(err, css){
if(err) console.error('Error:', err);
console.log(css);
});

### From String

fromString(css, relativePath, rootPath , cb)

var b64img = require('css-b64-images');
var css = fs.readFileSync('/your/www/root/dir/css/your-stylesheet.css');

b64img.fromString(css, '/your/www/root/dir/css/', '/your/www/root/dir/', function(err, css){
if(err) console.error('Error:', err);
console.log(css);
});


## LICENSE

MIT
2 changes: 1 addition & 1 deletion bin/css-b64-images
Expand Up @@ -6,7 +6,7 @@ var b64img = require('..'),

if(!cssFile) handleError(new Error('Usage: css-b64-images file.css'));

b64img(cssFile, root, function(err, css){
b64img.fromFile(cssFile, root, function(err, css){
if(err) console.error('Error:', err);
console.log(css);
});
Expand Down
59 changes: 33 additions & 26 deletions lib/css-b64-images.js
Expand Up @@ -5,38 +5,45 @@ var fs = require('fs'),
absoluteUrlRegex = /^\//,
externalUrlRegex = /http/;

module.exports = optimizeFile;
module.exports = {
fromFile: fromFile,
fromString: fromString
};

function optimizeFile(cssFile, root, cb) {
fs.readFile(cssFile, function(err, css){
function fromString(css, relativePath, rootPath , cb) {
if(!css.replace && css.toString) css = css.toString();
var urls = [], match;
while (match = imgRegex.exec(css)) {
urls.push(match[1]);
}
forEachSeries(urls, base64img, function(err){
if(err) return cb(err, css);
css = css.toString();
var urls = [], match;
while (match = imgRegex.exec(css)) {
urls.push(match[1]);
cb(null, css);
});

function base64img(imageUrl, cb){
if(externalUrlRegex.test(imageUrl)) {
return cb(new Error('Skip ' + imageUrl + ' External file.'), css);
}
forEachSeries(urls, base64img, function(err){

var imagePath;
if(absoluteUrlRegex.test(imageUrl)) {
imagePath = Path.join(root, imageUrl.substr(1));
}else{
imagePath = Path.join(relativePath, imageUrl);
}
replaceUrlByB64(imageUrl, imagePath, css, function (err, newCss){
if(err) return cb(err, css);
cb(null, css);
css = newCss;
cb();
});
}
}

function base64img(imageUrl, cb){
if(externalUrlRegex.test(imageUrl)) {
return cb(new Error('Skip ' + imageUrl + ' External file.'), css);
}

var imagePath;
if(absoluteUrlRegex.test(imageUrl)) {
imagePath = Path.join(root, imageUrl.substr(1));
}else{
imagePath = Path.join(Path.dirname(cssFile), imageUrl);
}
replaceUrlByB64(imageUrl, imagePath, css, function (err, newCss){
if(err) return cb(err, css);
css = newCss;
cb();
});
}
function fromFile(cssFile, root, cb) {
fs.readFile(cssFile, function(err, css){
if(err) return cb(err, css);
fromString(css.toString(), Path.dirname(cssFile), root, cb);
});
}

Expand Down
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Filirom1 <filirom1@gmail.com>",
"name": "css-b64-images",
"description": "Base64 images in your css",
"version": "0.1.0",
"version": "0.2.0",
"main": "lib/css-b64-images.js",
"bin": "bin/css-b64-images",
"repository": {
Expand All @@ -19,6 +19,10 @@
"scripts": {
"test": "mocha"
},
"licenses": [{
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
}],
"optionalDependencies": {},
"engines": {
"node": "*"
Expand Down
33 changes: 24 additions & 9 deletions test/css-b64-images-test.js
@@ -1,21 +1,36 @@
var Path = require('path'),
fs = require('fs'),
b64 = require('..');

require('should');

describe('A tricky CSS', function(){
describe('A complex CSS', function(){
var cssFile = Path.join(__dirname, 'fixture', 'css', 'style.css'),
relative = Path.join(__dirname, 'fixture', 'css');
root = Path.join(__dirname, 'fixture');
it('should be optimized with base64', function(done){
b64(cssFile, root, function(err, css){
css.should.include(".single-quote {\n background: url('data:image/gif;base64,");
css.should.include(".double-quote {\n background: url(\"data:image/gif;base64,");
css.should.include(".absolute {\n background: url('data:image/gif;base64,");

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");
it('a file should be optimized with base64', function(done){
b64.fromFile(cssFile, root, function(err, css){
cssShouldBeCorrect(css);
done();
});
});

it('a string should be optimized with base64', function(done){
var css = fs.readFileSync(cssFile);
b64.fromString(css, relative, root, function(err, css){
cssShouldBeCorrect(css);
done();
});
});
});

function cssShouldBeCorrect(css){
css.should.include(".single-quote {\n background: url('data:image/gif;base64,");
css.should.include(".double-quote {\n background: url(\"data:image/gif;base64,");
css.should.include(".absolute {\n background: url('data:image/gif;base64,");

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");
}

0 comments on commit 2107d0b

Please sign in to comment.