diff --git a/packages/@stylexjs/babel-plugin/__tests__/utf16-sort-test.js b/packages/@stylexjs/babel-plugin/__tests__/utf16-sort-test.js new file mode 100644 index 000000000..d7f01031e --- /dev/null +++ b/packages/@stylexjs/babel-plugin/__tests__/utf16-sort-test.js @@ -0,0 +1,122 @@ +/** + * 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. + */ + +'use strict'; + +import stylexPlugin from '../src/index'; +import { PreRule } from '../src/shared/preprocess-rules/PreRule'; +import createTheme from '../src/shared/stylex-create-theme'; +import { defaultOptions } from '../src/shared/utils/default-options'; + +const assemblyOptions = { useLayers: false, legacyDisableLayers: true }; + +function rule(name, ltr) { + return [name, { ltr, rtl: null }, 3000]; +} + +describe('UTF-16 code-unit ordering', () => { + test.each([ + [ + 'legacy class name', + rule('xB', '.xB{color:red}'), + rule('xa', '.xa{color:blue}'), + { useLegacyClassnamesSort: true }, + ], + [ + 'declared property name', + rule('x1', '.x1{--B:red}'), + rule('x2', '.x2{--a:red}'), + {}, + ], + [ + 'surrounding at-rule context before breakpoint width', + rule( + 'x1', + '@container B (width > 10px){@media (min-width: 900px){.x1{color:red}}}', + ), + rule( + 'x2', + '@container a (width > 10px){@media (min-width: 100px){.x2{color:red}}}', + ), + {}, + ], + [ + 'declaration text (case-sensitive)', + rule('x1', '.x1::after{content:"B"}'), + rule('x2', '.x2::after{content:"a"}'), + {}, + ], + [ + 'declaration text (including surrogate pairs)', + rule('x1', '.x1::after{content:"\u{1F600}"}'), + rule('x2', '.x2::after{content:"\uFF21"}'), + {}, + ], + [ + 'full rule text', + rule('xB', '.xB{color:red}'), + rule('xa', '.xa{color:red}'), + {}, + ], + ])('sorts by %s', (_name, first, second, overrides) => { + const options = { ...assemblyOptions, ...overrides }; + const expected = `${first[1].ltr}\n${second[1].ltr}`; + expect(stylexPlugin.processStylexRules([first, second], options)).toBe( + expected, + ); + expect(stylexPlugin.processStylexRules([second, first], options)).toBe( + expected, + ); + }); + + test('canonicalizes pseudo-classes around a pseudo-element for the class hash', () => { + const compile = (pseudos) => + new PreRule('color', 'red', pseudos).compiled(defaultOptions)[0]; + const [className, style] = compile([ + ':lang(a)', + ':lang(B)', + '::part(label)', + ':hover', + ':active', + ]); + const [permutedClass, permutedStyle] = compile([ + ':lang(B)', + ':lang(a)', + '::part(label)', + ':active', + ':hover', + ]); + expect(permutedClass).toBe(className); + expect(permutedStyle.ltr).toBe(style.ltr); + expect(className).toMatchInlineSnapshot('"xpbmbui"'); + expect(style.ltr).toBe( + `.${className}:lang(B):lang(a):active:hover::part(label){color:red}`, + ); + }); + + test('hashes theme overrides in code-unit order with the default first', () => { + const vars = { __varGroupHash__: 'xBase', color: 'var(--color)' }; + const upper = '@container B (width > 10px)'; + const lower = '@container a (width > 10px)'; + const [theme, rules] = createTheme(vars, { + color: { [lower]: 'blue', [upper]: 'red', default: 'black' }, + }); + expect( + createTheme(vars, { + color: { default: 'black', [upper]: 'red', [lower]: 'blue' }, + }), + ).toEqual([theme, rules]); + expect(theme.xBase).toMatchInlineSnapshot('"x15ir5s4 xBase"'); + const [className] = theme.xBase.split(' '); + const selector = `.${className}, .${className}:root`; + expect(Object.values(rules).map(({ ltr }) => ltr)).toEqual([ + `${selector}{--color:black;}`, + `${upper}{${selector}{--color:red;}}`, + `${lower}{${selector}{--color:blue;}}`, + ]); + }); +}); diff --git a/packages/@stylexjs/babel-plugin/src/index.js b/packages/@stylexjs/babel-plugin/src/index.js index ec30e5cbb..188478f7b 100644 --- a/packages/@stylexjs/babel-plugin/src/index.js +++ b/packages/@stylexjs/babel-plugin/src/index.js @@ -47,6 +47,7 @@ import transformStyleXCreateThemeNested from './visitors/stylex-create-theme-nes import { createUtilityStylesVisitor } from '@stylexjs/atoms/babel-transform'; import { convertObjectToAST } from './utils/js-to-ast'; import styleXCreateSet from './shared/stylex-create'; +import { compareStrings } from './shared/utils/compare-strings'; import { getProgramPath, hoistExpression } from './utils/ast-helpers'; import { injectDevClassNames } from './utils/dev-classname'; @@ -665,7 +666,7 @@ function compareWidthSortKeys( } if (a.context !== b.context) { - return a.context.localeCompare(b.context); + return compareStrings(a.context, b.context); } if (a.bound.kind !== b.bound.kind) { @@ -875,9 +876,10 @@ function processStylexRules( if (priorityComparison !== 0) return priorityComparison; if (useLegacyClassnamesSort) { - return classname1.localeCompare(classname2); + return compareStrings(classname1, classname2); } else { - const nameComparison = declaredPropertyName(rule1).localeCompare( + const nameComparison = compareStrings( + declaredPropertyName(rule1), declaredPropertyName(rule2), ); if (nameComparison !== 0) return nameComparison; @@ -894,9 +896,9 @@ function processStylexRules( const property1 = rule1.slice(rule1.lastIndexOf('{')); const property2 = rule2.slice(rule2.lastIndexOf('{')); - const propertyComparison = property1.localeCompare(property2); + const propertyComparison = compareStrings(property1, property2); if (propertyComparison !== 0) return propertyComparison; - return rule1.localeCompare(rule2); + return compareStrings(rule1, rule2); } }, ); diff --git a/packages/@stylexjs/babel-plugin/src/shared/stylex-create-theme.js b/packages/@stylexjs/babel-plugin/src/shared/stylex-create-theme.js index feea10946..3d1ad4b6a 100644 --- a/packages/@stylexjs/babel-plugin/src/shared/stylex-create-theme.js +++ b/packages/@stylexjs/babel-plugin/src/shared/stylex-create-theme.js @@ -16,6 +16,7 @@ import { wrapWithAtRules, } from './stylex-vars-utils'; import { isCSSType } from './types'; +import { compareStringsWithDefaultFirst } from './utils/compare-strings'; import { defaultOptions } from './utils/default-options'; // It takes an object of variables with their values and the original set of variables to override @@ -50,8 +51,8 @@ export default function styleXCreateTheme( // Sort @-rules to get a consistent unique hash value // But also put "default" first - const sortedAtRules = Object.keys(rulesByAtRule).sort((a, b) => - a === 'default' ? -1 : b === 'default' ? 1 : a.localeCompare(b), + const sortedAtRules = Object.keys(rulesByAtRule).sort( + compareStringsWithDefaultFirst, ); const atRulesStringForHash = sortedAtRules diff --git a/packages/@stylexjs/babel-plugin/src/shared/utils/compare-strings.js b/packages/@stylexjs/babel-plugin/src/shared/utils/compare-strings.js new file mode 100644 index 000000000..e1d48b340 --- /dev/null +++ b/packages/@stylexjs/babel-plugin/src/shared/utils/compare-strings.js @@ -0,0 +1,23 @@ +/** + * 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 + */ + +// Compare UTF-16 code units so generated output does not depend on the locale. +export function compareStrings(a: string, b: string): number { + return a < b ? -1 : a > b ? 1 : 0; +} + +export function compareStringsWithDefaultFirst(a: string, b: string): number { + if (a === 'default' && b !== 'default') { + return -1; + } + if (b === 'default' && a !== 'default') { + return 1; + } + return compareStrings(a, b); +} diff --git a/packages/@stylexjs/babel-plugin/src/shared/utils/rule-utils.js b/packages/@stylexjs/babel-plugin/src/shared/utils/rule-utils.js index 9f9983ab0..cfcf7b640 100644 --- a/packages/@stylexjs/babel-plugin/src/shared/utils/rule-utils.js +++ b/packages/@stylexjs/babel-plugin/src/shared/utils/rule-utils.js @@ -7,6 +7,7 @@ * @flow strict */ +import { compareStringsWithDefaultFirst } from './compare-strings'; import { arraySort } from './object-utils'; export const sortPseudos = ( @@ -35,7 +36,7 @@ export const sortPseudos = ( ) .flatMap((pseudo) => { if (Array.isArray(pseudo)) { - return arraySort(pseudo, stringComparator); + return arraySort(pseudo, compareStringsWithDefaultFirst); } return [pseudo]; }); @@ -44,15 +45,3 @@ export const sortPseudos = ( export const sortAtRules = ( atRules: $ReadOnlyArray, ): $ReadOnlyArray => 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); -};