Skip to content

Commit 2b028c7

Browse files
author
Michael Vurchio
committed
Add :local() pseudo selector
1 parent 738919d commit 2b028c7

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

example/webpack/App.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
-webkit-font-smoothing: antialiased;
4646
}
4747
48-
section {
48+
:local(section) {
4949
flex: 0 1 auto;
5050
flex-direction: column;
5151
display: flex;
5252
height: 100%;
5353
}
5454
55-
header {
55+
:local(header) {
5656
padding: 1rem;
5757
font-size: 1.2rem;
5858
font-weight: bold;
@@ -68,7 +68,7 @@ header {
6868
-webkit-overflow-scrolling: touch;
6969
}
7070
71-
footer {
71+
:local(footer) {
7272
padding: 1rem;
7373
text-align: right;
7474
border-top: 1px solid #d9d9d9;

example/webpack/components/Time.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
font-family: monospace;
2020
}
2121
22-
.bolder {
22+
:local(.bolder) {
2323
font-weight: 900;
2424
}
2525
26-
.bolder:last-child + p:not(:first-child) {
26+
:local(.bolder:last-child) + p:not(:first-child) {
2727
color: blue;
2828
}
2929
@media (min-width: 20rem) {
@@ -46,9 +46,9 @@
4646
font: 1em sans-serif;
4747
}
4848
p + span > strong { font-weight: 600; }
49-
:global(div) p > strong { font-weight: 600; }
49+
:global(div) :local(p > strong) { font-weight: 600; }
5050
51-
div *.bold { font-size: 12px;}
51+
:local(div) *.bold { font-size: 12px;}
5252
</style>
5353
<div
5454
class=" bolder light { true ? 'lighter red' : ''}"

example/webpack/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
options: {
2222
preprocess: [
2323
cssModules({
24-
mode: 'mixed',
24+
mode: 'native',
2525
includePaths: ['./'],
2626
}),
2727
],

src/processors/mixed.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const parser = (processor: Processor): void => {
7777
}
7878
});
7979
}
80+
81+
processor.parsePseudoLocalSelectors(node);
8082
}
8183
if (node.type === 'ClassSelector') {
8284
const generatedClassName = processor.createModuleClassname(node.name);

src/processors/native.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ const parser = (processor: Processor): void => {
5454

5555
node.children.forEach((item, index) => {
5656
let hasPushed = false;
57-
if (item.name === 'global' && item.type === 'PseudoClassSelector') {
57+
if (
58+
(item.name === 'global' || item.name === 'local') &&
59+
item.type === 'PseudoClassSelector'
60+
) {
5861
if (start > 0) {
5962
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
6063
hasPushed = true;
@@ -72,6 +75,8 @@ const parser = (processor: Processor): void => {
7275
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
7376
}
7477
});
78+
79+
processor.parsePseudoLocalSelectors(node);
7580
}
7681

7782
if (node.type === 'ClassSelector') {
@@ -101,6 +106,7 @@ const nativeProcessor = async (
101106
): Promise<string> => {
102107
const processor = new Processor(ast, content, filename, options, parser);
103108
const processedContent = processor.parse();
109+
console.log(processedContent);
104110
return processedContent;
105111
};
106112

src/processors/processor.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import MagicString from 'magic-string';
2-
import type { Ast, Style } from 'svelte/types/compiler/interfaces.d';
2+
import type { Ast, Style, TemplateNode } from 'svelte/types/compiler/interfaces.d';
33
import { CSSModuleList, PluginOptions } from '../types';
44
import { camelCase, createClassName, hasModuleAttribute, hasModuleImports } from '../lib';
55
import { parseImportDeclaration, parseTemplate } from '../parsers';
@@ -72,6 +72,23 @@ export default class Processor {
7272
this.cssModuleList[name] = value;
7373
};
7474

75+
/**
76+
* Parse pseudo selector :local()
77+
* @param node The ast "Selector" node to parse
78+
*/
79+
public parsePseudoLocalSelectors = (node: TemplateNode): void => {
80+
const pseudoLocalSelectors = node.children.filter(
81+
(item) => item.type === 'PseudoClassSelector' && item.name === 'local'
82+
);
83+
84+
if (pseudoLocalSelectors.length > 0) {
85+
pseudoLocalSelectors.forEach((item) => {
86+
this.magicContent.remove(item.start, item.start + `:local(`.length);
87+
this.magicContent.remove(item.end - 1, item.end);
88+
});
89+
}
90+
};
91+
7592
/**
7693
* Parse component
7794
* @returns The CssModule updated component

0 commit comments

Comments
 (0)