mime.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * @fileoverview Functions for encoding strings according to MIME
  16. * standards, especially RFC 1522.
  17. */
  18. goog.provide('goog.i18n.mime');
  19. goog.provide('goog.i18n.mime.encode');
  20. goog.require('goog.array');
  21. /**
  22. * Regular expression for matching those characters that are outside the
  23. * range that can be used in the quoted-printable encoding of RFC 1522:
  24. * anything outside the 7-bit ASCII encoding, plus ?, =, _ or space.
  25. * @type {RegExp}
  26. * @private
  27. */
  28. goog.i18n.mime.NONASCII_ = /[^!-<>@-^`-~]/g;
  29. /**
  30. * Like goog.i18n.NONASCII_ but also omits double-quotes.
  31. * @type {RegExp}
  32. * @private
  33. */
  34. goog.i18n.mime.NONASCII_NOQUOTE_ = /[^!#-<>@-^`-~]/g;
  35. /**
  36. * Encodes a string for inclusion in a MIME header. The string is encoded
  37. * in UTF-8 according to RFC 1522, using quoted-printable form.
  38. * @param {string} str The string to encode.
  39. * @param {boolean=} opt_noquote Whether double-quote characters should also
  40. * be escaped (should be true if the result will be placed inside a
  41. * quoted string for a parameter value in a MIME header).
  42. * @return {string} The encoded string.
  43. */
  44. goog.i18n.mime.encode = function(str, opt_noquote) {
  45. var nonascii =
  46. opt_noquote ? goog.i18n.mime.NONASCII_NOQUOTE_ : goog.i18n.mime.NONASCII_;
  47. if (str.search(nonascii) >= 0) {
  48. str = '=?UTF-8?Q?' +
  49. str.replace(
  50. nonascii,
  51. /**
  52. * @param {string} c The matched char.
  53. * @return {string} The quoted-printable form of utf-8 encoding.
  54. */
  55. function(c) {
  56. var i = c.charCodeAt(0);
  57. if (i == 32) {
  58. // Special case for space, which can be encoded as _ not =20
  59. return '_';
  60. }
  61. var a = goog.array.concat('', goog.i18n.mime.getHexCharArray(c));
  62. return a.join('=');
  63. }) +
  64. '?=';
  65. }
  66. return str;
  67. };
  68. /**
  69. * Get an array of UTF-8 hex codes for a given character.
  70. * @param {string} c The matched character.
  71. * @return {!Array<string>} A hex array representing the character.
  72. */
  73. goog.i18n.mime.getHexCharArray = function(c) {
  74. var i = c.charCodeAt(0);
  75. var a = [];
  76. // First convert the UCS-2 character into its UTF-8 bytes
  77. if (i < 128) {
  78. a.push(i);
  79. } else if (i <= 0x7ff) {
  80. a.push(0xc0 + ((i >> 6) & 0x3f), 0x80 + (i & 0x3f));
  81. } else if (i <= 0xffff) {
  82. a.push(
  83. 0xe0 + ((i >> 12) & 0x3f), 0x80 + ((i >> 6) & 0x3f), 0x80 + (i & 0x3f));
  84. } else {
  85. // (This is defensive programming, since ecmascript isn't supposed
  86. // to handle code points that take more than 16 bits.)
  87. a.push(
  88. 0xf0 + ((i >> 18) & 0x3f), 0x80 + ((i >> 12) & 0x3f),
  89. 0x80 + ((i >> 6) & 0x3f), 0x80 + (i & 0x3f));
  90. }
  91. // Now convert those bytes into hex strings (don't do anything with
  92. // a[0] as that's got the empty string that lets us use join())
  93. for (i = a.length - 1; i >= 0; --i) {
  94. a[i] = a[i].toString(16);
  95. }
  96. return a;
  97. };