4

Was viewing the source of Gmail for purely academic purposes and I came across this.

<input id=":3f4" 
    name="attach" 
    type="checkbox" 
    value="13777be311c96bab_13777be311c96bab_0.1_-1" 
    checked="">

Wonder of wonders, most elements have ids that starts with a :
I always thought the definition for ID attribute was this.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Or am I missing anything new? I mean is that OK with HTML5?

naveen
  • 53,448
  • 46
  • 161
  • 251
  • simple create an html and host it to see if it works.. then yes id attribute can start with colon otherwise no. – Murtaza May 23 '12 at 03:39
  • 2
    This assumes you can trust the browser. :-) – Ray Toal May 23 '12 at 03:43
  • ACtually that is a good point, @Murtaza! I had not thought of that. I'll delete my answer. The OP could have just looked it up. You are totally right. – Ray Toal May 23 '12 at 03:56
  • 2
    It is a valid question from the perspective of knowledge sharing and ease of searching. If you search google for "Can an ID attribute start with colon?" you receive this (now somewhat incorrect, even though there are hundreds of upvotes) answer: http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html. – Tim M. May 23 '12 at 04:00
  • It is much easier to locate information when it is formulated into specific questions. Granted, the OP could have read the spec, but these questions have merit because it makes it much easier to find an answer in the future. Personally, I learned something because it caused me to revisit the spec. – Tim M. May 23 '12 at 04:02
  • 1
    @murtaza: You are getting the question wrong. To me Gmail works ok? I was asking about the legality of that HTML, savy? – naveen May 23 '12 at 04:03
  • @naveen - If HTML standards have provided the feature legality is not the point to discuss.. I do not understand a person like you with 10K reputation is asking such question.. It is unexpected from you it is very easy to find the answer. please take a little effort to google your question. – Murtaza May 23 '12 at 04:08
  • 4
    It's a totally legit question, not sure what all the crying is about - there are floods of much worse questions coming in by the minute. It's important to know this so you can write code that adheres to the specification that browsers are supposed to follow, and doesn't rely on the browser's graceful handling of your incorrect code. Just because it works doesn't mean it's valid - and just because Google does it doesn't make it right either (but in this case, it is). – Wesley Murch May 23 '12 at 04:08
  • I personally request @TimMedora to flag the question to delete. – Murtaza May 23 '12 at 04:09
  • thanks ray and tim. Ray, you really did not have to delete that answer. murtaza, i googled that and was unable to find the answer linked by Tim. – naveen May 23 '12 at 04:09
  • @WesleyMurch i agree the way you explained, but the question is not at all specifying anything you mentioned.. it is a realistic question. but you can find the answers on internet, even a 15 yrs kid can find it. – Murtaza May 23 '12 at 04:11
  • @naveen I hope Tim has also googled to post this link.. if Tim could find the link.. means he search with proper words in google.. since you are questioning here you should be more clear to what you are asking and how. ideally you are not a new bee here. so this is always expected from you all talented persons.. and i truely, from the bottom of by heart appreciated your co-ordination.. i just want this to happen because it is not productive.. – Murtaza May 23 '12 at 04:13
  • so does we can also say it is a duplicate question using this link.. http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – Murtaza May 23 '12 at 04:16
  • 1
    Speech is silver. Silence is golden. Peace. – naveen May 23 '12 at 04:16
  • 2
    You personaly have some personal dispute with author of the question otherwise I don't know, @Murtaza. Question is legit and I got here because of doing exactly the same as he did, checking Gmail code. So it fulfills its meaning. Answering question here is valuable for everyone, not only the author of the question. He also provided quotation which proves he did some research before asking the question. Your first reaction is unbelievely amateur and wrong. I cannot believe that someone with reputation over 1k says some a big nonsense like that, I'm even not laughing, how wrong that is. – actimel Nov 26 '13 at 18:43

1 Answers1

9

Permissibility

It is allowed in the latest working draft: http://www.w3.org/TR/html-markup/datatypes.html#common.data.id

Any string, with the following restrictions:
- must be at least one character long
- must not contain any space characters

The spec also notes:

Previous versions of HTML placed greater restrictions on the content of ID values (for example, they did not permit ID values to begin with a number).

The definition you quoted appears in the HTML 4 spec.

There is a widely-visited SO thread which visits some of considerations regarding IDs (mainly from an HTML 4 perspective).

Rationale

After thinking about it more, I realized that there are two good questions here:

Why does the spec allow this?

IDs which can contain any character have the potential to break all sorts of things, such as CSS selectors (if proper escaping is not used), Sizzle (which jQuery uses) pattern matches, server IDs (such as ASP.Net web forms use) and IDs which are generated from model properties (such as one might do with a MVC pattern).

All those things aside, I believe a key goal of HTML 5 was to not create restrictions that weren't absolutely necessary (which was a shortcoming of XHTML). Just because a purpose hasn't been identified for something yet doesn't mean that it won't be in the future.

Despite the many things which won't work, certain things work just fine, for example document.getElementById(":foo")

http://jsfiddle.net/Xjast/

As with most things, it is up to the developer to be knowledgeable of the tools that he or she is using.

Why does Google do this?

Obviously this can't be answered conclusively unless you are part of the Gmail team. However, Google heavily minimizes and obfuscates their code; they also manage a huge amount of script, which suggests well-defined conventions.

Here's another thought. What if Google is leveraging the fact that CSS selectors require escaping of certain characters? This would go a long way towards reducing accidental restyling of content contained in an email message.

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163