Why shouldn't `'` be used to escape single quotes?
Asked Answered
H

5

205

As stated in, When did single quotes in HTML become so popular? and Jquery embedded quote in attribute, the Wikipedia entry on HTML says the following:

The single-quote character ('), when used to quote an attribute value, must also be escaped as ' or ' (should NOT be escaped as ' except in XHTML documents) when it appears within the attribute value itself.

Why shouldn't ' be used? Also, is " safe to be used instead of "?

Hogue answered 18/1, 2010 at 3:31 Comment(5)
' is valid in HTML5.Privily
Note that IE8 doesn't support '.Decor
There's a "not" missing from the Wikipedia quote, which should be "… when not used to quote an attribute value …".Naphtha
Oh, so if you don't care about anything older than IE11, ' is fine.Gwendolyn
@Calculuswhiz But of course it was a valid concern back in 2010, when this question was asked...Allx
O
221

" is on the official list of valid HTML 4 entities, but ' is not.

From C.16. The Named Character Reference ':

The named character reference ' (the apostrophe, U+0027) was introduced in XML 1.0 but does not appear in HTML. Authors should therefore use ' instead of ' to work as expected in HTML 4 user agents.

Overlook answered 18/1, 2010 at 3:37 Comment(5)
" is on the list though, so should be OK.Amygdalin
Today happened that on an IE8 machine ' was not parsed, so the user sees the code. Replacing with ' solved the problem.Pursley
&aquot; is NO substitute for ' as one's a single and the other a double.Brainbrainard
FYI: HTML5 does support both entitiesWycoff
@Brainbrainard I assume you mean " - there is no such entity as &aquot;Naphtha
P
50

" is valid in both HTML5 and HTML4.

' is valid in HTML5, but not HTML4. However, most browsers support ' for HTML4 anyway.

Privily answered 31/8, 2013 at 18:15 Comment(2)
Note that IE 8 doesn't support '.Decor
dont shoot me for saying - who cares about IE8...Nidus
A
36

' is not part of the HTML 4 standard.

" is, though, so is fine to use.

Advertent answered 18/1, 2010 at 3:36 Comment(4)
How can this be an answer when &aquot; = " not '? It's like suggesting a colon for a semicolon...Brainbrainard
@Richardakacyberkiwi Read the question all the way to the end.Skeens
@Anon.: ' is now part of the HTML standard.Decor
@Brainbrainard I assume you mean " - there is no such entity as &aquot;Naphtha
S
25

If you need to write semantically correct mark-up, even in HTML5, you must not use ' to escape single quotes. Although, I can imagine you actually meant apostrophe rather then single quote.

single quotes and apostrophes are not the same, semantically, although they might look the same.

Here's one apostrophe.

Use ' to insert it if you need HTML4 support. (edited)

In British English, single quotes are used like this:

"He told me to 'give it a try'", I said.

Quotes come in pairs. You can use:

<p><q>He told me to <q>give it a try</q></q>, I said.<p>

to have nested quotes in a semantically correct way, deferring the substitution of the actual characters to the rendering engine. This substitution can then be affected by CSS rules, like:

q {
  quotes: '"' '"' '<' '>';
} 

An old but seemingly still relevant article about semantically correct mark-up: The Trouble With EM ’n EN (and Other Shady Characters).

(edited) This used to be:

Use ’ to insert it if you need HTML4 support.

But, as @James_pic pointed out, that is not the straight single quote, but the "Single curved quote, right".

Submerge answered 23/12, 2014 at 15:57 Comment(6)
The question was clearly about using apostrophes and not quotes. Why would you post this?Shamanism
I clearly see single quote in the title. The question was why escaping them should not be done with &apos;. I answered this because the support for &apos; is not the only concern.Submerge
Single quotes are used that way in American English, also.Carolinacaroline
This should be the accepted answer since it clarifies that apostrophe and single quotes aren't the same thing and that's why you shouldn't use apostrophes to enclose a HTML attribute value.Nobukonoby
In Unicode, the apostrophe character (&#39;) is classed as a quotation mark, and is the only form of the "straight" quotation mark in the spec. The advice to use &#8217; is at best incomplete, because that is the character for "Single curved quote, right". If you really are going for semantic correctness, it should be used for right quotation marks, while &#8216; ("Single curved quote, left") should be used for left quotation marks.Audio
Most of this answer has nothing to do with the question, which was about HTML markup syntax, not English grammar.Naphtha
T
2

If you really need single quotes, apostrophes, you can use

html    | numeric | hex
&lsquo; | &#145;  | &#x91; // for the left/beginning single-quote and
&rsquo; | &#146;  | &#x92; // for the right/ending single-quote
Tablecloth answered 28/8, 2012 at 21:32 Comment(1)
This doesn't answer the question(s) asked.Naphtha

© 2022 - 2024 — McMap. All rights reserved.