path: fix bugs related to paths with trailing slashes
This commit is contained in:
parent
b916774255
commit
bb1c03989f
10
lib/path.js
10
lib/path.js
@ -290,7 +290,17 @@ if (isWindows) {
|
|||||||
// 'root' is just a slash, or nothing.
|
// 'root' is just a slash, or nothing.
|
||||||
var splitPathRe =
|
var splitPathRe =
|
||||||
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
|
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
|
||||||
|
var trailingSlash = /\/+$/;
|
||||||
var splitPath = function(filename) {
|
var splitPath = function(filename) {
|
||||||
|
|
||||||
|
// removes trailing slashes before spliting the path
|
||||||
|
var tail = trailingSlash.exec(filename);
|
||||||
|
if (tail) {
|
||||||
|
if (tail.index === 0) return ['/', '', '', ''];
|
||||||
|
|
||||||
|
filename = filename.slice(0, tail.index);
|
||||||
|
}
|
||||||
|
|
||||||
var result = splitPathRe.exec(filename);
|
var result = splitPathRe.exec(filename);
|
||||||
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
|
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
|
||||||
};
|
};
|
||||||
|
@ -30,6 +30,11 @@ var f = __filename;
|
|||||||
|
|
||||||
assert.equal(path.basename(f), 'test-path.js');
|
assert.equal(path.basename(f), 'test-path.js');
|
||||||
assert.equal(path.basename(f, '.js'), 'test-path');
|
assert.equal(path.basename(f, '.js'), 'test-path');
|
||||||
|
assert.equal(path.basename('/dir/basename.ext'), 'basename.ext');
|
||||||
|
assert.equal(path.basename('/basename.ext'), 'basename.ext');
|
||||||
|
assert.equal(path.basename('basename.ext'), 'basename.ext');
|
||||||
|
assert.equal(path.basename('basename.ext/'), 'basename.ext');
|
||||||
|
assert.equal(path.basename('basename.ext//'), 'basename.ext');
|
||||||
|
|
||||||
// POSIX filenames may include control characters
|
// POSIX filenames may include control characters
|
||||||
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
|
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
|
||||||
@ -47,6 +52,7 @@ assert.equal(path.dirname('/a/b/'), '/a');
|
|||||||
assert.equal(path.dirname('/a/b'), '/a');
|
assert.equal(path.dirname('/a/b'), '/a');
|
||||||
assert.equal(path.dirname('/a'), '/');
|
assert.equal(path.dirname('/a'), '/');
|
||||||
assert.equal(path.dirname('/'), '/');
|
assert.equal(path.dirname('/'), '/');
|
||||||
|
assert.equal(path.dirname('////'), '/');
|
||||||
|
|
||||||
if (isWindows) {
|
if (isWindows) {
|
||||||
assert.equal(path.dirname('c:\\'), 'c:\\');
|
assert.equal(path.dirname('c:\\'), 'c:\\');
|
||||||
@ -114,7 +120,8 @@ assert.equal(path.extname('..file..'), '.');
|
|||||||
assert.equal(path.extname('...'), '.');
|
assert.equal(path.extname('...'), '.');
|
||||||
assert.equal(path.extname('...ext'), '.ext');
|
assert.equal(path.extname('...ext'), '.ext');
|
||||||
assert.equal(path.extname('....'), '.');
|
assert.equal(path.extname('....'), '.');
|
||||||
assert.equal(path.extname('file.ext/'), '');
|
assert.equal(path.extname('file.ext/'), '.ext');
|
||||||
|
assert.equal(path.extname('file.ext//'), '.ext');
|
||||||
|
|
||||||
if (isWindows) {
|
if (isWindows) {
|
||||||
// On windows, backspace is a path separator.
|
// On windows, backspace is a path separator.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user