Html.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 $_markups = 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. * Set the default filter
  330. *
  331. * @return void
  332. */
  333. public function setDefaultFilter(Zend_Filter_Interface $filter = null)
  334. {
  335. if (null === $filter) {
  336. $this->_defaultFilter = new Zend_Filter();
  337. $this->_defaultFilter->addFilter(new Zend_Filter_HtmlEntities());
  338. $this->_defaultFilter->addFilter(new Zend_Filter_Callback('nl2br'));
  339. } else {
  340. $this->_defaultFilter = $filter;
  341. }
  342. }
  343. /**
  344. * Execute a replace token
  345. *
  346. * @param Zend_Markup_Token $token
  347. * @param array $tag
  348. * @return string
  349. */
  350. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  351. {
  352. if (isset($tag['tag'])) {
  353. if (!isset($tag['attributes'])) {
  354. $tag['attributes'] = array();
  355. }
  356. $attrs = self::renderAttributes($token, $tag['attributes']);
  357. return "<{$tag['tag']}{$attrs}>{$this->_render($token)}</{$tag['tag']}>";
  358. }
  359. return parent::_executeReplace($token, $tag);
  360. }
  361. /**
  362. * Execute a single replace token
  363. *
  364. * @param Zend_Markup_Token $token
  365. * @param array $tag
  366. * @return string
  367. */
  368. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  369. {
  370. if (isset($tag['tag'])) {
  371. if (!isset($tag['attributes'])) {
  372. $tag['attributes'] = array();
  373. }
  374. $attrs = self::renderAttributes($token, $tag['attributes']);
  375. return "<{$tag['tag']}{$attrs} />";
  376. }
  377. return parent::_executeSingleReplace($token, $tag);
  378. }
  379. /**
  380. * Render some attributes
  381. *
  382. * @param Zend_Markup_Token $token
  383. * @param array $tag
  384. * @return string
  385. */
  386. public static function renderAttributes(Zend_Markup_Token $token, array $attributes = array())
  387. {
  388. $attributes = array_merge(self::$_defaultAttributes, $attributes);
  389. $return = '';
  390. $tokenAttributes = $token->getAttributes();
  391. // correct style attribute
  392. if (isset($tokenAttributes['style'])) {
  393. $tokenAttributes['style'] = trim($tokenAttributes['style']);
  394. if ($tokenAttributes['style'][strlen($tokenAttributes['style']) - 1] != ';') {
  395. $tokenAttributes['style'] .= ';';
  396. }
  397. } else {
  398. $tokenAttributes['style'] = '';
  399. }
  400. // special treathment for 'align' and 'color' attribute
  401. if (isset($tokenAttributes['align'])) {
  402. $tokenAttributes['style'] .= 'text-align: ' . $tokenAttributes['align'] . ';';
  403. unset($tokenAttributes['align']);
  404. }
  405. if (isset($tokenAttributes['color']) && self::checkColor($tokenAttributes['color'])) {
  406. $tokenAttributes['style'] .= 'color: ' . $tokenAttributes['color'] . ';';
  407. unset($tokenAttributes['color']);
  408. }
  409. /*
  410. * loop through all the available attributes, and check if there is
  411. * a value defined by the token
  412. * if there is no value defined by the token, use the default value or
  413. * don't set the attribute
  414. */
  415. foreach ($attributes as $attribute => $value) {
  416. if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) {
  417. $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute], ENT_QUOTES) . '"';
  418. } elseif (!empty($value)) {
  419. $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES) . '"';
  420. }
  421. }
  422. return $return;
  423. }
  424. /**
  425. * Check if a color is a valid HTML color
  426. *
  427. * @param string $color
  428. *
  429. * @return bool
  430. */
  431. public static function checkColor($color)
  432. {
  433. /*
  434. * aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive,
  435. * purple, red, silver, teal, white, and yellow.
  436. */
  437. $colors = array(
  438. 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime',
  439. 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal',
  440. 'white', 'yellow'
  441. );
  442. if (in_array($color, $colors)) {
  443. return true;
  444. }
  445. if (preg_match('/\#[0-9a-f]{6}/i', $color)) {
  446. return true;
  447. }
  448. return false;
  449. }
  450. }