Permalink
Browse files

quoted-printable: Update dependencies

  • Loading branch information...
1 parent f93b9fe commit 3dbc5145967353c68d3a54e438811c0232df1251 @mathiasbynens committed May 5, 2014
View
@@ -28,10 +28,10 @@
var shouldDecode = this == encoded;
var value;
if (shouldDecode) {
- value = quotedPrintable.decode(encoded.value);
+ value = utf8.decode(quotedPrintable.decode(encoded.value));
decoded.value = value;
} else {
- value = quotedPrintable.encode(decoded.value);
+ value = quotedPrintable.encode(utf8.encode(decoded.value));
encoded.value = value;
}
value = decoded.value;
@@ -13,7 +13,8 @@
<textarea>If you believe that truth=3Dbeauty, then surely mathematics is the most bea=
utiful branch of philosophy.</textarea>
<h2>About this tool</h2>
-<p>This tool uses <a href=http://mths.be/quoted-printable><i>quoted-printable</i></a> to do all the encoding/decoding.
+<p>This tool uses <a href=http://mths.be/quoted-printable><i>quoted-printable</i></a> and <a href=http://mths.be/utf8js>utf8.js</a> to do all the encoding/decoding.
<p id=footer>Made by <a href=http://mathiasbynens.be/>@mathias</a> — <a href=https://github.com/mathiasbynens/mothereff.in/tree/master/quoted-printable>fork this on GitHub!</a></p>
<script src=vendor/quoted-printable.js></script>
+<script src=vendor/utf8.js></script>
<script src=eff.js></script>
@@ -1,4 +1,4 @@
-/*! http://mths.be/quoted-printable v0.1.1 by @mathias | MIT license */
+/*! http://mths.be/quoted-printable v0.2.1 by @mathias | MIT license */
;(function(root) {
// Detect free variables `exports`.
@@ -17,6 +17,7 @@
/*--------------------------------------------------------------------------*/
+ var stringFromCharCode = String.fromCharCode;
var decode = function(input) {
return input
// http://tools.ietf.org/html/rfc2045#section-6.7, rule 3:
@@ -29,16 +30,13 @@
// endings, but for compatibility reasons we should support separate CR
// and LF too.
.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);
});
};
@@ -49,17 +47,20 @@
};
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);
});
// Limit lines to 76 characters (not counting the CRLF line endings).
@@ -125,7 +126,7 @@
var quotedPrintable = {
'encode': encode,
'decode': decode,
- 'version': '0.1.1'
+ 'version': '0.2.1'
};
// Some AMD build optimizers, like r.js, check for specific condition patterns
@@ -136,18 +137,18 @@
define.amd
) {
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-
for (var key in quotedPrintable) {
quotedPrintable.hasOwnProperty(key) && (freeExports[key] = quotedPrintable[key]);
}
}
} else { // in Rhino or a web browser
- root.quotedPrintable = quotedPrintable
+ root.quotedPrintable = quotedPrintable;
}
}(this));
@@ -3,7 +3,8 @@
cd "$(dirname "${BASH_SOURCE}")"
curl -# "https://raw.githubusercontent.com/mathiasbynens/quoted-printable/master/quoted-printable.js" > quoted-printable.js
+curl -# "https://raw.githubusercontent.com/mathiasbynens/utf8.js/master/utf8.js" > utf8.js
-cat "quoted-printable.js" "../eff.js" > "/tmp/quoted-printable.js"
+cat "quoted-printable.js" "utf8.js" "../eff.js" > "/tmp/quoted-printable.js"
echo "Copying concatenated JS to pasteboard..."
pbcopy < "/tmp/quoted-printable.js"
@@ -0,0 +1,234 @@
+/*! http://mths.be/utf8js v2.0.0 by @mathias */
+;(function(root) {
+
+ // Detect free variables `exports`
+ var freeExports = typeof exports == 'object' && exports;
+
+ // Detect free variable `module`
+ var freeModule = typeof module == 'object' && module &&
+ module.exports == freeExports && module;
+
+ // Detect free variable `global`, from Node.js or Browserified code,
+ // and use it as `root`
+ var freeGlobal = typeof global == 'object' && global;
+ if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
+ root = freeGlobal;
+ }
+
+ /*--------------------------------------------------------------------------*/
+
+ var stringFromCharCode = String.fromCharCode;
+
+ // Taken from http://mths.be/punycode
+ function ucs2decode(string) {
+ var output = [];
+ var counter = 0;
+ var length = string.length;
+ var value;
+ var extra;
+ while (counter < length) {
+ value = string.charCodeAt(counter++);
+ if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
+ // high surrogate, and there is a next character
+ extra = string.charCodeAt(counter++);
+ if ((extra & 0xFC00) == 0xDC00) { // low surrogate
+ output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
+ } else {
+ // unmatched surrogate; only append this code unit, in case the next
+ // code unit is the high surrogate of a surrogate pair
+ output.push(value);
+ counter--;
+ }
+ } else {
+ output.push(value);
+ }
+ }
+ return output;
+ }
+
+ // Taken from http://mths.be/punycode
+ function ucs2encode(array) {
+ var length = array.length;
+ var index = -1;
+ var value;
+ var output = '';
+ while (++index < length) {
+ value = array[index];
+ if (value > 0xFFFF) {
+ value -= 0x10000;
+ output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
+ value = 0xDC00 | value & 0x3FF;
+ }
+ output += stringFromCharCode(value);
+ }
+ return output;
+ }
+
+ /*--------------------------------------------------------------------------*/
+
+ function createByte(codePoint, shift) {
+ return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
+ }
+
+ function encodeCodePoint(codePoint) {
+ if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
+ return stringFromCharCode(codePoint);
+ }
+ var symbol = '';
+ if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
+ symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
+ }
+ else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
+ symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
+ symbol += createByte(codePoint, 6);
+ }
+ else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
+ symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
+ symbol += createByte(codePoint, 12);
+ symbol += createByte(codePoint, 6);
+ }
+ symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
+ return symbol;
+ }
+
+ function utf8encode(string) {
+ var codePoints = ucs2decode(string);
+ var length = codePoints.length;
+ var index = -1;
+ var codePoint;
+ var byteString = '';
+ while (++index < length) {
+ codePoint = codePoints[index];
+ byteString += encodeCodePoint(codePoint);
+ }
+ return byteString;
+ }
+
+ /*--------------------------------------------------------------------------*/
+
+ function readContinuationByte() {
+ if (byteIndex >= byteCount) {
+ throw Error('Invalid byte index');
+ }
+
+ var continuationByte = byteArray[byteIndex] & 0xFF;
+ byteIndex++;
+
+ if ((continuationByte & 0xC0) == 0x80) {
+ return continuationByte & 0x3F;
+ }
+
+ // If we end up here, it’s not a continuation byte
+ throw Error('Invalid continuation byte');
+ }
+
+ function decodeSymbol() {
+ var byte1;
+ var byte2;
+ var byte3;
+ var byte4;
+ var codePoint;
+
+ if (byteIndex > byteCount) {
+ throw Error('Invalid byte index');
+ }
+
+ if (byteIndex == byteCount) {
+ return false;
+ }
+
+ // Read first byte
+ byte1 = byteArray[byteIndex] & 0xFF;
+ byteIndex++;
+
+ // 1-byte sequence (no continuation bytes)
+ if ((byte1 & 0x80) == 0) {
+ return byte1;
+ }
+
+ // 2-byte sequence
+ if ((byte1 & 0xE0) == 0xC0) {
+ var byte2 = readContinuationByte();
+ codePoint = ((byte1 & 0x1F) << 6) | byte2;
+ if (codePoint >= 0x80) {
+ return codePoint;
+ } else {
+ throw Error('Invalid continuation byte');
+ }
+ }
+
+ // 3-byte sequence (may include unpaired surrogates)
+ if ((byte1 & 0xF0) == 0xE0) {
+ byte2 = readContinuationByte();
+ byte3 = readContinuationByte();
+ codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
+ if (codePoint >= 0x0800) {
+ return codePoint;
+ } else {
+ throw Error('Invalid continuation byte');
+ }
+ }
+
+ // 4-byte sequence
+ if ((byte1 & 0xF8) == 0xF0) {
+ byte2 = readContinuationByte();
+ byte3 = readContinuationByte();
+ byte4 = readContinuationByte();
+ codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) |
+ (byte3 << 0x06) | byte4;
+ if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
+ return codePoint;
+ }
+ }
+
+ throw Error('Invalid UTF-8 detected');
+ }
+
+ var byteArray;
+ var byteCount;
+ var byteIndex;
+ function utf8decode(byteString) {
+ byteArray = ucs2decode(byteString);
+ byteCount = byteArray.length;
+ byteIndex = 0;
+ var codePoints = [];
+ var tmp;
+ while ((tmp = decodeSymbol()) !== false) {
+ codePoints.push(tmp);
+ }
+ return ucs2encode(codePoints);
+ }
+
+ /*--------------------------------------------------------------------------*/
+
+ var utf8 = {
+ 'version': '2.0.0',
+ 'encode': utf8encode,
+ 'decode': utf8decode
+ };
+
+ // Some AMD build optimizers, like r.js, check for specific condition patterns
+ // like the following:
+ if (
+ typeof define == 'function' &&
+ typeof define.amd == 'object' &&
+ define.amd
+ ) {
+ define(function() {
+ return utf8;
+ });
+ } else if (freeExports && !freeExports.nodeType) {
+ if (freeModule) { // in Node.js or RingoJS v0.8.0+
+ freeModule.exports = utf8;
+ } else { // in Narwhal or RingoJS v0.7.0-
+ var object = {};
+ var hasOwnProperty = object.hasOwnProperty;
+ for (var key in utf8) {
+ hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]);
+ }
+ }
+ } else { // in Rhino or a web browser
+ root.utf8 = utf8;
+ }
+
+}(this));

0 comments on commit 3dbc514

Please sign in to comment.