Skip to content

Commit

Permalink
Removed extra trailing new line from the generated string builder com…
Browse files Browse the repository at this point in the history
…ponents. Fixed blockquote rendering.
  • Loading branch information
stefanceriu committed Mar 30, 2022
1 parent 1bbc38c commit d330950
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
15 changes: 11 additions & 4 deletions ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 25 additions & 4 deletions ElementXTests/AttributedStringBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

Expand All @@ -237,15 +258,15 @@ 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
}
}

XCTFail("Couldn't find blockquote")

XCTAssertEqual(attributedStringBuilder.blockquoteCoalescedComponentsFrom(attributedString)?.count, 3)
}

// MARK: - Private
Expand Down

0 comments on commit d330950

Please sign in to comment.