csshtml

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: BSD-3-Clause Imports: 14 Imported by: 3

README

csshtml

A Go package that parses CSS stylesheets and applies them to HTML documents, producing a DOM tree with computed style attributes on each node.

Features

  • Parse CSS from files or strings
  • Apply CSS rules to HTML documents using selector matching
  • Support for @import, @font-face, and @page at-rules
  • Handles linked stylesheets (<link href="...">) in HTML documents
  • Returns a goquery Document with style attributes applied

Usage

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;"
}

API

  • NewCSSParser() - Create a new CSS parser
  • NewCSSParserWithDefaults() - Create a parser with default browser styles
  • AddCSSText(css string) - Parse and add CSS rules
  • ProcessHTMLFile(filename string) - Load HTML file, read linked stylesheets, apply CSS
  • ProcessHTMLChunk(html string) - Parse HTML string and apply CSS
  • ApplyCSS(doc *goquery.Document) - Apply collected CSS rules to a document

Documentation

Overview

Package csshtml combines CSS instructions and HTML text to a DOM with style attributes at the selected nodes.

This package can be used to construct a DOM tree from a set of CSS files or strings and applied to HTML files or texts.

Index

Constants

This section is empty.

Variables

View Source
var CSSdefaults = `` /* 2089-byte string literal not displayed */

CSSdefaults contains browser-like styling of some elements.

Functions

func GetAttributes

func GetAttributes(attrs []html.Attribute) map[string]string

GetAttributes returns a map from all HTML attributes.

func PapersizeWidthHeight

func PapersizeWidthHeight(spec string) (string, string)

PapersizeWidthHeight converts the spec to the width and height. The parameter can be a known paper size (such as A4 or letter) or a one or two parameter string such as 20cm 20cm. The return values are in one of the units 'mm' or 'in'. 'mm' if the parameter spec is one of a5, a4, a3, b5, b4, jis-b5 or jis-b4 and 'in' if the parameter spec is one of letter, legal or ledger.

func ResolveAttributes

func ResolveAttributes(attrs []html.Attribute) (resolved map[string]string, newAttributes []html.Attribute)

ResolveAttributes returns the resolved styles and the attributes of the node. The argument attrs is unchanged. This function transforms rules such as "margin: 1cm;" into "margin-left: 1cm; margin-right: 1cm; ...". Attributes without a ! prefix are not resolved, just copied to the newAttributes return value. All other attributes are copied to the newAttributes prefixed with a * and all the resolved attributes prefixed with an exclamation mark. The resolved map contains only the resolved attributes and values without a prefix.

Types

type CSS

type CSS struct {
	Pages      map[string]Page
	FileFinder func(string) (string, error)
	FontFaces  []FontFace
	// contains filtered or unexported fields
}

CSS is the main structure that contains cascading style sheet information. Multiple stylesheets can be added to the CSS structure and then applied to HTML.

func NewCSSParser

func NewCSSParser() *CSS

NewCSSParser returns a new CSS object

func NewCSSParserWithDefaults

func NewCSSParserWithDefaults() *CSS

NewCSSParserWithDefaults returns a new CSS object with the default stylesheet included. This is a convenience function which adds the CSSdefaults to the returned CSS struct.

func (*CSS) AddCSSText

func (c *CSS) AddCSSText(fragment string) error

AddCSSText parses CSS text and appends the rules to the previously read rules. If the fragment contains relative links to other files (fonts or other stylesheets for example), the dir stack must be set in advance.

func (*CSS) ApplyCSS

func (c *CSS) ApplyCSS(doc *goquery.Document) (*goquery.Document, error)

ApplyCSS resolves CSS rules in the DOM. Each CSS rule is added to the selection as an attribute (prefixed with a !). Pseudo elements are prefixed with ::.

func (*CSS) PopDir

func (c *CSS) PopDir()

PopDir removes the last entry from the dir stack.

func (*CSS) ProcessHTMLChunk

func (c *CSS) ProcessHTMLChunk(htmltext string) (*goquery.Document, error)

ProcessHTMLChunk reads the HTML text. If there are linked style sheets (<link href=...) these are also read. After reading, the CSS is applied to the HTML DOM which is returned.

func (*CSS) ProcessHTMLFile

func (c *CSS) ProcessHTMLFile(filename string) (*goquery.Document, error)

ProcessHTMLFile opens an HTML file, reads linked stylesheets, applies the CSS rules and returns the DOM structure.

func (*CSS) PushDir

func (c *CSS) PushDir(dir string)

PushDir adds a directory to the dir stack. When a file is opened, all new Open calls are relative to this directory. ProcessHTMLFile uses the dir stack internally when it reads a cascade of CSS files.

type ContentToken added in v0.0.3

type ContentToken struct {
	Type  ContentTokenType
	Value string // string literal or counter name ("page", "pages")
}

ContentToken represents a single parsed piece of a CSS content property value.

type ContentTokenType added in v0.0.3

type ContentTokenType int

ContentTokenType distinguishes different kinds of CSS content tokens.

const (
	// ContentString is a literal string from a CSS content value.
	ContentString ContentTokenType = iota
	// ContentCounter is a counter() function call.
	ContentCounter
	// ContentLeader is a leader() function call (CSS GCPM).
	ContentLeader
	// ContentURL is a url() reference to an image or other resource.
	ContentURL
)

type FontFace

type FontFace struct {
	Weight            int
	Style             string
	Family            string
	Source            []FontSource
	Features          []string
	VariationSettings map[string]float64 // axis tag -> value (e.g., "wght" -> 700)
	SizeAdjust        float64
}

FontFace contains information from a @font-face rule.

type FontSource

type FontSource struct {
	Local  string
	URI    string
	Format string
	Tech   string
}

FontSource has information from the src attribute.

type Page

type Page struct {
	PageArea        map[string]map[string]string // key value pairs for the page areas
	PageAreaContent map[string][]ContentToken    // parsed content tokens per area
	Attributes      []html.Attribute
	Papersize       string
	MarginLeft      string
	MarginRight     string
	MarginTop       string
	MarginBottom    string
	// contains filtered or unexported fields
}

Page defines a page.

Jump to

Keyboard shortcuts

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