Skip to content

Commit 6ae7b83

Browse files
authored
Merge pull request #278 from drmohundro/feat/add-parent-to-xmlelement
feat: add parent to XMLElement
2 parents 615f044 + 11f89c3 commit 6ae7b83

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v8.1.0 (September 27, 2024)
2+
3+
- Added the `parent` property to `XMLElement` (via [#278](https://github.com/drmohundro/SWXMLHash/pull/278) which closes [#277](https://github.com/drmohundro/SWXMLHash/issues/277))
4+
15
## v8.0.0 (September 17, 2024)
26

37
- Added Swift 6.0 support (via [#283](https://github.com/drmohundro/SWXMLHash/pull/283))

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM swiftlang/swift:nightly-6.0-focal
1+
FROM swift:6.0
22

33
ENV APP_HOME ./app
44
RUN mkdir $APP_HOME

Source/XMLElement.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class XMLElement: XMLContent {
3030
/// The name of the element
3131
public let name: String
3232

33+
public private(set) var parent: XMLElement?
34+
3335
/// Whether the element is case insensitive or not
3436
public var caseInsensitive: Bool {
3537
options.caseInsensitive
@@ -116,6 +118,7 @@ public class XMLElement: XMLContent {
116118

117119
func addElement(_ name: String, withAttributes attributes: [String: String], caseInsensitive: Bool) -> XMLElement {
118120
let element = XMLElement(name: name, index: count, options: options)
121+
element.parent = self
119122
count += 1
120123

121124
children.append(element)

Tests/SWXMLHashTests/XMLParsingTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ struct XMLParsingTests {
8888
#expect(xml!["root"]["catalog"]["book"][1]["author"].element?.text == "Ralls, Kim")
8989
}
9090

91+
@Test
92+
func shouldBeAbleToGetParent() {
93+
#expect(xml!["root"]["catalog"]["book"][1]["author"].element?.parent != nil)
94+
#expect(xml!["root"]["catalog"]["book"][1]["author"].element?.parent?.name == "book")
95+
#expect(xml!["root"]["catalog"]["book"][1]["author"].element?.parent?.attribute(by: "id")?.text == "bk102")
96+
}
97+
9198
@Test
9299
func shouldBeAbleToParseElementGroupsByIndex() {
93100
// swiftlint:disable:next force_try

0 commit comments

Comments
 (0)