|
1 | 1 | import Foundation
|
2 | 2 | import pugixml
|
3 | 3 |
|
| 4 | +internal extension Node { |
| 5 | + func value(forAttribute name: String) -> String? { |
| 6 | + let attribute = node.attribute(name) |
| 7 | + return attribute.empty() ? nil : String(cString: attribute.value()) |
| 8 | + } |
| 9 | + |
| 10 | + func setValue(_ value: String?, forAttribute name: String) { |
| 11 | + var node = node |
| 12 | + var attr = node.attribute(name) |
| 13 | + if attr.empty() { |
| 14 | + if value != nil { |
| 15 | + attr = node.append_attribute(name) |
| 16 | + attr.set_value(value) |
| 17 | + } |
| 18 | + } else { |
| 19 | + if let value { |
| 20 | + attr.set_value(value) |
| 21 | + } else { |
| 22 | + node.remove_attribute(attr) |
| 23 | + } |
| 24 | + } |
| 25 | + if name.hasPrefix("xmlns") { |
| 26 | + document.declaredNamespacesDidChange(for: self) |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
4 | 31 | public extension Node {
|
5 | 32 | /// A Boolean value indicating whether this node type supports attributes.
|
6 | 33 | ///
|
@@ -49,42 +76,92 @@ public extension Node {
|
49 | 76 | }
|
50 | 77 | }
|
51 | 78 | }
|
| 79 | +} |
52 | 80 |
|
53 |
| - /// Accesses the value of a specific attribute by its qualified name. |
| 81 | +public extension Node { |
| 82 | + /// The attributes of this element in document order, represented as an array of expanded names and their corresponding values. |
54 | 83 | ///
|
55 |
| - /// - Parameter name: The qualified name of the attribute to access. |
56 |
| - /// - Returns: The value of the attribute if it exists, or `nil` if the attribute is not present. |
| 84 | + /// - Returns: An array of tuples where each tuple contains an `ExpandedName` and the corresponding attribute value. |
57 | 85 | ///
|
58 |
| - /// - Example: |
59 |
| - /// ```swift |
60 |
| - /// let element = ... |
61 |
| - /// element["id"] = "12345" // Sets the "id" attribute |
62 |
| - /// let idValue = element["id"] // Retrieves the value of the "id" attribute |
63 |
| - /// element["class"] = nil // Removes the "class" attribute |
64 |
| - /// ``` |
65 |
| - subscript(attribute name: String) -> String? { |
| 86 | + /// - Note: Setting this property replaces all existing attributes with the new ones, preserving the specified order. |
| 87 | + var orderedAttributes: [(name: ExpandedName, value: String)] { |
66 | 88 | get {
|
67 |
| - let attribute = node.attribute(name) |
68 |
| - return attribute.empty() ? nil : String(cString: attribute.value()) |
| 89 | + return node.attributes.map {( |
| 90 | + ExpandedName(effectiveQualifiedAttributeName: String(cString: $0.name()), in: self), |
| 91 | + String(cString: $0.value()) |
| 92 | + )} |
69 | 93 | }
|
70 | 94 | nonmutating set {
|
71 | 95 | var node = node
|
72 |
| - var attr = node.attribute(name) |
73 |
| - if attr.empty() { |
74 |
| - if newValue != nil { |
75 |
| - attr = node.append_attribute(name) |
76 |
| - attr.set_value(newValue) |
77 |
| - } |
78 |
| - } else { |
79 |
| - if let newValue { |
80 |
| - attr.set_value(newValue) |
81 |
| - } else { |
82 |
| - node.remove_attribute(attr) |
83 |
| - } |
| 96 | + node.remove_attributes() |
| 97 | + for (name, value) in newValue { |
| 98 | + let qName = name.requestQualifiedAttributeName(for: self) |
| 99 | + var attr = node.append_attribute(qName) |
| 100 | + attr.set_value(value) |
84 | 101 | }
|
85 |
| - if name.hasPrefix("xmlns") { |
86 |
| - document.declaredNamespacesDidChange(for: self) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// The attributes of this element as a dictionary, where the keys are `ExpandedName` objects and the values are their corresponding attribute values. |
| 106 | + /// |
| 107 | + /// - Returns: A dictionary of attributes keyed by their expanded names. |
| 108 | + /// |
| 109 | + /// - Note: Setting this property replaces all existing attributes with the new ones. The order of attributes is determined by the dictionary's order. |
| 110 | + var namespacedAttributes: [ExpandedName: String] { |
| 111 | + get { Dictionary(orderedAttributes) { $1 } } |
| 112 | + nonmutating set { orderedAttributes = newValue.map { ($0, $1) } } |
| 113 | + } |
| 114 | + |
| 115 | + /// Accesses the value of an attribute by its name. |
| 116 | + /// |
| 117 | + /// - Parameter name: The name of the attribute to access; either a `String` or an `ExpandedName`. |
| 118 | + /// - Returns: The value of the attribute if it exists, or `nil` if no such attribute is found. |
| 119 | + /// |
| 120 | + /// - Note: When setting an attribute with an expanded name, its namespace is resolved based on the current scope. If `nil` is assigned, the attribute is removed. |
| 121 | + /// |
| 122 | + /// - Example: |
| 123 | + /// ```swift |
| 124 | + /// let name = ExpandedName(namespaceName: "http://example.com", localName: "attribute") |
| 125 | + /// element[attribute: name] = "value" // Adds or updates the attribute |
| 126 | + /// let value = element[attribute: name] // Retrieves the value |
| 127 | + /// element[attribute: name] = nil // Removes the attribute |
| 128 | + /// ``` |
| 129 | + subscript(attribute name: AttributeName) -> String? { |
| 130 | + get { |
| 131 | + if let qName = name.qualifiedName(in: self) { |
| 132 | + return value(forAttribute: qName) |
| 133 | + } else { |
| 134 | + return nil |
87 | 135 | }
|
88 | 136 | }
|
| 137 | + nonmutating set { |
| 138 | + let qName = name.requestQualifiedName(in: self) |
| 139 | + setValue(newValue, forAttribute: qName) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /// Accesses the value of an attribute by its local name and optional namespace URI. |
| 144 | + /// |
| 145 | + /// - Parameters: |
| 146 | + /// - localName: The local name of the attribute to access. |
| 147 | + /// - namespaceURI: The namespace name of the attribute, or `nil` if the attribute is not namespaced. |
| 148 | + /// - Returns: The value of the attribute if it exists, or `nil` if no such attribute is found. |
| 149 | + /// |
| 150 | + /// - Note: This subscript allows convenient access to attributes by specifying both the local name and namespace. |
| 151 | + /// |
| 152 | + /// - Example: |
| 153 | + /// ```swift |
| 154 | + /// element[attribute: "id", namespaceName: nil] = "123" // Sets an attribute with no namespace |
| 155 | + /// element[attribute: "name", namespaceName: "http://example.com"] = "example" // Sets a namespaced attribute |
| 156 | + /// let value = element[attribute: "name", namespaceName: "http://example.com"] // Retrieves the value |
| 157 | + /// element[attribute: "name", namespaceName: "http://example.com"] = nil // Removes the attribute |
| 158 | + /// ``` |
| 159 | + subscript(attribute localName: String, namespaceName namespaceURI: String?) -> String? { |
| 160 | + get { |
| 161 | + self[attribute: ExpandedName(namespaceName: namespaceURI, localName: localName)] |
| 162 | + } |
| 163 | + nonmutating set { |
| 164 | + self[attribute: ExpandedName(namespaceName: namespaceURI, localName: localName)] = newValue |
| 165 | + } |
89 | 166 | }
|
90 | 167 | }
|
0 commit comments