Skip to content

Commit

Permalink
add a data-uri function
Browse files Browse the repository at this point in the history
as described in a comment on #775
  • Loading branch information
jneen authored and lukeapage committed Jan 5, 2013
1 parent d56e0a4 commit 24e4108
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var fs = require('fs');

(function (tree) {

tree.functions = {
Expand Down Expand Up @@ -345,6 +347,18 @@ tree.functions = {
},
shade: function(color, amount) {
return this.mix(this.rgb(0, 0, 0), color, amount);
},
"data-uri": function(mimetype, path) {
var data = fs.readFileSync(path.value);
mimetype = mimetype.value;

if (/;base64$/.test(mimetype)) {
data = (new Buffer(data)).toString('base64');
}

var contents = new(tree.Anonymous)('data:'+mimetype+','+data);

return new(tree.URL)(contents);
}
};

Expand Down
3 changes: 3 additions & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@
average: #7b007b;
negation: #d73131;
}
#data-uri {
background-image: url(data:image/jpeg;base64,bm90IGFjdHVhbGx5IGEganBlZyBmaWxlCg==);
}
1 change: 1 addition & 0 deletions test/data/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@
average: average(#f60000, #0000f6);
negation: negation(#f60000, #313131);
}

#data-uri {
background-image: data-uri('image/jpeg;base64', 'test/data/image.jpg');
}

0 comments on commit 24e4108

Please sign in to comment.