Documentation is available at HTMLelement.php
- <?php
- /*
- Stolen from the web site of Jusin Watt
- http://www.justinsomnia.org
- Instructions by Ryan:
- HTML tags are instanitated with three parameters, the tag name, an initial value and the is_empty bool
- the is_empty bool is used to specify tags such as <br /> or <img /> in which no closing tag is used
- content OR nested tags can be added with the add_element function, if adding tags simply pass another HTMLElement
- the init_val constructor paramater allows an element to be instantiated with a child element, so for example
- you could create a <p> element with text thus:
- $text = new HTMLElement( 'p', 'sample text' );
- you could also use HTMLElements as the initial var, so if you wanted to create a <p> element nested in a <div> element
- you could do so thus:
- $layer = new HTMLElement( 'div', new HTMLElement( 'p', 'sample text' ) );
- http://www.pis.org
- */
- class HTMLElement
- {
- var $element_name; //the html tag of the element (div, p, form, etc)
- var $attributes; //any attributes to be included in the tag (style, onclick, etc) in the form "array( attribute => value )"
- var $childNodes;
- var $parentNode; //reference to the element's parent
- //changed by Ryan: additional parameter added allowing a new element to be created with child elements
- //this can be used for example to create a span element with text in a single call: $text = new HTMLElement( 'span', 'some text' )
- function HTMLElement( $element_name = "" )
- {
- $this->element_name = $element_name;
- $this->attributes = array();
- $this->childNodes = array();
- }
- function setAttribute($name, $value, $merge = true)
- {
- //$val = $value;
- if( $merge ) {
- $value = $this->CSSmergeParams( $this->attributes[$name], $value );
- }
- $this->attributes[$name] = $value;
- }
- function CSSmergeParams( $existing, $new ) {
- //split the value into paramater name/value pairs
- foreach( $this->CSStoArray($new) as $param ) {
- if( is_array($param) ) {
- $start = strpos( $existing, $param['name'] );
- if( $start !== false ) {
- //merge existing parameters
- //copy the existing attribute from start to the end of the attribute name and ':'
- $head = substr( $existing, 0, (1+$start+strlen($param['name']) ) );
- //copy the existing attribute from the ';' on
- $tail = substr( $existing, (1+$start+strlen($name) ) );
- $tail = substr( $tail, strpos( $tail, ';' ) );
- //$value = $head.$value.$tail;
- //$val = $value.$this->attributes[$name];
- $existing = $head.$param['value'].$tail;
- } else {
- //add new parameters
- $existing .= $param['name'].':'.$param['value'].';';
- }
- } else {
- $existing = $param;
- }
- }
- return $existing;
- }
- function CSStoArray( $string ) {
- $parameters = explode( ';', $string );
- if( count( $parameters ) > 1 ) {
- foreach( $parameters as $param ) {
- if( count( $NVPair = explode( ':', $param ) ) == 2 ) {
- $returnArray[] = array( 'name'=>$NVPair[0], 'value'=>$NVPair[1] );
- } else if( $param ) {
- $returnArray[] = $param;
- }
- }
- return $returnArray;
- } else {
- return array($string);
- }
- }
- //added by Ryan: allows an array of parameters to be passed and set as attributes
- function setAttributes( $paramArray, $merge = true ) {
- if( is_array( $paramArray ) ) {
- foreach( $paramArray as $key => $param ) {
- $this->setAttribute( $key, $param, $merge );
- }
- }
- }
- function getAttribute($name)
- {
- return $this->attributes[$name];
- }
- //added by Ryan: to be used in place of the position parameter in appendChild
- function &insertBefore( $element, $position = 0 ) {
- return $this->appendChild( $element, $position );
- }
- //changed by Ryan: returns a reference and takes a position parameter allowing the new element to be added at a specific location in the array
- function &appendChild( $element, $position = NULL )
- {
- if( $element )
- {
- if( !isset( $position ) ) {
- array_push( $this->childNodes, $element );
- end( $this->childNodes );
- $key = key( $this->childNodes );
- } else {
- $this->childNodes = array_merge( array_slice( $this->childNodes, 0, $position ), array( $position => $element ), array_slice( $this->childNodes, $position ) );
- $key = $position;
- }
- //added by Ryan: sets a reference to this in the child object's "parentNode" variable for tree traversal
- //note that if the child element is already part of another tree structure that relationship will be lost
- if ( is_subclass_of($element, "HTMLElement") || get_class($element) == "htmlelement" ) {
- $this->childNodes[$key]->set_parent( $this );
- }
- //added by Ryan: allows modification of elements by returning a reference to the newly added element
- return $this->childNodes[$key];
- }
- }
- //added by Ryan: sets the element's parent property for tree traversal
- //NOTE: this is a private function
- function set_parent( &$parent ) {
- $this->parentNode =& $parent;
- }
- //added by Ryan: returns a reference to this element's parent element
- function &getParentNode() {
- if( isset( $this->parentNode ) ) {
- return $this->parentNode;
- }
- }
- function &getElementById( $id ) {
- if( $id ) {
- if( $this->attributes['id'] == $id ) { return $this; }
- foreach( $this->childNodes as $key => $child ) {
- if( is_object( $child ) ) {
- if( $child->attributes['id'] == $id ) { return $this->childNodes[$key]; }
- else if( $ret =& $child->getElementById($id) ) { return $ret; }
- }
- }
- }else{
- echo 'error: no id specified for element search';
- }
- return false;
- }
- function getElementsByTagName( $tag ) {
- $returnArray = array();
- if( $tag && is_array($this->childNodes) ) {
- foreach( $this->childNodes as $key => $child ) {
- if( $child->element_name == $tag ) { $returnArray[] =& $this->childNodes[$key]; }
- $returnArray = array_merge( $returnArray, $child->getElementsByTagName( $tag ) );
- }
- }
- return $returnArray;
- }
- //added by Ryan: returns an array of references to all the element's child elements
- /*function get_elements() {
- foreach( $this->childNodes as $element ) {
- $return[ key($element) ] =& $element;
- }
- }
- //added by Ryan: returns a reference to the element specified by $key
- function &get_element( $key ) {
- if( isset( $this->childNodes[ $key ] ) ) {
- return $this->childNodes[ $key ];
- }
- }
- //added by Ryan: allows elements to be changed after being created
- function set_element( $key, $element ) {
- if( isset( $this->childNodes[ $key ] ) ) {
- $this->childNodes[ $key ] = $element;
- } else {
- $this->add_element( $elemenet );
- }
- }
- //added by Ryan: allows elements to be deleted after being created
- function removeChild( &$child ) {
- if( isset( $this->childNodes[ $key ] ) ) {
- unset( $this->childNodes[ $key ] );
- return true;
- }
- }
- //added by Ryan: returns an array of references to any child elements with with the specified attribute and value
- //if elements are given names, this would be useful for getting access to the element with a given name
- function search( $attribute, $value = NULL ) {
- //search this element's attributes
- if( $this->attributes[$attribute] ) {
- if( isset( $value ) ) {
- if( $this->attributes[$attribute] == $value ) {
- //if a value is requested and this element has an attribute with that value
- //add a reference to this object to the return array
- $return[] =& $this;
- }
- } else {
- //if a value isn't specified but this element has the requested attribute
- //add a reference to this object to the return array
- $return[] =& $this;
- }
- }
- //search any child elements for the attribute
- foreach( $this->childNodes as $element ) {
- if ( !(is_subclass_of($element, "HTMLElement") || get_class($element) == "htmlelement" ) ) {
- if( $found = $element->search( $attribute, $value ) ) {
- $return = array_merge( $return, $found );
- }
- }
- }
- return $return;
- }
- */
- function to_html()
- {
- $result = "";
- // create attribute name="value" pairs
- $attributes = " ";
- foreach ($this->attributes as $attribute => $value)
- {
- if ($value == "")
- {
- $attributes .= $attribute . " " ;
- }
- else
- {
- $attributes .= $attribute . "=\"" . htmlentities($value, ENT_COMPAT) . "\" ";
- }
- }
- // create start tag
- $result = "\n<" . $this->element_name . rtrim($attributes);
- // end start tag the xhtml way
- //Changed by Ryan: outputs single tag if no child elements are specified
- if (!count( $this->childNodes) || $is_empty )
- {
- $result .= " />\n";
- }
- else // include children elements followed by an end tag
- {
- $result .= ">";
- // create html between start and end tags
- $inner_html = "";
- foreach($this->childNodes as $element)
- {
- if (is_subclass_of($element, "HTMLElement") || get_class($element) == "htmlelement")
- {
- $inner_html .= $element->to_html();
- }
- else
- {
- $inner_html .= $element;
- }
- }
- $result .= $inner_html . "</" . $this->element_name . ">";
- }
- return $result;
- }
- }
- ?>
Documentation generated on Tue, 24 May 2005 03:58:25 -0400 by phpDocumentor 1.3.0RC3