esm-styles 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { IsSpecialSelector, IsClassSelector } from '../types/index.js';
|
|
2
|
+
export declare const startsWithHtmlTag: (selector: string) => boolean;
|
|
2
3
|
export declare const isSpecialSelector: IsSpecialSelector;
|
|
3
4
|
export declare const isClassSelector: IsClassSelector;
|
|
4
5
|
export declare const joinSelectorPath: (path: string[][]) => string[];
|
|
@@ -140,6 +140,22 @@ const isHtmlTag = (key) => {
|
|
|
140
140
|
// Only match if the key is a plain tag name (no underscores, no special chars)
|
|
141
141
|
return HTML_TAGS.has(key);
|
|
142
142
|
};
|
|
143
|
+
// Check if selector starts with an HTML tag followed by a combinator or other selector parts
|
|
144
|
+
export const startsWithHtmlTag = (selector) => {
|
|
145
|
+
// Common combinators and selector parts that might follow a tag
|
|
146
|
+
const combinators = [' ', '>', '+', '~', ':', '[', '.'];
|
|
147
|
+
// Check if the selector starts with an HTML tag followed by a combinator
|
|
148
|
+
for (const tag of HTML_TAGS) {
|
|
149
|
+
if (selector === tag)
|
|
150
|
+
return true;
|
|
151
|
+
for (const combinator of combinators) {
|
|
152
|
+
if (selector.startsWith(tag + combinator)) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
};
|
|
143
159
|
export const isSpecialSelector = (key) => {
|
|
144
160
|
// Pseudo-classes/elements, id, attribute selectors
|
|
145
161
|
return (key.startsWith(':') ||
|
|
@@ -155,8 +171,15 @@ export const joinSelectorPath = (path) => {
|
|
|
155
171
|
// Compute cartesian product of all segments
|
|
156
172
|
const combos = utils.cartesianProduct(path);
|
|
157
173
|
// Join each combination into a selector string
|
|
158
|
-
return combos.map((parts) => parts.reduce((acc, part) => {
|
|
159
|
-
if
|
|
174
|
+
return combos.map((parts) => parts.reduce((acc, part, idx) => {
|
|
175
|
+
// Check if previous part is a root selector
|
|
176
|
+
const prev = idx > 0 ? parts[idx - 1] : null;
|
|
177
|
+
const isPrevRoot = prev && (prev === ':root' || prev.startsWith(':root.'));
|
|
178
|
+
if (part === '*') {
|
|
179
|
+
// Universal selector
|
|
180
|
+
return acc + (acc ? ' ' : '') + '*';
|
|
181
|
+
}
|
|
182
|
+
else if (part.startsWith('__')) {
|
|
160
183
|
return acc + (acc ? ' ' : '') + '.' + part.slice(2);
|
|
161
184
|
}
|
|
162
185
|
else if (part.startsWith('_')) {
|
|
@@ -178,6 +201,10 @@ export const joinSelectorPath = (path) => {
|
|
|
178
201
|
else if (isHtmlTag(part)) {
|
|
179
202
|
return acc + (acc ? ' ' : '') + part;
|
|
180
203
|
}
|
|
204
|
+
else if (startsWithHtmlTag(part)) {
|
|
205
|
+
// Handle compound selectors that start with HTML tags (e.g., 'div > *')
|
|
206
|
+
return acc + (acc ? ' ' : '') + part;
|
|
207
|
+
}
|
|
181
208
|
else if (/^([a-z][a-z0-9]*)\.(.+)/.test(part)) {
|
|
182
209
|
// If part matches 'tag.class...' and tag is an HTML tag
|
|
183
210
|
const match = part.match(/^([a-z][a-z0-9]*)\.(.+)/);
|
|
@@ -185,7 +212,11 @@ export const joinSelectorPath = (path) => {
|
|
|
185
212
|
return acc + (acc ? ' ' : '') + match[1] + '.' + match[2];
|
|
186
213
|
}
|
|
187
214
|
}
|
|
188
|
-
// Not a tag, not a special selector: treat as class
|
|
215
|
+
// Not a tag, not a special selector: treat as class or custom element
|
|
216
|
+
// If previous part is a root selector, insert a space
|
|
217
|
+
if (isPrevRoot) {
|
|
218
|
+
return acc + ' ' + '.' + part;
|
|
219
|
+
}
|
|
189
220
|
return acc + (acc ? '' : '') + '.' + part;
|
|
190
221
|
}, ''));
|
|
191
222
|
};
|