Skip to content

Commit 2e983b0

Browse files
committed
Support grouped selectors
1 parent 36f2885 commit 2e983b0

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/Generator.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export function cssToHtml(css: CSSRuleList | string, options: Options = {}): HTM
1414
const output = document.createElement('body');
1515
const fillerElements = [] as HTMLElement[];
1616
function isFillerElement (element: HTMLElement | Element): boolean {
17-
for (const element of fillerElements) {
18-
if (element.isSameNode(element)) {
17+
for (const fillerElement of fillerElements) {
18+
if (fillerElement.isSameNode(element)) {
1919
return true;
2020
}
2121
}
@@ -38,7 +38,9 @@ export function cssToHtml(css: CSSRuleList | string, options: Options = {}): HTM
3838
return output;
3939
}
4040

41-
// Convert each rule into an HTML element, then add it to the output DOM.
41+
const separatedStyleRules = [] as [string, CSSStyleRule][];
42+
43+
// Filter and format the supplied style rules.
4244
for (const [index, rule] of Object.entries(styleRules) as [string, (CSSStyleRule | CSSMediaRule)][]) {
4345
// Skip:
4446
// - Non-style rules.
@@ -60,11 +62,20 @@ export function cssToHtml(css: CSSRuleList | string, options: Options = {}): HTM
6062
}
6163

6264
// Format any combinators in the rule's selector.
63-
let selector = rule.selectorText
65+
const selector = rule.selectorText
6466
.replaceAll(/([\w-])\s+([\w-\.\#])/g, '$1>$2') // Replace child combinator spaces with `>`.
6567
.replaceAll(/>{2,}/g, '>') // Remove excess `>`.
6668
.replaceAll(' ', ''); // Remove excess spaces.
6769

70+
// Separate selectors with commas into their own style rules.
71+
const splitSelector = selector.split(',');
72+
for (const subSelector of splitSelector) {
73+
separatedStyleRules.push([subSelector, rule]);
74+
}
75+
}
76+
77+
// Convert each rule into an HTML element, then add it to the output DOM.
78+
for (let [selector, rule] of separatedStyleRules) {
6879
// This object describes an element based on pieces of a selector.
6980
const descriptor = {
7081
previousElement: undefined as HTMLElement | undefined,

0 commit comments

Comments
 (0)