tdewolff / parse

Go parsers for web formats
MIT License
413 stars 65 forks source link

question about media queries #81

Closed dataf3l closed 3 years ago

dataf3l commented 3 years ago

question about media queries

is it possible to determine if a website has "media query" or responsiveness using this library? are "media queries" supported? do I need to use another library instead?

I see in the test code there is mention of "@media print", however it is unclear to me if somehting like:

@media only screen and (max-width: 600px) { body { background-color: lightblue; } }

would be supported by the parser and how the AST would look like for an expression like this one.

thanks in advance for your help, I think this an awesome project btw.

tdewolff commented 3 years ago

Yes you can, however the CSS parser is "online" as in you are parsing as you go (instead of generating an AST tree for you to inspect).

r := strings.NewReader("b{color:blue} @media (x=5) { a{color:red} }")
p := css.NewParser(parse.NewInput(r), false)
for {
    gt, _, data := p.Next()
    switch gt {
    case css.ErrorGrammar:
        return
    case css.BeginAtRuleGrammar:
        fmt.Println(string(data))
        for _, val := range p.Values() {
            fmt.Print(string(val.Data))
        }
    }
}

will print

@media
(x=5)
dataf3l commented 3 years ago

wow, this is amazing! I did not expect such a prompt response! you sir are awesome.