# Bug report: Newlines duplicated within fields

**URL:** <https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884>\
**Category:** Help\
**Created:** [February 8, 2024, 10:18am UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884 "2024-02-08T10:18:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bugman](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@bugman](https://forums.ankiweb.net/u/bugman)\
**Post date:** [February 8, 2024, 10:18am UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/1 "2024-02-08T10:18:05Z")

</div>

Hi, I’m new here. I have a bug to report, but I’m not sure which subforum to post on. So mods, please shift this to the correct place!

I have a frustrating bug, in that newlines are appear to be duplicated during field replacement. For example I have a card with the field {{Wiktionary}} with the contents:

```html
挑战
Verb
<ol>
  <li>(literally) to challenge (to a fight); to throw down the gauntlet</li>
  <li>(figuratively) to challenge (invite someone to take part in a contest)</li>
  <li>(figuratively) to challenge (put demands on); to test</li>
  <li>(figuratively) to challenge (dispute the validity of something)</li>
</ol>
Noun
<ol>
  <li>challenge; difficulty</li>
</ol>

```

I create this within AnkiDroid. If I have on the back:

```html
<div>
  <ol>
    <li>A</li>
    <li>B</li>
  </ol>
</div>
{{Wiktionary}}

```

The result is:

```none
  1. A
  2. B

挑战
Verb

    1. (literally) to challenge (to a fight); to throw down the gauntlet

    2. (figuratively) to challenge (invite someone to take part in a contest)

    3. (figuratively) to challenge (put demands on); to test

    4. (figuratively) to challenge (dispute the validity of something)

Noun

    1. challenge; difficulty

```

This shows that field replacement doesn’t simply insert the HTML code so that it matches the ordered list format of the HTML code above it.

If I open this card in AnkiWeb, I can see the newlines inserted when editing the card. If I remove them, then in AnkiDroid the {{Wiktionary}} field has had all newlines removed.

Is this a real bug or a design feature? How can I avoid it? I’m currently using CSS to set top and bottom margins to -20px to avoid the ugliness.

Cheers,

Edward

---

<div class="post-metadata">

**Author:** ![bugman](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@bugman](https://forums.ankiweb.net/u/bugman)\
**Post date:** [February 8, 2024, 10:22am UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/2 "2024-02-08T10:22:57Z")

</div>

I forgot to mention, but after field replacement the HTML code is obviously:

```html
<div>
  <ol>
    <li>A</li>
    <li>B</li>
  </ol>
</div>
挑战
Verb
<ol>

  <li>(literally) to challenge (to a fight); to throw down the gauntlet</li>

  <li>(figuratively) to challenge (invite someone to take part in a contest)</li>

  <li>(figuratively) to challenge (put demands on); to test</li>

  <li>(figuratively) to challenge (dispute the validity of something)</li>

</ol>

Noun
<ol>

  <li>challenge; difficulty</li>

</ol>

```

---

<div class="post-metadata">

**Author:** ![Danika\_Dakika](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/danika_dakika/32/17815_2.png) [@Danika\_Dakika](https://forums.ankiweb.net/u/Danika_Dakika)\
**Post date:** [February 8, 2024, 9:42pm UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/3 "2024-02-08T21:42:29Z")

</div>

[If this is AnkiDroid-specific, you may want to move it to the AnkiDroid category.]

---

<div class="post-metadata">

**Author:** ![bugman](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@bugman](https://forums.ankiweb.net/u/bugman)\
**Post date:** [February 9, 2024, 9:01am UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/4 "2024-02-09T09:01:44Z")

</div>

Maybe it is AnkiDroid’s conversion of HTML in fields to the Anki internal HTML format. I tried creating cards on Anki Desktop (Linux) and AnkiWeb with the following in a field:

```html
<div class="test">
  <ol>
    <li>x</li>
    <li>y</li>
  </ol>
</div>

```

But in both of those, the HTML was not recognised as HTML and the output included the HTML tags!

Edit: Using AnkiDroid to open the AnkiWeb card with HTML in the field, the field content ends up as:

```html
<div>&lt;div class="test"&gt;
</div><div>&nbsp; &lt;ol&gt;</div>&nbsp; &nbsp; &lt;li&gt;x&lt;/li&gt;
&nbsp; &nbsp; &lt;li&gt;y&lt;/li&gt;
<div>&nbsp; &lt;/ol&gt;</div><div>&lt;/div&gt;
</div>

```

---

<div class="post-metadata">

**Author:** ![Danika\_Dakika](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/danika_dakika/32/17815_2.png) [@Danika\_Dakika](https://forums.ankiweb.net/u/Danika_Dakika)\
**Post date:** [February 9, 2024, 11:24pm UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/5 "2024-02-09T23:24:01Z")

</div>

Are you putting your HTML in the right type of field?

When editing in AnkiDroid, you directly see the HTML. But when editing in AnkiWeb, you see the parsed/formatted version of the field. In AnkiDesktop, you can see either/both the HTML and the formatted version.

If you’re putting HTML in a formatted field (on AnkiWeb or AnkiDesktop), that definitely won’t work. As you can see, all of the `<` and `>` that mark the tags are encoded with their entity names (like `&gt;`), so that it won’t be mistaken for parsable HTML – it’s just text.

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [February 11, 2024, 4:22am UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/6 "2024-02-11T04:22:07Z")

</div>

Possibly related: [Editing a note turns insignificant newlines into explicit line breaks · Issue #3304 · ankidroid/Anki-Android · GitHub](https://github.com/ankidroid/Anki-Android/issues/3304)

---

<div class="post-metadata">

**Author:** ![bugman](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@bugman](https://forums.ankiweb.net/u/bugman)\
**Post date:** [February 12, 2024, 2:39pm UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/7 "2024-02-12T14:39:46Z")

</div>

Ah, I now see that I can see the HTML in AnkiDesktop! My newlines input into the field in AnkiDroid have been bizarrely converted to `<br>`. That’s why exactly the same HTML list added to the Back Template and a field end up being rendered differently!

For reference, the HTML field that I manually input into AnkiDroid as

```html
Verb
<ol>
  <li>(literally) to challenge (to a fight); to throw down the gauntlet</li>
  <li>(figuratively) to challenge (invite someone to take part in a contest)</li>
  <li>(figuratively) to challenge (put demands on); to test</li>
  <li>(figuratively) to challenge (dispute the validity of something)</li>
</ol>
Noun
<ol>
  <li>challenge; difficulty</li>
</ol>

```

is converted internally to:

```html
Verb<br><ol><br> <li>(literally) to challenge (to a fight); to throw down the gauntlet</li><br> <li>(figuratively) to challenge (invite someone to take part in a contest)</li><br> <li>(figuratively) to challenge (put demands on); to test</li><br> <li>(figuratively) to challenge (dispute the validity of something)</li><br></ol><br>Noun<br><ol><br> <li>challenge; difficulty</li><br></ol>

```

If I manually replace `<br>` on AnkiDesktop with newlines, then the newlines are accepted and the list becomes normal!

---

<div class="post-metadata">

**Author:** ![bugman](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@bugman](https://forums.ankiweb.net/u/bugman)\
**Post date:** [February 13, 2024, 1:56pm UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/8 "2024-02-13T13:56:33Z")

</div>

Thanks, that is clearly the same issue. For a permanent record, I’ll repost my response on that bug report.

* * *

I think I have encountered the same bug - or more like logic issue - and reported it on the Anki forum at:

```
https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884

```

Newlines input into AnkiDroid fields are being converted to `<br>`. This is in the file:

`AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt`

Specifically in the function `convertToHtmlNewline()`. The argument `replaceNewlines` appears be set to `true`. So all newlines in fields are converted to `<br>`.

For reference, the code is:

```kt
    /**
     * @param replaceNewlines Converts [FieldEditText.NEW_LINE] to HTML linebreaks
     */
    @VisibleForTesting
    @CheckResult
    fun getFieldsAsBundleForPreview(editFields: List<NoteField?>?, replaceNewlines: Boolean): Bundle {
        val fields = Bundle()
        // Save the content of all the note fields. We use the field's ord as the key to
        // easily map the fields correctly later.
        if (editFields == null) {
            return fields
        }
        for (e in editFields) {
            if (e?.fieldText == null) {
                continue
            }
            val fieldValue = convertToHtmlNewline(e.fieldText!!, replaceNewlines)
            fields.putString(e.ord.toString(), fieldValue)
        }
        return fields
    }

    fun convertToHtmlNewline(fieldData: String, replaceNewlines: Boolean): String {
        return if (!replaceNewlines) {
            fieldData
        } else {
            fieldData.replace(FieldEditText.NEW_LINE, "<br>")
        }
    }

```

This is being called from:

`AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt`

The code is:

```kt
        private fun shouldReplaceNewlines(): Boolean {
            return AnkiDroidApp.instance.sharedPrefs()
                .getBoolean(PREF_NOTE_EDITOR_NEWLINE_REPLACE, true)
        }

```

Looking through the interface, I found this option buried under:

`Settings -> Advanced -> Workarounds -> Replace newlines with HTML`

The text of this option is:

`In the Note Editor, convert any instances of <br> to newlines when editing a card`

Turing this off fixes the problem! But the logic seems reversed, as newlines are instead being converted to `<br>` **and vice versa**.

As a solution, could this option please be turned off by default? Having this option on by defaut is frighteningly confusing. And maybe the text of the option clarified by changing to:

`In the Note Editor, convert all newlines to be stored internally within the card as <br> (HTML line breaks)`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [March 14, 2024, 1:57pm UTC](https://forums.ankiweb.net/t/bug-report-newlines-duplicated-within-fields/40884/9 "2024-03-14T13:57:01Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
