Skip to content

Commit 0f08e4b

Browse files
committed
Allow avoiding de-duplication in xml_parent
1 parent 685bfff commit 0f08e4b

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# xml2 (development version)
22

3+
* `xml_parent()` now supports passing `deduplicate = TRUE` to avoid de-duplication of nodes in the returned nodeset.
4+
35
* `xml_find_all.xml_nodeset()` gains a `flatten` argument to control whether to return a single nodeset or a list of nodesets (#311, @jakejh)
46

57
* `write_xml()` and `write_html()` now return NULL invisibly, as they did prior to version 1.3.0 (#307)

R/classes.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ xml_nodeset <- function(nodes = list(), deduplicate = TRUE) {
8181
#' @param nodes A list (possible nested) of external pointers to nodes
8282
#' @return a nodeset
8383
#' @noRd
84-
make_nodeset <- function(nodes, doc) {
84+
make_nodeset <- function(nodes, doc, ...) {
8585
nodes <- unlist(nodes, recursive = FALSE)
8686

87-
xml_nodeset(lapply(nodes, xml_node, doc = doc))
87+
xml_nodeset(lapply(nodes, xml_node, doc = doc), ...)
8888
}
8989

9090
#' @export
@@ -147,7 +147,7 @@ nodeset_apply.xml_missing <- function(x, fun, ...) {
147147
}
148148

149149
#' @export
150-
nodeset_apply.xml_nodeset <- function(x, fun, ...) {
150+
nodeset_apply.xml_nodeset <- function(x, fun, ..., deduplicate = TRUE) {
151151
if (length(x) == 0)
152152
return(xml_nodeset())
153153

@@ -159,7 +159,7 @@ nodeset_apply.xml_nodeset <- function(x, fun, ...) {
159159
res[!is_missing] <- lapply(x[!is_missing], function(x) fun(x$node, ...))
160160
}
161161

162-
make_nodeset(res, x[[1]]$doc)
162+
make_nodeset(res, x[[1]]$doc, deduplicate = deduplicate)
163163
}
164164

165165
#' @export

R/xml_children.R

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#' # Note the each unique node only appears once in the output
2525
#' xml_parent(xml_children(x))
2626
#'
27+
#' # But you avoid this deduplication if needed
28+
#' xml_parent(xml_children(x), deduplicate = FALSE)
29+
#'
2730
#' # Mixed content
2831
#' x <- read_xml("<foo> a <b/> c <d>e</d> f</foo>")
2932
#' # Childen gets the elements, contents gets all node types
@@ -37,8 +40,8 @@
3740
#' xml_child(x)
3841
#' xml_child(x, 2)
3942
#' xml_child(x, "baz")
40-
xml_children <- function(x) {
41-
nodeset_apply(x, function(x) .Call(node_children, x, TRUE))
43+
xml_children <- function(x, ...) {
44+
nodeset_apply(x, function(x) .Call(node_children, x, TRUE), ...)
4245
}
4346

4447
#' @export
@@ -65,8 +68,8 @@ xml_contents <- function(x) {
6568

6669
#' @export
6770
#' @rdname xml_children
68-
xml_parents <- function(x) {
69-
nodeset_apply(x, function(x) .Call(node_parents, x))
71+
xml_parents <- function(x, ...) {
72+
nodeset_apply(x, function(x) .Call(node_parents, x), ...)
7073
}
7174

7275
#' @export
@@ -77,23 +80,23 @@ xml_siblings <- function(x) {
7780

7881
#' @export
7982
#' @rdname xml_children
80-
xml_parent <- function(x) {
83+
xml_parent <- function(x, ...) {
8184
UseMethod("xml_parent")
8285
}
8386

8487
#' @export
85-
xml_parent.xml_missing <- function(x) {
88+
xml_parent.xml_missing <- function(x, ...) {
8689
xml_missing()
8790
}
8891

8992
#' @export
90-
xml_parent.xml_node <- function(x) {
93+
xml_parent.xml_node <- function(x, ...) {
9194
xml_node(.Call(node_parent, x$node), x$doc)
9295
}
9396

9497
#' @export
95-
xml_parent.xml_nodeset <- function(x) {
96-
nodeset_apply(x, function(x) .Call(node_parent, x))
98+
xml_parent.xml_nodeset <- function(x, ...) {
99+
nodeset_apply(x, function(x) .Call(node_parent, x), ...)
97100
}
98101

99102

man/xml_children.Rd

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)