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

ignore arbitrary @import in comment #156

Merged
merged 1 commit into from
Oct 28, 2013

Conversation

altschuler
Copy link
Contributor

@import used in comments without "url (..." causes "Broken @import declaration" exceptions to be thrown.

It's probably not the most efficient solution as it saves all comment positions in memory, but it solved the problem.


options.relativeTo = options.relativeTo || options.root;
options._baseRelativeTo = options._baseRelativeTo || options.relativeTo;
options.visited = options.visited || [];

comments = data.match(/(\/\*(?!\*\/)[\s\S]*?\*\/)|(\/\/[^\n]*)/g);
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a nice regexp! We probably should lazy load this stuff in the loop below as if there are no @import statements then scanning whole data for comments is pointless.

@GoalSmashers
Copy link
Contributor

Good stuff, thanks for spending time on implementing it!

We need to check how it affects performance in real world scenarios too as the current bench task (npm run bench) uses a file without a single @import statement. It should not be a big deal as inlining a file from disk should dwarf this check but we need to make sure.

@altschuler
Copy link
Contributor Author

It's now fully lazy, and will only search for comments if any @import are found. Also it sets a flag if no comments are found, so that it won't keep trying to find comments if more @import are found.

What would you like to do regarding performance test? Should we add a test file with imports in it? It would make sense to measure that in the benchmark anyway I guess :)

correctedEndIndex = endIndex + lastEndIndex;

if (correctedEndIndex < idx)
return scanner(idx);
Copy link
Contributor

Choose a reason for hiding this comment

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

Spend a little while trying to understand the rewrite, but so far no luck with this line - what is the point re-running scanner with the same idx as the original one? This is probably to mitigate intermediate comments which does not have an @import inside them.

It could probably lead to stack overflow exception unless there is something tricky in it :-)

@GoalSmashers
Copy link
Contributor

Good stuff with making it lazy! Most @imports should end up on the top of the file so it should not have much impact.

We currently use test/data/big.css for benchmarking but it should have some @import statements too so we can see effect of such changes. Let's add it to master first so you can rebase and compare before and after your changes.

@altschuler
Copy link
Contributor Author

That's a really sharp catch! I accidentally forgot to move the data chopping statement during refactoring. Now data is chopped before the scan is re-run, which makes the search start where the previous execution ended. I added a test case for this.

Sounds good about benchmarking. Let me know if there's something I can do about it.

@GoalSmashers
Copy link
Contributor

Looks great now!

If you don't mind I'll add the @import to benchmarking tomorrow morning (already have an idea how to reorganize it) so you can rebase and see how it performs. Then we'll merge your code to master.

Do you consider it as a patch to 1.1 or should we ship it with upcoming 2.0 only?

On Oct 27, 2013, at 14:34, Simon Altschuler notifications@github.com wrote:

That's a really sharp catch! I accidentally forgot to move the data chopping statement during refactoring. Now data is chopped before the scan is re-run, which makes the search start where the previous execution ended. I added a test case for this.

Sounds good about benchmarking. Let me know if there's something I can do about it.


Reply to this email directly or view it on GitHub.

@altschuler
Copy link
Contributor Author

Great, thanks! I'd say this is more of a patch, since it's not a feature, but rather a fix, so a patch to 1.1 seems fit :)

@GoalSmashers
Copy link
Contributor

I agree. Once merged we will push 1.1.7 out. Stay tuned for updates.

On 27 Oct 2013, at 16:42, Simon Altschuler notifications@github.com wrote:

Great, thanks! I'd say this is more of a patch, since it's not a feature, but rather a fix, so a patch to 1.1 seems fit :)


Reply to this email directly or view it on GitHub.

@GoalSmashers
Copy link
Contributor

@altschuler please take a look at ef54fc0 - it now uses test/data-bench/complex.css to import a few files from test/data directory.

I applied this patch to your HEAD, then put a random @import inside some comments in test/data/big.css. It works fine for comments at the beginning of that file but if I go below line 28 (take 33 for example) then it starts to fail. No idea why as I haven't spent much time on it.

@altschuler
Copy link
Contributor Author

Yes, I can reproduce this, seems a bit odd. I'll take a deeper look later today.

@GoalSmashers
Copy link
Contributor

Cool! It may be helpful to trim big.css on line ~ 40 and deal with a much shorter file.

@altschuler
Copy link
Contributor Author

I've fixed this now and updated my branch (rebased from master). The bug was somewhat subtle but I find it rather odd that it worked at all. The code is a bit more verbose now, but should be easier to understand and more manageable :)

I tested it with many different imports in different places. I also couldn't measure any performance impact, event with loads of "fake" imports.

@GoalSmashers
Copy link
Contributor

@altschuler looks great!

Two minor things and we are done:

  • can you remove extra space in lines 42 and 48 so it reads function(idx)? Consistency is king!
  • can you squash all your commits into one? Clean git history is king too!

@altschuler
Copy link
Contributor Author

Agreed :) all done

@GoalSmashers
Copy link
Contributor

Sorry, one more thing - can you prepend commit's message with Fixes #156 -. We always reference issues plus GitHub will close it nicely once merged.

👍

GoalSmashers pushed a commit that referenced this pull request Oct 28, 2013
Ignore arbitrary @import in comments.
@GoalSmashers GoalSmashers merged commit 20fc422 into clean-css:master Oct 28, 2013
@GoalSmashers
Copy link
Contributor

@altschuler 1.1.7 is out with your patch. Many thanks for the important contribution!

@altschuler
Copy link
Contributor Author

Glad I could help!

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.

None yet

2 participants