Skip to content

Commit

Permalink
Merge pull request #81 from stucox/feature/style-attrs
Browse files Browse the repository at this point in the history
Add support for inlining assets in `style` attributes
  • Loading branch information
remy committed Jan 18, 2016
2 parents a95d94d + 6c9bba2 commit 5ceed81
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
13 changes: 7 additions & 6 deletions lib/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ function getImports(root, css) {
css = css.replace('@import ' + match, importedCSS);
return getImports.call(inliner, root, css);
});
} else {
if (inliner.options.compressCSS) {
inliner.emit('progress', 'compress css');
css = compress(css);
}
return Promise.resolve(css);
}

if (inliner.options.compressCSS) {
inliner.emit('progress', 'compress css');
css = compress(css);
}

return Promise.resolve(css);
}

function compress(css) {
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var tasks = {
svg: 'svg',
links: 'link[rel=stylesheet]',
styles: 'style',
'style-attrs': '[style]:not(svg *)', // only style attrs in HTML, not SVG
images: 'img',
videos: 'video',
};
Expand Down Expand Up @@ -320,11 +321,10 @@ function resolve(from, to) {

if (!this.isFile || from.indexOf('http') === 0) {
return require('url').resolve(from, to);
} else {
var base = path.dirname(from);
return path.resolve(base, to);
}

var base = path.dirname(from);
return path.resolve(base, to);
}

function updateTodo() {
Expand Down
15 changes: 15 additions & 0 deletions lib/tasks/style-attrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = resolve;
var debug = require('debug')('inliner');

function resolve(inliner, todo, $) {
debug('start %s style attributes', todo.length);
return todo.map(function links(style) {
var css = $(style).attr('style');
inliner.emit('progress', 'processing inline css');
return inliner.cssImports(inliner.url, css)
.then(inliner.cssImages.bind(inliner, inliner.url))
.then(function then(css) {
$(style).attr('style', css);
});
});
}
1 change: 1 addition & 0 deletions test/fixtures/image-style-attr.result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>css image in style attr</title> </head> <body> <div style="background:url(data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=) repeat;"></div> <div style="background:url( &apos;data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=&apos; ) repeat;"></div> <div style="background:url(data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=) repeat;"></div> <div style="background:url(&apos;data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=&apos;) repeat;"></div> </body> </html>
13 changes: 13 additions & 0 deletions test/fixtures/image-style-attr.src.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>css image in style attr</title>
</head>
<body>
<div style="background: url(1x1.gif) repeat;"></div>
<div style="background: url( '1x1.gif' ) repeat;"></div>
<div style="background: url(http://localhost:54321/1x1.gif) repeat;"></div>
<div style="background: url('http://localhost:54321/1x1.gif') repeat;"></div>
</body>
</html>

0 comments on commit 5ceed81

Please sign in to comment.