Skip to content

Commit

Permalink
always use utf8 offsets (#3024)
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok authored Feb 28, 2024
1 parent a697b39 commit c7de733
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions modules/react-native-ui-text-view/ios/RNUITextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,15 @@ class RNUITextView: UIView {
fractionOfDistanceBetweenInsertionPoints: nil
)

var lastUpperOffset: Int = 0
var lastUpperBound: String.Index? = nil
for child in self.reactSubviews() {
if let child = child as? RNUITextViewChild, let childText = child.text {
let fullText = self.textView.attributedText.string

// We want to skip over the children we have already checked, otherwise we could run into
// collisions of similar strings (i.e. links that get shortened to the same hostname but
// different paths)
let startIndex = fullText.index(fullText.startIndex, offsetBy: lastUpperOffset)
let range = fullText.range(of: childText, options: [], range: startIndex..<fullText.endIndex)
let range = fullText.range(of: childText, options: [], range: (lastUpperBound ?? String.Index(utf16Offset: 0, in: fullText) )..<fullText.endIndex)

if let lowerBound = range?.lowerBound, let upperBound = range?.upperBound {
let lowerOffset = lowerBound.utf16Offset(in: fullText)
Expand All @@ -128,7 +127,7 @@ class RNUITextView: UIView {
{
return child
} else {
lastUpperOffset = upperOffset
lastUpperBound = upperBound
}
}
}
Expand Down

0 comments on commit c7de733

Please sign in to comment.