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

Commit 53741732 authored by cketti's avatar cketti
Browse files

Remove line wrapping when prefix-quoting

We're using "quoted-printable" for transport encoding. So manually
wrapping lines shouldn't be necessary.

See #3654
parent 04111290
Loading
Loading
Loading
Loading
+0 −140
Original line number Diff line number Diff line
@@ -134,146 +134,6 @@ public class Utility {
//        }
    }

    /**
     * <p>Wraps a multiline string of text, identifying words by <code>' '</code>.</p>
     *
     * <p>New lines will be separated by the system property line separator.
     * Very long words, such as URLs will <i>not</i> be wrapped.</p>
     *
     * <p>Leading spaces on a new line are stripped.
     * Trailing spaces are not stripped.</p>
     *
     * <pre>
     * WordUtils.wrap(null, *) = null
     * WordUtils.wrap("", *) = ""
     * </pre>
     *
     * Adapted from the Apache Commons Lang library.
     * http://svn.apache.org/viewvc/commons/proper/lang
     *   /trunk/src/main/java/org/apache/commons/lang3/text/WordUtils.java
     * SVN Revision 925967, Mon Mar 22 06:16:49 2010 UTC
     *
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     *
     * @param str  the String to be word wrapped, may be null
     * @param wrapLength  the column to wrap the words at, less than 1 is treated as 1
     * @return a line with newlines inserted, <code>null</code> if null input
     */
    private static final String NEWLINE_REGEX = "(?:\\r?\\n)";
    public static String wrap(String str, int wrapLength) {
        StringBuilder result = new StringBuilder();
        for (String piece : str.split(NEWLINE_REGEX)) {
            result.append(wrap(piece, wrapLength, null, false));
            result.append("\r\n");
        }
        return result.toString();
    }

    /**
     * <p>Wraps a single line of text, identifying words by <code>' '</code>.</p>
     *
     * <p>Leading spaces on a new line are stripped.
     * Trailing spaces are not stripped.</p>
     *
     * <pre>
     * WordUtils.wrap(null, *, *, *) = null
     * WordUtils.wrap("", *, *, *) = ""
     * </pre>
     *
     * This is from the Apache Commons Lang library.
     * http://svn.apache.org/viewvc/commons/proper/lang
     *   /trunk/src/main/java/org/apache/commons/lang3/text/WordUtils.java
     * SVN Revision 925967, Mon Mar 22 06:16:49 2010 UTC
     *
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     *
     * @param str  the String to be word wrapped, may be null
     * @param wrapLength  the column to wrap the words at, less than 1 is treated as 1
     * @param newLineStr  the string to insert for a new line,
     *  <code>null</code> uses the system property line separator
     * @param wrapLongWords  true if long words (such as URLs) should be wrapped
     * @return a line with newlines inserted, <code>null</code> if null input
     */
    public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
        if (str == null) {
            return null;
        }
        if (newLineStr == null) {
            newLineStr = "\r\n";
        }
        if (wrapLength < 1) {
            wrapLength = 1;
        }
        int inputLineLength = str.length();
        int offset = 0;
        StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);

        while ((inputLineLength - offset) > wrapLength) {
            if (str.charAt(offset) == ' ') {
                offset++;
                continue;
            }
            int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);

            if (spaceToWrapAt >= offset) {
                // normal case
                wrappedLine.append(str.substring(offset, spaceToWrapAt));
                wrappedLine.append(newLineStr);
                offset = spaceToWrapAt + 1;
            } else {
                // really long word or URL
                if (wrapLongWords) {
                    // wrap really long word one line at a time
                    wrappedLine.append(str.substring(offset, wrapLength + offset));
                    wrappedLine.append(newLineStr);
                    offset += wrapLength;
                } else {
                    // do not wrap really long word, just extend beyond limit
                    spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
                    if (spaceToWrapAt >= 0) {
                        wrappedLine.append(str.substring(offset, spaceToWrapAt));
                        wrappedLine.append(newLineStr);
                        offset = spaceToWrapAt + 1;
                    } else {
                        wrappedLine.append(str.substring(offset));
                        offset = inputLineLength;
                    }
                }
            }
        }

        // Whatever is left in line is short enough to just pass through
        wrappedLine.append(str.substring(offset));

        return wrappedLine.toString();
    }

    /**
     * Extract the 'original' subject value, by ignoring leading
     * response/forward marker and '[XX]' formatted tags (as many mailing-list
+1 −6
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ import android.content.res.Resources;
import com.fsck.k9.Account.QuoteStyle;
import com.fsck.k9.CoreResourceProvider;
import com.fsck.k9.DI;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Message.RecipientType;
@@ -17,8 +16,6 @@ import static com.fsck.k9.message.quote.QuoteHelper.QUOTE_BUFFER_LENGTH;


public class TextQuoteCreator {
    private static final int REPLY_WRAP_LINE_WIDTH = 72;

    /**
     * Add quoting markup to a text message.
     * @param originalMessage Metadata for message being quoted.
@@ -42,10 +39,8 @@ public class TextQuoteCreator {
            }
            quotedText.append("\r\n");

            final String wrappedText = Utility.wrap(body, REPLY_WRAP_LINE_WIDTH - prefix.length());

            final String escapedPrefix = Matcher.quoteReplacement(prefix);
            quotedText.append(wrappedText.replaceAll("(?m)^", escapedPrefix));
            quotedText.append(body.replaceAll("(?m)^", escapedPrefix));

            return quotedText.toString();
        } else if (quoteStyle == QuoteStyle.HEADER) {
+17 −1
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ class TextQuoteCreatorTest : RobolectricTest() {
            > Line 1
            > Line 2
            > Line 3

            """.trimIndent().crlf())
    }

@@ -81,7 +80,24 @@ class TextQuoteCreatorTest : RobolectricTest() {
            On January 18, 1970 7:53:41 PM UTC, Alice <alice@sender.example> wrote:
            $1\t Line 1
            $1\t Line 2
            """.trimIndent().crlf())
    }

    @Test
    fun prefixQuote_withLongLines() {
        val messageBody = """
            [-------] [-------] [-------] [-------] [-------] [-------] [-------] [-------] [-------] [-------]
            [-------------------------------------------------------------------------------------------------]
            """.trimIndent().crlf()
        val quoteStyle = QuoteStyle.PREFIX
        val quotePrefix = "> "

        val quote = createQuote(messageBody, quoteStyle, quotePrefix)

        assertThat(quote).isEqualTo("""
            On January 18, 1970 7:53:41 PM UTC, Alice <alice@sender.example> wrote:
            > [-------] [-------] [-------] [-------] [-------] [-------] [-------] [-------] [-------] [-------]
            > [-------------------------------------------------------------------------------------------------]
            """.trimIndent().crlf())
    }