css

package
v0.0.0-...-0b4b87a Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCSS = `` /* 2776-byte string literal not displayed */

Variables

View Source
var (
	NoStyles     = errors.New("No styles to apply")
	InheritValue = errors.New("Value should be inherited")
)
View Source
var DefaultFontSize int
View Source
var PixelsPerPt float64

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func ClearFontCache

func ClearFontCache()

func ConvertColorToRGBA

func ConvertColorToRGBA(cssString string) (*color.RGBA, error)

func ConvertUnitToPx

func ConvertUnitToPx(fontsize int, percentbasis int, cssString string) (int, error)

func IsBorderStyle

func IsBorderStyle(s string) bool

func IsColor

func IsColor(c string) bool

func IsLength

func IsLength(l string) bool

func IsPercentage

func IsPercentage(p string) bool

func IsURL

func IsURL(u string) bool

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func ParseBlock

func ParseBlock(val string) map[StyleAttribute]StyleValue

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

Types

type CSSSelector

type CSSSelector struct {
	Selector    string
	OrderNumber uint
}

func (CSSSelector) Matches

func (s CSSSelector) Matches(el *html.Node, st State) bool

func (CSSSelector) NumberAttrs

func (s CSSSelector) NumberAttrs() int

func (CSSSelector) NumberClasses

func (s CSSSelector) NumberClasses() int

func (CSSSelector) NumberElements

func (s CSSSelector) NumberElements() int

func (CSSSelector) NumberIDs

func (s CSSSelector) NumberIDs() int

func (CSSSelector) NumberPseudo

func (s CSSSelector) NumberPseudo() int

func (CSSSelector) String

func (c CSSSelector) String() string

type FontFamily

type FontFamily string

type State

type State struct {
	Link, Visited, Active bool // CSS1
	Hover                 bool // CSS2
}

type StyleAttribute

type StyleAttribute string

type StyleRule

type StyleRule struct {
	Selector CSSSelector
	Name     StyleAttribute
	Value    StyleValue
	Src      StyleSource
}

func (StyleRule) Matches

func (r StyleRule) Matches(el *html.Node, st State) bool

func (StyleRule) String

func (sr StyleRule) String() string

type StyleSource

type StyleSource uint8
const (
	UnknownSrc StyleSource = iota
	UserAgentSrc
	UserSrc
	AuthorSrc
	InlineStyleSrc
)

func (StyleSource) String

func (s StyleSource) String() string

type StyleValue

type StyleValue struct {
	Value     string
	Important bool
}

func NewPxValue

func NewPxValue(x int) StyleValue

func NewValue

func NewValue(val string) StyleValue

func (StyleValue) String

func (sv StyleValue) String() string

type StyledElement

type StyledElement struct {
	// CSS Level 1 attributes
	// Shorthand attributes are commented out, because
	// there's not any time in code you would want them
	// instead of the expanded versions..
	FontFamily  StyleValue
	FontStyle   StyleValue
	FontVariant StyleValue
	FontWeight  StyleValue
	FontSize    StyleValue

	Color                StyleValue
	BackgroundColor      StyleValue
	BackgroundImage      StyleValue
	BackgroundRepeat     StyleValue
	BackgroundAttachment StyleValue
	BackgroundPosition   StyleValue

	WordSpacing   StyleValue
	LetterSpacing StyleValue

	TextDecoration StyleValue
	VerticalAlign  StyleValue
	TextTransform  StyleValue
	TextAlign      StyleValue
	TextIndent     StyleValue
	LineHeight     StyleValue

	MarginTop    StyleValue
	MarginRight  StyleValue
	MarginBottom StyleValue
	MarginLeft   StyleValue

	PaddingTop    StyleValue
	PaddingRight  StyleValue
	PaddingBottom StyleValue
	PaddingLeft   StyleValue

	BorderTopWidth StyleValue
	BorderTopColor StyleValue
	BorderTopStyle StyleValue

	BorderBottomWidth StyleValue
	BorderBottomColor StyleValue
	BorderBottomStyle StyleValue

	BorderLeftWidth StyleValue
	BorderLeftColor StyleValue
	BorderLeftStyle StyleValue

	BorderRightWidth StyleValue
	BorderRightColor StyleValue
	BorderRightStyle StyleValue

	Width  StyleValue
	Height StyleValue

	Float StyleValue
	Clear StyleValue

	Display StyleValue

	WhiteSpace StyleValue

	ListStyleType     StyleValue
	ListStyleImage    StyleValue
	ListStylePosition StyleValue

	// New CSS Level 2 Attributes
	// Table related properties
	TableLayout    StyleValue
	BorderCollapse StyleValue
	BorderSpacing  StyleValue
	EmptyCells     StyleValue
	CaptionSide    StyleValue

	// Visual Formatting related properties
	Position StyleValue
	Top      StyleValue
	Bottom   StyleValue
	Left     StyleValue
	Right    StyleValue

	Overflow   StyleValue
	Visibility StyleValue
	Clip       StyleValue
	ZIndex     StyleValue

	MinHeight StyleValue
	MaxHeight StyleValue
	MinWidth  StyleValue
	MaxWidth  StyleValue

	// Generated content related properties
	Content          StyleValue
	CounterIncrement StyleValue
	CounterReset     StyleValue
	Quotes           StyleValue

	// UI Related properties
	Cursor StyleValue

	// Other
	Direction   StyleValue
	UnicodeBidi StyleValue
	// contains filtered or unexported fields
}

A StyleElement is anything that has CSS rules applied to it.

func (*StyledElement) AddStyle

func (e *StyledElement) AddStyle(s StyleRule)

func (*StyledElement) ClearStyles

func (e *StyledElement) ClearStyles()

func (*StyledElement) DisplayProp

func (e *StyledElement) DisplayProp() string

func (*StyledElement) GetAttribute

func (e *StyledElement) GetAttribute(attr string) StyleValue

func (StyledElement) GetBackgroundImage

func (e StyledElement) GetBackgroundImage() (string, error)

Returns the URL as a string, and an error. Retrieving the URL is left as an exercise for the caller, since the CSS package doesn't know the host/path to resolve relative URLs

func (StyledElement) GetColor

func (e StyledElement) GetColor(defaultColour color.Color) (color.Color, error)

func (StyledElement) GetFontFace

func (e StyledElement) GetFontFace(fsize int, fontFamily FontFamily, weight font.Weight, style font.Style) font.Face

func (StyledElement) GetFontSize

func (e StyledElement) GetFontSize() (int, error)

func (StyledElement) MergeStyles

func (e StyledElement) MergeStyles(other ...*StyledElement) StyledElement

func (*StyledElement) SetFontSize

func (e *StyledElement) SetFontSize(size int)

func (*StyledElement) SortStyles

func (e *StyledElement) SortStyles() error

SortStyles will sort the rules on this element according to the CSS spec, which states

1. Find all declarations that apply too element/property (already done when this is called) 2. Sort according to importance (normal or important) and origin (author, user, or user agent). In ascending order of precedence:

  1. user agent declarations (defaults)
  2. user normal declrations (don't exist)
  3. author normal declarations
  4. author important declarations
  5. user important declarations (don't exist)

3. Sort rules with the same importance and origin by specificity of selector: more specific selectors will override more general ones. Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively. 4. Finally, sort by order specified: if two declarations have the same weight, origin, and specificity, the latter specified wins. Declarations in imported stylesheets are considered to be before any declaration in the style sheet itself BUG(driusan): The final tie break is not implemented

func (StyledElement) String

func (e StyledElement) String() string

type Stylesheet

type Stylesheet []StyleRule

func ExtractStyles

func ExtractStyles(n *html.Node, loader net.URLReader, context *url.URL, orderNo uint) (styles Stylesheet, nextOrderNo uint)

ExtractStyles takes an html Node as input, and extracts the unparsed text from any <style> elements in the HTML, returning the string of the style body.

func ParseStylesheet

func ParseStylesheet(val string, src StyleSource, importLoader net.URLReader, urlContext *url.URL, orderNo uint) (styles Stylesheet, nextOrderNoStart uint)

Notes

Bugs

  • The final tie break is not implemented

Jump to

Keyboard shortcuts

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