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