-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Shorthand inline CSS properties not saved correctly in the styleObj #1246
Conversation
|
return value | ||
.split(';') | ||
.map((declaration) => declaration.split(':')[0].trim()) | ||
.filter((declaration) => !!declaration); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the same thing but is neater
.filter((declaration) => !!declaration); | |
.filter(Boolean); |
return []; | ||
} | ||
return value | ||
.split(';') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if someone does style="content: 'l33t;'; background-color: orange"
? Then this'll get split after l33t
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, maybe there should be a more complex regex to split these values, some sort of CSSOM or even something extra crazy. Will keep thinking about this. Thanks for the feedback!
@@ -574,7 +575,10 @@ export default class MutationBuffer { | |||
item.attributes.style = {}; | |||
} | |||
const styleObj = item.attributes.style as styleAttributeValue; | |||
for (const pname of Array.from(target.style)) { | |||
const targetStyle = target.getAttribute('style'); | |||
const oldStyle = old.getAttribute('style'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note we could just get rid of the old
variable on line 567 altogether, as we've already got m.oldValue
corresponding to oldStyle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
old
variable on line 567 can be removed if you are not going to be using getPropertyValue
or getPropertyPriority
since it counts on CSSStyleDeclaration
type and string cannot be cast as one, it has to be assigned to a style attribute first
As for me getting the style attribute on line 579, it was a mistake. I could've just used the m.oldValue
Great spot! Myself and Justin found a few more examples where the browser really doesn't do the right thing in I think we also need to drop In terms of the parsing issues that @Juice10 pointed out, I had a look at https://github.com/vnmc/boreas/ as a way to parse CSS, which turned out to be very unwieldy but ultimately isn't a goer because of the large size; I'll see if there's a way to reduce that but until then I'll be looking out for another parser; regex likely won't cut it although having said that I haven't looked at the spec yet. |
https://github.com/jotform/css.js fails a simple |
Is there an easy way to import non typescript aware modules without having to rebuild them to target ESM? |
I think the solution is going to be 'no parse' and instead fallback to the full attribute text for the case when |
eoghanmurray Thanks for starting a conversation, I really appreciate it!
Can you please elaborate? What would you use instead of these two to get the value of a property?
Do you mean just taking the raw string value of a style attribute and not converting in to object, using it plain as that? |
@eoghanmurray @Juice10 is this something you guys had in mind?
|
(was afk last week) Moreso like
but there's more to it than that as it's possible to have multiple style mutations in the same 'batch', which are emitted as a single mutation. I'm preparing a PR with what I mean, the main bit is to get an integration test. Thanks for adding a test, that is very helpful. I'm going to try to prepare an integration test which should test for presence at the end of the right values, no matter whether the shorthand or longhand are used in the intermediary state. Expansion to longhand makes sense in terms of the CSSOM, and also in terms of the way we are doing compact changes, however it's a pity the spec didn't properly set out what to do with CSS variables, or that browsers haven't handled that without the current loss of information. |
The #1268 PR is ready for testing now if you wish to check it out; it implements a fallback to remove the special casing of the style attribute in certain cases. The regular string way is still supported by replay. |
Closing this PR since it got handled in #1268 |
For the record, the approach you took with record.test.ts was just as valid as the one I took with integration.test.ts as both are pretty much looking at recorded events (either directly, or through a snapshot in the integration test). The |
* Don't use the CSSOM when there's `var()` present as it fails badly #1246 * As the CSS Object Model expands out shorthand properties, do a check on the string length before choosing which format to go for - this approach allows 'var()' in a styleOMValue as it's only a problem when combined with a shorthand property - before this change background:black; was getting expaned to 10 OM properties as follows: 'style': { 'background-color': 'black', 'background-image': false, 'background-position-x': false, 'background-position-y': false, 'background-size': false, 'background-repeat-x': false, 'background-repeat-y': false, 'background-attachment': false, 'background-origin': false, 'background-clip': false } * Updates to remainder of tests based on refined compact style mutations * Apply suggestions from code review by: Justin Halsall <Juice10@users.noreply.github.com> --------- Authored-by: eoghanmurray <eoghan@getthere.ie>
* Don't use the CSSOM when there's `var()` present as it fails badly rrweb-io#1246 * As the CSS Object Model expands out shorthand properties, do a check on the string length before choosing which format to go for - this approach allows 'var()' in a styleOMValue as it's only a problem when combined with a shorthand property - before this change background:black; was getting expaned to 10 OM properties as follows: 'style': { 'background-color': 'black', 'background-image': false, 'background-position-x': false, 'background-position-y': false, 'background-size': false, 'background-repeat-x': false, 'background-repeat-y': false, 'background-attachment': false, 'background-origin': false, 'background-clip': false } * Updates to remainder of tests based on refined compact style mutations * Apply suggestions from code review by: Justin Halsall <Juice10@users.noreply.github.com> --------- Authored-by: eoghanmurray <eoghan@getthere.ie>
* Don't use the CSSOM when there's `var()` present as it fails badly rrweb-io#1246 * As the CSS Object Model expands out shorthand properties, do a check on the string length before choosing which format to go for - this approach allows 'var()' in a styleOMValue as it's only a problem when combined with a shorthand property - before this change background:black; was getting expaned to 10 OM properties as follows: 'style': { 'background-color': 'black', 'background-image': false, 'background-position-x': false, 'background-position-y': false, 'background-size': false, 'background-repeat-x': false, 'background-repeat-y': false, 'background-attachment': false, 'background-origin': false, 'background-clip': false } * Updates to remainder of tests based on refined compact style mutations * Apply suggestions from code review by: Justin Halsall <Juice10@users.noreply.github.com> --------- Authored-by: eoghanmurray <eoghan@getthere.ie>
* Don't use the CSSOM when there's `var()` present as it fails badly rrweb-io#1246 * As the CSS Object Model expands out shorthand properties, do a check on the string length before choosing which format to go for - this approach allows 'var()' in a styleOMValue as it's only a problem when combined with a shorthand property - before this change background:black; was getting expaned to 10 OM properties as follows: 'style': { 'background-color': 'black', 'background-image': false, 'background-position-x': false, 'background-position-y': false, 'background-size': false, 'background-repeat-x': false, 'background-repeat-y': false, 'background-attachment': false, 'background-origin': false, 'background-clip': false } * Updates to remainder of tests based on refined compact style mutations * Apply suggestions from code review by: Justin Halsall <Juice10@users.noreply.github.com> --------- Authored-by: eoghanmurray <eoghan@getthere.ie>
* Don't use the CSSOM when there's `var()` present as it fails badly rrweb-io#1246 * As the CSS Object Model expands out shorthand properties, do a check on the string length before choosing which format to go for - this approach allows 'var()' in a styleOMValue as it's only a problem when combined with a shorthand property - before this change background:black; was getting expaned to 10 OM properties as follows: 'style': { 'background-color': 'black', 'background-image': false, 'background-position-x': false, 'background-position-y': false, 'background-size': false, 'background-repeat-x': false, 'background-repeat-y': false, 'background-attachment': false, 'background-origin': false, 'background-clip': false } * Updates to remainder of tests based on refined compact style mutations * Apply suggestions from code review by: Justin Halsall <Juice10@users.noreply.github.com> --------- Authored-by: eoghanmurray <eoghan@getthere.ie>
Unfortunately this type of thing is not just a problem in the mutations, but also in the static stylesheet as evidenced in #1322 |
Description of the issue
Noticed that inline CSS properties that were defined as a shorthand and use CSS variables don't get included in the array created by
Array.from(target.style)
and therefor they are not iterated through.What gets included is their attempt to convert them to longhand properties, which wrongly assigns the value of a CSS declaration.
So, for example, if a element looks like this
<div style="background: var(--bg-white)"/>
thestyleObj
it creates has only the propertybackground-color
with an empty value (it should bebackground: var(--bg-white)
).Instead of taking the result of
target.style
which will NOT include shorthand properties if not specifically asked for withtarget.style.getPropertyValue()
, I wrote a little helper function to split the style attribute and get just the declarations that are actually there, using them as an iterator and keeping the rest of the functionality the same.This way, if a shorthand was defined it will be specifically requested with
target.style.getPropertyValue()
and it's value captured in thestyleObj
Tests
There is one test that fails when running
yarn test
outside of packages/rrweb, but it was failing even before my changes so I think that's fine.I'm having weird thing happen with
test/integration.test.ts
and can't figure out what's wrong with it. Sometimes it passes and sometimes it fails like this. I don't think my changes are affecting this, but thought it's better to ask you guys