镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 2b3a5ab

Browse files
committed
fix: stop the important modifier mutating shared rule property objects
addRule appended ' !important' to the properties object in place. Static utility maps and custom config rules hand out shared references, so one !-flagged class permanently poisoned the shared object: a second generator instance stacked ' !important !important', and later non-important uses of the same class emitted the flagged declarations. The modifier now writes to a fresh object.
1 parent d53348d commit 2b3a5ab

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎packages/crosswind/src/generator.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,11 +2058,16 @@ export class CSSGenerator {
20582058

20592059
const mediaQuery = this.getMediaQuery(parsed)
20602060

2061-
// Apply !important modifier
2061+
// Apply !important modifier. Build a fresh object — `properties` is
2062+
// often a shared reference (static utility maps, memoized custom-rule
2063+
// results), and mutating it in place poisoned every later use of the
2064+
// same object with stacking ' !important' suffixes.
20622065
if (parsed.important) {
2066+
const flagged: Record<string, string> = {}
20632067
for (const key in properties) {
2064-
properties[key] += ' !important'
2068+
flagged[key] = `${properties[key]} !important`
20652069
}
2070+
properties = flagged
20662071
}
20672072

20682073
const key = mediaQuery || 'base'

‎packages/crosswind/test/generator.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,3 +803,34 @@ describe('CSSGenerator - Edge Cases', () => {
803803
})
804804
})
805805
})
806+
807+
describe('important modifier isolation', () => {
808+
// addRule must not mutate the properties object it receives: custom rules
809+
// and static utility maps hand out shared references, and an in-place
810+
// ' !important' append poisoned every later use of the same object
811+
// (stacking suffixes across generator instances).
812+
it('does not mutate shared custom-rule objects', () => {
813+
const SHARED: Record<string, string> = { color: 'red' }
814+
const config = {
815+
...defaultConfig,
816+
rules: [[/^!?myred$/, () => SHARED]] as any,
817+
}
818+
819+
const genA = new CSSGenerator(config)
820+
genA.generate('!myred')
821+
expect(genA.toCSS(false)).toContain('color: red !important;')
822+
expect(SHARED.color).toBe('red')
823+
824+
// A second instance generating the important form must not double-append
825+
const genB = new CSSGenerator(config)
826+
genB.generate('!myred')
827+
expect(genB.toCSS(false)).toContain('color: red !important;')
828+
expect(genB.toCSS(false)).not.toContain('!important !important')
829+
830+
// The plain form stays non-important
831+
const genC = new CSSGenerator(config)
832+
genC.generate('myred')
833+
expect(genC.toCSS(false)).toContain('color: red;')
834+
expect(genC.toCSS(false)).not.toContain('!important')
835+
})
836+
})

0 commit comments

Comments
 (0)