Source for file HTMLelement.php

Documentation is available at HTMLelement.php

  1. <?php
  2.  
  3. /*
  4. Stolen from the web site of Jusin Watt
  5.  
  6.  
  7.  
  8. http://www.justinsomnia.org
  9.  
  10.  
  11.  
  12. Instructions by Ryan:
  13.  
  14. HTML tags are instanitated with three parameters, the tag name, an initial value and the is_empty bool
  15.  
  16. the is_empty bool is used to specify tags such as <br /> or <img /> in which no closing tag is used
  17.  
  18. content OR nested tags can be added with the add_element function, if adding tags simply pass another HTMLElement
  19.  
  20.  
  21.  
  22. the init_val constructor paramater allows an element to be instantiated with a child element, so for example
  23.  
  24. you could create a <p> element with text thus:
  25.  
  26.  
  27.  
  28. $text = new HTMLElement( 'p', 'sample text' );
  29.  
  30.  
  31.  
  32. you could also use HTMLElements as the initial var, so if you wanted to create a <p> element nested in a <div> element
  33.  
  34. you could do so thus:
  35.  
  36.  
  37.  
  38. $layer = new HTMLElement( 'div', new HTMLElement( 'p', 'sample text' ) );
  39.  
  40.  
  41.  
  42. http://www.pis.org
  43.  
  44.  
  45.  
  46. */
  47.  
  48.  
  49.  
  50.  
  51.  
  52. class HTMLElement
  53.  
  54. {
  55.  
  56. var $element_name; //the html tag of the element (div, p, form, etc)
  57.  
  58.  
  59. var $attributes; //any attributes to be included in the tag (style, onclick, etc) in the form "array( attribute => value )"
  60.  
  61.  
  62. var $childNodes;
  63.  
  64. var $parentNode; //reference to the element's parent
  65.  
  66.  
  67. //changed by Ryan: additional parameter added allowing a new element to be created with child elements
  68.  
  69. //this can be used for example to create a span element with text in a single call: $text = new HTMLElement( 'span', 'some text' )
  70.  
  71.  
  72. function HTMLElement( $element_name = "" )
  73.  
  74. {
  75.  
  76. $this->element_name = $element_name;
  77.  
  78. $this->attributes = array();
  79.  
  80. $this->childNodes = array();
  81.  
  82. }
  83.  
  84.  
  85. function setAttribute($name, $value, $merge = true)
  86.  
  87. {
  88.  
  89. //$val = $value;
  90.  
  91. if( $merge ) {
  92.  
  93. $value = $this->CSSmergeParams( $this->attributes[$name], $value );
  94.  
  95. }
  96.  
  97. $this->attributes[$name] = $value;
  98.  
  99. }
  100.  
  101.  
  102. function CSSmergeParams( $existing, $new ) {
  103.  
  104. //split the value into paramater name/value pairs
  105.  
  106. foreach( $this->CSStoArray($new) as $param ) {
  107.  
  108. if( is_array($param) ) {
  109.  
  110. $start = strpos( $existing, $param['name'] );
  111.  
  112.  
  113.  
  114. if( $start !== false ) {
  115.  
  116. //merge existing parameters
  117.  
  118.  
  119.  
  120. //copy the existing attribute from start to the end of the attribute name and ':'
  121.  
  122. $head = substr( $existing, 0, (1+$start+strlen($param['name']) ) );
  123.  
  124.  
  125. //copy the existing attribute from the ';' on
  126.  
  127. $tail = substr( $existing, (1+$start+strlen($name) ) );
  128.  
  129. $tail = substr( $tail, strpos( $tail, ';' ) );
  130.  
  131.  
  132. //$value = $head.$value.$tail;
  133.  
  134. //$val = $value.$this->attributes[$name];
  135.  
  136. $existing = $head.$param['value'].$tail;
  137.  
  138. } else {
  139.  
  140.  
  141. //add new parameters
  142.  
  143. $existing .= $param['name'].':'.$param['value'].';';
  144.  
  145. }
  146.  
  147. } else {
  148.  
  149. $existing = $param;
  150.  
  151. }
  152.  
  153. }
  154.  
  155.  
  156. return $existing;
  157.  
  158. }
  159.  
  160.  
  161. function CSStoArray( $string ) {
  162.  
  163. $parameters = explode( ';', $string );
  164.  
  165. if( count( $parameters ) > 1 ) {
  166.  
  167. foreach( $parameters as $param ) {
  168.  
  169. if( count( $NVPair = explode( ':', $param ) ) == 2 ) {
  170.  
  171. $returnArray[] = array( 'name'=>$NVPair[0], 'value'=>$NVPair[1] );
  172.  
  173. } else if( $param ) {
  174.  
  175. $returnArray[] = $param;
  176.  
  177. }
  178.  
  179. }
  180.  
  181. return $returnArray;
  182.  
  183. } else {
  184.  
  185. return array($string);
  186.  
  187. }
  188.  
  189. }
  190.  
  191.  
  192. //added by Ryan: allows an array of parameters to be passed and set as attributes
  193.  
  194.  
  195. function setAttributes( $paramArray, $merge = true ) {
  196.  
  197. if( is_array( $paramArray ) ) {
  198.  
  199. foreach( $paramArray as $key => $param ) {
  200.  
  201. $this->setAttribute( $key, $param, $merge );
  202.  
  203. }
  204.  
  205. }
  206.  
  207. }
  208.  
  209.  
  210. function getAttribute($name)
  211.  
  212. {
  213.  
  214. return $this->attributes[$name];
  215.  
  216. }
  217.  
  218.  
  219. //added by Ryan: to be used in place of the position parameter in appendChild
  220.  
  221.  
  222. function &insertBefore( $element, $position = 0 ) {
  223.  
  224. return $this->appendChild( $element, $position );
  225.  
  226. }
  227.  
  228.  
  229. //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
  230.  
  231.  
  232. function &appendChild( $element, $position = NULL )
  233.  
  234. {
  235.  
  236. if( $element )
  237.  
  238. {
  239.  
  240. if( !isset( $position ) ) {
  241.  
  242. array_push( $this->childNodes, $element );
  243.  
  244. end( $this->childNodes );
  245.  
  246. $key = key( $this->childNodes );
  247.  
  248. } else {
  249.  
  250. $this->childNodes = array_merge( array_slice( $this->childNodes, 0, $position ), array( $position => $element ), array_slice( $this->childNodes, $position ) );
  251.  
  252. $key = $position;
  253.  
  254. }
  255.  
  256.  
  257. //added by Ryan: sets a reference to this in the child object's "parentNode" variable for tree traversal
  258.  
  259. //note that if the child element is already part of another tree structure that relationship will be lost
  260.  
  261. if ( is_subclass_of($element, "HTMLElement") || get_class($element) == "htmlelement" ) {
  262.  
  263. $this->childNodes[$key]->set_parent( $this );
  264.  
  265. }
  266.  
  267.  
  268. //added by Ryan: allows modification of elements by returning a reference to the newly added element
  269.  
  270. return $this->childNodes[$key];
  271.  
  272. }
  273.  
  274. }
  275.  
  276.  
  277. //added by Ryan: sets the element's parent property for tree traversal
  278.  
  279. //NOTE: this is a private function
  280.  
  281.  
  282. function set_parent( &$parent ) {
  283.  
  284. $this->parentNode =& $parent;
  285.  
  286. }
  287.  
  288.  
  289. //added by Ryan: returns a reference to this element's parent element
  290.  
  291.  
  292. function &getParentNode() {
  293.  
  294. if( isset( $this->parentNode ) ) {
  295.  
  296. return $this->parentNode;
  297.  
  298. }
  299.  
  300. }
  301.  
  302.  
  303. function &getElementById( $id ) {
  304.  
  305. if( $id ) {
  306.  
  307. if( $this->attributes['id'] == $id ) { return $this; }
  308.  
  309. foreach( $this->childNodes as $key => $child ) {
  310.  
  311. if( is_object( $child ) ) {
  312.  
  313. if( $child->attributes['id'] == $id ) { return $this->childNodes[$key]; }
  314.  
  315. else if( $ret =& $child->getElementById($id) ) { return $ret; }
  316.  
  317. }
  318.  
  319. }
  320.  
  321. }else{
  322.  
  323. echo 'error: no id specified for element search';
  324.  
  325. }
  326.  
  327. return false;
  328.  
  329. }
  330.  
  331.  
  332. function getElementsByTagName( $tag ) {
  333.  
  334. $returnArray = array();
  335.  
  336. if( $tag && is_array($this->childNodes) ) {
  337.  
  338. foreach( $this->childNodes as $key => $child ) {
  339.  
  340. if( $child->element_name == $tag ) { $returnArray[] =& $this->childNodes[$key]; }
  341.  
  342. $returnArray = array_merge( $returnArray, $child->getElementsByTagName( $tag ) );
  343.  
  344. }
  345.  
  346. }
  347.  
  348. return $returnArray;
  349.  
  350. }
  351.  
  352.  
  353. //added by Ryan: returns an array of references to all the element's child elements
  354.  
  355. /*function get_elements() {
  356.  
  357. foreach( $this->childNodes as $element ) {
  358.  
  359. $return[ key($element) ] =& $element;
  360.  
  361. }
  362.  
  363. }
  364.  
  365.  
  366. //added by Ryan: returns a reference to the element specified by $key
  367.  
  368. function &get_element( $key ) {
  369.  
  370. if( isset( $this->childNodes[ $key ] ) ) {
  371.  
  372. return $this->childNodes[ $key ];
  373.  
  374. }
  375.  
  376. }
  377.  
  378.  
  379.  
  380. //added by Ryan: allows elements to be changed after being created
  381.  
  382. function set_element( $key, $element ) {
  383.  
  384. if( isset( $this->childNodes[ $key ] ) ) {
  385.  
  386. $this->childNodes[ $key ] = $element;
  387.  
  388. } else {
  389.  
  390. $this->add_element( $elemenet );
  391.  
  392. }
  393.  
  394. }
  395.  
  396.  
  397. //added by Ryan: allows elements to be deleted after being created
  398.  
  399. function removeChild( &$child ) {
  400.  
  401. if( isset( $this->childNodes[ $key ] ) ) {
  402.  
  403. unset( $this->childNodes[ $key ] );
  404.  
  405. return true;
  406.  
  407. }
  408.  
  409. }
  410.  
  411.  
  412. //added by Ryan: returns an array of references to any child elements with with the specified attribute and value
  413.  
  414. //if elements are given names, this would be useful for getting access to the element with a given name
  415.  
  416. function search( $attribute, $value = NULL ) {
  417.  
  418. //search this element's attributes
  419.  
  420. if( $this->attributes[$attribute] ) {
  421.  
  422. if( isset( $value ) ) {
  423.  
  424. if( $this->attributes[$attribute] == $value ) {
  425.  
  426. //if a value is requested and this element has an attribute with that value
  427.  
  428. //add a reference to this object to the return array
  429.  
  430. $return[] =& $this;
  431.  
  432. }
  433.  
  434. } else {
  435.  
  436. //if a value isn't specified but this element has the requested attribute
  437.  
  438. //add a reference to this object to the return array
  439.  
  440. $return[] =& $this;
  441.  
  442. }
  443.  
  444. }
  445.  
  446.  
  447. //search any child elements for the attribute
  448.  
  449. foreach( $this->childNodes as $element ) {
  450.  
  451. if ( !(is_subclass_of($element, "HTMLElement") || get_class($element) == "htmlelement" ) ) {
  452.  
  453. if( $found = $element->search( $attribute, $value ) ) {
  454.  
  455. $return = array_merge( $return, $found );
  456.  
  457. }
  458.  
  459. }
  460.  
  461. }
  462.  
  463. return $return;
  464.  
  465. }
  466.  
  467. */
  468.  
  469. function to_html()
  470.  
  471. {
  472.  
  473. $result = "";
  474.  
  475.  
  476. // create attribute name="value" pairs
  477.  
  478. $attributes = " ";
  479.  
  480. foreach ($this->attributes as $attribute => $value)
  481.  
  482. {
  483.  
  484. if ($value == "")
  485.  
  486. {
  487.  
  488. $attributes .= $attribute . " " ;
  489.  
  490. }
  491.  
  492. else
  493.  
  494. {
  495.  
  496. $attributes .= $attribute . "=\"" . htmlentities($value, ENT_COMPAT) . "\" ";
  497.  
  498. }
  499.  
  500. }
  501.  
  502. // create start tag
  503.  
  504. $result = "\n<" . $this->element_name . rtrim($attributes);
  505.  
  506.  
  507.  
  508. // end start tag the xhtml way
  509.  
  510. //Changed by Ryan: outputs single tag if no child elements are specified
  511.  
  512. if (!count( $this->childNodes) || $is_empty )
  513.  
  514. {
  515.  
  516. $result .= " />\n";
  517.  
  518. }
  519.  
  520. else // include children elements followed by an end tag
  521.  
  522. {
  523.  
  524. $result .= ">";
  525.  
  526.  
  527. // create html between start and end tags
  528.  
  529. $inner_html = "";
  530.  
  531. foreach($this->childNodes as $element)
  532.  
  533. {
  534.  
  535. if (is_subclass_of($element, "HTMLElement") || get_class($element) == "htmlelement")
  536.  
  537. {
  538.  
  539. $inner_html .= $element->to_html();
  540.  
  541. }
  542.  
  543. else
  544.  
  545. {
  546.  
  547. $inner_html .= $element;
  548.  
  549. }
  550.  
  551. }
  552.  
  553.  
  554. $result .= $inner_html . "</" . $this->element_name . ">";
  555.  
  556. }
  557.  
  558.  
  559.  
  560. return $result;
  561.  
  562. }
  563.  
  564. }
  565.  
  566. ?>

Documentation generated on Tue, 24 May 2005 03:58:25 -0400 by phpDocumentor 1.3.0RC3