diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-26 11:10:07 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-26 11:10:07 +0100 |
| commit | 3fe5cf3fcadd836b1b50efbb32f628044ad63e9f (patch) | |
| tree | 8b3e82da6b91f5c424c70810f243f88815384c99 /desktop/modules/mimeapps/MimeApps.js | |
| parent | 79408ae88bccf691106ce8f7c18f4be107ad3c4e (diff) | |
| download | quickshell-3fe5cf3fcadd836b1b50efbb32f628044ad63e9f.tar.gz quickshell-3fe5cf3fcadd836b1b50efbb32f628044ad63e9f.zip | |
fix(mimeapps): duplicate keys, CRLF MimeType lines
parse() lets the last duplicate key win, as GKeyFile does, but set() and
remove() only touched the first, so on a file with two lines for one type
an edit had no visible effect. set() now rewrites the first match and
drops the later ones; remove() drops every match.
A .desktop file with CRLF line endings left a \r on its MimeType= line,
which scan() returned as a bogus last mime type. Each item is trimmed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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; } |
