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
Changes from 1 commit
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 actuallyAttributes = self.elements.filter {
$0.key.isEmpty && $0.elements.isEmpty && !$0.attributes.isEmpty
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is key a critical check here? Maybe your original case fails because of the key value intrinsic feature described here? https://github.com/MaxDesiatov/XMLCoder#coding-key-value-intrinsic

}.flatMap {
$0.attributes
}
let allAttributes = self.attributes + actuallyAttributes

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