Movatterモバイル変換


[0]ホーム

URL:


cascadia

packagemodule
v1.3.3Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2024 License:BSD-2-ClauseImports:8Imported by:737

Details

Repository

github.com/andybalholm/cascadia

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.

Refer to godoc here.

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

funcFilteradded inv1.1.0

func Filter(nodes []*html.Node, mMatcher) (result []*html.Node)

Filter returns the nodes that match m.

funcQueryadded inv1.1.0

func Query(n *html.Node, mMatcher) *html.Node

Query returns the first node that matches m, from the descendants of n.If none matches, it returns nil.

funcQueryAlladded inv1.1.0

func QueryAll(n *html.Node, mMatcher) []*html.Node

QueryAll returns a slice of all the nodes that match m, from the descendantsof n.

Types

typeMatcheradded inv1.1.0

type Matcher interface {Match(n *html.Node)bool}

Matcher is the interface for basic selector functionality.Match returns whether a selector matches n.

typeSeladded 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.

funcParseadded inv1.1.0

func Parse(selstring) (Sel,error)

Parse parses a selector. Use `ParseWithPseudoElement`if you need support for pseudo-elements.

funcParseWithPseudoElementadded inv1.2.0

func ParseWithPseudoElement(selstring) (Sel,error)

ParseWithPseudoElement parses a single selector,with support for pseudo-element.

typeSelector

type Selector func(*html.Node)bool

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

func Compile(selstring) (Selector,error)

Compile parses a selector and returns, if successful, a Selector objectthat can be used to match against html.Node objects.

funcMustCompile

func MustCompile(selstring)Selector

MustCompile is like Compile, but panics instead of returning an error.

func (Selector)Filter

func (sSelector) Filter(nodes []*html.Node) (result []*html.Node)

Filter returns the nodes in nodes that match the selector.

func (Selector)Match

func (sSelector) Match(n *html.Node)bool

Match returns true if the node matches the selector.

func (Selector)MatchAll

func (sSelector) MatchAll(n *html.Node) []*html.Node

MatchAll returns a slice of the nodes that match the selector,from n and its children.

func (Selector)MatchFirst

func (sSelector) MatchFirst(n *html.Node) *html.Node

MatchFirst returns the first node that matches s, from n and its children.

typeSelectorGroupadded inv1.1.0

type SelectorGroup []Sel

A SelectorGroup is a list of selectors, which matches if any of theindividual selectors matches.

funcParseGroupadded 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.

funcParseGroupWithPseudoElementsadded 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)Matchadded inv1.1.0

func (sSelectorGroup) Match(n *html.Node)bool

Match returns true if the node matches one of the single selectors.

func (SelectorGroup)Stringadded inv1.2.0

func (cSelectorGroup) String()string

typeSpecificityadded 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)Addadded inv1.1.0

func (Specificity)Lessadded inv1.1.0

func (sSpecificity) Less(otherSpecificity)bool

returns `true` if s < other (strictly), false otherwise

Source Files

View all Source files

Directories

PathSynopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp