-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Use textContent of stylesheet if available #61
Conversation
This will fix a bug where our style collection would not work if the stylesheet was using this pattern: font: var(--some-var); font-weight: var(--some-other-var); Iterating over cssRules here would mean we collected styles that looked something like: font-style: ; font-variant-ligatures: ; font-variant-caps: ; font-variant-numeric: ; font-variant-east-asian: ; font-variant-alternates: ; font-variant-position: ; font-variant-emoji: ; font-stretch: ; font-size: ; line-height: ; font-family: ; font-optical-sizing: ; font-size-adjust: ; font-kerning: ; font-feature-settings: ; font-variation-settings: ; font-weight: var(--some-other-var); Notice how properties are expanded and left with no content. To fix this, we can use `textContent` of the style element instead. And if that isn't available, we fall back to `sheet.cssRules`. The only potential issue this could introduce is if you mix text content styles and dynamically injected ones (using e.g. `insertRule`). I doubt that's a common way to create styling however.
takeDOMSnapshot.js
Outdated
.split('\n') | ||
.map((line) => line.trim()) | ||
.filter((line) => line && !COMMENT_PATTERN.test(line)) | ||
.join(' '); |
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.
On line 16 we join with \n
. Should we do the same here?
takeDOMSnapshot.js
Outdated
? stripComments(element.textContent) | ||
: getContentFromStyleSheet(element.sheet); |
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.
The naming of these functions seems a bit off to me. Also, I think perhaps it should be refactored a bit where here and below we would call getContentFromStyleSheet(element)
and then that function would be this:
function getContentFromStyleSheet(element) {
const lines = element.textContent
? element.textContent.split('\n').map(line => line.trim())
: element.sheet.map((rule) => rule.cssText);
return lines
.filter((line) => line && !COMMENT_PATTERN.test(line))
.join('\n');
}
That sorts out the naming and avoids duplicating/forking logic.
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.
Oh it looks like adoptedStyleSheets will need slightly different logic.
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.
I updated this PR
expect(snapshot.cssBlocks[0].content).toMatch(/font-weight: 400;/); | ||
expect(snapshot.cssBlocks[0].content).toMatch(/font: 400 1rem/); |
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.
Should we do something here to make it more obvious that these are the variables? We could use names that don't match the styles exactly, and also maybe include the leading --
in the assertions?
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.
Sounds good.
I'll make some small changes here. |
I found the naming here to be a bit off, and I didn't like that the logic was duplicated and different. This commit attempts to refactor this code to have better naming and logical flow.
We had to make some small updates because we're no longer one-lining everything. I also took the time to name the custom vars better so that the assertions are easier to make.
This will fix a bug where our style collection would not work if the stylesheet was using this pattern:
font: var(--some-var);
font-weight: var(--some-other-var);
Iterating over cssRules here would mean we collected styles that looked something like:
font-style: ;
font-variant-ligatures: ;
font-variant-caps: ;
font-variant-numeric: ;
font-variant-east-asian: ;
font-variant-alternates: ;
font-variant-position: ;
font-variant-emoji: ;
font-stretch: ;
font-size: ;
line-height: ;
font-family: ;
font-optical-sizing: ;
font-size-adjust: ;
font-kerning: ;
font-feature-settings: ;
font-variation-settings: ;
font-weight: var(--some-other-var);
Notice how properties are expanded and left with no content.
To fix this, we can use
textContent
of the style element instead. And if that isn't available, we fall back tosheet.cssRules
.The only potential issue this could introduce is if you mix text content styles and dynamically injected ones (using e.g.
insertRule
). I doubt that's a common way to create styling however.