html 智能检查,修复

      在采用富文本框添加信息时, 如果直接用复制粘贴时,如果没的复制完整或是其它的情况,有可能会导致html标签不配对, 如没有</table>,<div>等一些标签,当把这些信息直接在显示在页面中时, 如果页面同样使用了原信息中不配对的标签,则可能会导致页面混乱,故写此方法来检查标签配对是否完整,并自动补充未配对的标签.

ContractedBlock.gif ExpandedBlockStart.gif html check
 1
 2        public string AutoCheckHtml(string htmlStr)
 3ExpandedBlockStart.gifContractedBlock.gif        {
 4            string checkTags = "table,tbody,tr,td,p,div,span";
 5            Stack st = new Stack();
 6            Regex regStart = new Regex(@"<(?<tag>[\w]+)[\s\S]*?>");
 7            Regex regEnd = new Regex(@"<\/(?<tag>[\w]+)[\s\S]*?>");
 8
 9            StringBuilder sb = new StringBuilder();
10
11            string tagName;
12            foreach (string s in htmlStr.Split('<'))
13ExpandedSubBlockStart.gifContractedSubBlock.gif            {
14                // filter first string that have no '<'
15                if (sb.Length == 0)
16ExpandedSubBlockStart.gifContractedSubBlock.gif                {
17                    sb.Append(" ");
18                    continue;
19                }

20                Match m;
21                //recode start tag
22                if ((m = regStart.Match("<" + s)).Success && 
23                        (tagName = m.Groups["tag"].Value) != "" && 
24                         checkTags.IndexOf(tagName.ToLower()) != -1)
25ExpandedSubBlockStart.gifContractedSubBlock.gif                {
26                    st.Push(tagName);
27                }

28                //process end tag
29                else if ((m = regEnd.Match("<" + s)).Success && 
30                        (tagName = m.Groups["tag"].Value) != "" && 
31                        checkTags.IndexOf(tagName.ToLower()) != -1)
32ExpandedSubBlockStart.gifContractedSubBlock.gif                {
33                    //have no start tag
34                    if (st.Count == 0 || st.Peek().ToString() != tagName) 
35ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
36                        sb.AppendFormat("<{0}>\r\n", tagName);
37                    }

38                    else
39                        st.Pop(); 
40                }

41                sb.Append("<" + s);
42            }

43            //Add end tags
44            while (st.Count > 0)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            {
46                sb.AppendFormat("</{0}>\r\n", st.Pop());
47            }
 
48            return sb.ToString();
49        }
1. checkTags 是要检查的标签,列在其中的才会检查
2. regStart和regEnd   开始标签和节束标签的正则表达式(代码中随手所写,可能不能匹配一些情况,根据实际需要再作补充)

      另外一种相关的处理, 富文本的分页, 如直接根据字符串长度截取,可能会从标签中间截断,或者把开始标签和结尾标签分开,根据以上原理同样可写出智能分页的代码.呵呵.等有时间写出来了再贴上来!

转载于:https://www.cnblogs.com/hanf/archive/2009/08/16/1547020.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值