Skip to content

Commit

Permalink
Merge pull request #253 from orlando/od/FIX-236
Browse files Browse the repository at this point in the history
fix(segment): trimming leading and trailing slashes from input
  • Loading branch information
rodneyrehm committed Oct 5, 2015
2 parents 56c0971 + 0f8e416 commit 1374ddf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@
return true;
}

function trimSlashes(text) {
var noLeadTrailSlashRegex = /^\/+|\/+$/g;
return text.replace(noLeadTrailSlashRegex, '');
}

URI._parts = function() {
return {
protocol: null,
Expand Down Expand Up @@ -1592,9 +1597,10 @@
segments.pop();
}

segments.push(v[i]);
segments.push(trimSlashes(v[i]));
}
} else if (v || typeof v === 'string') {
v = trimSlashes(v);
if (segments[segments.length -1] === '') {
// empty trailing elements have to be overwritten
// to prevent results such as /foo//bar
Expand All @@ -1605,7 +1611,7 @@
}
} else {
if (v) {
segments[segment] = v;
segments[segment] = trimSlashes(v);
} else {
segments.splice(segment, 1);
}
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,10 @@

u.segment(['', '', 'foo', '', '', 'bar', '', '']);
equal(u.path(), '/foo/bar/', 'segment collapsing empty parts');

u = new URI('https://google.com')
u.segment('//font.ttf//');
equal(u.path(), '/font.ttf', 'segment removes trailing and leading');
});
test('segmentCoded', function() {
var u = new URI('http://www.example.org/some%20thing/directory/foo.html'),
Expand Down

0 comments on commit 1374ddf

Please sign in to comment.