\csstidy

CSS Parser class

This class represents a CSS parser which reads CSS code and saves it in an array. In opposite to most other CSS parsers, it does not use regular expressions and thus has full CSS2 support and a higher reliability. Additional to that it applies some optimisations and fixes to the CSS code. An online version should be available here: http://cdburnerxp.se/cssparse/css_optimiser.php

Summary

Methods
Properties
Constants
csstidy()
get_cfg()
_load_template()
set_cfg()
_add_token()
log()
_unicode()
write_page()
write()
load_template()
parse_from_url()
is_token()
parse()
explode_selectors()
escaped()
css_add_property()
css_new_media_section()
css_new_selector()
css_new_property()
merge_css_blocks()
is_important()
gvw_important()
property_is_next()
property_is_valid()
parse_string_list()
$css
$tokens
$print
$optimise
$charset
$import
$namespace
$version
$settings
$status
$at
$selector
$property
$sel_separate
$value
$sub_value
$sub_value_arr
$str_char
$cur_string
$from
$invalid_at
$added
$log
$line
$quoted_string
$tokens_list
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$css

$css : array

Saves the parsed CSS. This array is empty if preserve_css is on.

Type

array

$tokens

$tokens : array

Saves the parsed CSS (raw)

Type

array

$print

$print : object

Printer class

Type

object

$optimise

$optimise : object

Optimiser class

Type

object

$charset

$charset : string

Saves the CSS charset (@charset)

Type

string

$import

$import : array

Saves all @import URLs

Type

array

$namespace

$namespace : string

Saves the namespace

Type

string

$version

$version : string

Contains the version of csstidy

Type

string

$settings

$settings : array

Stores the settings

Type

array

$status

$status : string

Saves the parser-status.

Possible values:

  • is = in selector
  • ip = in property
  • iv = in value
  • instr = in string (started at " or ' or ( )
  • ic = in comment (ignore everything)
  • at = in @-block

Type

string

$at

$at : string

Saves the current at rule (@media)

Type

string

$selector

$selector : string

Saves the current selector

Type

string

$property

$property : string

Saves the current property

Type

string

$sel_separate

$sel_separate : array

Saves the position of , in selectors

Type

array

$value

$value : string

Saves the current value

Type

string

$sub_value

$sub_value : string

Saves the current sub-value

Example for a subvalue: background:url(foo.png) red no-repeat; "url(foo.png)", "red", and "no-repeat" are subvalues, seperated by whitespace

Type

string

$sub_value_arr

$sub_value_arr : array

Array which saves all subvalues for a property.

Type

array

$str_char

$str_char : array

Saves the stack of characters that opened the current strings

Type

array

$cur_string

$cur_string

$from

$from : array

Status from which the parser switched to ic or instr

Type

array

$invalid_at

$invalid_at : bool

=true if in invalid at-rule

Type

bool

$added

$added : bool

=true if something has been added to the current selector

Type

bool

$log

$log : array

Array which saves the message log

Type

array

$line

$line : integer

Saves the line number

Type

integer

$quoted_string

$quoted_string : array

Marks if we need to leave quotes for a string

Type

array

$tokens_list

$tokens_list : string

List of tokens

Type

string

Methods

csstidy()

csstidy()

Loads standard template and sets default settings

get_cfg()

get_cfg(string $setting) : mixed

Get the value of a setting.

Parameters

string $setting

Returns

mixed

_load_template()

_load_template(string $template)

Load a template

Parameters

string $template

used by set_cfg to load a template via a configuration setting

set_cfg()

set_cfg(string $setting, mixed $value) : bool

Set the value of a setting.

Parameters

string $setting
mixed $value

Returns

bool

_add_token()

_add_token(mixed $type, string $data, bool $do)

Adds a token to $this->tokens

Parameters

mixed $type
string $data
bool $do

add a token even if preserve_css is off

log()

log(string $message, string $type, integer $line)

Add a message to the message log

Parameters

string $message
string $type
integer $line

_unicode()

_unicode(string $string, integer $i) : string

Parse unicode notations and find a replacement character

Parameters

string $string
integer $i

Returns

string

write_page()

write_page(string $filename, string $doctype, bool $externalcss, string $title, string $lang)

Write formatted output to a file

Parameters

string $filename
string $doctype

when printing formatted, is a shorthand for the document type

bool $externalcss

when printing formatted, indicates whether styles to be attached internally or as an external stylesheet

string $title

when printing formatted, is the title to be added in the head of the document

string $lang

when printing formatted, gives a two-letter language code to be added to the output

write()

write(string $filename, bool $formatted, string $doctype, bool $externalcss, string $title, string $lang, bool $pre_code)

Write plain output to a file

Parameters

string $filename
bool $formatted

whether to print formatted or not

string $doctype

when printing formatted, is a shorthand for the document type

bool $externalcss

when printing formatted, indicates whether styles to be attached internally or as an external stylesheet

string $title

when printing formatted, is the title to be added in the head of the document

string $lang

when printing formatted, gives a two-letter language code to be added to the output

bool $pre_code

whether to add pre and code tags around the code (for light HTML formatted templates)

load_template()

load_template(string $content, bool $from_file)

Loads a new template

Parameters

string $content

either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default"

bool $from_file

uses $content as filename if true

parse_from_url()

parse_from_url(string $url)

Starts parsing from URL

Parameters

string $url

is_token()

is_token(string $string, integer $i)

Checks if there is a token at the current position

Parameters

string $string
integer $i

parse()

parse(string $string) : bool

Parses CSS in $string. The code is saved as array in $this->css

Parameters

string $string

the CSS code

Returns

bool

explode_selectors()

explode_selectors()

Explodes selectors

escaped()

escaped(string $string, integer $pos) : bool

Checks if a character is escaped (and returns true if it is)

Parameters

string $string
integer $pos

Returns

bool

css_add_property()

css_add_property(string $media, string $selector, string $property, string $new_val)

Adds a property with value to the existing CSS code

Parameters

string $media
string $selector
string $property
string $new_val

css_new_media_section()

css_new_media_section(string $media) : string

Start a new media section.

Check if the media is not already known, else rename it with extra spaces to avoid merging

Parameters

string $media

Returns

string

css_new_selector()

css_new_selector(string $media, string $selector) : string

Start a new selector.

If already referenced in this media section, rename it with extra space to avoid merging except if merging is required, or last selector is the same (merge siblings)

never merge @font-face

Parameters

string $media
string $selector

Returns

string

css_new_property()

css_new_property(string $media, string $selector, string $property) : string

Start a new propertie.

If already references in this selector, rename it with extra space to avoid override

Parameters

string $media
string $selector
string $property

Returns

string

merge_css_blocks()

merge_css_blocks(string $media, string $selector, array $css_add)

Adds CSS to an existing media/selector

Parameters

string $media
string $selector
array $css_add

is_important()

is_important(string $value) : bool

Checks if $value is !important.

Parameters

string $value

Returns

bool

gvw_important()

gvw_important(string $value) : string

Returns a value without !important

Parameters

string $value

Returns

string

property_is_next()

property_is_next(string $istring, integer $pos) : bool

Checks if the next word in a string from pos is a CSS property

Parameters

string $istring
integer $pos

Returns

bool

property_is_valid()

property_is_valid(string $property) : \bool;

Checks if a property is valid

Parameters

string $property

Returns

\bool;

parse_string_list()

parse_string_list( $value) : array

Accepts a list of strings (e.g., the argument to format() in a @font-face src property) and returns a list of the strings. Converts things like:

format(abc) => format("abc") format(abc def) => format("abc","def") format(abc "def") => format("abc","def") format(abc, def, ghi) => format("abc","def","ghi") format("abc",'def') => format("abc","def") format("abc, def, ghi") => format("abc, def, ghi")

Parameters

$value

Returns

array