Permalink
Browse files

fix linebreak = in QP at end of string

  • Loading branch information...
andris9 committed Dec 22, 2012
1 parent c494c59 commit e9211423f392628edc8a0f139c5d97b930e48faf
Showing with 30 additions and 22 deletions.
  1. +1 −1 README.md
  2. +20 −20 lib/mimelib.js
  3. +1 −1 package.json
  4. +8 −0 test/mimelib.js
View
@@ -93,7 +93,7 @@ Decodes a string from Quoted-printable format (see also `encodeQuotedPrintable`
Encodes a string into Base64 format. Base64 is mime-word safe (see also `decodeBase64`)
- mimelib.encodeBase64(str [, charset]) -> String
+ mimelib.encodeBase64(str [, charset]) -> String
- `str` (String): String to be encoded into Base64
- `charset` (String): Destination charset, defaults to UTF-8
View
@@ -27,7 +27,7 @@ this.foldLine = function(str, maxLength, foldAnywhere, afterSpace, lineMargin){
* @return {String} Mime word encoded string
*/
module.exports.encodeMimeWord = function(str, encoding, charset, maxLength){
- return module.exports.mimeFunctions.encodeMimeWord(str, encoding, maxLength || 0, charset);
+ return module.exports.mimeFunctions.encodeMimeWord(str, encoding, maxLength || 0, charset);
};
/**
@@ -40,13 +40,13 @@ module.exports.encodeMimeWord = function(str, encoding, charset, maxLength){
* @return {String} String with possible mime word encoded parts
*/
module.exports.encodeMimeWords = function(str, encoding, maxLength, charset){
- return module.exports.mimeFunctions.encodeMimeWords(str, encoding, maxLength || 0, charset);
+ return module.exports.mimeFunctions.encodeMimeWords(str, encoding, maxLength || 0, charset);
};
/**
* Decodes a string from mime encoded word
*
- * @param {String} str Mime word encoded string
+ * @param {String} str Mime word encoded string
* @return {String} Decoded string
*/
module.exports.decodeMimeWord = function(str){
@@ -95,15 +95,15 @@ module.exports.decodeQuotedPrintable = function(str, mimeWord, charset){
}
charset = (charset || "").toString().toUpperCase().trim();
var decodedString = module.exports.mimeFunctions.decodeQuotedPrintable(str, "utf-8", charset);
- return charset == "BINARY" ? decodedString : decodedString.toString("utf-8");
+ return charset == "BINARY" ? decodedString : decodedString.toString("utf-8");
};
/**
* Encodes a string into Base64 format. Base64 is mime-word safe
*
* @param {String} str String to be encoded into Base64
* @param {String} [charset="UTF-8"] Destination charset
- * @return {String} Base64 encoded string
+ * @return {String} Base64 encoded string
*/
module.exports.encodeBase64 = function(str, charset){
return module.exports.mimeFunctions.encodeBase64(str, charset);
@@ -114,7 +114,7 @@ module.exports.encodeBase64 = function(str, charset){
*
* @param {String} str String to be decoded from Base64
* @param {String} [charset="UTF-8"] Source charset
- * @return {String} Decoded string
+ * @return {String} Decoded string
*/
module.exports.decodeBase64 = function(str, charset){
return module.exports.mimeFunctions.decodeBase64(str, "utf-8", charset).toString("utf-8");
@@ -138,7 +138,7 @@ module.exports.parseAddresses = function(addresses){
/**
* Parses header lines into an array of objects. Output: {'x-header': ['value']}
*
- * @param {String} headers Full header part to be parsed
+ * @param {String} headers Full header part to be parsed
* @return {Object} Parsed headers
*/
module.exports.parseHeaders = function(headers){
@@ -175,7 +175,7 @@ module.exports.mimeFunctions = {
toCharset = toCharset || "UTF-8";
fromCharset = fromCharset || "UTF-8";
- var buffer = convert(str || "", toCharset, fromCharset),
+ var buffer = convert(str || "", toCharset, fromCharset),
ranges = [[0x09],
[0x0A],
[0x0D],
@@ -199,11 +199,11 @@ module.exports.mimeFunctions = {
},
mimeDecode: function(str, toCharset, fromCharset){
- str = (str || "").toString();
+ str = (str || "").toString();
toCharset = toCharset || "UTF-8";
fromCharset = fromCharset || "UTF-8";
- var encodedBytesCount = (str.match(/\=[\da-fA-F]{2}/g) || []).length,
+ var encodedBytesCount = (str.match(/\=[\da-fA-F]{2}/g) || []).length,
bufferLength = str.length - encodedBytesCount * 2,
chr, hex,
buffer = new Buffer(bufferLength),
@@ -237,7 +237,7 @@ module.exports.mimeFunctions = {
decodeQuotedPrintable: function(str, toCharset, fromCharset){
str = (str || "").toString();
- str = str.replace(/\=\r?\n/g, "");
+ str = str.replace(/\=(?:\r?\n|$)/g, "");
return this.mimeDecode(str, toCharset, fromCharset);
},
@@ -255,8 +255,8 @@ module.exports.mimeFunctions = {
},
encodeMimeWord: function(str, encoding, maxLength, toCharset, fromCharset){
- toCharset = (toCharset || "utf-8").toString().toUpperCase().trim();
- encoding = (encoding || "Q").toString().toUpperCase().trim().charAt(0);
+ toCharset = (toCharset || "utf-8").toString().toUpperCase().trim();
+ encoding = (encoding || "Q").toString().toUpperCase().trim().charAt(0);
var encodedStr;
if(maxLength && maxLength > 7 + toCharset.length){
@@ -291,7 +291,7 @@ module.exports.mimeFunctions = {
},
decodeMimeWord: function(str, toCharset){
- str = (str || "").toString().trim();
+ str = (str || "").toString().trim();
var fromCharset, encoding, match;
@@ -302,7 +302,7 @@ module.exports.mimeFunctions = {
fromCharset = match[1];
encoding = (match[2] || "Q").toString().toUpperCase();
- str = (match[3] || "").replace(/_/g, " ");
+ str = (match[3] || "").replace(/_/g, " ");
if(encoding == "B"){
return this.decodeBase64(str, toCharset, fromCharset);
@@ -332,8 +332,8 @@ module.exports.mimeFunctions = {
},
foldLine: function(str, lineLengthMax, afterSpace, lineMargin){
- lineLengthMax = lineLengthMax || 76;
- str = (str || "").toString().trim();
+ lineLengthMax = lineLengthMax || 76;
+ str = (str || "").toString().trim();
var pos = 0, len = str.length, result = "", line, match, lineMargin = lineMargin || Math.floor(lineLengthMax/5);
@@ -397,7 +397,7 @@ module.exports.mimeFunctions = {
header = this.decodeHeaderLine(lines[i]);
key = (header[0] || "").toString().toLowerCase().trim();
value = header[1] || "";
- if(!toCharset || (toCharset || "").toString().trim().match(/^utf[\-_]?8$/i)){
+ if(!toCharset || (toCharset || "").toString().trim().match(/^utf[\-_]?8$/i)){
value = value.toString("utf-8");
}
if(!headersObj[key]){
@@ -411,9 +411,9 @@ module.exports.mimeFunctions = {
},
decodeHeaderLine: function(header, toCharset){
- var line = (header || "").toString().replace(/(?:\r?\n|\r)[ \t]*/g, " ").trim(),
+ var line = (header || "").toString().replace(/(?:\r?\n|\r)[ \t]*/g, " ").trim(),
match = line.match(/^\s*([^:]+):(.*)$/),
- key = (match && match[1] || "").trim(),
+ key = (match && match[1] || "").trim(),
value = (match && match[2] || "").trim();
value = this.decodeMimeWords(value, toCharset);
View
@@ -1,7 +1,7 @@
{
"name": "mimelib",
"description": "MIME functions to encode/decode e-mails etc.",
- "version": "0.2.8",
+ "version": "0.2.9",
"author" : "Andris Reinman",
"homepage":"http://github.com/andris9/mimelib",
"maintainers":[
View
@@ -64,6 +64,14 @@ exports["Quoted printable"] = {
test.done();
},
+ "Decode string with soft linebreaks": function(test){
+ var input = "Tere =\r\nvana kere=",
+ output = "Tere vana kere";
+
+ test.equal(output, mimelib.decodeQuotedPrintable(input));
+ test.done();
+ },
+
"Surrogate pair": function(test){
// 💩 pile of poo
test.equal("=F0=9F=92=A9", mimelib.encodeQuotedPrintable('\ud83d\udca9'))

0 comments on commit e921142

Please sign in to comment.