<?php
/************************************************** 
 *                 Bot Gadu-Gadu                  * 
 ************************************************** 
 * Wersja: 2.4 Stable                             * 
 * Data: 29.08.2012 (15:36)                       * 
 * Autor: Jacek Kowalski (http://jacekk.info)     * 
 *                                                * 
 * Utwor rozprowadzany na licencji                * 
 * http://creativecommons.org/licenses/by-nc/2.5/ * 
 **************************************************/

class DOMHelper {
	static function ltrim($node) {
		while(($node->firstChild instanceof DOMElement) && $node->firstChild->tagName == 'br' && $node->firstChild->getAttribute('auto')=='1') {
			$node->removeChild($node->firstChild);
		}
		
		if($node->firstChild instanceof DOMElement) {
			self::ltrim($node->firstChild);
		}
	}
	
	static function rtrim($node) {
		while(($node->lastChild instanceof DOMElement) && $node->lastChild->tagName == 'br' && $node->lastChild->getAttribute('auto')=='1') {
			$node->removeChild($node->lastChild);
		}
		
		if($node->lastChild instanceof DOMElement) {
			self::rtrim($node->lastChild);
		}
	}
	
	static function trim($node) {
		self::ltrim($node);
		self::rtrim($node);
	}
	
	static function cloneNode($node, $saveto, $tag=NULL) {
		if($tag === NULL) {
			$tag = $node->tagName;
		}
		
		$saveto = $saveto->ownerDocument->createElement($tag);
		
		foreach($node->attributes as $attr) {
			if($attr->name == 'color' || $attr->name == 'style' || $attr->name == 'src') {
				$saveto->setAttributeNode($saveto->ownerDocument->importNode($attr, TRUE));
			}
		}
		
		return $saveto;
	}
	
	static function insertElement($tag, $node) {
		$tag = $node->ownerDocument->createElement($tag);
		$tag->setAttribute('auto', '1');
		$node->appendChild($tag);
	}
}
?>