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
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ extension NSMutableAttributedString {

assert(!replacementString.contains(stringToFind),
"Allowing the replacement string to contain the original string would result in a ininite loop.")

let swiftUTF16Range = string.utf16.range(from: range)
let rangeDifference = (replacementString as NSString).length - (stringToFind as NSString).length
var swiftUTF16Range = string.utf16.range(from: range)
var swiftRange = string.range(from: swiftUTF16Range)

while let matchRange = string.range(of: stringToFind, options: [], range: swiftRange, locale: nil) {
let matchNSRange = string.utf16NSRange(from: matchRange)

replaceCharacters(in: matchNSRange, with: replacementString)

// Since the new string may invalidate the initial swift range, we want to update it.
swiftRange = swiftRange.lowerBound ..< string.index(matchRange.lowerBound, offsetBy: replacementString.count)
// And because the string that backs the NSAttributeString will change after the replace we need to recalculate the index from scratch
let updatedRange = range.extendedRight(by: rangeDifference)
swiftUTF16Range = string.utf16.range(from: updatedRange)
swiftRange = string.range(from: swiftUTF16Range)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,14 @@ class NSMutableAttributedStringReplaceOcurrencesTests: XCTestCase {

XCTAssertEqual(newAttrString, NSAttributedString(string: "🌎💚💚😬💚🌎"))
}

/// Tests that replacing ocurrences on the end of a range work correctly
///
func testReplaceOcurrencesThatAreOnEndOfRange() {
let attrString = NSAttributedString(string: "Hello"+String(.paragraphSeparator)+"Amazing"+String(.paragraphSeparator)+"World"+String(.paragraphSeparator))
let newAttrString = NSMutableAttributedString(attributedString: attrString)
newAttrString.replaceOcurrences(of: String(.paragraphSeparator), with: String(.lineFeed), within: NSRange(location:6, length: 8))

XCTAssertEqual(newAttrString, NSAttributedString(string: "Hello"+String(.paragraphSeparator)+"Amazing"+String(.lineFeed)+"World"+String(.paragraphSeparator)))
}
}
16 changes: 16 additions & 0 deletions AztecTests/TextKit/TextViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,22 @@ class TextViewTests: XCTestCase {
XCTAssertEqual(textView.text, Constants.sampleText0 + Constants.sampleText1 + String(.lineFeed) + String(.lineFeed))
}

/// Verifies that toggling a Blockquote paragraph in the middle of paragraph does not crash system.
///
func testTogglingBlockquoteOnMiddleOfRangeDoesNotCrash() {
let initialQuote = """
<blockquote><strong>Quote One</strong></blockquote>
<blockquote><strong>Quote Two</strong></blockquote>
<blockquote><strong>Quote Three</strong></blockquote>
"""
let textView = TextViewStub(withHTML: initialQuote)

textView.selectedTextRange = textView.textRange(from: textView.endOfDocument, to: textView.endOfDocument)
textView.toggleBlockquote(range: NSRange(location: 11, length: 0))

XCTAssertEqual(textView.text, "Quote One" + String(.paragraphSeparator)+"Quote Two"+String(.lineFeed)+"Quote Three")
}


// MARK: - Pre

Expand Down