aboutsummaryrefslogtreecommitdiffstats
path: root/desktop
diff options
context:
space:
mode:
Diffstat (limited to 'desktop')
-rw-r--r--desktop/modules/mimeapps/MimeApps.js18
-rw-r--r--desktop/modules/mimeapps/test_mimeapps.js8
2 files changed, 21 insertions, 5 deletions
diff --git a/desktop/modules/mimeapps/MimeApps.js b/desktop/modules/mimeapps/MimeApps.js
index 9e3c670..dfadf28 100644
--- a/desktop/modules/mimeapps/MimeApps.js
+++ b/desktop/modules/mimeapps/MimeApps.js
@@ -73,8 +73,15 @@ function set(text, mime, id) {
s = [ls.length - 1, ls.length];
}
const line = mime + "=" + id + ";";
- for (let i = s[0] + 1; i < s[1]; i++)
- if (keyOf(ls[i]) === mime) { ls[i] = line; return join(ls); }
+ // The first match is rewritten and later duplicates dropped, since parse()
+ // lets the last one win and a leftover would undo the edit.
+ let first = -1;
+ for (let i = s[0] + 1; i < s[1]; i++) {
+ if (keyOf(ls[i]) !== mime) continue;
+ if (first < 0) { ls[i] = line; first = i; }
+ else { ls.splice(i, 1); i--; s[1]--; }
+ }
+ if (first >= 0) return join(ls);
// After the last non-blank line, so a blank line before the next header stays.
let at = s[1];
while (at > s[0] + 1 && ls[at - 1].trim() === "") at--;
@@ -87,9 +94,10 @@ function set(text, mime, id) {
function remove(text, mime) {
const ls = lines(text), s = section(ls);
if (!s) return text;
+ let hit = false;
for (let i = s[0] + 1; i < s[1]; i++)
- if (keyOf(ls[i]) === mime) { ls.splice(i, 1); return join(ls); }
- return text;
+ if (keyOf(ls[i]) === mime) { ls.splice(i, 1); i--; s[1]--; hit = true; }
+ return hit ? join(ls) : text;
}
// The applications directories in XDG order, the user's first.
@@ -113,7 +121,7 @@ function scan(out, appDirs) {
if (!dir) continue;
const id = path.slice(dir.length + 1).replace(/\//g, "-");
if (id in apps) continue;
- apps[id] = line.slice(i + ":MimeType=".length).split(";").filter(m => m);
+ apps[id] = line.slice(i + ":MimeType=".length).split(";").map(m => m.trim()).filter(m => m);
}
return apps;
}
diff --git a/desktop/modules/mimeapps/test_mimeapps.js b/desktop/modules/mimeapps/test_mimeapps.js
index 469c9a4..7f629db 100644
--- a/desktop/modules/mimeapps/test_mimeapps.js
+++ b/desktop/modules/mimeapps/test_mimeapps.js
@@ -45,6 +45,12 @@ assert.strictEqual(M.remove(file, "text/html"), file.replace("text/html=firefox.
assert.strictEqual(M.remove(file, "nope/x"), file);
assert.strictEqual(M.remove("[Default Applications]\na/b=y.desktop;\n", "a/b"), "[Default Applications]\n");
+// Duplicate keys: parse takes the last; set edits the first and drops the rest; remove drops all.
+const dup = "[Default Applications]\ntext/html=a.desktop;\ntext/html=b.desktop;\n";
+assert.strictEqual(M.parse(dup)["text/html"], "b.desktop");
+assert.strictEqual(M.set(dup, "text/html", "c.desktop"), "[Default Applications]\ntext/html=c.desktop;\n");
+assert.strictEqual(M.remove(dup, "text/html"), "[Default Applications]\n");
+
// dirs: the user's first, trailing slashes dropped.
const dirs = M.dirs("/home/you", "", "/usr/local/share/:/usr/share");
assert.deepStrictEqual(dirs, ["/home/you/.local/share/applications", "/usr/local/share/applications", "/usr/share/applications"]);
@@ -56,6 +62,8 @@ const out = "/home/you/.local/share/applications/firefox.desktop:MimeType=text/h
+ "/usr/share/applications/kde4/okular.desktop:MimeType=application/pdf;\n";
assert.deepStrictEqual(M.scan(out, dirs), { "firefox.desktop": ["text/html"], "kde4-okular.desktop": ["application/pdf"] });
assert.deepStrictEqual(M.scan("", dirs), {});
+// A CRLF .desktop file leaves a \r on the line; it is not a mime type.
+assert.deepStrictEqual(M.scan("/usr/share/applications/a.desktop:MimeType=a/b;c/d;\r\n", dirs), { "a.desktop": ["a/b", "c/d"] });
// role: the app most types point at, and how many distinct apps.
assert.deepStrictEqual(M.role({ a: "x.desktop", b: "y.desktop", c: "x.desktop" }, ["a", "b", "c", "d"]), { id: "x.desktop", apps: 2 });