Skip to content

Commit 3dc8e07

Browse files
committed
fix: improve psuedo element printing
1 parent 758d167 commit 3dc8e07

3 files changed

Lines changed: 35 additions & 25 deletions

File tree

src/__tests__/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,25 @@ test("includes pseudo elements", () => {
290290
p::selection {
291291
background: green;
292292
}
293+
294+
div ::before {
295+
display: block;
296+
}
293297
`;
294298

295299
expect(testHTML(html, styles)).toMatchInlineSnapshot(`
296300
"<div>
297-
<style scoped>
298-
::after {
301+
<style>@scope{:scope{
302+
&::after {
299303
color: green;
300304
content: \\"hello\\"
301305
}
302-
::selection {background: red}
303-
</style>
306+
&::selection {background: red}
307+
}}</style>
304308
<span>
305-
<style scoped>
306-
::selection {background: blue}
307-
</style>
309+
<style>@scope{:scope{
310+
&::selection {background: blue}
311+
}}</style>
308312
Content
309313
</span>
310314
</div>"

src/stringify.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ function printPseudoElements(data: VisualData) {
7272
return "";
7373
}
7474

75-
return `<style scoped>\n${Object.keys(pseudoStyles)
75+
return `<style>@scope{:scope{\n${Object.keys(pseudoStyles)
7676
.sort()
77-
.map((name) => indent(`${name} {${printProperties(pseudoStyles[name])}}`))
78-
.join("\n")}\n</style>`;
77+
.map((name) =>
78+
indent(`&${name} {${printProperties(pseudoStyles[name])}}`)
79+
)
80+
.join("\n")}\n}}</style>`;
7981
}
8082

8183
function printProperties(styles: { [x: string]: string }) {

src/stylesheets.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import splitSelectors from "./split-selector";
33
import { SelectorWithStyles } from "./types";
44
import { getDefaultStyles } from "./default-styles";
55
const pseudoElementRegex =
6-
/::?(before|after|first-letter|first-line|selection|backdrop|placeholder|marker|spelling-error|grammar-error)/gi;
6+
/([>~|+\s])?\s*::?(before|after|first-letter|first-line|selection|backdrop|placeholder|marker|spelling-error|grammar-error|target(?:-text)?)/gi;
77

88
/**
99
* Given a document, reads all style sheets returns extracts all CSSRules
@@ -52,8 +52,9 @@ export function getPseudoElementStyles(
5252
let match: RegExpExecArray | null = null;
5353
let seenPseudos: string[] | null = null;
5454

55-
while ((match = pseudoElementRegex.exec(selectorText))) {
56-
const name = `::${match[1]}`;
55+
while ((match = pseudoElementRegex.exec(baseSelector))) {
56+
const name = `::${match[2]}`;
57+
const childCombinator = match[1];
5758

5859
if (seenPseudos) {
5960
if (!seenPseudos.includes(name)) {
@@ -64,8 +65,8 @@ export function getPseudoElementStyles(
6465
}
6566

6667
baseSelector =
67-
selectorText.slice(0, match.index) +
68-
selectorText.slice(match.index + match[0].length);
68+
baseSelector.slice(0, match.index) + (childCombinator || "");
69+
baseSelector.slice(match.index + match[0].length);
6970
}
7071

7172
if (seenPseudos && el.matches(baseSelector || "*")) {
@@ -79,20 +80,23 @@ export function getPseudoElementStyles(
7980
return rulesByPseudoElement;
8081
}, {});
8182

82-
const foundPseudoElements = Object.keys(stylesByPseudoElement);
83+
let appliedPseudoElementStyles: null | {
84+
[name: string]: { [property: string]: string };
85+
} = null;
8386

84-
if (!foundPseudoElements.length) {
85-
return null;
86-
}
87-
88-
return foundPseudoElements.reduce((styleByPseudoElement, name) => {
89-
styleByPseudoElement[name] = getAppliedStylesForElement(
87+
for (const name in stylesByPseudoElement) {
88+
const styles = getAppliedStylesForElement(
9089
el,
9190
name,
9291
stylesByPseudoElement[name]
93-
)!;
94-
return styleByPseudoElement;
95-
}, {} as { [x: string]: { [x: string]: string } });
92+
);
93+
if (styles) {
94+
appliedPseudoElementStyles ||= {};
95+
appliedPseudoElementStyles[name] = styles;
96+
}
97+
}
98+
99+
return appliedPseudoElementStyles;
96100
}
97101

98102
/**

0 commit comments

Comments
 (0)