写这个的愿意是要从浏览器获得福利,利用浏览器容错的能力得到无错标准的HTML流
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
var emptyString = "";
var replaceFlag = "[firefox_thinkernote_pugin]";
var cssArray = new Array();
function elementToString(element)
{
function getScript(element)
{
/*
if(element.childNodes.length == 0)
return "<script " + tagAttrs + "></script>\n";
else
return "<script " + tagAttrs + ">"+ replaceFlag +"</script>\n";
*/
return "";
}
function replaceTag(element,tagAttrs)
{
if(tagName != "")
return ""
if(element.childNodes.length == 0)
return "<div " + tagAttrs + "></div>\n";
else
return "<div " + tagAttrs + ">"+ replaceFlag +"</div>\n";
}
function getAttriblesOfTag(element)
{
var attrs = emptyString;
for(i=0;i<element.attributes.length;i++)
{
attrs += ' ' + element.attributes[i].name + '="' + element.attributes[i].value +'"'
}
return attrs;
}
function getLinks(element)
{
function insertCss(content)
{
s = "<style>"+content+"</style>\n";
cssArray.push(s);
}
var attrs = emptyString;
for(i=0;i<element.attributes.length;i++)
{
if(element.attributes[i].name=="href")
{
href = element.attributes[i].value;
if(href.indexOf("http://")==-1)
{
alert("np");
}
get(href,insertCss,"text",false)
}
}
return "";
}
function getTag(element)
{
tagName = element.tagName.toLowerCase();
if (tagName == "script")
return getScript(element);
if (tagName == "link")
return getLinks(element);
tagAttrs = getAttriblesOfTag(element);
if(element.childNodes.length == 0)
//return "<"+tagName+ "" + tagAttrs + " />\n";
return "<"+tagName + tagAttrs + "></"+ tagName +">\n";
else
return "<"+tagName + tagAttrs + ">"+replaceFlag+"</"+ tagName +">\n";
}
function getText(element)
{
return element.nodeValue.replace("\n","");
}
switch(element.nodeType)
{
case ELEMENT_NODE:
return getTag(element)
case TEXT_NODE:
return getText(element);
}
return "";
}
function freachTree(element)
{
var html = emptyString;
html = elementToString(element);
if(element.childNodes.length > 0)
{
var innerhtml = emptyString;
for(var i =0 ;i< element.childNodes.length;i++)
{
innerhtml += freachTree(element.childNodes[i]);
}
html = html.replace(replaceFlag,innerhtml);
}
return html;
}
function domToHtml(document)
{
htmlHead = freachTree(document.head);
htmlBody = freachTree(document.body);
html = '<!Doctype html><html xmlns="http://www.w3.org/1999/xhtml">'+htmlHead+"\n"+htmlBody+"</html>";
/*
var s = '';
for(i=0;i<cssArray.length;i++)
{
s += cssArray[i];
}
if(s.length>0)
{
html = html.replace("</head>",s+"</head>")
}
*/
return html;
}