Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a70f09e authored by Daniel U's avatar Daniel U
Browse files

Tweak toHtml() paragraph conversion behavior

For mode TO_HTML_PARAGRAPH_LINES_INDIVIDUAL:
1) Empty line outputs <br> instead of <p><br></p>

2) A <ul> element is closed if it is followed by a <br>

3) Explicit zero vertical margin is applied to <ul> instead of <li>'s

4) More deterministic way to encode AlignmentSpans: only spans with
   SPAN_PARAGRAPH flag is used. If multiple spans exist, the last one
   (which is added most recently) is used.

BUG: 26224878
Change-Id: I86a2aeced9965ae465daac1ace64e5e41cf45caf
parent 1914dc60
Loading
Loading
Loading
Loading
+80 −47
Original line number Diff line number Diff line
@@ -356,24 +356,48 @@ public class Html {
        }
    }

    private static String getTextStyles(Spanned text, int start, int end) {
        final StringBuilder style = new StringBuilder(" style=\"margin-top:0; margin-bottom:0;");
    private static String getTextStyles(Spanned text, int start, int end,
            boolean forceNoVerticalMargin, boolean includeTextAlign) {
        String margin = null;
        String textAlign = null;

        if (forceNoVerticalMargin) {
            margin = "margin-top:0; margin-bottom:0;";
        }
        if (includeTextAlign) {
            final AlignmentSpan[] alignmentSpans = text.getSpans(start, end, AlignmentSpan.class);
        final int len = alignmentSpans.length;
        if (len > 0) {
            final Layout.Alignment alignment = alignmentSpans[len - 1].getAlignment();

            // Only use the last AlignmentSpan with flag SPAN_PARAGRAPH
            for (int i = alignmentSpans.length - 1; i >= 0; i--) {
                AlignmentSpan s = alignmentSpans[i];
                if ((text.getSpanFlags(s) & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH) {
                    final Layout.Alignment alignment = s.getAlignment();
                    if (alignment == Layout.Alignment.ALIGN_NORMAL) {
                style.append(" text-align:start;");
                        textAlign = "text-align:start;";
                    } else if (alignment == Layout.Alignment.ALIGN_CENTER) {
                style.append(" text-align:center;");
                        textAlign = "text-align:center;";
                    } else if (alignment == Layout.Alignment.ALIGN_OPPOSITE) {
                style.append(" text-align:end;");
                        textAlign = "text-align:end;";
                    }
                    break;
                }
            }
        }

        style.append("\"");
        return style.toString();
        if (margin == null && textAlign == null) {
            return "";
        }

        final StringBuilder style = new StringBuilder(" style=\"");
        if (margin != null && textAlign != null) {
            style.append(margin).append(" ").append(textAlign);
        } else if (margin != null) {
            style.append(margin);
        } else if (textAlign != null) {
            style.append(textAlign);
        }

        return style.append("\"").toString();
    }

    private static void withinBlockquote(StringBuilder out, Spanned text, int start, int end,
@@ -395,6 +419,14 @@ public class Html {
                next = end;
            }

            if (next == i) {
                if (isInList) {
                    // Current paragraph is no longer a list item; close the previously opened list
                    isInList = false;
                    out.append("</ul>\n");
                }
                out.append("<br>\n");
            } else {
                boolean isListItem = false;
                ParagraphStyle[] paragraphStyles = text.getSpans(i, next, ParagraphStyle.class);
                for (ParagraphStyle paragraphStyle : paragraphStyles) {
@@ -409,7 +441,9 @@ public class Html {
                if (isListItem && !isInList) {
                    // Current paragraph is the first item in a list
                    isInList = true;
                out.append("<ul>\n");
                    out.append("<ul")
                            .append(getTextStyles(text, i, next, true, false))
                            .append(">\n");
                }

                if (isInList && !isListItem) {
@@ -419,14 +453,12 @@ public class Html {
                }

                String tagType = isListItem ? "li" : "p";
            out.append("<").append(tagType).append(getTextDirection(text, start, next))
                    .append(getTextStyles(text, start, next)).append(">");
                out.append("<").append(tagType)
                        .append(getTextDirection(text, i, next))
                        .append(getTextStyles(text, i, next, !isListItem, true))
                        .append(">");

            if (next - i == 0) {
                out.append("<br>");
            } else {
                withinParagraph(out, text, i, next);
            }

                out.append("</");
                out.append(tagType);
@@ -436,6 +468,7 @@ public class Html {
                    isInList = false;
                    out.append("</ul>\n");
                }
            }

            next++;
        }