Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Public Member Functions | List of all members
CssParser Class Reference

Public Member Functions

 __construct ($source=null, array $plugins=null)
 appendToken (aCssToken $token)
 clearBuffer ()
 getAndClearBuffer ($trim="", $tolower=false)
 getBuffer ($trim="", $tolower=false)
 getMediaTypes ()
 getSource ()
 getState ()
 getPlugin ($class)
 getTokens ()
 isState ($state)
 parse ($source)
 popState ()
 pushState ($state)
 setBuffer ($buffer)
 setExclusive ($exclusive)
 setMediaTypes (array $mediaTypes)
 setState ($state)
 unsetExclusive ()
 unsetMediaTypes ()

Detailed Description

Definition at line 1378 of file cssmin.php.

Constructor & Destructor Documentation

CssParser::__construct (   $source = null,
array  $plugins = null 
)

Constructer.

Create instances of the used plugins.

Parameters
string$sourceCSS source [optional]
array$pluginsPlugin configuration [optional]
Returns
void

Definition at line 1437 of file cssmin.php.

References parse(), and CssMin\triggerError().

{
$plugins = array_merge(array
(
"Comment" => true,
"String" => true,
"Url" => true,
"Expression" => true,
"Ruleset" => true,
"AtCharset" => true,
"AtFontFace" => true,
"AtImport" => true,
"AtKeyframes" => true,
"AtMedia" => true,
"AtPage" => true,
"AtVariables" => true
), is_array($plugins) ? $plugins : array());
// Create plugin instances
foreach ($plugins as $name => $config)
{
if ($config !== false)
{
$class = "Css" . $name . "ParserPlugin";
$config = is_array($config) ? $config : array();
if (class_exists($class))
{
$this->plugins[] = new $class($this, $config);
}
else
{
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The plugin <code>" . $name . "</code> with the class name <code>" . $class . "</code> was not found"));
}
}
}
if (!is_null($source))
{
$this->parse($source);
}
}

Member Function Documentation

CssParser::appendToken ( aCssToken  $token)

Append a token to the array of tokens.

Parameters
aCssToken$tokenToken to append
Returns
void

Definition at line 1482 of file cssmin.php.

{
$this->tokens[] = $token;
}
CssParser::clearBuffer ( )

Clears the current buffer.

Returns
void

Definition at line 1491 of file cssmin.php.

{
$this->buffer = "";
}
CssParser::getAndClearBuffer (   $trim = "",
  $tolower = false 
)

Returns and clear the current buffer.

Parameters
string$trimChars to use to trim the returned buffer
boolean$tolowerif TRUE the returned buffer will get converted to lower case
Returns
string

Definition at line 1502 of file cssmin.php.

References getBuffer().

{
$r = $this->getBuffer($trim, $tolower);
$this->buffer = "";
return $r;
}
CssParser::getBuffer (   $trim = "",
  $tolower = false 
)

Returns the current buffer.

Parameters
string$trimChars to use to trim the returned buffer
boolean$tolowerif TRUE the returned buffer will get converted to lower case
Returns
string

Definition at line 1515 of file cssmin.php.

Referenced by getAndClearBuffer().

{
$r = $this->buffer;
if ($trim)
{
$r = trim($r, " \t\n\r\0\x0B" . $trim);
}
if ($tolower)
{
$r = strtolower($r);
}
return $r;
}
CssParser::getMediaTypes ( )

Returns the current media types state.

Returns
array

Definition at line 1533 of file cssmin.php.

{
return $this->stateMediaTypes;
}
CssParser::getPlugin (   $class)

Returns a plugin by class name.

Parameters
string$nameClass name of the plugin
Returns
aCssParserPlugin

Definition at line 1561 of file cssmin.php.

{
static $index = null;
if (is_null($index))
{
$index = array();
for ($i = 0, $l = count($this->plugins); $i < $l; $i++)
{
$index[get_class($this->plugins[$i])] = $i;
}
}
return isset($index[$class]) ? $this->plugins[$index[$class]] : false;
}
CssParser::getSource ( )

Returns the CSS source.

Returns
string

Definition at line 1542 of file cssmin.php.

{
return $this->source;
}
CssParser::getState ( )

Returns the current state.

Returns
integer The current state

Definition at line 1551 of file cssmin.php.

{
return $this->state;
}
CssParser::getTokens ( )

Returns the parsed tokens.

Returns
array

Definition at line 1579 of file cssmin.php.

{
return $this->tokens;
}
CssParser::isState (   $state)

Returns if the current state equals the passed state.

Parameters
integer$stateState to compare with the current state
Returns
boolean TRUE is the state equals to the passed state; FALSE if not

Definition at line 1589 of file cssmin.php.

{
return ($this->state == $state);
}
CssParser::parse (   $source)

Parse the CSS source and return a array with parsed tokens.

Parameters
string$sourceCSS source
Returns
array Array with tokens

Definition at line 1599 of file cssmin.php.

Referenced by __construct().

{
// Reset
$this->source = "";
$this->tokens = array();
// Create a global and plugin lookup table for trigger chars; set array of plugins as local variable and create
// several helper variables for plugin handling
$globalTriggerChars = "";
$plugins = $this->plugins;
$pluginCount = count($plugins);
$pluginIndex = array();
$pluginTriggerStates = array();
$pluginTriggerChars = array();
for ($i = 0, $l = count($plugins); $i < $l; $i++)
{
$tPluginClassName = get_class($plugins[$i]);
$pluginTriggerChars[$i] = implode("", $plugins[$i]->getTriggerChars());
$tPluginTriggerStates = $plugins[$i]->getTriggerStates();
$pluginTriggerStates[$i] = $tPluginTriggerStates === false ? false : "|" . implode("|", $tPluginTriggerStates) . "|";
$pluginIndex[$tPluginClassName] = $i;
for ($ii = 0, $ll = strlen($pluginTriggerChars[$i]); $ii < $ll; $ii++)
{
$c = substr($pluginTriggerChars[$i], $ii, 1);
if (strpos($globalTriggerChars, $c) === false)
{
$globalTriggerChars .= $c;
}
}
}
// Normalise line endings
$source = str_replace("\r\n", "\n", $source); // Windows to Unix line endings
$source = str_replace("\r", "\n", $source); // Mac to Unix line endings
$this->source = $source;
// Variables
$buffer = &$this->buffer;
$exclusive = &$this->stateExclusive;
$state = &$this->state;
$c = $p = null;
// --
for ($i = 0, $l = strlen($source); $i < $l; $i++)
{
// Set the current Char
$c = $source[$i]; // Is faster than: $c = substr($source, $i, 1);
// Normalize and filter double whitespace characters
if ($exclusive === false)
{
if ($c === "\n" || $c === "\t")
{
$c = " ";
}
if ($c === " " && $p === " ")
{
continue;
}
}
$buffer .= $c;
// Extended processing only if the current char is a global trigger char
if (strpos($globalTriggerChars, $c) !== false)
{
// Exclusive state is set; process with the exclusive plugin
if ($exclusive)
{
$tPluginIndex = $pluginIndex[$exclusive];
if (strpos($pluginTriggerChars[$tPluginIndex], $c) !== false && ($pluginTriggerStates[$tPluginIndex] === false || strpos($pluginTriggerStates[$tPluginIndex], $state) !== false))
{
$r = $plugins[$tPluginIndex]->parse($i, $c, $p, $state);
// Return value is TRUE => continue with next char
if ($r === true)
{
continue;
}
// Return value is numeric => set new index and continue with next char
elseif ($r !== false && $r != $i)
{
$i = $r;
continue;
}
}
}
// Else iterate through the plugins
else
{
$triggerState = "|" . $state . "|";
for ($ii = 0, $ll = $pluginCount; $ii < $ll; $ii++)
{
// Only process if the current char is one of the plugin trigger chars
if (strpos($pluginTriggerChars[$ii], $c) !== false && ($pluginTriggerStates[$ii] === false || strpos($pluginTriggerStates[$ii], $triggerState) !== false))
{
// Process with the plugin
$r = $plugins[$ii]->parse($i, $c, $p, $state);
// Return value is TRUE => break the plugin loop and and continue with next char
if ($r === true)
{
break;
}
// Return value is numeric => set new index, break the plugin loop and and continue with next char
elseif ($r !== false && $r != $i)
{
$i = $r;
break;
}
}
}
}
}
$p = $c; // Set the parent char
}
return $this->tokens;
}
CssParser::popState ( )

Remove the last state of the state stack and return the removed stack value.

Returns
integer Removed state value

Definition at line 1713 of file cssmin.php.

{
$r = array_pop($this->states);
$this->state = $this->states[count($this->states) - 1];
return $r;
}
CssParser::pushState (   $state)

Adds a new state onto the state stack.

Parameters
integer$stateState to add onto the state stack.
Returns
integer The index of the added state in the state stacks

Definition at line 1725 of file cssmin.php.

{
$r = array_push($this->states, $state);
$this->state = $this->states[count($this->states) - 1];
return $r;
}
CssParser::setBuffer (   $buffer)

Sets/restores the buffer.

Parameters
string$bufferBuffer to set
Returns
void

Definition at line 1737 of file cssmin.php.

{
$this->buffer = $buffer;
}
CssParser::setExclusive (   $exclusive)

Set the exclusive state.

Parameters
string$exclusiveExclusive state
Returns
void

Definition at line 1747 of file cssmin.php.

{
$this->stateExclusive = $exclusive;
}
CssParser::setMediaTypes ( array  $mediaTypes)

Set the media types state.

Parameters
array$mediaTypesMedia types state
Returns
void

Definition at line 1757 of file cssmin.php.

{
$this->stateMediaTypes = $mediaTypes;
}
CssParser::setState (   $state)

Sets the current state in the state stack; equals to CssParser::popState() + CssParser::pushState().

Parameters
integer$stateState to set
Returns
integer

Definition at line 1767 of file cssmin.php.

{
$r = array_pop($this->states);
array_push($this->states, $state);
$this->state = $this->states[count($this->states) - 1];
return $r;
}
CssParser::unsetExclusive ( )

Removes the exclusive state.

Returns
void

Definition at line 1779 of file cssmin.php.

{
$this->stateExclusive = false;
}
CssParser::unsetMediaTypes ( )

Removes the media types state.

Returns
void

Definition at line 1788 of file cssmin.php.

{
$this->stateMediaTypes = false;
}

The documentation for this class was generated from the following file: