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
26 changes: 25 additions & 1 deletion Aztec/Classes/TextKit/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ open class TextView: UITextView {

ensureRemovalOfParagraphStylesBeforeRemovingCharacter(at: selectedRange)

super.deleteBackward()
preserveTypingAttributes {
super.deleteBackward()
}

ensureRemovalOfParagraphAttributesWhenPressingBackspaceAndEmptyingTheDocument()
ensureCursorRedraw(afterEditing: deletedString.string)
Expand Down Expand Up @@ -967,6 +969,28 @@ open class TextView: UITextView {
}


// WORKAROUND: iOS 11 introduced an issue that's causing UITextView to lose it's typing
// attributes under certain circumstances. This method will determine the Typing Attributes based on
/// the TextStorage attributes, whenever possible.
///
/// Issue: https://github.com/wordpress-mobile/AztecEditor-iOS/issues/749
///
private func preserveTypingAttributes(beforeDeletion block: () -> Void) {
let document = textStorage.string
guard selectedRange.location == document.characters.count else {
block()
return
}

let previousLocation = max(selectedRange.location - 1, 0)
let previousAttributes = textStorage.attributes(at: previousLocation, effectiveRange: nil)

block()

typingAttributes = previousAttributes
}


// MARK: - Links

/// Adds a link to the designated url on the specified range.
Expand Down
35 changes: 35 additions & 0 deletions AztecTests/TextKit/TextViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,9 @@ class TextViewTests: XCTestCase {
XCTAssertEqual(textView.text, Constants.sampleText0 + Constants.sampleText1 + String(.lineFeed) + String(.paragraphSeparator))
}


// MARK: - Media

func testInsertVideo() {
let textView = createEmptyTextView()
let _ = textView.replaceWithVideo(at: NSRange(location:0, length:0), sourceURL: URL(string: "video.mp4")!, posterURL: URL(string: "video.jpg"), placeHolderImage: nil)
Expand Down Expand Up @@ -1447,6 +1450,9 @@ class TextViewTests: XCTestCase {
XCTAssertEqual(textView.getHTML(), "<p><video src=\"newVideo.mp4\" poster=\"video.jpg\" data-wpvideopress=\"ABCDE\"></video></p>")
}


// MARK: - Comments

/// This test check if the insertion of a Comment Attachment works correctly and the expected tag gets inserted
///
func testInsertComment() {
Expand All @@ -1470,6 +1476,9 @@ class TextViewTests: XCTestCase {
XCTAssertEqual(html, "<p><!--some other comment should go here--><!--more--></p>")
}


// MARK: - HR

/// This test check if the insertion of an horizontal ruler works correctly and the hr tag is inserted
///
func testReplaceRangeWithHorizontalRuler() {
Expand Down Expand Up @@ -1571,6 +1580,9 @@ class TextViewTests: XCTestCase {
XCTAssertEqual(textView.getHTML(), "<p><img src=\"image.jpg\" class=\"alignnone wp-image-169\" alt=\"Changed Alt\" title=\"Title\"></p>")
}


// MARK: - Bugfixing

/// This test verifies that the H1 Header does not get lost during the Rich <> Raw transitioning.
///
func testToggleHtmlWithTwoEmptyLineBreaksDoesNotLooseHeaderStyle() {
Expand Down Expand Up @@ -1664,4 +1676,27 @@ class TextViewTests: XCTestCase {
let expected = "<ol><li><ol><li><ol><li><blockquote>First Item</blockquote></li></ol></li></ol></li></ol>"
XCTAssert(textView.getHTML(prettyPrint: false) == expected)
}

/// This test verifies that the `deleteBackward` call does not result in loosing the Typing Attributes.
/// Precisely, we'll ensure that the Italics style isn't lost after hitting backspace, and retyping the
/// deleted character.
///
/// Ref. Issue #749: Loosing Style after hitting Backspace
///
func testDeleteBackwardsDoesNotEndUpLoosingItalicsStyle() {
let textView = createTextView(withHTML: "")

textView.toggleBoldface(self)
textView.insertText("First Line")
textView.insertText("\n")

textView.toggleItalics(self)
textView.insertText("Second")

let expectedHTML = textView.getHTML(prettyPrint: false)
textView.deleteBackward()
textView.insertText("d")

XCTAssertEqual(textView.getHTML(prettyPrint: false), expectedHTML)
}
}