aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/i18n/mime.js
blob: aeded392cd866ca36dfd874f750ce5c3d33d0bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2010 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Functions for encoding strings according to MIME
 * standards, especially RFC 1522.
 */
goog.provide('goog.i18n.mime');
goog.provide('goog.i18n.mime.encode');


/**
 * Regular expression for matching those characters that are outside the
 * range that can be used in the quoted-printable encoding of RFC 1522:
 * anything outside the 7-bit ASCII encoding, plus ?, =, _ or space.
 * @type {RegExp}
 * @private
 */
goog.i18n.mime.NONASCII_ = /[^!-<>@-^`-~]/g;


/**
 * Like goog.i18n.NONASCII_ but also omits double-quotes.
 * @type {RegExp}
 * @private
 */
goog.i18n.mime.NONASCII_NOQUOTE_ = /[^!#-<>@-^`-~]/g;


/**
 * Encodes a string for inclusion in a MIME header. The string is encoded
 * in UTF-8 according to RFC 1522, using quoted-printable form.
 * @param {string} str The string to encode.
 * @param {boolean=} opt_noquote Whether double-quote characters should also
 *     be escaped (should be true if the result will be placed inside a
 *     quoted string for a parameter value in a MIME header).
 * @return {string} The encoded string.
 */
goog.i18n.mime.encode = function(str, opt_noquote) {
  var nonascii = opt_noquote ?
      goog.i18n.mime.NONASCII_NOQUOTE_ : goog.i18n.mime.NONASCII_;

  if (str.search(nonascii) >= 0) {
    str = '=?UTF-8?Q?' + str.replace(nonascii,
        /**
         * @param {string} c The matched char.
         * @return {string} The quoted-printable form of utf-8 encoding.
         */
        function(c) {
          var i = c.charCodeAt(0);
          if (i == 32) {
            // Special case for space, which can be encoded as _ not =20
            return '_';
          }

          var a = [''];  // start with empty string to get = before every char
          // First convert the UCS-2 character into its UTF-8 bytes
          if (i < 128) {
            a.push(i);
          } else if (i <= 0x7ff) {
            a.push(
                0xc0 + ((i >> 6) & 0x3f),
                0x80 + (i & 0x3f));
          } else if (i <= 0xffff) {
            a.push(
                0xe0 + ((i >> 12) & 0x3f),
                0x80 + ((i >> 6) & 0x3f),
                0x80 + (i & 0x3f));
          } else {
            // (This is defensive programming, since ecmascript isn't supposed
            // to handle code points that take more than 16 bits.)
            a.push(
                0xf0 + ((i >> 18) & 0x3f),
                0x80 + ((i >> 12) & 0x3f),
                0x80 + ((i >> 6) & 0x3f),
                0x80 + (i & 0x3f));
          }
          // Now convert those bytes into hex strings (don't do anything with
          // a[0] as that's got the empty string that lets us use join(),
          // below).
          for (i = a.length - 1; i > 0; --i) {
            a[i] = a[i].toString(16);
          }
          // Form the string with = before each byte.
          return a.join('=');
        }) + '?=';
  }
  return str;
};