@@ -60,8 +60,10 @@ should only rely on the following documented APIs:
60
60
[`last`](@ref Base.last(::NodeChildren)),
61
61
[`isempty`](@ref Base.isempty(::NodeChildren))
62
62
- Appending or prepending new children to a parent node can be done with the
63
- [`push!`](@ref Base.push!(children::NodeChildren{T}, child::T) where {T <: Node}) and
64
- [`pushfirst!`](@ref Base.pushfirst!(children::NodeChildren{T}, child::T) where {T <: Node})
63
+ [`push!`](@ref Base.push!(children::NodeChildren{T}, child::T) where {T <: Node}),
64
+ [`pushfirst!`](@ref Base.pushfirst!(children::NodeChildren{T}, child::T) where {T <: Node}),
65
+ [`append!`](@ref Base.append!(::NodeChildren{T}, ::Any) where T),
66
+ and [`prepend!`](@ref Base.prepend!(::NodeChildren{T}, ::Any) where T)
65
67
methods
66
68
67
69
Other ways to work with child nodes that do not directly reference `.children` are:
@@ -401,6 +403,50 @@ function insert_before!(node::T, sibling::T) where {T <: Node}
401
403
return node
402
404
end
403
405
406
+ """
407
+ append!(node.children::NodeChildren, children) -> NodeChildren
408
+
409
+ Adds all the elements of the iterable `children` to the end of the list of children of `node`.
410
+ If any of `children` are part of another tree, then they are unlinked from that tree first
411
+ (see [`unlink!`](@ref)). Returns the iterator over children.
412
+
413
+ !!! warning "Error during an append"
414
+
415
+ The operation is not atomic, and an error during an `append!` (e.g. due to an element of
416
+ the wrong type in `children`) can result in a partial append of the new children, similar
417
+ to how `append!` behaves with arrays
418
+ (see [JuliaLang/julia#15868](https://github.com/JuliaLang/julia/issues/15868)).
419
+ """
420
+ function Base. append! (nodechildren:: NodeChildren{T} , children) where T
421
+ for child in children
422
+ isa (child, T) || throw (ArgumentError (" invalid element type ($(typeof (child)) ) in children, expected $T " ))
423
+ push! (nodechildren, child)
424
+ end
425
+ return nodechildren
426
+ end
427
+
428
+ """
429
+ prepend!(node.children::NodeChildren, children) -> NodeChildren
430
+
431
+ Adds all the elements of the iterable `children` to the beginning of the list of children of `node`.
432
+ If any of `children` are part of another tree, then they are unlinked from that tree first
433
+ (see [`unlink!`](@ref)). Returns the iterator over children.
434
+
435
+ !!! warning "Error during a prepend"
436
+
437
+ The operation is not atomic, and an error during a `prepend!` (e.g. due to an element of
438
+ the wrong type in `children`) can result in a partial prepend of the new children, similar
439
+ to how `append!` behaves with arrays
440
+ (see [JuliaLang/julia#15868](https://github.com/JuliaLang/julia/issues/15868)).
441
+ """
442
+ function Base. prepend! (nodechildren:: NodeChildren{T} , children) where T
443
+ for child in Iterators. reverse (children)
444
+ isa (child, T) || throw (ArgumentError (" invalid element type ($(typeof (child)) ) in children, expected $T " ))
445
+ pushfirst! (nodechildren, child)
446
+ end
447
+ return nodechildren
448
+ end
449
+
404
450
# Check if this is a root node. Next and previous should also be nothing, but this is not
405
451
# enforced. This function is currently not part of the public API.
406
452
isrootnode (node:: Node ) = isnothing (node. parent)
0 commit comments