Fixed extra newlines

pull/184/head
M66B 4 years ago
parent 6515a50c0c
commit 3e43688be2

@ -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);

@ -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<Void>() {
@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);
}

@ -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);

Loading…
Cancel
Save