Source File
css_decls.go
Belonging Package
github.com/evanw/esbuild/internal/css_parser
package css_parser
import (
)
var shortColorName = map[uint32]string{
0x000080ff: "navy",
0x008000ff: "green",
0x008080ff: "teal",
0x4b0082ff: "indigo",
0x800000ff: "maroon",
0x800080ff: "purple",
0x808000ff: "olive",
0x808080ff: "gray",
0xa0522dff: "sienna",
0xa52a2aff: "brown",
0xc0c0c0ff: "silver",
0xcd853fff: "peru",
0xd2b48cff: "tan",
0xda70d6ff: "orchid",
0xdda0ddff: "plum",
0xee82eeff: "violet",
0xf0e68cff: "khaki",
0xf0ffffff: "azure",
0xf5deb3ff: "wheat",
0xf5f5dcff: "beige",
0xfa8072ff: "salmon",
0xfaf0e6ff: "linen",
0xff0000ff: "red",
0xff6347ff: "tomato",
0xff7f50ff: "coral",
0xffa500ff: "orange",
0xffc0cbff: "pink",
0xffd700ff: "gold",
0xffe4c4ff: "bisque",
0xfffafaff: "snow",
0xfffff0ff: "ivory",
}
var colorNameToHex = map[string]uint32{
"black": 0x000000ff,
"silver": 0xc0c0c0ff,
"gray": 0x808080ff,
"white": 0xffffffff,
"maroon": 0x800000ff,
"red": 0xff0000ff,
"purple": 0x800080ff,
"fuchsia": 0xff00ffff,
"green": 0x008000ff,
"lime": 0x00ff00ff,
"olive": 0x808000ff,
"yellow": 0xffff00ff,
"navy": 0x000080ff,
"blue": 0x0000ffff,
"teal": 0x008080ff,
"aqua": 0x00ffffff,
"orange": 0xffa500ff,
"aliceblue": 0xf0f8ffff,
"antiquewhite": 0xfaebd7ff,
"aquamarine": 0x7fffd4ff,
"azure": 0xf0ffffff,
"beige": 0xf5f5dcff,
"bisque": 0xffe4c4ff,
"blanchedalmond": 0xffebcdff,
"blueviolet": 0x8a2be2ff,
"brown": 0xa52a2aff,
"burlywood": 0xdeb887ff,
"cadetblue": 0x5f9ea0ff,
"chartreuse": 0x7fff00ff,
"chocolate": 0xd2691eff,
"coral": 0xff7f50ff,
"cornflowerblue": 0x6495edff,
"cornsilk": 0xfff8dcff,
"crimson": 0xdc143cff,
"cyan": 0x00ffffff,
"darkblue": 0x00008bff,
"darkcyan": 0x008b8bff,
"darkgoldenrod": 0xb8860bff,
"darkgray": 0xa9a9a9ff,
"darkgreen": 0x006400ff,
"darkgrey": 0xa9a9a9ff,
"darkkhaki": 0xbdb76bff,
"darkmagenta": 0x8b008bff,
"darkolivegreen": 0x556b2fff,
"darkorange": 0xff8c00ff,
"darkorchid": 0x9932ccff,
"darkred": 0x8b0000ff,
"darksalmon": 0xe9967aff,
"darkseagreen": 0x8fbc8fff,
"darkslateblue": 0x483d8bff,
"darkslategray": 0x2f4f4fff,
"darkslategrey": 0x2f4f4fff,
"darkturquoise": 0x00ced1ff,
"darkviolet": 0x9400d3ff,
"deeppink": 0xff1493ff,
"deepskyblue": 0x00bfffff,
"dimgray": 0x696969ff,
"dimgrey": 0x696969ff,
"dodgerblue": 0x1e90ffff,
"firebrick": 0xb22222ff,
"floralwhite": 0xfffaf0ff,
"forestgreen": 0x228b22ff,
"gainsboro": 0xdcdcdcff,
"ghostwhite": 0xf8f8ffff,
"gold": 0xffd700ff,
"goldenrod": 0xdaa520ff,
"greenyellow": 0xadff2fff,
"grey": 0x808080ff,
"honeydew": 0xf0fff0ff,
"hotpink": 0xff69b4ff,
"indianred": 0xcd5c5cff,
"indigo": 0x4b0082ff,
"ivory": 0xfffff0ff,
"khaki": 0xf0e68cff,
"lavender": 0xe6e6faff,
"lavenderblush": 0xfff0f5ff,
"lawngreen": 0x7cfc00ff,
"lemonchiffon": 0xfffacdff,
"lightblue": 0xadd8e6ff,
"lightcoral": 0xf08080ff,
"lightcyan": 0xe0ffffff,
"lightgoldenrodyellow": 0xfafad2ff,
"lightgray": 0xd3d3d3ff,
"lightgreen": 0x90ee90ff,
"lightgrey": 0xd3d3d3ff,
"lightpink": 0xffb6c1ff,
"lightsalmon": 0xffa07aff,
"lightseagreen": 0x20b2aaff,
"lightskyblue": 0x87cefaff,
"lightslategray": 0x778899ff,
"lightslategrey": 0x778899ff,
"lightsteelblue": 0xb0c4deff,
"lightyellow": 0xffffe0ff,
"limegreen": 0x32cd32ff,
"linen": 0xfaf0e6ff,
"magenta": 0xff00ffff,
"mediumaquamarine": 0x66cdaaff,
"mediumblue": 0x0000cdff,
"mediumorchid": 0xba55d3ff,
"mediumpurple": 0x9370dbff,
"mediumseagreen": 0x3cb371ff,
"mediumslateblue": 0x7b68eeff,
"mediumspringgreen": 0x00fa9aff,
"mediumturquoise": 0x48d1ccff,
"mediumvioletred": 0xc71585ff,
"midnightblue": 0x191970ff,
"mintcream": 0xf5fffaff,
"mistyrose": 0xffe4e1ff,
"moccasin": 0xffe4b5ff,
"navajowhite": 0xffdeadff,
"oldlace": 0xfdf5e6ff,
"olivedrab": 0x6b8e23ff,
"orangered": 0xff4500ff,
"orchid": 0xda70d6ff,
"palegoldenrod": 0xeee8aaff,
"palegreen": 0x98fb98ff,
"paleturquoise": 0xafeeeeff,
"palevioletred": 0xdb7093ff,
"papayawhip": 0xffefd5ff,
"peachpuff": 0xffdab9ff,
"peru": 0xcd853fff,
"pink": 0xffc0cbff,
"plum": 0xdda0ddff,
"powderblue": 0xb0e0e6ff,
"rosybrown": 0xbc8f8fff,
"royalblue": 0x4169e1ff,
"saddlebrown": 0x8b4513ff,
"salmon": 0xfa8072ff,
"sandybrown": 0xf4a460ff,
"seagreen": 0x2e8b57ff,
"seashell": 0xfff5eeff,
"sienna": 0xa0522dff,
"skyblue": 0x87ceebff,
"slateblue": 0x6a5acdff,
"slategray": 0x708090ff,
"slategrey": 0x708090ff,
"snow": 0xfffafaff,
"springgreen": 0x00ff7fff,
"steelblue": 0x4682b4ff,
"tan": 0xd2b48cff,
"thistle": 0xd8bfd8ff,
"tomato": 0xff6347ff,
"turquoise": 0x40e0d0ff,
"violet": 0xee82eeff,
"wheat": 0xf5deb3ff,
"whitesmoke": 0xf5f5f5ff,
"yellowgreen": 0x9acd32ff,
"rebeccapurple": 0x663399ff,
}
func ( string) (uint32, bool) {
:= uint32(0)
for , := range {
<<= 4
switch {
case >= '0' && <= '9':
|= uint32() - '0'
case >= 'a' && <= 'f':
|= uint32() - ('a' - 10)
case >= 'A' && <= 'F':
|= uint32() - ('A' - 10)
default:
return 0, false
}
}
return , true
}
func ( uint32) uint32 {
return (( & 0xF000) << 16) | (( & 0xFF00) << 12) | (( & 0x0FF0) << 8) | (( & 0x00FF) << 4) | ( & 0x000F)
}
func ( uint32) int { return int( >> 24) }
func ( uint32) int { return int(( >> 16) & 255) }
func ( uint32) int { return int(( >> 8) & 255) }
func ( uint32) int { return int( & 255) }
func ( float64) string {
:= fmt.Sprintf("%.03f", )
for [len()-1] == '0' {
= [:len()-1]
}
if [len()-1] == '.' {
= [:len()-1]
}
return
}
func ( css_ast.Token) (float64, bool) {
switch .Kind {
case css_lexer.TNumber:
if , := strconv.ParseFloat(.Text, 64); == nil {
return , true
}
case css_lexer.TDimension:
if , := strconv.ParseFloat(.DimensionValue(), 64); == nil {
switch .DimensionUnit() {
case "deg":
return , true
case "grad":
return * (360.0 / 400.0), true
case "rad":
return * (180.0 / math.Pi), true
case "turn":
return * 360.0, true
}
}
}
return 0, false
}
func ( css_ast.Token) css_ast.Token {
if .Kind == css_lexer.TPercentage {
if , := strconv.ParseFloat(.Text[:len(.Text)-1], 64); == nil {
.Kind = css_lexer.TNumber
.Text = floatToString( / 100.0)
}
}
return
}
func ( *parser) () css_ast.Token {
:= css_ast.Token{
Kind: css_lexer.TComma,
Text: ",",
}
if !.options.RemoveWhitespace {
.Whitespace = css_ast.WhitespaceAfter
}
return
}
if , := parseHex(); {
= expandHex()
.Kind = css_lexer.TFunction
.Text = "rgba"
:= .commaToken()
.Children = &[]css_ast.Token{
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR())}, ,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG())}, ,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB())}, ,
{Kind: css_lexer.TNumber, Text: floatToString(float64(hexA()) / 255)},
}
}
if , := parseHex(); {
.Kind = css_lexer.TFunction
.Text = "rgba"
:= .commaToken()
.Children = &[]css_ast.Token{
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR())}, ,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG())}, ,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB())}, ,
{Kind: css_lexer.TNumber, Text: floatToString(float64(hexA()) / 255)},
}
}
}
}
case css_lexer.TIdent:
if == "rebeccapurple" && .options.UnsupportedCSSFeatures.Has(compat.RebeccaPurple) {
.Kind = css_lexer.THash
.Text = "663399"
}
case css_lexer.TFunction:
switch {
case "rgb", "rgba", "hsl", "hsla":
if .options.UnsupportedCSSFeatures.Has(compat.Modern_RGB_HSL) {
:= *.Children
:= false
:= false
if ( == "hsl" || == "hsla") && len() > 0 {
if , := degreesForAngle([0]); {
[0].Kind = css_lexer.TNumber
[0].Text = floatToString()
}
}
switch len() {
= true
[0].Whitespace = 0
[1].Whitespace = 0
:= .commaToken()
.Children = &[]css_ast.Token{
[0], ,
[1], ,
[2],
}
if [3].Kind == css_lexer.TDelimSlash {
= true
[0].Whitespace = 0
[1].Whitespace = 0
[2].Whitespace = 0
:= .commaToken()
.Children = &[]css_ast.Token{
[0], ,
[1], ,
[2], ,
lowerAlphaPercentageToNumber([4]),
}
}
if [1].Kind == css_lexer.TComma && [3].Kind == css_lexer.TComma && [5].Kind == css_lexer.TComma {
= true
[6] = lowerAlphaPercentageToNumber([6])
}
}
if {
if == "rgba" {
.Text = "rgb"
} else if == "hsla" {
.Text = "hsl"
}
} else if {
if == "rgb" {
.Text = "rgba"
} else if == "hsl" {
.Text = "hsla"
}
}
}
}
}
return
}
func ( css_ast.Token) (uint32, bool) {
:= .Text
switch .Kind {
case css_lexer.TIdent:
if , := colorNameToHex[strings.ToLower()]; {
return , true
}
case css_lexer.THash:
switch len() {
, , = [0], [1], [2]
if [3].Kind == css_lexer.TDelimSlash {
, , , = [0], [1], [2], [4]
}
if [1].Kind == css_lexer.TComma && [3].Kind == css_lexer.TComma && [5].Kind == css_lexer.TComma {
, , , = [0], [2], [4], [6]
}
}
if , := parseColorByte(, 1); {
if , := parseColorByte(, 1); {
if , := parseColorByte(, 1); {
if , := parseAlphaByte(); {
return uint32(( << 24) | ( << 16) | ( << 8) | ), true
}
}
}
}
case "hsl", "hsla":
:= *.Children
var , , , css_ast.Token
switch len() {
, , = [0], [1], [2]
if [3].Kind == css_lexer.TDelimSlash {
, , , = [0], [1], [2], [4]
}
if , := degreesForAngle(); {
if , := fractionForPercentage(); {
if , := fractionForPercentage(); {
if , := parseAlphaByte(); {
/= 360.0
var float64
if <= 0.5 {
= * ( + 1)
} else {
= + - ( * )
}
:= *2 -
:= hueToRgb(, , +1.0/3.0)
:= hueToRgb(, , )
:= hueToRgb(, , -1.0/3.0)
return uint32(( << 24) | ( << 16) | ( << 8) | ), true
}
}
}
}
}
}
return 0, false
}
func ( float64, float64, float64) uint32 {
-= math.Floor()
*= 6.0
var float64
if < 1 {
= (-)* +
} else if < 3 {
=
} else if < 4 {
= (-)*(4-) +
} else {
=
}
:= int(math.Round( * 255))
if < 0 {
= 0
} else if > 255 {
= 255
}
return uint32()
}
func ( css_ast.Token) (float64, bool) {
if .Kind == css_lexer.TPercentage {
if , := strconv.ParseFloat(.PercentValue(), 64); == nil {
if < 0 {
return 0, true
}
if > 100 {
return 1, true
}
return / 100.0, true
}
}
return 0, false
}
func ( css_ast.Token) (uint32, bool) {
if .Kind == css_lexer.T(0) {
return 255, true
}
return parseColorByte(, 255)
}
func ( css_ast.Token, float64) (uint32, bool) {
var int
var bool
switch .Kind {
case css_lexer.TNumber:
if , := strconv.ParseFloat(.Text, 64); == nil {
= int(math.Round( * ))
= true
}
case css_lexer.TPercentage:
if , := strconv.ParseFloat(.PercentValue(), 64); == nil {
= int(math.Round( * (255.0 / 100.0)))
= true
}
}
if < 0 {
= 0
} else if > 255 {
= 255
}
return uint32(),
}
if , := parseColor(); {
if hexA() == 255 {
.Children = nil
if , := shortColorName[]; {
.Kind = css_lexer.TIdent
.Text =
} else {
.Kind = css_lexer.THash
>>= 8
:= compactHex()
if == expandHex() {
.Text = fmt.Sprintf("%03x", )
} else {
.Text = fmt.Sprintf("%06x", )
}
}
} else if !.options.UnsupportedCSSFeatures.Has(compat.HexRGBA) {
.Children = nil
.Kind = css_lexer.THash
:= compactHex()
if == expandHex() {
.Text = fmt.Sprintf("%04x", )
} else {
.Text = fmt.Sprintf("%08x", )
}
} else {
.Kind = css_lexer.TFunction
.Text = "rgba"
:= .commaToken()
:= floatToString(float64(hexA()) / 255)
if .options.MangleSyntax {
if , := mangleNumber(); {
=
}
}
.Children = &[]css_ast.Token{
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR())}, ,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG())}, ,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB())}, ,
{Kind: css_lexer.TNumber, Text: },
}
}
}
return
}
func ( *parser) ( []css_ast.R) {
for , := range {
, := .(*css_ast.RDeclaration)
if ! {
continue
}
switch .Key {
case css_ast.DBackgroundColor,
css_ast.DBorderBlockEndColor,
css_ast.DBorderBlockStartColor,
css_ast.DBorderBottomColor,
css_ast.DBorderColor,
css_ast.DBorderInlineEndColor,
css_ast.DBorderInlineStartColor,
css_ast.DBorderLeftColor,
css_ast.DBorderRightColor,
css_ast.DBorderTopColor,
css_ast.DCaretColor,
css_ast.DColor,
css_ast.DColumnRuleColor,
css_ast.DFloodColor,
css_ast.DLightingColor,
css_ast.DOutlineColor,
css_ast.DStopColor,
css_ast.DTextDecorationColor,
css_ast.DTextEmphasisColor:
if len(.Value) == 1 {
.Value[0] = .lowerColor(.Value[0])
if .options.MangleSyntax {
.Value[0] = .mangleColor(.Value[0])
}
}
}
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |