From 393b5ff8274553750d971a81ceeb2f9b2f2b9172 Mon Sep 17 00:00:00 2001 From: Rajdeep Kwatra Date: Wed, 12 Jun 2024 14:41:55 +1000 Subject: [PATCH] Fixed logic for attachment ranges (#311) --- Proton/Sources/Swift/Editor/EditorView.swift | 2 +- Proton/Tests/Editor/EditorViewTests.swift | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Proton/Sources/Swift/Editor/EditorView.swift b/Proton/Sources/Swift/Editor/EditorView.swift index 823a4b62..14a8bdbb 100644 --- a/Proton/Sources/Swift/Editor/EditorView.swift +++ b/Proton/Sources/Swift/Editor/EditorView.swift @@ -838,7 +838,7 @@ open class EditorView: UIView { } public func attachmentsInRange(_ range: NSRange) -> [AttachmentRange] { - guard range.endLocation < attributedText.length else { return [] } + guard range.endLocation <= attributedText.length else { return [] } let substring = attributedText.attributedSubstring(from: range) return substring.attachmentRanges } diff --git a/Proton/Tests/Editor/EditorViewTests.swift b/Proton/Tests/Editor/EditorViewTests.swift index 0b104b40..17ed1cfb 100644 --- a/Proton/Tests/Editor/EditorViewTests.swift +++ b/Proton/Tests/Editor/EditorViewTests.swift @@ -687,6 +687,23 @@ class EditorViewTests: XCTestCase { XCTAssertEqual(text.string, expectedString) } + func testAttachmentsInRangeWithValidRange() { + let editor = EditorView() + + let panel1 = PanelView() + panel1.editor.replaceCharacters(in: .zero, with: NSAttributedString(string: "Text inside panel 1")) + let panelAttachment1 = Attachment(panel1, size: .matchContent) + editor.insertAttachment(in: editor.textEndRange, attachment: panelAttachment1) + + let panel2 = PanelView() + panel2.editor.replaceCharacters(in: .zero, with: NSAttributedString(string: "Text inside panel 2")) + let panelAttachment2 = Attachment(panel2, size: .matchContent) + editor.insertAttachment(in: editor.textEndRange, attachment: panelAttachment2) + + let attachments = editor.attachmentsInRange(NSRange(location: editor.attributedText.length - 2, length: 2)) + XCTAssertEqual(attachments.count, 1) + } + func testGetsFullAttributedTextFromRange() { let viewController = EditorTestViewController() let editor = viewController.editor