PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/vjCommentPlugin/lib/tools/htmlpurifier/library/HTMLPurifier/Generator.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 229 lines | 124 code | 25 blank | 80 comment | 39 complexity | 69341e24127fb66dbd32d47290e41776 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Generates HTML from tokens.
  4. * @todo Refactor interface so that configuration/context is determined
  5. * upon instantiation, no need for messy generateFromTokens() calls
  6. * @todo Make some of the more internal functions protected, and have
  7. * unit tests work around that
  8. */
  9. class HTMLPurifier_Generator
  10. {
  11. /**
  12. * Whether or not generator should produce XML output
  13. */
  14. private $_xhtml = true;
  15. /**
  16. * :HACK: Whether or not generator should comment the insides of <script> tags
  17. */
  18. private $_scriptFix = false;
  19. /**
  20. * Cache of HTMLDefinition during HTML output to determine whether or
  21. * not attributes should be minimized.
  22. */
  23. private $_def;
  24. /**
  25. * Cache of %Output.SortAttr
  26. */
  27. private $_sortAttr;
  28. /**
  29. * Cache of %Output.FlashCompat
  30. */
  31. private $_flashCompat;
  32. /**
  33. * Stack for keeping track of object information when outputting IE
  34. * compatibility code.
  35. */
  36. private $_flashStack = array();
  37. /**
  38. * Configuration for the generator
  39. */
  40. protected $config;
  41. /**
  42. * @param $config Instance of HTMLPurifier_Config
  43. * @param $context Instance of HTMLPurifier_Context
  44. */
  45. public function __construct($config, $context) {
  46. $this->config = $config;
  47. $this->_scriptFix = $config->get('Output.CommentScriptContents');
  48. $this->_sortAttr = $config->get('Output.SortAttr');
  49. $this->_flashCompat = $config->get('Output.FlashCompat');
  50. $this->_def = $config->getHTMLDefinition();
  51. $this->_xhtml = $this->_def->doctype->xml;
  52. }
  53. /**
  54. * Generates HTML from an array of tokens.
  55. * @param $tokens Array of HTMLPurifier_Token
  56. * @param $config HTMLPurifier_Config object
  57. * @return Generated HTML
  58. */
  59. public function generateFromTokens($tokens) {
  60. if (!$tokens) return '';
  61. // Basic algorithm
  62. $html = '';
  63. for ($i = 0, $size = count($tokens); $i < $size; $i++) {
  64. if ($this->_scriptFix && $tokens[$i]->name === 'script'
  65. && $i + 2 < $size && $tokens[$i+2] instanceof HTMLPurifier_Token_End) {
  66. // script special case
  67. // the contents of the script block must be ONE token
  68. // for this to work.
  69. $html .= $this->generateFromToken($tokens[$i++]);
  70. $html .= $this->generateScriptFromToken($tokens[$i++]);
  71. }
  72. $html .= $this->generateFromToken($tokens[$i]);
  73. }
  74. // Tidy cleanup
  75. if (extension_loaded('tidy') && $this->config->get('Output.TidyFormat')) {
  76. $tidy = new Tidy;
  77. $tidy->parseString($html, array(
  78. 'indent'=> true,
  79. 'output-xhtml' => $this->_xhtml,
  80. 'show-body-only' => true,
  81. 'indent-spaces' => 2,
  82. 'wrap' => 68,
  83. ), 'utf8');
  84. $tidy->cleanRepair();
  85. $html = (string) $tidy; // explicit cast necessary
  86. }
  87. // Normalize newlines to system defined value
  88. if ($this->config->get('Core.NormalizeNewlines')) {
  89. $nl = $this->config->get('Output.Newline');
  90. if ($nl === null) $nl = PHP_EOL;
  91. if ($nl !== "\n") $html = str_replace("\n", $nl, $html);
  92. }
  93. return $html;
  94. }
  95. /**
  96. * Generates HTML from a single token.
  97. * @param $token HTMLPurifier_Token object.
  98. * @return Generated HTML
  99. */
  100. public function generateFromToken($token) {
  101. if (!$token instanceof HTMLPurifier_Token) {
  102. trigger_error('Cannot generate HTML from non-HTMLPurifier_Token object', E_USER_WARNING);
  103. return '';
  104. } elseif ($token instanceof HTMLPurifier_Token_Start) {
  105. $attr = $this->generateAttributes($token->attr, $token->name);
  106. if ($this->_flashCompat) {
  107. if ($token->name == "object") {
  108. $flash = new stdclass();
  109. $flash->attr = $token->attr;
  110. $flash->param = array();
  111. $this->_flashStack[] = $flash;
  112. }
  113. }
  114. return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
  115. } elseif ($token instanceof HTMLPurifier_Token_End) {
  116. $_extra = '';
  117. if ($this->_flashCompat) {
  118. if ($token->name == "object" && !empty($this->_flashStack)) {
  119. $flash = array_pop($this->_flashStack);
  120. $compat_token = new HTMLPurifier_Token_Empty("embed");
  121. foreach ($flash->attr as $name => $val) {
  122. if ($name == "classid") continue;
  123. if ($name == "type") continue;
  124. if ($name == "data") $name = "src";
  125. $compat_token->attr[$name] = $val;
  126. }
  127. foreach ($flash->param as $name => $val) {
  128. if ($name == "movie") $name = "src";
  129. $compat_token->attr[$name] = $val;
  130. }
  131. $_extra = "<!--[if IE]>".$this->generateFromToken($compat_token)."<![endif]-->";
  132. }
  133. }
  134. return $_extra . '</' . $token->name . '>';
  135. } elseif ($token instanceof HTMLPurifier_Token_Empty) {
  136. if ($this->_flashCompat && $token->name == "param" && !empty($this->_flashStack)) {
  137. $this->_flashStack[count($this->_flashStack)-1]->param[$token->attr['name']] = $token->attr['value'];
  138. }
  139. $attr = $this->generateAttributes($token->attr, $token->name);
  140. return '<' . $token->name . ($attr ? ' ' : '') . $attr .
  141. ( $this->_xhtml ? ' /': '' ) // <br /> v. <br>
  142. . '>';
  143. } elseif ($token instanceof HTMLPurifier_Token_Text) {
  144. return $this->escape($token->data, ENT_NOQUOTES);
  145. } elseif ($token instanceof HTMLPurifier_Token_Comment) {
  146. return '<!--' . $token->data . '-->';
  147. } else {
  148. return '';
  149. }
  150. }
  151. /**
  152. * Special case processor for the contents of script tags
  153. * @warning This runs into problems if there's already a literal
  154. * --> somewhere inside the script contents.
  155. */
  156. public function generateScriptFromToken($token) {
  157. if (!$token instanceof HTMLPurifier_Token_Text) return $this->generateFromToken($token);
  158. // Thanks <http://lachy.id.au/log/2005/05/script-comments>
  159. $data = preg_replace('#//\s*$#', '', $token->data);
  160. return '<!--//--><![CDATA[//><!--' . "\n" . trim($data) . "\n" . '//--><!]]>';
  161. }
  162. /**
  163. * Generates attribute declarations from attribute array.
  164. * @note This does not include the leading or trailing space.
  165. * @param $assoc_array_of_attributes Attribute array
  166. * @param $element Name of element attributes are for, used to check
  167. * attribute minimization.
  168. * @return Generate HTML fragment for insertion.
  169. */
  170. public function generateAttributes($assoc_array_of_attributes, $element = false) {
  171. $html = '';
  172. if ($this->_sortAttr) ksort($assoc_array_of_attributes);
  173. foreach ($assoc_array_of_attributes as $key => $value) {
  174. if (!$this->_xhtml) {
  175. // Remove namespaced attributes
  176. if (strpos($key, ':') !== false) continue;
  177. // Check if we should minimize the attribute: val="val" -> val
  178. if ($element && !empty($this->_def->info[$element]->attr[$key]->minimized)) {
  179. $html .= $key . ' ';
  180. continue;
  181. }
  182. }
  183. $html .= $key.'="'.$this->escape($value).'" ';
  184. }
  185. return rtrim($html);
  186. }
  187. /**
  188. * Escapes raw text data.
  189. * @todo This really ought to be protected, but until we have a facility
  190. * for properly generating HTML here w/o using tokens, it stays
  191. * public.
  192. * @param $string String data to escape for HTML.
  193. * @param $quote Quoting style, like htmlspecialchars. ENT_NOQUOTES is
  194. * permissible for non-attribute output.
  195. * @return String escaped data.
  196. */
  197. public function escape($string, $quote = null) {
  198. // Workaround for APC bug on Mac Leopard reported by sidepodcast
  199. // http://htmlpurifier.org/phorum/read.php?3,4823,4846
  200. if ($quote === null) $quote = ENT_COMPAT;
  201. return htmlspecialchars($string, $quote, 'UTF-8');
  202. }
  203. }
  204. // vim: et sw=4 sts=4