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

Added .none the StringEncodingStrategy #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Sources/XMLCoding/Encoder/XMLEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ open class XMLEncoder {

/// Encoded the `String` as a CData-encoded string.
case cdata

/// Do NOT encode the `String`.
case none
}

/// The strategy to use for encoding `Data` values.
Expand Down Expand Up @@ -164,6 +167,7 @@ open class XMLEncoder {
return result
}
}


/// The strategy to use for encoding attributes on a node.
public enum AttributeEncodingStrategy {
Expand Down
10 changes: 5 additions & 5 deletions Sources/XMLCoding/XMLStackParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal class _XMLElement {
throw EncodingError.invalidValue(object, EncodingError.Context(codingPath: [], debugDescription: "Top-level encoded as non-root XML fragment."))
}

return element.toXMLString(with: header, withCDATA: options.stringEncodingStrategy != .deferredToString).data(using: .utf8, allowLossyConversion: true)!
return element.toXMLString(with: header, withCDATA: options.stringEncodingStrategy == .cdata, ignoreEscaping: options.stringEncodingStrategy == .none).data(using: .utf8, allowLossyConversion: true)!
}

fileprivate static func createElement(parentElement: _XMLElement?, key: String, object: [String: Container]) {
Expand Down Expand Up @@ -196,13 +196,13 @@ internal class _XMLElement {

func toXMLString(with header: XMLHeader? = nil, withCDATA cdata: Bool, ignoreEscaping: Bool = false) -> String {
if let header = header, let headerXML = header.toXML() {
return headerXML + _toXMLString(withCDATA: cdata)
return headerXML + _toXMLString(withCDATA: cdata, ignoreEscaping: ignoreEscaping)
} else {
return _toXMLString(withCDATA: cdata)
return _toXMLString(withCDATA: cdata, ignoreEscaping: ignoreEscaping)
}
}

fileprivate func _toXMLString(indented level: Int = 0, withCDATA cdata: Bool, ignoreEscaping: Bool = false) -> String {
fileprivate func _toXMLString(indented level: Int = 0, withCDATA cdata: Bool, ignoreEscaping: Bool) -> String {
var string = String(repeating: " ", count: level * 4)
string += "<\(key)"

Expand All @@ -223,7 +223,7 @@ internal class _XMLElement {

for childElement in children {
for child in childElement.value {
string += child._toXMLString(indented: level + 1, withCDATA: cdata)
string += child._toXMLString(indented: level + 1, withCDATA: cdata, ignoreEscaping: ignoreEscaping)
string += "\n"
}
}
Expand Down