Skip to content

Commit

Permalink
feat: #648 - added support for symbols (#652)
Browse files Browse the repository at this point in the history
Co-authored-by: Mauro Erta <mauro.erta@sap.com>
  • Loading branch information
mauroerta and Mauro Erta authored Jan 31, 2023
1 parent 5980606 commit 6bb4925
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/jss/src/generateClassName.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
function getClassNameGenerator() {
const cache = new Map<string, string>();

const symbolsMap = {
'*': 'all',
'.': 'dot',
',': 'comma',
'>': 'grtr',
'+': 'plus',
'~': 'tld',
'[': 'sqropn',
']': 'sqrclsd',
'^': 'crt',
$: 'dlr',
'|': 'or',
'(': 'opn',
')': 'clsd',
'=': 'eql',
};

function escapeString(string: string) {
if (cache.has(string)) {
return cache.get(string);
}

const escaped = string.replace(/[^\w\s-]/gi, '').trim();
const replaced = Object.keys(symbolsMap).reduce(
(acc, symbol) =>
acc.replace(new RegExp('\\' + symbol, 'gi'), symbolsMap[symbol]),
string,
);

const escaped = replaced.replace(/[^\w-]/gi, '');
cache.set(string, escaped);

return escaped;
Expand Down
10 changes: 10 additions & 0 deletions packages/jss/tests/generateClassName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ describe('generateClassName', () => {

expect(className).toBe('p-bg-primary');
});

it('should convert symbols into plain text', () => {
const className = generateClassName({
'& div > p': {
bg: 'primary',
},
});

expect(className).toBe('divgrtrp-bg-primary');
});
});
1 change: 1 addition & 0 deletions packages/spec/src/properties/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const colorsMap = [
'color',
'stroke',
'caretColor',
'accentColor',
'borderColor',
'outlineColor',
'borderTopColor',
Expand Down

0 comments on commit 6bb4925

Please sign in to comment.