-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure unique classNames for pseudo classes & elems (#750)
* fix: ensure unique classNames for different orders of pseudo class and elements * chore: rebase onto PR with repro * chore: better comments * fix: variable names
- Loading branch information
Showing
5 changed files
with
98 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
*/ | ||
|
||
import { arraySort } from './object-utils'; | ||
|
||
export const sortPseudos = ( | ||
pseudos: $ReadOnlyArray<string>, | ||
): $ReadOnlyArray<string> => { | ||
if (pseudos.length < 2) { | ||
return pseudos; | ||
} | ||
|
||
return pseudos | ||
.reduce( | ||
(acc, pseudo) => { | ||
if (pseudo.startsWith('::')) { | ||
return [...acc, pseudo]; | ||
} | ||
|
||
const lastElement = acc[acc.length - 1]; | ||
const allButLast = acc.slice(0, acc.length - 1); | ||
if (Array.isArray(lastElement)) { | ||
return [...allButLast, [...lastElement, pseudo]]; | ||
} else { | ||
return [...allButLast, lastElement, [pseudo]].filter(Boolean); | ||
} | ||
}, | ||
[] as $ReadOnlyArray<string | $ReadOnlyArray<string>>, | ||
) | ||
.flatMap((pseudo) => { | ||
if (Array.isArray(pseudo)) { | ||
return arraySort(pseudo, stringComparator); | ||
} | ||
return [pseudo]; | ||
}); | ||
}; | ||
|
||
export const sortAtRules = ( | ||
atRules: $ReadOnlyArray<string>, | ||
): $ReadOnlyArray<string> => arraySort(atRules); | ||
|
||
// a comparator function that sorts strings alphabetically | ||
// but where `default` always comes first | ||
const stringComparator = (a: string, b: string): number => { | ||
if (a === 'default') { | ||
return -1; | ||
} | ||
if (b === 'default') { | ||
return 1; | ||
} | ||
return a.localeCompare(b); | ||
}; |