Search Unity

Bug Pasted /r/n aren't removed and break input fields

Discussion in 'UGUI & TextMesh Pro' started by Kamyker, Jul 11, 2021.

  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
    This is what happens when there's copy/pasted \r\n at the end:

    Fix in TMP_InputField::Append(string input):
    Code (CSharp):
    1. for (int i = 0, imax = input.Length; i < imax; ++i)
    2. {
    3.     char c = input[i];
    4.  
    5.     if (c >= ' ' || c == '\t' || c == 10 || c == '\n'
    6.         || (c == '\r' && (i + 1 == imax || input[i + 1] != '\n')))
    7.     {
    8.         Append(c);
    9.     }
    10. }
     
    Last edited: Jul 11, 2021
  2. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I am trying to reproduce the behavior you outlined above.

    Are you copy / pasting just "\r\n"?

    Are you inserting these before or after the "{"?
     
  4. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
    I'm copying the new line on windows "CR LF". Better example:

    Insert (Ctrl + V) "CR LF" inside a word and then hit backspace. I guess what's happening is that LF is removed but CR is still there breaking the input.

    Instead of backspace you can try pressing left arrow, the first click will go back to previous line, next one will do nothing like it moved left of CR that's treated as empty sign.

    I guess better fix (mine just ignores CR when next char is LF) would be to remove CRLF together when using backspace or delete and jump over them when using arrows. #Edit Or not as there's logic that converts single CRs to LFs when m_SoftKeyboard is used. Ignoring CRs and converting single ones to LF everywhere seems like simpler solution.
     
    Last edited: Aug 7, 2021
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I could prevent <CR> from being appended to the text with the following simple change.

    upload_2021-8-7_0-41-0.png

    However, this would not prevent a <CR> from existing in the text being set on the input field. As such, when using left or right arrow (just like in the Unity Editor) the caret would stop on those hidden control characters. Ie. stop on <CR> and <LR>. Same when using Backspace or Del.

    There are TMP users using <CR> in text objects to create text overwrite FX but does this make sense in an Input Field? Is anyone actually using <CR> in an input field?

    In Microsoft Word, Notepad++, etc. <CR><LF> are distinct characters but are treated as a single character when using in the sense that Backspace or Del will remove both.
     
  6. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
    I guess not as overwrite FX makes no sense there.

    Your fix sounds better and simper. c == 10 is the same as c == '\n'?
     
    Last edited: Aug 7, 2021
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Yes.

    I will actually address this differently as it is possible some users might be using <CR>.

    The change I will make will continue to allow <CR> to be present in the text but just like MS Word or similar text editors treat <CR><LF> as a single character, I will do so as well when using Backspace, Delete, Left and Right Arrow which these 2 characters will be treated as one when "Allow Rich Text Editing" is disabled.
     
    Kamyker likes this.
  8. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
    Seems like clicking at the end still breaks it, caret goes between \r and \n.

    In this example I click on the end of a line (a bit next to the last letter) than delete to remove \n


    This fixes it in OnPointerDown:
    Code (CSharp):
    1.  if(insertionSide == CaretPosition.Right && m_TextComponent.textInfo.characterInfo[insertionIndex].character == '\r')
    2.     insertionIndex--;
     
    Last edited: Aug 31, 2021
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Please see if you get the same behavior with the latest preview release of the TMP package.

    The latest preview for 2018.4 is version 1.6.0-preview.1, version 2.2.0-preview.1 for Unity 2019.4 and version 3.2.0-pre.1 for Unity 2020.x.

    In Unity 2021.x, you may have to manually add the package by editing the manifest.json in the Packages folder due to package discovery restrictions.
     
  10. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
    Bug I described there is on pre.1.


    Instead of deleting I can also start typing:
     
  11. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you provide me with a simple repro project for me to look at? If more convenient, you can submit a bug report with project or provide me with a zip of the project via PM (in case you don't want to share).

    If you go the submit bug way, please provide me with the Case # once you have it.
     
  12. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,090
    Sure, did both, zip is 20mb as there's no library folder, Case 1362068 7gb.