selector

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

README

selector

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

It is based on andybalholm/cascadia commit 8919e381b2b9868b86bb29a833d893796a188f1f, with some API simplification.

Documentation

Overview

Package selector is an implementation of CSS selectors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

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

Filter returns the nodes that match m.

func MatchAll

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

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

func MatchFirst

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

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

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 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 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 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 MustCompile

func MustCompile(sel string) SelectorGroup

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

func ParseGroup

func ParseGroup(sel string) (SelectorGroup, error)

ParseGroup 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

Jump to

Keyboard shortcuts

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