Skip to content

Commit 8d72edc

Browse files
committed
Account for Header to Paragraph conversions.
1 parent cce2da7 commit 8d72edc

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

Aztec/Classes/TextKit/TextStorage.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,14 @@ open class TextStorage: NSTextStorage {
233233
let currentAttrs = attributes(at: 0, effectiveRange: nil)
234234

235235
guard
236+
// the text currently in storage has a headingRepresentation key
236237
let headerSize = currentAttrs[.headingRepresentation],
237-
attributedString.attribute(.headingRepresentation, at: 0, effectiveRange: nil) == nil
238+
// the text coming in doesn't have a headingRepresentation key
239+
attributedString.attribute(.headingRepresentation, at: 0, effectiveRange: nil) == nil,
240+
// the text coming in has a paragraph style attribute
241+
let paragraphStyle = attributedString.attributes(at: 0, effectiveRange: nil)[.paragraphStyle] as? ParagraphStyle,
242+
// the paragraph style contains a property that's a Header type
243+
paragraphStyle.properties.contains(where: { $0 is Header })
238244
else {
239245
// Either the heading attribute wasn't present in the existing string,
240246
// or the attributed string already had it.

AztecTests/TextKit/TextStorageTests.swift

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)