web/lib/Zend/View/Helper/HtmlElement.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_View_Helper_Abstract
       
    25  */
       
    26 require_once 'Zend/View/Helper/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @category   Zend
       
    30  * @package    Zend_View
       
    31  * @subpackage Helper
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 abstract 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      * Converts an associative array to a string of tag attributes.
       
    80      *
       
    81      * @access public
       
    82      *
       
    83      * @param array $attribs From this array, each key-value pair is
       
    84      * converted to an attribute name and value.
       
    85      *
       
    86      * @return string The XHTML for the attributes.
       
    87      */
       
    88     protected function _htmlAttribs($attribs)
       
    89     {
       
    90         $xhtml = '';
       
    91         foreach ((array) $attribs as $key => $val) {
       
    92             $key = $this->view->escape($key);
       
    93 
       
    94             if (('on' == substr($key, 0, 2)) || ('constraints' == $key)) {
       
    95                 // Don't escape event attributes; _do_ substitute double quotes with singles
       
    96                 if (!is_scalar($val)) {
       
    97                     // non-scalar data should be cast to JSON first
       
    98                     require_once 'Zend/Json.php';
       
    99                     $val = Zend_Json::encode($val);
       
   100                 }
       
   101                 $val = preg_replace('/"([^"]*)":/', '$1:', $val);
       
   102             } else {
       
   103                 if (is_array($val)) {
       
   104                     $val = implode(' ', $val);
       
   105                 }
       
   106                 $val = $this->view->escape($val);
       
   107             }
       
   108 
       
   109             if ('id' == $key) {
       
   110                 $val = $this->_normalizeId($val);
       
   111             }
       
   112 
       
   113             if (strpos($val, '"') !== false) {
       
   114                 $xhtml .= " $key='$val'";
       
   115             } else {
       
   116                 $xhtml .= " $key=\"$val\"";
       
   117             }
       
   118 
       
   119         }
       
   120         return $xhtml;
       
   121     }
       
   122 
       
   123     /**
       
   124      * Normalize an ID
       
   125      *
       
   126      * @param  string $value
       
   127      * @return string
       
   128      */
       
   129     protected function _normalizeId($value)
       
   130     {
       
   131         if (strstr($value, '[')) {
       
   132             if ('[]' == substr($value, -2)) {
       
   133                 $value = substr($value, 0, strlen($value) - 2);
       
   134             }
       
   135             $value = trim($value, ']');
       
   136             $value = str_replace('][', '-', $value);
       
   137             $value = str_replace('[', '-', $value);
       
   138         }
       
   139         return $value;
       
   140     }
       
   141 }