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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
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
64 changes: 64 additions & 0 deletions Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
Expand Up @@ -64,4 +64,68 @@ 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 simpleScalarPropertiesInputNamespace = Attribute(key: "xmlns", value: "https://example.com")
let simpleScalarPropertiesInput = Element(key: "SimpleScalarPropertiesInput",
elements: [nested],
attributes: [simpleScalarPropertiesInputNamespace])

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

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

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 simpleScalarPropertiesInputNamespace = Attribute(key: "xmlns", value: "https://example.com")
let simpleScalarPropertiesInput = Element(key: "SimpleScalarPropertiesInput",
elements: [nested],
attributes: [simpleScalarPropertiesInputNamespace])

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

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