﻿// хорошие теги
goodTags0=["a","p","br","strong","b","em","i","tt","code","pre","ul","ol","li","img",
	"table","tbody","thead","tfoot","caption","tr","td","th","col","colgroup","h1","h2","h3","h4","h5","h6",
	"small","big","sub","sup","div"]

// теги, которые canHaveHTML() и могут быть пустыми.
canEmptyTags0=["td","th"]

goodAttributes0=[]
// хорошие атрибуты (допустимы у всех тегов)
goodAttributes0[""] = ["href", "target", "name", "title", "alt", "src", "id", "bgColor", "color", "class", "width", "height"]

// дополнительные допустимые атрибуты для отдельных тегов
goodAttributes0["img"] = ["border", "align"]
goodAttributes0["table"] = ["cellSpacing", "cellPadding", "border"] 
goodAttributes0["td"] = ["colSpan", "rowSpan"]
goodAttributes0["tr td col colgroup"] = ["noWrap", "align", "vAlign"]
goodAttributes0["a area"] = ["href", "name"]
goodAttributes0["br"] = ["clear"]

// однозначно убиваемые псевдо-атрибуты (отсутствуют в коллекции attributes)
mustDieAttributes=["x:str","x:num"] // внимание - в 6-ом MSIE убийство несуществующих атрибутов у тега TABLE ведет к краху браузера

// хорошие классы (не убиваем)
goodClasses0=["important","noindent","note","h1","h2","h3","h4","h5","h6"]

goodTags=[]
for(i in goodTags0){
	goodTags[goodTags0[i]]=true
}
canEmptyTags=[]
for(i in canEmptyTags0){
	canEmptyTags[canEmptyTags0[i]]=true
}

goodAttributes=[]
for(i in goodAttributes0){
	var splitted=i.split(" ")
	for(ii in splitted){
		goodAttributes[splitted[ii]]=[]		
		for(j in goodAttributes0[i]){
			goodAttributes[splitted[ii]][goodAttributes0[i][j]]=true
		}		
	}
}

goodClasses=[]
for(i in goodClasses0){
	goodClasses[goodClasses0[i]]=true
}
function cleanObject(o){
	var s=""
	var myAttributes=[]
	var i
	
	if(o.outerHTML.substr(0,2)=="<?"){
		o.removeNode(false)
		return
	}
	var tag=o.tagName.toLowerCase()

	// удаляем плохие, заказные и пустые теги
	if(!goodTags[tag] || (self.clean_a && tag=="a") || (self.clean_img && tag=="img") || (o.canHaveHTML && o.innerHTML=="" && !canEmptyTags[tag])){
		try{
			o.removeNode(false)
		}catch(e){}

		return
	}

	var a=o.attributes
	if(!a) return

	for(i in a){ // хм...
		if(""+a[i]!="null"){ // странно, но просто if(a[i]) не прокатывает
			myAttributes[i]=a[i]
		}
	}
	// киляем плохие атрибуты и плохие классы
	for(i in myAttributes){
		//i=i.toLowerCase()
		if( 
			(
				!goodAttributes[""][i] &&
				(
					!goodAttributes[tag] || !goodAttributes[tag][i] || i=="class" || i=="className"
				)
			)
			||
			(
				self.clean_colors &&
				(
					i=="bgColor" || i=="color"
				)
			)
			||
			(
				self.clean_aligns &&
				(
					i=="align" || i=="vAlign"
				)
			)
		){
			
			if(i=="class"){
				i="className"
			}
			if(i!="className" || !goodClasses[o.className]){
				o.removeAttribute(i)
			}
		}	
	}
	// в 6-ом MSIE не киляем непонятных атрибутов у таблиц. см. выше.
	if(o.tagName.toLowerCase()!="table"){
		for(i in mustDieAttributes){
			o.removeAttribute(mustDieAttributes[i])
		}
	}
	o.style.cssText=""
}


function cleanTree(o,mustClean){
	var c=o.children
	var i
	if(c){
		for(i=c.length-1;i>=0;i--){
			cleanTree(c[i],true)
		}
	}
	
	if(mustClean) cleanObject(o)
}

function cleanUpMsWordHTML(str){
	clean_a = false
	clean_img = false
	clean_colors = false
	clean_aligns = false
	
	if(str != ""){
		
		var buffer = document.createElement( "div" );

		buffer.innerHTML=str;

		cleanTree(buffer,false);
	
		if(buffer.innerHTML.substr(0,6)=="&nbsp;")
			buffer.innerHTML=buffer.innerHTML.substr(6)
	
		return buffer.innerHTML;
	}
}

function cleanUpHTMLPlaceholders()
{
	var wordCleanUp = document.getElementById('wordCleanUp');
	
	if ( wordCleanUp != null && wordCleanUp.checked )
	{
		var objects = document.getElementsByTagName('OBJECT');
		
		for ( i = 0; i < objects.length; i++ )
		{		
			if ( objects.item(i)['HTML'] != null && objects.item(i)['HTML'] != "" )
			{
				objects.item(i)['HTML'] = cleanUpMsWordHTML(objects.item(i)['HTML']);
			}
		}
	}
}