Skip to content

Commit fbdbfe6

Browse files
committed
Error on inputs greater than length 1
1 parent eba90d2 commit fbdbfe6

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
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+
* `read_html()` and `read_xml()` now error if passed strings of length greater than one (#121)
4+
35
* `read_xml.raw()` had an inadvertent regression in 1.3.0 and is now again fixed (#300)
46

57
* Compilation fix on macOS 10.15.4 (@kevinushey, #296)

R/xml_parse.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ read_xml.character <- function(x, encoding = "", ..., as_html = FALSE,
9898
stop("Document is empty", call. = FALSE)
9999
}
100100

101+
if (length(x) > 1) {
102+
stop("`x` must be a string of length 1", call. = FALSE)
103+
}
104+
101105
options <- parse_options(options, xml_parse_options())
102106
if (grepl("<|>", x)) {
103107
read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, options = options)

tests/testthat/test-read-xml.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,7 @@ test_that("read_html works with non-ASCII encodings", {
102102
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body>\U2019</body></html>\n")
103103
})
104104

105+
test_that("read_xml and read_html fail with > 1 input", {
106+
expect_error(read_xml(c("foo", "bar")), "`x` must be a string of length 1")
107+
expect_error(read_html(c("foo", "bar")), "`x` must be a string of length 1")
108+
})

0 commit comments

Comments
 (0)