/library/Zend/View/Helper/HtmlElement.php

https://bitbucket.org/hamidrezas/melobit · PHP · 156 lines · 68 code · 13 blank · 75 comment · 17 complexity · 5908ea08cef20c5ae22c24e7fc7e94f9 MD5 · raw file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: HtmlElement.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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 strict?
  73. *
  74. * @return boolean
  75. */
  76. protected function _isStrictDoctype()
  77. {
  78. $doctype = $this->view->doctype();
  79. return $doctype->isStrict();
  80. }
  81. /**
  82. * Converts an associative array to a string of tag attributes.
  83. *
  84. * @access public
  85. *
  86. * @param array $attribs From this array, each key-value pair is
  87. * converted to an attribute name and value.
  88. *
  89. * @return string The XHTML for the attributes.
  90. */
  91. protected function _htmlAttribs($attribs)
  92. {
  93. $xhtml = '';
  94. foreach ((array) $attribs as $key => $val) {
  95. $key = $this->view->escape($key);
  96. if (('on' == substr($key, 0, 2)) || ('constraints' == $key)) {
  97. // Don't escape event attributes; _do_ substitute double quotes with singles
  98. if (!is_scalar($val)) {
  99. // non-scalar data should be cast to JSON first
  100. require_once 'Zend/Json.php';
  101. $val = Zend_Json::encode($val);
  102. }
  103. // Escape single quotes inside event attribute values.
  104. // This will create html, where the attribute value has
  105. // single quotes around it, and escaped single quotes or
  106. // non-escaped double quotes inside of it
  107. $val = str_replace('\'', '&#39;', $val);
  108. } else {
  109. if (is_array($val)) {
  110. $val = implode(' ', $val);
  111. }
  112. $val = $this->view->escape($val);
  113. }
  114. if ('id' == $key) {
  115. $val = $this->_normalizeId($val);
  116. }
  117. if (strpos($val, '"') !== false) {
  118. $xhtml .= " $key='$val'";
  119. } else {
  120. $xhtml .= " $key=\"$val\"";
  121. }
  122. }
  123. return $xhtml;
  124. }
  125. /**
  126. * Normalize an ID
  127. *
  128. * @param string $value
  129. * @return string
  130. */
  131. protected function _normalizeId($value)
  132. {
  133. if (strstr($value, '[')) {
  134. if ('[]' == substr($value, -2)) {
  135. $value = substr($value, 0, strlen($value) - 2);
  136. }
  137. $value = trim($value, ']');
  138. $value = str_replace('][', '-', $value);
  139. $value = str_replace('[', '-', $value);
  140. }
  141. return $value;
  142. }
  143. }