Refactor: rename messageSanitize to Tools.parseMessage

This commit is contained in:
Guangcong Luo 2013-05-11 23:09:38 -07:00
parent d4c2b0441d
commit 2d43510f26
5 changed files with 74 additions and 74 deletions

View File

@ -4631,9 +4631,9 @@ function Battle(frame, logFrame, noPreload) {
args.shift();
var message = args.join('|');
if (message.substr(0,2) === '//') {
self.log('<div class="chat"><strong style="' + hashColor(toUserid(name)) + '">' + Tools.escapeHTML(name) + ':</strong> <em>' + messageSanitize(message.substr(1)) + '</em></div>', preempt);
self.log('<div class="chat"><strong style="' + hashColor(toUserid(name)) + '">' + Tools.escapeHTML(name) + ':</strong> <em>' + Tools.parseMessage(message.substr(1)) + '</em></div>', preempt);
} else if (message.substr(0,4).toLowerCase() === '/me ') {
self.log('<div class="chat"><strong style="' + hashColor(toUserid(name)) + '">&bull;</strong> <em>' + Tools.escapeHTML(name) + ' <i>' + messageSanitize(message.substr(4)) + '</i></em></div>', preempt);
self.log('<div class="chat"><strong style="' + hashColor(toUserid(name)) + '">&bull;</strong> <em>' + Tools.escapeHTML(name) + ' <i>' + Tools.parseMessage(message.substr(4)) + '</i></em></div>', preempt);
} else if (message.substr(0,14).toLowerCase() === '/data-pokemon ') {
self.log('<div class="chat"><ul class=\"utilichart\">'+Chart.pokemonRow(Tools.getTemplate(message.substr(14)),'',{})+'<li style=\"clear:both\"></li></ul></div>', preempt);
} else if (message.substr(0,11).toLowerCase() === '/data-item ') {
@ -4643,7 +4643,7 @@ function Battle(frame, logFrame, noPreload) {
} else if (message.substr(0,11).toLowerCase() === '/data-move ') {
self.log('<div class="chat"><ul class=\"utilichart\">'+Chart.moveRow(Tools.getMove(message.substr(11)),'',{})+'<li style=\"clear:both\"></li></ul></div>', preempt);
} else {
self.log('<div class="chat"><strong style="' + hashColor(toUserid(name)) + '" class="username" data-name="'+Tools.escapeHTML(name)+'">' + Tools.escapeHTML(name) + ':</strong> <em>' + messageSanitize(message) + '</em></div>', preempt);
self.log('<div class="chat"><strong style="' + hashColor(toUserid(name)) + '" class="username" data-name="'+Tools.escapeHTML(name)+'">' + Tools.escapeHTML(name) + ':</strong> <em>' + Tools.parseMessage(message) + '</em></div>', preempt);
}
break;
case 'chatmsg':
@ -4658,7 +4658,7 @@ function Battle(frame, logFrame, noPreload) {
self.log('<div class="chat">' + Tools.sanitizeHTML(list) + '</div>', preempt);
break;
case 'pm':
self.log('<div class="chat"><strong>' + Tools.escapeHTML(args[1]) + ':</strong> <span class="message-pm"><i style="cursor:pointer" onclick="selectTab(\'lobby\');rooms.lobby.popupOpen(\'' + Tools.escapeHTML(args[2], true) + '\')">(Private to ' + Tools.escapeHTML(args[3]) + ')</i> ' + messageSanitize(args[4]) + '</span>');
self.log('<div class="chat"><strong>' + Tools.escapeHTML(args[1]) + ':</strong> <span class="message-pm"><i style="cursor:pointer" onclick="selectTab(\'lobby\');rooms.lobby.popupOpen(\'' + Tools.escapeHTML(args[2], true) + '\')">(Private to ' + Tools.escapeHTML(args[3]) + ')</i> ' + Tools.parseMessage(args[4]) + '</span>');
break;
case 'askreg':
self.log('<div class="broadcast-blue"><b>Register an account to protect your ladder rating!</b><br /><button name="register" value="'+Tools.escapeHTML(args[1])+'"><b>Register</b></button></div>');

View File

@ -101,69 +101,6 @@ function hashColor(name) {
return colorCache[name];
}
function messageSanitize(str) {
str = Tools.escapeHTML(str);
// Don't format console commands (>>).
if (str.substr(0, 8) === '&gt;&gt;') return str;
// Don't format console results (<<).
if (str.substr(0, 8) === '&lt;&lt;') return str;
return str.
// ``code``
replace(/\`\`([^< ]([^<`]*?[^< ])?)\`\`/g, '<code>$1</code>').
// ~~strikethrough~~
replace(/\~\~([^< ]([^<]*?[^< ])?)\~\~/g, '<s>$1</s>').
// linking of URIs
replace(/(https?\:\/\/[a-z0-9-.]+(\/([^\s]*[^\s?.,])?)?|[a-z0-9]([a-z0-9-\.]*[a-z0-9])?\.(com|org|net|edu|us)((\/([^\s]*[^\s?.,])?)?|\b))/ig, function(uri) {
// Insert http:// before URIs without a URI scheme specified.
var fulluri = uri.replace(/^([a-z]*[^a-z:])/g, 'http://$1');
var event;
if (Tools.interstice.isWhitelisted(fulluri)) {
event = 'External link';
} else {
event = 'Interstice link';
fulluri = Tools.escapeHTML(Tools.interstice.getURI(Tools.unescapeHTML(fulluri)));
}
return '<a href="' + fulluri +
'" target="_blank" onclick="if (window._gaq) _gaq.push([\'_trackEvent\', \'' +
event +
'\', \'' + Tools.escapeQuotes(fulluri) + '\']);">' + uri + '</a>';
}).
// google [blah]
// google[blah]
// Google search for 'blah'
replace(/(\bgoogle ?\[([^\]<]+)\])/ig, function(p0, p1, p2) {
p2 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p2)));
return '<a href="http://www.google.com/search?ie=UTF-8&q=' + p2 +
'" target="_blank">' + p1 + '</a>';
}).
// gl [blah]
// gl[blah
// Google search for 'blah' and visit the first result ("I'm feeling lucky")
replace(/(\bgl ?\[([^\]<]+)\])/ig, function(p0, p1, p2) {
p2 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p2)));
return '<a href="http://www.google.com/search?ie=UTF-8&btnI&q=' + p2 +
'" target="_blank">' + p1 + '</a>';
}).
// wiki [blah]
// Search Wikipedia for 'blah' (and visit the article for 'blah' if it exists)
replace(/(\bwiki ?\[([^\]<]+)\])/ig, function(p0, p1, p2) {
p2 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p2)));
return '<a href="http://en.wikipedia.org/w/index.php?title=Special:Search&search=' +
p2 + '" target="_blank">' + p1 + '</a>';
}).
// [[blah]]
// Short form of gl[[blah]]
replace(/\[\[([^< ]([^<`]*?[^< ])?)\]\]/ig, function(p0, p1) {
var q = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p1)));
return '<a href="http://www.google.com/search?ie=UTF-8&btnI&q=' + q +
'" target="_blank">' + p1 +'</a>';
}).
// __italics__
replace(/\_\_([^< ]([^<]*?[^< ])?)\_\_/g, '<i>$1</i>').
// **bold**
replace(/\*\*([^< ]([^<]*?[^< ])?)\*\*/g, '<b>$1</b>');
}
function toId(text) {
text = text || '';
if (typeof text === 'number') text = ''+text;
@ -360,6 +297,69 @@ var Tools = {
return Tools.escapeHTML(formatid);
},
parseMessage: function(str) {
str = Tools.escapeHTML(str);
// Don't format console commands (>>).
if (str.substr(0, 8) === '&gt;&gt;') return str;
// Don't format console results (<<).
if (str.substr(0, 8) === '&lt;&lt;') return str;
return str.
// ``code``
replace(/\`\`([^< ]([^<`]*?[^< ])?)\`\`/g, '<code>$1</code>').
// ~~strikethrough~~
replace(/\~\~([^< ]([^<]*?[^< ])?)\~\~/g, '<s>$1</s>').
// linking of URIs
replace(/(https?\:\/\/[a-z0-9-.]+(\/([^\s]*[^\s?.,])?)?|[a-z0-9]([a-z0-9-\.]*[a-z0-9])?\.(com|org|net|edu|us)((\/([^\s]*[^\s?.,])?)?|\b))/ig, function(uri) {
// Insert http:// before URIs without a URI scheme specified.
var fulluri = uri.replace(/^([a-z]*[^a-z:])/g, 'http://$1');
var event;
if (Tools.interstice.isWhitelisted(fulluri)) {
event = 'External link';
} else {
event = 'Interstice link';
fulluri = Tools.escapeHTML(Tools.interstice.getURI(Tools.unescapeHTML(fulluri)));
}
return '<a href="' + fulluri +
'" target="_blank" onclick="if (window._gaq) _gaq.push([\'_trackEvent\', \'' +
event +
'\', \'' + Tools.escapeQuotes(fulluri) + '\']);">' + uri + '</a>';
}).
// google [blah]
// google[blah]
// Google search for 'blah'
replace(/(\bgoogle ?\[([^\]<]+)\])/ig, function(p0, p1, p2) {
p2 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p2)));
return '<a href="http://www.google.com/search?ie=UTF-8&q=' + p2 +
'" target="_blank">' + p1 + '</a>';
}).
// gl [blah]
// gl[blah
// Google search for 'blah' and visit the first result ("I'm feeling lucky")
replace(/(\bgl ?\[([^\]<]+)\])/ig, function(p0, p1, p2) {
p2 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p2)));
return '<a href="http://www.google.com/search?ie=UTF-8&btnI&q=' + p2 +
'" target="_blank">' + p1 + '</a>';
}).
// wiki [blah]
// Search Wikipedia for 'blah' (and visit the article for 'blah' if it exists)
replace(/(\bwiki ?\[([^\]<]+)\])/ig, function(p0, p1, p2) {
p2 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p2)));
return '<a href="http://en.wikipedia.org/w/index.php?title=Special:Search&search=' +
p2 + '" target="_blank">' + p1 + '</a>';
}).
// [[blah]]
// Short form of gl[[blah]]
replace(/\[\[([^< ]([^<`]*?[^< ])?)\]\]/ig, function(p0, p1) {
var q = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p1)));
return '<a href="http://www.google.com/search?ie=UTF-8&btnI&q=' + q +
'" target="_blank">' + p1 +'</a>';
}).
// __italics__
replace(/\_\_([^< ]([^<]*?[^< ])?)\_\_/g, '<i>$1</i>').
// **bold**
replace(/\*\*([^< ]([^<]*?[^< ])?)\*\*/g, '<b>$1</b>');
},
escapeHTML: function(str, jsEscapeToo) {
str = (str?''+str:'');
str = str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');

View File

@ -733,13 +733,13 @@
var pmuserid = toUserid(pm);
var oName = pm;
if (pmuserid === app.user.get('userid')) oName = name;
this.$chat.append('<div class="chat">' + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <span class="message-pm"><i class="pmnote" data-name="' + Tools.escapeHTML(oName) + '">(Private to ' + Tools.escapeHTML(pm) + ')</i> ' + messageSanitize(message) + '</span></div>');
this.$chat.append('<div class="chat">' + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <span class="message-pm"><i class="pmnote" data-name="' + Tools.escapeHTML(oName) + '">(Private to ' + Tools.escapeHTML(pm) + ')</i> ' + Tools.parseMessage(message) + '</span></div>');
} else if (message.substr(0,4).toLowerCase() === '/me ') {
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">&bull;</strong> <em' + (name.substr(1) === app.user.get('name') ? ' class="mine"' : '') + '>' + clickableName + ' <i>' + messageSanitize(message.substr(4)) + '</i></em></div>');
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">&bull;</strong> <em' + (name.substr(1) === app.user.get('name') ? ' class="mine"' : '') + '>' + clickableName + ' <i>' + Tools.parseMessage(message.substr(4)) + '</i></em></div>');
} else if (message.substr(0,5).toLowerCase() === '/mee ') {
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">&bull;</strong> <em' + (name.substr(1) === app.user.get('name') ? ' class="mine"' : '') + '>' + clickableName + '<i>' + messageSanitize(message.substr(5)) + '</i></em></div>');
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">&bull;</strong> <em' + (name.substr(1) === app.user.get('name') ? ' class="mine"' : '') + '>' + clickableName + '<i>' + Tools.parseMessage(message.substr(5)) + '</i></em></div>');
} else if (message.substr(0,10).toLowerCase() === '/announce ') {
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <span class="message-announce">' + messageSanitize(message.substr(10)) + '</span></div>');
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <span class="message-announce">' + Tools.parseMessage(message.substr(10)) + '</span></div>');
} else if (message.substr(0,6).toLowerCase() === '/warn ') {
overlay('rules', {warning: message.substr(6)});
} else if (message.substr(0,14).toLowerCase() === '/data-pokemon ') {
@ -753,7 +753,7 @@
} else {
// Normal chat message.
if (message.substr(0,2) === '//') message = message.substr(1);
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <em' + (name.substr(1) === app.user.get('name') ? ' class="mine"' : '') + '>' + messageSanitize(message) + '</em></div>');
this.$chat.append(chatDiv + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <em' + (name.substr(1) === app.user.get('name') ? ' class="mine"' : '') + '>' + Tools.parseMessage(message) + '</em></div>');
}
}
}, {

View File

@ -72,7 +72,7 @@
var color = hashColor(toId(name));
var clickableName = '<span class="username" data-name="' + Tools.escapeHTML(name) + '">' + Tools.escapeHTML(name.substr(1)) + '</span>';
if (name.substr(0, 1) !== ' ') clickableName = '<small>' + Tools.escapeHTML(name.substr(0, 1)) + '</small>'+clickableName;
$chat.append('<div class="chat">' + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <em' + (target === oName ? ' class="mine"' : '') + '>' + messageSanitize(message) + '</em></div>');
$chat.append('<div class="chat">' + timestamp + '<strong style="' + color + '">' + clickableName + ':</strong> <em' + (target === oName ? ' class="mine"' : '') + '>' + Tools.parseMessage(message) + '</em></div>');
if (autoscroll) {
$chatFrame.scrollTop($chat.height());

View File

@ -1348,7 +1348,7 @@
},
initialize: function(data) {
this.type = 'semimodal';
this.$el.html('<p style="white-space:pre-wrap">'+Tools.escapeHTML(data.message)+'</p><p class="buttonbar"><button name="close" autofocus><strong>OK</strong></button></p>').css('max-width', 480);
this.$el.html('<p style="white-space:pre-wrap">'+Tools.parseMessage(data.message)+'</p><p class="buttonbar"><button name="close" autofocus><strong>OK</strong></button></p>').css('max-width', 480);
},
dispatchClickButton: function(e) {