2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Adam Daniel <adaniel1@eesus.jnj.com> |
17 // +----------------------------------------------------------------------+
22 * Base class for all HTML classes
24 * @author Adam Daniel <adaniel1@eesus.jnj.com>
26 * @package HTML_Common
32 * Base class for all HTML classes
34 * @author Adam Daniel <adaniel1@eesus.jnj.com>
42 * Associative array of table attributes
46 var $_attributes = array();
49 * Tab offset of the table
64 * Contains the line end string
69 var $_lineEnd = "\12";
72 * HTML comment on the object
81 * @param mixed $attributes Associative array of table tag attributes
82 * or HTML attributes name="value" pairs
83 * @param int $tabOffset Indent offset in tabs
86 function HTML_Common($attributes = null, $tabOffset = 0)
88 $this->setAttributes($attributes);
89 $this->setTabOffset($tabOffset);
93 * Returns the current API version
100 } // end func apiVersion
103 * Returns the lineEnd
110 function _getLineEnd()
112 return $this->_lineEnd
;
113 } // end func getLineEnd
116 * Returns a string containing the unit for indenting HTML
125 } // end func _getTab
128 * Returns a string containing the offset for the whole HTML code
135 return str_repeat($this->_getTab(), $this->_tabOffset
);
136 } // end func _getTabs
139 * Returns an HTML formatted attribute string
140 * @param array $attributes
144 function _getAttrString($attributes)
148 if (is_array($attributes)) {
149 foreach ($attributes as $key => $value) {
150 $strAttr .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
154 } // end func _getAttrString
157 * Returns a valid atrributes array from either a string or array
158 * @param mixed $attributes Either a typical HTML attribute string or an associative array
161 function _parseAttributes($attributes)
163 if (is_array($attributes)) {
165 foreach ($attributes as $key => $value) {
167 $key = $value = strtolower($value);
169 $key = strtolower($key);
175 } elseif (is_string($attributes)) {
176 $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
177 "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
178 if (preg_match_all($preg, $attributes, $regs)) {
179 for ($counter=0; $counter<count($regs[1]); $counter++
) {
180 $name = $regs[1][$counter];
181 $check = $regs[0][$counter];
182 $value = $regs[7][$counter];
183 if (trim($name) == trim($check)) {
184 $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
186 if (substr($value, 0, 1) == "\"" ||
substr($value, 0, 1) == "'") {
187 $value = substr($value, 1, -1);
189 $arrAttr[strtolower(trim($name))] = trim($value);
195 } // end func _parseAttributes
198 * Returns the array key for the given non-name-value pair attribute
200 * @param string $attr Attribute
201 * @param array $attributes Array of attribute
207 function _getAttrKey($attr, $attributes)
209 if (isset($attributes[strtolower($attr)])) {
214 } //end func _getAttrKey
217 * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
218 * @param array $attr1 Original attributes array
219 * @param array $attr2 New attributes array
222 function _updateAttrArray(&$attr1, $attr2)
224 if (!is_array($attr2)) {
227 foreach ($attr2 as $key => $value) {
228 $attr1[$key] = $value;
230 } // end func _updateAtrrArray
233 * Removes the given attribute from the given array
235 * @param string $attr Attribute name
236 * @param array $attributes Attribute array
242 function _removeAttr($attr, &$attributes)
244 $attr = strtolower($attr);
245 if (isset($attributes[$attr])) {
246 unset($attributes[$attr]);
248 } //end func _removeAttr
251 * Returns the value of the given attribute
253 * @param string $attr Attribute name
259 function getAttribute($attr)
261 $attr = strtolower($attr);
262 if (isset($this->_attributes
[$attr])) {
263 return $this->_attributes
[$attr];
266 } //end func getAttribute
269 * Sets the HTML attributes
270 * @param mixed $attributes Either a typical HTML attribute string or an associative array
273 function setAttributes($attributes)
275 $this->_attributes
= $this->_parseAttributes($attributes);
276 } // end func setAttributes
279 * Returns the assoc array (default) or string of attributes
281 * @param bool Whether to return the attributes as string
284 * @return mixed attributes
286 function getAttributes($asString = false)
289 return $this->_getAttrString($this->_attributes
);
291 return $this->_attributes
;
293 } //end func getAttributes
296 * Updates the passed attributes without changing the other existing attributes
297 * @param mixed $attributes Either a typical HTML attribute string or an associative array
300 function updateAttributes($attributes)
302 $this->_updateAttrArray($this->_attributes
, $this->_parseAttributes($attributes));
303 } // end func updateAttributes
306 * Removes an attribute
308 * @param string $attr Attribute name
314 function removeAttribute($attr)
316 $this->_removeAttr($attr, $this->_attributes
);
317 } //end func removeAttribute
320 * Sets the line end style to Windows, Mac, Unix or a custom string.
322 * @param string $style "win", "mac", "unix" or custom string.
327 function setLineEnd($style)
331 $this->_lineEnd
= "\15\12";
334 $this->_lineEnd
= "\12";
337 $this->_lineEnd
= "\15";
340 $this->_lineEnd
= $style;
342 } // end func setLineEnd
345 * Sets the tab offset
350 function setTabOffset($offset)
352 $this->_tabOffset
= $offset;
353 } // end func setTabOffset
356 * Returns the tabOffset
362 function getTabOffset()
364 return $this->_tabOffset
;
365 } //end func getTabOffset
368 * Sets the string used to indent HTML
371 * @param string $string String used to indent ("\11", "\t", ' ', etc.).
375 function setTab($string)
377 $this->_tab
= $string;
381 * Sets the HTML comment to be displayed at the beginning of the HTML string
388 function setComment($comment)
390 $this->_comment
= $comment;
391 } // end func setHtmlComment
394 * Returns the HTML comment
400 function getComment()
402 return $this->_comment
;
403 } //end func getComment
406 * Abstract method. Must be extended to return the objects HTML
418 * Displays the HTML to the screen
424 print $this->toHtml();
425 } // end func display
427 } // end class HTML_Common