Skip to content
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

Implement CSSNumericValue.parse() #206

Merged
merged 1 commit into from
Feb 4, 2024

Conversation

johannesodland
Copy link
Contributor

Add proper tokenization and parsing for CSSNumericValue.parse().

test/expected.txt Outdated Show resolved Hide resolved
@johannesodland johannesodland marked this pull request as ready for review January 22, 2024 17:09
@bramus
Copy link
Collaborator

bramus commented Jan 26, 2024

I think we might have come at a point where it might be a good idea to split off the CSSOM polyfill into its own repo with its own tests? I mean, feels like this could become its own library.

@johannesodland
Copy link
Contributor Author

I think we might have come at a point where it might be a good idea to split off the CSSOM polyfill into its own repo with its own tests? I mean, feels like this could become its own library.

It still limited to implementing the parts needed for this library.. But I’ve been thinking we should start by moving the implementation into its own folder to make it more explicit that the code is meant for polyfilling parts of the typed cssom.

As it is right now it could be tempting to use internal functions directly without going through the public api when implementing scroll driven animations. Then we would end up with too tight a coupling.

I can create a refactoring PR once the open PRs are merged.

@bramus
Copy link
Collaborator

bramus commented Jan 26, 2024

I can create a refactoring PR once the open PRs are merged.

Yeah, sounds like a good plan.

Which PRs specifically – other than this one – definitely need to be merged?

@johannesodland
Copy link
Contributor Author

Which PRs specifically – other than this one – definitely need to be merged?

I was wrong. I looked through the other PRs, and it looks like none of them are touching the cssom implementation, so we should be fine after this PR is in. We could do it as part of this PR as well, if that's preferable.

@bramus
Copy link
Collaborator

bramus commented Jan 27, 2024

I think it’d be best to do this refactor as part of a separate PR.

@johannesodland johannesodland force-pushed the css-numeric-value-parser branch 4 times, most recently from 8bcc515 to e64a826 Compare January 29, 2024 18:04
Copy link
Collaborator

@bramus bramus left a comment

Choose a reason for hiding this comment

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

Some minor nits to address (changes suggested).

src/simplify-calculation.js Outdated Show resolved Hide resolved
src/simplify-calculation.js Outdated Show resolved Hide resolved
src/numeric-values.js Outdated Show resolved Hide resolved
src/numeric-values.js Outdated Show resolved Hide resolved
src/numeric-values.js Outdated Show resolved Hide resolved
src/numeric-values.js Outdated Show resolved Hide resolved
src/numeric-values.js Outdated Show resolved Hide resolved
@johannesodland johannesodland force-pushed the css-numeric-value-parser branch 3 times, most recently from d2a0a83 to ef9eb58 Compare February 2, 2024 16:34
@johannesodland johannesodland requested a review from bramus February 2, 2024 16:49
@johannesodland
Copy link
Contributor Author

@bramus Thank you for the review 🙏
I think I have addressed all your remarks, and have rebased onto master.

@bramus
Copy link
Collaborator

bramus commented Feb 2, 2024

Thanks!

Pinging @flackr to take a look before merging. I think this one should go in before #221.

Copy link
Owner

@flackr flackr left a comment

Choose a reason for hiding this comment

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

Wow, this is a lot! I'm a bit concerned about the size increase. We'll have to think about what parts of this could be removed for a reduced build. Also, do we skip this parsing code if the browser supports CSSStyleValue.parse?

src/numeric-values.js Show resolved Hide resolved
src/tokenizer.js Show resolved Hide resolved
src/tokenizer.js Outdated Show resolved Hide resolved
@flackr
Copy link
Owner

flackr commented Feb 2, 2024

Wow, this is a lot! I'm a bit concerned about the size increase. We'll have to think about what parts of this could be removed for a reduced build. Also, do we skip this parsing code if the browser supports CSSStyleValue.parse?

To be clear, I'm ecstatic to see such a precise implementation following the spec steps! I just think the extra kilobytes will be too much for some sites, and we'll want to have a light version of the polyfill where you have to construct the CSSOM value instead of parsing it from a string.

@johannesodland
Copy link
Contributor Author

Wow, this is a lot! I'm a bit concerned about the size increase. We'll have to think about what parts of this could be removed for a reduced build. Also, do we skip this parsing code if the browser supports CSSStyleValue.parse?

To be clear, I'm ecstatic to see such a precise implementation following the spec steps! I just think the extra kilobytes will be too much for some sites, and we'll want to have a light version of the polyfill where you have to construct the CSSOM value instead of parsing it from a string.

Yeah, I know it's a lot 😅, and by all means we don't have to use it. It's been an interesting exercise :)
The parsing code should only be executed in browsers without CSSStyleValue.parse() support (Looking at you Firefox), but it does add 2.1kB to the compressed build.

If we had a build system that could build two targets, we could build one with a full implementation of CSSStyleValue.parse() and a light build with with a simple or no implementation of CSSStyleValue.parse(). That simple implementation could be limited to supporting CSSUnitValues.

The current solution though (that I'm at fault for), supports some math functions but can fail in more complex calculations. And there's no clear definition of what it will support or not. It also fails in browsers without regex lookahead support (Safari < 16.4).

@johannesodland johannesodland force-pushed the css-numeric-value-parser branch 2 times, most recently from 20ae1ed to 8e7cf38 Compare February 3, 2024 13:19
return tokenizeString(input);
}
// Assert: Only the preceding types should be passed as input.
throw new TypeError(`Invalid input type git${typeof input}`)
Copy link
Owner

Choose a reason for hiding this comment

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

I think the text "git" prepended before the type may have been unintentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh.. that's embarrassing 😅
I'm having an issue where the focus is delayed when switching between the IDE and the terminal 😞

Fixed in https://github.com/flackr/scroll-timeline/compare/8e7cf381430bd1d2c7511debec7d954ebaccaf66..4ed64d0898c93a488381f6ad35c42033b39698a8

src/tokenizer.js Outdated
let position = this.index
for (let i = 0; i < 3; i++) {
const nextCodePoint = this.input.codePointAt(position);
if (typeof nextCodePoint !== 'undefined') {
Copy link
Owner

Choose a reason for hiding this comment

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

According to the MDN docs, codePointAt only returns undefined if the index is out of range. I think it would be cleaner to change the for loop condition to i < 3 && position < this.input.length, then if the loop runs we always expect it to find a codepoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🙏

src/tokenizer.js Show resolved Hide resolved
src/tokenizer.js Outdated Show resolved Hide resolved
Update src/simplify-calculation.js

Co-authored-by: Bramus <[email protected]>

Update src/simplify-calculation.js

Co-authored-by: Bramus <[email protected]>

Update src/numeric-values.js

Co-authored-by: Bramus <[email protected]>

Update src/numeric-values.js

Co-authored-by: Bramus <[email protected]>

Update src/numeric-values.js

Co-authored-by: Bramus <[email protected]>

Update src/numeric-values.js

Co-authored-by: Bramus <[email protected]>

Reify e and pi, rename to transformToCSSNumericValue and add jsdoc
@johannesodland johannesodland force-pushed the css-numeric-value-parser branch from cf22c2b to 0ff6f35 Compare February 3, 2024 23:42
Copy link
Owner

@flackr flackr left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, looks good to me!

@bramus bramus merged commit d605aba into flackr:master Feb 4, 2024
2 checks passed
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.

3 participants