Skip to content

Commit 12d8bd0

Browse files
committed
Merge branch 'develop' into issue/notify_changes_only_on_user_event
2 parents 33dfadf + b9e565d commit 12d8bd0

4 files changed

Lines changed: 68 additions & 4 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 characterToReplaceLastEmptyLine: 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 = characterToReplaceLastEmptyLine {
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.isEmpty, string.isEmptyLineAtEndOfFile(at: string.count), string.hasSuffix(String(.paragraphSeparator)), 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
///

Aztec/Classes/TextKit/TextStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ open class TextStorage: NSTextStorage {
8181

8282
// MARK: - HTML Conversion
8383

84-
let htmlConverter = HTMLConverter()
84+
public let htmlConverter = HTMLConverter()
8585

8686
// MARK: - PluginManager
8787

Aztec/Classes/TextKit/TextView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ open class TextView: UITextView {
272272

273273
// MARK: - TextKit Aztec Subclasses
274274

275-
var storage: TextStorage {
275+
public var storage: TextStorage {
276276
return textStorage as! TextStorage
277277
}
278278

AztecTests/TextKit/TextStorageTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,48 @@ 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.characterToReplaceLastEmptyLine = Character(.zeroWidthSpace)
512+
513+
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
514+
expectedResult = String(.zeroWidthSpace)
515+
result = String(storage.string)
516+
517+
XCTAssertEqual(expectedResult, result)
518+
}
519+
520+
func testCiteOutput() {
521+
let initialHTML = "<cite>Hello<br></cite>"
522+
523+
// Setup
524+
let defaultAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 14),
525+
.paragraphStyle: ParagraphStyle.default]
526+
527+
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
528+
var expectedResult = String("Hello")+String(.lineSeparator)
529+
var result = String(storage.string)
530+
531+
XCTAssertEqual(expectedResult, result)
532+
533+
storage.htmlConverter.characterToReplaceLastEmptyLine = Character(.zeroWidthSpace)
534+
535+
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
536+
expectedResult = String("Hello")+String(.lineSeparator)
537+
result = String(storage.string)
538+
539+
XCTAssertEqual(expectedResult, result)
540+
}
497541
}

0 commit comments

Comments
 (0)