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

HtmlEscapingQuotes

Html Problem Overview


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 "?

Html Solutions


Solution 1 - Html

" 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.

Solution 2 - Html

" is valid in both HTML5 and HTML4.

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

Solution 3 - Html

' is not part of the HTML 4 standard.

" is, though, so is fine to use.

Solution 4 - Html

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".

Solution 5 - Html

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbradView Question on Stackoverflow
Solution 1 - HtmlcletusView Answer on Stackoverflow
Solution 2 - HtmlZazView Answer on Stackoverflow
Solution 3 - HtmlAnon.View Answer on Stackoverflow
Solution 4 - HtmlR. SchreursView Answer on Stackoverflow
Solution 5 - HtmlJeffView Answer on Stackoverflow