Skip to content

Commit

Permalink
css combinator can be a single byte
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 25, 2023
1 parent 39c3962 commit 0546cf7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (r *RSelector) Hash() (uint32, bool) {
for _, sub := range sel.SubclassSelectors {
hash = helpers.HashCombine(hash, sub.Hash())
}
hash = helpers.HashCombineString(hash, sel.Combinator)
hash = helpers.HashCombine(hash, uint32(sel.Combinator))
}
}
hash = HashRules(hash, r.Rules)
Expand Down Expand Up @@ -629,10 +629,10 @@ func (a ComplexSelector) Equal(b ComplexSelector, check *CrossFileEqualityCheck)
}

type CompoundSelector struct {
Combinator string // Optional, may be ""
TypeSelector *NamespacedName
SubclassSelectors []SS
HasNestingSelector bool // "&"
Combinator uint8 // Optional, may be 0
HasNestingSelector bool // "&"
}

type NameToken struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func isSafeSelectors(complexSelectors []css_ast.ComplexSelector) bool {
return false
}

if compound.Combinator != "" {
if compound.Combinator != 0 {
// "Before Internet Explorer 10, the combinator only works in standards mode"
// Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
return false
Expand Down
14 changes: 7 additions & 7 deletions internal/css_parser/css_parser_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *parser) parseComplexSelector(opts parseSelectorOpts) (result css_ast.Co
// This is an extension: https://drafts.csswg.org/css-nesting-1/
r := p.current().Range
combinator := p.parseCombinator()
if combinator != "" {
if combinator != 0 {
if opts.isTopLevel {
p.maybeWarnAboutNesting(r)
}
Expand All @@ -74,7 +74,7 @@ func (p *parser) parseComplexSelector(opts parseSelectorOpts) (result css_ast.Co

// Optional combinator
combinator := p.parseCombinator()
if combinator != "" {
if combinator != 0 {
p.eat(css_lexer.TWhitespace)
}

Expand Down Expand Up @@ -366,21 +366,21 @@ loop:
return tokens
}

func (p *parser) parseCombinator() string {
func (p *parser) parseCombinator() uint8 {
switch p.current().Kind {
case css_lexer.TDelimGreaterThan:
p.advance()
return ">"
return '>'

case css_lexer.TDelimPlus:
p.advance()
return "+"
return '+'

case css_lexer.TDelimTilde:
p.advance()
return "~"
return '~'

default:
return ""
return 0
}
}
6 changes: 3 additions & 3 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,18 @@ func (p *printer) printComplexSelectors(selectors []css_ast.ComplexSelector, ind
}

func (p *printer) printCompoundSelector(sel css_ast.CompoundSelector, isFirst bool, isLast bool) {
if !isFirst && sel.Combinator == "" {
if !isFirst && sel.Combinator == 0 {
// A space is required in between compound selectors if there is no
// combinator in the middle. It's fine to convert "a + b" into "a+b"
// but not to convert "a b" into "ab".
p.print(" ")
}

if sel.Combinator != "" {
if sel.Combinator != 0 {
if !isFirst && !p.options.MinifyWhitespace {
p.print(" ")
}
p.print(sel.Combinator)
p.css = append(p.css, sel.Combinator)
if !p.options.MinifyWhitespace {
p.print(" ")
}
Expand Down

0 comments on commit 0546cf7

Please sign in to comment.