Html.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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' => null,
  151. 'group' => 'inline',
  152. 'filter' => true,
  153. ),
  154. 'img' => array(
  155. 'type' => 6,
  156. 'callback' => null,
  157. 'group' => 'inline-empty',
  158. 'filter' => true,
  159. ),
  160. 'code' => array(
  161. 'type' => 6,
  162. 'callback' => null,
  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' => null,
  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. * Constructor
  305. *
  306. * @param array|Zend_Config $options
  307. *
  308. * @return void
  309. */
  310. public function __construct($options = array())
  311. {
  312. if ($options instanceof Zend_Config) {
  313. $options = $options->toArray();
  314. }
  315. $this->_pluginLoader = new Zend_Loader_PluginLoader(array(
  316. 'Zend_Markup_Renderer_Html' => 'Zend/Markup/Renderer/Html/'
  317. ));
  318. parent::__construct($options);
  319. }
  320. /**
  321. * Execute a replace token
  322. *
  323. * @param Zend_Markup_Token $token
  324. * @param array $tag
  325. * @return string
  326. */
  327. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  328. {
  329. if (isset($tag['tag'])) {
  330. if (!isset($tag['attributes'])) {
  331. $tag['attributes'] = array();
  332. }
  333. $attrs = self::renderAttributes($token, $tag['attributes']);
  334. return "<{$tag['tag']}{$attrs}>{$this->_render($token)}</{$tag['tag']}>";
  335. }
  336. return parent::_executeReplace($token, $tag);
  337. }
  338. /**
  339. * Execute a single replace token
  340. *
  341. * @param Zend_Markup_Token $token
  342. * @param array $tag
  343. * @return string
  344. */
  345. protected function _executeSingleReplace(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} />";
  353. }
  354. return parent::_executeSingleReplace($token, $tag);
  355. }
  356. /**
  357. * Render some attributes
  358. *
  359. * @param Zend_Markup_Token $token
  360. * @param array $tag
  361. * @return string
  362. */
  363. public static function renderAttributes(Zend_Markup_Token $token, array $attributes = array())
  364. {
  365. $attributes = array_merge(self::$_defaultAttributes, $attributes);
  366. $return = '';
  367. $tokenAttributes = $token->getAttributes();
  368. // correct style attribute
  369. if (isset($tokenAttributes['style'])) {
  370. $tokenAttributes['style'] = trim($tokenAttributes['style']);
  371. if ($tokenAttributes['style'][strlen($tokenAttributes['style']) - 1] != ';') {
  372. $tokenAttributes['style'] .= ';';
  373. }
  374. } else {
  375. $tokenAttributes['style'] = '';
  376. }
  377. // special treathment for 'align' and 'color' attribute
  378. if (isset($tokenAttributes['align'])) {
  379. $tokenAttributes['style'] .= 'text-align: ' . $tokenAttributes['align'] . ';';
  380. unset($tokenAttributes['align']);
  381. }
  382. if (isset($tokenAttributes['color']) && self::checkColor($tokenAttributes['color'])) {
  383. $tokenAttributes['style'] .= 'color: ' . $tokenAttributes['color'] . ';';
  384. unset($tokenAttributes['color']);
  385. }
  386. /*
  387. * loop through all the available attributes, and check if there is
  388. * a value defined by the token
  389. * if there is no value defined by the token, use the default value or
  390. * don't set the attribute
  391. */
  392. foreach ($attributes as $attribute => $value) {
  393. if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) {
  394. $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute], ENT_QUOTES) . '"';
  395. } elseif (!empty($value)) {
  396. $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES) . '"';
  397. }
  398. }
  399. return $return;
  400. }
  401. /**
  402. * Check if a color is a valid HTML color
  403. *
  404. * @param string $color
  405. *
  406. * @return bool
  407. */
  408. public static function checkColor($color)
  409. {
  410. /*
  411. * aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive,
  412. * purple, red, silver, teal, white, and yellow.
  413. */
  414. $colors = array(
  415. 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime',
  416. 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal',
  417. 'white', 'yellow'
  418. );
  419. if (in_array($color, $colors)) {
  420. return true;
  421. }
  422. if (preg_match('/\#[0-9a-f]{6}/i', $color)) {
  423. return true;
  424. }
  425. return false;
  426. }
  427. /**
  428. * Filter method, used for converting newlines to <br /> tags
  429. *
  430. * @param string $value
  431. * @return string
  432. */
  433. protected function _filter($value)
  434. {
  435. if ($this->_filter) {
  436. return nl2br(htmlentities($value, ENT_QUOTES));
  437. }
  438. return $value;
  439. }
  440. }