Skip to content

Commit

Permalink
Fix similar screens matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Machy8 committed Jan 19, 2023
1 parent 1d066fb commit 9a8e8d9
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 29 deletions.
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
}
}

0 comments on commit 9a8e8d9

Please sign in to comment.