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