Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Aztec/Classes/Formatters/AttributeFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ protocol AttributeFormatter {
func applicationRange(for range: NSRange, in text: NSAttributedString) -> NSRange

func worksInEmptyRange() -> Bool

func needsEmptyLinePlaceholder() -> Bool
}


Expand Down Expand Up @@ -118,7 +120,7 @@ extension AttributeFormatter {
@discardableResult func applyAttributes(to text: NSMutableAttributedString, at range: NSRange) -> NSRange {
var rangeToApply = applicationRange(for: range, in: text)

if worksInEmptyRange() && ( rangeToApply.length == 0 || text.length == 0) {
if needsEmptyLinePlaceholder() && worksInEmptyRange() && ( rangeToApply.length == 0 || text.length == 0) {
let placeholder = placeholderForEmptyLine(using: placeholderAttributes)
text.insert(placeholder, at: rangeToApply.location)
rangeToApply = NSMakeRange(rangeToApply.location, placeholder.length)
Expand Down Expand Up @@ -210,6 +212,10 @@ extension CharacterAttributeFormatter {
func worksInEmptyRange() -> Bool {
return false
}

func needsEmptyLinePlaceholder() -> Bool {
return false
}
}


Expand All @@ -227,4 +233,8 @@ extension ParagraphAttributeFormatter {
func worksInEmptyRange() -> Bool {
return true
}

func needsEmptyLinePlaceholder() -> Bool {
return true
}
}
4 changes: 4 additions & 0 deletions Aztec/Classes/Formatters/HeaderFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,9 @@ open class HeaderFormatter: ParagraphAttributeFormatter {
}
return false
}

func needsEmptyLinePlaceholder() -> Bool {
return false
}
}

6 changes: 4 additions & 2 deletions Aztec/Classes/TextKit/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,8 @@ open class TextView: UITextView {
}

for formatter in formattersThatBreakAfterEnter {
if formatter.present(in: textStorage, at: range.location) {
formatter.removeAttributes(from: textStorage, at: range)
if formatter.present(in: typingAttributes) {
typingAttributes = formatter.remove(from: typingAttributes)
return true
}
}
Expand Down Expand Up @@ -833,8 +833,10 @@ open class TextView: UITextView {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
let pristine = self.selectedRange
let updated = NSMakeRange(max(pristine.location - 1, 0), 0)
let beforeTypingAttributes = self.typingAttributes
self.selectedRange = updated
self.selectedRange = pristine
self.typingAttributes = beforeTypingAttributes
}
}

Expand Down
27 changes: 27 additions & 0 deletions AztecTests/TextViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,31 @@ class AztecVisualTextViewTests: XCTestCase {

XCTAssertEqual(textView.getHTML(), "<h1>Header</h1>")
}


/// Tests that there is no HTML Corruption when editing text, after toggling H1 and entering two lines of text.
///
/// Input:
/// - "Header\n12" (Inserted character by character)
/// - Delete Backwards event.
///
/// Ref. https://github.com/wordpress-mobile/WordPress-Aztec-iOS/issues/407
///
func testDeletingBackwardAfterTogglingHeaderDoesNotTriggerInvalidHTML() {
let textView = createTextView(withHTML: "")

textView.toggleHeader(.h1, range: .zero)
textView.insertText("H")
textView.insertText("e")
textView.insertText("a")
textView.insertText("d")
textView.insertText("e")
textView.insertText("r")
textView.insertText("\n")
textView.insertText("1")
textView.insertText("2")
textView.deleteBackward()

XCTAssertEqual(textView.getHTML(), "<h1>Header<br></h1>1")
}
}