2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Base class for all HTML classes
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
16 * @package HTML_Common
17 * @author Adam Daniel <adaniel1@eesus.jnj.com>
18 * @copyright 2001-2009 The PHP Group
19 * @license http://www.php.net/license/3_01.txt PHP License 3.01
20 * @version CVS: $Id: Common.php,v 1.15 2009/04/03 15:26:22 avb Exp $
21 * @link http://pear.php.net/package/HTML_Common/
25 * Base class for all HTML classes
28 * @package HTML_Common
29 * @author Adam Daniel <adaniel1@eesus.jnj.com>
30 * @version Release: 1.2.5
36 * Associative array of attributes
40 var $_attributes = array();
43 * Tab offset of the tag
58 * Contains the line end string
63 var $_lineEnd = "\12";
66 * HTML comment on the object
75 * @param mixed $attributes Associative array of table tag attributes
76 * or HTML attributes name="value" pairs
77 * @param int $tabOffset Indent offset in tabs
80 function HTML_Common($attributes = null, $tabOffset = 0)
82 $this->setAttributes($attributes);
83 $this->setTabOffset($tabOffset);
87 * Returns the current API version
94 } // end func apiVersion
103 function _getLineEnd()
105 return $this->_lineEnd;
106 } // end func getLineEnd
109 * Returns a string containing the unit for indenting HTML
118 } // end func _getTab
121 * Returns a string containing the offset for the whole HTML code
128 return str_repeat($this->_getTab(), $this->_tabOffset);
129 } // end func _getTabs
132 * Returns an HTML formatted attribute string
133 * @param array $attributes
137 function _getAttrString($attributes)
141 if (is_array($attributes)) {
142 $charset = HTML_Common::charset();
143 foreach ($attributes as $key => $value) {
144 $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
148 } // end func _getAttrString
151 * Returns a valid atrributes array from either a string or array
152 * @param mixed $attributes Either a typical HTML attribute string or an associative array
156 function _parseAttributes($attributes)
158 if (is_array($attributes)) {
160 foreach ($attributes as $key => $value) {
162 $key = $value = strtolower($value);
164 $key = strtolower($key);
170 } elseif (is_string($attributes)) {
171 $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
172 "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
173 if (preg_match_all($preg, $attributes, $regs)) {
174 for ($counter=0; $counter<count($regs[1]); $counter++) {
175 $name = $regs[1][$counter];
176 $check = $regs[0][$counter];
177 $value = $regs[7][$counter];
178 if (trim($name) == trim($check)) {
179 $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
181 if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
182 $arrAttr[strtolower(trim($name))] = substr($value, 1, -1);
184 $arrAttr[strtolower(trim($name))] = trim($value);
191 } // end func _parseAttributes
194 * Returns the array key for the given non-name-value pair attribute
196 * @param string $attr Attribute
197 * @param array $attributes Array of attribute
202 function _getAttrKey($attr, $attributes)
204 if (isset($attributes[strtolower($attr)])) {
209 } //end func _getAttrKey
212 * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
213 * @param array $attr1 Original attributes array
214 * @param array $attr2 New attributes array
217 function _updateAttrArray(&$attr1, $attr2)
219 if (!is_array($attr2)) {
222 foreach ($attr2 as $key => $value) {
223 $attr1[$key] = $value;
225 } // end func _updateAtrrArray
228 * Removes the given attribute from the given array
230 * @param string $attr Attribute name
231 * @param array $attributes Attribute array
236 function _removeAttr($attr, &$attributes)
238 $attr = strtolower($attr);
239 if (isset($attributes[$attr])) {
240 unset($attributes[$attr]);
242 } //end func _removeAttr
245 * Returns the value of the given attribute
247 * @param string $attr Attribute name
250 * @return string|null returns null if an attribute does not exist
252 function getAttribute($attr)
254 $attr = strtolower($attr);
255 if (isset($this->_attributes[$attr])) {
256 return $this->_attributes[$attr];
259 } //end func getAttribute
262 * Sets the value of the attribute
264 * @param string Attribute name
265 * @param string Attribute value (will be set to $name if omitted)
268 function setAttribute($name, $value = null)
270 $name = strtolower($name);
271 if (is_null($value)) {
274 $this->_attributes[$name] = $value;
275 } // end func setAttribute
278 * Sets the HTML attributes
279 * @param mixed $attributes Either a typical HTML attribute string or an associative array
282 function setAttributes($attributes)
284 $this->_attributes = $this->_parseAttributes($attributes);
285 } // end func setAttributes
288 * Returns the assoc array (default) or string of attributes
290 * @param bool Whether to return the attributes as string
293 * @return mixed attributes
295 function getAttributes($asString = false)
298 return $this->_getAttrString($this->_attributes);
300 return $this->_attributes;
302 } //end func getAttributes
305 * Updates the passed attributes without changing the other existing attributes
306 * @param mixed $attributes Either a typical HTML attribute string or an associative array
309 function updateAttributes($attributes)
311 $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
312 } // end func updateAttributes
315 * Removes an attribute
317 * @param string $attr Attribute name
322 function removeAttribute($attr)
324 $this->_removeAttr($attr, $this->_attributes);
325 } //end func removeAttribute
328 * Sets the line end style to Windows, Mac, Unix or a custom string.
330 * @param string $style "win", "mac", "unix" or custom string.
335 function setLineEnd($style)
339 $this->_lineEnd = "\15\12";
342 $this->_lineEnd = "\12";
345 $this->_lineEnd = "\15";
348 $this->_lineEnd = $style;
350 } // end func setLineEnd
353 * Sets the tab offset
358 function setTabOffset($offset)
360 $this->_tabOffset = $offset;
361 } // end func setTabOffset
364 * Returns the tabOffset
370 function getTabOffset()
372 return $this->_tabOffset;
373 } //end func getTabOffset
376 * Sets the string used to indent HTML
379 * @param string $string String used to indent ("\11", "\t", ' ', etc.).
383 function setTab($string)
385 $this->_tab = $string;
389 * Sets the HTML comment to be displayed at the beginning of the HTML string
396 function setComment($comment)
398 $this->_comment = $comment;
399 } // end func setHtmlComment
402 * Returns the HTML comment
408 function getComment()
410 return $this->_comment;
411 } //end func getComment
414 * Abstract method. Must be extended to return the objects HTML
426 * Displays the HTML to the screen
432 print $this->toHtml();
433 } // end func display
436 * Sets the charset to use by htmlspecialchars() function
438 * Since this parameter is expected to be global, the function is designed
439 * to be called statically:
441 * HTML_Common::charset('utf-8');
445 * $charset = HTML_Common::charset();
448 * @param string New charset to use. Omit if just getting the
449 * current value. Consult the htmlspecialchars() docs
450 * for a list of supported character sets.
451 * @return string Current charset
455 function charset($newCharset = null)
457 static $charset = 'ISO-8859-1';
459 if (!is_null($newCharset)) {
460 $charset = $newCharset;
463 } // end func charset
464 } // end class HTML_Common