CssTidy

csstidy
in package

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

Tags
author

Florian Schmitz (floele at gmail dot com) 2005-2006

version
2.2.1

Table of Contents

Properties

$added  : bool
=true if something has been added to the current selector
$at  : string
Saves the current at rule (@media)
$charset  : string
Saves the CSS charset (@charset)
$css  : array<string|int, mixed>
Saves the parsed CSS. This array is empty if preserve_css is on.
$cur_string  : mixed
$data  : array<string|int, mixed>
Various CSS Data for CSSTidy
$from  : array<string|int, mixed>
Status from which the parser switched to ic or instr
$import  : array<string|int, mixed>
Saves all @import URLs
$invalid_at  : bool
=true if in invalid at-rule
$line  : int
Saves the line number
$log  : array<string|int, mixed>
Array which saves the message log
$namespace  : string
Saves the namespace
$next_selector_at  : string
Saves the at rule for next selector (during @font-face or other @)
$optimise  : object
Optimiser class
$print  : object
Printer class
$property  : string
Saves the current property
$quoted_string  : array<string|int, mixed>
Marks if we need to leave quotes for a string
$sel_separate  : array<string|int, mixed>
Saves the position of , in selectors
$selector  : string
Saves the current selector
$settings  : array<string|int, mixed>
Stores the settings
$status  : string
Saves the parser-status.
$str_char  : array<string|int, mixed>
Saves the stack of characters that opened the current strings
$sub_value  : string
Saves the current sub-value
$sub_value_arr  : array<string|int, mixed>
Array which saves all subvalues for a property.
$template  : mixed
$tokens  : array<string|int, mixed>
Saves the parsed CSS (raw)
$tokens_list  : string
List of tokens
$value  : string
Saves the current value
$version  : string
Contains the version of csstidy

Methods

__construct()  : mixed
Loads standard template and sets default settings
_add_token()  : mixed
Adds a token to $this->tokens
_load_template()  : mixed
Load a template
_unicode()  : string
Parse unicode notations and find a replacement character
css_add_important_comment()  : mixed
Add an important comment to the css code (one we want to keep)
css_add_property()  : mixed
Adds a property with value to the existing CSS code
css_check_last_media_section_or_inc()  : int|string
Check if a current media section is the continuation of the last one if not inc the name of the media section to avoid a merging
css_close_media_section()  : string
Close a media section Find the parent media we were in before or the root
css_new_media_section()  : string
Start a new media section.
css_new_property()  : string
Start a new propertie.
css_new_selector()  : string
Start a new selector.
escaped()  : bool
Checks if a character is escaped (and returns true if it is)
explode_selectors()  : mixed
Explodes selectors
get_cfg()  : mixed
Get the value of a setting.
gvw_important()  : string
Returns a value without !important
is_important()  : bool
Checks if $value is !important.
is_token()  : mixed
Checks if there is a token at the current position
load_template()  : mixed
Loads a new template
log()  : mixed
Add a message to the message log
merge_css_blocks()  : mixed
Adds CSS to an existing media/selector
parse()  : bool
Parses CSS in $string. The code is saved as array in $this->css
parse_from_url()  : mixed
Starts parsing from URL
parse_string_list()  : array<string|int, mixed>
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:
property_is_next()  : bool
Checks if the next word in a string from pos is a CSS property
property_is_valid()  : bool
Checks if a property is valid
quote_font_format()  : string
format() in font-face needs quoted values for somes browser (FF at least)
set_cfg()  : bool
Set the value of a setting.
write()  : mixed
Write plain output to a file
write_page()  : mixed
Write formatted output to a file

Properties

$added

=true if something has been added to the current selector

public bool $added = \false
Tags
access

private

$at

Saves the current at rule (@media)

public string $at = ''
Tags
access

private

$charset

Saves the CSS charset (@charset)

public string $charset = ''
Tags
access

private

$css

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

public array<string|int, mixed> $css = array()
Tags
access

public

$data

Various CSS Data for CSSTidy

public array<string|int, mixed> $data
Tags
todo

array{csstidy:array<string,mixed>}

$from

Status from which the parser switched to ic or instr

public array<string|int, mixed> $from = array()
Tags
access

private

$import

Saves all @import URLs

public array<string|int, mixed> $import = array()
Tags
access

private

$invalid_at

=true if in invalid at-rule

public bool $invalid_at = \false
Tags
access

private

$line

Saves the line number

public int $line = 1
Tags
access

private

$log

Array which saves the message log

public array<string|int, mixed> $log = array()
Tags
access

private

$namespace

Saves the namespace

public string $namespace = ''
Tags
access

private

$next_selector_at

Saves the at rule for next selector (during @font-face or other @)

public string $next_selector_at = ''
Tags
access

private

$property

Saves the current property

public string $property = ''
Tags
access

private

$quoted_string

Marks if we need to leave quotes for a string

public array<string|int, mixed> $quoted_string = array()
Tags
access

private

$sel_separate

Saves the position of , in selectors

public array<string|int, mixed> $sel_separate = array()
Tags
access

private

$selector

Saves the current selector

public string $selector = ''
Tags
access

private

$settings

Stores the settings

public array<string|int, mixed> $settings = array()
Tags
access

private

$status

Saves the parser-status.

public string $status = 'is'

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
Tags
access

private

$str_char

Saves the stack of characters that opened the current strings

public array<string|int, mixed> $str_char = array()
Tags
access

private

$sub_value

Saves the current sub-value

public string $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

Tags
access

private

$sub_value_arr

Array which saves all subvalues for a property.

public array<string|int, mixed> $sub_value_arr = array()
Tags
see
sub_value
access

private

$tokens

Saves the parsed CSS (raw)

public array<string|int, mixed> $tokens = array()
Tags
access

private

$tokens_list

List of tokens

public string $tokens_list = ""

$value

Saves the current value

public string $value = ''
Tags
access

private

$version

Contains the version of csstidy

public string $version = '2.0.3'
Tags
access

private

Methods

__construct()

Loads standard template and sets default settings

public __construct() : mixed
Tags
access

private

version
1.3

_add_token()

Adds a token to $this->tokens

public _add_token(mixed $type, string $data[, bool $do = false ]) : mixed
Parameters
$type : mixed
$data : string
$do : bool = false

add a token even if preserve_css is off

Tags
access

private

version
1.0

_load_template()

Load a template

public _load_template(string $template) : mixed
Parameters
$template : string

used by set_cfg to load a template via a configuration setting

Tags
access

private

version
1.4

_unicode()

Parse unicode notations and find a replacement character

public _unicode(string &$string, int &$i) : string
Parameters
$string : string
$i : int
Tags
access

private

version
1.2
Return values
string

css_add_important_comment()

Add an important comment to the css code (one we want to keep)

public css_add_important_comment(mixed $comment) : mixed
Parameters
$comment : mixed

css_add_property()

Adds a property with value to the existing CSS code

public css_add_property(string $media, string $selector, string $property, string $new_val) : mixed
Parameters
$media : string
$selector : string
$property : string
$new_val : string
Tags
access

private

version
1.2

css_check_last_media_section_or_inc()

Check if a current media section is the continuation of the last one if not inc the name of the media section to avoid a merging

public css_check_last_media_section_or_inc(int|string $media) : int|string
Parameters
$media : int|string
Return values
int|string

css_close_media_section()

Close a media section Find the parent media we were in before or the root

public css_close_media_section(mixed $current_media) : string
Parameters
$current_media : mixed
Return values
string

css_new_media_section()

Start a new media section.

public css_new_media_section(string $current_media, string $new_media[, bool $at_root = false ]) : string

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

Parameters
$current_media : string
$new_media : string
$at_root : bool = false
Return values
string

css_new_property()

Start a new propertie.

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

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

Parameters
$media : string
$selector : string
$property : string
Return values
string

css_new_selector()

Start a new selector.

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

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
$media : string
$selector : string
Return values
string

escaped()

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

public static escaped(string &$string, int $pos) : bool
Parameters
$string : string
$pos : int
Tags
access

public

version
1.02
Return values
bool

explode_selectors()

Explodes selectors

public explode_selectors() : mixed
Tags
access

private

version
1.0

get_cfg()

Get the value of a setting.

public get_cfg(string $setting) : mixed
Parameters
$setting : string
Tags
access

public

version
1.0

gvw_important()

Returns a value without !important

public gvw_important(string $value) : string
Parameters
$value : string
Tags
access

public

version
1.0
Return values
string

is_important()

Checks if $value is !important.

public is_important(string &$value) : bool
Parameters
$value : string
Tags
access

public

version
1.0
Return values
bool

is_token()

Checks if there is a token at the current position

public is_token(string &$string, int $i) : mixed
Parameters
$string : string
$i : int
Tags
access

public

version
1.11

load_template()

Loads a new template

public load_template(string $content[, bool $from_file = true ]) : mixed
Parameters
$content : string

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

$from_file : bool = true

uses $content as filename if true

Tags
access

public

version
1.1
see
http://csstidy.sourceforge.net/templates.php

log()

Add a message to the message log

public log(string $message, string $type[, int $line = -1 ]) : mixed
Parameters
$message : string
$type : string
$line : int = -1
Tags
access

private

version
1.0

merge_css_blocks()

Adds CSS to an existing media/selector

public merge_css_blocks(string $media, string $selector, array<string|int, mixed> $css_add) : mixed
Parameters
$media : string
$selector : string
$css_add : array<string|int, mixed>
Tags
access

private

version
1.1

parse()

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

public parse(string $string) : bool
Parameters
$string : string

the CSS code

Tags
access

public

version
1.1
Return values
bool

parse_from_url()

Starts parsing from URL

public parse_from_url(string $url) : mixed
Parameters
$url : string
Tags
access

public

version
1.0

parse_string_list()

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:

public parse_string_list(string $value) : array<string|int, mixed>

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 : string
Return values
array<string|int, mixed>

property_is_next()

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

public property_is_next(string $istring, int $pos) : bool
Parameters
$istring : string
$pos : int
Tags
access

private

version
1.2
Return values
bool

property_is_valid()

Checks if a property is valid

public property_is_valid(string $property) : bool
Parameters
$property : string
Tags
access

public

version
1.0
Return values
bool

quote_font_format()

format() in font-face needs quoted values for somes browser (FF at least)

public quote_font_format(mixed $value) : string
Parameters
$value : mixed
Return values
string

set_cfg()

Set the value of a setting.

public set_cfg(array<string|int, mixed>|string $setting[, mixed $value = null ]) : bool
Parameters
$setting : array<string|int, mixed>|string
$value : mixed = null
Tags
access

public

version
1.0
Return values
bool

write()

Write plain output to a file

public write(string $filename[, bool $formatted = false ][, string $doctype = 'xhtml1.1' ][, bool $externalcss = true ][, string $title = '' ][, string $lang = 'en' ][, bool $pre_code = true ]) : mixed
Parameters
$filename : string
$formatted : bool = false

whether to print formatted or not

$doctype : string = 'xhtml1.1'

when printing formatted, is a shorthand for the document type

$externalcss : bool = true

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

$title : string = ''

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

$lang : string = 'en'

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

$pre_code : bool = true

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

Tags
access

public

version
1.4

write_page()

Write formatted output to a file

public write_page(string $filename[, string $doctype = 'xhtml1.1' ][, bool $externalcss = true ][, string $title = '' ][, string $lang = 'en' ]) : mixed
Parameters
$filename : string
$doctype : string = 'xhtml1.1'

when printing formatted, is a shorthand for the document type

$externalcss : bool = true

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

$title : string = ''

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

$lang : string = 'en'

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

Tags
access

public

version
1.4

        
On this page

Search results