From 7ae7ad0d352ed17692b1e292431533a5e78017ad Mon Sep 17 00:00:00 2001 From: Henrik Bengtsson Date: Fri, 6 Oct 2023 18:25:04 -0700 Subject: [PATCH] Make xml_serialize()/xml_unserialize() work also for HTML documents (fix #407) --- R/xml_serialize.R | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/R/xml_serialize.R b/R/xml_serialize.R index 1869268e..74e16084 100644 --- a/R/xml_serialize.R +++ b/R/xml_serialize.R @@ -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 @@ -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 }