From 3e43688be28336a249c6136fa89ea84490586764 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 4 Aug 2020 15:20:41 +0200 Subject: [PATCH] Fixed extra newlines --- .../eu/faircode/email/ActivitySignature.java | 28 +++++++++++-------- .../eu/faircode/email/FragmentAnswer.java | 12 +++++--- .../java/eu/faircode/email/HtmlHelper.java | 24 +++++++++++----- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivitySignature.java b/app/src/main/java/eu/faircode/email/ActivitySignature.java index a3492db096..dde70b6648 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySignature.java +++ b/app/src/main/java/eu/faircode/email/ActivitySignature.java @@ -48,6 +48,8 @@ import androidx.appcompat.app.AlertDialog; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.snackbar.Snackbar; +import org.jsoup.nodes.Document; + public class ActivitySignature extends ActivityBase { private ViewGroup view; private EditTextCompose etText; @@ -205,26 +207,18 @@ public class ActivitySignature extends ActivityBase { } private void save() { - etText.clearComposingText(); - String html = (etText.getRaw() - ? etText.getText().toString() - : HtmlHelper.toHtml(etText.getText(), this)); Intent result = new Intent(); - result.putExtra("html", html); + result.putExtra("html", getHtml()); setResult(RESULT_OK, result); finish(); } private void html(boolean raw) { + String html = getHtml(); etText.setRaw(raw); - if (!raw || dirty) { - etText.clearComposingText(); - String html = (raw - ? HtmlHelper.toHtml(etText.getText(), this) - : etText.getText().toString()); + if (!raw || dirty) getIntent().putExtra("html", html); - } if (raw) style_bar.setVisibility(View.GONE); @@ -232,6 +226,18 @@ public class ActivitySignature extends ActivityBase { load(); } + private String getHtml() { + etText.clearComposingText(); + + if (etText.getRaw()) + return etText.getText().toString(); + else { + String html = HtmlHelper.toHtml(etText.getText(), this); + Document d = HtmlHelper.fixEdit(JsoupEx.parse(html)); + return d.body().html(); + } + } + private void insertImage() { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); diff --git a/app/src/main/java/eu/faircode/email/FragmentAnswer.java b/app/src/main/java/eu/faircode/email/FragmentAnswer.java index 4f5a97aafd..cf7389a002 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAnswer.java +++ b/app/src/main/java/eu/faircode/email/FragmentAnswer.java @@ -49,6 +49,8 @@ import androidx.constraintlayout.widget.Group; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.snackbar.Snackbar; +import org.jsoup.nodes.Document; + import static android.app.Activity.RESULT_OK; public class FragmentAnswer extends FragmentBase { @@ -246,7 +248,7 @@ public class FragmentAnswer extends FragmentBase { args.putString("name", etName.getText().toString().trim()); args.putBoolean("favorite", cbFavorite.isChecked()); args.putBoolean("hide", cbHide.isChecked()); - args.putString("text", HtmlHelper.toHtml(etText.getText(), getContext())); + args.putString("html", HtmlHelper.toHtml(etText.getText(), getContext())); new SimpleTask() { @Override @@ -265,25 +267,27 @@ public class FragmentAnswer extends FragmentBase { String name = args.getString("name"); boolean favorite = args.getBoolean("favorite"); boolean hide = args.getBoolean("hide"); - String text = args.getString("text"); + String html = args.getString("html"); if (TextUtils.isEmpty(name)) throw new IllegalArgumentException(context.getString(R.string.title_no_name)); + Document document = HtmlHelper.fixEdit(JsoupEx.parse(html)); + DB db = DB.getInstance(context); if (id < 0) { EntityAnswer answer = new EntityAnswer(); answer.name = name; answer.favorite = favorite; answer.hide = hide; - answer.text = text; + answer.text = document.body().html(); answer.id = db.answer().insertAnswer(answer); } else { EntityAnswer answer = db.answer().getAnswer(id); answer.name = name; answer.favorite = favorite; answer.hide = hide; - answer.text = text; + answer.text = document.body().html(); db.answer().updateAnswer(answer); } diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 6db18d0d39..6945b2aef1 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -288,13 +288,7 @@ public class HtmlHelper { static Document sanitizeCompose(Context context, String html, boolean show_images) { try { - Document parsed = JsoupEx.parse(html); - - // Prevent extra newline at end - Element body = parsed.body(); - if (body != null && body.childrenSize() == 1 && "p".equals(body.child(0).tagName())) - body.child(0).tagName("span").appendChild(new Element("br")); - + Document parsed = fixEdit(JsoupEx.parse(html)); return sanitize(context, parsed, false, show_images); } catch (Throwable ex) { // OutOfMemoryError @@ -307,6 +301,22 @@ public class HtmlHelper { } } + static Document fixEdit(Document document) { + // Prevent extra newline at end + Element body = document.body(); + if (body != null && body.childrenSize() == 1) { + Element holder = body.child(0); + if ("p".equals(holder.tagName())) { + holder.tagName("span"); + int c = holder.childrenSize(); + Element last = (c > 0 ? holder.child(c - 1) : null); + if (last == null || !"br".equals(last.tagName())) + holder.appendChild(new Element("br")); + } + } + return document; + } + static Document sanitizeView(Context context, Document parsed, boolean show_images) { try { return sanitize(context, parsed, true, show_images);