@@ -143,10 +143,10 @@ open class TextStorage: NSTextStorage {
143143
144144 // MARK: - NSAttributedString preprocessing
145145
146- private func preprocessAttributesForInsertion( _ attributedString: NSAttributedString ) -> NSAttributedString {
146+ private func preprocessAttributesForInsertion( _ attributedString: NSAttributedString , _ range : NSRange ) -> NSAttributedString {
147147 let stringWithAttachments = preprocessAttachmentsForInsertion ( attributedString)
148148 let stringWithHeadings = preprocessHeadingsForInsertion ( stringWithAttachments)
149- let preprocessedString = preprocessMarkForInsertion ( stringWithHeadings)
149+ let preprocessedString = preprocessMarkForInsertion ( stringWithHeadings, range )
150150
151151 return preprocessedString
152152 }
@@ -264,13 +264,18 @@ open class TextStorage: NSTextStorage {
264264 ///
265265 /// - Returns: the preprocessed string.
266266 ///
267- fileprivate func preprocessMarkForInsertion( _ attributedString: NSAttributedString ) -> NSAttributedString {
268- guard textStore. length > 0 , attributedString. length > 0 else {
267+ fileprivate func preprocessMarkForInsertion( _ attributedString: NSAttributedString , _ range : NSRange ) -> NSAttributedString {
268+ guard textStore. length > 0 , attributedString. length > 0 , range . endLocation != range . lowerBound else {
269269 return attributedString
270270 }
271+ var startAt = 0
272+
273+ if range. endLocation > range. lowerBound {
274+ startAt = range. lowerBound
275+ }
271276
272277 // Get the attributes of the start of the current string in storage.
273- let currentAttrs = attributes ( at: 0 , effectiveRange: nil )
278+ let currentAttrs = attributes ( at: startAt , effectiveRange: nil )
274279
275280 guard
276281 // the text currently in storage has a markHtmlRepresentation key
@@ -341,7 +346,7 @@ open class TextStorage: NSTextStorage {
341346
342347 override open func replaceCharacters( in range: NSRange , with attrString: NSAttributedString ) {
343348
344- let preprocessedString = preprocessAttributesForInsertion ( attrString)
349+ let preprocessedString = preprocessAttributesForInsertion ( attrString, range )
345350
346351 beginEditing ( )
347352
0 commit comments