Skip to content

Commit

Permalink
Search with xpath
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jan 26, 2024
1 parent 84769bf commit 9fe933a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion R/live.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,25 @@ LiveHTML <- R6::R6Class(
if (!missing(css)) {
unlist(self$session$DOM$querySelectorAll(private$root_id, css)$nodeIds)
} else {
cli::cli_abort("{.arg xpath} is not supported by <ChromoteSession>.")
search <- glue::glue("
(function() {{
const xpathResult = document.evaluate('{xpath}', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
const nodes = [];
for (let i = 0; i < xpathResult.snapshotLength; i++) {{
nodes.push(xpathResult.snapshotItem(i));
}}
return(nodes);
}})();
")

object_id <- self$session$Runtime$evaluate(search)$result$objectId
props <- self$session$Runtime$getProperties(object_id, ownProperties = TRUE)

ids <- map_chr(props$result, function(prop) prop$value$objectId %||% NA_character_)
# Drop non-nodes
ids <- ids[!is.na(ids)]

unlist(map(ids, self$session$DOM$requestNode), use.names = FALSE)
}
},

Expand Down

0 comments on commit 9fe933a

Please sign in to comment.