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

fix(serializer): normalize style attributes #237

Merged
merged 11 commits into from
Apr 8, 2024

Conversation

jmsjtu
Copy link
Member

@jmsjtu jmsjtu commented Apr 4, 2024

Split into two commits to make the changes more obvious.

See #236 for class changes.

BREAKING CHANGE: snapshots may change.

W-15365974

@jmsjtu jmsjtu changed the title Jtu/normalize style attr fix(serializer): normalize style attributes Apr 4, 2024
} else {
elm.removeAttribute(name);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably split on ; and normalize whitespace for each, since there can be arbitrary whitespace before/after the semicolon.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also this would be cleaner to go in its own cleanElementStyle function/file imo.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also: this is not going to handle zany things like content or grid-template-areas which may contain spaces or semicolons in the value, but I'm not sure that that's worth handling. It quickly devolves into prettier/full CSS parsing, and we probably just need a lightweight formatter.

Copy link
Member Author

Choose a reason for hiding this comment

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

@nolanlawson I've updated the logic so reduce the spaces when there is a ';'.

I tried to preserve the space if it's preceded by a ';'

So something like this:

<div style="color: blue;   text-align: center"></div>

will output as:

<div style="color: blue; text-align: center"></div>

See the tests for different variations, let me know what you think, alternatively we can just strip all the spaces before/after a ';'.

Also added some logic to replace /t and /n.

@jmsjtu jmsjtu requested a review from nolanlawson April 4, 2024 20:30
Comment on lines 17 to 20
.split(';')
// Preserve space character following a ';', style="color: blue; text-align: center"
.map((attr) => attr.trimEnd())
.join(';');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.split(';')
// Preserve space character following a ';', style="color: blue; text-align: center"
.map((attr) => attr.trimEnd())
.join(';');
.split(';')
.map(_ => _.trim())
.filter(Boolean)
.map(_ => `${_};`)
.join(' ');

This avoids the duplicates ;s at the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.split(';')
// Preserve space character following a ';', style="color: blue; text-align: center"
.map((attr) => attr.trimEnd())
.join(';');
.split(';')
.map(_ => `${_.trim()};`)
.filter(_ => _ !== ';')
.join(' ');

Same but one less loop.

Copy link
Member Author

@jmsjtu jmsjtu Apr 4, 2024

Choose a reason for hiding this comment

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

This will also append a ';' at the end of a style:

<div style="  color: blue  "></div>

Becomes:

<div style="color: blue;"></div>

Empty ';' will be removed as well

<div style=";;"></div>

Becomes:

<div />

Just want to double-check, are we trying to normalize the whitespace or completely format the style attribute output?

Asking because it won't exactly line up with what the component author will see in the browser.

Actually, on second thought, I don't think ☝️ will be an issue

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think we want consistent output. So consistent ;s at the end, consistent spacing, and just <div /> if the style is semantically empty.

style="color: blue; text-align: center;"
/>
<div
style="color : blue; text-align : center;"
Copy link
Contributor

@nolanlawson nolanlawson Apr 4, 2024

Choose a reason for hiding this comment

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

text-align : center

This one is going to lead to inconsistencies between static/dynamic sadly... Not sure how to solve without a full CSS parse 🤦

Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought, we can't use a CSS parser because we've never validated this content, so it could be syntactically invalid and consumers could be relying on that.

I think we should split on : as well and normalize that so that it's always in the format:

color: red;

or

color: red !important;

Copy link
Contributor

Choose a reason for hiding this comment

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

@nolanlawson
Copy link
Contributor

This doesn't work well for cases like this:

<div style="background-image: url(data: image/svg+xml;base64,abc123); background-size: 12px;"></div>

This incorrectly handles the ;s, serializing into:

<div
     style="background-image: url(data: image/svg+xml; base64,abc123); background-size: 12px;"
/>

Copy link
Contributor

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

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

See above and #239

@nolanlawson nolanlawson merged commit b5f3ebb into master Apr 8, 2024
10 checks passed
@nolanlawson nolanlawson deleted the jtu/normalize-style-attr branch April 8, 2024 17:11
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