Skip to content

Commit

Permalink
Allow avoiding de-duplication in xml_parent
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhester committed Oct 28, 2020
1 parent 685bfff commit 0f08e4b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# xml2 (development version)

* `xml_parent()` now supports passing `deduplicate = TRUE` to avoid de-duplication of nodes in the returned nodeset.

* `xml_find_all.xml_nodeset()` gains a `flatten` argument to control whether to return a single nodeset or a list of nodesets (#311, @jakejh)

* `write_xml()` and `write_html()` now return NULL invisibly, as they did prior to version 1.3.0 (#307)
Expand Down
8 changes: 4 additions & 4 deletions R/classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ xml_nodeset <- function(nodes = list(), deduplicate = TRUE) {
#' @param nodes A list (possible nested) of external pointers to nodes
#' @return a nodeset
#' @noRd
make_nodeset <- function(nodes, doc) {
make_nodeset <- function(nodes, doc, ...) {
nodes <- unlist(nodes, recursive = FALSE)

xml_nodeset(lapply(nodes, xml_node, doc = doc))
xml_nodeset(lapply(nodes, xml_node, doc = doc), ...)
}

#' @export
Expand Down Expand Up @@ -147,7 +147,7 @@ nodeset_apply.xml_missing <- function(x, fun, ...) {
}

#' @export
nodeset_apply.xml_nodeset <- function(x, fun, ...) {
nodeset_apply.xml_nodeset <- function(x, fun, ..., deduplicate = TRUE) {
if (length(x) == 0)
return(xml_nodeset())

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

make_nodeset(res, x[[1]]$doc)
make_nodeset(res, x[[1]]$doc, deduplicate = deduplicate)
}

#' @export
Expand Down
21 changes: 12 additions & 9 deletions R/xml_children.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#' # Note the each unique node only appears once in the output
#' xml_parent(xml_children(x))
#'
#' # But you avoid this deduplication if needed
#' xml_parent(xml_children(x), deduplicate = FALSE)
#'
#' # Mixed content
#' x <- read_xml("<foo> a <b/> c <d>e</d> f</foo>")
#' # Childen gets the elements, contents gets all node types
Expand All @@ -37,8 +40,8 @@
#' xml_child(x)
#' xml_child(x, 2)
#' xml_child(x, "baz")
xml_children <- function(x) {
nodeset_apply(x, function(x) .Call(node_children, x, TRUE))
xml_children <- function(x, ...) {
nodeset_apply(x, function(x) .Call(node_children, x, TRUE), ...)
}

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

#' @export
#' @rdname xml_children
xml_parents <- function(x) {
nodeset_apply(x, function(x) .Call(node_parents, x))
xml_parents <- function(x, ...) {
nodeset_apply(x, function(x) .Call(node_parents, x), ...)
}

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

#' @export
#' @rdname xml_children
xml_parent <- function(x) {
xml_parent <- function(x, ...) {
UseMethod("xml_parent")
}

#' @export
xml_parent.xml_missing <- function(x) {
xml_parent.xml_missing <- function(x, ...) {
xml_missing()
}

#' @export
xml_parent.xml_node <- function(x) {
xml_parent.xml_node <- function(x, ...) {
xml_node(.Call(node_parent, x$node), x$doc)
}

#' @export
xml_parent.xml_nodeset <- function(x) {
nodeset_apply(x, function(x) .Call(node_parent, x))
xml_parent.xml_nodeset <- function(x, ...) {
nodeset_apply(x, function(x) .Call(node_parent, x), ...)
}


Expand Down
9 changes: 6 additions & 3 deletions man/xml_children.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f08e4b

Please sign in to comment.