css

package module
v0.0.0-...-c88ee70 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2019 License: BSD-3-Clause Imports: 9 Imported by: 0

README

go-css

Important! This was originally forked from napsy.
Original Repo.


This parser understands simple CSS and comes with a basic CSS syntax checker. Does not currently support the following features:

  • Media queries partially supported
  • Comma seperated selectors partially supported

Installation

go get github.com/itskass/go-css

Example usage:


import "github.com/itskass/go-css"

ex1 := `rule {
	style1: value1;
	style2: value2;
}`

stylesheet, err := css.Unmarshal([]byte(ex1))
if err != nil {
	panic(err)
}

fmt.Printf("Defined rules:\n")

for k, _ := range stylesheet {
	fmt.Printf("- rule %q\n", k)
}

You can get a CSS verifiable property by calling CSSStyle:

style, err := css.CSSStyle("background-color", styleSheet["body"])
if err != nil {
	fmt.Printf("Error checking body background color: %v\n", err)
} else {
	fmt.Printf("Body background color is %v", style)
}

Most of the CSS properties are currently not implemented, but you can always write your own handler by writing a StyleHandler function and adding it to the StylesTable map.


(Forked)

This was forked from napsy because I required extra functionality like the ability to return all tokens amoung others.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StylesTable = map[string]StyleHandler{
	"background":                    background,
	"background-attachment":         backgroundAttachment,
	"background-color":              backgroundColor,
	"background-image":              backgroundImage,
	"background-position":           backgroundPosition,
	"background-repeat":             backgroundRepeat,
	"border":                        border,
	"border-bottom":                 borderBottom,
	"border-bottom-color":           borderBottomColor,
	"border-bottom-style":           borderBottomStyle,
	"border-bottom-width":           borderBottomWidth,
	"border-color":                  borderColor,
	"border-left":                   borderLeft,
	"border-left-color":             borderLeftColor,
	"border-left-style":             borderLeftStyle,
	"border-left-width":             borderLeftWidth,
	"border-right":                  borderRight,
	"border-right-color":            borderRightColor,
	"border-right-style":            borderRightStyle,
	"border-right-width":            borderRightWidth,
	"border-style":                  borderStyle,
	"border-top":                    borderTop,
	"border-top-color":              borderTopColor,
	"border-top-style":              borderTopStyle,
	"border-top-width":              borderTopWidth,
	"border-width":                  borderWidth,
	"clear":                         clear,
	"clip":                          clip,
	"color":                         color,
	"cursor":                        cursor,
	"display":                       display,
	"filter":                        filter,
	"font":                          font,
	"font-family":                   fontFamily,
	"font-size":                     fontSize,
	"font-variant":                  fontVariant,
	"font-weight":                   fontWeight,
	"height":                        height,
	"left":                          left,
	"letter-spacing":                letterSpacing,
	"line-height":                   lineHeight,
	"list-style":                    listStyle,
	"list-style-image":              listStyleImage,
	"list-style-position":           listStylePosition,
	"list-style-type":               listStyleType,
	"margin":                        margin,
	"margin-bottom":                 marginBottom,
	"margin-left":                   marginLeft,
	"margin-right":                  marginRight,
	"margin-top":                    marginTop,
	"overflow":                      overflow,
	"padding":                       padding,
	"padding-bottom":                paddingBottom,
	"padding-left":                  paddingLeft,
	"padding-right":                 paddingRight,
	"padding-top":                   paddingTop,
	"page-break-after":              pageBreakAfter,
	"page-break-before":             pageBreakBefore,
	"position":                      position,
	"float":                         float,
	"text-align":                    textAlign,
	"text-decoration":               textDecoration,
	"text-decoration: blink":        textDecorationBlink,
	"text-decoration: line-through": textDecorationLineThrough,
	"text-decoration: none":         textDecorationNone,
	"text-decoration: overline":     textDecorationOverline,
	"text-decoration: underline":    textDecorationUnderline,
	"text-indent":                   textIndent,
	"text-transform":                textTransform,
	"top":                           top,
	"vertical-align":                verticalAlign,
	"visibility":                    visibility,
	"width":                         width,
	"z-index":                       zIndex,
}

Common CSS styles. You can overwrite the handlers with your own.

Functions

func BlockCount

func BlockCount(tokens *list.List) int

BlockCount returns the number code blocks in the css

func Comments

func Comments(b []byte) []string

Comments returns all css comments

func Identifiers

func Identifiers(tokens *list.List) []string

Identifiers returns all class, id and element identifiers including duplicants

func Licenses

func Licenses(b []byte) []string

Licenses returns all css License. Will return empty string if no license exists .

func Parse

func Parse(l *list.List) (map[Rule]map[string]string, error)

func Styles

func Styles(css map[Rule]map[string]string) []string

Styles will return all the styles in a css as an arrays

func Tokenize

func Tokenize(b []byte) *list.List

Tokenize builds a token list from css bytes

func Unmarshal

func Unmarshal(b []byte) (map[Rule]map[string]string, error)

Unmarshal will take a byte slice, containing sylesheet rules and return a map of a rules map.

Types

type Rule

type Rule string

Rule is a string type that represents a CSS rule.

func Selectors

func Selectors(tokens *list.List) []Rule

Selectors will return all the selectors, including duplicants.

func (Rule) Type

func (rule Rule) Type() string

Type returns the rule type, which can be a class, id or a tag.

type Style

type Style struct {
	Value interface{}
	// contains filtered or unexported fields
}

func CSSStyle

func CSSStyle(name string, styles map[string]string) (Style, error)

CSSStyle returns an error-checked parsed style, or an error if the style is unknown. Most of the styles are not supported yet.

func (Style) String

func (style Style) String() string

func (Style) Unit

func (style Style) Unit() UnitType

type StyleHandler

type StyleHandler func(value string) (Style, error)

StyleHandler is a function that checks the style value for errors and returns a Style

type TokenEntry

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

type UnitType

type UnitType int
const (
	UnitNone UnitType = iota
	UnitPixels
	UnitEm
	UnitRem
	UnitPercent
	UnitPt
	UnitAuto
)

type UnitValue

type UnitValue float64

Jump to

Keyboard shortcuts

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