00001 <?php 00002 00010 class HTMLPurifier_Generator 00011 { 00012 00016 private $_xhtml = true; 00017 00021 private $_scriptFix = false; 00022 00027 private $_def; 00028 00032 private $_sortAttr; 00033 00037 protected $config; 00038 00043 public function __construct($config, $context) { 00044 $this->config = $config; 00045 $this->_scriptFix = $config->get('Output.CommentScriptContents'); 00046 $this->_sortAttr = $config->get('Output.SortAttr'); 00047 $this->_def = $config->getHTMLDefinition(); 00048 $this->_xhtml = $this->_def->doctype->xml; 00049 } 00050 00057 public function generateFromTokens($tokens) { 00058 if (!$tokens) return ''; 00059 00060 // Basic algorithm 00061 $html = ''; 00062 for ($i = 0, $size = count($tokens); $i < $size; $i++) { 00063 if ($this->_scriptFix && $tokens[$i]->name === 'script' 00064 && $i + 2 < $size && $tokens[$i+2] instanceof HTMLPurifier_Token_End) { 00065 // script special case 00066 // the contents of the script block must be ONE token 00067 // for this to work. 00068 $html .= $this->generateFromToken($tokens[$i++]); 00069 $html .= $this->generateScriptFromToken($tokens[$i++]); 00070 } 00071 $html .= $this->generateFromToken($tokens[$i]); 00072 } 00073 00074 // Tidy cleanup 00075 if (extension_loaded('tidy') && $this->config->get('Output.TidyFormat')) { 00076 $tidy = new Tidy; 00077 $tidy->parseString($html, array( 00078 'indent'=> true, 00079 'output-xhtml' => $this->_xhtml, 00080 'show-body-only' => true, 00081 'indent-spaces' => 2, 00082 'wrap' => 68, 00083 ), 'utf8'); 00084 $tidy->cleanRepair(); 00085 $html = (string) $tidy; // explicit cast necessary 00086 } 00087 00088 // Normalize newlines to system defined value 00089 $nl = $this->config->get('Output.Newline'); 00090 if ($nl === null) $nl = PHP_EOL; 00091 if ($nl !== "\n") $html = str_replace("\n", $nl, $html); 00092 return $html; 00093 } 00094 00100 public function generateFromToken($token) { 00101 if (!$token instanceof HTMLPurifier_Token) { 00102 trigger_error('Cannot generate HTML from non-HTMLPurifier_Token object', E_USER_WARNING); 00103 return ''; 00104 00105 } elseif ($token instanceof HTMLPurifier_Token_Start) { 00106 $attr = $this->generateAttributes($token->attr, $token->name); 00107 return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>'; 00108 00109 } elseif ($token instanceof HTMLPurifier_Token_End) { 00110 return '</' . $token->name . '>'; 00111 00112 } elseif ($token instanceof HTMLPurifier_Token_Empty) { 00113 $attr = $this->generateAttributes($token->attr, $token->name); 00114 return '<' . $token->name . ($attr ? ' ' : '') . $attr . 00115 ( $this->_xhtml ? ' /': '' ) // <br /> v. <br> 00116 . '>'; 00117 00118 } elseif ($token instanceof HTMLPurifier_Token_Text) { 00119 return $this->escape($token->data, ENT_NOQUOTES); 00120 00121 } elseif ($token instanceof HTMLPurifier_Token_Comment) { 00122 return '<!--' . $token->data . '-->'; 00123 } else { 00124 return ''; 00125 00126 } 00127 } 00128 00134 public function generateScriptFromToken($token) { 00135 if (!$token instanceof HTMLPurifier_Token_Text) return $this->generateFromToken($token); 00136 // Thanks <http://lachy.id.au/log/2005/05/script-comments> 00137 $data = preg_replace('#//\s*$#', '', $token->data); 00138 return '<!--//--><![CDATA[//><!--' . "\n" . trim($data) . "\n" . '//--><!]]>'; 00139 } 00140 00149 public function generateAttributes($assoc_array_of_attributes, $element = false) { 00150 $html = ''; 00151 if ($this->_sortAttr) ksort($assoc_array_of_attributes); 00152 foreach ($assoc_array_of_attributes as $key => $value) { 00153 if (!$this->_xhtml) { 00154 // Remove namespaced attributes 00155 if (strpos($key, ':') !== false) continue; 00156 // Check if we should minimize the attribute: val="val" -> val 00157 if ($element && !empty($this->_def->info[$element]->attr[$key]->minimized)) { 00158 $html .= $key . ' '; 00159 continue; 00160 } 00161 } 00162 $html .= $key.'="'.$this->escape($value).'" '; 00163 } 00164 return rtrim($html); 00165 } 00166 00177 public function escape($string, $quote = ENT_COMPAT) { 00178 return htmlspecialchars($string, $quote, 'UTF-8'); 00179 } 00180 00181 } 00182 00183 // vim: et sw=4 sts=4
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
