Skip to content

Commit

Permalink
Merge pull request #1277 from wordpress-mobile/issue/add_option_to_no…
Browse files Browse the repository at this point in the history
…t_clean_whitespaces

Add option to disable collapsing of whitespaces.
  • Loading branch information
SergioEstevao authored Apr 20, 2020
2 parents 79ec095 + 028b48f commit c6a8103
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Aztec/Classes/Libxml2/Converters/In/HTMLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ open class HTMLParser {
case NoRootNode = "No root node"
}

var shouldCollapseSpaces: Bool = true

/// Public initializer
///
public init() { }
Expand Down Expand Up @@ -71,7 +73,7 @@ open class HTMLParser {

let rootNodePtr = xmlDocGetRootElement(document)
let nodeConverter = InNodeConverter()

nodeConverter.shouldCollapseSpaces = shouldCollapseSpaces
guard let rootNode = rootNodePtr?.pointee,
let node = nodeConverter.convert(rootNode) as? RootNode else {
return RootNode(children: [TextNode(text: "")])
Expand Down
12 changes: 8 additions & 4 deletions Aztec/Classes/Libxml2/Converters/In/InNodeConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import Foundation
import libxml2

class InNodeConverter: SafeConverter {


init(shouldCollapseSpaces: Bool = true) {
self.shouldCollapseSpaces = shouldCollapseSpaces
}
var shouldCollapseSpaces: Bool = true
/// Converts a single node (from libxml2) into an HTML.Node.
///
/// - Parameters:
Expand Down Expand Up @@ -53,7 +57,7 @@ class InNodeConverter: SafeConverter {
var children = [Node]()

if rawNode.children != nil {
let nodesConverter = InNodesConverter()
let nodesConverter = InNodesConverter(shouldCollapseSpaces: shouldCollapseSpaces)
children.append(contentsOf: nodesConverter.convert(rawNode.children))
}

Expand All @@ -80,7 +84,7 @@ class InNodeConverter: SafeConverter {
var children = [Node]()

if rawNode.children != nil {
let nodesConverter = InNodesConverter()
let nodesConverter = InNodesConverter(shouldCollapseSpaces: shouldCollapseSpaces)
children.append(contentsOf: nodesConverter.convert(rawNode.children))
}

Expand All @@ -105,7 +109,7 @@ class InNodeConverter: SafeConverter {
fileprivate func createTextNode(_ rawNode: xmlNode) -> TextNode {
let text = String(cString: rawNode.content)
let node = TextNode(text: text)

node.shouldCollapseSpaces = shouldCollapseSpaces
return node
}

Expand Down
9 changes: 6 additions & 3 deletions Aztec/Classes/Libxml2/Converters/In/InNodesConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import libxml2
/// Converts a C linked list of xmlNode to [HTML.Node].
///
class InNodesConverter: SafeCLinkedListToArrayConverter<InNodeConverter> {

required init() {
super.init(elementConverter: InNodeConverter(), next: { return $0.next })

let shouldCollapseSpaces: Bool

required init(shouldCollapseSpaces: Bool = true) {
self.shouldCollapseSpaces = shouldCollapseSpaces
super.init(elementConverter: InNodeConverter(shouldCollapseSpaces: shouldCollapseSpaces), next: { return $0.next })
}
}
11 changes: 9 additions & 2 deletions Aztec/Classes/Libxml2/DOM/Data/TextNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class TextNode: Node {

let contents: String

var shouldCollapseSpaces: Bool = true

// MARK: - CustomReflectable

override public var customMirror: Mirror {
Expand Down Expand Up @@ -92,10 +94,15 @@ extension TextNode {
// U+000A, which is non-breaking space. We need to maintain it.
//
let whitespace = CharacterSet.whitespacesAndNewlines
let whitespaceToKeep = CharacterSet(charactersIn: String(.nonBreakingSpace)+String(.lineSeparator))
var whitespaceToKeep = CharacterSet(charactersIn: String(.nonBreakingSpace)+String(.lineSeparator))
if ( !shouldCollapseSpaces ) {
whitespaceToKeep.insert(charactersIn: String(.space))
}
let whitespaceToRemove = whitespace.subtracting(whitespaceToKeep)

let trimmedText = text.trimmingCharacters(in: whitespaceToRemove)
if ( !shouldCollapseSpaces ) {
return trimmedText
}
var singleSpaceText = trimmedText
let doubleSpace = " "
let singleSpace = " "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class HTMLConverter {
///
open var characterToReplaceLastEmptyLine: Character?

/// If this value is set the converter will behave like a browser and collapse extra white spaces
///
open var shouldCollapseSpaces: Bool = true

let htmlToTree = HTMLParser()

private(set) lazy var treeToAttributedString: AttributedStringSerializer = {
Expand Down Expand Up @@ -53,6 +57,7 @@ public class HTMLConverter {
///
func attributedString(from html: String, defaultAttributes: [NSAttributedString.Key: Any]? = [:]) -> NSAttributedString {
let processedHTML = pluginManager.process(html: html)
htmlToTree.shouldCollapseSpaces = shouldCollapseSpaces
let rootNode = htmlToTree.parse(processedHTML)

pluginManager.process(htmlTree: rootNode)
Expand Down
17 changes: 17 additions & 0 deletions AztecTests/TextKit/TextStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,21 @@ class TextStorageTests: XCTestCase {
XCTAssert(font.pointSize == 14)
}
}

/// Verifies that spaces are not collapsed
///
func testConverterCollapsesSpacesText() {
let initialHTML = "<p> Hello World </p>"

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

storage.htmlConverter.shouldCollapseSpaces = false
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)

let expectedResult = "<p> Hello World </p>"
let result = storage.getHTML()
XCTAssertEqual(expectedResult, result)
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.18.0
-------
* Added an option to not colapse whitespaces when saving the HTML.

1.17.1
-----
* Fix drawing of underlines when they include the last character of content.
Expand Down
2 changes: 1 addition & 1 deletion WordPress-Aztec-iOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'WordPress-Aztec-iOS'
s.version = '1.17.1'
s.version = '1.18.0'
s.summary = 'The native HTML Editor.'

# This description is used to generate tags and improve search results.
Expand Down
2 changes: 1 addition & 1 deletion WordPress-Editor-iOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'WordPress-Editor-iOS'
s.version = '1.17.1'
s.version = '1.18.0'
s.summary = 'The WordPress HTML Editor.'

# This description is used to generate tags and improve search results.
Expand Down

0 comments on commit c6a8103

Please sign in to comment.