Skip to content

Fix for issue #13: Stylesheet paths are not always relative #14

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

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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var gutil = require('gulp-util');
var through = require('through2');
var juice = require('juice2');

module.exports = function(opt){
module.exports = function(main_opt){
return through.obj(function (file, enc, cb) {
opt = opt || {};
var opt = JSON.parse(JSON.stringify(main_opt || {}));

// 'url' option is required
// set it automatically if not provided
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 12 mutates opt. Because opt was scoped to the exported module itself and not to each call of that module, the first call will mutate the opt.url and the second call will never care about file.path. This change fixes that by keeping main_opt distinct from opt and cloning it rather than mutating a reference.

Expand Down
15 changes: 15 additions & 0 deletions test/expected/multiple/one/out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>


</head>
<body style="font-family: Arial;">
<h1 style="border: 1px solid #ccc; color: blue;">Hi</h1>
<table>
<tr>
<td class="headline" style="font-size: 24px; padding: 5px;">Some Headline</td>
</tr>
</table>
</body>
</html>

15 changes: 15 additions & 0 deletions test/expected/multiple/two/out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>


</head>
<body style="font-family: Arial;">
<h1 style="border: 1px solid #ccc; color: red;">Hi</h1>
<table>
<tr>
<td class="headline" style="font-size: 24px; padding: 5px;">Some Headline</td>
</tr>
</table>
</body>
</html>

13 changes: 13 additions & 0 deletions test/fixtures/multiple/one/file.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
font-family: Arial;
}
h1 {
color: blue;
}
.headline {
font-size: 24px;
}

td {
padding: 5px;
}
18 changes: 18 additions & 0 deletions test/fixtures/multiple/one/in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<style>
h1 {
border: 1px solid #ccc;
}
</style>
<link rel="stylesheet" href="file.css"/>
</head>
<body>
<h1>Hi</h1>
<table>
<tr>
<td class="headline">Some Headline</td>
</tr>
</table>
</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/multiple/two/file.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
font-family: Arial;
}
h1 {
color: red;
}
.headline {
font-size: 24px;
}

td {
padding: 5px;
}
18 changes: 18 additions & 0 deletions test/fixtures/multiple/two/in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<style>
h1 {
border: 1px solid #ccc;
}
</style>
<link rel="stylesheet" href="file.css"/>
</head>
<body>
<h1>Hi</h1>
<table>
<tr>
<td class="headline">Some Headline</td>
</tr>
</table>
</body>
</html>
26 changes: 20 additions & 6 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ var path = require('path');
var gutil = require('gulp-util');
var inlineCss = require('../index');

function compare(input, options, done) {
function compare(inputPath, outputPath, options, done) {

var input = fs.readFileSync(inputPath);

var fakeFile = new gutil.File({
path: path.resolve('./test/fixtures/in.html'),
path: path.resolve('./'+inputPath),
cwd: './test/',
base: './test/fixtures/',
contents: new Buffer(String(input))
});

var output = fs.readFileSync(path.join('test', 'expected', 'out.html'));
var output = fs.readFileSync(outputPath);

// Create a plugin stream
var stream = inlineCss(options);
Expand All @@ -36,8 +39,19 @@ function compare(input, options, done) {

describe('gulp-inline-css', function() {
it('Should convert linked css to inline css', function(done) {
var input = fs.readFileSync(path.join('test', 'fixtures', 'in.html'));
var inputPath = path.join('test', 'fixtures', 'in.html');
var outputPath = path.join('test', 'expected', 'out.html');
var options = {};
compare(input, options, done);
compare(inputPath, outputPath, options, done);
});
});
it('Should handle multiple HTML files with relative stylesheets', function(done){
var one = path.join('test', 'fixtures', 'multiple', 'one', 'in.html');
var two = path.join('test', 'fixtures', 'multiple', 'two', 'in.html');
var options = {};
var doneCount = 0;
var bothDone = function(){ doneCount++; if(doneCount == 2){ done(); } };
compare(one, path.join('test', 'expected', 'multiple', 'one', 'out.html'), options, bothDone);
compare(two, path.join('test', 'expected', 'multiple', 'two', 'out.html'), options, bothDone);
});
});