HTML and CSS Reference
In-Depth Information
Escape Less-Than Sign
Convert < to &lt; .
x < y ==> y > x
x &lt; y ==> y > x
Motivation
Although some browsers can recover from an unescaped less-than sign some of the time, not all can. An
unescaped less-than sign is more likely than not to cause content to be hidden in the browser. Even if you aren't
transitioning to full XHTML, this one is a critical fix.
Potential Trade-offs
None. This change can only improve your web pages. However, you do need to be careful about embedded
JavaScript within pages. In these cases, sometimes the less-than sign cannot be escaped. You can either move
the script to an external document where the escaping is not necessary or reverse the sense of the comparison.
Mechanics
Because this is a real bug that does cause problems on pages, it's unlikely to show up in a lot of places. You can
usually find all the occurrences and fix them by hand.
I don't know one regular expression that will find all cases of these. However, a few will serve to find most. The
first thing to look for is any less-than sign followed by whitespace. This is never legal in HTML. This regular
expression will find those:
<\s
If you're not using any embedded JavaScript, you can search for <(\s) and replace it with &lt;\1 . However, if
you're using JavaScript, you need to be more careful and should probably let Tidy or TagSoup do the work.
If your pages involve mathematics at all, it's also worth doing a search for a < followed by a digit:
<\d
However, a validator such as xmllint or HTML Validator should easily find all cases of these, along with a few
cases the simple search will mix.
Embedded JavaScript presents a problem here. JavaScript does not recognize &lt; as a less-than sign. Inside
JavaScript, you have to use the literal character. A less-than sign can usually be recast as a greater-than sign
with arguments reversed. For example, instead of writing
if (x < 7)
Search WWH ::




Custom Search