Skip to content

Commit

Permalink
css: case-insensitive color functions
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 8, 2023
1 parent 5671059 commit 6fa536b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/css_parser/css_decls_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,13 @@ func (p *parser) lowerAndMinifyColor(token css_ast.Token, wouldClamp *bool) css_
}

case css_lexer.TIdent:
if text == "rebeccapurple" && p.options.unsupportedCSSFeatures.Has(compat.RebeccaPurple) {
if p.options.unsupportedCSSFeatures.Has(compat.RebeccaPurple) && strings.EqualFold(text, "rebeccapurple") {
token.Kind = css_lexer.THash
token.Text = "663399"
}

case css_lexer.TFunction:
switch text {
switch strings.ToLower(text) {
case "rgb", "rgba", "hsl", "hsla":
if p.options.unsupportedCSSFeatures.Has(compat.Modern_RGB_HSL) {
args := *token.Children
Expand Down Expand Up @@ -379,15 +379,15 @@ func (p *parser) lowerAndMinifyColor(token css_ast.Token, wouldClamp *bool) css_
}

if removeAlpha {
if text == "rgba" {
if strings.EqualFold(text, "rgba") {
token.Text = "rgb"
} else if text == "hsla" {
} else if strings.EqualFold(text, "hsla") {
token.Text = "hsl"
}
} else if addAlpha {
if text == "rgb" {
if strings.EqualFold(text, "rgb") {
token.Text = "rgba"
} else if text == "hsl" {
} else if strings.EqualFold(text, "hsl") {
token.Text = "hsla"
}
}
Expand Down Expand Up @@ -463,7 +463,7 @@ func parseColor(token css_ast.Token) (parsedColor, bool) {
}

case css_lexer.TFunction:
switch text {
switch strings.ToLower(text) {
case "rgb", "rgba":
args := *token.Children
var r, g, b, a css_ast.Token
Expand Down Expand Up @@ -591,7 +591,7 @@ func parseColor(token css_ast.Token) (parsedColor, bool) {
if v1, ok := args[2].NumberOrFractionForPercentage(); ok {
if v2, ok := args[3].NumberOrFractionForPercentage(); ok {
if a, ok := parseAlphaByte(alpha); ok {
switch colorSpace.Text {
switch strings.ToLower(colorSpace.Text) {
case "a98-rgb":
r, g, b := lin_a98rgb(v0, v1, v2)
x, y, z := lin_a98rgb_to_xyz(r, g, b)
Expand Down
11 changes: 11 additions & 0 deletions internal/css_parser/css_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ func TestColorFunction(t *testing.T) {
expectPrintedLowerMangle(t, "a { before: 0; box-shadow: 1px color(display-p3 1 0 0 / 0.5); after: 1 }",
"a {\n before: 0;\n box-shadow: 1px rgba(255, 15, 14, .5);\n box-shadow: 1px color(display-p3 1 0 0 / .5);\n after: 1;\n}\n", "")

// Check case sensitivity
expectPrintedLower(t, "a { color: color(srgb 0.87 0.98 0.807) }", "a {\n color: #deface;\n}\n", "")
expectPrintedLower(t, "A { Color: Color(Srgb 0.87 0.98 0.807) }", "A {\n Color: #deface;\n}\n", "")
expectPrintedLower(t, "A { COLOR: COLOR(SRGB 0.87 0.98 0.807) }", "A {\n COLOR: #deface;\n}\n", "")

// Check in-range colors in various color spaces
expectPrintedLower(t, "a { color: color(a98-rgb 0.9 0.98 0.81) }", "a {\n color: #deface;\n}\n", "")
expectPrintedLower(t, "a { color: color(a98-rgb 90% 98% 81%) }", "a {\n color: #deface;\n}\n", "")
Expand Down Expand Up @@ -640,6 +645,7 @@ func TestColorHSLA(t *testing.T) {

func TestLowerColor(t *testing.T) {
expectPrintedLower(t, "a { color: rebeccapurple }", "a {\n color: #663399;\n}\n", "")
expectPrintedLower(t, "a { color: ReBeCcApUrPlE }", "a {\n color: #663399;\n}\n", "")

expectPrintedLower(t, "a { color: #0123 }", "a {\n color: rgba(0, 17, 34, .2);\n}\n", "")
expectPrintedLower(t, "a { color: #1230 }", "a {\n color: rgba(17, 34, 51, 0);\n}\n", "")
Expand All @@ -660,9 +666,13 @@ func TestLowerColor(t *testing.T) {
expectPrintedLower(t, "a { color: hsla(-200grad 2% 3%) }", "a {\n color: hsl(-180, 2%, 3%);\n}\n", "")

expectPrintedLower(t, "a { color: rgb(1 2 3 / 4) }", "a {\n color: rgba(1, 2, 3, 4);\n}\n", "")
expectPrintedLower(t, "a { color: RGB(1 2 3 / 4) }", "a {\n color: rgba(1, 2, 3, 4);\n}\n", "")
expectPrintedLower(t, "a { color: rgba(1% 2% 3% / 4%) }", "a {\n color: rgba(1%, 2%, 3%, 0.04);\n}\n", "")
expectPrintedLower(t, "a { color: RGBA(1% 2% 3% / 4%) }", "a {\n color: RGBA(1%, 2%, 3%, 0.04);\n}\n", "")
expectPrintedLower(t, "a { color: hsl(1 2% 3% / 4) }", "a {\n color: hsla(1, 2%, 3%, 4);\n}\n", "")
expectPrintedLower(t, "a { color: HSL(1 2% 3% / 4) }", "a {\n color: hsla(1, 2%, 3%, 4);\n}\n", "")
expectPrintedLower(t, "a { color: hsla(1 2% 3% / 4%) }", "a {\n color: hsla(1, 2%, 3%, 0.04);\n}\n", "")
expectPrintedLower(t, "a { color: HSLA(1 2% 3% / 4%) }", "a {\n color: HSLA(1, 2%, 3%, 0.04);\n}\n", "")

expectPrintedLower(t, "a { color: rgb(1, 2, 3, 4) }", "a {\n color: rgba(1, 2, 3, 4);\n}\n", "")
expectPrintedLower(t, "a { color: rgba(1%, 2%, 3%, 4%) }", "a {\n color: rgba(1%, 2%, 3%, 0.04);\n}\n", "")
Expand All @@ -673,6 +683,7 @@ func TestLowerColor(t *testing.T) {
expectPrintedLower(t, "a { color: hsl(1deg, 2%, 3%, 0.4%) }", "a {\n color: hsla(1, 2%, 3%, 0.004);\n}\n", "")

expectPrintedLower(t, "a { color: hwb(90deg 20% 40%) }", "a {\n color: #669933;\n}\n", "")
expectPrintedLower(t, "a { color: HWB(90deg 20% 40%) }", "a {\n color: #669933;\n}\n", "")
expectPrintedLower(t, "a { color: hwb(90deg 20% 40% / 0.2) }", "a {\n color: rgba(102, 153, 51, .2);\n}\n", "")
expectPrintedLower(t, "a { color: hwb(1deg 40% 80%) }", "a {\n color: #555555;\n}\n", "")
expectPrintedLower(t, "a { color: hwb(1deg 9000% 50%) }", "a {\n color: #aaaaaa;\n}\n", "")
Expand Down

0 comments on commit 6fa536b

Please sign in to comment.