From 0546cf7a8cb1d89409e4634bdb3b5d6c65e0a6c5 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Fri, 24 Mar 2023 23:33:28 -0400 Subject: [PATCH] css combinator can be a single byte --- internal/css_ast/css_ast.go | 6 +++--- internal/css_parser/css_parser.go | 2 +- internal/css_parser/css_parser_selector.go | 14 +++++++------- internal/css_printer/css_printer.go | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/css_ast/css_ast.go b/internal/css_ast/css_ast.go index ae94ecff2fd..a5ed930062b 100644 --- a/internal/css_ast/css_ast.go +++ b/internal/css_ast/css_ast.go @@ -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) @@ -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 { diff --git a/internal/css_parser/css_parser.go b/internal/css_parser/css_parser.go index 424234ead28..c254b3122a9 100644 --- a/internal/css_parser/css_parser.go +++ b/internal/css_parser/css_parser.go @@ -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 diff --git a/internal/css_parser/css_parser_selector.go b/internal/css_parser/css_parser_selector.go index c776e8544ac..ae74d6996f2 100644 --- a/internal/css_parser/css_parser_selector.go +++ b/internal/css_parser/css_parser_selector.go @@ -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) } @@ -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) } @@ -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 } } diff --git a/internal/css_printer/css_printer.go b/internal/css_printer/css_printer.go index 10a9a26d572..7ff2a34f834 100644 --- a/internal/css_printer/css_printer.go +++ b/internal/css_printer/css_printer.go @@ -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(" ") }