ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
HTMLPurifier_Generator Class Reference

Generates HTML from tokens. More...

+ Collaboration diagram for HTMLPurifier_Generator:

Public Member Functions

 __construct ($config, $context)
 generateFromTokens ($tokens)
 Generates HTML from an array of tokens.
 generateFromToken ($token)
 Generates HTML from a single token.
 generateScriptFromToken ($token)
 Special case processor for the contents of script tags.
 generateAttributes ($assoc_array_of_attributes, $element=false)
 Generates attribute declarations from attribute array.
 escape ($string, $quote=ENT_COMPAT)
 Escapes raw text data.

Protected Attributes

 $config
 Configuration for the generator.

Private Attributes

 $_xhtml = true
 Whether or not generator should produce XML output.
 $_scriptFix = false
 :HACK: Whether or not generator should comment the insides of <script> tags
 $_def
 Cache of HTMLDefinition during HTML output to determine whether or not attributes should be minimized.
 $_sortAttr
 Cache of Output.SortAttr.

Detailed Description

Generates HTML from tokens.

Todo:

Refactor interface so that configuration/context is determined upon instantiation, no need for messy generateFromTokens() calls

Make some of the more internal functions protected, and have unit tests work around that

Definition at line 10 of file Generator.php.

Constructor & Destructor Documentation

HTMLPurifier_Generator::__construct (   $config,
  $context 
)
Parameters
$configInstance of HTMLPurifier_Config
$contextInstance of HTMLPurifier_Context

Definition at line 43 of file Generator.php.

References $config.

{
$this->config = $config;
$this->_scriptFix = $config->get('Output.CommentScriptContents');
$this->_sortAttr = $config->get('Output.SortAttr');
$this->_def = $config->getHTMLDefinition();
$this->_xhtml = $this->_def->doctype->xml;
}

Member Function Documentation

HTMLPurifier_Generator::escape (   $string,
  $quote = ENT_COMPAT 
)

Escapes raw text data.

Todo:
This really ought to be protected, but until we have a facility for properly generating HTML here w/o using tokens, it stays public.
Parameters
$stringString data to escape for HTML.
$quoteQuoting style, like htmlspecialchars. ENT_NOQUOTES is permissible for non-attribute output.
Returns
String escaped data.

Definition at line 177 of file Generator.php.

Referenced by generateAttributes(), and generateFromToken().

{
return htmlspecialchars($string, $quote, 'UTF-8');
}

+ Here is the caller graph for this function:

HTMLPurifier_Generator::generateAttributes (   $assoc_array_of_attributes,
  $element = false 
)

Generates attribute declarations from attribute array.

Note
This does not include the leading or trailing space.
Parameters
$assoc_array_of_attributesAttribute array
$elementName of element attributes are for, used to check attribute minimization.
Returns
Generate HTML fragment for insertion.

Definition at line 149 of file Generator.php.

References $key, and escape().

Referenced by generateFromToken().

{
$html = '';
if ($this->_sortAttr) ksort($assoc_array_of_attributes);
foreach ($assoc_array_of_attributes as $key => $value) {
if (!$this->_xhtml) {
// Remove namespaced attributes
if (strpos($key, ':') !== false) continue;
// Check if we should minimize the attribute: val="val" -> val
if ($element && !empty($this->_def->info[$element]->attr[$key]->minimized)) {
$html .= $key . ' ';
continue;
}
}
$html .= $key.'="'.$this->escape($value).'" ';
}
return rtrim($html);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

HTMLPurifier_Generator::generateFromToken (   $token)

Generates HTML from a single token.

Parameters
$tokenHTMLPurifier_Token object.
Returns
Generated HTML

Definition at line 100 of file Generator.php.

References elseif(), escape(), and generateAttributes().

Referenced by generateFromTokens(), and generateScriptFromToken().

{
if (!$token instanceof HTMLPurifier_Token) {
trigger_error('Cannot generate HTML from non-HTMLPurifier_Token object', E_USER_WARNING);
return '';
} elseif ($token instanceof HTMLPurifier_Token_Start) {
$attr = $this->generateAttributes($token->attr, $token->name);
return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
} elseif ($token instanceof HTMLPurifier_Token_End) {
return '</' . $token->name . '>';
} elseif ($token instanceof HTMLPurifier_Token_Empty) {
$attr = $this->generateAttributes($token->attr, $token->name);
return '<' . $token->name . ($attr ? ' ' : '') . $attr .
( $this->_xhtml ? ' /': '' ) // <br /> v. <br>
. '>';
} elseif ($token instanceof HTMLPurifier_Token_Text) {
return $this->escape($token->data, ENT_NOQUOTES);
} elseif ($token instanceof HTMLPurifier_Token_Comment) {
return '<!--' . $token->data . '-->';
} else {
return '';
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

HTMLPurifier_Generator::generateFromTokens (   $tokens)

Generates HTML from an array of tokens.

Parameters
$tokensArray of HTMLPurifier_Token
$configHTMLPurifier_Config object
Returns
Generated HTML

Definition at line 57 of file Generator.php.

References $size, $tidy, generateFromToken(), and generateScriptFromToken().

{
if (!$tokens) return '';
// Basic algorithm
$html = '';
for ($i = 0, $size = count($tokens); $i < $size; $i++) {
if ($this->_scriptFix && $tokens[$i]->name === 'script'
&& $i + 2 < $size && $tokens[$i+2] instanceof HTMLPurifier_Token_End) {
// script special case
// the contents of the script block must be ONE token
// for this to work.
$html .= $this->generateFromToken($tokens[$i++]);
$html .= $this->generateScriptFromToken($tokens[$i++]);
}
$html .= $this->generateFromToken($tokens[$i]);
}
// Tidy cleanup
if (extension_loaded('tidy') && $this->config->get('Output.TidyFormat')) {
$tidy = new Tidy;
$tidy->parseString($html, array(
'indent'=> true,
'output-xhtml' => $this->_xhtml,
'show-body-only' => true,
'indent-spaces' => 2,
'wrap' => 68,
), 'utf8');
$tidy->cleanRepair();
$html = (string) $tidy; // explicit cast necessary
}
// Normalize newlines to system defined value
$nl = $this->config->get('Output.Newline');
if ($nl === null) $nl = PHP_EOL;
if ($nl !== "\n") $html = str_replace("\n", $nl, $html);
return $html;
}

+ Here is the call graph for this function:

HTMLPurifier_Generator::generateScriptFromToken (   $token)

Special case processor for the contents of script tags.

Warning
This runs into problems if there's already a literal –> somewhere inside the script contents.

Definition at line 134 of file Generator.php.

References $data, and generateFromToken().

Referenced by generateFromTokens().

{
if (!$token instanceof HTMLPurifier_Token_Text) return $this->generateFromToken($token);
// Thanks <http://lachy.id.au/log/2005/05/script-comments>
$data = preg_replace('#//\s*$#', '', $token->data);
return '<!--//--><![CDATA[//><!--' . "\n" . trim($data) . "\n" . '//--><!]]>';
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

HTMLPurifier_Generator::$_def
private

Cache of HTMLDefinition during HTML output to determine whether or not attributes should be minimized.

Definition at line 27 of file Generator.php.

HTMLPurifier_Generator::$_scriptFix = false
private

:HACK: Whether or not generator should comment the insides of <script> tags

Definition at line 21 of file Generator.php.

HTMLPurifier_Generator::$_sortAttr
private

Cache of Output.SortAttr.

Definition at line 32 of file Generator.php.

HTMLPurifier_Generator::$_xhtml = true
private

Whether or not generator should produce XML output.

Definition at line 16 of file Generator.php.

HTMLPurifier_Generator::$config
protected

Configuration for the generator.

Definition at line 37 of file Generator.php.

Referenced by __construct().


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