Skip to content

Commit 1fd6fa2

Browse files
authored
Fixed issue with double tapping on textblock that may cause selected range to change (#300)
1 parent 7910aa2 commit 1fd6fa2

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Diff for: Proton/Sources/Swift/Core/RichTextView.swift

+5
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ class RichTextView: AutogrowingTextView {
247247
textBlockRange.contains(location)
248248
else { return nil }
249249

250+
// return textblock range if new range is entirely contained within textblock range
251+
if textBlockRange.intersection(new) == new {
252+
return textBlockRange
253+
}
254+
250255
if isReverseTraversal {
251256
return adjustedTextBlockRangeReverse(new: new, old: old, textBlockRange: textBlockRange)
252257
} else {

Diff for: Proton/Tests/Core/TextBlockAttributeTests.swift

+15-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ class TextBlockAttributeTests: XCTestCase {
3939
XCTAssertEqual(textView.selectedRange, NSRange(location: 8, length: 0))
4040
}
4141

42+
func testDoubleTapOnTextBlock() {
43+
let textView = RichTextView(context: RichTextViewContext())
44+
let text = NSMutableAttributedString(string: "01234", attributes: [.textBlock: true])
45+
textView.attributedText = text
46+
47+
textView.selectedRange = text.fullRange
48+
let range = NSRange(location: 0, length: 1).toTextRange(textInput: textView)
49+
textView.selectedTextRange = range
50+
XCTAssertEqual(textView.selectedRange, text.fullRange)
51+
}
52+
4253
func testSetsFocusBeforeForNonFocusableText() {
4354
let textView = RichTextView(context: RichTextViewContext())
4455
let text = NSMutableAttributedString(string: "0123")
@@ -147,7 +158,7 @@ class TextBlockAttributeTests: XCTestCase {
147158
XCTAssertEqual(range, NSRange(location: 12, length: 7))
148159
}
149160

150-
func testUnselectsSelectedTextBlockForward() {
161+
func testRetainsSelectedTextBlockForward() {
151162
let textView = RichTextView(context: RichTextViewContext())
152163
let text = NSMutableAttributedString(string: "This is test string")
153164
textView.attributedText = text
@@ -157,10 +168,10 @@ class TextBlockAttributeTests: XCTestCase {
157168
textView.selectedTextRange = NSRange(location: 9, length: 3).toTextRange(textInput: textView) // "est"
158169
let range = textView.selectedRange
159170

160-
XCTAssertEqual(range, NSRange(location: 12, length: 0))
171+
XCTAssertEqual(range, NSRange(location: 8, length: 4))
161172
}
162173

163-
func testUnselectsSelectedTextBlockReverse() {
174+
func testRetainsSelectedTextBlockReverse() {
164175
let textView = RichTextView(context: RichTextViewContext())
165176
let text = NSMutableAttributedString(string: "This is test string")
166177
textView.attributedText = text
@@ -170,7 +181,7 @@ class TextBlockAttributeTests: XCTestCase {
170181
textView.selectedTextRange = NSRange(location: 8, length: 3).toTextRange(textInput: textView) // "tes"
171182
let range = textView.selectedRange
172183

173-
XCTAssertEqual(range, NSRange(location: 8, length: 0))
184+
XCTAssertEqual(range, NSRange(location: 8, length: 4))
174185
}
175186

176187
func testUnselectsTextSelectedWithTextBlockReverse() {

Diff for: Proton/Tests/ExtensionTests/NSAttributedStringExtensionTests.swift

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class NSAttributedStringExtensionTests: XCTestCase {
3030
XCTAssertEqual(range, NSRange(location: 0, length: text.length))
3131
}
3232

33+
func testGetsSubstring() {
34+
let text = NSAttributedString(string: "This is a test string")
35+
let substring = text.substring(from: NSRange(location: 5, length: 2))
36+
XCTAssertEqual(substring, "is")
37+
}
38+
3339
func testGetRangeForAttachment() {
3440
let string = "This is a test string"
3541
let text = NSMutableAttributedString(string: string)

0 commit comments

Comments
 (0)