Skip to content

Commit 97df6e2

Browse files
Correctly set indexInParent in Markup.child(at:) (#45)
This fixes a recent regression where `Markup.child(at:)` began returning markup with incorrect metadata resulting in out-of-bounds errors. The `indexInParent` value of a child is unrelated to the parent's `indexInParent`. This was missed initially because tests were only checking for children of the first item where `indexInParent` would be 0. A new test has been added that asserts `Markup.child(at:)` returns correct values for children of nested items as well.
1 parent 7a7c59d commit 97df6e2

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Sources/Markdown/Base/Markup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ extension Markup {
204204
childId: firstChildID.childId + siblingSubtreeCount
205205
)
206206

207-
childMetadata = MarkupMetadata(id: childID, indexInParent: indexInParent + position)
207+
childMetadata = MarkupMetadata(id: childID, indexInParent: position)
208208
}
209209

210210
let rawChild = raw.markup.child(at: position)

Tests/MarkdownTests/Base/MarkupTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,25 @@ final class MarkupTests: XCTestCase {
348348
}
349349
}
350350

351+
func testNestedChildAtPositionHasCorrectMetadata() throws {
352+
let source = "This is some **bold** and *italic* text and a [multi-formatted *link* **to** `github`](github.com)."
353+
354+
let document = Document(parsing: source)
355+
let link = try XCTUnwrap(document.child(at: 0)?.child(at: 5) as? Link)
356+
XCTAssertEqual(link.indexInParent, 5)
357+
358+
for (index, sequencedChild) in link.children.enumerated() {
359+
let indexedChild = try XCTUnwrap(link.child(at: index))
360+
361+
let indexedChildMetadata = indexedChild.raw.metadata
362+
let sequencedChildMetadata = sequencedChild.raw.metadata
363+
364+
XCTAssertEqual(indexedChildMetadata.id, sequencedChildMetadata.id)
365+
XCTAssertEqual(indexedChildMetadata.indexInParent, sequencedChildMetadata.indexInParent)
366+
XCTAssertEqual(indexedChildMetadata.indexInParent, index)
367+
}
368+
}
369+
351370
func testChildAtPositionHasCorrectDataID() throws {
352371
let source = "This is a [*link*](github.com). And some **bold** and *italic* text."
353372

@@ -361,6 +380,19 @@ final class MarkupTests: XCTestCase {
361380
}
362381
}
363382

383+
func testNestedChildAtPositionHasCorrectDataID() throws {
384+
let source = "This is some **bold** and *italic* text and a [multi-formatted *link* **to** `github`](github.com)."
385+
386+
let document = Document(parsing: source)
387+
let link = try XCTUnwrap(document.child(at: 0)?.child(at: 5) as? Link)
388+
389+
for (index, sequencedChild) in link.children.enumerated() {
390+
let indexedChild = try XCTUnwrap(link.child(at: index))
391+
392+
XCTAssertEqual(indexedChild._data.id, sequencedChild._data.id)
393+
}
394+
}
395+
364396
func assertEqualType<FirstType, SecondType>(
365397
_ first: FirstType,
366398
_ second: SecondType.Type,

0 commit comments

Comments
 (0)