Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode element with empty key, empty element, and attributes #223

Merged
merged 6 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,16 @@ struct XMLCoderElement: Equatable {
_ string: inout String,
_ charactersEscapedInAttributes: [(String, String)]
) {
let attributesBelongingToContainer = self.elements.filter {
$0.key.isEmpty && !$0.attributes.isEmpty
}.flatMap {
$0.attributes
}
let allAttributes = self.attributes + attributesBelongingToContainer

let attributes = formatting.contains(.sortedKeys) ?
self.attributes.sorted(by: { $0.key < $1.key }) :
self.attributes
allAttributes.sorted(by: { $0.key < $1.key }) :
allAttributes
formatXMLAttributes(
from: attributes,
into: &string,
Expand Down Expand Up @@ -266,10 +273,9 @@ struct XMLCoderElement: Equatable {

if !key.isEmpty {
string += "<\(key)"
formatXMLAttributes(formatting, &string, escapedCharacters.attributes)
}

formatXMLAttributes(formatting, &string, escapedCharacters.attributes)

if !elements.isEmpty {
let prettyPrintElements = prettyPrinted && !containsTextNodes
if !key.isEmpty {
Expand All @@ -282,7 +288,9 @@ struct XMLCoderElement: Equatable {
string += "</\(key)>"
}
} else {
string += " />"
if !key.isEmpty {
string += " />"
}
}

return string
Expand Down
66 changes: 66 additions & 0 deletions Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,70 @@ class XMLElementTests: XCTestCase {
XCTAssert(whitespaceElement2.isWhitespaceWithNoElements())
XCTAssert(whitespaceElement3.isWhitespaceWithNoElements())
}

func testNestedElementWith_Namespace_Attribute() {
typealias Attribute = XMLCoderElement.Attribute
typealias Element = XMLCoderElement
let nested = Element(key: "Nested",
elements: [
Element(key: "",
elements: [],
attributes: [
Attribute(key: "xsi:someName", value: "nestedAttrValue")
]
)],
attributes: [
Attribute(key: "xmlns:xsi", value: "https://example.com")
])
let inputNamespace = Attribute(key: "xmlns", value: "https://example.com")
let input = Element(key: "Input",
elements: [nested],
attributes: [inputNamespace])

let result = input.toXMLString(
escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements,
attributes: XMLEncoder().charactersEscapedInAttributes),
formatting: [],
indentation: .spaces(4)
)

MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
XCTAssertEqual(result, """
<Input xmlns="https://example.com"><Nested xmlns:xsi="https://example.com" xsi:someName="nestedAttrValue"></Nested></Input>
""")
}

func testNestedElementWith_Namespace_Attribute_Element() {
typealias Attribute = XMLCoderElement.Attribute
typealias Element = XMLCoderElement
let nested = Element(key: "Nested",
elements: [
Element(key: "",
elements: [
Element(key: "nonAttrField",
elements: [Element(key: "", stringValue: "hello")],
attributes: [])
],
attributes: [
Attribute(key: "xsi:someName", value: "nestedAttrValue")
]
)],
attributes: [
Attribute(key: "xmlns:xsi", value: "https://example.com")
])
let inputNamespace = Attribute(key: "xmlns", value: "https://example.com")
let input = Element(key: "Input",
elements: [nested],
attributes: [inputNamespace])

let result = input.toXMLString(
escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements,
attributes: XMLEncoder().charactersEscapedInAttributes),
formatting: [],
indentation: .spaces(4)
)

XCTAssertEqual(result, """
<Input xmlns="https://example.com"><Nested xmlns:xsi="https://example.com" xsi:someName="nestedAttrValue"><nonAttrField>hello</nonAttrField></Nested></Input>
""")
}
}