diff options
Diffstat (limited to 'desktop/modules/mimeapps/MimeApps.js')
| -rw-r--r-- | desktop/modules/mimeapps/MimeApps.js | 18 |
1 files changed, 13 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; } |
