cssparser

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2025 License: MIT Imports: 5 Imported by: 1

README

Go CSS Parser

Installation

go get go.baoshuo.dev/cssparser

API

  • ParseStylesheet(content string, options ...ParserOption) (*Stylesheet, error)
    Parses a complete CSS stylesheet.
  • ParseDeclarations(content string, options ...ParserOption) ([]*Declaration, error)
    Parses CSS declarations, typically used for inline styles.

Here are the available options for the parser:

  • WithInline(bool)
    Whether to parse inline styles.
  • WithLooseParsing(bool)
    Whether to allow loose parsing, which is more permissive and allows for some errors in the CSS syntax.

Usage

package main

import "go.baoshuo.dev/cssparser"

func main() {
  // Parse CSS declarations (e.g., from style attribute)
  declarations, err := cssparser.ParseDeclarations(`
    color: red;
    font-size: 16px;
  `)

  // Parse CSS stylesheet (e.g., from a <style> tag)
  stylesheet, err := cssparser.ParseStylesheet(`
    .example {
      color: blue;
      font-size: 14px;
    }
  `)
}

Credits

Author

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

Personal Homepage · Blog · GitHub @renbaoshuo

Documentation

Overview

Package `cssparser` provides a parser for CSS (Cascading Style Sheets) files.

It allows parsing of stylesheets and declarations, handling both inline and embedded styles.

For example, you can use it to parse a CSS stylesheet and retrieve its rules or declarations.

Here's a brief overview of the main functions: - ParseStylesheet(content string) (*Stylesheet, error): Parses a complete CSS stylesheet. - ParseDeclarations(content string) ([]*Declaration, error): Parses CSS declarations, typically used for inline styles.

Here are the available options for the parser: - WithInline(bool): Whether to parse inline styles. - WithLooseParsing(bool): Whether to allow loose parsing, which is more permissive and allows for some errors in the CSS syntax.

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CssRule

type CssRule struct {
	Kind         CssRuleKind
	Name         string         // At Rule name (eg: "@media")
	Prelude      string         // Raw prelude: https://github.com/csstree/csstree/discussions/168
	Selectors    []string       // Qualified Rule selectors parsed from prelude
	Declarations []*Declaration // Style properties
	Rules        []*CssRule     // At Rule embedded rules
	EmbedLevel   int            // Current rule embedding level
}

func NewCssRule

func NewCssRule(kind CssRuleKind) *CssRule

func (*CssRule) Equal

func (r *CssRule) Equal(o *CssRule) bool

func (*CssRule) String

func (r *CssRule) String() string

type CssRuleKind

type CssRuleKind int
const (
	QualifiedRule CssRuleKind = iota
	AtRule
)

func (CssRuleKind) String

func (k CssRuleKind) String() string

type Declaration

type Declaration struct {
	Property  string
	Value     string
	Important bool
}

func NewCssDeclaration

func NewCssDeclaration() *Declaration

func ParseDeclarations

func ParseDeclarations(content string, options ...ParserOption) ([]*Declaration, error)

func (*Declaration) Equal

func (d *Declaration) Equal(other *Declaration) bool

func (*Declaration) String

func (d *Declaration) String() string

func (*Declaration) StringWithImportant

func (d *Declaration) StringWithImportant(option bool) string

func (*Declaration) ValueString

func (d *Declaration) ValueString() string

func (*Declaration) ValueStringWithImportant

func (d *Declaration) ValueStringWithImportant(option bool) string

type DeclarationsByProperty

type DeclarationsByProperty []*Declaration

func (DeclarationsByProperty) Len

func (ds DeclarationsByProperty) Len() int

Implements sort.Interface

func (DeclarationsByProperty) Less

func (ds DeclarationsByProperty) Less(i, j int) bool

func (DeclarationsByProperty) Swap

func (ds DeclarationsByProperty) Swap(i, j int)

func (DeclarationsByProperty) ToObject

func (ds DeclarationsByProperty) ToObject() map[string]string

ToObject converts DeclarationsByProperty to a map[string]string

type Parser

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

func NewParser

func NewParser(content string, options ...ParserOption) *Parser

func (*Parser) ParseDeclarations

func (p *Parser) ParseDeclarations() ([]*Declaration, error)

ParseDeclarations parses a list of CSS declarations from the input, now it uses only in inline mode.

func (*Parser) ParseRule

func (p *Parser) ParseRule() (*CssRule, error, bool)

func (*Parser) ParseRules

func (p *Parser) ParseRules() ([]*CssRule, error)

func (*Parser) ParseStylesheet

func (p *Parser) ParseStylesheet() (*Stylesheet, error)

type ParserOption added in v0.0.8

type ParserOption func(*Parser)

func WithInline added in v0.0.8

func WithInline(inline bool) ParserOption

func WithLooseParsing added in v0.0.8

func WithLooseParsing(loose bool) ParserOption

type Stylesheet

type Stylesheet struct {
	Rules []*CssRule
}

func NewStylesheet

func NewStylesheet() *Stylesheet

func ParseStylesheet

func ParseStylesheet(content string, options ...ParserOption) (*Stylesheet, error)

func (*Stylesheet) String

func (s *Stylesheet) String() string

Jump to

Keyboard shortcuts

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