Browse Source

Feature #272 - PEAR, HTML_Common class: Upgrading to version 1.2.5 (last stable). There is a solution inside about the function htmlspecialchars() and the encoding it uses as it is called within PEAR.

Ivan Tcholakov 15 years ago
parent
commit
8314b62fcd
1 changed files with 92 additions and 38 deletions
  1. 92 38
      main/inc/lib/pear/HTML/Common.php

+ 92 - 38
main/inc/lib/pear/HTML/Common.php

@@ -1,42 +1,46 @@
 <?php
-/* vim: set expandtab tabstop=4 shiftwidth=4: */
-// +----------------------------------------------------------------------+
-// | PHP Version 4                                                        |
-// +----------------------------------------------------------------------+
-// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
-// +----------------------------------------------------------------------+
-// | This source file is subject to version 2.0 of the PHP license,       |
-// | that is bundled with this package in the file LICENSE, and is        |
-// | available at through the world-wide-web at                           |
-// | http://www.php.net/license/2_02.txt.                                 |
-// | If you did not receive a copy of the PHP license and are unable to   |
-// | obtain it through the world-wide-web, please send a note to          |
-// | license@php.net so we can mail you a copy immediately.               |
-// +----------------------------------------------------------------------+
-// | Author: Adam Daniel <adaniel1@eesus.jnj.com>                         |
-// +----------------------------------------------------------------------+
-//
-// $Id: Common.php 20456 2009-05-10 17:27:44Z ivantcholakov $
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
 /**
  * Base class for all HTML classes
  *
+ * PHP versions 4 and 5
+ *
+ * LICENSE: This source file is subject to version 3.01 of the PHP license
+ * that is available through the world-wide-web at the following URI:
+ * http://www.php.net/license/3_01.txt If you did not receive a copy of
+ * the PHP License and are unable to obtain it through the web, please
+ * send a note to license@php.net so we can mail you a copy immediately.
+ *
+ * @category    HTML
+ * @package     HTML_Common
  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
- * @version     1.7
- * @since       PHP 4.0.3pl1
- * @abstract
+ * @copyright   2001-2009 The PHP Group
+ * @license     http://www.php.net/license/3_01.txt PHP License 3.01
+ * @version     CVS: $Id: Common.php,v 1.15 2009/04/03 15:26:22 avb Exp $
+ * @link        http://pear.php.net/package/HTML_Common/
  */
-class HTML_Common {
 
+/**
+ * Base class for all HTML classes
+ *
+ * @category    HTML
+ * @package     HTML_Common
+ * @author      Adam Daniel <adaniel1@eesus.jnj.com>
+ * @version     Release: 1.2.5
+ * @abstract
+ */
+class HTML_Common
+{
     /**
-     * Associative array of table attributes
+     * Associative array of attributes
      * @var     array
      * @access  private
      */
     var $_attributes = array();
 
     /**
-     * Tab offset of the table
+     * Tab offset of the tag
      * @var     int
      * @access  private
      */
@@ -95,7 +99,6 @@ class HTML_Common {
      * @since     1.7
      * @access    private
      * @return    string
-     * @throws
      */
     function _getLineEnd()
     {
@@ -135,10 +138,13 @@ class HTML_Common {
     {
         $strAttr = '';
 
-		global $charset;
         if (is_array($attributes)) {
+            $charset = HTML_Common::charset();
             foreach ($attributes as $key => $value) {
-                $strAttr .= ' ' . $key . '="' . @htmlspecialchars($value, ENT_QUOTES, $charset) . '"';
+            	// Modified by Ivan Tcholakov, 16-MAR-2010
+                //$strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
+                $strAttr .= ' ' . $key . '="' . @htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
+                //
             }
         }
         return $strAttr;
@@ -148,6 +154,7 @@ class HTML_Common {
      * Returns a valid atrributes array from either a string or array
      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
      * @access   private
+     * @return   array
      */
     function _parseAttributes($attributes)
     {
@@ -175,9 +182,10 @@ class HTML_Common {
                         $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
                     } else {
                         if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
-                            $value = substr($value, 1, -1);
+                            $arrAttr[strtolower(trim($name))] = substr($value, 1, -1);
+                        } else {
+                            $arrAttr[strtolower(trim($name))] = trim($value);
                         }
-                        $arrAttr[strtolower(trim($name))] = trim($value);
                     }
                 }
                 return $arrAttr;
@@ -192,8 +200,7 @@ class HTML_Common {
      * @param     array     $attributes   Array of attribute
      * @since     1.0
      * @access    private
-     * @return    array key
-     * @throws
+     * @return    bool
      */
     function _getAttrKey($attr, $attributes)
     {
@@ -209,7 +216,6 @@ class HTML_Common {
      * @param    array   $attr1      Original attributes array
      * @param    array   $attr2      New attributes array
      * @access   private
-     * @return   array
      */
     function _updateAttrArray(&$attr1, $attr2)
     {
@@ -227,9 +233,8 @@ class HTML_Common {
      * @param     string    $attr           Attribute name
      * @param     array     $attributes     Attribute array
      * @since     1.4
-     * @access    public
+     * @access    private
      * @return    void
-     * @throws
      */
     function _removeAttr($attr, &$attributes)
     {
@@ -245,8 +250,7 @@ class HTML_Common {
      * @param     string    $attr   Attribute name
      * @since     1.5
      * @access    public
-     * @return    void
-     * @throws
+     * @return    string|null   returns null if an attribute does not exist
      */
     function getAttribute($attr)
     {
@@ -258,6 +262,22 @@ class HTML_Common {
     } //end func getAttribute
 
     /**
+     * Sets the value of the attribute
+     *
+     * @param   string  Attribute name
+     * @param   string  Attribute value (will be set to $name if omitted)
+     * @access  public
+     */
+    function setAttribute($name, $value = null)
+    {
+        $name = strtolower($name);
+        if (is_null($value)) {
+            $value = $name;
+        }
+        $this->_attributes[$name] = $value;
+    } // end func setAttribute
+
+    /**
      * Sets the HTML attributes
      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
      * @access   public
@@ -301,7 +321,6 @@ class HTML_Common {
      * @since     1.4
      * @access    public
      * @return    void
-     * @throws
      */
     function removeAttribute($attr)
     {
@@ -325,7 +344,7 @@ class HTML_Common {
             case 'unix':
                 $this->_lineEnd = "\12";
                 break;
-            case 'mac';
+            case 'mac':
                 $this->_lineEnd = "\15";
                 break;
             default:
@@ -416,5 +435,40 @@ class HTML_Common {
         print $this->toHtml();
     } // end func display
 
+    /**
+     * Sets the charset to use by htmlspecialchars() function
+     *
+     * Since this parameter is expected to be global, the function is designed
+     * to be called statically:
+     * <code>
+     * HTML_Common::charset('utf-8');
+     * </code>
+     * or
+     * <code>
+     * $charset = HTML_Common::charset();
+     * </code>
+     *
+     * @param   string  New charset to use. Omit if just getting the
+     *                  current value. Consult the htmlspecialchars() docs
+     *                  for a list of supported character sets.
+     * @return  string  Current charset
+     * @access  public
+     * @static
+     */
+    function charset($newCharset = null)
+    {
+        // Modified by Ivan Tcholakov, 16-MAR-2010
+        //static $charset = 'ISO-8859-1';
+        static $charset;
+        if (!isset($charset)) {
+            $charset = api_get_system_encoding();
+        }
+        //
+
+        if (!is_null($newCharset)) {
+            $charset = $newCharset;
+        }
+        return $charset;
+    } // end func charset
 } // end class HTML_Common
 ?>