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

Conversation

zachrose
Copy link

@zachrose zachrose commented Oct 9, 2014

No description provided.

@jonkemp
Copy link
Owner

jonkemp commented Oct 9, 2014

I'm not clear on what the problem and what the fix does.

Also, is there anyway to add tests for this?

@zachrose
Copy link
Author

zachrose commented Oct 9, 2014

Sure thing. I've just added a test that hopefully explains the issue. (Take out JSON.parse/JSON.stringify in index.js to see the test fail.)

I'm personally not sure how on how to add a test that in effect says, "also don't do this other thing that was never expected in the first place," but for now I've just added it as a second test case with its own fixtures. I welcome any suggestions on how to do this better.

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.

@jonkemp
Copy link
Owner

jonkemp commented Oct 9, 2014

Ah, I see what the issue is. Thanks for pointing that out.

I don't see the need to immediately stringify and then parse the args. Why did you add that?

@zachrose
Copy link
Author

JSON.parse(JSON.stringify(object)) is just the 5-cent way to clone an object in JavaScript. Objects are passed by reference, so if I do:

foo = {a: 2};
bar = foo;
bar.a = 1;
assert(foo.a == 2);

...you will see that foo.a is not 2, because bar is just foo by another name. Cloning is the only way to get a copy of foo that doesn't mess with bar. There are many suggested ways to clone an object in JavaScript. My usual way to do it is to require Underscore and do this:

var _ = require('underscore')
var original = {a: 1};
var clone = _.extend({}, original);

...and I would be happy to do that here instead if you prefer.

@jonkemp
Copy link
Owner

jonkemp commented Oct 10, 2014

Ok. Please squash the commits. Then I'll merge it.

@jonkemp
Copy link
Owner

jonkemp commented Oct 30, 2014

Thanks for tracking this down and providing a fix. This issue has been resolved outside of this PR however, so this is no longer needed. Closing.

@jonkemp jonkemp closed this Oct 30, 2014
@zachrose
Copy link
Author

Ah, sorry I forgot to get to it. Thanks for closing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants