Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add `Quoted-Printable` encoder/decoder

  • Loading branch information...
commit f93b9fe63be6fa3ea553dd890c0b2280db081d7f 1 parent 107c976
@mathiasbynens authored
View
2  bacon/index.html
@@ -14,7 +14,7 @@
<div><label><input name=alphabet type=radio checked> use the common 24-letter cipher alphabet <code>ABCDEFGHIKLMNOPQRSTUWXYZ</code></label></div>
<div><label><input name=alphabet type=radio> use the full alphabet of 26 letters <code>ABCDEFGHIJKLMNOPQRSTUVWXYZ</code></label></div>
<h2>About this tool</h2>
-<p>This tool uses <a href=http://mths.be/bacon><i>bacon-cipher</i></a> to encode any string you enter in the ‘plaintext field, or to decode any Bacon-encoded ciphertext you enter in the other field.
+<p>This tool uses <a href=http://mths.be/bacon><i>bacon-cipher</i></a> to encode any string you enter in the ‘plaintext field, or to decode any Bacon-encoded ciphertext you enter in the other field.
<p id=footer>Made by <a href=http://mathiasbynens.be/>@mathias</a> — <a href=https://github.com/mathiasbynens/mothereff.in/tree/master/bacon>fork this on GitHub!</a></p>
<script src=vendor/bacon.js></script>
<script src=eff.js></script>
View
58 js-escapes/eff.js
@@ -1,32 +1,32 @@
(function(window, document, evil) {
- var pre = document.getElementsByTagName('pre')[0],
- code = document.getElementsByTagName('code')[0],
- textarea = document.getElementsByTagName('textarea')[0],
- inputs = document.getElementsByTagName('input'),
- checkboxOnlyASCII = inputs[0],
- checkboxES6 = inputs[1],
- checkboxStringBody = inputs[2],
- permalink = document.getElementById('permalink'),
- // http://mathiasbynens.be/notes/localstorage-pattern
- storage = (function() {
- var uid = new Date,
- storage,
- result;
- try {
- (storage = window.localStorage).setItem(uid, uid);
- result = storage.getItem(uid) == uid;
- storage.removeItem(uid);
- return result && storage;
- } catch(e) {}
- }()),
- cache = {
- '\n': '\\n',
- '\"': '\\\"',
- '\u2028': '\\u2028',
- '\u2029': '\\u2029',
- '\'': '\\\''
- };
+ var pre = document.getElementsByTagName('pre')[0];
+ var code = document.getElementsByTagName('code')[0];
+ var textarea = document.getElementsByTagName('textarea')[0];
+ var inputs = document.getElementsByTagName('input');
+ var checkboxOnlyASCII = inputs[0];
+ var checkboxES6 = inputs[1];
+ var checkboxStringBody = inputs[2];
+ var permalink = document.getElementById('permalink');
+ // http://mathiasbynens.be/notes/localstorage-pattern
+ var storage = (function() {
+ var uid = new Date;
+ var storage;
+ var result;
+ try {
+ (storage = window.localStorage).setItem(uid, uid);
+ result = storage.getItem(uid) == uid;
+ storage.removeItem(uid);
+ return result && storage;
+ } catch(exception) {}
+ }());
+ var cache = {
+ '\n': '\\n',
+ '\"': '\\\"',
+ '\u2028': '\\u2028',
+ '\u2029': '\\u2029',
+ '\'': '\\\''
+ };
function encode(string) {
// URL-encode some more characters to avoid issues when using permalink URLs in Markdown
@@ -138,8 +138,8 @@
// Google Analytics
window._gaq = [['_setAccount', 'UA-6065217-60'], ['_trackPageview']];
(function(d, t) {
- var g = d.createElement(t),
- s = d.getElementsByTagName(t)[0];
+ var g = d.createElement(t);
+ var s = d.getElementsByTagName(t)[0];
g.src = '//www.google-analytics.com/ga.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
View
5 quoted-printable/README.md
@@ -0,0 +1,5 @@
+# [`Quoted-Printable` encoder/decoder](http://mothereff.in/quoted-printable)
+
+This tool can be used to encode/decode any text using the `Quoted-Printable` content transfer encoding. It uses [_quoted-printable_](http://mths.be/quoted-printable) under the hood.
+
+Made by [Mathias Bynens](http://mathiasbynens.be/).
View
87 quoted-printable/eff.css
@@ -0,0 +1,87 @@
+html, textarea {
+ font: .8em/1.6 sans-serif;
+}
+
+body {
+ max-width: 40em;
+ padding: 0 1em;
+}
+
+h1 {
+ text-align: center;
+ font-size: 1.3em;
+ margin: 0 0 .5em;
+ padding-top: 1em;
+}
+
+h2 {
+ font-size: 1em;
+}
+
+a {
+ color: #333;
+ text-decoration: none;
+ border-bottom: 1px solid #aaa;
+ padding: .1em .2em;
+}
+
+a:hover, a:focus {
+ color: #fff;
+ border-color: #036;
+ background: #36c;
+}
+
+textarea {
+ font-family: Monaco, Consolas, monospace;
+}
+
+#footer {
+ margin-top: 2em;
+ text-align: center;
+}
+
+textarea {
+ border: 3px double green;
+ background: #90ee90;
+ width: 100%;
+ -moz-box-sizing: border-box;
+ -webkit-box-sizing: border-box;
+ box-sizing: border-box;
+ display: block;
+ margin: 1em 0 .5em;
+ padding: .7em;
+ resize: vertical;
+ min-height: 9.5em;
+}
+
+code {
+ font-family: Monaco, Consolas, monospace;
+ font-size: .9em;
+ white-space: pre;
+ white-space: pre-wrap;
+ word-wrap: break-word;
+}
+
+.invalid, :invalid {
+ border-color: red;
+ background: #ffb6c1;
+}
+
+@media (min-width: 42em) {
+
+ html {
+ font-size: 1.2em;
+ background: #c4c4c4;
+ height: 100%;
+ }
+
+ body {
+ margin: 0 auto;
+ padding: 0 2em;
+ min-height: 100%;
+ background: #fff;
+ border: solid #aaa;
+ border-width: 0 1px;
+ }
+
+}
View
72 quoted-printable/eff.js
@@ -0,0 +1,72 @@
+(function(window, document) {
+
+ var textareas = document.getElementsByTagName('textarea');
+ var decoded = textareas[0];
+ var encoded = textareas[1];
+ var permalink = document.getElementById('permalink');
+ // http://mathiasbynens.be/notes/localstorage-pattern
+ var storage = (function() {
+ var uid = new Date;
+ var storage;
+ var result;
+ try {
+ (storage = window.localStorage).setItem(uid, uid);
+ result = storage.getItem(uid) == uid;
+ storage.removeItem(uid);
+ return result && storage;
+ } catch (exception) {}
+ }());
+
+ function encode(string) {
+ // URL-encode some more characters to avoid issues when using permalink URLs in Markdown
+ return encodeURIComponent(string).replace(/['()_*]/g, function(character) {
+ return '%' + character.charCodeAt().toString(16);
+ });
+ }
+
+ function update() {
+ var shouldDecode = this == encoded;
+ var value;
+ if (shouldDecode) {
+ value = quotedPrintable.decode(encoded.value);
+ decoded.value = value;
+ } else {
+ value = quotedPrintable.encode(decoded.value);
+ encoded.value = value;
+ }
+ value = decoded.value;
+ permalink.hash = encode(value);
+ storage && (storage.quotedPrintable = value);
+ };
+
+ // http://mathiasbynens.be/notes/oninput
+ decoded.onkeyup = encoded.onkeyup = update;
+ decoded.oninput = encoded.oninput = function() {
+ decoded.onkeyup = encoded.onkeyup = null;
+ update.call(this);
+ };
+
+ if (storage) {
+ storage.quotedPrintable && (decoded.value = storage.quotedPrintable);
+ update();
+ }
+
+ window.onhashchange = function() {
+ decoded.value = decodeURIComponent(location.hash.slice(1));
+ update();
+ };
+
+ if (location.hash) {
+ window.onhashchange();
+ }
+
+}(this, document));
+
+// Google Analytics
+window._gaq = [['_setAccount', 'UA-6065217-60'], ['_trackPageview']];
+(function(d, t) {
+ var g = d.createElement(t),
+ s = d.getElementsByTagName(t)[0];
+ g.src = '//www.google-analytics.com/ga.js';
+ s.parentNode.insertBefore(g, s);
+}(document, 'script'));
View
19 quoted-printable/index.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html lang=en>
+<meta charset=utf-8>
+<title>`Quoted-Printable` encoder/decoder</title>
+<meta name=viewport content="width=device-width,initial-scale=1">
+<link rel=stylesheet href=eff.css>
+<meta name=description content="An online, on-the-fly `Quoted-Printable` encoder/decoder.">
+<h1><code>Quoted-Printable</code> encoder/decoder</h1>
+<noscript><strong>To use this tool, please <a href=http://enable-javascript.com/>enable JavaScript</a> and reload the page.</strong></noscript>
+<h2>Decoded:</h2>
+<textarea autofocus>If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy.</textarea>
+<h2>Encoded: (<a href=#If%20you%20believe%20that%20truth%3Dbeauty%2C%20then%20surely%20mathematics%20is%20the%20most%20beautiful%20branch%20of%20philosophy. id=permalink>permalink</a>)</h2>
+<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 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=eff.js></script>
View
153 quoted-printable/vendor/quoted-printable.js
@@ -0,0 +1,153 @@
+/*! http://mths.be/quoted-printable v0.1.1 by @mathias | MIT license */
+;(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 decode = function(input) {
+ return input
+ // http://tools.ietf.org/html/rfc2045#section-6.7, rule 3:
+ // “Therefore, when decoding a `Quoted-Printable` body, any trailing white
+ // space on a line must be deleted, as it will necessarily have been added
+ // by intermediate transport agents.”
+ .replace(/[\t\x20]$/gm, '')
+ // Remove hard line breaks. Note: this includes `=` followed by a line
+ // break. Proper `Quoted-Printable`-encoded data only contains CRLF line
+ // 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
+ // 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, '%'));
+ });
+ };
+
+ var handleTrailingCharacters = function(string) {
+ return string
+ .replace(/\x20$/, '=20') // Handle trailing space.
+ .replace(/\t$/, '=09') // Handle trailing tab.
+ };
+
+ 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 '';
+ }
+ return encodeURIComponent($0).replace(/%/g, '=');
+ });
+
+ // Limit lines to 76 characters (not counting the CRLF line endings).
+ var lines = encoded.split(/\r\n?|\n/g);
+ var lineIndex = -1;
+ var lineCount = lines.length;
+ var result = [];
+ while (++lineIndex < lineCount) {
+ var line = lines[lineIndex];
+ // Leave room for the trailing `=` for soft line breaks.
+ var LINE_LENGTH = 75;
+ var index = 0;
+ var length = line.length;
+ while (index < length) {
+ var buffer = encoded.slice(index, index + LINE_LENGTH);
+ // If this line ends with `=`, optionally followed by a single uppercase
+ // hexadecimal digit, we broke an escape sequence in half. Fix it by
+ // moving these characters to the next line.
+ if (/=$/.test(buffer)) {
+ buffer = buffer.slice(0, LINE_LENGTH - 1);
+ index += LINE_LENGTH - 1;
+ } else if (/=[A-F0-9]$/.test(buffer)) {
+ buffer = buffer.slice(0, LINE_LENGTH - 2);
+ index += LINE_LENGTH - 2;
+ } else {
+ index += LINE_LENGTH;
+ }
+ result.push(buffer);
+ }
+ }
+
+ // Encode space and tab characters at the end of encoded lines. Note that
+ // with the current implementation, this can only occur at the very end of
+ // the encoded string — every other line ends with `=` anyway.
+ var lastLineLength = buffer.length;
+ if (/[\t\x20]$/.test(buffer)) {
+ // There’s a space or a tab at the end of the last encoded line. Remove
+ // this line from the `result` array, as it needs to change.
+ result.pop();
+ if (lastLineLength + 2 <= LINE_LENGTH + 1) {
+ // It’s possible to encode the character without exceeding the line
+ // length limit.
+ result.push(
+ handleTrailingCharacters(buffer)
+ );
+ } else {
+ // It’s not possible to encode the character without exceeding the line
+ // length limit. Remvoe the character from the line, and insert a new
+ // line that contains only the encoded character.
+ result.push(
+ buffer.slice(0, lastLineLength - 1),
+ handleTrailingCharacters(
+ buffer.slice(lastLineLength - 1, lastLineLength)
+ )
+ );
+ }
+ }
+
+ // `Quoted-Printable` uses CRLF.
+ return result.join('=\r\n');
+ };
+
+ var quotedPrintable = {
+ 'encode': encode,
+ 'decode': decode,
+ 'version': '0.1.1'
+ };
+
+ // 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 quotedPrintable
+ });
+ } else if (freeExports && !freeExports.nodeType) {
+ if (freeModule) { // in Node.js or RingoJS v0.8.0+
+ 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
+ }
+
+}(this));
View
9 quoted-printable/vendor/update.sh
@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+
+cd "$(dirname "${BASH_SOURCE}")"
+
+curl -# "https://raw.githubusercontent.com/mathiasbynens/quoted-printable/master/quoted-printable.js" > quoted-printable.js
+
+cat "quoted-printable.js" "../eff.js" > "/tmp/quoted-printable.js"
+echo "Copying concatenated JS to pasteboard..."
+pbcopy < "/tmp/quoted-printable.js"
Please sign in to comment.
Something went wrong with that request. Please try again.