| 1 |
// Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Functions for encoding strings according to MIME |
| 17 |
* standards, especially RFC 1522. |
| 18 |
*/ |
| 19 |
goog.provide('goog.i18n.mime'); |
| 20 |
goog.provide('goog.i18n.mime.encode'); |
| 21 |
|
| 22 |
goog.require('goog.array'); |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Regular expression for matching those characters that are outside the |
| 27 |
* range that can be used in the quoted-printable encoding of RFC 1522: |
| 28 |
* anything outside the 7-bit ASCII encoding, plus ?, =, _ or space. |
| 29 |
* @type {RegExp} |
| 30 |
* @private |
| 31 |
*/ |
| 32 |
goog.i18n.mime.NONASCII_ = /[^!-<>@-^`-~]/g; |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Like goog.i18n.NONASCII_ but also omits double-quotes. |
| 37 |
* @type {RegExp} |
| 38 |
* @private |
| 39 |
*/ |
| 40 |
goog.i18n.mime.NONASCII_NOQUOTE_ = /[^!#-<>@-^`-~]/g; |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Encodes a string for inclusion in a MIME header. The string is encoded |
| 45 |
* in UTF-8 according to RFC 1522, using quoted-printable form. |
| 46 |
* @param {string} str The string to encode. |
| 47 |
* @param {boolean=} opt_noquote Whether double-quote characters should also |
| 48 |
* be escaped (should be true if the result will be placed inside a |
| 49 |
* quoted string for a parameter value in a MIME header). |
| 50 |
* @return {string} The encoded string. |
| 51 |
*/ |
| 52 |
goog.i18n.mime.encode = function(str, opt_noquote) { |
| 53 |
var nonascii = |
| 54 |
opt_noquote ? goog.i18n.mime.NONASCII_NOQUOTE_ : goog.i18n.mime.NONASCII_; |
| 55 |
|
| 56 |
if (str.search(nonascii) >= 0) { |
| 57 |
str = '=?UTF-8?Q?' + |
| 58 |
str.replace( |
| 59 |
nonascii, |
| 60 |
/** |
| 61 |
* @param {string} c The matched char. |
| 62 |
* @return {string} The quoted-printable form of utf-8 encoding. |
| 63 |
*/ |
| 64 |
function(c) { |
| 65 |
var i = c.charCodeAt(0); |
| 66 |
if (i == 32) { |
| 67 |
// Special case for space, which can be encoded as _ not =20 |
| 68 |
return '_'; |
| 69 |
} |
| 70 |
var a = goog.array.concat('', goog.i18n.mime.getHexCharArray(c)); |
| 71 |
return a.join('='); |
| 72 |
}) + |
| 73 |
'?='; |
| 74 |
} |
| 75 |
return str; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Get an array of UTF-8 hex codes for a given character. |
| 81 |
* @param {string} c The matched character. |
| 82 |
* @return {!Array<string>} A hex array representing the character. |
| 83 |
*/ |
| 84 |
goog.i18n.mime.getHexCharArray = function(c) { |
| 85 |
var i = c.charCodeAt(0); |
| 86 |
var a = []; |
| 87 |
// First convert the UCS-2 character into its UTF-8 bytes |
| 88 |
if (i < 128) { |
| 89 |
a.push(i); |
| 90 |
} else if (i <= 0x7ff) { |
| 91 |
a.push(0xc0 + ((i >> 6) & 0x3f), 0x80 + (i & 0x3f)); |
| 92 |
} else if (i <= 0xffff) { |
| 93 |
a.push( |
| 94 |
0xe0 + ((i >> 12) & 0x3f), 0x80 + ((i >> 6) & 0x3f), 0x80 + (i & 0x3f)); |
| 95 |
} else { |
| 96 |
// (This is defensive programming, since ecmascript isn't supposed |
| 97 |
// to handle code points that take more than 16 bits.) |
| 98 |
a.push( |
| 99 |
0xf0 + ((i >> 18) & 0x3f), 0x80 + ((i >> 12) & 0x3f), |
| 100 |
0x80 + ((i >> 6) & 0x3f), 0x80 + (i & 0x3f)); |
| 101 |
} |
| 102 |
// Now convert those bytes into hex strings (don't do anything with |
| 103 |
// a[0] as that's got the empty string that lets us use join()) |
| 104 |
for (i = a.length - 1; i >= 0; --i) { |
| 105 |
a[i] = a[i].toString(16); |
| 106 |
} |
| 107 |
return a; |
| 108 |
}; |