Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a data-uri function. #1086

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,40 @@ tree.functions = {
},
shade: function(color, amount) {
return this.mix(this.rgb(0, 0, 0), color, amount);
},
"data-uri": function(mimetype, path) {
if (typeof window !== 'undefined') {
throw new Error('data-uri() is not supported in the browser.');
}

mimetype = mimetype.value;
path = (path && path.value);

var fs = require('fs');
var useBase64 = false;

// detect the mimetype if not given
if (arguments.length < 2) {
var mime = require('mime');
path = mimetype;
mimetype = mime.lookup(path);

// use base 64 unless it's an ASCII or UTF-8 format
var charset = mime.charsets.lookup(mimetype);
useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
if (useBase64) mimetype += ';base64';
}
else {
useBase64 = /;base64$/.test(mimetype)
}

var buf = fs.readFileSync(path);

buf = useBase64 ? buf.toString('base64')
: encodeURIComponent(buf);

var uri = "'data:"+mimetype+','+buf+"'";
return new(tree.URL)(new(tree.Anonymous)(uri));
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"main" : "./lib/less/index",
"directories" : { "test": "./test" },
"engines" : { "node": ">=0.4.2" },
"optionalDependencies" : { "ycssmin": ">=1.0.1" },
"optionalDependencies" : { "ycssmin": ">=1.0.1", "mime": "1.2.x" },
"devDependencies" : { "diff": "~1.0" },
"scripts": {
"test": "make test"
Expand Down
10 changes: 10 additions & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,13 @@
average: #7b007b;
negation: #d73131;
}
#data-uri {
uri: url('data:image/jpeg;base64,bm90IGFjdHVhbGx5IGEganBlZyBmaWxlCg==');
}
#data-uri-guess {
uri: url('data:image/jpeg;base64,bm90IGFjdHVhbGx5IGEganBlZyBmaWxlCg==');
}
#data-uri-ascii {
uri-1: url('data:text/html,%3Ch1%3EThis%20page%20is%20100%25%20Awesome.%3C%2Fh1%3E%0A');
uri-2: url('data:text/html,%3Ch1%3EThis%20page%20is%20100%25%20Awesome.%3C%2Fh1%3E%0A');
}
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.
1 change: 1 addition & 0 deletions test/data/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>This page is 100% Awesome.</h1>
13 changes: 13 additions & 0 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,16 @@
average: average(#f60000, #0000f6);
negation: negation(#f60000, #313131);
}

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

#data-uri-guess {
uri: data-uri('test/data/image.jpg');
}

#data-uri-ascii {
uri-1: data-uri('text/html', 'test/data/page.html');
uri-2: data-uri('test/data/page.html');
}