A Go package that parses CSS stylesheets and applies them to HTML documents, producing a DOM tree with computed style attributes on each node.
- Parse CSS from files or strings
- Apply CSS rules to HTML documents using selector matching
- Support for
@import,@font-face, and@pageat-rules - Handles linked stylesheets (
<link href="...">) in HTML documents - Returns a goquery Document with style attributes applied
package main
import "github.com/speedata/csshtml"
func main() {
// Create a new CSS parser
css := csshtml.NewCSSParser()
// Add CSS rules
css.AddCSSText(`
body { font-family: serif; }
h1 { color: blue; font-size: 24pt; }
`)
// Process HTML and apply CSS
doc, err := css.ProcessHTMLChunk(`
<html>
<body>
<h1>Hello World</h1>
</body>
</html>
`)
if err != nil {
panic(err)
}
// The returned document has style attributes on matching elements
// doc.Find("h1") will have style="color: blue; font-size: 24pt;"
}NewCSSParser()- Create a new CSS parserNewCSSParserWithDefaults()- Create a parser with default browser stylesAddCSSText(css string)- Parse and add CSS rulesProcessHTMLFile(filename string)- Load HTML file, read linked stylesheets, apply CSSProcessHTMLChunk(html string)- Parse HTML string and apply CSSApplyCSS(doc *goquery.Document)- Apply collected CSS rules to a document