Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 2bc3735

Browse files
committed
Fix a bug where typing and delete a single space hid the placeholder.
1 parent d465693 commit 2bc3735

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ENV
22
**/.python-version
33
**/.vscode
4+
**/.idea
45
**/.DS_Store
56
# Generated
67
/target/

platforms/ios/lib/WysiwygComposer/Sources/HTMLParser/Extensions/NSAttributedString+Range.swift

+9
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ extension NSAttributedString {
7979
/// - range: the range on which the elements should be detected. Entire range if omitted
8080
/// - Returns: an array of matching ranges
8181
func discardableTextRanges(in range: NSRange? = nil) -> [NSRange] {
82+
// When typing a space into an empty composer, the single space being disposable results in
83+
// `applyReplaceAll` failing, leaving attributedContent out of sync. The reconciliation then
84+
// adds a second space to the model that isn't in the text view.
85+
if string == .zwsp {
86+
// This fixes that, although it doesn't help in the case of adding a single space to e.g.
87+
// a list/quote where the same result can be seen with the model having an extra space.
88+
return []
89+
}
90+
8291
var ranges = [NSRange]()
8392

8493
enumerateTypedAttribute(.discardableText, in: range) { (isDiscardable: Bool, range: NSRange, _) in

platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests.swift

+15
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ final class WysiwygComposerViewModelTests: XCTestCase {
4141
viewModel.textView.attributedText = viewModel.attributedContent.text
4242
waitExpectation(expectation: expectTrue, timeout: 2.0)
4343
}
44+
45+
func testIsContentEmptyAfterDeletingSingleSpace() {
46+
// When typing a single space.
47+
_ = viewModel.replaceText(range: .zero, replacementText: " ")
48+
viewModel.textView.attributedText = NSAttributedString(string: " ")
49+
viewModel.didUpdateText()
50+
51+
// And then deleting that space.
52+
_ = viewModel.replaceText(range: .init(location: 0, length: 1), replacementText: "")
53+
viewModel.textView.attributedText = NSAttributedString(string: "")
54+
viewModel.didUpdateText()
55+
56+
// Then the content should be empty for the placeholder to be shown.
57+
XCTAssertTrue(viewModel.isContentEmpty)
58+
}
4459

4560
func testSimpleTextInputIsAccepted() throws {
4661
let shouldChange = viewModel.replaceText(range: .zero,

0 commit comments

Comments
 (0)