Skip to content

Commit

Permalink
css: emit mappings for type selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 16, 2023
1 parent 002e020 commit 1bce9c1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
9 changes: 7 additions & 2 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,14 @@ func (sel CompoundSelector) Clone() CompoundSelector {

type NameToken struct {
Text string
Loc logger.Loc
Kind css_lexer.T
}

func (a NameToken) Equal(b NameToken) bool {
return a.Text == b.Text && a.Kind == b.Kind
}

type NamespacedName struct {
// If present, this is an identifier or "*" and is followed by a "|" character
NamespacePrefix *NameToken
Expand All @@ -780,8 +785,8 @@ func (n NamespacedName) Clone() NamespacedName {
}

func (a NamespacedName) Equal(b NamespacedName) bool {
return a.Name == b.Name && (a.NamespacePrefix == nil) == (b.NamespacePrefix == nil) &&
(a.NamespacePrefix == nil || b.NamespacePrefix == nil || *a.NamespacePrefix == *b.NamespacePrefix)
return a.Name.Equal(b.Name) && (a.NamespacePrefix == nil) == (b.NamespacePrefix == nil) &&
(a.NamespacePrefix == nil || b.NamespacePrefix == nil || a.NamespacePrefix.Equal(b.Name))
}

type SS interface {
Expand Down
4 changes: 3 additions & 1 deletion internal/css_parser/css_parser_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,10 @@ func (p *parser) parseComplexSelector(opts parseComplexSelectorOpts) (result css
}

func (p *parser) nameToken() css_ast.NameToken {
t := p.current()
return css_ast.NameToken{
Kind: p.current().Kind,
Kind: t.Kind,
Loc: t.Range.Loc,
Text: p.decoded(),
}
}
Expand Down
14 changes: 11 additions & 3 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,14 @@ func (p *printer) printCompoundSelector(sel css_ast.CompoundSelector, isFirst bo
}

func (p *printer) printNamespacedName(nsName css_ast.NamespacedName, whitespace trailingWhitespace) {
if nsName.NamespacePrefix != nil {
switch nsName.NamespacePrefix.Kind {
if prefix := nsName.NamespacePrefix; prefix != nil {
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(prefix.Loc, "", p.css)
}

switch prefix.Kind {
case css_lexer.TIdent:
p.printIdent(nsName.NamespacePrefix.Text, identNormal, canDiscardWhitespaceAfter)
p.printIdent(prefix.Text, identNormal, canDiscardWhitespaceAfter)
case css_lexer.TDelimAsterisk:
p.print("*")
default:
Expand All @@ -533,6 +537,10 @@ func (p *printer) printNamespacedName(nsName css_ast.NamespacedName, whitespace
p.print("|")
}

if p.options.AddSourceMappings {
p.builder.AddSourceMapping(nsName.Name.Loc, "", p.css)
}

switch nsName.Name.Kind {
case css_lexer.TIdent:
p.printIdent(nsName.Name.Text, identNormal, whitespace)
Expand Down

0 comments on commit 1bce9c1

Please sign in to comment.