开发者

What’s a good HTML library for ASP.NET that supports replacing HTML5 tags with backwards-compatible equivalents?

开发者 https://www.devze.com 2023-02-27 01:09 出处:网络
I\'ve been running a regex string.replace for a while to change the HTML output for older browsers, replacing HTML specific tags with span/div tags via an HttpModule. Since IE6-8 won\'t recognize tags

I've been running a regex string.replace for a while to change the HTML output for older browsers, replacing HTML specific tags with span/div tags via an HttpModule. Since IE6-8 won't recognize tags such as article, footer and so on.

But when trying to 开发者_运维问答fix a bug in the regex pattern (and posting about it here) I found out that it's really NOT a good way to go about it, regex is not an HTML parser. (Read a great article by Jeff Atwood about it)

Since there's still a bug in my regex I'm starting to look for an alternate solution for the problem. I've used modernizr and I works great. But my problem is that some of the users of the application I'm building can't use JS and I still want the markup in HTML5 (for video mostly).

Have anyone done this without JS and regex? I've been looking for a HTML parser but haven't found one where I can replace tag names.


Have you tried the Html Agility Pack? That seems to be the de facto .NET library for manipulating HTML.


The two reasonably well known .NET parsers for HTML are the HTML Agility Pack and SGMLReader. I've not used either in anger, so can't offer any detailed advice on them.

The result of a parser however, is typically a DOM, and the DOM API does not provide a means to change the name of an element. So in each case where you want to convert, you'll need to:

  • Create a new div or span element
  • move the attributes from the HTML5 element to the new one
  • add any appropriate class name that you want to the class attribute (e.g. 'footer' or 'article')
  • move the child nodes of the HTML5 element to be children of the new one (preserving order)
  • replace the HTML5 element in its parent with the new element.


I’m not familiar with ASP.NET and C#, but you might want to look at XML libraries — they might accept your HTML5 as input even if it’s not strictly XML, and allow you to do the tag replacement you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消