Skip to content

CSS parser skips selectors before commas (e.g. .hello, .ivan → .ivan only) #127

@ivanorangedigital

Description

@ivanorangedigital

When parsing CSS with comma-separated selectors, only the last selector is kept.
This issue was found using the example code from the official documentation on pkg.go.dev/github.com/tdewolff/parse/v2/css.

Here’s the exact example (taken and slightly modified from the official docs):

package main

import (
	"bytes"
	"fmt"

	"github.com/tdewolff/parse/v2"
	"github.com/tdewolff/parse/v2/css"
)

func main() {
	r := bytes.NewBufferString(".hello, .x, .ivan {color: red;} .ivan {color: blue;} @keframes x {from {color:blue}}")
	// false because this is not inline style content
	p := css.NewParser(parse.NewInput(r), false)
	out := ""
	for {
		gt, _, data := p.Next()
		if gt == css.ErrorGrammar {
			break
		} else if gt == css.AtRuleGrammar || gt == css.BeginAtRuleGrammar || gt == css.BeginRulesetGrammar || gt == css.DeclarationGrammar {
			out += string(data)
			if gt == css.DeclarationGrammar {
				out += ":"
			}
			for _, val := range p.Values() {
				out += string(val.Data)
			}
			if gt == css.BeginAtRuleGrammar || gt == css.BeginRulesetGrammar {
				out += "{"
			} else if gt == css.AtRuleGrammar || gt == css.DeclarationGrammar {
				out += ";"
			}
		} else {
			out += string(data)
		}
	}
	fmt.Println(out)
}

Output:

.ivan{color:red;}.ivan{color:blue;}@keframes x{from {color:blue}}

Expected output:

.hello, .x, .ivan{color:red;}.ivan{color:blue;}@keframes x{from {color:blue}}

It seems that the parser skips selectors before commas, as if comma-separated selector lists are not properly recognized by the grammar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions