Skip to content

Create a text validator/formatter mechanism#9535

Merged
xster merged 13 commits intoflutter:masterfrom
xster:text-formatter
Apr 28, 2017
Merged

Create a text validator/formatter mechanism#9535
xster merged 13 commits intoflutter:masterfrom
xster:text-formatter

Conversation

@xster
Copy link
Copy Markdown
Member

@xster xster commented Apr 21, 2017

#9511

Add a text formatter interface used by EditingText. Provide some default implementations.

@xster xster requested review from abarth and cbracken April 21, 2017 21:18
@xster
Copy link
Copy Markdown
Member Author

xster commented Apr 21, 2017

Soliciting some high level feedback before I put in all the tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only those two instead of all four?

nit: spaces around named argument declarations

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was arbitrary :) changed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/.$/ patterns./

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... defaults to the empty string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reworded a bit

Copy link
Copy Markdown
Member

@cbracken cbracken Apr 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

@xster xster Apr 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected my original statement. Should be a straight-up 1:1 replacement of \n with a space.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's true no? If there's 20 \n in whichever placement, replaceAll will replace them all with 20 spaces right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly - yep.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the formatting here isn't consistent with the style we normally use

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline between the members

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we guarantee that you can't compose something with a newline?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was pedantic with the wording. But pragmatically, I don't think it's possible to enter a newline while composing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about if the code passes in a newline in the original string? Or if someone writes a custom keyboard that does it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant if someone writes Flutter code that sets the initial value of the editable text field to 'a\nb'.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, good call, that was a disaster... Changed

@xster
Copy link
Copy Markdown
Member Author

xster commented Apr 22, 2017

I expanded this PR a bit to avoid diffbase gymnastics.

  • Added a WhitelistingTextInputFormatter
  • Use a digits only whitelisting formatter in the gallery demo
  • Add a phone number formatter in the gallery demo

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

US phone number

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put the { on the previous line and the } on the next line to match similar code elsewhere in the codebase

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: don't use => if you have a line break

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@xster
Copy link
Copy Markdown
Member Author

xster commented Apr 25, 2017

Added tests

@xster
Copy link
Copy Markdown
Member Author

xster commented Apr 28, 2017

Gentle ping

@Hixie
Copy link
Copy Markdown
Contributor

Hixie commented Apr 28, 2017

LGTM but @cbracken should rereview since he marked it as X.

Copy link
Copy Markdown
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@xster
Copy link
Copy Markdown
Member Author

xster commented Apr 28, 2017

oh ya, that's a really good point. Since the TextSelection stuff is already in services. Done

@xster xster merged commit f65fea8 into flutter:master Apr 28, 2017
@xster xster deleted the text-formatter branch April 28, 2017 22:33
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants