Unicode and HTML: Difference between revisions

Content deleted Content added
m Web browser support: dab Cyrillic alphabet - Cyrillic script, replaced: CyrillicCyrillic using AWB
No edit summary
Line 7:
 
== HTML document characters ==
 
Web pages are typically [[HTML]] or [[XHTML]] documents. Both types of documents consist, at a fundamental level, of [[character (computing)|character]]s, which are [[grapheme]]s and grapheme-like units, independent of how they manifest in [[computer storage]] systems and [[computer network|network]]s.
 
Line 14 ⟶ 13:
Like HTML documents, an XHTML document is a sequence of Unicode characters. However, an XHTML document is an [[XML]] document, which, while not having an explicit "document character" layer of [[abstraction]], nevertheless relies upon a similar definition of permissible characters that cover most, but not all, of the Unicode/UCS character definitions. The sets used by HTML and XHTML/XML are slightly different, but these differences have little effect on the average document author.
 
Regardless of whether the document is HTML or XHTML, when stored on a [[file system]] or transmitted over a network, the document's characters are ''encoded'' as a sequence of [[bit]] [[octet (computing)|octet]]s (''[[byte]]s'') according to a particular character encoding. This encoding may either be a [[Unicode Transformation Format]], like [[UTF-8]], that can directly encode any Unicode character, or a legacy encoding, like [[Windows-1252]], that cannot. However, even when using encodings that do not support all Unicode characters, the encoded document may make use of [[numeric character references]]. For example <code>&amp;#x263A;</code> ({{unicodeUnicode|☺}}) is used to indicate a smiling face character in the Unicode character set.
 
=== Character encoding===
Line 31 ⟶ 30:
{{main|character entity reference}}
 
In HTML there is a standard set of 252 named ''character entities'' for characters&nbsp;&mdash; - some common, some obscure&nbsp;&mdash; - that are either not found in certain character encodings or are markup sensitive in some contexts (for example angle brackets and quotation marks). Although any Unicode character can be referenced by its numeric code point, some HTML document authors prefer to use these named entities instead, where possible, as they are less cryptic and were better supported by early browsers.
 
Character entities can be included in an HTML document via the use of ''entity references'', which take the form '''<code>&amp;</code>'''<var>EntityName</var>'''<code>;</code>''', where <var>EntityName</var> is the name of the entity. For example, <code>&amp;mdash;</code>, much like <code>&amp;#8212;</code> or <code>&amp;#x2014;</code>, represents {{U+|2014}}: the [[em dash]] character&nbsp;&mdash; like this&nbsp;"&mdash;" even if the character encoding used doesn't contain that character.
 
For the full list, see: [[List of XML and HTML character entity references]].
Line 41 ⟶ 40:
 
===Encoding information===
When a document is transmitted via a [[MIME]] message or a transport that uses MIME content types such as an [[HTTP]] response, the message may signal the encoding via a Content-Type header, such as <code>Content-Type: text/html; charset=UTF-8</code>. Other external means of declaring encoding are permitted but rarely used. If the document uses an [[Comparison_of_Unicode_encodings|Unicode encoding]], the encoding info might also be present in the form of a [[Byte order mark]]. Finally, the encoding can be declared via the HTML syntax. For the <code>text/html</code> serialisation then, as long as the page is encoded in an extension of [[ASCII]] (such as [[UTF-8]], and thus, not if the page is using [[UTF-16]]), a <code>meta</code> element, like <code>&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt;</code> or (starting with [[HTML5]]) <code>&lt;meta charset="UTF-8"></code> can be used. For HTML pages serialized as XML, then declaration options is to either rely on the encoding default (which for XML documents is UTF-8), or to use an XML encoding declaration — se below. The meta attribute plays no role in HTML served as XML.
 
===Encoding defaults===
An encoding default applies when there is no external or internal encoding declaration and also no Byte order mark. While the encoding default for HTML pages served as XML is required to be UTF-8, the encoding default for a regular Web page (that is: for HTML pages serialized as <code>text/html</code>) varies depending on the localisationlocalization of the browser. For a system set up mainly for Western European languages, it will generally be [[ISO 8859-1#Windows-1252|Windows-1252]]. For the Russian locale, the default is typically [[Windows-1251]]. For a browser from a ___location where ''legacy'' multibytemulti-byte character encodings are prevalent, some form of autodetectionauto-detection is likely to be applied.
 
===Encoding trends===
Because of the legacy of 8-bit text representations in [[programming language]]s and [[operating system]]s and the desire to avoid burdening users with the need to understand the nuances of encoding, many text editors used by HTML authors are unable or unwilling to offer a choice of encodings when saving files to disk and often do not even allow input of characters beyond a very limited range. Consequently many HTML authors are unaware of encoding issues and may not have any idea what encoding their documents actually use. Misunderstandings, such as the belief that the encoding declaration affects a change in the actual encoding (whereas it is actually just a label that could be inaccurate), is also a reason for this editor attitude. Another factor contributing in the same direction, is the arrival of UTF-8 — which greatly diminishes the need for other encodings, and thus modern editors tends to default, as recommended by the HTML5 specifiction specification,<ref>{{Cite web|url=http://www.w3.org/TR/html5/semantics.html#charset|title=HTML5|author=Ian Hickson|accessdate=17 September 2011|year=2011|quote=Authors are encouraged to use UTF-8. Conformance checkers may advise authors against using legacy encodings. [RFC3629] Authoring tools should default to using UTF-8 for newly-created documents. [RFC3629]}}</ref>, to UTF-8.
 
===Byte order mark/Unicode sniffing===
For both serializations of HTML (content-type "text/html" and content/type "application/xhtml+xml"), the Byte order mark (BOM) is an effective way to transmit encoding information within an HTML document. For UTF-8, the BOM is optional, while it is a must for the UTF-16 and the UTF-32 encodings. (Note: UTF-16 and UTF-32 without the BOM are formally known under different names, they are different encodings, and thus needs some form of encoding declaration – see [[UTF-16BE]], [[UTF-16LE]], [[UTF-32LE]] and [[UTF-32BE]].) The use of the BOM character (U+FEFF) means that the encoding automatically declares itself to any processing application. Processing applications need only look for an initial 0x0000FEFF, 0xFEFF or 0xEFBBBF in the byte steam to identify the document as UTF-32, UTF-16 or UTF-8 encoded respectively. No additional metadata mechanisms are required for these encodings since the byte-order mark includes all of the information necessary for processing applications. In most circumstances the byte-order mark character is handled by editing applications separately from the other characters so there is little risk of an author removing or otherwise changing the byte order mark to indicate the wrong encoding (as can happen when the encoding is declared in English/Latin script). If the document lacks a byte-order mark, the fact that the first non-blank printable character in an HTML document is supposed to be '''&lt;'''"<" (U+003C) can be used to determine a UTF-8/UTF-16/UTF-32 encoding.
 
===Encoding overriding===
Many HTML documents are served with inaccurate encoding information, or no encoding information at all. In order to determine the encoding in such cases, many browsers allow the user to manually select an encoding name from a list. They may also employ an encoding autodetectionauto-detection algorithm that works in concert '''with''' or — ''in the case of the BOM and in case of HTML served as XML'' — '''against''' the manual override.
 
For HTML documents which are <code>text/html</code> serialized, manual override may apply to all documents, or only those for which the encoding cannot be ascertained by looking at declarations and/or byte patterns. The fact that the manual override is present and widely used hinders the adoption of accurate encoding declarations on the Web; therefore the problem is likely to persist. But note that Internet Explorer, Chrome and Safari — for both XML and <code>text/html</code> serializations — do not permit the encoding to be overridden whenever the page includes the BOM.<ref>[http://www.w3.org/Bugs/Public/show_bug.cgi?id=12897 Bug 12897 - In some parsers, UTF-8 BOM trumps the HTTP charset attribute (Encoding sniffing algorithm)]</ref>
 
For HTML documents serialized with the preferred XML label — <code>application/xhtml+xml</code>, manual encoding override is not permitted. To override the encoding of such an XML document would mean that that the document stopped being XML, as it is a fatal error for XML documents to have an encoding declaration with detectable errors. Currently, Gecko browsers such as Firefox, abide to this rule, whereas the bulk of the other common browsers that support HTML as XML, such as Webkit browsers (Chrome/Safari) <ref>[https://bugs.webkit.org/show_bug.cgi?id=66189 Bug 66189 - XML parser doesn't emit FATAL ERROR for all, detectable encoding errors]</ref> do — against the XML specificaitonspecification — allow the encoding of XHTML documents to be manually overridden.
 
==Web browser support==
Line 166 ⟶ 165:
*[http://www.w3.org/TR/REC-html40/HTMLlat1.ent Latin-1], [http://www.w3.org/TR/REC-html40/HTMLspecial.ent "Special"], and [http://www.w3.org/TR/REC-html40/HTMLsymbol.ent Mathematical, Greek and Symbolic] named character entity definitions for HTML 4.01
*[http://www.unicodemap.org/ UnicodeMap.org] - Browse Unicode characters, ranges, and other information
*[http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id= SIL's freeware fonts, editors and documentation]
*[http://www.alanwood.net/unicode/ Alan Wood’s Unicode Resources] - Unicode fonts and information (www.alanwood.net/unicode).
*http://www.phon.ucl.ac.uk/home/wells/ipa-unicode.htm The International Phonetic Alphabet in Unicode
*http://www.alanwood.net/unicode/cjk_compatibility_ideographs.html CJK Compatibility Ideographs