css

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package css - parse CSS

DESCRIPTION

`css` is a library offering basic CSS parsing. CSS support is minimal enough to support github.com/pirmd/libro needs, essentially identifying possible CSS security issues based on a CSS white-list approach.

`css` is based on github.com/gorilla/css CSS scanner.

Even though, I've tried hard enough to stick to CSS standard and detecting possible CSS syntax issues, it is not a target at this time. You'd probably better go with github.com/aymerick/douceur/. Several test cases are borrowed for its implementation so `css` might be close in term of parsing "correctness". Besides implementation choices and type format, main difference lies in the fact that `css` kept github.com/gorilla/css tokens when describing CSS Rules for easier rules inspection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Declaration

type Declaration struct {
	// Property is the style property's name.
	Property string

	// Value is the style property's value as a list of token.
	Value Value

	// IsImportant is true if the property is tagged as '!important'.
	IsImportant bool
	// contains filtered or unexported fields
}

Declaration represents a style definition.

func (*Declaration) AppendToValue

func (d *Declaration) AppendToValue(tok *scanner.Token) error

AppendToValue adds a new token to a property. It makes sure that '!important' tag are captured.

func (*Declaration) String

func (d *Declaration) String() string

type Rule

type Rule struct {

	// Selectors contains the list of tokens of pre-Block declaration that is to say:
	//  - list of selectors for qualified rules,
	//  - list of at-rule specific properties (anything before a ';' or '{'),
	// Selectors are split during parsing operation using ',' as separator,
	Selectors []Value

	// Declarations contains the list of style declarations for the given Rule.
	Declarations []*Declaration

	// AtKeyword is the at-keyword introducing teh Rule. If the Rule is a
	// qualified rule, AtKeyword is nil.
	AtKeyword *scanner.Token

	// EmbeddedRuleset contains the nested rules for at-rules that expects it,
	// otherwise is nil.
	EmbeddedRuleset Ruleset
}

Rule represents a CSS rule

func (Rule) String

func (r Rule) String() string

type Ruleset

type Ruleset []*Rule

Ruleset represents a series of rules. NOTA: Ruleset is slightly different from https://www.w3.org/People/howcome/foo.dir/grammar.html definition as it extend the notion to also capture at-rules nested rules.

func Parse

func Parse(s string) (Ruleset, error)

Parse parses CSS text for its list of Rules.

func ParseInline

func ParseInline(s string) (Ruleset, error)

ParseInline parses inline CSS text for its list of Rules.

func (Ruleset) String

func (rules Ruleset) String() string

type Value

type Value []*scanner.Token

Value represents a tokenized element (without comments).

func (*Value) Append

func (val *Value) Append(tok *scanner.Token)

Append adds a new token to a Value.

func (Value) Scrub

func (val Value) Scrub() Value

Scrub is called once all token have been scanned for a given Value so that the Value's content is cleaned from:

  • Trailing or Tailing token of type scanner.TokenS;
  • Token of type scanner.TokenComment;
  • "inline" comments are detected and Value is rescanned without them to address of tricky injections like "expr/*XSS*/ession(alert('XSS'))".

func (Value) String

func (val Value) String() string

Jump to

Keyboard shortcuts

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