Asp.Net检查HTML是否闭合以及自动修复
1、htmlCheck类
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;
namespace htmlCheck
{
/// <summary>
/// Asp.Net检查HTML是否闭合以及自动修复 http://webapi.cnblogs.com/
/// </summary>
class TagsList
{
private ArrayList data; public int Size
{
get
{
return data.Count;
}
} public TagsList()
{
data = new ArrayList();
} public void add(String str)
{
data.Add(str);
} public string get(int index)
{
if (index < data.Count)
return (string)data[index];
else
return null;
} public bool remove(string str)
{
if (data.IndexOf(str) == -) return false;
data.Remove(str);
return true;
} public void remove(int index)
{
data.RemoveAt(index);
}
} public class TagsChecker
{
/// <summary>
/// 检查html标签是闭合
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool check(string str)
{
TagsList[] unclosedTags = getUnclosedTags(str); if (unclosedTags[].Size != )
{
return false;
}
for (int i = ; i < unclosedTags[].Size; i++)
{
if (unclosedTags[].get(i) != null)
return false;
} return true;
}
/// <summary>
/// 处理未闭合的html代码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string fix(String str)
{
StringBuilder fixeds = new StringBuilder(); // 存放修复后的字符串
TagsList[] unclosedTags = getUnclosedTags(str); // 生成新字符串
for (int i = unclosedTags[].Size - ; i > -; i--)
{
fixeds.Append("<" + unclosedTags[].get(i) + ">");
} fixeds.Append(str); for (int i = unclosedTags[].Size - ; i > -; i--)
{
String s = null;
if ((s = unclosedTags[].get(i)) != null)
{
fixeds.Append("</" + s + ">");
}
} return fixeds.ToString();
} private static TagsList[] getUnclosedTags(String str)
{
StringBuilder temp = new StringBuilder(); // 存放标签
TagsList[] unclosedTags = new TagsList[];
unclosedTags[] = new TagsList(); // 前不闭合,如有</div>而前面没有<div>
unclosedTags[] = new TagsList(); // 后不闭合,如有<div>而后面没有</div>
bool flag = false; // 记录双引号"或单引号'
char currentJump = ' '; // 记录需要跳过''还是"" char current = ' ', last = ' '; // 当前 & 上一个 // 开始判断
for (int i = ; i < str.Length; )
{
current = str[i++]; // 读取一个字符
if (current == '"' || current == '\'')
{
flag = flag ? false : true; // 若为引号,flag翻转
currentJump = current;
if (flag)
{
while (i < str.Length && str[i++] != currentJump)
; // 跳过引号之间的部分
flag = false;
}
}
else if (current == '<')
{ // 开始提取标签
current = str[i++];
if (current == '/')
{ // 标签的闭合部分,如</div>
current = str[i++]; // 读取标签
while (i < str.Length && current != '>')
{
temp.Append(current);
current = str[i++];
} // 从tags_bottom移除一个闭合的标签
if (!unclosedTags[].remove(temp.ToString()))
{ // 若移除失败,说明前面没有需要闭合的标签
unclosedTags[].add(temp.ToString()); // 此标签需要前闭合
}
temp.Remove(, temp.Length); // 清空temp
}
else
{ // 标签的前部分,如<div>
last = current;
while (i < str.Length && current != ' '
&& current != ' ' && current != '>')
{
temp.Append(current);
last = current;
current = str[i++];
} // 已经读取到标签,跳过其他内容,如<div id=test>跳过id=test
while (i < str.Length && current != '>')
{
last = current;
current = str[i++];
if (current == '"' || current == '\'')
{ // 判断双引号
flag = flag ? false : true;
currentJump = current;
if (flag)
{ // 若引号不闭合,跳过到下一个引号之间的内容
while (i < str.Length && str[i++] != currentJump)
;
current = str[i++];
flag = false;
}
}
}
if (last != '/' && current == '>') // 判断这种类型:<TagName />
unclosedTags[].add(temp.ToString());
temp.Remove(, temp.Length);
}
}
}
return unclosedTags;
}
}
}
2、使用方法
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using htmlCheck;//注意,添加这个引用
using System.Text.RegularExpressions; public partial class ceshi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str1 = "<p>tt<table><tr><td>webapi</td></tr><tr><td>323</td></p>";
string str2 = "tt<u>ss</u><div id=test name=\"<test>\"><a>fds</a></div>";
Response.Write("\r\n检查文本 " + str1);
Response.Write("\r\n结果:" + TagsChecker.check(str1));
Response.Write("\r\n检查文本 " + str2);
Response.Write("\r\n结果:" + TagsChecker.check(str2));
Response.Write("\r\n修复文本 " + str1);
Response.Write("\r\n结果:" + TagsChecker.fix(str1)); }
}
说明:转载的,已测试,确实闭合修复了,但是闭合的位置不正确
Asp.Net检查HTML是否闭合以及自动修复的更多相关文章
- asp.net 检查文件夹和文件是否存在
原文 asp.net 检查文件夹和文件是否存在 允许 path 参数指定相对或绝对路径信息. 相对路径信息被解释为相对于当前工作目录. 检查该目录是否存在之前,从 path 参数的末尾移除尾随空格. ...
- asp.net检查验证字符串是否为纯数字方法小结
原文 asp.net检查验证字符串是否为纯数字方法小结 在asp.net中验证字符串是不是为数字我们没有像php中那么多丰富的函数来直接使用,这里我整理了一些比较实例的验证字符串是否为纯数字方法代码 ...
- asp.net检查服务器上目录或文件是否存在示例
原文 asp.net检查服务器上目录或文件是否存在示例 asp.net为我们提供了文件系统对象了,对于目录与文件判断是否存在我们有System.IO.File.Exists与System.IO.Dir ...
- asp.net C#检查URL是否有效
我们有时候需要对用户输入的网站(URL)进行有效性检查, 代码如下 复制代码 function CheckUrl(str) { var RegUrl = new RegExp(); Re ...
- ASP.NET(转自wiki)
ASP.NET是由微软在.NET Framework框架中所提供,开发Web应用程序的类库,封装在System.Web.dll文件中,显露出System.Web名字空间,并提供ASP.NET网页处理. ...
- .net学习笔记----Asp.net的生命周期之一应用程序生命周期
Http请求刚刚到达服务器的时候 当服务器接收到一个 Http请求的时候,IIS (Internet Information Services,互联网信息服务)首先需要决定如何去处理这个请求. 什么是 ...
- ASP连接access 数据库的增删改查 - imsoft.cnblogs
假设数据库文件名叫data.mdb里面有2个表:1.admin2.news假设admin是保存用户名和密码,里面有字段:UserName,PassWord.假设我们要在判断一个用户名叫name,密码是 ...
- ASP.NET 应用程序生命周期概述[转自MSDN]
本文转自:http://msdn.microsoft.com/zh-cn/library/ms178473(VS.80).aspx 下表描述了 ASP.NET 应用程序生命周期的各个阶段. 阶段 ...
- asp.net 文件上传示例整理
ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录. 代码如下 复制代码 ...
随机推荐
- 打造强大的BaseModel(1):让Model自我描述
前言 从事iOS开发已经两年了,从一无所知到现在能独立带领团队完成一系列APP的开发,网络上的大神给了我太多的帮助.他们无私地贡献自己的心得和经验,写出了一篇篇精美的文章.现在我也开始为大家贡献自己的 ...
- Android View的绘制机制流程深入详解(一)
本系列文章主要着重深入介绍Android View的绘制机制及流程,第一篇主要介绍并分析LayoutInflater的原理, 从而理解setContentView的加载原理.对于LayoutInfla ...
- DHCP服务自动分配IP地址原理
转载自:http://blog.csdn.net/lycb_gz/article/details/8499559 DHCP在提供服务时,DHCP客户端是以UDP 68号端口进行数据传输的,而DHCP服 ...
- Windows下性能最好的I/O模型——完成端口
I/O模型--完成端口 设计目的: 常见的网络通信分为两种:同步和异步. 在同步通信中,每一次接受数据都会导致主线程的挂起,从而阻塞住了其他操作.为了解决这一问题,我们通常会采取同步通信+多线程的策略 ...
- Verilog-FPGA硬件电路设计之一——if语句优先级(always块中的阻塞赋值生成的组合逻辑电路是按照顺利执行的)
出处:http://bbs.ednchina.com/BLOG_ARTICLE_3013262.HTM 综合软件:Quartus II 一.有优先级的if语句 if..else if.. else i ...
- 【转】android adb命令
1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): andr ...
- JavaScript高级程序设计(第三版)学习笔记8、9、10章
第8章,BOM BOM的核心对象是window,具有双重角色,既是js访问浏览器的一个接口,又是ECMAScript规定的Global对象.因此,在全局作用域中声明的函数.变量都会变成window对象 ...
- 【转】MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
[转]MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据, ...
- ASP大数据量使用GetRows()提升速度
抽取10万条数据,Access数据库,GetRows() 现有10W条数据,Access数据库保存 通过正常提取: <% Set conn= Server.CreateObject(" ...
- JavaScript高级程序设计(九):基本概念----语句的特殊点
一.Label语句.break/continue语句和for循环语句的结合使用: 1.Label语句可以在代码中添加标签,以便将来使用.语法: label:statment eg: start:f ...