Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / pear / HTML / Common.php
blob2c687dd4af8cb8bc853dd74c1ea08933b052b6d3
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
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 // +----------------------------------------------------------------------+
19 // $Id$
21 /**
22 * Base class for all HTML classes
24 * @author Adam Daniel <adaniel1@eesus.jnj.com>
25 * @category HTML
26 * @package HTML_Common
27 * @version 1.2.2
28 * @abstract
31 /**
32 * Base class for all HTML classes
34 * @author Adam Daniel <adaniel1@eesus.jnj.com>
35 * @version 1.7
36 * @since PHP 4.0.3pl1
37 * @abstract
39 class HTML_Common {
41 /**
42 * Associative array of table attributes
43 * @var array
44 * @access private
46 var $_attributes = array();
48 /**
49 * Tab offset of the table
50 * @var int
51 * @access private
53 var $_tabOffset = 0;
55 /**
56 * Tab string
57 * @var string
58 * @since 1.7
59 * @access private
61 var $_tab = "\11";
63 /**
64 * Contains the line end string
65 * @var string
66 * @since 1.7
67 * @access private
69 var $_lineEnd = "\12";
71 /**
72 * HTML comment on the object
73 * @var string
74 * @since 1.5
75 * @access private
77 var $_comment = '';
79 /**
80 * Class constructor
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
84 * @access public
86 function HTML_Common($attributes = null, $tabOffset = 0)
88 $this->setAttributes($attributes);
89 $this->setTabOffset($tabOffset);
90 } // end constructor
92 /**
93 * Returns the current API version
94 * @access public
95 * @returns double
97 function apiVersion()
99 return 1.7;
100 } // end func apiVersion
103 * Returns the lineEnd
105 * @since 1.7
106 * @access private
107 * @return string
108 * @throws
110 function _getLineEnd()
112 return $this->_lineEnd;
113 } // end func getLineEnd
116 * Returns a string containing the unit for indenting HTML
118 * @since 1.7
119 * @access private
120 * @return string
122 function _getTab()
124 return $this->_tab;
125 } // end func _getTab
128 * Returns a string containing the offset for the whole HTML code
130 * @return string
131 * @access private
133 function _getTabs()
135 return str_repeat($this->_getTab(), $this->_tabOffset);
136 } // end func _getTabs
139 * Returns an HTML formatted attribute string
140 * @param array $attributes
141 * @return string
142 * @access private
144 function _getAttrString($attributes)
146 $strAttr = '';
148 if (is_array($attributes)) {
149 foreach ($attributes as $key => $value) {
150 $strAttr .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
153 return $strAttr;
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
159 * @access private
161 function _parseAttributes($attributes)
163 if (is_array($attributes)) {
164 $ret = array();
165 foreach ($attributes as $key => $value) {
166 if (is_int($key)) {
167 $key = $value = strtolower($value);
168 } else {
169 $key = strtolower($key);
171 $ret[$key] = $value;
173 return $ret;
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));
185 } else {
186 if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
187 $value = substr($value, 1, -1);
189 $arrAttr[strtolower(trim($name))] = trim($value);
192 return $arrAttr;
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
202 * @since 1.0
203 * @access private
204 * @return bool
205 * @throws
207 function _getAttrKey($attr, $attributes)
209 if (isset($attributes[strtolower($attr)])) {
210 return true;
211 } else {
212 return null;
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
220 * @access private
222 function _updateAttrArray(&$attr1, $attr2)
224 if (!is_array($attr2)) {
225 return false;
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
237 * @since 1.4
238 * @access private
239 * @return void
240 * @throws
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
254 * @since 1.5
255 * @access public
256 * @return void
257 * @throws
259 function getAttribute($attr)
261 $attr = strtolower($attr);
262 if (isset($this->_attributes[$attr])) {
263 return $this->_attributes[$attr];
265 return null;
266 } //end func getAttribute
269 * Sets the HTML attributes
270 * @param mixed $attributes Either a typical HTML attribute string or an associative array
271 * @access public
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
282 * @since 1.6
283 * @access public
284 * @return mixed attributes
286 function getAttributes($asString = false)
288 if ($asString) {
289 return $this->_getAttrString($this->_attributes);
290 } else {
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
298 * @access public
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
309 * @since 1.4
310 * @access public
311 * @return void
312 * @throws
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.
323 * @since 1.7
324 * @access public
325 * @return void
327 function setLineEnd($style)
329 switch ($style) {
330 case 'win':
331 $this->_lineEnd = "\15\12";
332 break;
333 case 'unix':
334 $this->_lineEnd = "\12";
335 break;
336 case 'mac':
337 $this->_lineEnd = "\15";
338 break;
339 default:
340 $this->_lineEnd = $style;
342 } // end func setLineEnd
345 * Sets the tab offset
347 * @param int $offset
348 * @access public
350 function setTabOffset($offset)
352 $this->_tabOffset = $offset;
353 } // end func setTabOffset
356 * Returns the tabOffset
358 * @since 1.5
359 * @access public
360 * @return int
362 function getTabOffset()
364 return $this->_tabOffset;
365 } //end func getTabOffset
368 * Sets the string used to indent HTML
370 * @since 1.7
371 * @param string $string String used to indent ("\11", "\t", ' ', etc.).
372 * @access public
373 * @return void
375 function setTab($string)
377 $this->_tab = $string;
378 } // end func setTab
381 * Sets the HTML comment to be displayed at the beginning of the HTML string
383 * @param string
384 * @since 1.4
385 * @access public
386 * @return void
388 function setComment($comment)
390 $this->_comment = $comment;
391 } // end func setHtmlComment
394 * Returns the HTML comment
396 * @since 1.5
397 * @access public
398 * @return string
400 function getComment()
402 return $this->_comment;
403 } //end func getComment
406 * Abstract method. Must be extended to return the objects HTML
408 * @access public
409 * @return string
410 * @abstract
412 function toHtml()
414 return '';
415 } // end func toHtml
418 * Displays the HTML to the screen
420 * @access public
422 function display()
424 print $this->toHtml();
425 } // end func display
427 } // end class HTML_Common