124

GitHub - ericchiang/css: CSS Selectors for Go

 6 years ago
source link: https://github.com/ericchiang/css
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CSS selectors in Go

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>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK