Skip to content
CSS Selectors for Go
Go Other
  1. Go 98.6%
  2. Other 1.4%
Branch: master
Clone or download
Latest commit f08e94f Dec 10, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example first commit Dec 14, 2015
scripts setup fuzzing Dec 10, 2017
testdata setup fuzzing Dec 10, 2017
.gitignore setup fuzzing Dec 10, 2017
.travis.yml enable travis on PRs Dec 10, 2017
LICENSE *: add a LICENSE file Sep 22, 2017
Makefile setup fuzzing Dec 10, 2017
README.md expand documentation Jan 17, 2016
compile.go fix parsing of '-2n-2' case for :nth-child Jan 17, 2016
compile_test.go enable travis on PRs Dec 10, 2017
css.go expand documentation Jan 17, 2016
fuzz.go setup fuzzing Dec 10, 2017
grammar.txt first commit Dec 14, 2015
lex.go first commit Dec 14, 2015
lex_test.go fix parsing of '-2n-2' case for :nth-child Jan 17, 2016
selector.go added :nth-child pseudo Jan 17, 2016
selector_test.go added :nth-child pseudo Jan 17, 2016

README.md

A CSS selector compiler

GoDoc

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.Compile("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>

Details

This package implements the W3 Selectors Level 3 specification. The exact grammar can be found in the grammar.txt file.

You can’t perform that action at this time.