Trash character in webview's textfield

Using iOS keyboard extension, I want to move the caret to first of strings in textfield.

So, I try to get the string before cursor, and move cursor. The offset is the text length, like below.

The textfield's text in safari(apple.com search field) is "abcd". But, the return value is " abcd" including space character at first.

So, the cursor does not move because the length(length 5) is exceeded the real text length(length 4).

This is definitely appeared independently of iOS versions.

guard let text = textDocumentProxy.documentContextBeforeInput else {
      return
    }
     
    print("text(\(text)) = \(text.count)")
     
    textDocumentProxy.adjustTextPosition(byCharacterOffset: -text.count)
Trash character in webview's textfield
 
 
Q