cssinliner

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 14 Imported by: 0

README

Go CSS Inliner

Installation

go get go.baoshuo.dev/cssinliner

Usage

package main

import (
  "go.baoshuo.dev/cssinliner"
)

func main() {
  html := `<html><head><style>body { color: red; }</style></head><body>Hello World</body></html>`

  result, err := cssinliner.Inline(html)
  if err != nil {
    panic(err)
  }

  println(result)
}

This package provides these functions:

  • Inline(html string, options... InlinerOption) (string, error)
    Inlines CSS styles into the provided HTML string.
  • InlineFile(path string, options... InlinerOption) (string, error)
    Reads an HTML file from the specified path and inlines CSS styles into it.

The available options include:

  • WithAllowLoadRemoteStylesheets(allow bool)
    Allows the inliner to fetch remote stylesheets.
  • WithAllowReadLocalFiles(allow bool, path string)
    Allows the inliner to fetch local stylesheets from the specified path.

Credits

Author

go-css-inliner © Baoshuo, Released under the MIT License.

Personal Homepage · Blog · GitHub @renbaoshuo

Documentation

Overview

Package `cssinliner` provides functionality to inline CSS styles into HTML documents.

It allows for inlining styles from remote stylesheets, local files, and inline styles within the HTML document itself.

It supports options to control whether remote stylesheets and local files should be loaded, and it processes the HTML to apply styles directly to elements.

Here's a brief overview of the main functions: - Inline(html string, options... InlinerOption) (string, error): Inlines CSS styles into the provided HTML string. - InlineFile(path string, options... InlinerOption) (string, error): Reads an HTML file from the specified path and inlines CSS styles into it.

The available options include: - WithAllowLoadRemoteStylesheets(allow bool): Allows the inliner to fetch remote stylesheets. - WithAllowReadLocalFiles(allow bool, path string): Allows the inliner to fetch local stylesheets from the specified path.

The source code of this package is hosted on GitHub: https://github.com/renbaoshuo/go-css-inliner

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeSpecificity

func ComputeSpecificity(selector string) int

ComputeSpecificity computes style rule specificity

cf. http://www.w3.org/TR/selectors/#specificity

func Inlinable

func Inlinable(selector string) bool

func Inline

func Inline(html string, options ...InlinerOption) (string, error)

Inline processes the HTML content and inlines the CSS styles.

func InlineFile

func InlineFile(path string, options ...InlinerOption) (string, error)

Types

type AttrToStyleRule

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

type CssFilePreprocessor added in v0.0.5

type CssFilePreprocessor func(css, path string) (string, error)

type Element

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

func NewElement

func NewElement(element *goquery.Selection, parserOptions ...cssparser.ParserOption) *Element

type HtmlPreprocessor added in v0.0.5

type HtmlPreprocessor func(html, path string) (string, error)

type Inliner

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

func NewInliner

func NewInliner(html string, options ...InlinerOption) *Inliner

func (*Inliner) Inline

func (inliner *Inliner) Inline() (string, error)

type InlinerOption

type InlinerOption func(*Inliner)

func WithAllowLoadRemoteStylesheets

func WithAllowLoadRemoteStylesheets(allow bool) InlinerOption

WithAllowLoadRemoteStylesheets allows the inliner to fetch remote stylesheets.

func WithAllowReadLocalFiles

func WithAllowReadLocalFiles(allow bool, path string) InlinerOption

WithAllowReadLocalFiles allows the inliner to fetch local stylesheets.

func WithCssFilePreprocessor added in v0.0.5

func WithCssFilePreprocessor(preprocessor CssFilePreprocessor) InlinerOption

WithCssPreprocessor allows setting a custom CSS preprocessor function. This function can be used to modify the CSS before inlining.

NOTE: This function only applies to CSS files, not <style> tags. If you want to preprocess CSS in <style> tags, use the `WithHtmlPreprocessor` option to modify the HTML to include the preprocessed CSS directly in the <style> tags.

func WithHtmlPreprocessor added in v0.0.5

func WithHtmlPreprocessor(preprocessor HtmlPreprocessor) InlinerOption

WithHtmlPreprocessor allows setting a custom HTML preprocessor function. This function can be used to modify the HTML before processing.

func WithParserOptions added in v0.0.4

func WithParserOptions(parserOptions ...cssparser.ParserOption) InlinerOption

WithParserOptions allows setting custom CSS parser options. This can be used to customize the behavior of the CSS parser.

type StyleDeclaration

type StyleDeclaration struct {
	StyleRule   *StyleRule
	Declaration *cssparser.Declaration
}

func NewStyleDeclaration

func NewStyleDeclaration(styleRule *StyleRule, declaration *cssparser.Declaration) *StyleDeclaration

func (*StyleDeclaration) Specificity

func (styleDecl *StyleDeclaration) Specificity() int

type StyleRule

type StyleRule struct {
	Selector     string                   // The style rule selector
	Declarations []*cssparser.Declaration // The style rule properties
	Specificity  int                      // Selector specificity
}

StyleRule represents a Qualifier Rule for a uniq selector

func NewStyleRule

func NewStyleRule(selector string, declarations []*cssparser.Declaration) *StyleRule

func (*StyleRule) String

func (styleRule *StyleRule) String() string

Jump to

Keyboard shortcuts

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