Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First embed version.

  • Loading branch information...
commit c92702b46e34dbf2818cf2b953fd214c215a197c 0 parents
@jakubpawlowicz authored
9 Makefile
@@ -0,0 +1,9 @@
+TEST_DIR = test
+
+all: test
+
+test:
+ @@echo "Running all tests via vows"
+ @@vows ${TEST_DIR}/*-test.js
+
+.PHONY: all test
9 embed.js
@@ -0,0 +1,9 @@
+var buf = new Buffer("abc123");
+console.log(buf.toString());
+console.log(buf.toString('base64'));
+
+require('fs').readFile('gradient.png', function(error, text) {
+ if (error) throw error;
+ console.log(text.toString('base64'));
+ // console.log(new Buffer(text));
+});
29 lib/enhance.js
@@ -0,0 +1,29 @@
+var fs = require('fs'),
+ querystring = require('querystring');
+
+exports.enhance = {
+ process: function(css, options) {
+ return css.replace(/url\(([^\)]+)\)/g, function(match, url) {
+ var tokens = url.split('?');
+ var imagePath = tokens[0];
+ var query = querystring.parse(tokens[1] || '');
+ if (query['embed'] === undefined) return match;
+
+ var pathTokens = imagePath.split('.');
+ var extension = pathTokens[pathTokens.length - 1];
+ var type = '';
+ switch (extension) {
+ case 'jpg':
+ case 'jpeg':
+ type = 'jpeg';
+ break;
+ case 'png':
+ type = 'png';
+ }
+
+ var base64 = fs.readFileSync(options.rootPath + imagePath).toString('base64');
+
+ return "url(data:image/" + type + ";base64," + base64 + ")";
+ });
+ }
+};
BIN  test/data/gradient.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/data/gradient.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/data/gradient.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 test/embed-test.js
@@ -0,0 +1,33 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ fs = require('fs'),
+ enhance = require('../lib/enhance.js').enhance;
+
+var runOn = function(css) {
+ return enhance.process(css, { rootPath: process.cwd() });
+};
+
+var base64 = function(imageName) {
+ return fs.readFileSync(process.cwd() + '/test/data/' + imageName).toString('base64');
+};
+
+vows.describe('embedding images').addBatch({
+ 'no embed': {
+ topic: 'a{background:url(/test/data/gradient.jpg);}',
+ 'for no ?embed parameter': function(css) {
+ assert.equal(runOn(css), css);
+ }
+ },
+ 'one jpeg': {
+ topic: 'a{background:url(/test/data/gradient.jpg?embed)}',
+ 'should give Base64 embedded content': function(css) {
+ assert.equal(runOn(css), "a{background:url(data:image/jpeg;base64," + base64('gradient.jpg') + ")}")
+ }
+ },
+ 'one png': {
+ topic: 'a{background:url(/test/data/gradient.png?embed)}',
+ 'should give Base64 embedded content': function(css) {
+ assert.equal(runOn(css), "a{background:url(data:image/png;base64," + base64('gradient.png') + ")}")
+ }
+ }
+}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.