Parse URLs with Handlebars

This commit is contained in:
Mattias Erming 2014-04-21 12:27:43 +02:00
parent 6366fd38a2
commit d49ee1326b
2 changed files with 31 additions and 14 deletions

View File

@ -114,7 +114,7 @@
<div class="time">{{time}}</div> <div class="time">{{time}}</div>
<div class="user">{{from}}</div> <div class="user">{{from}}</div>
<div class="type">{{type}}</div> <div class="type">{{type}}</div>
<div class="text">{{text}}</div> <div class="text">{{{uri text}}}</div>
</div> </div>
{{/each}} {{/each}}
</script> </script>

View File

@ -35,13 +35,6 @@ $(function() {
return tpl[id](json); return tpl[id](json);
} }
Handlebars.registerHelper(
"partial",
function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
function handleEvent(event, json) { function handleEvent(event, json) {
var data = json.data; var data = json.data;
switch (event) { switch (event) {
@ -245,12 +238,6 @@ $(function() {
$(this).closest(".window").find(".messages").scrollToBottom(); $(this).closest(".window").find(".messages").scrollToBottom();
}); });
function uri(text) {
return URI.withinString(text, function(url) {
return "<a href='" + url + "' target='_blank'>" + url + "</a>";
});
}
var highest = 1; var highest = 1;
$.fn.bringToTop = function() { $.fn.bringToTop = function() {
return this.css('z-index', highest++) return this.css('z-index', highest++)
@ -262,4 +249,34 @@ $(function() {
.removeClass("active") .removeClass("active")
.end(); .end();
}; };
function uri(text) {
return URI.withinString(text, function(url) {
return "<a href='" + url + "' target='_blank'>" + url + "</a>";
});
}
function escape(string) {
var e = {
"<": "&lt;",
">": "&gt;",
};
return string.replace(/[<>]/g, function (s) {
return e[s];
});
}
Handlebars.registerHelper(
"uri",
function(text) {
return uri(escape(text));
}
);
Handlebars.registerHelper(
"partial",
function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
}); });