Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editing a note turns insignificant newlines into explicit line breaks #3304

Open
hssm opened this issue Jul 28, 2015 · 26 comments · Fixed by #7692
Open

Editing a note turns insignificant newlines into explicit line breaks #3304

hssm opened this issue Jul 28, 2015 · 26 comments · Fixed by #7692
Labels
Bug Keep Open avoids the stale bot Priority-Medium Reproduced This bug has been reproduced by a maintainer

Comments

@hssm
Copy link
Member

hssm commented Jul 28, 2015

Originally reported on Google Code with ID 2408

AnkiDroid v2.3.2/Android 4.3:

There is a field on some of my notes that contains HTML code as follows:

---
<h2>Headline</h2>

line 1<br />
line 2<br />
...

---

AnkiDroid displays this correctly.

But whenever I edit and save such a note, even if I do not touch that particular field,
each (insignificant) newline gets turned into another explicit line break, so I end
up with double-spaced output:

---
<h2>Headline</h2>



line 1

line 2

...

---

Expected behaviour:

1. AnkiDroid should not modify a field I did not edit.

2. AnkiDroid should not make insignificant whitespace read from existing content significant
when editing. For me, it would be acceptable to have it stripped if that helps (i.e.
to have the data above internally turned into 

  "<h2>Headline</h2>line 1</br>line2</br>..."

before being fed to the edit box); see 1. though.

Reported by post@johannesrohrer.de on 2014-11-17 11:56:10

@hssm
Copy link
Member Author

hssm commented Jul 28, 2015

This problem has caused me a lot of unnecessary work as well. The Anki Windows version
handles HTML nicely, so I have to switch frequently and correct mistakes Ankidroid
introduced. I would appreciate if suggestion 2 would not be necessary.

Reported by tobias.vornholt@online.de on 2015-01-26 20:44:23

@hssm hssm added Bug Priority-Medium Accepted Maintainers welcome a PR implementing this feature labels Jul 28, 2015
@hssm
Copy link
Member Author

hssm commented Jul 28, 2015

This isn't a problem for most users because they include their formatting in the card
template rather than the fields themselves. I strongly suggest you do the same as this
may not get fixed for a while unfortuantely

Reported by perceptualchaos2 on 2015-01-27 00:07:38

@hssm
Copy link
Member Author

hssm commented Jul 28, 2015

I do that for basic formatting of course, but I´m afraid that doesn´t make much sense
when it comes to structuring longer cards with list etc. So I hope someone will tackle
this problem one day...

Reported by tobias.vornholt@online.de on 2015-01-28 22:59:28

@hssm
Copy link
Member Author

hssm commented Jul 28, 2015

I agree that an edit-save cycle with no actual changes should not modify the content.

Reported by nicolas.raoul on 2015-04-21 06:48:44

  • Status changed: Accepted
  • Labels added: Priority-Medium
  • Labels removed: Priority-Undecided

@hssm
Copy link
Member Author

hssm commented Jul 28, 2015

I am having issues with this too, even editing tags to the card will trigger this behaviour.

I think users that care more about mark-up of the cards and e.g. use more lists (dl
tags and nested lists) run into this issue. 

Reported by emil.rehnberg on 2015-06-18 05:53:41

@barkdoll
Copy link

barkdoll commented Oct 6, 2017

I have run into the same issues with automatic <br /> tags being entered when editing my cards. I would prefer if these did not generate when saving my cards as well. Has anyone looked into this bug as of recent?

@AxelRann
Copy link

AxelRann commented Jan 1, 2018

I have this problem as well. Not being able to edit a card from my phone without rendering it unreadable is a big pain. Anyone dealing with this would be a hero.

@chriskempson
Copy link

I think the problem wont be solved until AnkiDroid supports HTML rendering when editing fields which would bring it inline with the desktop version.

@bzolka
Copy link

bzolka commented Sep 4, 2019

This is causing quite a lot of extra work to me as well, would be great to have this fixed.

@MightyPork
Copy link

MightyPork commented Sep 8, 2019

same issue here, two times I tried to correct a card on my phone and two times i had to fix it on desktop later. Learning to never edit on the phone, it's just not worth it.

Screenshot_20190908_092208

Tip: Ctrl+Shift+X in the card browser opens raw HTML so you can find-replace all <br> with nothing.

@francisashley
Copy link

Same issue here.

Updating a note in AnkiDroid inserts six additional <br> tags which messes with the template.

The only way to see and fix these tags is to edit the card in the desktop client.

@mikehardy
Copy link
Member

It would indeed be great to "have this fixed" but no one has proposed a fix yet - we would welcome a fix for it https://github.com/ankidroid/Anki-Android/wiki/Contributing

@timrae timrae added Reproduced This bug has been reproduced by a maintainer and removed Accepted Maintainers welcome a PR implementing this feature labels Jan 3, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Jun 3, 2020

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

@github-actions github-actions bot added the Stale label Jun 3, 2020
@MightyPork
Copy link

shoo, shoo, stupid bot. still broken

@mikehardy
Copy link
Member

@MightyPork you might like the preview of the visual editor: #1377 (comment)

@github-actions
Copy link
Contributor

github-actions bot commented Aug 9, 2020

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

@github-actions github-actions bot added the Stale label Aug 9, 2020
@david-allison david-allison added Keep Open avoids the stale bot and removed Stale labels Aug 10, 2020
@fireheart2008
Copy link

Oh my god! This bug is driving me crazy!
It's unfathomable that this bug was reported 5 years ago and still existing in late 2020

@mikehardy
Copy link
Member

@fireheart2008 we would love to see it fixed! Do you have a solution you could propose?

@david-allison david-allison self-assigned this Nov 15, 2020
@david-allison
Copy link
Member

Round 2 - let's add a preference. This should be viable now a user can add a custom toolbar button for <br/>

@euu2021
Copy link

euu2021 commented Jul 6, 2021

Was this supposed to be fixed on 2.14? I'm still getting it on 2.15.4, in some situations, as I reported here.

@mikehardy
Copy link
Member

Have you tried the new preference mentioned here? #7692

@euu2021
Copy link

euu2021 commented Jul 6, 2021

I thought I had to have it on. I haven't read correctly, sorry.
Now, I turned it off and everything is perfect. Thanks!

@secularbugtax
Copy link

secularbugtax commented Jan 23, 2024

NINE YEAR PERSISTENT BUG! It's 2024 and I am still getting this bug in a HUGE way. Edited a note with a table, it generated a number of line breaks <BR> possibly equivalent to the number of rows in the table. You can't edit the HTML in Ankidroid directly so I had to fix it on desktop. For some reason after multiple syncs, I can't fix the note. Big headache and obviously a big problem, but I have no coding experience and am unlikely to be able to contribute to solving it.

@user1823
Copy link
Contributor

user1823 commented Jan 28, 2024

@david-allison, as the other user mentioned, this bug still persists.

Turning off the option added in #7692 may solve it. But, this is not a good solution for most people because this would be moving a step away from a WYSIWYG editor (because all the <br> tags are visible).

Also, I think that the title of the preference is misleading. I think that it should be "Replace HTML with newlines" because it replaces <br> tags with newlines when enabled.

Example:

Desktop:

AnkiDroid: (displays incorrectly)

After syncing to Desktop without making any edit to the note on AnkiDroid, no change happens in the HTML.

But after making a small edit

The linebreak in the HTML gets replaced by a <br> tag.
image

Turning off the option added in #7692 solves the issue in the sense that the HTML is no longer altered after editing the card in AnkiDroid. However, the card still displays incorrectly on AnkiDroid (same as the second image above). Also, as mentioned above, it is a suboptimal solution because it takes the note editor one step away from a WYSIWYG editor.

@user1823

This comment was marked as resolved.

@edward-dauvergne
Copy link

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:

    /**
     * @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:

        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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Keep Open avoids the stale bot Priority-Medium Reproduced This bug has been reproduced by a maintainer
Projects
None yet