@@ -580,32 +580,19 @@ class TextStorageTests: XCTestCase {
580580 XCTAssertEqual ( expectedResult, result)
581581 }
582582
583- /// Verifies that missing Heading attributes are retained on string replacements
583+ /// Verifies that missing Heading attributes are retained on string replacements when appropriate
584584 ///
585585 func testMissingHeadingAttributeIsRetained( ) {
586- let defaultAttributes : [ NSAttributedString . Key : Any ] = [
587- . font: UIFont . systemFont ( ofSize: 14 ) ,
588- . paragraphStyle: ParagraphStyle . default,
589- . headingRepresentation: 2
590- ]
591-
592- let headerString = NSAttributedString (
593- string: " Hello i'm a header " ,
594- attributes: defaultAttributes
595- )
596-
597- storage. setAttributedString ( headerString)
586+ let formatter = HeaderFormatter ( headerLevel: . h2)
587+ storage. replaceCharacters ( in: storage. rangeOfEntireString, with: " Hello i'm a header " )
588+ formatter. applyAttributes ( to: storage, at: storage. rangeOfEntireString)
598589
599590 let originalAttributes = storage. attributes ( at: 0 , effectiveRange: nil )
600591 XCTAssertEqual ( storage. string, " Hello i'm a header " )
601592 XCTAssertEqual ( originalAttributes. count, 3 )
602593 XCTAssertNotNil ( originalAttributes [ . headingRepresentation] )
603594
604- // autocorrect seemed to strip custom keys, so remove .headingRepresentation
605- let autoCorrectedAttributes : [ NSAttributedString . Key : Any ] = [
606- . font: UIFont . systemFont ( ofSize: 14 ) ,
607- . paragraphStyle: ParagraphStyle . default
608- ]
595+ let autoCorrectedAttributes = originalAttributes. filter { $0. key != . headingRepresentation }
609596
610597 let autoCorrectedString = NSAttributedString (
611598 string: " I'm " ,
@@ -617,7 +604,33 @@ class TextStorageTests: XCTestCase {
617604
618605 let finalAttributes = storage. attributes ( at: range. location, effectiveRange: nil )
619606 XCTAssertEqual ( storage. string, " Hello I'm a header " )
620- XCTAssertEqual ( finalAttributes. count, 3 )
621- XCTAssertNotNil ( finalAttributes [ . headingRepresentation] )
607+ XCTAssertEqual ( originalAttributes. keys, finalAttributes. keys)
608+ }
609+
610+ /// Verifies that converting a Heading to a Paragraph doesn't retain the heading attribute
611+ ///
612+ func testHeadingToParagraphDoesNotRetainHeadingAttribute( ) {
613+ let headerFormatter = HeaderFormatter ( headerLevel: . h2)
614+ storage. replaceCharacters ( in: storage. rangeOfEntireString, with: " Hello I'm a header " )
615+ headerFormatter. applyAttributes ( to: storage, at: storage. rangeOfEntireString)
616+
617+ let originalAttributes = storage. attributes ( at: 0 , effectiveRange: nil )
618+ XCTAssertEqual ( storage. string, " Hello I'm a header " )
619+ XCTAssertNotNil ( originalAttributes [ . headingRepresentation] )
620+
621+ let paragraphAttributes : [ NSAttributedString . Key : Any ] = [
622+ . font: UIFont . systemFont ( ofSize: 14 ) ,
623+ . paragraphStyle: ParagraphStyle . default
624+ ]
625+
626+ let paragraphString = NSAttributedString (
627+ string: " Hello I'm a paragraph " ,
628+ attributes: paragraphAttributes
629+ )
630+ storage. replaceCharacters ( in: storage. rangeOfEntireString, with: paragraphString)
631+
632+ let finalAttributes = storage. attributes ( at: 0 , effectiveRange: nil )
633+ XCTAssertEqual ( storage. string, " Hello I'm a paragraph " )
634+ XCTAssertNil ( finalAttributes [ . headingRepresentation] )
622635 }
623636}
0 commit comments