From e10bbfac70cef59c86068056e95710c39989b3d6 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Wed, 30 Oct 2019 03:03:25 +0300 Subject: [PATCH] Normalize loaded content for text/plain;charset=utf-8 mime type (#9) Normalize \r\n and \r into \n --- CHANGELOG.md | 2 ++ src/threaded.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04127fa..7de2710 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Perform loaded data normalization for text/plain;charset=utf-8 mime type + ## 0.3.5 -- 2019-09-3 - Fix primary selection storing, when releasing button outside of the surface diff --git a/src/threaded.rs b/src/threaded.rs index 6bf8662..ea5d3c3 100644 --- a/src/threaded.rs +++ b/src/threaded.rs @@ -309,6 +309,9 @@ fn clipboard_thread( contents }) }); + // Normalization should happen only on `text/plain;charset=utf-8`, in case we + // add other mime types consult gtk for normalization. + let contents = normilize_to_lf(contents); load_send.send(contents).unwrap(); } // Store text in the clipboard @@ -400,6 +403,9 @@ fn clipboard_thread( } else { String::new() }; + // Normalization should happen only on `text/plain;charset=utf-8`, in case we + // add other mime types consult gtk for normalization. + let contents = normilize_to_lf(contents); load_send.send(contents).unwrap(); } // Store text in the primary clipboard @@ -719,3 +725,12 @@ fn implement_seat( }) .unwrap(); } + +// Normalize \r and \r\n into \n. +// +// Gtk does this for text/plain;charset=utf-8, so following them here, otherwise there is +// a chance of getting extra new lines on load, since they're converting \r and \n into +// \r\n on every store. +fn normilize_to_lf(text: String) -> String { + text.replace("\r\n", "\n").replace("\r", "\n") +}