Domanda

I am working on a project in Visual Studio 2012 that is littered with deprecated HTML markup. This results in a great many Visual Studio warnings. The vast number of these are a result of the extensive usage of the align attribute tag.

<td align="Left">
....

So I'm literally seeing hundreds of the following warnings:

Validation (XHTML5): Attribute 'align' is not a valid attribute of element 'td'.    

What would be the best way to perform a site-wide update to convert this to an inline CSS style? I've experimented with a find-replace regex but some of the elements already have style definitions so the regex approach would cause duplicate style tags.

Is there a Visual Studio extension that will automate this cleanup operation for me? I've looked at ReSharper and CodeMaid but not sure if these will do the job?

EDIT:

I also have the following deprecated html attributes I'm looking to relocate to a style tag.

<td height="50">
<td width="50>
<table cellpadding="2">
È stato utile?

Soluzione

There is no simple way, for several reasons. The align attribute has different meanings in different tags, and different CSS codes would be needed. For td alone, it would be simple, but then you would need to combine text-align: ... with existing content in style attribute if present. In effect, you would need an HTML parser and a CSS parser.

The cellpadding attribute has never been valid and does not work in td, only in table. Replacing it with style attributes would mean that you need to put such an attribute in each and every th and td in the table.

Other presentational attributes can be more tricky. HTML5 has a complicated section on Rendering, describing (among other things) the “expected” effects of presentational markup in CSS terms (these descriptions does not quite match reality, but mostly they do).

And the result would still not be the same in all cases. Presentational attributes in HTML have a special role in formatting, and it cannot be described in CSS (it is described in CSS specifications, but you cannot express it in CSS code).

(The features are not “depreciated” in HTML5, but “obsolete”. This means that browsers are expected to keep supporting them. There is no good reason to “clean up” them on old pages—there is no possible gain, and there are loads of problems that the “clean up” causes, especially if mistakes are made in it, and human beings make mistakes.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top