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(styles): prevent utility API from creating unwanted classes #3644

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion packages/styles/src/functions/_string.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
@use 'sass:string';

@function replace($string, $term, $replacement: '') {
@function contains($string, $term) {
$index: string.index($string, $term);
@return if($index == null, false, true);
}

@function replace($string, $term, $replacement) {
$index: string.index($string, $term);

@if $index {
Expand Down
14 changes: 8 additions & 6 deletions packages/styles/src/utilities/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
$prefixes: map.get($classesConfig, prefixes);

@each $key, $value in $tokens {
$suffix: string.replace($key, 'post-utility-#{$group}');
@if (string.contains($key, 'post-utility-#{$group}')) {
$suffix: string.replace($key, 'post-utility-#{$group}', '');

@each $prefix, $properties in $prefixes {
@if $responsive {
@include generate-responsive-utilities($properties, $value, $prefix, $suffix);
} @else {
@include generate-utilities($properties, $value, $prefix, $suffix);
@each $prefix, $properties in $prefixes {
@if $responsive {
@include generate-responsive-utilities($properties, $value, $prefix, $suffix);
} @else {
@include generate-utilities($properties, $value, $prefix, $suffix);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/styles/tests/functions/string.test.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@

$paragraph: "I think Ruth's dog is cuter than your dog!";

// it should return true if the term is found
@include jest.equal(
true,
string.contains($paragraph, 'dog')
);

// it should return false if the term is not found
@include jest.equal(
false,
string.contains($paragraph, 'cat')
);

// it should replace a term by another
@include jest.equal(
'I think my dog is cuter than your dog!',
Expand Down