Prevent data-uri function from embedding files larger than 32KB.

Although IE8 does support data-uris, it only does so with a limit of 32KB. It's a silly limitation, but a source of potential bugs. When the limit is exceeded, the data-uri() function will simply return a normal url() value with a relative path to the asset.

One may pass --no-ie-compat to lessc to avoid this safeguard.
This commit is contained in:
Daniel Stockman
2013-02-18 18:54:11 -08:00
committed by Luke Page
parent 7116b3b1c5
commit e4fe935ea1
9 changed files with 39 additions and 0 deletions

View File

@@ -11,6 +11,7 @@
'strictImports', // option -
'dumpLineNumbers', // option - whether to dump line numbers
'compress', // option - whether to compress
'ieCompat', // option - whether to enforce IE compatibility (IE8 data-uri)
'mime', // browser only - mime type for sheet import
'entryPath', // browser only - path of entry less file
'rootFilename', // browser only - href of the entry less file

View File

@@ -416,6 +416,24 @@ tree.functions = {
var buf = fs.readFileSync(filePath);
// IE8 cannot handle a data-uri larger than 32KB. If this is exceeded
// and the --ieCompat flag is enabled, return a normal url() instead.
var DATA_URI_MAX_KB = 32,
fileSizeInKB = parseInt((buf.length / 1024), 10);
if (fileSizeInKB >= DATA_URI_MAX_KB) {
// the url() must be relative, not an absolute file path
filePath = path.relative(this.currentDirectory, filePath);
if (this.env.ieCompat !== false) {
// TODO: respect verbose or silent flags here
console.error("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
return new(tree.URL)(new(tree.Anonymous)(filePath));
} else {
// if explicitly disabled (via --no-ie-compat on CLI, or env.ieCompat === false), merely warn
console.warn("WARNING: Embedding %s (%dKB) exceeds IE8's data-uri size limit of %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
}
}
buf = useBase64 ? buf.toString('base64')
: encodeURIComponent(buf);

View File

@@ -31,6 +31,7 @@ var lessc_helper = {
sys.puts(" -h, --help Print help (this message) and exit.");
sys.puts(" --include-path Set include paths. Separated by `:'. Use `;' on Windows.");
sys.puts(" --no-color Disable colorized output.");
sys.puts(" --no-ie-compat Disable IE compatibility checks.");
sys.puts(" -l, --lint Syntax check only (lint).");
sys.puts(" -s, --silent Suppress output of error messages.");
sys.puts(" --strict-imports Force evaluation of imports.");