99
1010#include < algorithm>
1111#include < memory>
12+ #include < string>
1213
1314#include " flutter/common/constants.h"
1415#include " flutter/fml/platform/darwin/string_range_sanitization.h"
@@ -95,6 +96,19 @@ typedef NS_ENUM(NSUInteger, FlutterTextAffinity) {
9596 return flutter::TextRange ([base unsignedLongValue ], [extent unsignedLongValue ]);
9697}
9798
99+ // Returns |range| as a TextRange clamped to |length|.
100+ //
101+ // A selection made backwards is reported as a range whose length is negative
102+ // when read as a signed value, so the length is interpreted signed and the
103+ // direction of the range is preserved. A location of NSNotFound clamps to
104+ // |length|.
105+ static flutter::TextRange ClampNSRange (NSRange range, size_t length) {
106+ const long text_length = static_cast <long >(length);
107+ const long signed_length = std::clamp (static_cast <long >(range.length ), -text_length, text_length);
108+ const long base = std::clamp (static_cast <long >(range.location ), 0L , text_length);
109+ return flutter::TextRange (base, std::clamp (base + signed_length, 0L , text_length));
110+ }
111+
98112// Returns the autofill hint content type, if specified; otherwise nil.
99113static NSString * GetAutofillHint (NSDictionary * autofill) {
100114 NSArray <NSString *>* hints = autofill[kAutofillHints ];
@@ -535,6 +549,9 @@ - (void)updateCaretRect:(NSDictionary*)dictionary {
535549}
536550
537551- (void )setEditingState : (NSDictionary *)state {
552+ if (_activeModel == nullptr ) {
553+ return ;
554+ }
538555 NSString * selectionAffinity = state[kSelectionAffinityKey ];
539556 if (selectionAffinity != nil ) {
540557 _textAffinity = [selectionAffinity isEqualToString: kTextAffinityUpstream ]
@@ -543,13 +560,25 @@ - (void)setEditingState:(NSDictionary*)state {
543560 }
544561
545562 NSString * text = state[kTextKey ];
563+ if (![text isKindOfClass: [NSString class ]]) {
564+ text = @" " ;
565+ }
546566
547- flutter::TextRange selected_range = RangeFromBaseExtent (
548- state[kSelectionBaseKey ], state[kSelectionExtentKey ], _activeModel->selection ());
549- _activeModel->SetSelection (selected_range);
550-
551- flutter::TextRange composing_range = RangeFromBaseExtent (
552- state[kComposingBaseKey ], state[kComposingExtentKey ], _activeModel->composing_range ());
567+ // The framework computes these ranges against its own copy of the text, which may be a different
568+ // revision than the text it sends here, and the ranges are dropped entirely by |SetText| if they
569+ // don't fit.
570+ //
571+ // Clamp them to the incoming text so a stale offset degrades the caret position rather than
572+ // discarding the text update.
573+ size_t text_length = text.length ;
574+ flutter::TextRange selected_range =
575+ RangeFromBaseExtent (state[kSelectionBaseKey ], state[kSelectionExtentKey ],
576+ _activeModel->selection ())
577+ .ClampedTo (text_length);
578+ flutter::TextRange composing_range =
579+ RangeFromBaseExtent (state[kComposingBaseKey ], state[kComposingExtentKey ],
580+ _activeModel->composing_range ())
581+ .ClampedTo (text_length);
553582
554583 const bool wasComposing = _activeModel->composing ();
555584 _activeModel->SetText ([text UTF8String ], selected_range, composing_range);
@@ -772,17 +801,7 @@ - (void)insertText:(id)string replacementRange:(NSRange)range {
772801 _eventProducedOutput |= true ;
773802
774803 if (range.location != NSNotFound ) {
775- // The selected range can actually have negative numbers, since it can start
776- // at the end of the range if the user selected the text going backwards.
777- // Cast to a signed type to determine whether or not the selection is reversed.
778- long signedLength = static_cast <long >(range.length );
779- long location = range.location ;
780- long textLength = _activeModel->text_range ().end ();
781-
782- size_t base = std::clamp (location, 0L , textLength);
783- size_t extent = std::clamp (location + signedLength, 0L , textLength);
784-
785- _activeModel->SetSelection (flutter::TextRange (base, extent));
804+ _activeModel->SetSelection (ClampNSRange (range, _activeModel->text_range ().end ()));
786805 } else if (_activeModel->composing () &&
787806 !(_activeModel->composing_range () == _activeModel->selection ())) {
788807 // When confirmed by Japanese IME, string replaces range of composing_range.
@@ -902,10 +921,11 @@ - (void)setMarkedText:(id)string
902921 // the case, because in situations where the replacementRange is actually
903922 // specified (i.e. when switching between characters equivalent after long
904923 // key press) the replacementRange is provided while there is no composition.
924+ //
925+ // The range is clamped to the text because SetComposingRange rejects ranges
926+ // that don't fit.
905927 _activeModel->SetComposingRange (
906- flutter::TextRange (replacementRange.location ,
907- replacementRange.location + replacementRange.length ),
908- 0 );
928+ ClampNSRange (replacementRange, _activeModel->text_range ().end ()), 0 );
909929 }
910930
911931 flutter::TextRange composingBeforeChange = _activeModel->composing_range ();
@@ -914,12 +934,20 @@ - (void)setMarkedText:(id)string
914934 // Input string may be NSString or NSAttributedString.
915935 BOOL isAttributedString = [string isKindOfClass: [NSAttributedString class ]];
916936 const NSString * rawString = isAttributedString ? [string string ] : string;
917- _activeModel->UpdateComposingText (
918- (const char16_t *)[rawString cStringUsingEncoding: NSUTF16StringEncoding],
919- flutter::TextRange (selectedRange.location , selectedRange.location + selectedRange.length ));
937+
938+ // Copy the UTF-16 code units directly. The C string form truncates at an
939+ // embedded NUL and returns NULL when the string cannot be converted.
940+ std::u16string marked_text_utf16 (rawString.length , u' \0 ' );
941+ [rawString getCharacters: reinterpret_cast <unichar *>(marked_text_utf16.data ())
942+ range: NSMakeRange (0 , marked_text_utf16.length ())];
943+
944+ // selectedRange is provided by the IME. Re-clamp to the text.
945+ // NSNotFound (no selection), clamps to end of the marked text.
946+ _activeModel->UpdateComposingText (marked_text_utf16,
947+ ClampNSRange (selectedRange, marked_text_utf16.length ()));
920948
921949 if (_enableDeltaModel) {
922- std::string marked_text = [rawString UTF8String ];
950+ std::string marked_text = rawString ? [rawString UTF8String ] : " " ;
923951 [self updateEditStateWithDelta: flutter: :TextEditingDelta (textBeforeChange,
924952 selectionBeforeChange.collapsed ()
925953 ? composingBeforeChange
0 commit comments