css

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 7 Imported by: 54 modules, 52 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

CSS selectors in Go

Go Reference

This package implements a CSS selector compiler for Go's HTML parsing package golang.org/x/net/html.

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/ericchiang/css"
	"golang.org/x/net/html"
)

var data = `
<p>
  <h2 id="foo">a header</h2>
  <h2 id="bar">another header</h2>
</p>`

func main() {
	sel, err := css.Parse("h2#foo")
	if err != nil {
		panic(err)
	}
	node, err := html.Parse(strings.NewReader(data))
	if err != nil {
		panic(err)
	}
	for _, ele := range sel.Select(node) {
		html.Render(os.Stdout, ele)
	}
	fmt.Println()
}
$ go run example/css.go
<h2 id="foo">a header</h2>

Documentation

Overview

Package css implements CSS selector HTML search.

Selectors compiled by this package search through golang.org/x/net/html nodes and should be used in conjunction with that package.

data := `<p>
	<h2 id="foo">a header</h2>
	<h2 id="bar">another header</h2>
</p>`

sel, err := css.Parse("h2#foo")
if err != nil {
	// handle error
}
node, err := html.Parse(strings.NewReader(data))
if err != nil {
	// handle error
}
elements := sel.Select(node)

This package aims to support Selectors Level 4 https://www.w3.org/TR/selectors-4/.

The universal selector (*) is supported, along with:

a                       // Type selector
ns|a                    // Type selector with namespace
.red                    // Class selector
#demo                   // ID selector
[attr]                  // Attribute selector
[attr=value]            // Attribute selector value
[attr~=value]           // Attribute selector element of list
[attr|=value]           // Attribute selector value or "{value}-" prefix
[attr^=value]           // Attribute selector prefix
[attr$=value]           // Attribute selector suffix
[attr*=value]           // Attribute selector contains value
[attr=value i]          // Attribute selector case insensitive modifier
foo, bar                // Selector list
foo bar                 // Descendant combinator
foo > bar               // Child combinator
foo ~ bar               // General sibling combinator
foo + bar               // Adjacent sibling combinator
:empty                  // Element with no children
:first-child            // First child of parent
:first-of-type          // First child of its type of parent
:last-child             // Last child of parent
:last-of-type           // Last child of its type of parent
:only-child             // Only child of parent
:only-of-type           // Only child of its type parent
:root                   // Root element
:nth-child(An+B)        // Positional child matcher
:nth-last-child(An+B)   // Reverse positional child matcher
:nth-last-of-type(An+B) // Reverse positional child matcher of type
:nth-of-type(An+B)      // Positional child matcher of type

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParseError

type ParseError struct {
	Pos int
	Msg string
}

ParseError is returned indicating an lex, parse, or compilation error with the associated position in the string the error occurred.

func (*ParseError) Error

func (p *ParseError) Error() string

Error returns a formatted version of the error.

type Selector

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

Selector is a compiled CSS selector.

func MustParse

func MustParse(s string) *Selector

MustParse is like Parse but panics on errors.

func Parse

func Parse(s string) (*Selector, error)

Parse compiles a complex selector list from a string. The parser supports Selectors Level 4.

Multiple selectors are supported through comma separated values. For example "h1, h2".

Parse reports the first error hit when compiling.

func (*Selector) Select

func (s *Selector) Select(n *html.Node) []*html.Node

Select returns any matches from a parsed HTML document.

Jump to

Keyboard shortcuts

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