Skip to content

Commit 72d1729

Browse files
Merge pull request #1285 from wordpress-mobile/issue/fix_collapse_of_white_spaces
Fix collapse of white spaces when parsing HTML text.
2 parents 52e48a8 + 8fd4180 commit 72d1729

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ open class HTMLParser {
4545
//
4646
htmlHandleOmittedElem(0)
4747

48+
var parserOptions = HTML_PARSE_RECOVER.rawValue | HTML_PARSE_NODEFDTD.rawValue | HTML_PARSE_NOERROR.rawValue | HTML_PARSE_NOWARNING.rawValue | HTML_PARSE_NOIMPLIED.rawValue
49+
if shouldCollapseSpaces {
50+
parserOptions = parserOptions | HTML_PARSE_NOBLANKS.rawValue
51+
}
4852
let document = htmlCtxtReadMemory(parserContext,
4953
htmlPtr,
5054
Int32(wrappedHTML.lengthOfBytes(using: String.Encoding.utf8)),
5155
"",
5256
"UTF-8",
53-
Int32(HTML_PARSE_RECOVER.rawValue | HTML_PARSE_NODEFDTD.rawValue | HTML_PARSE_NOERROR.rawValue | HTML_PARSE_NOWARNING.rawValue | HTML_PARSE_NOIMPLIED.rawValue | HTML_PARSE_NOBLANKS.rawValue))
57+
Int32(parserOptions))
5458

5559
defer {
5660
xmlFreeDoc(document)

AztecTests/TextKit/TextStorageTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ class TextStorageTests: XCTestCase {
566566
/// Verifies that spaces are not collapsed
567567
///
568568
func testConverterCollapsesSpacesText() {
569-
let initialHTML = "<p> Hello World </p>"
569+
let initialHTML = "<p> Hello <br> <strong>World</strong> </p>"
570570

571571
// Setup
572572
let defaultAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 14),
@@ -575,7 +575,7 @@ class TextStorageTests: XCTestCase {
575575
storage.htmlConverter.shouldCollapseSpaces = false
576576
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
577577

578-
let expectedResult = "<p> Hello World </p>"
578+
let expectedResult = "<p> Hello <br> <strong>World</strong> </p>"
579579
let result = storage.getHTML()
580580
XCTAssertEqual(expectedResult, result)
581581
}

0 commit comments

Comments
 (0)