ILIAS  release_4-4 Revision
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. More...
 
 generateFromToken ($token)
 Generates HTML from a single token. More...
 
 generateScriptFromToken ($token)
 Special case processor for the contents of script tags. More...
 
 generateAttributes ($assoc_array_of_attributes, $element=false)
 Generates attribute declarations from attribute array. More...
 
 escape ($string, $quote=null)
 Escapes raw text data. More...
 

Protected Attributes

 $config
 Configuration for the generator. More...
 

Private Attributes

 $_xhtml = true
 Whether or not generator should produce XML output. More...
 
 $_scriptFix = false
 :HACK: Whether or not generator should comment the insides of <script> tags More...
 
 $_def
 Cache of HTMLDefinition during HTML output to determine whether or not attributes should be minimized. More...
 
 $_sortAttr
 Cache of Output.SortAttr. More...
 
 $_flashCompat
 Cache of Output.FlashCompat. More...
 
 $_innerHTMLFix
 Cache of Output.FixInnerHTML. More...
 
 $_flashStack = array()
 Stack for keeping track of object information when outputting IE compatibility code. More...
 

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

◆ __construct()

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

Definition at line 59 of file Generator.php.

References $config.

59  {
60  $this->config = $config;
61  $this->_scriptFix = $config->get('Output.CommentScriptContents');
62  $this->_innerHTMLFix = $config->get('Output.FixInnerHTML');
63  $this->_sortAttr = $config->get('Output.SortAttr');
64  $this->_flashCompat = $config->get('Output.FlashCompat');
65  $this->_def = $config->getHTMLDefinition();
66  $this->_xhtml = $this->_def->doctype->xml;
67  }
$config
Configuration for the generator.
Definition: Generator.php:53

Member Function Documentation

◆ escape()

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

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 245 of file Generator.php.

Referenced by generateAttributes(), and generateFromToken().

245  {
246  // Workaround for APC bug on Mac Leopard reported by sidepodcast
247  // http://htmlpurifier.org/phorum/read.php?3,4823,4846
248  if ($quote === null) $quote = ENT_COMPAT;
249  return htmlspecialchars($string, $quote, 'UTF-8');
250  }
+ Here is the caller graph for this function:

◆ generateAttributes()

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 186 of file Generator.php.

References escape().

Referenced by generateFromToken().

186  {
187  $html = '';
188  if ($this->_sortAttr) ksort($assoc_array_of_attributes);
189  foreach ($assoc_array_of_attributes as $key => $value) {
190  if (!$this->_xhtml) {
191  // Remove namespaced attributes
192  if (strpos($key, ':') !== false) continue;
193  // Check if we should minimize the attribute: val="val" -> val
194  if ($element && !empty($this->_def->info[$element]->attr[$key]->minimized)) {
195  $html .= $key . ' ';
196  continue;
197  }
198  }
199  // Workaround for Internet Explorer innerHTML bug.
200  // Essentially, Internet Explorer, when calculating
201  // innerHTML, omits quotes if there are no instances of
202  // angled brackets, quotes or spaces. However, when parsing
203  // HTML (for example, when you assign to innerHTML), it
204  // treats backticks as quotes. Thus,
205  // <img alt="``" />
206  // becomes
207  // <img alt=`` />
208  // becomes
209  // <img alt='' />
210  // Fortunately, all we need to do is trigger an appropriate
211  // quoting style, which we do by adding an extra space.
212  // This also is consistent with the W3C spec, which states
213  // that user agents may ignore leading or trailing
214  // whitespace (in fact, most don't, at least for attributes
215  // like alt, but an extra space at the end is barely
216  // noticeable). Still, we have a configuration knob for
217  // this, since this transformation is not necesary if you
218  // don't process user input with innerHTML or you don't plan
219  // on supporting Internet Explorer.
220  if ($this->_innerHTMLFix) {
221  if (strpos($value, '`') !== false) {
222  // check if correct quoting style would not already be
223  // triggered
224  if (strcspn($value, '"\' <>') === strlen($value)) {
225  // protect!
226  $value .= ' ';
227  }
228  }
229  }
230  $html .= $key.'="'.$this->escape($value).'" ';
231  }
232  return rtrim($html);
233  }
escape($string, $quote=null)
Escapes raw text data.
Definition: Generator.php:245
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateFromToken()

HTMLPurifier_Generator::generateFromToken (   $token)

Generates HTML from a single token.

Parameters
$tokenHTMLPurifier_Token object.
Returns
Generated HTML

Definition at line 120 of file Generator.php.

References escape(), and generateAttributes().

Referenced by generateFromTokens(), and generateScriptFromToken().

120  {
121  if (!$token instanceof HTMLPurifier_Token) {
122  trigger_error('Cannot generate HTML from non-HTMLPurifier_Token object', E_USER_WARNING);
123  return '';
124 
125  } elseif ($token instanceof HTMLPurifier_Token_Start) {
126  $attr = $this->generateAttributes($token->attr, $token->name);
127  if ($this->_flashCompat) {
128  if ($token->name == "object") {
129  $flash = new stdclass();
130  $flash->attr = $token->attr;
131  $flash->param = array();
132  $this->_flashStack[] = $flash;
133  }
134  }
135  return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
136 
137  } elseif ($token instanceof HTMLPurifier_Token_End) {
138  $_extra = '';
139  if ($this->_flashCompat) {
140  if ($token->name == "object" && !empty($this->_flashStack)) {
141  // doesn't do anything for now
142  }
143  }
144  return $_extra . '</' . $token->name . '>';
145 
146  } elseif ($token instanceof HTMLPurifier_Token_Empty) {
147  if ($this->_flashCompat && $token->name == "param" && !empty($this->_flashStack)) {
148  $this->_flashStack[count($this->_flashStack)-1]->param[$token->attr['name']] = $token->attr['value'];
149  }
150  $attr = $this->generateAttributes($token->attr, $token->name);
151  return '<' . $token->name . ($attr ? ' ' : '') . $attr .
152  ( $this->_xhtml ? ' /': '' ) // <br /> v. <br>
153  . '>';
154 
155  } elseif ($token instanceof HTMLPurifier_Token_Text) {
156  return $this->escape($token->data, ENT_NOQUOTES);
157 
158  } elseif ($token instanceof HTMLPurifier_Token_Comment) {
159  return '<!--' . $token->data . '-->';
160  } else {
161  return '';
162 
163  }
164  }
Concrete end token class.
Definition: End.php:10
Concrete start token class.
Definition: Start.php:6
generateAttributes($assoc_array_of_attributes, $element=false)
Generates attribute declarations from attribute array.
Definition: Generator.php:186
Abstract base token class that all others inherit from.
Definition: Token.php:6
Concrete empty token class.
Definition: Empty.php:6
escape($string, $quote=null)
Escapes raw text data.
Definition: Generator.php:245
Concrete text token class.
Definition: Text.php:12
Concrete comment token class.
Definition: Comment.php:6
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateFromTokens()

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 75 of file Generator.php.

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

75  {
76  if (!$tokens) return '';
77 
78  // Basic algorithm
79  $html = '';
80  for ($i = 0, $size = count($tokens); $i < $size; $i++) {
81  if ($this->_scriptFix && $tokens[$i]->name === 'script'
82  && $i + 2 < $size && $tokens[$i+2] instanceof HTMLPurifier_Token_End) {
83  // script special case
84  // the contents of the script block must be ONE token
85  // for this to work.
86  $html .= $this->generateFromToken($tokens[$i++]);
87  $html .= $this->generateScriptFromToken($tokens[$i++]);
88  }
89  $html .= $this->generateFromToken($tokens[$i]);
90  }
91 
92  // Tidy cleanup
93  if (extension_loaded('tidy') && $this->config->get('Output.TidyFormat')) {
94  $tidy = new Tidy;
95  $tidy->parseString($html, array(
96  'indent'=> true,
97  'output-xhtml' => $this->_xhtml,
98  'show-body-only' => true,
99  'indent-spaces' => 2,
100  'wrap' => 68,
101  ), 'utf8');
102  $tidy->cleanRepair();
103  $html = (string) $tidy; // explicit cast necessary
104  }
105 
106  // Normalize newlines to system defined value
107  if ($this->config->get('Core.NormalizeNewlines')) {
108  $nl = $this->config->get('Output.Newline');
109  if ($nl === null) $nl = PHP_EOL;
110  if ($nl !== "\n") $html = str_replace("\n", $nl, $html);
111  }
112  return $html;
113  }
Concrete end token class.
Definition: End.php:10
$size
Definition: RandomTest.php:79
generateScriptFromToken($token)
Special case processor for the contents of script tags.
Definition: Generator.php:171
generateFromToken($token)
Generates HTML from a single token.
Definition: Generator.php:120
+ Here is the call graph for this function:

◆ generateScriptFromToken()

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 171 of file Generator.php.

References $data, and generateFromToken().

Referenced by generateFromTokens().

171  {
172  if (!$token instanceof HTMLPurifier_Token_Text) return $this->generateFromToken($token);
173  // Thanks <http://lachy.id.au/log/2005/05/script-comments>
174  $data = preg_replace('#//\s*$#', '', $token->data);
175  return '<!--//--><![CDATA[//><!--' . "\n" . trim($data) . "\n" . '//--><!]]>';
176  }
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data
Concrete text token class.
Definition: Text.php:12
generateFromToken($token)
Generates HTML from a single token.
Definition: Generator.php:120
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $_def

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.

◆ $_flashCompat

HTMLPurifier_Generator::$_flashCompat
private

Cache of Output.FlashCompat.

Definition at line 37 of file Generator.php.

◆ $_flashStack

HTMLPurifier_Generator::$_flashStack = array()
private

Stack for keeping track of object information when outputting IE compatibility code.

Definition at line 48 of file Generator.php.

◆ $_innerHTMLFix

HTMLPurifier_Generator::$_innerHTMLFix
private

Cache of Output.FixInnerHTML.

Definition at line 42 of file Generator.php.

◆ $_scriptFix

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.

◆ $_sortAttr

HTMLPurifier_Generator::$_sortAttr
private

Cache of Output.SortAttr.

Definition at line 32 of file Generator.php.

◆ $_xhtml

HTMLPurifier_Generator::$_xhtml = true
private

Whether or not generator should produce XML output.

Definition at line 16 of file Generator.php.

◆ $config

HTMLPurifier_Generator::$config
protected

Configuration for the generator.

Definition at line 53 of file Generator.php.

Referenced by __construct().


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