Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Aztec/Classes/NSAttributedString/Conversions/HTMLConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public class HTMLConverter {
}

// MARK: - Converters: HTML -> AttributedString


/// If a value is set the character set will be used to replace any last empty line created by the converter.
///
open var characterToReplaceLastEmptyLine: Character?

let htmlToTree = HTMLParser()

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

let defaultAttributes = defaultAttributes ?? [:]
let attributedString = treeToAttributedString.serialize(rootNode, defaultAttributes: defaultAttributes)
var attributedString = treeToAttributedString.serialize(rootNode, defaultAttributes: defaultAttributes)

if let characterToUse = characterToReplaceLastEmptyLine {
attributedString = replaceLastEmptyLine(in: attributedString, with: characterToUse)
}

return attributedString
}

func replaceLastEmptyLine(in attributedString: NSAttributedString, with replacement: Character) -> NSAttributedString {
var result = attributedString
let string = attributedString.string
if !string.isEmpty, string.isEmptyLineAtEndOfFile(at: string.count), string.hasSuffix(String(.paragraphSeparator)), let location = string.location(before: attributedString.length) {
let mutableString = NSMutableAttributedString(attributedString: attributedString)
let attributes = mutableString.attributes(at: location, effectiveRange: nil)
mutableString.replaceCharacters(in: NSRange(location: location, length: attributedString.length-location), with: NSAttributedString(string: String(replacement), attributes: attributes))
result = mutableString
}
return result
}


/// Check if the given html string is supported to be parsed into Attributed Strings.
///
Expand Down
2 changes: 1 addition & 1 deletion Aztec/Classes/TextKit/TextStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ open class TextStorage: NSTextStorage {

// MARK: - HTML Conversion

let htmlConverter = HTMLConverter()
public let htmlConverter = HTMLConverter()

// MARK: - PluginManager

Expand Down
2 changes: 1 addition & 1 deletion Aztec/Classes/TextKit/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ open class TextView: UITextView {

// MARK: - TextKit Aztec Subclasses

var storage: TextStorage {
public var storage: TextStorage {
return textStorage as! TextStorage
}

Expand Down
44 changes: 44 additions & 0 deletions AztecTests/TextKit/TextStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,48 @@ class TextStorageTests: XCTestCase {

XCTAssertEqual(expectedResult, result)
}

func testEmptyListOutput() {
let initialHTML = "<ul><li></li></ul>"

// Setup
let defaultAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 14),
.paragraphStyle: ParagraphStyle.default]

storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
var expectedResult = String(.paragraphSeparator)
var result = String(storage.string)

XCTAssertEqual(expectedResult, result)

storage.htmlConverter.characterToReplaceLastEmptyLine = Character(.zeroWidthSpace)

storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
expectedResult = String(.zeroWidthSpace)
result = String(storage.string)

XCTAssertEqual(expectedResult, result)
}

func testCiteOutput() {
let initialHTML = "<cite>Hello<br></cite>"

// Setup
let defaultAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 14),
.paragraphStyle: ParagraphStyle.default]

storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
var expectedResult = String("Hello")+String(.lineSeparator)
var result = String(storage.string)

XCTAssertEqual(expectedResult, result)

storage.htmlConverter.characterToReplaceLastEmptyLine = Character(.zeroWidthSpace)

storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
expectedResult = String("Hello")+String(.lineSeparator)
result = String(storage.string)

XCTAssertEqual(expectedResult, result)
}
}