views:

309

answers:

4

In HTML attribute name=value pairs, what are the characters allowed for the 'name' portion? ..... Looking at some common attributes it appears that only letters (a-z and A-Z) are used, but what other chars could be allowed as well?... maybe digits (0-9), hyphens (-), and periods (.) ... is there any spec for this?

+2  A: 

The values allowed are listed at http://www.w3.org/TR/html4/index/attributes.html - if you add a custom attribute, then you aren't writing HTML any more.

David Dorward
Conclusive. So all the allowed chars are those present in that document. Thanks!
Jenko
A: 

Assuming you're talking about XHTML, the XML rules apply.

See http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name

Names and Tokens

[4]     NameStartChar    ::=    ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
[4a]    NameChar    ::=    NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5]     Name    ::=    NameStartChar (NameChar)*
[6]     Names    ::=    Name (#x20 Name)*
[7]     Nmtoken    ::=    (NameChar)+
[8]     Nmtokens    ::=    Nmtoken (#x20 Nmtoken)*
S.Lott
+4  A: 

Maybe I'm missing something, but I believe the question is based on a false assumption. In HTML, attributes are strictly defined according to a fixed specification. If you 'make up' your own attribute names, you are no longer writing valid HTML.

Daan
+1  A: 

It depends what you mean by "allowed". Each tag has a fixed list of attribute names which are valid, and in html they are case insentitive. In one important sense, only these characters in the correct sequence are "allowed".

Another way of looking at it, is what characters will browsers treat as a valid attribute name. The best advice here comes from the parser spec of HTML 5, which can be found here: http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#before-attribute-name-state

It says all characters except tab, line feed, form feed, space, solidus, greater than sign, quotation mark, apostrophe and equals sign will be treated as part of the attribute name. Personally, I wouldn't attempt pushing the edge cases of this though.

Alohci
Answer my question. "all characters except ... will be treated as part of the attribute name" -- Kudos on finding this info, that too in a spec!
Jenko