/*	
	DOM to XHTML String Parser v0.2 <http://www.jamesv.org/>
	Copyright 2007 James Vreeland
	This software is licensed under CC-Attribution <http://creativecommons.org/licenses/by/3.0/>
*/


//List of tags that need to be self closed
var self_closing_tag_names = [];
	self_closing_tag_names["br"] = true;
	self_closing_tag_names["hr"] = true;
	self_closing_tag_names["link"] = true;
	self_closing_tag_names["meta"] = true;
	self_closing_tag_names["input"] = true;
	self_closing_tag_names["img"] = true;


//List of attributes that get more or less made up by browser and should be stripped
var attr_ignore_checks = [];
	attr_ignore_checks["names"] = [];
	attr_ignore_checks["names"]["contentEditable"] = true;
	attr_ignore_checks["names"]["start"] = true;
	attr_ignore_checks["names"]["loop"] = true;
	attr_ignore_checks["names"]["tabIndex"] = true;
	
	attr_ignore_checks["values"] = [];
	attr_ignore_checks["values"][""] =  true;
	attr_ignore_checks["values"]["false"] =  true;
	attr_ignore_checks["values"][null] =  true;

/*
	Converts single XHTML node into string
*/
function parseNode(node, omit_outer_node) {
  if(node){
  	var node_value = "";
  	//Is the tag one that should be self closing?
  	if(self_closing_tag_names[node.tagName.toLowerCase()]) {
  		if(!omit_outer_node) {
  			node_value += '<' + node.tagName.toLowerCase()  + parseAttributes(node) + ' />';
  		}
  	} else {
  		if(!omit_outer_node) {
  			node_value += '<' + node.tagName.toLowerCase()  + parseAttributes(node) + '>';
  		}

  		for( var x = 0; x < node.childNodes.length; x++ ) {
  			//Find all contained Tags and parse them accordingly
  			if(node.childNodes[x].nodeName != "#text" && node.childNodes[x].nodeName != "#comment" && node.childNodes[x].nodeName != "#cdata-section") {
  				node_value += parseNode(node.childNodes[x]);
  			} else {
  			//Find and add all Text/Comment nodes
  				node_value += node.childNodes[x].nodeValue;
  			}
  		}
  		if(!omit_outer_node) {
  			node_value += "</" + node.tagName.toLowerCase() + ">";
  		}
  	}
  	return node_value;
  }
}

/*
	Loops through all attributes for a node and returns a cleaned string of pertinent values
*/
function parseAttributes(node) {
	var node_attributes = "";
	
	for( var x = 0; x < node.attributes.length; x++ ) {
		
		//Is this attribute name or value in the ignore list?
		if(!attr_ignore_checks["names"][node.attributes[x].nodeName] && !attr_ignore_checks["values"][node.attributes[x].nodeValue]) {
			node_attributes +=  ' ' + node.attributes[x].nodeName + '="' + node.attributes[x].nodeValue + '"';
		}
	}
	
	return node_attributes;
}

