|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file stylexInject.ts |
| 5 | + * @input Each CSS rule the StyleX babel plugin injects at runtime in tests |
| 6 | + * @output The `inject` that vitest.config.ts names as the plugin's |
| 7 | + * `runtimeInjection`: StyleX's own, plus every rule StyleX's dev runtime |
| 8 | + * drops because it keys the rule by an at-rule's prelude alone, inserted |
| 9 | + * into a sheet of its own |
| 10 | + * @position Test setup only; production CSS comes from the build |
| 11 | + * (`processStylexRules`), which keeps every rule |
| 12 | + * |
| 13 | + * StyleX 0.19.0's dev runtime keys each rule it has inserted |
| 14 | + * (`getSeenRuleKey` in `lib/es/inject.mjs`, which it does not export; copied |
| 15 | + * below as `seenRuleKey`) and inserts a rule only once per key. A conditional |
| 16 | + * rule whose prelude has one parenthesized condition, |
| 17 | + * `@media (min-width: 100px)`, is keyed by its prelude plus the key of the |
| 18 | + * rule inside it. Any other at-rule is keyed by its prelude alone: |
| 19 | + * - a prelude with more than one condition, such as |
| 20 | + * `@media (prefers-reduced-motion: reduce) and (pointer: coarse)`, at the |
| 21 | + * top level or inside a one-condition prelude; |
| 22 | + * - a named container query; |
| 23 | + * - `@starting-style`. |
| 24 | + * |
| 25 | + * jsdom then gets only the first rule written under such a key, and a test |
| 26 | + * that reads a later one reads nothing. This module inserts each of those |
| 27 | + * later rules itself, once, after StyleX's own sheet. The one exception is the |
| 28 | + * group last-member rule, which the test setup keeps out of jsdom |
| 29 | + * (jsdomRefusedRules.ts). |
| 30 | + * |
| 31 | + * facebook/stylex#1991 keys every conditional prelude by the rule inside it; |
| 32 | + * `@starting-style` rules still share one key there. Remove this module once |
| 33 | + * StyleX keys every rule by its own selector. |
| 34 | + * |
| 35 | + * SYNC: When modified, update this header, /vitest.config.ts and |
| 36 | + * /internal/test-utils/src/README.md |
| 37 | + */ |
| 38 | + |
| 39 | +import inject from '@stylexjs/stylex/lib/stylex-inject'; |
| 40 | +import {GROUP_LAST_MEMBER} from './jsdomRefusedRules'; |
| 41 | + |
| 42 | +type InjectArgs = Parameters<typeof inject>[0]; |
| 43 | + |
| 44 | +// StyleX 0.19.0's `getSeenRuleKey`, as written in `lib/es/inject.mjs`. |
| 45 | +const SELECTOR = /\s*([,])\s*/g; |
| 46 | +const ONE_CONDITION = /^@(media|supports|container)\s*\([^)]+\)\s*{/; |
| 47 | +function seenRuleKey(cssText: string): string | null { |
| 48 | + if (ONE_CONDITION.test(cssText)) { |
| 49 | + const index = cssText.indexOf('{'); |
| 50 | + const query = cssText.substring(0, index).trim(); |
| 51 | + const next = seenRuleKey(cssText.substring(index + 1).trim()); |
| 52 | + const inner = |
| 53 | + next != null && next !== '' ? next.replace(SELECTOR, '$1') : ''; |
| 54 | + return `${query} { ${inner}`; |
| 55 | + } |
| 56 | + const selector = cssText.split('{')[0].trim(); |
| 57 | + return selector !== '' ? selector.replace(SELECTOR, '$1') : null; |
| 58 | +} |
| 59 | + |
| 60 | +/** Whether StyleX keys `cssText` by an at-rule's prelude, not a selector. */ |
| 61 | +function keyedByPrelude(cssText: string): boolean { |
| 62 | + let rest = cssText; |
| 63 | + while (ONE_CONDITION.test(rest)) { |
| 64 | + rest = rest.slice(rest.indexOf('{') + 1).trim(); |
| 65 | + } |
| 66 | + return rest.startsWith('@'); |
| 67 | +} |
| 68 | + |
| 69 | +// The keys StyleX has inserted a rule under, every rule under such a key that |
| 70 | +// is in the page (the first, which StyleX inserted, and this module's), and |
| 71 | +// this module's sheet. They live on globalThis: `vi.resetModules()` loads this |
| 72 | +// module again, but StyleX's injector keeps its keys, so a record kept in |
| 73 | +// module state would insert restored rules again and lose a later rule |
| 74 | +// imported first after a reset. |
| 75 | +interface InjectRecord { |
| 76 | + readonly keys: Set<string>; |
| 77 | + readonly present: Set<string>; |
| 78 | + sheet: CSSStyleSheet | null; |
| 79 | +} |
| 80 | +const RECORD = Symbol.for('astryx.stylexTestInject'); |
| 81 | +const record: InjectRecord = (( |
| 82 | + globalThis as unknown as Record<symbol, InjectRecord | undefined> |
| 83 | +)[RECORD] ??= {keys: new Set(), present: new Set(), sheet: null}); |
| 84 | +const {keys, present} = record; |
| 85 | + |
| 86 | +function ownSheet(): CSSStyleSheet | null { |
| 87 | + if (typeof document === 'undefined') { |
| 88 | + return null; |
| 89 | + } |
| 90 | + if (record.sheet == null || !(record.sheet.ownerNode?.isConnected ?? false)) { |
| 91 | + const style = document.createElement('style'); |
| 92 | + style.setAttribute('data-stylex-test-inject', ''); |
| 93 | + document.head.append(style); |
| 94 | + record.sheet = style.sheet; |
| 95 | + } |
| 96 | + return record.sheet; |
| 97 | +} |
| 98 | + |
| 99 | +export default function stylexTestInject(args: InjectArgs): string { |
| 100 | + const text = inject(args); |
| 101 | + if (text === '' || !keyedByPrelude(text)) { |
| 102 | + return text; |
| 103 | + } |
| 104 | + const key = seenRuleKey(text); |
| 105 | + if (key == null) { |
| 106 | + return text; |
| 107 | + } |
| 108 | + if (!keys.has(key)) { |
| 109 | + // The first rule under this key: StyleX inserted it. |
| 110 | + keys.add(key); |
| 111 | + present.add(text); |
| 112 | + return text; |
| 113 | + } |
| 114 | + if (present.has(text) || text.includes(GROUP_LAST_MEMBER)) { |
| 115 | + return text; |
| 116 | + } |
| 117 | + present.add(text); |
| 118 | + const target = ownSheet(); |
| 119 | + target?.insertRule(text, target.cssRules.length); |
| 120 | + return text; |
| 121 | +} |
0 commit comments