cascadia

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: MIT Imports: 13 Imported by: 0

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 = `
<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.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)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CssUnescape

func CssUnescape(b []byte) string

func Filter

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

Filter returns the nodes that match m.

func IsPseudoElement

func IsPseudoElement(sel Sel) bool

func Query

func Query(n *html.Node, m Matcher) *html.Node

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

func QueryAll

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

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

Types

type ActivePseudoClassSelector

type ActivePseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (ActivePseudoClassSelector) IsClass

func (c ActivePseudoClassSelector) IsClass(n *html.Node) bool

func (ActivePseudoClassSelector) Match

func (ActivePseudoClassSelector) PseudoElement

func (c ActivePseudoClassSelector) PseudoElement() string

func (ActivePseudoClassSelector) Specificity

func (s ActivePseudoClassSelector) Specificity() Specificity

func (ActivePseudoClassSelector) String

func (c ActivePseudoClassSelector) String() string

type AttrSelector

type AttrSelector struct {
	// contains filtered or unexported fields
}

func (AttrSelector) Match

func (t AttrSelector) Match(n *html.Node) bool

Matches elements by attribute value.

func (AttrSelector) PseudoElement

func (c AttrSelector) PseudoElement() string

func (AttrSelector) Specificity

func (c AttrSelector) Specificity() Specificity

func (AttrSelector) String

func (c AttrSelector) String() string

type CheckedPseudoClassSelector

type CheckedPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (CheckedPseudoClassSelector) IsClass

func (c CheckedPseudoClassSelector) IsClass(n *html.Node) bool

func (CheckedPseudoClassSelector) Match

func (CheckedPseudoClassSelector) PseudoElement

func (c CheckedPseudoClassSelector) PseudoElement() string

func (CheckedPseudoClassSelector) Specificity

func (s CheckedPseudoClassSelector) Specificity() Specificity

func (CheckedPseudoClassSelector) String

type ClassSelector

type ClassSelector struct {
	Class string
}

func (ClassSelector) Match

func (t ClassSelector) Match(n *html.Node) bool

Matches elements by class attribute.

func (ClassSelector) PseudoElement

func (c ClassSelector) PseudoElement() string

func (ClassSelector) Specificity

func (c ClassSelector) Specificity() Specificity

func (ClassSelector) String

func (c ClassSelector) String() string

type CombinedSelector

type CombinedSelector struct {
	// contains filtered or unexported fields
}

CombinedSelector is a selector that combines two selectors with a combinator. If the selector is more than two parts, the first contains all but the last part, and the second contains the last part.

func (CombinedSelector) First

func (c CombinedSelector) First() Sel

func (CombinedSelector) Match

func (t CombinedSelector) Match(n *html.Node) bool

func (CombinedSelector) PseudoElement

func (c CombinedSelector) PseudoElement() string

on combinedSelector, a pseudo-element only makes sense on the last selector, although others increase specificity.

func (CombinedSelector) Second

func (c CombinedSelector) Second() Sel

func (CombinedSelector) Specificity

func (s CombinedSelector) Specificity() Specificity

func (CombinedSelector) String

func (c CombinedSelector) String() string

type CompoundSelector

type CompoundSelector struct {
	// contains filtered or unexported fields
}

func (CompoundSelector) Match

func (t CompoundSelector) Match(n *html.Node) bool

Matches elements if each sub-selectors matches.

func (CompoundSelector) PseudoElement

func (c CompoundSelector) PseudoElement() string

func (CompoundSelector) PseudoElements

func (c CompoundSelector) PseudoElements() []Sel

func (CompoundSelector) PseudoElementsString

func (c CompoundSelector) PseudoElementsString() string

func (CompoundSelector) Selectors

func (c CompoundSelector) Selectors() []Sel

func (CompoundSelector) Specificity

func (s CompoundSelector) Specificity() Specificity

func (CompoundSelector) String

func (c CompoundSelector) String() string

type ContainsPseudoClassSelector

type ContainsPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (ContainsPseudoClassSelector) IsClass

func (c ContainsPseudoClassSelector) IsClass(n *html.Node) bool

func (ContainsPseudoClassSelector) Match

func (ContainsPseudoClassSelector) PseudoElement

func (c ContainsPseudoClassSelector) PseudoElement() string

func (ContainsPseudoClassSelector) Specificity

func (s ContainsPseudoClassSelector) Specificity() Specificity

func (ContainsPseudoClassSelector) String

type CssDeclaration

type CssDeclaration struct {
	Property string // Property is the property for the declaration (e.g., "color")
	Value    string // Value is the value for the declaration (e.g., "red")
}

CssDeclaration represents a CSS declaration, which includes a property and a value.

type CssRule

type CssRule struct {
	Selector     Sel              // Selector is the selector for the rule
	Declarations []CssDeclaration // Declarations is a list of declarations for the rule (e.g., property-value pairs)
	// contains filtered or unexported fields
}

CssRule represents a CSS rule, which includes a selector and a list of declarations. It also includes a condition, which is used to represent the condition of an at-rule (e.g., @media). The condition can also be a pseudo-element or pseudo-class for a compound selector (e.g., :hover, ::before).

func ExtractRules

func ExtractRules(r io.Reader, inline bool) ([]CssRule, error)

func (CssRule) GetCondition

func (r CssRule) GetCondition() string

func (CssRule) GetSelector

func (r CssRule) GetSelector() string

func (CssRule) String

func (r CssRule) String() string

func (CssRule) ToCssFormat

func (r CssRule) ToCssFormat() string

type DisabledPseudoClassSelector

type DisabledPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (DisabledPseudoClassSelector) IsClass

func (c DisabledPseudoClassSelector) IsClass(n *html.Node) bool

func (DisabledPseudoClassSelector) Match

func (DisabledPseudoClassSelector) PseudoElement

func (c DisabledPseudoClassSelector) PseudoElement() string

func (DisabledPseudoClassSelector) Specificity

func (s DisabledPseudoClassSelector) Specificity() Specificity

func (DisabledPseudoClassSelector) String

type EmptyElementPseudoClassSelector

type EmptyElementPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (EmptyElementPseudoClassSelector) IsClass

func (c EmptyElementPseudoClassSelector) IsClass(n *html.Node) bool

func (EmptyElementPseudoClassSelector) Match

Matches empty elements.

func (EmptyElementPseudoClassSelector) PseudoElement

func (c EmptyElementPseudoClassSelector) PseudoElement() string

func (EmptyElementPseudoClassSelector) Specificity

func (s EmptyElementPseudoClassSelector) Specificity() Specificity

func (EmptyElementPseudoClassSelector) String

type EnabledPseudoClassSelector

type EnabledPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (EnabledPseudoClassSelector) IsClass

func (c EnabledPseudoClassSelector) IsClass(n *html.Node) bool

func (EnabledPseudoClassSelector) Match

func (EnabledPseudoClassSelector) PseudoElement

func (c EnabledPseudoClassSelector) PseudoElement() string

func (EnabledPseudoClassSelector) Specificity

func (s EnabledPseudoClassSelector) Specificity() Specificity

func (EnabledPseudoClassSelector) String

type FocusPseudoClassSelector

type FocusPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (FocusPseudoClassSelector) IsClass

func (c FocusPseudoClassSelector) IsClass(n *html.Node) bool

func (FocusPseudoClassSelector) Match

func (s FocusPseudoClassSelector) Match(n *html.Node) bool

func (FocusPseudoClassSelector) PseudoElement

func (c FocusPseudoClassSelector) PseudoElement() string

func (FocusPseudoClassSelector) Specificity

func (s FocusPseudoClassSelector) Specificity() Specificity

func (FocusPseudoClassSelector) String

func (c FocusPseudoClassSelector) String() string

type HoverPseudoClassSelector

type HoverPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (HoverPseudoClassSelector) IsClass

func (c HoverPseudoClassSelector) IsClass(n *html.Node) bool

func (HoverPseudoClassSelector) Match

func (s HoverPseudoClassSelector) Match(n *html.Node) bool

func (HoverPseudoClassSelector) PseudoElement

func (c HoverPseudoClassSelector) PseudoElement() string

func (HoverPseudoClassSelector) Specificity

func (s HoverPseudoClassSelector) Specificity() Specificity

func (HoverPseudoClassSelector) String

func (c HoverPseudoClassSelector) String() string

type IdSelector

type IdSelector struct {
	// contains filtered or unexported fields
}

func (IdSelector) Match

func (t IdSelector) Match(n *html.Node) bool

Matches elements by id attribute.

func (IdSelector) PseudoElement

func (c IdSelector) PseudoElement() string

func (IdSelector) Specificity

func (c IdSelector) Specificity() Specificity

func (IdSelector) String

func (c IdSelector) String() string

type InputPseudoClassSelector

type InputPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (InputPseudoClassSelector) IsClass

func (c InputPseudoClassSelector) IsClass(n *html.Node) bool

func (InputPseudoClassSelector) Match

func (s InputPseudoClassSelector) Match(n *html.Node) bool

Matches input, select, textarea and button elements.

func (InputPseudoClassSelector) PseudoElement

func (c InputPseudoClassSelector) PseudoElement() string

func (InputPseudoClassSelector) Specificity

func (s InputPseudoClassSelector) Specificity() Specificity

func (InputPseudoClassSelector) String

func (c InputPseudoClassSelector) String() string

type IsPseudoClassSelector

type IsPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (IsPseudoClassSelector) Match

func (s IsPseudoClassSelector) Match(n *html.Node) bool

func (IsPseudoClassSelector) PseudoElement

func (c IsPseudoClassSelector) PseudoElement() string

func (IsPseudoClassSelector) Selectors

func (c IsPseudoClassSelector) Selectors() []Sel

func (IsPseudoClassSelector) Specificity

func (s IsPseudoClassSelector) Specificity() Specificity

func (IsPseudoClassSelector) String

func (c IsPseudoClassSelector) String() string

type LangPseudoClassSelector

type LangPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (LangPseudoClassSelector) IsClass

func (c LangPseudoClassSelector) IsClass(n *html.Node) bool

func (LangPseudoClassSelector) Match

func (s LangPseudoClassSelector) Match(n *html.Node) bool

func (LangPseudoClassSelector) PseudoElement

func (c LangPseudoClassSelector) PseudoElement() string

func (LangPseudoClassSelector) Specificity

func (s LangPseudoClassSelector) Specificity() Specificity

func (LangPseudoClassSelector) String

func (c LangPseudoClassSelector) String() string

type LinkPseudoClassSelector

type LinkPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (LinkPseudoClassSelector) IsClass

func (c LinkPseudoClassSelector) IsClass(n *html.Node) bool

func (LinkPseudoClassSelector) Match

func (s LinkPseudoClassSelector) Match(n *html.Node) bool

Match implements :link

func (LinkPseudoClassSelector) PseudoElement

func (c LinkPseudoClassSelector) PseudoElement() string

func (LinkPseudoClassSelector) Specificity

func (s LinkPseudoClassSelector) Specificity() Specificity

func (LinkPseudoClassSelector) String

func (c LinkPseudoClassSelector) String() string

type Matcher

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

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

type NthPseudoClassSelector

type NthPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (NthPseudoClassSelector) IsClass

func (c NthPseudoClassSelector) IsClass(n *html.Node) bool

func (NthPseudoClassSelector) Match

func (s NthPseudoClassSelector) Match(n *html.Node) bool

func (NthPseudoClassSelector) PseudoElement

func (c NthPseudoClassSelector) PseudoElement() string

func (NthPseudoClassSelector) Specificity

func (s NthPseudoClassSelector) Specificity() Specificity

func (NthPseudoClassSelector) String

func (c NthPseudoClassSelector) String() string

type OnlyChildPseudoClassSelector

type OnlyChildPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (OnlyChildPseudoClassSelector) IsClass

func (c OnlyChildPseudoClassSelector) IsClass(n *html.Node) bool

func (OnlyChildPseudoClassSelector) Match

Match implements :only-child. If `ofType` is true, it implements :only-of-type instead.

func (OnlyChildPseudoClassSelector) PseudoElement

func (c OnlyChildPseudoClassSelector) PseudoElement() string

func (OnlyChildPseudoClassSelector) Specificity

func (s OnlyChildPseudoClassSelector) Specificity() Specificity

func (OnlyChildPseudoClassSelector) String

type PopoverPseudoClassSelector

type PopoverPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (PopoverPseudoClassSelector) IsClass

func (c PopoverPseudoClassSelector) IsClass(n *html.Node) bool

func (PopoverPseudoClassSelector) Match

func (PopoverPseudoClassSelector) PseudoElement

func (c PopoverPseudoClassSelector) PseudoElement() string

func (PopoverPseudoClassSelector) Specificity

func (s PopoverPseudoClassSelector) Specificity() Specificity

func (PopoverPseudoClassSelector) String

type ReadOnlyPseudoClassSelector

type ReadOnlyPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (ReadOnlyPseudoClassSelector) IsClass

func (c ReadOnlyPseudoClassSelector) IsClass(n *html.Node) bool

func (ReadOnlyPseudoClassSelector) Match

func (ReadOnlyPseudoClassSelector) PseudoElement

func (c ReadOnlyPseudoClassSelector) PseudoElement() string

func (ReadOnlyPseudoClassSelector) Specificity

func (s ReadOnlyPseudoClassSelector) Specificity() Specificity

func (ReadOnlyPseudoClassSelector) String

type RegexpPseudoClassSelector

type RegexpPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (RegexpPseudoClassSelector) IsClass

func (c RegexpPseudoClassSelector) IsClass(n *html.Node) bool

func (RegexpPseudoClassSelector) Match

func (RegexpPseudoClassSelector) PseudoElement

func (c RegexpPseudoClassSelector) PseudoElement() string

func (RegexpPseudoClassSelector) Specificity

func (s RegexpPseudoClassSelector) Specificity() Specificity

func (RegexpPseudoClassSelector) String

func (c RegexpPseudoClassSelector) String() string

type RelativePseudoClassSelector

type RelativePseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (RelativePseudoClassSelector) Match

func (RelativePseudoClassSelector) PseudoElement

func (c RelativePseudoClassSelector) PseudoElement() string

func (RelativePseudoClassSelector) Specificity

func (s RelativePseudoClassSelector) Specificity() Specificity

Specificity returns the specificity of the most specific selectors in the pseudo-class arguments. See https://www.w3.org/TR/selectors/#specificity-rules

func (RelativePseudoClassSelector) String

type RootPseudoClassSelector

type RootPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (RootPseudoClassSelector) IsClass

func (c RootPseudoClassSelector) IsClass(n *html.Node) bool

func (RootPseudoClassSelector) Match

func (s RootPseudoClassSelector) Match(n *html.Node) bool

Match implements :root

func (RootPseudoClassSelector) PseudoElement

func (c RootPseudoClassSelector) PseudoElement() string

func (RootPseudoClassSelector) Specificity

func (s RootPseudoClassSelector) Specificity() Specificity

func (RootPseudoClassSelector) String

func (c RootPseudoClassSelector) String() string

type Sel

type Sel interface {
	Matcher
	Specificity() 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.

func Parse

func Parse(sel string) (Sel, error)

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

func ParseWithPseudoElement

func ParseWithPseudoElement(sel string) (Sel, error)

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

type SelectorGroup

type SelectorGroup []Sel

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

func ParseGroup

func ParseGroup(sel string) (SelectorGroup, error)

ParseGroup parses a selector, or a group of selectors separated by commas. Use `ParseGroupWithPseudoElements` if you need support for pseudo-elements.

func ParseGroupWithPseudoElements

func ParseGroupWithPseudoElements(sel string) (SelectorGroup, error)

ParseGroupWithPseudoElements parses a selector, or a group of selectors separated by commas. It supports pseudo-elements.

func (SelectorGroup) Match

func (s SelectorGroup) Match(n *html.Node) bool

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

func (SelectorGroup) String

func (c SelectorGroup) String() string

type Specificity

type Specificity [3]int

Specificity is the CSS specificity as defined in https://www.w3.org/TR/selectors/#specificity-rules with the convention Specificity = [A,B,C].

func (Specificity) Add

func (s Specificity) Add(other Specificity) Specificity

func (Specificity) Less

func (s Specificity) Less(other Specificity) bool

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

type TagSelector

type TagSelector struct {
	// contains filtered or unexported fields
}

func (TagSelector) Match

func (t TagSelector) Match(n *html.Node) bool

Matches elements with a given tag name.

func (TagSelector) PseudoElement

func (c TagSelector) PseudoElement() string

func (TagSelector) Specificity

func (c TagSelector) Specificity() Specificity

func (TagSelector) String

func (c TagSelector) String() string

type TargetPseudoClassSelector

type TargetPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (TargetPseudoClassSelector) IsClass

func (c TargetPseudoClassSelector) IsClass(n *html.Node) bool

func (TargetPseudoClassSelector) Match

func (TargetPseudoClassSelector) PseudoElement

func (c TargetPseudoClassSelector) PseudoElement() string

func (TargetPseudoClassSelector) Specificity

func (s TargetPseudoClassSelector) Specificity() Specificity

func (TargetPseudoClassSelector) String

func (c TargetPseudoClassSelector) String() string

type VisitedPseudoClassSelector

type VisitedPseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (VisitedPseudoClassSelector) IsClass

func (c VisitedPseudoClassSelector) IsClass(n *html.Node) bool

func (VisitedPseudoClassSelector) Match

func (VisitedPseudoClassSelector) PseudoElement

func (c VisitedPseudoClassSelector) PseudoElement() string

func (VisitedPseudoClassSelector) Specificity

func (s VisitedPseudoClassSelector) Specificity() Specificity

func (VisitedPseudoClassSelector) String

type WherePseudoClassSelector

type WherePseudoClassSelector struct {
	// contains filtered or unexported fields
}

func (WherePseudoClassSelector) Match

func (s WherePseudoClassSelector) Match(n *html.Node) bool

func (WherePseudoClassSelector) PseudoElement

func (c WherePseudoClassSelector) PseudoElement() string

func (WherePseudoClassSelector) Selectors

func (c WherePseudoClassSelector) Selectors() []Sel

func (WherePseudoClassSelector) Specificity

func (s WherePseudoClassSelector) Specificity() Specificity

func (WherePseudoClassSelector) String

func (c WherePseudoClassSelector) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL