Html.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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_Markup_Renderer_RendererAbstract
  28. */
  29. require_once 'Zend/Markup/Renderer/RendererAbstract.php';
  30. /**
  31. * HTML renderer
  32. *
  33. * @category Zend
  34. * @package Zend_Markup
  35. * @subpackage Renderer
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Markup_Renderer_Html extends Zend_Markup_Renderer_RendererAbstract
  40. {
  41. /**
  42. * Tag info
  43. *
  44. * @var array
  45. */
  46. protected $_tags = array(
  47. 'b' => array(
  48. 'type' => 10, // self::TYPE_REPLACE | self::TAG_NORMAL
  49. 'tag' => 'strong',
  50. 'group' => 'inline',
  51. 'filter' => true,
  52. ),
  53. 'u' => array(
  54. 'type' => 10,
  55. 'tag' => 'span',
  56. 'attributes' => array(
  57. 'style' => 'text-decoration: underline;',
  58. ),
  59. 'group' => 'inline',
  60. 'filter' => true,
  61. ),
  62. 'i' => array(
  63. 'type' => 10,
  64. 'tag' => 'em',
  65. 'group' => 'inline',
  66. 'filter' => true,
  67. ),
  68. 'cite' => array(
  69. 'type' => 10,
  70. 'tag' => 'cite',
  71. 'group' => 'inline',
  72. 'filter' => true,
  73. ),
  74. 'del' => array(
  75. 'type' => 10,
  76. 'tag' => 'del',
  77. 'group' => 'inline',
  78. 'filter' => true,
  79. ),
  80. 'ins' => array(
  81. 'type' => 10,
  82. 'tag' => 'ins',
  83. 'group' => 'inline',
  84. 'filter' => true,
  85. ),
  86. 'sub' => array(
  87. 'type' => 10,
  88. 'tag' => 'sub',
  89. 'group' => 'inline',
  90. 'filter' => true,
  91. ),
  92. 'sup' => array(
  93. 'type' => 10,
  94. 'tag' => 'sup',
  95. 'group' => 'inline',
  96. 'filter' => true,
  97. ),
  98. 'span' => array(
  99. 'type' => 10,
  100. 'tag' => 'span',
  101. 'group' => 'inline',
  102. 'filter' => true,
  103. ),
  104. 'acronym' => array(
  105. 'type' => 10,
  106. 'tag' => 'acronym',
  107. 'group' => 'inline',
  108. 'filter' => true,
  109. ),
  110. // headings
  111. 'h1' => array(
  112. 'type' => 10,
  113. 'tag' => 'h1',
  114. 'group' => 'inline',
  115. 'filter' => false,
  116. ),
  117. 'h2' => array(
  118. 'type' => 10,
  119. 'tag' => 'h2',
  120. 'group' => 'inline',
  121. 'filter' => false,
  122. ),
  123. 'h3' => array(
  124. 'type' => 10,
  125. 'tag' => 'h3',
  126. 'group' => 'inline',
  127. 'filter' => false,
  128. ),
  129. 'h4' => array(
  130. 'type' => 10,
  131. 'tag' => 'h4',
  132. 'group' => 'inline',
  133. 'filter' => false,
  134. ),
  135. 'h5' => array(
  136. 'type' => 10,
  137. 'tag' => 'h5',
  138. 'group' => 'inline',
  139. 'filter' => false,
  140. ),
  141. 'h6' => array(
  142. 'type' => 10,
  143. 'tag' => 'h6',
  144. 'group' => 'inline',
  145. 'filter' => false,
  146. ),
  147. // callback tags
  148. 'url' => array(
  149. 'type' => 6, // self::TYPE_CALLBACK | self::TAG_NORMAL
  150. 'callback' => array('Zend_Markup_Renderer_Html', '_htmlUrl'),
  151. 'group' => 'inline',
  152. 'filter' => true,
  153. ),
  154. 'img' => array(
  155. 'type' => 6,
  156. 'callback' => array('Zend_Markup_Renderer_Html', '_htmlImg'),
  157. 'group' => 'inline_empty',
  158. 'filter' => true,
  159. ),
  160. 'code' => array(
  161. 'type' => 6,
  162. 'callback' => array('Zend_Markup_Renderer_Html', '_htmlCode'),
  163. 'group' => 'block_empty',
  164. 'filter' => false,
  165. ),
  166. 'p' => array(
  167. 'type' => 10,
  168. 'tag' => 'p',
  169. 'group' => 'block',
  170. 'filter' => true,
  171. ),
  172. 'ignore' => array(
  173. 'type' => 10,
  174. 'start' => '',
  175. 'end' => '',
  176. 'group' => 'block_empty',
  177. 'filter' => true,
  178. ),
  179. 'quote' => array(
  180. 'type' => 10,
  181. 'tag' => 'blockquote',
  182. 'group' => 'block',
  183. 'filter' => true,
  184. ),
  185. 'list' => array(
  186. 'type' => 6,
  187. 'callback' => array('Zend_Markup_Renderer_Html', '_htmlList'),
  188. 'group' => 'list',
  189. 'filter' => false,
  190. ),
  191. '*' => array(
  192. 'type' => 10,
  193. 'tag' => 'li',
  194. 'group' => 'list-item',
  195. 'filter' => false,
  196. ),
  197. 'hr' => array(
  198. 'type' => 9, // self::TYPE_REPLACE | self::TAG_SINGLE
  199. 'tag' => 'hr',
  200. 'group' => 'block',
  201. ),
  202. // aliases
  203. 'bold' => array(
  204. 'type' => 16,
  205. 'name' => 'b',
  206. ),
  207. 'strong' => array(
  208. 'type' => 16,
  209. 'name' => 'b',
  210. ),
  211. 'italic' => array(
  212. 'type' => 16,
  213. 'name' => 'i',
  214. ),
  215. 'em' => array(
  216. 'type' => 16,
  217. 'name' => 'i',
  218. ),
  219. 'emphasized' => array(
  220. 'type' => 16,
  221. 'name' => 'i',
  222. ),
  223. 'underline' => array(
  224. 'type' => 16,
  225. 'name' => 'u',
  226. ),
  227. 'citation' => array(
  228. 'type' => 16,
  229. 'name' => 'cite',
  230. ),
  231. 'deleted' => array(
  232. 'type' => 16,
  233. 'name' => 'del',
  234. ),
  235. 'insert' => array(
  236. 'type' => 16,
  237. 'name' => 'ins',
  238. ),
  239. 'strike' => array(
  240. 'type' => 16,
  241. 'name' => 's',
  242. ),
  243. 's' => array(
  244. 'type' => 16,
  245. 'name' => 'del',
  246. ),
  247. 'subscript' => array(
  248. 'type' => 16,
  249. 'name' => 'sub',
  250. ),
  251. 'superscript' => array(
  252. 'type' => 16,
  253. 'name' => 'sup',
  254. ),
  255. 'a' => array(
  256. 'type' => 16,
  257. 'name' => 'url',
  258. ),
  259. 'image' => array(
  260. 'type' => 16,
  261. 'name' => 'img',
  262. ),
  263. 'li' => array(
  264. 'type' => 16,
  265. 'name' => '*',
  266. ),
  267. 'color' => array(
  268. 'type' => 16,
  269. 'name' => 'span'
  270. )
  271. );
  272. /**
  273. * Element groups
  274. *
  275. * @var array
  276. */
  277. protected $_groups = array(
  278. 'block' => array('block', 'inline', 'block_empty', 'inline_empty', 'list'),
  279. 'inline' => array('inline', 'inline_empty'),
  280. 'list' => array('list-item'),
  281. 'list-item' => array('inline', 'inline_empty', 'list'),
  282. 'block_empty' => array(),
  283. 'inline_empty' => array(),
  284. );
  285. /**
  286. * The current group
  287. *
  288. * @var string
  289. */
  290. protected $_group = 'block';
  291. /**
  292. * Default attributes
  293. *
  294. * @var array
  295. */
  296. protected static $_defaultAttributes = array(
  297. 'id' => '',
  298. 'class' => '',
  299. 'style' => '',
  300. 'lang' => '',
  301. 'title' => ''
  302. );
  303. /**
  304. * Execute a replace token
  305. *
  306. * @param Zend_Markup_Token $token
  307. * @param array $tag
  308. * @return string
  309. */
  310. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  311. {
  312. if (isset($tag['tag'])) {
  313. if (!isset($tag['attributes'])) {
  314. $tag['attributes'] = array();
  315. }
  316. $attrs = self::_renderAttributes($token, $tag['attributes']);
  317. return "<{$tag['tag']}{$attrs}>{$this->_render($token)}</{$tag['tag']}>";
  318. }
  319. return parent::_executeReplace($token, $tag);
  320. }
  321. /**
  322. * Execute a single replace token
  323. *
  324. * @param Zend_Markup_Token $token
  325. * @param array $tag
  326. * @return string
  327. */
  328. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  329. {
  330. if (isset($tag['tag'])) {
  331. if (!isset($tag['attributes'])) {
  332. $tag['attributes'] = array();
  333. }
  334. $attrs = self::_renderAttributes($token, $tag['attributes']);
  335. return "<{$tag['tag']}{$attrs} />";
  336. }
  337. return parent::_executeSingleReplace($token, $tag);
  338. }
  339. /**
  340. * Render some attributes
  341. *
  342. * @param Zend_Markup_Token $token
  343. * @param array $tag
  344. * @return string
  345. */
  346. protected static function _renderAttributes(Zend_Markup_Token $token, array $attributes = array())
  347. {
  348. $attributes = array_merge(self::$_defaultAttributes, $attributes);
  349. $return = '';
  350. $tokenAttributes = $token->getAttributes();
  351. // correct style attribute
  352. if (isset($tokenAttributes['style'])) {
  353. $tokenAttributes['style'] = trim($tokenAttributes['style']);
  354. if ($tokenAttributes['style'][strlen($tokenAttributes['style']) - 1] != ';') {
  355. $tokenAttributes['style'] .= ';';
  356. }
  357. } else {
  358. $tokenAttributes['style'] = '';
  359. }
  360. // special treathment for 'align' and 'color' attribute
  361. if (isset($tokenAttributes['align'])) {
  362. $tokenAttributes['style'] .= 'text-align: ' . $tokenAttributes['align'] . ';';
  363. unset($tokenAttributes['align']);
  364. }
  365. if (isset($tokenAttributes['color']) && self::_checkColor($tokenAttributes['color'])) {
  366. $tokenAttributes['style'] .= 'color: ' . $tokenAttributes['color'] . ';';
  367. unset($tokenAttributes['color']);
  368. }
  369. /*
  370. * loop through all the available attributes, and check if there is
  371. * a value defined by the token
  372. * if there is no value defined by the token, use the default value or
  373. * don't set the attribute
  374. */
  375. foreach ($attributes as $attribute => $value) {
  376. if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) {
  377. $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute], ENT_QUOTES) . '"';
  378. } elseif (!empty($value)) {
  379. $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES) . '"';
  380. }
  381. }
  382. return $return;
  383. }
  384. /**
  385. * Method for the URL tag
  386. *
  387. * @param Zend_Markup_Token $token
  388. * @param string $text
  389. * @return string
  390. */
  391. protected static function _htmlUrl(Zend_Markup_Token $token, $text)
  392. {
  393. if ($token->hasAttribute('url')) {
  394. $url = $token->getAttribute('url');
  395. } else {
  396. $url = $text;
  397. }
  398. // check if the URL is valid
  399. if (!Zend_Uri::check($url)) {
  400. return $text;
  401. }
  402. $attributes = self::_renderAttributes($token);
  403. return "<a href=\"{$url}\"{$attributes}>{$text}</a>";
  404. }
  405. /**
  406. * Method for the img tag
  407. *
  408. * @param Zend_Markup_Token $token
  409. * @param string $text
  410. * @return string
  411. */
  412. protected static function _htmlImg(Zend_Markup_Token $token, $text)
  413. {
  414. $url = $text;
  415. // check if the URL is valid
  416. if (!Zend_Uri::check($url)) {
  417. return $text;
  418. }
  419. if ($token->hasAttribute('alt')) {
  420. $alt = $token->getAttribute('alt');
  421. } else {
  422. // try to get the alternative from the URL
  423. $alt = rtrim($text, '/');
  424. $alt = strrchr($alt, '/');
  425. if (false !== strpos($alt, '.')) {
  426. $alt = substr($alt, 1, strpos($alt, '.') - 1);
  427. }
  428. }
  429. return "<img src=\"{$url}\" alt=\"{$alt}\"" . self::_renderAttributes($token) . " />";
  430. }
  431. /**
  432. * Method for the list tag
  433. *
  434. * @param Zend_Markup_Token $token
  435. * @param string $text
  436. * @return void
  437. */
  438. protected static function _htmlList(Zend_Markup_Token $token, $text)
  439. {
  440. $type = null;
  441. if ($token->hasAttribute('list')) {
  442. // because '01' == '1'
  443. if ($token->getAttribute('list') === '01') {
  444. $type = 'decimal-leading-zero';
  445. } else {
  446. switch ($token->getAttribute('list')) {
  447. case '1':
  448. $type = 'decimal';
  449. break;
  450. case 'i':
  451. $type = 'lower-roman';
  452. break;
  453. case 'I':
  454. $type = 'upper-roman';
  455. break;
  456. case 'a':
  457. $type = 'lower-alpha';
  458. break;
  459. case 'A':
  460. $type = 'upper-alpha';
  461. break;
  462. // the following type is unsupported by IE (including IE8)
  463. case 'alpha':
  464. $type = 'lower-greek';
  465. break;
  466. // the CSS names itself
  467. case 'armenian': // unsupported by IE (including IE8)
  468. case 'decimal':
  469. case 'decimal-leading-zero': // unsupported by IE (including IE8)
  470. case 'georgian': // unsupported by IE (including IE8)
  471. case 'lower-alpha':
  472. case 'lower-greek': // unsupported by IE (including IE8)
  473. case 'lower-latin': // unsupported by IE (including IE8)
  474. case 'lower-roman':
  475. case 'upper-alpha':
  476. case 'upper-latin': // unsupported by IE (including IE8)
  477. case 'upper-roman':
  478. $type = $token->getAttribute('list');
  479. break;
  480. }
  481. }
  482. }
  483. if (null !== $type) {
  484. return "<ol style=\"list-style-type: {$type}\">{$text}</ol>";
  485. } else {
  486. return "<ul>{$text}</ul>";
  487. }
  488. }
  489. /**
  490. * Method for the code tag
  491. *
  492. * @param Zend_Markup_Token $token
  493. * @param string $text
  494. * @return string
  495. */
  496. protected static function _htmlCode(Zend_Markup_Token $token, $text)
  497. {
  498. return highlight_string($text, true);
  499. }
  500. /**
  501. * Check if a color is a valid HTML color
  502. *
  503. * @param string $color
  504. *
  505. * @return bool
  506. */
  507. protected static function _checkColor($color)
  508. {
  509. /*
  510. * aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive,
  511. * purple, red, silver, teal, white, and yellow.
  512. */
  513. $colors = array(
  514. 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime',
  515. 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal',
  516. 'white', 'yellow'
  517. );
  518. if (in_array($color, $colors)) {
  519. return true;
  520. }
  521. if (preg_match('/\#[0-9a-f]{6}/i', $color)) {
  522. return true;
  523. }
  524. return false;
  525. }
  526. /**
  527. * Filter method, used for converting newlines to <br /> tags
  528. *
  529. * @param string $value
  530. * @return string
  531. */
  532. protected function _filter($value)
  533. {
  534. if ($this->_filter) {
  535. return nl2br(htmlentities($value, ENT_QUOTES));
  536. }
  537. return $value;
  538. }
  539. }