cascadia
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
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 outcascadia the command line tool, a thin wrapper around this package.
Example
The following is an example of how you can use Cascadia.
package mainimport ("fmt""log""strings""github.com/andybalholm/cascadia""golang.org/x/net/html")var pricingHtml string = `<div class="card mb-4 box-shadow"><div class="card-header"><h4 class="my-0 font-weight-normal">Free</h4></div><div class="card-body"><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></div></div><div class="card mb-4 box-shadow"><div class="card-header"><h4 class="my-0 font-weight-normal">Pro</h4></div><div class="card-body"><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></div></div><div class="card mb-4 box-shadow"><div class="card-header"><h4 class="my-0 font-weight-normal">Enterprise</h4></div><div class="card-body"><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></div></div>`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.Dataprice := Query(p, ".pricing-card-title").FirstChild.DatausersIncluded := Query(p, "li:first-child").FirstChild.Datastorage := Query(p, "li:nth-child(2)").FirstChild.DatadetailsUrl := 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 #1Name: FreePrice: $0/moUsers: 10 users includedStorage: 2 GB of storageDetails: https://example.comPlan #2Name: ProPrice: $15/moUsers: 20 users includedStorage: 10 GB of storageDetails: https://example.comPlan #3Name: EnterprisePrice: $29/moUsers: 30 users includedStorage: 15 GB of storageDetails: (No link available)
Documentation¶
Overview¶
Package cascadia is an implementation of CSS selectors.
Index¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
Types¶
typeMatcher¶added inv1.1.0
Matcher is the interface for basic selector functionality.Match returns whether a selector matches n.
typeSel¶added inv1.1.0
type Sel interface {MatcherSpecificity()Specificity// Returns a CSS input compiling to this selector.String()string// Returns a pseudo-element, or an empty string.PseudoElement()string}Sel is the interface for all the functionality provided by selectors.
funcParse¶added inv1.1.0
Parse parses a selector. Use `ParseWithPseudoElement`if you need support for pseudo-elements.
funcParseWithPseudoElement¶added inv1.2.0
ParseWithPseudoElement parses a single selector,with support for pseudo-element.
typeSelector¶
A Selector is a function which tells whether a node matches or not.
This type is maintained for compatibility; I recommend using the newer andmore idiomatic interfaces Sel and Matcher.
funcCompile¶
Compile parses a selector and returns, if successful, a Selector objectthat can be used to match against html.Node objects.
funcMustCompile¶
MustCompile is like Compile, but panics instead of returning an error.
typeSelectorGroup¶added inv1.1.0
type SelectorGroup []Sel
A SelectorGroup is a list of selectors, which matches if any of theindividual selectors matches.
funcParseGroup¶added inv1.1.0
func ParseGroup(selstring) (SelectorGroup,error)
ParseGroup parses a selector, or a group of selectors separated by commas.Use `ParseGroupWithPseudoElements`if you need support for pseudo-elements.
funcParseGroupWithPseudoElements¶added inv1.2.0
func ParseGroupWithPseudoElements(selstring) (SelectorGroup,error)
ParseGroupWithPseudoElements parses a selector, or a group of selectors separated by commas.It supports pseudo-elements.
func (SelectorGroup)Match¶added inv1.1.0
func (sSelectorGroup) Match(n *html.Node)bool
Match returns true if the node matches one of the single selectors.
func (SelectorGroup)String¶added inv1.2.0
func (cSelectorGroup) String()string
typeSpecificity¶added inv1.1.0
type Specificity [3]int
Specificity is the CSS specificity as defined inhttps://www.w3.org/TR/selectors/#specificity-ruleswith the convention Specificity = [A,B,C].
func (Specificity)Add¶added inv1.1.0
func (sSpecificity) Add(otherSpecificity)Specificity
func (Specificity)Less¶added inv1.1.0
func (sSpecificity) Less(otherSpecificity)bool
returns `true` if s < other (strictly), false otherwise