Skip to content

Commit

Permalink
update light-dark regex to support different color syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
VladBrok committed Feb 14, 2024
1 parent 08d3d43 commit dc8ec90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const PREFERS_COLOR_ONLY = /^\(\s*prefers-color-scheme\s*:\s*(dark|light)\s*\)$/
const PREFERS_COLOR = /\(\s*prefers-color-scheme\s*:\s*(dark|light)\s*\)/g
const LIGHT_DARK = /light-dark\(\s*(.+?)\s*,\s*(.+?)\s*\)/g
const LIGHT_DARK = /light-dark\(\s*((?:[^(),]|\(.+\))+?)\s*,\s*((?:[^(),]|\(.+\))+?)\s*\)/gs
const STRING = /"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'/dg

function escapeRegExp(string) {
Expand Down
53 changes: 53 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,59 @@ html:where(.is-light) {
)
})

test('transforms light-dark() with various color formats', () => {
run(
`html {
border: 1px solid light-dark(rgb(0, 0, 0), var(--color));
color: light-dark( hsla(120, 100%, 50%, 0.3) , hsl(
var(--red-hue)
var(--red-sat)
calc(var(--red-lit) - 20%)
));
}`,
`@media (prefers-color-scheme:dark) {
html:where(:not(.is-light)) {
color: hsl(
var(--red-hue)
var(--red-sat)
calc(var(--red-lit) - 20%)
)
}
}
html:where(.is-dark) {
color: hsl(
var(--red-hue)
var(--red-sat)
calc(var(--red-lit) - 20%)
)
}
@media (prefers-color-scheme:light) {
html:where(:not(.is-dark)) {
color: hsla(120, 100%, 50%, 0.3)
}
}
html:where(.is-light) {
color: hsla(120, 100%, 50%, 0.3)
}
@media (prefers-color-scheme:dark) {
html:where(:not(.is-light)) {
border: 1px solid var(--color)
}
}
html:where(.is-dark) {
border: 1px solid var(--color)
}
@media (prefers-color-scheme:light) {
html:where(:not(.is-dark)) {
border: 1px solid rgb(0, 0, 0)
}
}
html:where(.is-light) {
border: 1px solid rgb(0, 0, 0)
}`
)
})

test('does not transform light-dark() inside strings', () => {
run(
`html {
Expand Down

0 comments on commit dc8ec90

Please sign in to comment.