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

Commit 9da2f664 authored by Ibrahim Yilmaz's avatar Ibrahim Yilmaz
Browse files

[Text Consistency] Improve consecutive newlines cleaning

NewlineNormalizer replaces consecutive newlines with a single newline in the input text.
This also catches whitespace characters.
Fixes: 337021118
Test: NewlineNormalizerTest
Flag: ACONFIG android.app.Flags.clean_up_spans_and_new_lines STAGING

Change-Id: Id0d0ec324ba40ce7f1acf9dd9b712f46b8ed1431
parent 6f8c3e61
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -115,6 +115,7 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.graphics.ColorUtils;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.ContrastColorUtil;
import com.android.internal.util.NewlineNormalizer;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -3187,7 +3188,7 @@ public class Notification implements Parcelable
            return charSequence;
        }
        return charSequence.toString().replaceAll("[\r\n]+", "\n");
        return NewlineNormalizer.normalizeNewlines(charSequence.toString());
    }
    private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed 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.
 */

package com.android.internal.util;


import java.util.regex.Pattern;

/**
 * Utility class that replaces consecutive empty lines with single new line.
 * @hide
 */
public class NewlineNormalizer {

    private static final Pattern MULTIPLE_NEWLINES = Pattern.compile("\\v(\\s*\\v)?");

    // Private constructor to prevent instantiation
    private NewlineNormalizer() {}

    /**
     * Replaces consecutive newlines with a single newline in the input text.
     */
    public static String normalizeNewlines(String text) {
        return MULTIPLE_NEWLINES.matcher(text).replaceAll("\n");
    }
}
+71 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed 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.
 */

package com.android.internal.util;

import static junit.framework.Assert.assertEquals;


import android.platform.test.annotations.DisabledOnRavenwood;
import android.platform.test.ravenwood.RavenwoodRule;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Test for {@link NewlineNormalizer}
 * @hide
 */
@DisabledOnRavenwood(blockedBy = NewlineNormalizer.class)
@RunWith(AndroidJUnit4.class)
public class NewlineNormalizerTest {

    @Rule
    public final RavenwoodRule mRavenwood = new RavenwoodRule();

    @Test
    public void testEmptyInput() {
        assertEquals("", NewlineNormalizer.normalizeNewlines(""));
    }

    @Test
    public void testSingleNewline() {
        assertEquals("\n", NewlineNormalizer.normalizeNewlines("\n"));
    }

    @Test
    public void testMultipleConsecutiveNewlines() {
        assertEquals("\n", NewlineNormalizer.normalizeNewlines("\n\n\n\n\n"));
    }

    @Test
    public void testNewlinesWithSpacesAndTabs() {
        String input = "Line 1\n  \n \t \n\tLine 2";
        // Adjusted expected output to include the tab character
        String expected = "Line 1\n\tLine 2";
        assertEquals(expected, NewlineNormalizer.normalizeNewlines(input));
    }

    @Test
    public void testMixedNewlineCharacters() {
        String input = "Line 1\r\nLine 2\u000BLine 3\fLine 4\u2028Line 5\u2029Line 6";
        String expected = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6";
        assertEquals(expected, NewlineNormalizer.normalizeNewlines(input));
    }
}