Skip to content

Commit

Permalink
Make xml_serialize()/xml_unserialize() work also for HTML documents (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Oct 7, 2023
1 parent ef2310b commit 7ae7ad0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions R/xml_serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ xml_serialize.xml_document <- function(object, connection, ...) {
connection <- file(connection, "w", raw = TRUE)
on.exit(close(connection))
}
serialize(structure(as.character(object, ...), class = "xml_serialized_document"), connection)
serialize(structure(as.character(object, ...), doc_type = doc_type(object), class = "xml_serialized_document"), connection)
}

#' @export
Expand Down Expand Up @@ -64,9 +64,15 @@ xml_unserialize <- function(connection, ...) {
# Select only the root
res <- xml_find_first(x, "/node()")
} else if (inherits(object, "xml_serialized_document")) {
res <- read_xml(unclass(object), ...)
read_xml_int <- function(object, as_html = FALSE, ...) {
if (missing(as_html)) {
as_html <- identical(attr(object, "doc_type", exact = TRUE), "html")
}
read_xml(unclass(object), as_html = as_html, ...)
}
res <- read_xml_int(unclass(object), ...)
} else {
abort("Not a serialized xml2 object")
stop("Not a serialized xml2 object", call. = FALSE)
}
res
}

0 comments on commit 7ae7ad0

Please sign in to comment.