Bug 1682031 - Add PathUtils.appendRelative r=emalysz

Differential Revision: https://phabricator.services.mozilla.com/D99505
This commit is contained in:
Barret Rennie 2020-12-17 23:55:45 +00:00
parent 5fef30be08
commit cff1a2c34d
4 changed files with 66 additions and 1 deletions

View File

@ -41,6 +41,15 @@ namespace PathUtils {
[Throws]
DOMString join(DOMString... components);
/**
* Join the given relative path to the base path.
*
* @param base The base path. This must be an absolute path.
* @param relativePath A relative path to join to the base path.
*/
[Throws]
DOMString joinRelative(DOMString base, DOMString relativePath);
/**
* Normalize a path by removing multiple separators and `..` and `.`
* directories.

View File

@ -35,6 +35,7 @@ static constexpr auto ERROR_EMPTY_PATH =
"PathUtils does not support empty paths"_ns;
static constexpr auto ERROR_INITIALIZE_PATH = "Could not initialize path"_ns;
static constexpr auto ERROR_GET_PARENT = "Could not get parent path"_ns;
static constexpr auto ERROR_JOIN = "Could not append to path"_ns;
static void ThrowError(ErrorResult& aErr, const nsresult aResult,
const nsCString& aMessage) {
@ -163,7 +164,7 @@ void PathUtils::Join(const GlobalObject&, const Sequence<nsString>& aComponents,
const auto components = Span<const nsString>(aComponents).Subspan(1);
for (const auto& component : components) {
if (nsresult rv = path->Append(component); NS_FAILED(rv)) {
ThrowError(aErr, rv, "Could not append to path"_ns);
ThrowError(aErr, rv, ERROR_JOIN);
return;
}
}
@ -171,6 +172,28 @@ void PathUtils::Join(const GlobalObject&, const Sequence<nsString>& aComponents,
MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
}
void PathUtils::JoinRelative(const GlobalObject&, const nsAString& aBasePath,
const nsAString& aRelativePath, nsString& aResult,
ErrorResult& aErr) {
if (aRelativePath.IsEmpty()) {
aResult = aBasePath;
return;
}
nsCOMPtr<nsIFile> path = new nsLocalFile();
if (nsresult rv = path->InitWithPath(aBasePath); NS_FAILED(rv)) {
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
return;
}
if (nsresult rv = path->AppendRelativePath(aRelativePath); NS_FAILED(rv)) {
ThrowError(aErr, rv, ERROR_JOIN);
return;
}
MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
}
void PathUtils::Normalize(const GlobalObject&, const nsAString& aPath,
nsString& aResult, ErrorResult& aErr) {
if (aPath.IsEmpty()) {

View File

@ -32,6 +32,10 @@ class PathUtils final {
static void Join(const GlobalObject&, const Sequence<nsString>& aComponents,
nsString& aResult, ErrorResult& aErr);
static void JoinRelative(const GlobalObject&, const nsAString& aBasePath,
const nsAString& aRelativePath, nsString& aResult,
ErrorResult& aErr);
static void Normalize(const GlobalObject&, const nsAString& aPath,
nsString& aResult, ErrorResult& aErr);

View File

@ -22,6 +22,7 @@
const UNRECOGNIZED_PATH = /Could not initialize path: NS_ERROR_FILE_UNRECOGNIZED_PATH/;
const EMPTY_PATH = /PathUtils does not support empty paths/;
const JOIN = /Could not append to path/;
add_task(function test_filename() {
Assert.throws(
@ -199,6 +200,34 @@
}
});
add_task(function test_join_relative() {
if (Services.appinfo.OS === "WINNT") {
is(
PathUtils.joinRelative("C:", ""),
"C:",
"PathUtils.joinRelative() with an empty relative path"
);
is(
PathUtils.joinRelative("C:", "foo\\bar\\baz"),
"C:\\foo\\bar\\baz",
"PathUtils.joinRelative() with a relative path containing path separators"
);
} else {
is(
PathUtils.joinRelative("/", ""),
"/",
"PathUtils.joinRelative() with an empty relative path"
);
is(
PathUtils.joinRelative("/", "foo/bar/baz"),
"/foo/bar/baz",
"PathUtils.joinRelative() with a relative path containing path separators"
);
}
});
add_task(async function test_normalize() {
Assert.throws(
() => PathUtils.normalize(""),