Skip to content
This repository has been archived by the owner on Dec 21, 2018. It is now read-only.

Commit

Permalink
Escape unicode before submitting to the website.
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmon committed Aug 27, 2012
1 parent 87c8ae3 commit 05693e3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ AuthManager.prototype.checkUser = function(nick) {

AuthManager.prototype._askNickserv = function(nick) {
var msg = 'status ' + nick;
client.say('nickserv', msg);
irc_client.say('nickserv', msg);
};

exports.AuthManager = AuthManager;
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ function eq(a, b, name) {
actual = utils.parseArgs(args);
eq(expected, actual, "Bare apostrophe");
})();


(function test_escapeUnicode() {
var actual;
var expected;
var obj;

obj = {foo: 'bar'};
actual = utils.jsonStringifyUnicode(obj);
expected = JSON.stringify(obj);
eq(expected, actual, "Basic JSON");

actual = utils.jsonStringifyUnicode('I like π', true);
expected = '"I like π"';
eq(expected, actual, "Unicode mode");

actual = utils.jsonStringifyUnicode('I like π', false);
expected = '"I like \\u03c0"';
eq(expected, actual, "Escape mode");

actual = utils.jsonStringifyUnicode('I like π');
expected = '"I like \\u03c0"';
eq(expected, actual, "Default mode");
})();
26 changes: 20 additions & 6 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var _ = require('underscore');
var http = require('http');
var events = require('events');

var request = function(path, method, data, emitter) {
var request = function(path, method, data, emitter, unicode) {
if (data === undefined) {
data = {};
}
var body = JSON.stringify(data);
var body = exports.jsonStringifyUnicode(data, unicode);
var options = {
host: config.standup.host,
port: config.standup.port,
Expand Down Expand Up @@ -48,7 +48,7 @@ var request = function(path, method, data, emitter) {
});

return emitter;
}
};

exports.request = request;

Expand All @@ -68,8 +68,8 @@ exports.ifAuthorized = function(user, channel, callback) {
};

exports.escapeRegExp = function(str) {
return str.replace(/[-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
}
return str.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
};

/* Parse things like quotes strings from an argument list. */
exports.parseArgs = function(argList) {
Expand Down Expand Up @@ -104,4 +104,18 @@ exports.parseArgs = function(argList) {
}
});
return args;
}
};

exports.jsonStringifyUnicode = function(str, emitUnicode) {
if (emitUnicode === undefined) {
emitUnicoe = false;
}

var json = JSON.stringify(str);
if (!emitUnicode) {
json = json.replace(/[\u007f-\uffff]/g, function(c) {
return '\\u'+('0000'+c.charCodeAt(0).toString(16)).slice(-4);
});
}
return json;
};

0 comments on commit 05693e3

Please sign in to comment.