Skip to content

Commit

Permalink
Add argument checking
Browse files Browse the repository at this point in the history
Fixes #378
  • Loading branch information
hadley committed Jan 23, 2024
1 parent e17b3c3 commit 259775a
Show file tree
Hide file tree
Showing 16 changed files with 985 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Imports:
httr (>= 0.5),
lifecycle (>= 1.0.3),
magrittr,
rlang (>= 1.0.0),
rlang (>= 1.1.0),
selectr,
tibble,
xml2 (>= 1.3)
Expand Down
14 changes: 10 additions & 4 deletions R/form.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ html_form.xml_nodeset <- function(x, base_url = NULL) {

#' @export
html_form.xml_node <- function(x, base_url = NULL) {
stopifnot(xml2::xml_name(x) == "form")
if (xml2::xml_name(x) != "form") {
cli::cli_abort("{.arg x} must be a <form> element.")
}
check_string(base_url, allow_null = TRUE)

attr <- as.list(xml2::xml_attrs(x))
name <- attr$id %||% attr$name %||% "<unnamed>" # for human readers
Expand Down Expand Up @@ -180,13 +183,13 @@ submission_find_submit <- function(fields, idx, error_call = caller_env()) {
list()
} else {
if (length(buttons) > 1) {
inform(paste0("Submitting with '", buttons[[1]]$name, "'"))
cli::cli_inform("Submitting with button {.str {buttons[[1]]$name}}.")
}
buttons[[1]]
}
} else if (is.numeric(idx) && length(idx) == 1) {
if (idx < 1 || idx > length(buttons)) {
cli::cli_abort("Numeric `submit` out of range.", call = error_call)
cli::cli_abort("Numeric {.arg submit} out of range.", call = error_call)
}
buttons[[idx]]
} else if (is.character(idx) && length(idx) == 1) {
Expand All @@ -201,7 +204,10 @@ submission_find_submit <- function(fields, idx, error_call = caller_env()) {
}
buttons[[idx]]
} else {
cli::cli_abort("`submit` must be NULL, a string, or a number.", call = error_call)
cli::cli_abort(
"{.arg submit} must be NULL, a string, or a number.",
call = error_call
)
}
}

Expand Down
3 changes: 3 additions & 0 deletions R/html.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ html_name <- function(x) {
#' @export
#' @importFrom xml2 xml_attr
html_attr <- function(x, name, default = NA_character_) {
check_string(name)
check_string(default, allow_na = TRUE)

xml_attr(x, name, default = default)
}

Expand Down
Loading

0 comments on commit 259775a

Please sign in to comment.