Skip to content

Commit 9cdaa78

Browse files
committed
Add option to replace last empty lines by another special character.
1 parent 236fcf9 commit 9cdaa78

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

Aztec/Classes/NSAttributedString/Conversions/HTMLConverter.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public class HTMLConverter {
2020
}
2121

2222
// MARK: - Converters: HTML -> AttributedString
23-
23+
24+
/// If a value is set the character set will be used to replace any last empty line created by the converter.
25+
///
26+
open var characterToReplaceLastEmtpyLine: Character?
27+
2428
let htmlToTree = HTMLParser()
2529

2630
private(set) lazy var treeToAttributedString: AttributedStringSerializer = {
@@ -54,11 +58,27 @@ public class HTMLConverter {
5458
pluginManager.process(htmlTree: rootNode)
5559

5660
let defaultAttributes = defaultAttributes ?? [:]
57-
let attributedString = treeToAttributedString.serialize(rootNode, defaultAttributes: defaultAttributes)
61+
var attributedString = treeToAttributedString.serialize(rootNode, defaultAttributes: defaultAttributes)
62+
63+
if let characterToUse = characterToReplaceLastEmtpyLine {
64+
attributedString = replaceLastEmptyLine(in: attributedString, with: characterToUse)
65+
}
5866

5967
return attributedString
6068
}
6169

70+
func replaceLastEmptyLine(in attributedString: NSAttributedString, with replacement: Character) -> NSAttributedString {
71+
var result = attributedString
72+
let string = attributedString.string
73+
if string.isEmptyLineAtEndOfFile(at: string.count), !string.isEmpty, let location = string.location(before: attributedString.length) {
74+
let mutableString = NSMutableAttributedString(attributedString: attributedString)
75+
let attributes = mutableString.attributes(at: location, effectiveRange: nil)
76+
mutableString.replaceCharacters(in: NSRange(location: location, length: attributedString.length-location), with: NSAttributedString(string: String(replacement), attributes: attributes))
77+
result = mutableString
78+
}
79+
return result
80+
}
81+
6282

6383
/// Check if the given html string is supported to be parsed into Attributed Strings.
6484
///

AztecTests/TextKit/TextStorageTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,26 @@ class TextStorageTests: XCTestCase {
494494

495495
XCTAssertEqual(expectedResult, result)
496496
}
497+
498+
func testEmptyListOutput() {
499+
let initialHTML = "<ul><li></li></ul>"
500+
501+
// Setup
502+
let defaultAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 14),
503+
.paragraphStyle: ParagraphStyle.default]
504+
505+
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
506+
var expectedResult = String(.paragraphSeparator)
507+
var result = String(storage.string)
508+
509+
XCTAssertEqual(expectedResult, result)
510+
511+
storage.htmlConverter.characterToReplaceLastEmtpyLine = Character(.zeroWidthSpace)
512+
513+
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
514+
expectedResult = String(.zeroWidthSpace)
515+
result = String(storage.string)
516+
517+
XCTAssertEqual(expectedResult, result)
518+
}
497519
}

0 commit comments

Comments
 (0)