Skip to content

Use css-tree #234

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 8 commits into from
Jul 27, 2025
Merged

Use css-tree #234

merged 8 commits into from
Jul 27, 2025

Conversation

asamuzaK
Copy link
Contributor

@asamuzaK asamuzaK commented Jul 21, 2025

Switch to CSSTree to improve code quality.

  • Add some fixes to CSSStyleDeclaration setter

Related wpts:

  • to-upstream

    • css/css-syntax/parse-invalid-input.html
  • css/cssom

    • serialization-CSSDeclaration-with-important.html
  • css/css-color

    • light-dark-currentcolor-in-color.html
      Fails but this is expected. Need to fix getComputedStyle()

@asamuzaK asamuzaK force-pushed the tree branch 2 times, most recently from 7b20db2 to f091f92 Compare July 21, 2025 22:25
@asamuzaK asamuzaK marked this pull request as draft July 21, 2025 22:37
@domenic
Copy link
Member

domenic commented Jul 22, 2025

It would be nice if jsdom only had one CSS parser in its dependency tree. jsdom/jsdom#3910 is trying to replace rrweb-cssom with a better replacement. Maybe jsdom should use csstree instead as well?

@domenic
Copy link
Member

domenic commented Jul 22, 2025

Well, I guess we cannot eliminate some cssom package because that's how we currently implement many objects... https://github.com/jsdom/jsdom/blob/aa52984e863d2722fb8beae757d7ea5568cea1b2/lib/jsdom/level2/style.js#L23-L30

Still, it is a good goal to eventually have just a single parser.

Maybe we can just implement the objects from scratch inside jsdom, and delegate the hard parsing/serialization work to csstree. For example, https://drafts.csswg.org/cssom/#the-medialist-interface looks pretty easy to do from scratch as long as you have an algorithm media query parsing and serialization.

@asamuzaK
Copy link
Contributor Author

I am aware that a PR to replace rrweb-cssom has been submitted.
I'm looking forward to seeing a replacement, since rrweb-cssom doesn't seem to be actively maintained.

Apart from that, this PR aims to resolve some inconsistencies with cssstyle.
cssstyle is described as a fork of the CSSStyleDeclaration part of NV/CSSOM.
https://github.com/jsdom/cssstyle/blob/main/lib/CSSStyleDeclaration.js#L2

However, what it actually does is parse CSS values using the original CSSStyleDeclaration from NV/CSSOM (now rrweb-cssom).
Very weird implementation...

dummyRule = CSSOM.parse(`#bogus{${value}}`).cssRules[0].style;

Ideally, cssstyle should take the place of the CSSStyleDeclaration part, but what happens if we add cssstyle as a dependency to rrweb-cssom?
Circular dependency.

That's why I want to remove rrweb-cssom from cssstyle.

CSSTree is a great library, but implementing CSSOM-related things and CSSStyleDeclaration will likely require complex processing from the AST.
So I personally think it's better to only use it partially, rather than as a single CSS parser for jsdom.

@asamuzaK asamuzaK marked this pull request as ready for review July 23, 2025 10:28
@asamuzaK
Copy link
Contributor Author

asamuzaK commented Jul 23, 2025

I will give my thoughts on #235 separately.

Anyway, I will request a review for this PR.
The wpt test is at https://github.com/jsdom/jsdom/blob/main/test/web-platform-tests/to-upstream/css/css-syntax/parse-invalid-input.html
There is a caveat that @asamuzakjp/css-color supports light-dark(), but css-tree not yet, so wpt test related to light-dark() fails.
It turns out that something that should have failed had mistakenly passed.

@asamuzaK
Copy link
Contributor Author

Ah, found https://github.com/csstools/postcss-plugins/tree/main/packages/css-syntax-patches-for-csstree
i will add one more push.

@asamuzaK asamuzaK marked this pull request as draft July 23, 2025 11:05
@asamuzaK asamuzaK marked this pull request as ready for review July 23, 2025 14:34
@asamuzaK
Copy link
Contributor Author

PR ready

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

Why did you remove @csstools/css-syntax-patches-for-csstree? (Also, when you do add it, we should add a test which depends on that package existing.)

@asamuzaK
Copy link
Contributor Author

Why did you remove @csstools/css-syntax-patches-for-csstree? (Also, when you do add it, we should add a test which depends on that package existing.)

It is useful for cssTree.lexer.
I did expected that it also change result in cssTree.parse, but it did not.
So removed.

@asamuzaK
Copy link
Contributor Author

cssTree.lexer is very powerful and it can probably replace our isValid functions under properties/.
But thats another issue and I will take a look later.

@asamuzaK
Copy link
Contributor Author

Changed my mind and decided to implement isValidPropertyValue() now.
Currently it only applies to CSSStyleDecoration.

@asamuzaK asamuzaK marked this pull request as draft July 24, 2025 23:19
@asamuzaK asamuzaK marked this pull request as ready for review July 25, 2025 00:31
@asamuzaK asamuzaK requested a review from domenic July 25, 2025 00:32
@asamuzaK asamuzaK force-pushed the tree branch 2 times, most recently from a65f48c to 2587927 Compare July 25, 2025 14:52
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

I am a little worried that we only replaced one instance of parseKeyword(), and left all the existing ones alone. However, I cannot see any obvious cases where this halfway state would cause problems, so I will merge this.

I would love it if you were able to open an issue detailing your plans for replacing more and more parts of this library with css-tree. (Or maybe discussing in #235 is best.)

@domenic domenic merged commit 40fe70b into jsdom:main Jul 27, 2025
3 checks passed
@asamuzaK asamuzaK deleted the tree branch July 27, 2025 06:49
@asamuzaK
Copy link
Contributor Author

Could you please release a new version?
Then I can start working on fixing jsdom/jsdom#3895 and else.

@domenic
Copy link
Member

domenic commented Jul 28, 2025

Done. Thank you for your work!

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.

2 participants