Removed extra trailing new line from the generated string builder components. Fixed blockquote rendering.

This commit is contained in:
Stefan Ceriu
2022-03-30 16:34:43 +03:00
parent 1bbc38c03e
commit d330950ed3
3 changed files with 37 additions and 9 deletions
@@ -84,9 +84,9 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
removeDefaultForegroundColor(mutableAttributedString)
addLinks(mutableAttributedString)
removeLinkColors(mutableAttributedString)
removeDTCoreTextArtifacts(mutableAttributedString)
replaceMarkedBlockquotes(mutableAttributedString)
replaceMarkedCodeBlocks(mutableAttributedString)
removeDTCoreTextArtifacts(mutableAttributedString)
return try? AttributedString(mutableAttributedString, including: \.elementX)
}
@@ -95,10 +95,17 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
guard let attributedString = attributedString else {
return nil
}
return attributedString.runs[\.blockquote].map { (value, range) in
AttributedStringBuilderComponent(attributedString: AttributedString(attributedString[range]),
isBlockquote: value != nil)
var attributedString = AttributedString(attributedString[range])
// Remove trailing new lines if any
if let lastCharacter = attributedString.characters.last,
lastCharacter.isNewline {
attributedString = AttributedString(attributedString.characters.dropLast())
}
return AttributedStringBuilderComponent(attributedString: attributedString, isBlockquote: value != nil)
}
}
@@ -13,7 +13,7 @@ struct FormattedBodyText: View {
let attributedComponents: [AttributedStringBuilderComponent]
var body: some View {
VStack(alignment: .leading, spacing: 0.0) {
VStack(alignment: .leading, spacing: 8.0) {
ForEach(attributedComponents, id: \.self) { component in
if component.isBlockquote {
HStack(spacing: 4.0) {
@@ -223,10 +223,31 @@ class AttributedStringBuilderTests: XCTestCase {
XCTAssertTrue(foundLink)
}
func testBlockquotes() {
func testSingleBlockquote() {
let htmlString = "<blockquote>Blockquote</blockquote>"
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string")
return
}
XCTAssertEqual(attributedString.runs.count, 1)
XCTAssertEqual(attributedStringBuilder.blockquoteCoalescedComponentsFrom(attributedString)?.count, 1)
for run in attributedString.runs {
if run.elementX.blockquote != nil {
return
}
}
XCTFail("Couldn't find blockquote")
}
func testBlockquoteWithinText() {
let htmlString = """
The text before the blockquote
<blockquote cite="http://www.worldwildlife.org/who/index.html"> For 50 years, WWF has been protecting the future of nature. The world's leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.</blockquote>
<blockquote> For 50 years, WWF has been protecting the future of nature. The world's leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.</blockquote>
The text after the blockquote
"""
@@ -237,6 +258,8 @@ The text after the blockquote
XCTAssertEqual(attributedString.runs.count, 3)
XCTAssertEqual(attributedStringBuilder.blockquoteCoalescedComponentsFrom(attributedString)?.count, 3)
for run in attributedString.runs {
if run.elementX.blockquote != nil {
return
@@ -244,8 +267,6 @@ The text after the blockquote
}
XCTFail("Couldn't find blockquote")
XCTAssertEqual(attributedStringBuilder.blockquoteCoalescedComponentsFrom(attributedString)?.count, 3)
}
// MARK: - Private