-
Notifications
You must be signed in to change notification settings - Fork 12
Fix invalid CSS; add assertions that CSS is valid #1554
Conversation
🦋 Changeset detectedLatest commit: eefed91 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
a9f9e91
to
f8c97dd
Compare
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 is a great catch. I have an issue with the fact that this relies on a specific library’s behaviour when parsing CSS.
We want to avoid early abstraction, but as we are already using the transform
four times already, I would strongly suggest creating a helper method like isValidCSS
that is tested with valid and invalid inputs. While I am quite familiar with lightningcss
having used if very recently, I think that the indirection of creating a buffer, specifying a filename and expecting errors to be thrown makes this code harder to maintain.
packages/@guardian/source-foundations/src/accessibility/accessibility.test.ts
Outdated
Show resolved
Hide resolved
packages/@guardian/source-foundations/src/accessibility/accessibility.test.ts
Outdated
Show resolved
Hide resolved
packages/@guardian/source-foundations/src/accessibility/focus-halo.ts
Outdated
Show resolved
Hide resolved
packages/@guardian/source-foundations/src/utils/jest-matchers/toBeValidCSS.ts
Outdated
Show resolved
Hide resolved
/** | ||
* @deprecated this uses the SCSS parent selector. In the future we | ||
* should look to make this just CSS or move into into our main reset. | ||
*/ | ||
// remove styling of invalid input elements that gets applied in Firefox | ||
const input = ` |
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.
are we deprecating the name, the language or the functionality here?
i.e. if this become pure css, what is deprecated about this?
i wonder if we should rename this to rename this to inputSCSS
and re-export it as a deprecated input
with the deprecation message?
/** | |
* @deprecated this uses the SCSS parent selector. In the future we | |
* should look to make this just CSS or move into into our main reset. | |
*/ | |
// remove styling of invalid input elements that gets applied in Firefox | |
const input = ` | |
/** | |
* @deprecated use `resets.inputSCSS` | |
*/ | |
const input = inputSCSS; | |
// remove styling of invalid input elements that gets applied in Firefox | |
const inputSCSS = ` |
that effectively adds a new export (resets.inputSCSS
) while flagging up the old, ambiguous name as something we want to remove.
that would then decouple the misleading name issue from any future work we might want to do on making the reset available as pure 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.
Agreed, that’s a lot clearer - thanks for the suggestion!
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 discussed yday - I've changed input
to just have the type: SerialisedStyles
which should indicate this properly
64840ac
to
2014c02
Compare
7b8b22f
to
a9e08ae
Compare
a9e08ae
to
e561e46
Compare
packages/@guardian/source-foundations/src/typography/font-styles.ts
Outdated
Show resolved
Hide resolved
81c1235
to
1eedd58
Compare
…on't let SCSS/SASS-isms slip into a framework-agnostic library. write tests for the blocks of CSS that we export from foundations to assert that it is valid and parent selectors do not slip in
…get syntax highlighting for our CSS block definitions
…BeValidCSS which wraps the lightningcss to simplify our test cases and make them easier to read
… as a separate pr
…d not come from lightningcss - otherwise return the jest matcher response
…acts on our code and is not part of it
Co-authored-by: Alex Sanders <alex@sndrs.dev>
… changelog around the internal testing as it does not affect the end user Co-authored-by: Alex Sanders <alex.sanders@guardian.co.uk>
…input reset and each of the typography method exports. also assert that these work through tests
1eedd58
to
eefed91
Compare
Closing to reopen as two distinct changes, one for the CSS validity assertions and another for our Emotion exports |
What is the purpose of this change?
In some cases, Emotion CSS syntax has started to slip into
source-foundations
.This means that the latest version of foundations is breaking the build in projects like dotcom rendering (AMP specifically) when upgrading the version where the CSS blocks that we export are being rendered directly to the page.
This change fixes some invalid CSS comments that use
//
vs/* .. */
in our reset which could affect the cascade and adds unit tests which make an assertion using thelightningcss
parser so we have confidence that we are shipping valid CSS.We also clarify the use of the parent selector in
resets.input
and in the Typography module — by making two exports for each font type. The difference is explained below:headline.medium()
— This will now return aSerializedStyles
object, because it returns a parent selector. It is a breaking change to the API because this used to return an invalid CSS string.headlineString.medium()
— This will return a CSS string as before, but without including the parent selector.These assertions are implemented as a custom Jest matcher called
toBeValidCSS
, which accepts an option for when the CSS to check is a fragment and not surrounded by a selector.I was wary of adding a new dependency here, but decided to include this in the pull request proposal because it seems very easy to let invalid CSS syntax slip into what is (I think at least?) a package that should be framework agnostic.
Thanks @SiAdcock & @joecowton1 for a great discussion around this!