From 37ce3c6bfaa7fb016e20dc095aa93eadbaacb20f Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Mon, 20 Apr 2015 18:48:06 +0200 Subject: [PATCH] Strip spurious slashes and white space in path segments of room names. --- static/js/directives/roombar.js | 8 +++++--- static/js/services/resturl.js | 22 ++++++++++++++++------ static/js/services/rooms.js | 17 +++++++++-------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/static/js/directives/roombar.js b/static/js/directives/roombar.js index 0573e23e..250e0184 100644 --- a/static/js/directives/roombar.js +++ b/static/js/directives/roombar.js @@ -32,15 +32,17 @@ define(['underscore', 'angular', 'text!partials/roombar.html'], function(_, angu $scope.newRoomName = ""; }; - //console.log("roomBar directive link", arguments); - //$scope.layout.roombar = true; - $scope.save = function() { if ($scope.roombarform.$invalid) { return; } var roomName = rooms.joinByName($scope.newRoomName); if (roomName !== $scope.currentRoomName) { + // Room name accepted. + $scope.roombarform.$setPristine(); + } else { + // Room name did not apply. Reset new name and form. + $scope.newRoomName = roomName; $scope.roombarform.$setPristine(); } }; diff --git a/static/js/services/resturl.js b/static/js/services/resturl.js index c585076c..fdacf59f 100644 --- a/static/js/services/resturl.js +++ b/static/js/services/resturl.js @@ -20,7 +20,7 @@ */ "use strict"; -define(["underscore"], function(_) { +define(["underscore", "jquery"], function(_, $) { // restURL return ["globalContext", "$window", function(context, $window) { @@ -35,24 +35,34 @@ define(["underscore"], function(_) { RestURL.prototype.api = function(path) { return (context.Cfg.B || "/") + "api/v1/" + path; }; - RestURL.prototype.encodeRoomURL = function(name, prefix) { + RestURL.prototype.encodeRoomURL = function(name, prefix, cb) { // Split parts so slashes are allowed. var parts = name.split("/"); var url = []; + var nn = []; if (typeof prefix !== "undefined") { url.push(prefix); } - // Allow some thing in room name parts. + // Allow some things in room name parts. _.each(parts, function(p) { + if (p === "") { + // Skip empty parts, effectly stripping spurious slashes. + return; + } + // Trim parts. removing white space from start and end. + p = $.trim(p); + nn.push(p); + // URL encode. p = $window.encodeURIComponent(p); + // Encode back certain stuff we allow. p = p.replace(/^%40/, "@"); p = p.replace(/^%24/, "$"); p = p.replace(/^%2B/, "+"); url.push(p); }); - if (url.length > 1 && url[url.length-1] === "") { - // Remove trailing slash if any. - url.pop(); + if (cb) { + cb(url.join("/")); + return nn.join("/"); } return url.join("/"); }; diff --git a/static/js/services/rooms.js b/static/js/services/rooms.js index 94d0ba16..f21bee68 100644 --- a/static/js/services/rooms.js +++ b/static/js/services/rooms.js @@ -212,15 +212,16 @@ define([ return canJoinRooms; }, joinByName: function(name, replace) { - var url = restURL.encodeRoomURL(name, ""); - // Apply new URL. - safeApply($rootScope, function(scope) { - $location.path(url); - if (replace) { - $location.replace(); - } + var nn = restURL.encodeRoomURL(name, "", function(url) { + // Apply new URL. + safeApply($rootScope, function(scope) { + $location.path(url); + if (replace) { + $location.replace(); + } + }); }); - return name; + return nn; }, joinDefault: function(replace) { return rooms.joinByName("", replace);