cssparser

package module
v0.0.6 Latest Latest
Warning

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

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

README

go-css-parser

Installation

go get go.baoshuo.dev/css-parser

Usage

package main

import "go.baoshuo.dev/css-parser"

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.

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 added in v0.0.6

type Declaration struct {
	Property  string
	Value     string
	Important bool
}

func NewCssDeclaration

func NewCssDeclaration() *Declaration

func ParseDeclarations

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

func (*Declaration) Equal added in v0.0.6

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

func (*Declaration) String added in v0.0.6

func (d *Declaration) String() string

func (*Declaration) StringWithImportant added in v0.0.6

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

func (*Declaration) ValueString added in v0.0.6

func (d *Declaration) ValueString() string

func (*Declaration) ValueStringWithImportant added in v0.0.6

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 added in v0.0.5

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, inline bool) *Parser

func (*Parser) ParseDeclarations

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

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 Stylesheet added in v0.0.6

type Stylesheet struct {
	Rules []*CssRule
}

func NewStylesheet added in v0.0.6

func NewStylesheet() *Stylesheet

func ParseStylesheet

func ParseStylesheet(content string) (*Stylesheet, error)

func (*Stylesheet) String added in v0.0.6

func (s *Stylesheet) String() string

Source Files

  • css_declaration.go
  • css_rule.go
  • css_stylesheet.go
  • doc.go
  • parser.go
  • parser_base.go
  • parser_util.go

Jump to

Keyboard shortcuts

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