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>
32 * Associative array of table attributes
37 var $_attributes = array();
40 * Tab offset of the object
57 * HTML comment on the object
65 * Contains the line end string
71 var $_lineEnd = "\12";
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
104 function _getLineEnd()
106 return $this->_lineEnd;
107 } //end func getLineEnd
110 * Returns a string containing the unit for indenting HTML
119 } // end func _getTab
122 * Returns a string containing the offset for the whole HTML code
129 return str_repeat($this->_getTab(), $this->_tabOffset);
130 } // end func _getTabs
133 * Returns an HTML formatted attribute string
134 * @param array $attributes
138 function _getAttrString($attributes)
141 if (is_array($attributes)) {
142 foreach ($attributes as $key => $value) {
143 $strAttr .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
147 } // end func _getAttrString
150 * Returns a valid atrributes array from either a string or array
151 * @param mixed $attributes Either a typical HTML attribute string or an associative array
154 function _parseAttributes($attributes)
156 if (is_array($attributes)) {
158 foreach ($attributes as $key => $value) {
160 $key = $value = strtolower($value);
162 $key = strtolower($key);
168 } elseif (is_string($attributes)) {
169 $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
170 "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
171 if (preg_match_all($preg, $attributes, $regs)) {
172 for ($counter=0; $counter<count($regs[1]); $counter++) {
173 $name = $regs[1][$counter];
174 $check = $regs[0][$counter];
175 $value = $regs[7][$counter];
176 if (trim($name) == trim($check)) {
177 $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
179 if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
180 $value = substr($value, 1, -1);
182 $arrAttr[strtolower(trim($name))] = trim($value);
188 } // end func _parseAttributes
191 * Returns the array key for the given non-name-value pair attribute
193 * @param string $attr Attribute
194 * @param array $attributes Array of attribute
200 function _getAttrKey($attr, $attributes)
202 if (isset($attributes[strtolower($attr)])) {
207 } //end func _getAttrKey
210 * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
211 * @param array $attr1 Original attributes array
212 * @param array $attr2 New attributes array
216 function _updateAttrArray(&$attr1, $attr2)
218 if (!is_array($attr2)) {
221 foreach ($attr2 as $key => $value) {
222 $attr1[$key] = $value;
224 } // end func _updateAtrrArray
227 * Removes the given attribute from the given array
229 * @param string $attr Attribute name
230 * @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
253 function getAttribute($attr)
255 $attr = strtolower($attr);
256 if (isset($this->_attributes[$attr])) {
257 return $this->_attributes[$attr];
260 } //end func getAttribute
263 * Sets the HTML attributes
264 * @param mixed $attributes Either a typical HTML attribute string or an associative array
267 function setAttributes($attributes)
269 $this->_attributes = $this->_parseAttributes($attributes);
270 } // end func _setAttributes
273 * Returns an assoc array of attributes
280 function getAttributes()
282 return $this->_attributes;
283 } //end func getAttributes
286 * Updates the passed attributes without changing the other existing attributes
287 * @param mixed $attributes Either a typical HTML attribute string or an associative array
290 function updateAttributes($attributes)
292 $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
293 } // end func updateAttributes
296 * Removes an attribute
298 * @param string $attr Attribute name
304 function removeAttribute($attr)
306 $this->_removeAttr($attr, $this->_attributes);
307 } //end func removeAttribute
310 * Sets the line end style to Windows, Mac, Unix or a custom string.
312 * @param string $style "win", "mac", "unix" or custom string.
317 function setLineEnd($style)
321 $this->_lineEnd = "\15\12";
324 $this->_lineEnd = "\12";
327 $this->_lineEnd = "\15";
330 $this->_lineEnd = $style;
333 } // end func setLineEnd
336 * Sets the tab offset
341 function setTabOffset($offset)
343 $this->_tabOffset = $offset;
344 } // end func setTabOffset
347 * Returns the tabOffset
354 function getTabOffset()
356 return $this->_tabOffset;
357 } //end func getTabOffset
360 * Sets the string used to indent HTML
363 * @param string $string String used to indent ("\11", "\t", ' ', etc.).
367 function setTab($string)
369 $this->_tab = $string;
373 * Sets the HTML comment to be displayed at the beginning of the HTML string
381 function setComment($comment)
383 $this->_comment = $comment;
384 } // end func setHtmlComment
387 * Returns the HTML comment
394 function getComment()
396 return $this->_comment;
397 } //end func getComment
400 * Abstract method. Must be extended to return the objects HTML
412 * Displays the HTML to the screen
418 print $this->toHtml();
419 } // end func display
421 } // end class HTML_Common