Skip to content

Commit 62c8860

Browse files
committed
Add variable to control the collapse of whitespaces in HTML
1 parent 56d83e6 commit 62c8860

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

Aztec/Classes/Libxml2/Converters/In/HTMLParser.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ open class HTMLParser {
88
case NoRootNode = "No root node"
99
}
1010

11+
var shouldCollapseSpaces: Bool = true
12+
1113
/// Public initializer
1214
///
1315
public init() { }
@@ -71,7 +73,7 @@ open class HTMLParser {
7173

7274
let rootNodePtr = xmlDocGetRootElement(document)
7375
let nodeConverter = InNodeConverter()
74-
76+
nodeConverter.shouldCollapseSpaces = shouldCollapseSpaces
7577
guard let rootNode = rootNodePtr?.pointee,
7678
let node = nodeConverter.convert(rootNode) as? RootNode else {
7779
return RootNode(children: [TextNode(text: "")])

Aztec/Classes/Libxml2/Converters/In/InNodeConverter.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import libxml2
33

44
class InNodeConverter: SafeConverter {
55

6+
var shouldCollapseSpaces: Bool = true
67
/// Converts a single node (from libxml2) into an HTML.Node.
78
///
89
/// - Parameters:
@@ -105,7 +106,7 @@ class InNodeConverter: SafeConverter {
105106
fileprivate func createTextNode(_ rawNode: xmlNode) -> TextNode {
106107
let text = String(cString: rawNode.content)
107108
let node = TextNode(text: text)
108-
109+
node.shouldCollapseSpaces = shouldCollapseSpaces
109110
return node
110111
}
111112

Aztec/Classes/Libxml2/DOM/Data/TextNode.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class TextNode: Node {
77

88
let contents: String
99

10-
let shouldRemoveExtraSpaces: Bool = false
10+
var shouldCollapseSpaces: Bool = true
1111

1212
// MARK: - CustomReflectable
1313

@@ -95,12 +95,12 @@ extension TextNode {
9595
//
9696
let whitespace = CharacterSet.whitespacesAndNewlines
9797
var whitespaceToKeep = CharacterSet(charactersIn: String(.nonBreakingSpace)+String(.lineSeparator))
98-
if ( !shouldRemoveExtraSpaces ) {
98+
if ( !shouldCollapseSpaces ) {
9999
whitespaceToKeep.insert(charactersIn: String(.space))
100100
}
101101
let whitespaceToRemove = whitespace.subtracting(whitespaceToKeep)
102102
let trimmedText = text.trimmingCharacters(in: whitespaceToRemove)
103-
if ( !shouldRemoveExtraSpaces ) {
103+
if ( !shouldCollapseSpaces ) {
104104
return trimmedText
105105
}
106106
var singleSpaceText = trimmedText

Aztec/Classes/NSAttributedString/Conversions/HTMLConverter.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public class HTMLConverter {
2525
///
2626
open var characterToReplaceLastEmptyLine: Character?
2727

28+
/// If this value is set the converter will behave like a browser and collapse extra white spaces
29+
///
30+
open var shouldCollapseSpaces: Bool = true
31+
2832
let htmlToTree = HTMLParser()
2933

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

5863
pluginManager.process(htmlTree: rootNode)

0 commit comments

Comments
 (0)