Skip to content

Commit

Permalink
xml2: Workaround bug for HTML documents (r-lib/xml2#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Oct 6, 2023
1 parent 88d96b1 commit 13caa3d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: marshal
Version: 0.0.0-9033
Version: 0.0.0-9034
Title: Framework to Marshal Objects to be Used in Another R Process
Depends:
R (>= 3.2.0)
Expand Down
9 changes: 7 additions & 2 deletions R/marshal.xml2.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ marshal.xml_nodeset <- function(xml2, ...) {

marshal_xml2 <- function(xml2, ...) {
res <- list(
marshalled = xml2::xml_serialize(xml2, connection = NULL)
marshalled = list(
doctype = xml2:::doc_type(xml2),
raw = xml2::xml_serialize(xml2, connection = NULL)
)
)
class(res) <- marshal_class(xml2)

Expand All @@ -47,7 +50,9 @@ marshal_xml2 <- function(xml2, ...) {

unmarshal_xml2 <- function(xml2, ...) {
object <- xml2[["marshalled"]]
res <- xml2::xml_unserialize(object)
args <- list(object[["raw"]])
if (object[["doctype"]] == "html") args$as_html <- TRUE
res <- do.call(xml2::xml_unserialize, args = args)
stopifnot(identical(class(res), marshal_unclass(xml2)))
res
}
Expand Down
14 changes: 14 additions & 0 deletions tests/marshal.xml2.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ if (requireNamespace("xml2", quietly = TRUE)) {
doc2 <- unmarshal(doc_)

stopifnot(all.equal(doc2, doc))

file <- system.file("extdata", "r-project.html", package = "xml2")
doc <- xml2::read_html(file)

## Assert marshallability
stopifnot(marshallable(doc))

## Marshal 'xml_document' object
doc_ <- marshal(doc)

## Unmarshal 'xml_document' object
doc2 <- unmarshal(doc_)

stopifnot(all.equal(doc2, doc))
}

0 comments on commit 13caa3d

Please sign in to comment.