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 ¶
- func ComputeSpecificity(selector string) int
- func Inlinable(selector string) bool
- func Inline(html string, options ...InlinerOption) (string, error)
- func InlineFile(path string, options ...InlinerOption) (string, error)
- type AttrToStyleRule
- type CssFilePreprocessor
- type Element
- type HtmlPreprocessor
- type Inliner
- type InlinerOption
- func WithAllowLoadRemoteStylesheets(allow bool) InlinerOption
- func WithAllowReadLocalFiles(allow bool, path string) InlinerOption
- func WithCssFilePreprocessor(preprocessor CssFilePreprocessor) InlinerOption
- func WithHtmlPreprocessor(preprocessor HtmlPreprocessor) InlinerOption
- func WithParserOptions(parserOptions ...cssparser.ParserOption) InlinerOption
- type StyleDeclaration
- type StyleRule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComputeSpecificity ¶
ComputeSpecificity computes style rule specificity
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 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 Inliner ¶
type Inliner struct {
// contains filtered or unexported fields
}
func NewInliner ¶
func NewInliner(html string, options ...InlinerOption) *Inliner
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