From a0e7b40d29375394c7233627e009e153672fb26d Mon Sep 17 00:00:00 2001 From: Daniel Asher Date: Mon, 11 Jul 2016 13:54:50 +0100 Subject: [PATCH] make XMLElement children property public and add "xml parsing - should be able to iterate over mixed content" test --- Source/SWXMLHash.swift | 2 +- Tests/SWXMLHashSpecs.swift | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Source/SWXMLHash.swift b/Source/SWXMLHash.swift index bbee7482..da75fc99 100644 --- a/Source/SWXMLHash.swift +++ b/Source/SWXMLHash.swift @@ -640,7 +640,7 @@ public class XMLElement: XMLContent { .reduce("", combine: { $0 + $1!.text }) } - var children = [XMLContent]() + public var children = [XMLContent]() var count: Int = 0 var index: Int diff --git a/Tests/SWXMLHashSpecs.swift b/Tests/SWXMLHashSpecs.swift index a89610c0..c7c65af8 100644 --- a/Tests/SWXMLHashSpecs.swift +++ b/Tests/SWXMLHashSpecs.swift @@ -91,6 +91,26 @@ class SWXMLHashTests: QuickSpec { expect(xml!["root"]["header"].element?.text).to(equal("header mixed contentmore mixed content")) } + it("should be able to iterate over mixed content") { + let mixedContentXml = "

mixed content iteration support" + let parsed = SWXMLHash.parse(mixedContentXml) + let result = parsed["html"]["body"]["p"].element!.children.reduce("") { acc, child in + switch child { + case let elm as XMLElement: + guard let text = elm.text else { return acc } + return acc + text + case let elm as TextElement: + return acc + elm.text + default: + XCTAssert(false, "Unknown element type") + return acc + } + } + + expect(result).to(equal("mixed content iteration support")) + + } + it("should handle interleaving XML elements") { let interleavedXml = "

one

two

three

four
" let parsed = SWXMLHash.parse(interleavedXml)