问题
It's another Visual Studio 2008 HTML formatting question...I think I have either found a bug in the infamously bad VS HTML formatting, or I'm doing something wrong. Here's what I'm doing:
I remove all client side tags via:
Tools -> Options -> Text Editor -> HTML -> Format -> Tag Specific options
I then add b and span tags:
alt text http://www.xtupload.com/new/thumb-3BB0_49B92330.jpg
I press CTRL+E,CTRL+D and I get these two differing results:
1
alt text http://www.xtupload.com/new/image-CBF1_49B92330.jpg
The P before the span tag isn't formatted properly
2
alt text http://www.xtupload.com/new/image-3AB6_49B92330.jpg
The P tag is formatted correctly.
This for a .ASPX extension file.
回答1:
It looks like it is a bug, and isn't dependent on the tag being SPAN or B.
The work around I found
Add an extra space before the closing P.
How it fails
<p><b>My title</b></p>
Gets re-formatted as
<p>
<b>My title</b></p>
How to get it to work
<p><b>My title</b> </p>
(NB the space after the B) gets reformatted as:
<p>
<b>My title</b>
</p>
And that extra space is removed by VS anyway. Hallelujah my HTML looks beautiful!
回答2:
I followed the same method as Chris. I decided to use a RegEx find and replace to do it for the whole document. The regex finds any closing p or h* tags that aren't preceded by white space or the start of a line and inserts a newline before the closing tag. Examine the regex to get a better understanding. Here's what I used:
Find what:
{[^:b^]}{\</(p|(h:z))\>}:b*$
Replace with:
\1\n\2
It only finds p and h* because those were the only two I found had this problem. Other tags can be added.
回答3:
You can customize the layout per tag, if it bothers you that much. Go to the options dialog and select the formatting option under Text Editor -> HTML
Having said that, I don't like some of the inconsistencies I couldn't fix, so I stopped using it except to reformat code from someone else before I started working on it. Once the initial reformat is done, I maintain the formatting manually.
来源:https://stackoverflow.com/questions/639057/visual-studio-2008-html-formatting-does-it-ever-work