MCPcopy Create free account
hub / github.com/andybalholm/cascadia

github.com/andybalholm/cascadia

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.3.5 ↗ · + Follow · compare 2 versions
183 symbols 420 edges 12 files 80 documented · 44% ● updated 58d agov1.0.0 · 2018-02-20★ 760

Browse by type

⨍ Functions 151◇ Types & classes 32
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

cascadia

The Cascadia package implements CSS selectors for use with the parse trees produced by the html package.

To test CSS selectors without writing Go code, check out cascadia the command line tool, a thin wrapper around this package.

Refer to godoc here.

Example

The following is an example of how you can use Cascadia.

package main

import (
    "fmt"
    "log"
    "strings"

    "github.com/andybalholm/cascadia"
    "golang.org/x/net/html"
)

var pricingHtml string = `






        <h4 class="my-0 font-weight-normal">Free</h4>






        <h1 class="card-title pricing-card-title">$0/mo</h1>
        <ul class="list-unstyled mt-3 mb-4">
            <li>10 users included</li>
            <li>2 GB of storage</li>
            <li><a href="https://example.com">See more</a></li>
        </ul>













        <h4 class="my-0 font-weight-normal">Pro</h4>






        <h1 class="card-title pricing-card-title">$15/mo</h1>
        <ul class="list-unstyled mt-3 mb-4">
            <li>20 users included</li>
            <li>10 GB of storage</li>
            <li><a href="https://example.com">See more</a></li>
        </ul>













        <h4 class="my-0 font-weight-normal">Enterprise</h4>






        <h1 class="card-title pricing-card-title">$29/mo</h1>
        <ul class="list-unstyled mt-3 mb-4">
            <li>30 users included</li>
            <li>15 GB of storage</li>
            <li><a>See more</a></li>
        </ul>






`

func Query(n *html.Node, query string) *html.Node {
    sel, err := cascadia.Parse(query)
    if err != nil {
        return &html.Node{}
    }
    return cascadia.Query(n, sel)
}

func QueryAll(n *html.Node, query string) []*html.Node {
    sel, err := cascadia.Parse(query)
    if err != nil {
        return []*html.Node{}
    }
    return cascadia.QueryAll(n, sel)
}

func AttrOr(n *html.Node, attrName, or string) string {
    for _, a := range n.Attr {
        if a.Key == attrName {
            return a.Val
        }
    }
    return or
}

func main() {
    doc, err := html.Parse(strings.NewReader(pricingHtml))
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("List of pricing plans:\n\n")
    for i, p := range QueryAll(doc, "div.card.mb-4.box-shadow") {
        planName := Query(p, "h4").FirstChild.Data
        price := Query(p, ".pricing-card-title").FirstChild.Data
        usersIncluded := Query(p, "li:first-child").FirstChild.Data
        storage := Query(p, "li:nth-child(2)").FirstChild.Data
        detailsUrl := AttrOr(Query(p, "li:last-child a"), "href", "(No link available)")
        fmt.Printf(
            "Plan #%d\nName: %s\nPrice: %s\nUsers: %s\nStorage: %s\nDetails: %s\n\n",
            i+1,
            planName,
            price,
            usersIncluded,
            storage,
            detailsUrl,
        )
    }
}

The output is:

List of pricing plans:

Plan #1
Name: Free
Price: $0/mo
Users: 10 users included
Storage: 2 GB of storage
Details: https://example.com

Plan #2
Name: Pro
Price: $15/mo
Users: 20 users included
Storage: 10 GB of storage
Details: https://example.com

Plan #3
Name: Enterprise
Price: $29/mo
Users: 30 users included
Storage: 15 GB of storage
Details: (No link available)

Extension points exported contracts — how you extend this code

browse all types & interfaces →

Core symbols most depended-on inside this repo

browse all functions →

Shape

Method 89
Function 62
Struct 27
Interface 2
TypeAlias 2
FuncType 1

Languages

Go100%

Modules by API surface

selector.go64 symbols
pseudo_classes.go42 symbols
serialize.go23 symbols
parser.go23 symbols
selector_test.go12 symbols
w3_test.go6 symbols
specificity_test.go4 symbols
specificity.go3 symbols
parser_test.go2 symbols
benchmark_test.go2 symbols
serialize_test.go1 symbols
fuzz/fuzz.go1 symbols

Dependencies from manifests, versioned

golang.org/x/netv0.55.0 · 1×

For agents

$ claude mcp add cascadia \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page