Skip to content

Commit

Permalink
fix: correct support for multiple classes, fixes #43
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Feb 20, 2023
1 parent d059d92 commit fde1497
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 14 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Anton Korzunov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
File renamed without changes.
43 changes: 43 additions & 0 deletions __tests__/edge-cases/missing-43.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { loadStyleDefinitions } from '../../src';
import { getCriticalStyles } from '../../src/getCSS';

describe('missing styles', () => {
it('result should contain 1600', async () => {
const styles = loadStyleDefinitions(
() => ['test.css'],
// // .downloads-1u5ev.downloadsFallback-1btP7 .logo-2Dv5-,
() => `
.a.b .c {
position: relative;
}
`
);
await styles;

expect(styles.ast['test.css'].selectors).toMatchInlineSnapshot(`
Array [
Object {
"declaration": 1,
"hash": ".a.b .c18suomu-1gpll6f0",
"media": Array [],
"parents": Array [
"a",
"b",
],
"pieces": Array [
"c",
],
"postfix": ".c",
"selector": ".a.b .c",
},
]
`);

const extracted = getCriticalStyles('<div class="a b"><div class="c"></div></div>\n', styles);

expect(extracted).toMatchInlineSnapshot(`
"<style type=\\"text/css\\" data-used-styles=\\"test.css\\">.a.b .c { position: relative; }
</style>"
`);
});
});
14 changes: 14 additions & 0 deletions __tests__/extraction.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ describe('extraction stories', () => {
.top .child {
margin: 10px;
}
.grand.top .child {
position: correct
}
.grand.top {
margin: 10px;
}
.grand.top.incorrect .child {
position: incorrect
}
`
);
await styles;
Expand All @@ -160,6 +172,8 @@ describe('extraction stories', () => {
.child { padding: 10px; }
.grand .child { padding: 10px; }
.top .child { margin: 10px; }
.grand.top .child { position: correct; }
.grand.top { margin: 10px; }
"
`);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/mapStyles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('test parent selector', () => {
});

it('should return the double style', () => {
expect(extractParents('.a.b c')).toEqual(['a.b']);
expect(extractParents('.a.b c')).toEqual(['a', 'b']);
});

it('should keep the first style; drop last', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@theuiteam/lib-builder": "0.1.4"
},
"engines": {
"node": ">=10"
"node": ">=11"
},
"files": [
"dist"
Expand Down
5 changes: 4 additions & 1 deletion src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const extractParents = (selector: string): string[] => {

const effectiveMatcher = ruleSelection.filter(classish);

const selectors = effectiveMatcher.map((x) => x.replace(/[.\s.:]+/, '').replace(/PLUS_SYMBOL/g, '+')).filter(Boolean);
const selectors = effectiveMatcher
.map((x) => x.replace(/[.\s.:]+/, '').replace(/PLUS_SYMBOL/g, '+'))
.filter(Boolean)
.flatMap<string>((cl) => cl.split('.'));

return selectors;
};
Expand Down
12 changes: 1 addition & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@
"importHelpers": true,
"target": "es6",
"moduleResolution": "node",
"lib": [
"dom",
"es5",
"es6",
"es2017.object",
"scripthost",
"es2015.collection",
"es2015.symbol",
"es2015.iterable",
"es2015.promise"
],
"lib": ["dom", "es5", "es6", "es2019", "scripthost"],
"types": ["node", "jest"],
"typeRoots": ["./node_modules/@types"],
"jsx": "react"
Expand Down

0 comments on commit fde1497

Please sign in to comment.