Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Fix invalid CSS; add assertions that CSS is valid #1554

Closed
wants to merge 16 commits into from

Conversation

ob6160
Copy link
Contributor

@ob6160 ob6160 commented Sep 22, 2022

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 the lightningcss 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 a SerializedStyles 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!

@ob6160 ob6160 requested a review from a team as a code owner September 22, 2022 13:34
@changeset-bot
Copy link

changeset-bot bot commented Sep 22, 2022

🦋 Changeset detected

Latest commit: eefed91

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@guardian/source-foundations Major
@guardian/eslint-plugin-source-foundations Major
@guardian/source-react-components-development-kitchen Major
@guardian/source-react-components Major
@guardian/eslint-plugin-source-react-components Major

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

@github-actions github-actions bot added the foundations Affects @guardian/source-foundations label Sep 22, 2022
@github-actions
Copy link
Contributor

yarn.lock changes

Click to toggle table visibility
Name Status Previous Current
detect-libc ADDED - 1.0.3
lightningcss-darwin-arm64 ADDED - 1.16.0
lightningcss-darwin-x64 ADDED - 1.16.0
lightningcss-linux-arm-gnueabihf ADDED - 1.16.0
lightningcss-linux-arm64-gnu ADDED - 1.16.0
lightningcss-linux-arm64-musl ADDED - 1.16.0
lightningcss-linux-x64-gnu ADDED - 1.16.0
lightningcss-linux-x64-musl ADDED - 1.16.0
lightningcss-win32-x64-msvc ADDED - 1.16.0
lightningcss ADDED - 1.16.0

@ob6160 ob6160 force-pushed the ob/fix-invalid-css branch 2 times, most recently from a9f9e91 to f8c97dd Compare September 22, 2022 13:44
@ob6160
Copy link
Contributor Author

ob6160 commented Sep 22, 2022

mxdvl
mxdvl previously approved these changes Sep 22, 2022
Copy link
Contributor

@mxdvl mxdvl left a 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.

@ob6160 ob6160 requested review from mxdvl and sndrs September 22, 2022 16:05
@ob6160 ob6160 requested review from sndrs and a team September 26, 2022 11:27
@ob6160 ob6160 requested a review from sndrs September 26, 2022 11:38
.changeset/popular-adults-behave.md Outdated Show resolved Hide resolved
Comment on lines 19 to 24
/**
* @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 = `
Copy link
Member

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?

Suggested change
/**
* @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.

Copy link
Contributor Author

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!

Copy link
Contributor Author

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

@github-actions github-actions bot added the react-components Affects @guardian/source-react-components label Sep 26, 2022
@ob6160 ob6160 requested a review from sndrs September 26, 2022 12:51
@ob6160 ob6160 force-pushed the ob/fix-invalid-css branch 2 times, most recently from 7b8b22f to a9e08ae Compare September 27, 2022 14:02
@github-actions github-actions bot removed the react-components Affects @guardian/source-react-components label Sep 27, 2022
@ob6160 ob6160 requested a review from a team September 27, 2022 14:16
@ob6160 ob6160 force-pushed the ob/fix-invalid-css branch 2 times, most recently from 81c1235 to 1eedd58 Compare September 30, 2022 16:10
ob6160 and others added 16 commits October 3, 2022 12:04
…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
…d not come from lightningcss - otherwise return the jest matcher response
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
@ob6160
Copy link
Contributor Author

ob6160 commented Oct 3, 2022

Closing to reopen as two distinct changes, one for the CSS validity assertions and another for our Emotion exports

@ob6160
Copy link
Contributor Author

ob6160 commented Oct 6, 2022

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
foundations Affects @guardian/source-foundations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants