diff --git a/Aztec/Classes/Converters/HTMLNodeToNSAttributedString.swift b/Aztec/Classes/Converters/HTMLNodeToNSAttributedString.swift index fbb96217a..8a27bd514 100644 --- a/Aztec/Classes/Converters/HTMLNodeToNSAttributedString.swift +++ b/Aztec/Classes/Converters/HTMLNodeToNSAttributedString.swift @@ -152,18 +152,17 @@ class HTMLNodeToNSAttributedString: SafeConverter { fileprivate func string(for element: ElementNode, inheriting attributes: [String:Any]) -> NSAttributedString { let childAttributes = self.attributes(for: element, inheriting: attributes) - + let content = NSMutableAttributedString() + if let nodeType = element.standardName, let implicitRepresentation = nodeType.implicitRepresentation(withAttributes: childAttributes) { - - return implicitRepresentation - } - let content = NSMutableAttributedString() - - for child in element.children { - let childContent = convert(child, inheriting: childAttributes) - content.append(childContent) + content.append(implicitRepresentation) + } else { + for child in element.children { + let childContent = convert(child, inheriting: childAttributes) + content.append(childContent) + } } guard !element.needsClosingParagraphSeparator() else { diff --git a/Aztec/Classes/Converters/NSAttributedStringToNodes.swift b/Aztec/Classes/Converters/NSAttributedStringToNodes.swift index 9de9cd869..0333fae04 100644 --- a/Aztec/Classes/Converters/NSAttributedStringToNodes.swift +++ b/Aztec/Classes/Converters/NSAttributedStringToNodes.swift @@ -27,21 +27,23 @@ class NSAttributedStringToNodes: Converter { /// func convert(_ attrString: NSAttributedString) -> RootNode { var nodes = [Node]() - var previous = [Node]() + var previous: [Node]? attrString.enumerateParagraphRanges(spanning: attrString.rangeOfEntireString) { (paragraphRange, _) in let paragraph = attrString.attributedSubstring(from: paragraphRange) let children = createNodes(fromParagraph: paragraph) - let left = rightmostParagraphStyleElements(from: previous) - let right = leftmostParagraphStyleElements(from: children) + if let previous = previous { + let left = rightmostParagraphStyleElements(from: previous) + let right = leftmostParagraphStyleElements(from: children) - guard !merge(left: left, right: right) else { - return - } + guard !merge(left: left, right: right) else { + return + } - if !previous.isEmpty && left.count == 0 && right.count == 0 { - nodes += [ ElementNode(type: .br) ] + if left.count == 0 && right.count == 0 { + nodes += [ ElementNode(type: .br) ] + } } nodes += children diff --git a/Aztec/Classes/Libxml2/DOM/Data/ElementNode.swift b/Aztec/Classes/Libxml2/DOM/Data/ElementNode.swift index 53b1cd6c1..eb6afa67d 100644 --- a/Aztec/Classes/Libxml2/DOM/Data/ElementNode.swift +++ b/Aztec/Classes/Libxml2/DOM/Data/ElementNode.swift @@ -81,7 +81,7 @@ extension Libxml2 { return true } - return isLastInAncestorEndingInBlockLevelSeparation() + return !isLastInTree() && isLastInAncestorEndingInBlockLevelSeparation() } // MARK: - Node Queries diff --git a/Aztec/Classes/Libxml2/DOM/Data/TextNode.swift b/Aztec/Classes/Libxml2/DOM/Data/TextNode.swift index 7d7141580..64ac090b3 100644 --- a/Aztec/Classes/Libxml2/DOM/Data/TextNode.swift +++ b/Aztec/Classes/Libxml2/DOM/Data/TextNode.swift @@ -42,7 +42,7 @@ extension Libxml2 { return true } - return isLastInAncestorEndingInBlockLevelSeparation() + return !isLastInTree() && isLastInAncestorEndingInBlockLevelSeparation() } // MARK: - LeafNode diff --git a/AztecTests/Converters/NSAttributedStringToNodesTests.swift b/AztecTests/Converters/NSAttributedStringToNodesTests.swift index 376beeda3..370a174a5 100644 --- a/AztecTests/Converters/NSAttributedStringToNodesTests.swift +++ b/AztecTests/Converters/NSAttributedStringToNodesTests.swift @@ -633,6 +633,49 @@ class NSAttributedStringToNodesTests: XCTestCase { let restoredTextNode = restoredSpanNode?.children.first as? TextNode XCTAssert(restoredTextNode?.contents == text) } + + + /// Verifies that Unsupported HTML is preserved, and converted into Nodes. + /// + /// - Input: Ehlo World! + /// + /// - Output: The same!! + /// + func testMultipleNewlinesAreProperlyMappedIntoBreakNodes() { + let text = "\nHello\n\n\nEveryone\n\nYEAH\nSarasa" + let testingString = NSMutableAttributedString(string: text) + + + // Convert + Verify + let node = rootNode(from: testingString) + + let expectedNodes = [ + 0: nil, + 1: "Hello", + 2: nil, + 3: nil, + 4: nil, + 5: "Everyone", + 6: nil, + 7: nil, + 8: "YEAH", + 9: nil, + 10: "Sarasa" + ] + + XCTAssert(node.children.count == expectedNodes.count) + + for (index, text) in expectedNodes { + if let text = text { + let textNode = node.children[index] as? TextNode + XCTAssert(textNode?.contents == text) + } else { + let breakNode = node.children[index] as? ElementNode + XCTAssert(breakNode?.name == "br") + XCTAssert(breakNode?.children.count == 0) + } + } + } }