Skip to content

Commit

Permalink
Fixed editmask to include characters only if delta is not 0
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Jul 11, 2024
1 parent 482128d commit 88f01f2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Proton/Sources/ObjC/PRTextStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str {
NSInteger delta = str.length - range.length;
[_storage replaceCharactersInRange:range withString:str];
[_storage fixAttributesInRange:NSMakeRange(0, _storage.length)];
[self edited:NSTextStorageEditedCharacters | NSTextStorageEditedAttributes range:range changeInLength:delta];

NSTextStorageEditActions editedMask = NSTextStorageEditedAttributes;

if (delta != 0) {
editedMask = NSTextStorageEditedCharacters | NSTextStorageEditedAttributes;
}

[self edited: editedMask range:range changeInLength:delta];
[self endEditing];
}

Expand Down
1 change: 1 addition & 0 deletions Proton/Tests/Helpers/AttachmentGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct AttachmentGenerator {
for col in 0..<numColumns {
let editorInit = {
let editor = EditorView(allowAutogrowing: false)
editor.forceApplyAttributedText = true
editor.attributedText = NSAttributedString(string: "Table \(id) {\(row), \(col)} Text in cell")
return editor
}
Expand Down
44 changes: 43 additions & 1 deletion Proton/Tests/TextProcessors/TextProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,48 @@ class TextProcessorTests: XCTestCase {
waitForExpectations(timeout: 1.0)
}

func testInvokesDidProcessEditingContentOnPartialDelete() {
let testExpectation = functionExpectation()

let editor = EditorView()
let name = "TextProcessorTest"
let mockProcessor = MockTextProcessor(name: name)
mockProcessor.didProcessEditing = { processedEditor, editingMask, range, delta in
XCTAssertEqual(processedEditor, editor)
XCTAssertTrue(editingMask.contains(.editedAttributes))
XCTAssertTrue(editingMask.contains(.editedCharacters))
XCTAssertEqual(delta, -2)
testExpectation.fulfill()
}
let testString = NSAttributedString(string: "test")
editor.replaceCharacters(in: .zero, with: testString)
editor.registerProcessor(mockProcessor)
let processRange = NSRange(location: 2, length: 2)
editor.replaceCharacters(in: processRange, with: NSAttributedString())
waitForExpectations(timeout: 1.0)
}

func testInvokesDidProcessEditingContentOnFullDelete() {
let testExpectation = functionExpectation()

let editor = EditorView()

let name = "TextProcessorTest"
let mockProcessor = MockTextProcessor(name: name)
mockProcessor.didProcessEditing = { processedEditor, editingMask, range, delta in
XCTAssertEqual(processedEditor, editor)
XCTAssertTrue(editingMask.contains(.editedAttributes))
XCTAssertTrue(editingMask.contains(.editedCharacters))
XCTAssertEqual(delta, -4)
testExpectation.fulfill()
}
let testString = NSAttributedString(string: "test")
editor.replaceCharacters(in: .zero, with: testString)
editor.registerProcessor(mockProcessor)
editor.replaceCharacters(in: editor.attributedText.fullRange, with: NSAttributedString())
waitForExpectations(timeout: 1.0)
}

func testInvokesWillProcessAttributeChanges() {
let testExpectation = functionExpectation()

Expand All @@ -394,7 +436,7 @@ class TextProcessorTests: XCTestCase {
waitForExpectations(timeout: 1.0)
}

func testInvokesDidlProcessAttributeChanges() {
func testInvokesDidProcessAttributeChanges() {
let testExpectation = functionExpectation()

let editor = EditorView()
Expand Down

0 comments on commit 88f01f2

Please sign in to comment.