HtmlElement.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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-2009 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-2009 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. * Converts an associative array to a string of tag attributes.
  73. *
  74. * @access public
  75. *
  76. * @param array $attribs From this array, each key-value pair is
  77. * converted to an attribute name and value.
  78. *
  79. * @return string The XHTML for the attributes.
  80. */
  81. protected function _htmlAttribs($attribs)
  82. {
  83. $xhtml = '';
  84. foreach ((array) $attribs as $key => $val) {
  85. $key = $this->view->escape($key);
  86. if (('on' == substr($key, 0, 2)) || ('constraints' == $key)) {
  87. // Don't escape event attributes; _do_ substitute double quotes with singles
  88. if (!is_scalar($val)) {
  89. // non-scalar data should be cast to JSON first
  90. require_once 'Zend/Json.php';
  91. $val = Zend_Json::encode($val);
  92. }
  93. $val = preg_replace('/"([^"]*)":/', '$1:', $val);
  94. } else {
  95. if (is_array($val)) {
  96. $val = implode(' ', $val);
  97. }
  98. $val = $this->view->escape($val);
  99. }
  100. if ('id' == $key) {
  101. $val = $this->_normalizeId($val);
  102. }
  103. if (strpos($val, '"') !== false) {
  104. $xhtml .= " $key='$val'";
  105. } else {
  106. $xhtml .= " $key=\"$val\"";
  107. }
  108. }
  109. return $xhtml;
  110. }
  111. /**
  112. * Normalize an ID
  113. *
  114. * @param string $value
  115. * @return string
  116. */
  117. protected function _normalizeId($value)
  118. {
  119. if (strstr($value, '[')) {
  120. if ('[]' == substr($value, -2)) {
  121. $value = substr($value, 0, strlen($value) - 2);
  122. }
  123. $value = trim($value, ']');
  124. $value = str_replace('][', '-', $value);
  125. $value = str_replace('[', '-', $value);
  126. }
  127. return $value;
  128. }
  129. }