C# 修正不成對的HTML標籤

  • 3243
  • 0

摘要:C# 修正不成對的HTML標籤

前端 jQuery接到 html字串就直接用 $(html)轉成 jQuery物件,如果 html標籤不成對,在 IE會壞掉,FIREFOX跟 CHROME可以動,但顯示效果不對…

在 Render處理 html:


        protected override void Render(HtmlTextWriter writer)
        {
            if (Request["control"]!=null)
            {
                string control = Request["control"];
                string[] controls = control.Split(new char[] { ',' });
                if (controls.Length > 0)
                {
                    System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                    foreach (string c in controls)
                    {
                        div.Controls.Add(Page.FindControl(c));
                    }
                    div.RenderControl(writer);
                }
                else
                {
                    Page.FindControl(Request["control"]).RenderControl(writer);
                }
                return;
            }
            base.Render(writer);
        }
有問題的 html:……噗,點部落會自己修正不成對的 html tag,貼不上來XD

第一個想到在後端用正規式硬來… 但是只弄出 (?is)<(?<tag>\w+)[^>]*>(?!.*<\/\1[^>]*>),這個在巢狀結構沒辦法找出不完整的標籤…例如:[div][div][/div],中間的 div起始標籤會被當成第一個 div的內容,但實際上是第一個 div少了結束標籤…想不到怎麼做,還請指教。

第二個:http://htmlagilitypack.codeplex.com/


//using HtmlAgilityPack;
        protected override void Render(HtmlTextWriter writer)
        {
            if (Request["control"]!=null)
            {
                string control = Request["control"];
                string[] controls = control.Split(new char[] { ',' });
                if (controls.Length > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    StringWriter tw = new StringWriter(sb);
                    HtmlTextWriter hw = new HtmlTextWriter(tw);
                    System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                    foreach (string c in controls)
                    {
                        div.Controls.Add(Page.FindControl(c));
                    }
                    div.RenderControl(hw);
                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(sb.ToString());
                    doc.Save(writer);
                }
                else
                {
                    Page.FindControl(Request["control"]).RenderControl(writer);
                }
                return;
            }
            base.Render(writer);
        }

第三個:http://markupsanitizer.codeplex.com/


        protected override void Render(HtmlTextWriter writer)
        {
            if (Request["control"]!=null)
            {
                string control = Request["control"];
                string[] controls = control.Split(new char[] { ',' });
                if (controls.Length > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    StringWriter tw = new StringWriter(sb);
                    HtmlTextWriter hw = new HtmlTextWriter(tw);
                    System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                    foreach (string c in controls)
                    {
                        div.Controls.Add(Page.FindControl(c));
                    }

                    div.RenderControl(hw);
                    writer.Write(MarkupSanitizer.Sanitizer.SanitizeMarkup(sb.ToString()).MarkupText);
                }
                else
                {
                    Page.FindControl(Request["control"]).RenderControl(writer);
                }
                return;
            }
            base.Render(writer);
        }

最後採用第二個方法,第三個太乾淨了。