quoted-printable
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "quoted-printable", | ||
"version": "0.1.1", | ||
"description": "A robust JavaScript implementation of the `Quoted-Printable` content transfer encoding as defined by RFC 2045, using UTF-8 for any non-ASCII symbols.", | ||
"version": "0.2.0", | ||
"description": "A robust & character encoding–agnostic JavaScript implementation of the `Quoted-Printable` content transfer encoding as defined by RFC 2045.", | ||
"homepage": "http://mths.be/quoted-printable", | ||
@@ -6,0 +6,0 @@ "main": "quoted-printable.js", |
@@ -1,2 +0,2 @@ | ||
/*! http://mths.be/quoted-printable v0.1.1 by @mathias | MIT license */ | ||
/*! http://mths.be/quoted-printable v0.2.0 by @mathias | MIT license */ | ||
;(function(root) { | ||
@@ -20,2 +20,3 @@ | ||
var stringFromCharCode = String.fromCharCode; | ||
var decode = function(input) { | ||
@@ -33,12 +34,9 @@ return input | ||
.replace(/=?(?:\r\n?|\n)/g, '') | ||
// Remove encoded lone surrogates from the input. | ||
// TODO (?): Show warning along the lines of “lone surrogates values | ||
// detected in input” or “only scalar values are allowed”. | ||
.replace(/=ED=[AB][0-9A-F]=[89AB][0-9A-F]/gi, '') | ||
// Decode series of escape sequences of the form `=XX` where `XX` is any | ||
// Decode escape sequences of the form `=XX` where `XX` is any | ||
// combination of two hexidecimal digits. For optimal compatibility, | ||
// lowercase hexadecimal digits are supported as well. See | ||
// http://tools.ietf.org/html/rfc2045#section-6.7, note 1. | ||
.replace(/(?:=[a-fA-F0-9]{2})+/g, function($0) { | ||
return decodeURIComponent($0.replace(/=/g, '%')); | ||
.replace(/=([a-fA-F0-9]{2})/g, function($0, $1) { | ||
var codePoint = parseInt($1, 16); | ||
return stringFromCharCode(codePoint); | ||
}); | ||
@@ -54,13 +52,16 @@ }; | ||
var regexUnsafeSymbols = /[\0-\b\n-\x1F=\x7F-\uD7FF\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF]/g; | ||
var regexLoneSurrogate = /^[\uD800-\uDFFF]$/; | ||
var encode = function(string) { | ||
// Encode symbols that are definitely unsafe (i.e. unsafe in any context). | ||
var encoded = string.replace(regexUnsafeSymbols, function($0) { | ||
if (regexLoneSurrogate.test($0)) { | ||
// TODO (?): Show warning along the lines of “lone surrogates values | ||
// detected in input” or “only scalar values are allowed”. | ||
return ''; | ||
var encoded = string.replace(regexUnsafeSymbols, function(symbol) { | ||
if (symbol > '\xFF') { | ||
throw RangeError( | ||
'`quotedPrintable.encode()` expects extended ASCII input only. ' + | ||
'Don\u2019t forget to encode the input first using a character ' + | ||
'encoding like UTF-8.' | ||
); | ||
} | ||
return encodeURIComponent($0).replace(/%/g, '='); | ||
var codePoint = symbol.charCodeAt(0); | ||
var hexadecimal = codePoint.toString(16).toUpperCase(); | ||
return '=' + ('0' + hexadecimal).slice(-2); | ||
}); | ||
@@ -131,3 +132,3 @@ | ||
'decode': decode, | ||
'version': '0.1.1' | ||
'version': '0.2.0' | ||
}; | ||
@@ -143,7 +144,7 @@ | ||
define(function() { | ||
return quotedPrintable | ||
return quotedPrintable; | ||
}); | ||
} else if (freeExports && !freeExports.nodeType) { | ||
if (freeModule) { // in Node.js or RingoJS v0.8.0+ | ||
freeModule.exports = quotedPrintable | ||
freeModule.exports = quotedPrintable; | ||
} else { // in Narwhal or RingoJS v0.7.0- | ||
@@ -155,5 +156,5 @@ for (var key in quotedPrintable) { | ||
} else { // in Rhino or a web browser | ||
root.quotedPrintable = quotedPrintable | ||
root.quotedPrintable = quotedPrintable; | ||
} | ||
}(this)); |
# quoted-printable [![Build status](https://travis-ci.org/mathiasbynens/quoted-printable.svg?branch=master)](https://travis-ci.org/mathiasbynens/quoted-printable) [![Dependency status](https://gemnasium.com/mathiasbynens/quoted-printable.svg)](https://gemnasium.com/mathiasbynens/quoted-printable) | ||
_quoted-printable_ is a JavaScript implementation of [the `Quoted-Printable` content transfer encoding as defined by RFC 2045](http://tools.ietf.org/html/rfc2045#section-6.7), using UTF-8 for any non-ASCII symbols. It can be used to encode plaintext to its `Quoted-Printable` encoding, or the other way around (i.e. decoding). [Here’s an online demo.](http://mothereff.in/quoted-printable) | ||
_quoted-printable_ is a character encoding–agnostic JavaScript implementation of [the `Quoted-Printable` content transfer encoding as defined by RFC 2045](http://tools.ietf.org/html/rfc2045#section-6.7). It can be used to encode plaintext to its `Quoted-Printable` encoding, or the other way around (i.e. decoding). [Here’s an online demo using the UTF-8 character encoding.](http://mothereff.in/quoted-printable) | ||
@@ -65,23 +65,27 @@ ## Installation | ||
### `quotedPrintable.encode(text)` | ||
### `quotedPrintable.encode(input)` | ||
This function takes a string of text (the `text` parameter) and `Quoted-Printable`-encodes it, using UTF-8 to encode any non-ASCII symbols. | ||
This function takes an encoded byte string (the `input` parameter) and `Quoted-Printable`-encodes it. Each item in the input string represents an octet as per the desired character encoding. Here’s an example that uses UTF-8: | ||
```js | ||
quotedPrintable.encode('foo=bar'); | ||
var utf8 = require('utf8'); | ||
quotedPrintable.encode(utf8.encode('foo=bar')); | ||
// → 'foo=3Dbar' | ||
quotedPrintable.encode('Iñtërnâtiônàlizætiøn☃💩'); | ||
quotedPrintable.encode(utf8.encode('Iñtërnâtiônàlizætiøn☃💩')); | ||
// → 'I=C3=B1t=C3=ABrn=C3=A2ti=C3=B4n=C3=A0liz=C3=A6ti=C3=B8n=E2=98=83=F0=9F=92=\r\n=A9' | ||
``` | ||
### `quoted-printable.decode(text, options)` | ||
### `quotedPrintable.decode(text)` | ||
This function takes a string of text (the `text` parameter) and `Quoted-Printable`-decodes it, using UTF-8 to decode any non-ASCII octets. | ||
This function takes a string of text (the `text` parameter) and `Quoted-Printable`-decodes it. The return value is a ‘byte string’, i.e. a string of which each item represents an octet as per the character encoding that’s being used. Here’s an example that uses UTF-8: | ||
```js | ||
quoted-printable.decode('foo=3Dbar'); | ||
var utf8 = require('utf8'); | ||
utf8.decode(quotedPrintable.decode('foo=3Dbar')); | ||
// → 'foo=bar' | ||
quoted-printable.decode('I=C3=B1t=C3=ABrn=C3=A2ti=C3=B4n=C3=A0liz=C3=A6ti=C3=B8n=E2=98=83=F0=9F=92=\r\n=A9'); | ||
utf8.decode(quotedPrintable.decode('I=C3=B1t=C3=ABrn=C3=A2ti=C3=B4n=C3=A0liz=C3=A6ti=C3=B8n=E2=98=83=F0=9F=92=\r\n=A9')); | ||
// → 'Iñtërnâtiônàlizætiøn☃💩' | ||
@@ -98,3 +102,3 @@ ``` | ||
After that, you’ll be able to use `quoted-printable` on the command line: | ||
After that, you’ll be able to use `quoted-printable` on the command line. Note that while the _quoted-printable_ library itself is character encoding–agnostic, the command-line tool applies the UTF-8 character encoding on all input. | ||
@@ -101,0 +105,0 @@ ```bash |
Sorry, the diff of this file is not supported yet
17656
140
152