Create a text validator/formatter mechanism#9535
Conversation
|
Soliciting some high level feedback before I put in all the tests |
There was a problem hiding this comment.
why only those two instead of all four?
nit: spaces around named argument declarations
There was a problem hiding this comment.
it was arbitrary :) changed
There was a problem hiding this comment.
... defaults to the empty string.
There was a problem hiding this comment.
Maybe:
/// This formatter attempts to preserve the [TextEditingValue.selection].
That said, as-is, I'm not convinced this adds much. It feels like we're saying 'This formatter behaves how you'd expect it to.' Perhaps more directly state that this formatter will adjust the selection in the case that blacklisted characters were removed?
There was a problem hiding this comment.
Is this correct?
During a copy-paste, I'd assume a replacementString where every contiguous string of newlines is collapsed to a single 0x20 space.
During a copy-paste, I'd assume a replacementString that replaces each newline with a 0x20 space.
During text entry, ideally I'd want \n to be replaced with the empty string, but in single-line text fields, I suspect we'll also set a keyboard that only supplies a Done key so using a space is probably a non-issue.
There was a problem hiding this comment.
Not sure I fully caught what you meant. Can you add an example? Or did you want r'\n+'?
if the user pastes:
with a replacement string of ' ' (1 space), it outputting ' ' (3 spaces) feels more predictable (and custom behaviours are 1 line away for the developer).
There was a problem hiding this comment.
Corrected my original statement. Should be a straight-up 1:1 replacement of \n with a space.
There was a problem hiding this comment.
I think that's true no? If there's 20 \n in whichever placement, replaceAll will replace them all with 20 spaces right?
There was a problem hiding this comment.
nit: the formatting here isn't consistent with the style we normally use
There was a problem hiding this comment.
nit: newline between the members
There was a problem hiding this comment.
can we guarantee that you can't compose something with a newline?
There was a problem hiding this comment.
Can't but I think it's a bit too edge case-y to worry about. My intent is just handle cases where if someone blacklists latin characters and starts typing chinese, there should still be latin characters in the composition interim. If the user presses send/save/wtv right at that moment, I think we should 'commit' the composition first (which triggers the formatting) and then pass it out.
There was a problem hiding this comment.
I think when you're in a single-line text field, we need to be able to guarantee that you cannot ever enter a newline.
There was a problem hiding this comment.
I was pedantic with the wording. But pragmatically, I don't think it's possible to enter a newline while composing.
There was a problem hiding this comment.
How about if the code passes in a newline in the original string? Or if someone writes a custom keyboard that does it?
There was a problem hiding this comment.
what did you mean the code passes in? Did you mean if the user passes in a second formatter that breaks the first one?
Either way, added handling to format text under composition
There was a problem hiding this comment.
I meant if someone writes Flutter code that sets the initial value of the editable text field to 'a\nb'.
There was a problem hiding this comment.
mutating the list is sketchy. if someone creates one then passes it in each time, it'll get progressively longer.
if someone passes in a const list, it'll crash.
There was a problem hiding this comment.
Ya, good call, that was a disaster... Changed
|
I expanded this PR a bit to avoid diffbase gymnastics.
Now fixes #9511. Fixes #9340. And fixes a bug in the gallery on iOS that prevents me from testing #9200. PTAL. Will add tests all around. |
There was a problem hiding this comment.
put the { on the previous line and the } on the next line to match similar code elsewhere in the codebase
There was a problem hiding this comment.
nit: don't use => if you have a line break
|
Added tests |
|
Gentle ping |
|
LGTM but @cbracken should rereview since he marked it as X. |
cbracken
left a comment
There was a problem hiding this comment.
lgtm
Since TextInputFormatter & co. aren't strictly dependent on anything in widgets, consider moving those to services. On the off chance anyone else decided to implement their own widgets alternative, they could still deal with the existing validators.
…ult implementations.
|
oh ya, that's a really good point. Since the TextSelection stuff is already in services. Done |
#9511
Add a text formatter interface used by EditingText. Provide some default implementations.