Html.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Markup
  17. * @subpackage Renderer
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Uri
  24. */
  25. require_once 'Zend/Uri.php';
  26. /**
  27. * @see Zend_Filter_HtmlEntities
  28. */
  29. require_once 'Zend/Filter/HtmlEntities.php';
  30. /**
  31. * @see Zend_Filter_Callback
  32. */
  33. require_once 'Zend/Filter/Callback.php';
  34. /**
  35. * @see Zend_Markup_Renderer_RendererAbstract
  36. */
  37. require_once 'Zend/Markup/Renderer/RendererAbstract.php';
  38. /**
  39. * HTML renderer
  40. *
  41. * @category Zend
  42. * @package Zend_Markup
  43. * @subpackage Renderer
  44. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Markup_Renderer_Html extends Zend_Markup_Renderer_RendererAbstract
  48. {
  49. /**
  50. * Tag info
  51. *
  52. * @var array
  53. */
  54. protected $_tags = array(
  55. 'b' => array(
  56. 'type' => 10, // self::TYPE_REPLACE | self::TAG_NORMAL
  57. 'tag' => 'strong',
  58. 'group' => 'inline',
  59. 'filter' => true,
  60. ),
  61. 'u' => array(
  62. 'type' => 10,
  63. 'tag' => 'span',
  64. 'attributes' => array(
  65. 'style' => 'text-decoration: underline;',
  66. ),
  67. 'group' => 'inline',
  68. 'filter' => true,
  69. ),
  70. 'i' => array(
  71. 'type' => 10,
  72. 'tag' => 'em',
  73. 'group' => 'inline',
  74. 'filter' => true,
  75. ),
  76. 'cite' => array(
  77. 'type' => 10,
  78. 'tag' => 'cite',
  79. 'group' => 'inline',
  80. 'filter' => true,
  81. ),
  82. 'del' => array(
  83. 'type' => 10,
  84. 'tag' => 'del',
  85. 'group' => 'inline',
  86. 'filter' => true,
  87. ),
  88. 'ins' => array(
  89. 'type' => 10,
  90. 'tag' => 'ins',
  91. 'group' => 'inline',
  92. 'filter' => true,
  93. ),
  94. 'sub' => array(
  95. 'type' => 10,
  96. 'tag' => 'sub',
  97. 'group' => 'inline',
  98. 'filter' => true,
  99. ),
  100. 'sup' => array(
  101. 'type' => 10,
  102. 'tag' => 'sup',
  103. 'group' => 'inline',
  104. 'filter' => true,
  105. ),
  106. 'span' => array(
  107. 'type' => 10,
  108. 'tag' => 'span',
  109. 'group' => 'inline',
  110. 'filter' => true,
  111. ),
  112. 'acronym' => array(
  113. 'type' => 10,
  114. 'tag' => 'acronym',
  115. 'group' => 'inline',
  116. 'filter' => true,
  117. ),
  118. // headings
  119. 'h1' => array(
  120. 'type' => 10,
  121. 'tag' => 'h1',
  122. 'group' => 'inline',
  123. 'filter' => false,
  124. ),
  125. 'h2' => array(
  126. 'type' => 10,
  127. 'tag' => 'h2',
  128. 'group' => 'inline',
  129. 'filter' => false,
  130. ),
  131. 'h3' => array(
  132. 'type' => 10,
  133. 'tag' => 'h3',
  134. 'group' => 'inline',
  135. 'filter' => false,
  136. ),
  137. 'h4' => array(
  138. 'type' => 10,
  139. 'tag' => 'h4',
  140. 'group' => 'inline',
  141. 'filter' => false,
  142. ),
  143. 'h5' => array(
  144. 'type' => 10,
  145. 'tag' => 'h5',
  146. 'group' => 'inline',
  147. 'filter' => false,
  148. ),
  149. 'h6' => array(
  150. 'type' => 10,
  151. 'tag' => 'h6',
  152. 'group' => 'inline',
  153. 'filter' => false,
  154. ),
  155. // callback tags
  156. 'url' => array(
  157. 'type' => 6, // self::TYPE_CALLBACK | self::TAG_NORMAL
  158. 'callback' => null,
  159. 'group' => 'inline',
  160. 'filter' => true,
  161. ),
  162. 'img' => array(
  163. 'type' => 6,
  164. 'callback' => null,
  165. 'group' => 'inline-empty',
  166. 'filter' => true,
  167. ),
  168. 'code' => array(
  169. 'type' => 6,
  170. 'callback' => null,
  171. 'group' => 'block-empty',
  172. 'filter' => false,
  173. ),
  174. 'p' => array(
  175. 'type' => 10,
  176. 'tag' => 'p',
  177. 'group' => 'block',
  178. 'filter' => true,
  179. ),
  180. 'ignore' => array(
  181. 'type' => 10,
  182. 'start' => '',
  183. 'end' => '',
  184. 'group' => 'block-empty',
  185. 'filter' => true,
  186. ),
  187. 'quote' => array(
  188. 'type' => 10,
  189. 'tag' => 'blockquote',
  190. 'group' => 'block',
  191. 'filter' => true,
  192. ),
  193. 'list' => array(
  194. 'type' => 6,
  195. 'callback' => null,
  196. 'group' => 'list',
  197. 'filter' => false,
  198. ),
  199. '*' => array(
  200. 'type' => 10,
  201. 'tag' => 'li',
  202. 'group' => 'list-item',
  203. 'filter' => false,
  204. ),
  205. 'hr' => array(
  206. 'type' => 9, // self::TYPE_REPLACE | self::TAG_SINGLE
  207. 'tag' => 'hr',
  208. 'group' => 'block',
  209. ),
  210. // aliases
  211. 'bold' => array(
  212. 'type' => 16,
  213. 'name' => 'b',
  214. ),
  215. 'strong' => array(
  216. 'type' => 16,
  217. 'name' => 'b',
  218. ),
  219. 'italic' => array(
  220. 'type' => 16,
  221. 'name' => 'i',
  222. ),
  223. 'em' => array(
  224. 'type' => 16,
  225. 'name' => 'i',
  226. ),
  227. 'emphasized' => array(
  228. 'type' => 16,
  229. 'name' => 'i',
  230. ),
  231. 'underline' => array(
  232. 'type' => 16,
  233. 'name' => 'u',
  234. ),
  235. 'citation' => array(
  236. 'type' => 16,
  237. 'name' => 'cite',
  238. ),
  239. 'deleted' => array(
  240. 'type' => 16,
  241. 'name' => 'del',
  242. ),
  243. 'insert' => array(
  244. 'type' => 16,
  245. 'name' => 'ins',
  246. ),
  247. 'strike' => array(
  248. 'type' => 16,
  249. 'name' => 's',
  250. ),
  251. 's' => array(
  252. 'type' => 16,
  253. 'name' => 'del',
  254. ),
  255. 'subscript' => array(
  256. 'type' => 16,
  257. 'name' => 'sub',
  258. ),
  259. 'superscript' => array(
  260. 'type' => 16,
  261. 'name' => 'sup',
  262. ),
  263. 'a' => array(
  264. 'type' => 16,
  265. 'name' => 'url',
  266. ),
  267. 'image' => array(
  268. 'type' => 16,
  269. 'name' => 'img',
  270. ),
  271. 'li' => array(
  272. 'type' => 16,
  273. 'name' => '*',
  274. ),
  275. 'color' => array(
  276. 'type' => 16,
  277. 'name' => 'span'
  278. )
  279. );
  280. /**
  281. * Element groups
  282. *
  283. * @var array
  284. */
  285. protected $_groups = array(
  286. 'block' => array('block', 'inline', 'block-empty', 'inline-empty', 'list'),
  287. 'inline' => array('inline', 'inline-empty'),
  288. 'list' => array('list-item'),
  289. 'list-item' => array('inline', 'inline-empty', 'list'),
  290. 'block-empty' => array(),
  291. 'inline-empty' => array(),
  292. );
  293. /**
  294. * The current group
  295. *
  296. * @var string
  297. */
  298. protected $_group = 'block';
  299. /**
  300. * Default attributes
  301. *
  302. * @var array
  303. */
  304. protected static $_defaultAttributes = array(
  305. 'id' => '',
  306. 'class' => '',
  307. 'style' => '',
  308. 'lang' => '',
  309. 'title' => ''
  310. );
  311. /**
  312. * Constructor
  313. *
  314. * @param array|Zend_Config $options
  315. *
  316. * @return void
  317. */
  318. public function __construct($options = array())
  319. {
  320. if ($options instanceof Zend_Config) {
  321. $options = $options->toArray();
  322. }
  323. $this->_pluginLoader = new Zend_Loader_PluginLoader(array(
  324. 'Zend_Markup_Renderer_Html' => 'Zend/Markup/Renderer/Html/'
  325. ));
  326. parent::__construct($options);
  327. }
  328. /**
  329. * Initialize the default filters
  330. *
  331. * @return void
  332. */
  333. public function addDefaultFilters()
  334. {
  335. $this->getFilterChain()->addFilter(new Zend_Filter_HtmlEntities());
  336. $this->getFilterChain()->addFilter(new Zend_Filter_Callback('nl2br'));
  337. }
  338. /**
  339. * Execute a replace token
  340. *
  341. * @param Zend_Markup_Token $token
  342. * @param array $tag
  343. * @return string
  344. */
  345. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  346. {
  347. if (isset($tag['tag'])) {
  348. if (!isset($tag['attributes'])) {
  349. $tag['attributes'] = array();
  350. }
  351. $attrs = self::renderAttributes($token, $tag['attributes']);
  352. return "<{$tag['tag']}{$attrs}>{$this->_render($token)}</{$tag['tag']}>";
  353. }
  354. return parent::_executeReplace($token, $tag);
  355. }
  356. /**
  357. * Execute a single replace token
  358. *
  359. * @param Zend_Markup_Token $token
  360. * @param array $tag
  361. * @return string
  362. */
  363. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  364. {
  365. if (isset($tag['tag'])) {
  366. if (!isset($tag['attributes'])) {
  367. $tag['attributes'] = array();
  368. }
  369. $attrs = self::renderAttributes($token, $tag['attributes']);
  370. return "<{$tag['tag']}{$attrs} />";
  371. }
  372. return parent::_executeSingleReplace($token, $tag);
  373. }
  374. /**
  375. * Render some attributes
  376. *
  377. * @param Zend_Markup_Token $token
  378. * @param array $tag
  379. * @return string
  380. */
  381. public static function renderAttributes(Zend_Markup_Token $token, array $attributes = array())
  382. {
  383. $attributes = array_merge(self::$_defaultAttributes, $attributes);
  384. $return = '';
  385. $tokenAttributes = $token->getAttributes();
  386. // correct style attribute
  387. if (isset($tokenAttributes['style'])) {
  388. $tokenAttributes['style'] = trim($tokenAttributes['style']);
  389. if ($tokenAttributes['style'][strlen($tokenAttributes['style']) - 1] != ';') {
  390. $tokenAttributes['style'] .= ';';
  391. }
  392. } else {
  393. $tokenAttributes['style'] = '';
  394. }
  395. // special treathment for 'align' and 'color' attribute
  396. if (isset($tokenAttributes['align'])) {
  397. $tokenAttributes['style'] .= 'text-align: ' . $tokenAttributes['align'] . ';';
  398. unset($tokenAttributes['align']);
  399. }
  400. if (isset($tokenAttributes['color']) && self::checkColor($tokenAttributes['color'])) {
  401. $tokenAttributes['style'] .= 'color: ' . $tokenAttributes['color'] . ';';
  402. unset($tokenAttributes['color']);
  403. }
  404. /*
  405. * loop through all the available attributes, and check if there is
  406. * a value defined by the token
  407. * if there is no value defined by the token, use the default value or
  408. * don't set the attribute
  409. */
  410. foreach ($attributes as $attribute => $value) {
  411. if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) {
  412. $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute], ENT_QUOTES) . '"';
  413. } elseif (!empty($value)) {
  414. $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES) . '"';
  415. }
  416. }
  417. return $return;
  418. }
  419. /**
  420. * Check if a color is a valid HTML color
  421. *
  422. * @param string $color
  423. *
  424. * @return bool
  425. */
  426. public static function checkColor($color)
  427. {
  428. /*
  429. * aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive,
  430. * purple, red, silver, teal, white, and yellow.
  431. */
  432. $colors = array(
  433. 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime',
  434. 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal',
  435. 'white', 'yellow'
  436. );
  437. if (in_array($color, $colors)) {
  438. return true;
  439. }
  440. if (preg_match('/\#[0-9a-f]{6}/i', $color)) {
  441. return true;
  442. }
  443. return false;
  444. }
  445. }