Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add fromString

  • Loading branch information...
commit 2107d0bfcc647846988554a86a234cd5fc1244b5 1 parent 29d589c
Filirom1 authored
View
22 README.md
@@ -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
View
2  bin/css-b64-images
@@ -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);
});
View
59 lib/css-b64-images.js
@@ -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);
});
}
View
6 package.json
@@ -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": {
@@ -19,6 +19,10 @@
"scripts": {
"test": "mocha"
},
+ "licenses": [{
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/MIT"
+ }],
"optionalDependencies": {},
"engines": {
"node": "*"
View
33 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");
+}
Please sign in to comment.
Something went wrong with that request. Please try again.