Shahbaz
Shahbaz

Reputation: 47603

Could an HTML tag have < anywhere in it? (In the strings for example)

I know that < is not used inside an HTML tag. I know that it is illegal in URIs too. I can deduce that it is also invalid in class or id of an HTML element. I have never seen it used in styles either.

I need to know however if there is any weird special case that < could be entered.

Let me elaborate. I want to match HTML tags in some text and say, throw them away. In this text, < is normally escaped (so written like \<), but I am assuming there could be user input mistake. So, I want to see if I there is ever a chance that such a thing could be a tag:

<.........<......>  <-- the whole thing is a tag

(where the .s could be anything, such as ")

Or could I safely assume the first < was a mistake by the user?


The question sounds a bit too specific, so I'm going to make it a bit more general: What are the characters that absolutely cannot appear inside an html tag?

Upvotes: 0

Views: 77

Answers (3)

Curtis
Curtis

Reputation: 103428

If you're concerned about their being a clash of HTML tags, and are concerned about the validity of >, you can use the encoded HTML &gt;:

<a href="#" title="My anchor &gt; title">My Link Text</a>​

http://jsfiddle.net/Curt/gKP89/

But as @Quentin & @roryf have pointed out, this shouldn't be an issue.

Upvotes: 1

roryf
roryf

Reputation: 30170

<a title="A title with a < less than symbol">My awesome link</a>

Is perfectly valid HTML.

Upvotes: 4

Quentin
Quentin

Reputation: 944529

I can deduce that it is also invalid in class

It isn't.

or id of an HTML element

True for HTML 4, but not for HTML 5.

What are the characters that absolutely cannot appear inside an html tag?

There aren't any. Some might only be able to appear in attribute values, but since they appear inside the tag, any character can.

Upvotes: 4

Related Questions