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

feat: add parent to XMLElement #278

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM swiftlang/swift:nightly-6.0-focal
FROM swift:6.0

ENV APP_HOME ./app
RUN mkdir $APP_HOME
Expand Down
3 changes: 3 additions & 0 deletions Source/XMLElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions Tests/SWXMLHashTests/XMLParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down