3

I understand the rationale behind why most of the HTML character Entities were created, but I don't understand the purpose behind the ampersand HTML entity. & contains more bits than a & symbol, and it even has the & inside it already!

Why was it decided that ampersands should be encoded in the format that requires more data, instead of just using the symbol?

  • It is like how we have a way of including a quote inside another quote. Computers have long had this problem: code can be data in a different situation, and data can be code. That is what makes computers different from ordinary machines, and what makes human language different from the noises animals make. Now go forth and self-reference! – user251748 Feb 23 '18 at 13:10
14

To avoid ambiguity. Suppose you want to write a website about HTML. You write the line: "To write a literal < you have to write &lt;." Now, to write that down in HTML:

<p>To write a literal &lt; you have to write &lt;.

... oops. To make it work, you have to have some way to distinguish the character & from the HTML syntax starting with &. So that's why you have to write:

<p>To write a literal &lt; you have to write &amp;lt;.

... which renders correctly.

  • I would say it is to allow ambiguity. To be able to include two levels of interpretation in one context. – user251748 Feb 23 '18 at 13:15
7

It looks like this goes all the way back to SCRIPT which was invented in 1968.

roughly speaking, SCRIPT became SCRIPT/VS which became GML which became SGMLguid which became HTML

Here is the origonal paper introducing SCRIPT:

http://web.mit.edu/smadnick/www/papers/J002.pdf

Why choose &? well imagine you are in a terminal window. you have no mouse. you can not use the cursor keys to traverse the screen. You can only type on the command line. if you want to delete a line, type %, to delete a character, type @

You can see towards the end ¢ is used to denote an escape character. cent used to be on keyboards (shift 6), but was dropped. (something to do with ascii) most keyboards replaced it with ^ but some with ¬

So all of sudden to program in SCRIPT you have a super hard time typing the escape character on new computers. You need to pick a replacement character, If you use one the symbols on the same key, some people aren't going to be able to type it, because keyboard makers are still arguing.

You need to choose a replacement escape character for the one that used to be on key 6. What do you choose?

enter image description here

  • 2
    I don't think your answer really answers the question, but I really, really like your answer. – Chris G Feb 22 '18 at 23:34
  • @ChrisG This is the only answer to the question asked: why is & the escape character in HTML? However since an answer explaining what an escape character is got accepted, The question is presumably poorly expressed. – David Arno Feb 23 '18 at 6:26
  • 1
    @DavidArno But the question asked wasn't why & is the escape character, it was why & has to be escaped itself. – Sebastian Redl Feb 23 '18 at 7:59
  • 1
    Its true I had to interpret the question at a slightly deeper level. But it was interesting researching the answer – Ewan Feb 23 '18 at 8:10
  • @SebastianRedl, yep I see that now. However, the way the question is phrased, I read it as "why is & used as the escape character, when & itself then has to be escaped?" Luckily some of you are better at reading badly phrased questions than I am though, so what the OP wanted to know got answered :) – David Arno Feb 23 '18 at 9:17
5

In any data format, the escaping mechanism must be escaped itself. The escape character is a special character.

For example, I want to display this text:

Ampersands are escaped like &amp;

If I write this HTML as

<p>Ampersands are escaped like <code>&amp;</code>

it will be displayed as:

Ampersands are escaped like &

So I actually need to write:

<p>Ampersands are escaped like <code>&amp;amp;</code>

which displays correctly as:

Ampersands are escaped like &amp;

In many languages, the backslash is an escape character. Then, the literal string C:\projects will have to be escaped as C:\\projects.

If a data format is supposed to be able to represent arbitrary data, it either needs a known length, or a delimiter. Handling known lengths is unwieldy in human-editable formats. But if we have a delimiter, it must be escapeable. So we need an escape mechanism, and the escape mechanism needs to escape itself. One minimal solution is to use the delimiter as the escape character. E.g. for a delimiter ', the string don't do that could be encoded as 'don''t do that'.

  • The ASCII Escape character was originally designed for this exact purpose. Its full name is Data Link Escape. I used it in a modem / mux situation before. – user251748 Feb 23 '18 at 13:16
1

Because "&" starts an entity, so using it by itself isn't valid HTML (although some browsers may accept it, this isn't guaranteed).

  • 2
    Lone ampersands are OK in HTML, unless they form an ambiguous ampersand, i.e. a named character reference that isn't defined in the HTML standard. Otherwise ampersands are well-defined. – amon Feb 22 '18 at 20:16
  • 1
    @amon - sure, but it's worth noting that the definition of when they're allowed has varied between different HTML versions (HTML2, for example, only allowed them if they weren't followed by an alphanumeric character, regardless of if a semicolon followed), and that even with the strictest definition you still need to be able to escape them in some circumstances. Also, while HTML allows this, neither SGML nor XML do, so SGML parsers with an HTML DTD will fail, and XHTML parsers won't accept it either. Therefore, I wouldn't recommend actually using this facility. – Jules Feb 23 '18 at 14:36
  • First there was HTML. Then there was chaos, as every browser vendor did whatever they liked. Serious Engineers™ were fed up by that chaos and created XHTML. But writing XML by hand sucks, so the chaos continued. Finally, the chaos was codified and called HTML5. It parses the chaos just fine, but in a well-defined manner. It is no longer useful to interpret HTML5 as an SGML dialect. And except for XHTML, HTML was never XML. But every remotely up to date browser or HTML parser will deal with HTML5 just fine, at least on a syntactic level. – amon Feb 23 '18 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.