HtmlElement.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2014 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_View_Helper_Abstract
  24. */
  25. require_once 'Zend/View/Helper/Abstract.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_View
  29. * @subpackage Helper
  30. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_View_Helper_HtmlElement extends Zend_View_Helper_Abstract
  34. {
  35. /**
  36. * EOL character
  37. */
  38. const EOL = "\n";
  39. /**
  40. * The tag closing bracket
  41. *
  42. * @var string
  43. */
  44. protected $_closingBracket = null;
  45. /**
  46. * Get the tag closing bracket
  47. *
  48. * @return string
  49. */
  50. public function getClosingBracket()
  51. {
  52. if (!$this->_closingBracket) {
  53. if ($this->_isXhtml()) {
  54. $this->_closingBracket = ' />';
  55. } else {
  56. $this->_closingBracket = '>';
  57. }
  58. }
  59. return $this->_closingBracket;
  60. }
  61. /**
  62. * Is doctype XHTML?
  63. *
  64. * @return boolean
  65. */
  66. protected function _isXhtml()
  67. {
  68. $doctype = $this->view->doctype();
  69. return $doctype->isXhtml();
  70. }
  71. /**
  72. * Is doctype HTML5?
  73. *
  74. * @return boolean
  75. */
  76. protected function _isHtml5()
  77. {
  78. $doctype = $this->view->doctype();
  79. return $doctype->isHtml5();
  80. }
  81. /**
  82. * Is doctype strict?
  83. *
  84. * @return boolean
  85. */
  86. protected function _isStrictDoctype()
  87. {
  88. $doctype = $this->view->doctype();
  89. return $doctype->isStrict();
  90. }
  91. /**
  92. * Converts an associative array to a string of tag attributes.
  93. *
  94. * @access public
  95. *
  96. * @param array $attribs From this array, each key-value pair is
  97. * converted to an attribute name and value.
  98. *
  99. * @return string The XHTML for the attributes.
  100. */
  101. protected function _htmlAttribs($attribs)
  102. {
  103. $xhtml = '';
  104. foreach ((array) $attribs as $key => $val) {
  105. $key = $this->view->escape($key);
  106. if (('on' == substr($key, 0, 2)) || ('constraints' == $key)) {
  107. // Don't escape event attributes; _do_ substitute double quotes with singles
  108. if (!is_scalar($val)) {
  109. // non-scalar data should be cast to JSON first
  110. require_once 'Zend/Json.php';
  111. $val = Zend_Json::encode($val);
  112. }
  113. // Escape single quotes inside event attribute values.
  114. // This will create html, where the attribute value has
  115. // single quotes around it, and escaped single quotes or
  116. // non-escaped double quotes inside of it
  117. $val = str_replace('\'', '&#39;', $val);
  118. } else {
  119. if (is_array($val)) {
  120. $val = implode(' ', $val);
  121. }
  122. $val = $this->view->escape($val);
  123. }
  124. if ('id' == $key) {
  125. $val = $this->_normalizeId($val);
  126. }
  127. if (strpos($val, '"') !== false) {
  128. $xhtml .= " $key='$val'";
  129. } else {
  130. $xhtml .= " $key=\"$val\"";
  131. }
  132. }
  133. return $xhtml;
  134. }
  135. /**
  136. * Normalize an ID
  137. *
  138. * @param string $value
  139. * @return string
  140. */
  141. protected function _normalizeId($value)
  142. {
  143. if (strstr($value, '[')) {
  144. if ('[]' == substr($value, -2)) {
  145. $value = substr($value, 0, strlen($value) - 2);
  146. }
  147. $value = trim($value, ']');
  148. $value = str_replace('][', '-', $value);
  149. $value = str_replace('[', '-', $value);
  150. }
  151. return $value;
  152. }
  153. }