Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix similar screens matching #175

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/stylify/src/Compiler/CustomSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class CustomSelector {
};

processTree(rootSelector, this.tree);
console.log(selectors);

return selectors;
}

Expand Down
49 changes: 34 additions & 15 deletions packages/stylify/src/Compiler/MacroMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,51 @@ export class MacroMatch {

if (this.screenAndPseudoClassesMatch) {
const screenAndPseudoClassesMatchArray = this.screenAndPseudoClassesMatch.split(':');
let possibleScreenMatch = screenAndPseudoClassesMatchArray[0]
.replace(/&&/ig, ' and ')
.replace(/\|\|/ig, ', ');
const operators: string[] = [];
const possibleScreenMatchItems = screenAndPseudoClassesMatchArray[0]
.replace(/&&/ig, () => {
operators.push(' and ');
return '__OPERATOR__';
})
.replace(/\|\|/ig, () => {
operators.push(', ');
return '__OPERATOR__';
})
.split('__OPERATOR__');

let screenMatched = false;
let possibleScreenMatchString = '';

for (const key in screens) {
const screenRegExp = new RegExp('\\b' + key, 'g');
const screenMatches = screenRegExp.exec(possibleScreenMatch);
possibleScreenMatchItems.forEach((possibleScreenMatch) => {
for (const key in screens) {
const screenRegExp = new RegExp(`\\b${key}$`, 'g');
const screenMatches = screenRegExp.exec(possibleScreenMatch);

if (screenMatches === null) {
continue;
if (screenMatches === null) {
continue;
}

let screenData = screens[key];

if (typeof screenData === 'function') {
screenData = screenData(screenMatches[0]);
}

possibleScreenMatch = possibleScreenMatch.replace(screenRegExp, screenData);
screenMatched = true;
}

let screenData = screens[key];
const operator = operators[0] ?? '';

if (typeof screenData === 'function') {
screenData = screenData(screenMatches[0]);
if (operator) {
operators.shift();
}

possibleScreenMatch = possibleScreenMatch.replace(screenRegExp, screenData);
screenMatched = true;
}
possibleScreenMatchString += `${possibleScreenMatch}${operator}`;
});

if (screenMatched) {
this.screen = possibleScreenMatch;
this.screen = possibleScreenMatchString;
screenAndPseudoClassesMatchArray.shift();
}

Expand Down
13 changes: 0 additions & 13 deletions packages/stylify/tests/jest/dynamic-screens.test.ts

This file was deleted.

33 changes: 33 additions & 0 deletions packages/stylify/tests/jest/screens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import TestUtils from '../../../../tests/TestUtils';
import { Compiler, minifiedSelectorGenerator } from '../../src';

const testName = 'screens';
const testUtils = new TestUtils('stylify', testName);

beforeEach(() => {
minifiedSelectorGenerator.processedSelectors = {};
});

/* test('Dynamic screens', (): void => {
const inputIndex = testUtils.getHtmlInputFile();

const compiler = new Compiler({ dev: true });
let compilationResult = compiler.compile(inputIndex);

testUtils.testCssFileToBe(compilationResult.generateCss());
}); */

test('Similar screens', (): void => {
const compiler = new Compiler({
dev: true,
screens: {
'tablet': '(min-width: 123px)',
'tablet-deprecated': '(min-width: 345px)'
}
});

testUtils.testCssFileToBe(
compiler.compile('tablet:color:blue tablet-deprecated:color:red', undefined, false).generateCss(),
'similar-screens'
);
});
11 changes: 11 additions & 0 deletions packages/stylify/tests/jest/screens/expected/similar-screens.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@media (min-width: 123px) {
.tablet\:color\:blue{
color: blue
}
}

@media (min-width: 345px) {
.tablet-deprecated\:color\:red{
color: red
}
}