-
Notifications
You must be signed in to change notification settings - Fork 35
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
Handle multiple semicolons to prevent csso crash, fixes #243 #244
Conversation
src/run.js
Outdated
@@ -218,6 +218,10 @@ const processPage = ({ | |||
redirectResponses[responseUrl] = redirectsTo; | |||
} else if (resourceType === 'stylesheet') { | |||
response.text().then(text => { | |||
// Double semicolons can crash csso. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment with a link to css/csso#378
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a unit test. The unit test needs to also explain and mention css/csso#378
tests/examples/extra-semicolons.css
Outdated
@@ -0,0 +1,11 @@ | |||
a { | |||
color: blue;;;;background-color: red;; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really necessary to test all of these different occurrences of double semi-colons? Isn't the error just going to happen on 1/the first of them?
I'm guessing you're working on a unit test too. |
Whoops, guess I should probably have used some fancypants git commands to reduce the commit-spam :) |
src/run.js
Outdated
// Semicolon sequences can crash CSSO, so we remove them. | ||
// https://github.com/peterbe/minimalcss/issues/243 | ||
// https://github.com/css/csso/issues/378 | ||
while (/;\s*;/.test(text)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have file src/utils.js
you can place this function there with explanations in jsdoc
// Extra semicolons can cause csso.minify() to throw: | ||
// [TypeError: Cannot read property '0' of undefined] | ||
// https://github.com/peterbe/minimalcss/issues/243 | ||
// https://github.com/css/csso/issues/378 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess
const { finalCss } = await runMinimalcss('extra-semicolons');
expect(finalCss).toMatch('a{color:red}');
would be enough for test, no need check if exception undefined if we check return value
Can you please squash commits and change message to:
|
unit tests added along with explanation about csso
Thanks, @stereobooster. I've made the changes you suggested. I'm not sure how to squash commits, though. Is that an option you can select when merging? |
https://stackoverflow.com/a/5189600 this is what I meant by squash commits. And yes it is also possible to squash at merge via button in the interface |
I've been editing this directly on github (which no longer seems like a brilliant idea) so I don't have a local branch to rebase/squash. Can I leave the squashing to you? |
src/run.js
Outdated
@@ -218,6 +218,7 @@ const processPage = ({ | |||
redirectResponses[responseUrl] = redirectsTo; | |||
} else if (resourceType === 'stylesheet') { | |||
response.text().then(text => { | |||
text = utils.removeSequentialSemis(text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wonderful comments you put above the utility function removeSequentialSemis
belongs here instead. That utility function doesn't need a hack-explanation since it's just a function. A great function for that matter!
Also, when you move that comment can you make the URLs complete. I.e. add the https://
so that they become clickable URLs in editors and github.
src/utils.js
Outdated
/** | ||
* Semicolon sequences can crash CSSO, so we remove them from CSS text. | ||
* github.com/peterbe/minimalcss/issues/243 | ||
* github.com/css/csso/issues/378 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned above, these comments belong to where this function is used. But please put something else in here. The code could do with a really simple function like Replaces all occurances of a ';'<0 or more whitespace>';' to just a single ';'.
css = css.replace(/;\s*;/g, ';'); | ||
} | ||
return css; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, if you feel like you want to perfect this PR, since that string function got uplifted into its own function, can you add a simple test for it.
At the moment the utils.test.js
doesn't exist but it soon will hopefully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do feel like perfecting this PR, but I have a vague suspicion that creating utils.test.js
in two separate branches might introduce some kind of merge conflict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you rebase your branch against origin/master you should have a file now that you can add to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebase without squash that is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That didn't go as planned...
I think it's going to be simpler for me to submit another pull request, replacing this one.
Squashing into master...jc275:remove-sequential-semicolons
This throws an error if invalid CSS is found during `csstree.walk`. I couldn't find a nice way to output the line or surrounding nodes to help the dev.
Lines 132-143 are moved outside the if-block, but are otherwise unchanged.
<p>This Pull Request updates dependency <a href="https://renovatebot.com/gh/facebook/jest">jest</a> from <code>v23.4.2</code> to <code>v23.5.0</code></p> <p><details><br /> <summary>Release Notes</summary></p> <h3 id="v2350httpsgithubcomfacebookjestblobmasterchangelogmd82032350"><a href="https://renovatebot.com/gh/facebook/jest/blob/master/CHANGELOG.md#​2350"><code>v23.5.0</code></a></h3> <p><a href="https://renovatebot.com/gh/facebook/jest/compare/v23.4.2…v23.5.0">Compare Source</a></p> <h5 id="features">Features</h5> <ul> <li><code>[jest-cli]</code> Add package name to <code>NotifyReporter</code> notification (<a href="https://renovatebot.com/gh/facebook/jest/pull/5898">#​5898</a>)</li> <li><code>[jest-runner]</code> print stack trace when <code>process.exit</code> is called from user code (<a href="https://renovatebot.com/gh/facebook/jest/pull/6714">#​6714</a>)</li> <li><code>[jest-each]</code> introduces <code>%#</code> option to add index of the test to its title (<a href="https://renovatebot.com/gh/facebook/jest/pull/6414">#​6414</a>)</li> <li><code>[pretty-format]</code> Support serializing <code>DocumentFragment</code> (<a href="https://renovatebot.com/gh/facebook/jest/pull/6705">#​6705</a>)</li> <li><code>[jest-validate]</code> Add <code>recursive</code> and <code>recursiveBlacklist</code> options for deep config checks (<a href="https://renovatebot.com/gh/facebook/jest/pull/6802">#​6802</a>)</li> <li><code>[jest-cli]</code> Check watch plugins for key conflicts (<a href="https://renovatebot.com/gh/facebook/jest/pull/6697">#​6697</a>)</li> </ul> <h5 id="fixes">Fixes</h5> <ul> <li><code>[jest-snapshot</code> Mark snapshots as obsolete when moved to an inline snapshot (<a href="https://renovatebot.com/gh/facebook/jest/pull/6773">#​6773</a>)</li> <li><code>[jest-config]</code> Fix <code>--coverage</code> with <code>--findRelatedTests</code> overwriting <code>collectCoverageFrom</code> options (<a href="https://renovatebot.com/gh/facebook/jest/pull/6736">#​6736</a>)</li> <li><code>[jest-config]</code> Update default config for testURL from 'about:blank' to '<a href="http://localhost">http://localhost</a>' to address latest JSDOM security warning. (<a href="https://renovatebot.com/gh/facebook/jest/pull/6792">#​6792</a>)</li> <li><code>[jest-cli]</code> Fix <code>testMatch</code> not working with negations (<a href="https://renovatebot.com/gh/facebook/jest/pull/6648">#​6648</a>)</li> <li><code>[jest-cli]</code> Don't report promises as open handles (<a href="https://renovatebot.com/gh/facebook/jest/pull/6716">#​6716</a>)</li> <li><code>[jest-each]</code> Add timeout support to parameterised tests (<a href="https://renovatebot.com/gh/facebook/jest/pull/6660">#​6660</a>)</li> <li><code>[jest-cli]</code> Improve the message when running coverage while there are no files matching global threshold (<a href="https://renovatebot.com/gh/facebook/jest/pull/6334">#​6334</a>)</li> <li><code>[jest-snapshot]</code> Correctly merge property matchers with the rest of the snapshot in <code>toMatchSnapshot</code>. (<a href="https://renovatebot.com/gh/facebook/jest/pull/6528">#​6528</a>)</li> <li><code>[jest-snapshot]</code> Add error messages for invalid property matchers. (<a href="https://renovatebot.com/gh/facebook/jest/pull/6528">#​6528</a>)</li> <li><code>[jest-cli]</code> Show open handles from inside test files as well (<a href="https://renovatebot.com/gh/facebook/jest/pull/6263">#​6263</a>)</li> <li><code>[jest-haste-map]</code> Fix a problem where creating folders ending with <code>.js</code> could cause a crash (<a href="https://renovatebot.com/gh/facebook/jest/pull/6818">#​6818</a>)</li> </ul> <h5 id="chore--maintenance">Chore & Maintenance</h5> <ul> <li><code>[docs]</code> Document another option to avoid warnings with React 16 (<a href="https://renovatebot.com/gh/facebook/jest/issues/5258">#​5258</a>)</li> </ul> <h5 id="chore--maintenance-1">Chore & Maintenance</h5> <ul> <li><code>[docs]</code> Add note explaining when <code>jest.setTimeout</code> should be called (<a href="https://renovatebot.com/gh/facebook/jest/pull/6817/files">#​6817</a>)</li> </ul> <hr /> <p></details></p> <hr /> <p>This PR has been generated by <a href="https://renovatebot.com">Renovate Bot</a>.</p>
<p>This Pull Request updates devDependency <a href="https://renovatebot.com/gh/fastify/fastify-static">fastify-static</a> from <code>v0.12.0</code> to <code>v0.13.0</code></p> <p><details><br /> <summary>Release Notes</summary></p> <h3 id="v0130httpsgithubcomfastifyfastify-staticreleasesv0130"><a href="https://renovatebot.com/gh/fastify/fastify-static/releases/v0.13.0">v0.13.0</a></h3> <p><a href="https://renovatebot.com/gh/fastify/fastify-static/compare/v0.12.0…v0.13.0">Compare Source</a></p> <ul> <li>Updated readalbe-stream@​3 <a href="https://renovatebot.com/gh/fastify/fastify-static/issues/68">#​68</a> </li> <li>Dropped support for Node 9.</li> </ul> <hr /> <p></details></p> <hr /> <p>This PR has been generated by <a href="https://renovatebot.com">Renovate Bot</a>.</p>
Aside from tests and documentation, this should be the only change needed. Camel-cased `ignoreJsErrors` (and `ignoreCssErrors`) might be more in keeping with the [style guide](https://github.com/peterbe/minimalcss/blob/master/CONTRIBUTING.md#style-guide). peterbe#249
I didn't like that we had extracted out some utility functions but never had *any* tests for them. The most important tests are the tests that use the public API but if the code was sufficiently complex that utility functions had to be broken out, we should have at least some tests for them.
Sorry for the chaos! Closing in favour of #259. Let's try again... |
Fixes #243
There may be a safer way to handle this (e.g. via
css-tree
) rather than naively editing the CSS text... but this works for me :)