这是indexloc提供的服务,不要输入任何密码
Skip to content

Report invalid selectors #336

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

Merged
merged 4 commits into from
Jun 17, 2025
Merged

Report invalid selectors #336

merged 4 commits into from
Jun 17, 2025

Conversation

jamesnw
Copy link
Contributor

@jamesnw jamesnw commented Jun 12, 2025

Description

Invalid selectors, for instance .w-[10], crash the polyfill. This PR throws a more specific error to guide the developer in fixing it. It does not attempt to ignore or fix the error.

There may be other places where invalid CSS will break the polyfill's assumption of validity, and we could use a similar approach as they are reported.

Related Issue(s)

Closes #335

Steps to test/reproduce

Locally, add a rule with a selector like .w-[10] that is invalid, and see that the error is thrown.

Or, view console at https://codepen.io/jamessw/pen/GgJdEeM

Copy link

netlify bot commented Jun 12, 2025

Deploy Preview for anchor-position-wpt canceled.

Name Link
🔨 Latest commit d69a9be
🔍 Latest deploy log https://app.netlify.com/projects/anchor-position-wpt/deploys/68516bf273619500083511f8

Copy link

netlify bot commented Jun 12, 2025

Deploy Preview for anchor-polyfill ready!

Name Link
🔨 Latest commit d69a9be
🔍 Latest deploy log https://app.netlify.com/projects/anchor-polyfill/deploys/68516bf2e435ff0008d8a66e
😎 Deploy Preview https://deploy-preview-336--anchor-polyfill.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Member

@jgerigmeyer jgerigmeyer left a comment

Choose a reason for hiding this comment

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

I'm okay merging this, but it feels like it might be better to more generally expose parsing errors from CSSTree rather than catching this one specific selector case. I think we could use onParseError in our single call to CSSTree parse, e.g.:

    onParseError: (err) => {
      throw new Error(err.formattedMessage);
    },

Or if we don't want to stop execution, in case the invalid CSS doesn't require the polyfill:

    onParseError: (err) => console.error(err.formattedMessage),

@jamesnw What do you think?

@jamesnw jamesnw force-pushed the report-invalid-selector branch from 47295b9 to 117e2bb Compare June 13, 2025 13:57
@jamesnw
Copy link
Contributor Author

jamesnw commented Jun 13, 2025

I think you're right that a more global option would be helpful.

I'm still leaning towards throwing an error and stopping execution. Catching the error in onParseError doesn't fix or skip that node, so the polyfill still executes (and throws an additional error) on that node.

I updated this PR with this. I included the original error as the cause, and also added a note to make it clear that the polyfill did not complete.

@jamesnw jamesnw requested a review from jgerigmeyer June 13, 2025 13:59
@jgerigmeyer
Copy link
Member

I'm still leaning towards throwing an error and stopping execution. Catching the error in onParseError doesn't fix or skip that node, so the polyfill still executes (and throws an additional error) on that node.

Right, but sometimes the invalid CSS doesn't contain any relevant (to anchor positioning) CSS, and the polyfill still executes perfectly fine. For example, this triggers onParseError and breaks the polyfill:

.foo {
  anchor-name: var(w-[10]);
}

While this triggers onParseError but the polyfill executes normally:

.foo {
  width: var(w-[10]);
}

I don't think we want the polyfill to throw on just any CSS parsing error, but I do think we want to expose parsing errors to users to help with debugging.

@jamesnw
Copy link
Contributor Author

jamesnw commented Jun 13, 2025

Hmm. I don't think the polyfill should take the role of a linter, and it could be confusing if the polyfill is reporting CSS parse errors that are unrelated to the polyfill working. It also is confusing, as you point out, for the polyfill to not work if there are unrelated CSS parse errors that don't break the polyfill.

In the ideal situation, the polyfill would only surface parse errors if it results in the polyfill not running. There are two ways of doing that- first would be identifying parse errors that do break the polyfill, and catch and throw those. But as you point out, that's going to be pretty context specific.

The other way would be storing all the parse errors, and then wrapping the entire polyfill in a try catch. If it throws, then reporting the parse errors. I don't like that idea either.

My current leaning is going back to the original solution- catching individual errors in places where they will break the polyfill, and throwing with better guidance on the fix. Thoughts?

@jgerigmeyer
Copy link
Member

The other way would be storing all the parse errors, and then wrapping the entire polyfill in a try catch. If it throws, then reporting the parse errors. I don't like that idea either.

I guess this approach doesn't really bother me, and it seems much more straightforward than trying to anticipate places in our code where malformed CSS (resulting in an unparsed Raw node) will trigger polyfill errors. It seems like as a user, if the polyfill can't run for whatever reason, I would love to see a list of any invalid CSS. 🤔

I don't feel strongly though, so if you think logging from onParseError is heavy-handed, then we can just handle this one case now and see if other bug reports arise.

@jamesnw
Copy link
Contributor Author

jamesnw commented Jun 16, 2025

@jgerigmeyer I haven't fixed the tests yet, but I'm curious on your take on this. I think I have it reporting only parse errors in the user's stylesheets, and only when the polyfill fails.

I store all the parse errors during the cascade step, since that's the initial pass through the CSS. Otherwise, we'd be reporting on errors parsing stylesheets that were changed by the polyfill.

I only wrapped the cascade and parse steps in the try catch, as they are the steps that parse CSS. While it's possible that a syntax error would cause the polyfill to fail in later steps, I think it's more likely that the syntax error would be unrelated, and likely to be a red herring.

It does report all parse errors, and doesn't attempt to identify which parse error caused the polyfill to fail.

Copy link
Member

@jgerigmeyer jgerigmeyer left a comment

Choose a reason for hiding this comment

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

@jamesnw This looks good to me!

@jamesnw jamesnw requested a review from jgerigmeyer June 17, 2025 13:24
Copy link
Member

@jgerigmeyer jgerigmeyer left a comment

Choose a reason for hiding this comment

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

:shipit:

@jamesnw jamesnw merged commit 52bef55 into main Jun 17, 2025
15 checks passed
@jamesnw jamesnw deleted the report-invalid-selector branch June 17, 2025 16:03
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.

[BUG] crash: TypeError: can't access property "map", t.children is undefined
2 participants