技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

我自己写的domtohtml

写这个的愿意是要从浏览器获得福利,利用浏览器容错的能力得到无错标准的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;
}

 

posted on   codestyle  阅读(361)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示