Unicodeに変換

スカラー値列

[69] スカラー値列 (scalar value string) (USVString) は、Unicodeスカラー値文字列です。

[70] ほとんど「Unicode文字列」という意味に近いですが、 「文字」の語義を巡っては相当の混乱があります。

仕様書

意味

[27] USVString は、Unicodeスカラー値を表します >>25

[29] 型名は、 USVString です >>25

構文

[28] 辞書メンバー引数既定値の指定では、 文字列リテラルとして記述できます >>25

文脈

[30] ほとんどの場合は DOMString を使うべきであり、 Unicodeスカラー値の列が必要な場合に限って USVString とするべきです >>25

[60] 次の場面で使われています。

[61] ネットワークの入出力 (に伴い文字コードの変換が発生するもの) だけでなく、 URL でも USVString が使われています。利用箇所数でいえば USVString のほとんどは URL文字列です。 JavaScriptDOM符号単位列 (DOMString) ベースである以上 URLサロゲートが混入する可能性は排除できないのですが、 Ian HicksonURL構文解析器で行うべきと主張したのに対し、 AnneURL構文解析器の入出力はUnicodeスカラー値列だと定め、 URL を使うあらゆる APIデータ型USVString に書き換えていきました。 (つまり URL構文解析器の入力時ではなく、 メソッドの呼び出しや IDL属性設定器の呼び出しの時点でサロゲートの検査を行うこととしました。)

[62] 内容属性反映する USVStringIDL属性経由で属性値を設定するとサロゲートの検査が行われ、 setAttribute などを使うと検査が行われません。 サロゲートがそのまま URL Standard構文解析器に渡るとすると素片識別子部分ならそのまま、 それ以外なら UTF-8パーセント符号化されるはずで、前者の場合サロゲートのまま出力されるはずですが、 Chrome では U+FFFD に置き換わっています。 (Firefox では素片識別子全体がUTF-8パーセント符号化されるので、そもそも URL Standard と動作が違います。) WebIDL レベルで変換しても、結局 URL構文解析器に渡す直前またはその中でまた検査するのが現実の実装ということになるので、 WebIDL レベルでの変換が本当に必要だったのかに疑問が生じます。

変換

[31] USVString 型のIDL値入力から JavaScript への変換は、次のようにします >>26

  1. [35] スカラー値列を、入力の表すUnicodeスカラー値の列に設定します。
  2. [36] 符号単位列を、スカラー値列UTF-16符号化した結果の符号単位の列に設定します。
  3. [37] 符号単位列と同じ符号単位の列を表す String 値を返します。

[33] JavaScript 入力から USVStringIDL値への変換は、次のようにします >>26

  1. [32] を、入力DOMString変換した結果に設定します。
  2. [34] DOMStringをUnicodeスカラー値列に変換した結果を返します。

[39] 符号単位列についてDOMStringをUnicodeスカラー値列に変換 (convert a DOMString to a sequence of Unicode scalar values) するには、 次のようにします >>38

  1. [42] スカラー値列を、に設定します。
  2. [40] n を、文字列長さに設定します。
  3. [41] i を、 0 に設定します。
  4. [43] i < n の間、繰り返し、
    1. [44] c を、符号単位列において索引 i符号単位に設定します。
    2. [45] c により、
      c範囲 [ 0xDC00, 0xDFFF ] に含まれる
      スカラー値列の末尾に U+FFFD を追加します。
      c範囲 [ 0xD800, 0xDBFF ] に含まれる
      1. [46] in - 1 なら、
        1. [47] スカラー値列の末尾に U+FFFD を追加します。
      2. [48] それ以外なら、
        1. [49] d を、符号単位列において索引 i + 1 の符号単位に設定します。
        2. [50] d範囲 [ 0xDC00, 0xDFFF ] に含まれるなら、
          1. [51] スカラー値列の末尾に符号位置 216 + 210 × (c & 0x3FF) + (d & 0x3FF) のUnicode文字を追加します。
          2. [52] iインクリメントします。
        3. [53] それ以外なら、
          1. [54] スカラー値列 の末尾に U+FFFD を追加します。
      それ以外
      スカラー値列の末尾に符号位置 cUnicode文字を追加します。
    3. [55] iインクリメントします。
  5. [56] スカラー値列を返します。

[57] これはつまり、サロゲートペアになっていないサロゲートが含まれる時、 U+FFFD に置換されることとなります。

[73] 次の場面で使われます。

[74] Unicodeに変換する場面

歴史

EnsureUTF16

XXX

ScalarValueString

USVString

[12] EnsureUTF16>>11 で削除されました。

[14] >>13USVString が追加されました。

[15] Web Applications 1.0 r8836 Switch to using USVString where that makes the prose simpler. (WebSocket, primarily) ( ( 版)) https://html5.org/r/8836

[16] Since icon takes a URL -> USVString · f62c920 · whatwg/notifications ( ( 版)) https://github.com/whatwg/notifications/commit/f62c920ad1bca50d40ccc3e9a9a6ffef234ae5bc

[17] Allow USVString to be serialized. · heycam/webidl@d7aaded ( 版) https://github.com/heycam/webidl/commit/d7aaded036ea48a4d9a8fd543d2cba046ce34cab

[18] USVString is the right type for things to be encoded and sent over th… · w3c/webappsec-credential-management@c749164 ( 版) https://github.com/w3c/webappsec-credential-management/commit/c7491649ac0ae7d30f00e31c70f5c7194cc9f48b

[19] Editorial: define Event attribute defaults through IDL · whatwg/html@0be0229 ( 版) https://github.com/whatwg/html/commit/0be02299f128cbb7c65a7ce259fdd838fd44a4b5

[20] Use USVString for oldURL, newURL, and origin attributes on events · whatwg/html@5d5702d ( 版) https://github.com/whatwg/html/commit/5d5702d35a0b7457481d1013154f14aab25500bf

[21] Update WebSocket to use Fetch's WebSocket alterations · whatwg/html@3dadbca ( 版) https://github.com/whatwg/html/commit/3dadbcad063a10b586ef52dd4b427aa339048ee7

[22] Use USVString for document.domain/referrer/cookie · whatwg/html@8170d82 ( 版) https://github.com/whatwg/html/commit/8170d82a2fc93c8ff7981c54aa4ccafd54204552

[23] Make EventSource use the base URL of its global · whatwg/html@3175b53 ( 版) https://github.com/whatwg/html/commit/3175b53a7fab83a62fc1c5674e8356f720d2aa4a

[24] Reflect USVString IDL attributes for URLs as well as DOMString ones · whatwg/html@2ced624 ( 版) https://github.com/whatwg/html/commit/2ced6248d16e5a742e324ba3f506cc6d179b0414

[58] FirefoxChrome も、 <a href>IDL属性で既に USVString として動作するようになっているようです。 IE9DOMString としているようです。

[59] FirefoxChrome<blockquote cite>USVString ではなく DOMString としています。

[63] Use USVString for URLs and origins in IDL ( (zcorpan著, )) https://github.com/whatwg/dom/commit/1bb85a48e07d1000e00bd792d61247b9a5e2e4ae

[64] Use USVString for all URLs ( (domenic著, )) https://github.com/whatwg/html/commit/018b983b77b2cd908f6d00100e7e0abe893dd2c3

[65] Ensure strings in the form data set contain USVs (yuyokk著, ) https://github.com/whatwg/html/commit/f35af4efea8f89743ebdda4661c3bb58349828a9

[66] Use USVString rather than [EnsureUTF16] for fileName. (mkruisselbrink著, ) https://github.com/w3c/FileAPI/commit/abf6637473a9c81247fce46ef0a2954be95908f1

[67] Define JavaScript string and scalar value string (annevk著, ) https://github.com/whatwg/infra/commit/f1be763cfba23d2fc780b35403074c599e69616e

[68] Define length for byte sequences and strings (annevk著, ) https://github.com/whatwg/infra/commit/c484a4fe0d7bdc52b0f17ab99e49eaa9e56e3f56

[71] [cssom] Use USVString for href attribute of Stylesheet and CSSImportRule (SimonSapin著, ) https://github.com/w3c/csswg-drafts/commit/4ade7360db96f95c902e9c8b24dd6e4e20d49ec7

[72] IDL amendments and small misc issues. (#271) (andypaicu著, ) https://github.com/w3c/webappsec-csp/commit/82aebd3dcd00492ce718e92ada3ebf1e4133cf36

[75] Editorial: rewrite send()'s body/content-type processing (domenic著, ) https://github.com/whatwg/xhr/commit/f47bbab42dabe1f52e5e9f1ed1fa6df06a6eb310

[76] [typed-om] Per WG resolution, just use USVString for everything. Fixes … (tabatkins著, ) https://github.com/w3c/css-houdini-drafts/commit/5e48fe4bcf68c81c2da464fa8f1b8fd0595d3428

[77] Should we be using DOMString, USVString, or CSSOMString? · Issue #687 · w3c/css-houdini-drafts () https://github.com/w3c/css-houdini-drafts/issues/687

[78] SecurityPolicyViolationEvent needs more USVString · Issue #266 · w3c/webappsec-csp () https://github.com/w3c/webappsec-csp/issues/266