diff --git a/CHANGELOG.md b/CHANGELOG.md index e91355e..dc84820 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v8.1.0 (September 27, 2024) + +- 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)) + ## v8.0.0 (September 17, 2024) - Added Swift 6.0 support (via [#283](https://github.com/drmohundro/SWXMLHash/pull/283)) diff --git a/Dockerfile b/Dockerfile index 9aeeff9..49e70f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM swiftlang/swift:nightly-6.0-focal +FROM swift:6.0 ENV APP_HOME ./app RUN mkdir $APP_HOME diff --git a/Source/XMLElement.swift b/Source/XMLElement.swift index 202c14d..a1bbda1 100644 --- a/Source/XMLElement.swift +++ b/Source/XMLElement.swift @@ -30,6 +30,8 @@ public class XMLElement: XMLContent { /// The name of the element public let name: String + public private(set) var parent: XMLElement? + /// Whether the element is case insensitive or not public var caseInsensitive: Bool { options.caseInsensitive @@ -116,6 +118,7 @@ public class XMLElement: XMLContent { func addElement(_ name: String, withAttributes attributes: [String: String], caseInsensitive: Bool) -> XMLElement { let element = XMLElement(name: name, index: count, options: options) + element.parent = self count += 1 children.append(element) diff --git a/Tests/SWXMLHashTests/XMLParsingTests.swift b/Tests/SWXMLHashTests/XMLParsingTests.swift index 5d9f512..58d65f3 100644 --- a/Tests/SWXMLHashTests/XMLParsingTests.swift +++ b/Tests/SWXMLHashTests/XMLParsingTests.swift @@ -88,6 +88,13 @@ struct XMLParsingTests { #expect(xml!["root"]["catalog"]["book"][1]["author"].element?.text == "Ralls, Kim") } + @Test + func shouldBeAbleToGetParent() { + #expect(xml!["root"]["catalog"]["book"][1]["author"].element?.parent != nil) + #expect(xml!["root"]["catalog"]["book"][1]["author"].element?.parent?.name == "book") + #expect(xml!["root"]["catalog"]["book"][1]["author"].element?.parent?.attribute(by: "id")?.text == "bk102") + } + @Test func shouldBeAbleToParseElementGroupsByIndex() { // swiftlint:disable:next force_try