Skip to content

Commit 2ad9f24

Browse files
dylanpieperclaude
andcommitted
Fix textarea question type detection in survey parsing
Problem: Textarea questions were being parsed as "unknown" type instead of "textarea", preventing proper restoration when navigating between pages. Root cause: HTML extraction was only getting "form-control" class instead of the full "shiny-input-textarea form-control" class string for textarea elements. Solution: - Enhanced HTML extraction to check both class attribute and tag name - Added fallback logic for textarea elements with incomplete class strings - Replaced exact string matching with pattern matching for question types This ensures textarea questions are properly detected and restored during navigation, fixing cookie-based session management for text input fields. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7e131f0 commit 2ad9f24

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

R/config.R

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,9 +1329,25 @@ extract_question_structure_html <- function(html_content) {
13291329
# Loop through all question nodes and extract information
13301330
for (question_node in question_nodes) {
13311331
question_id <- rvest::html_attr(question_node, "data-question-id")
1332-
type <- question_node |>
1333-
rvest::html_nodes(glue::glue("#{question_id}")) |>
1334-
rvest::html_attr("class")
1332+
1333+
# Enhanced extraction that considers both class and tag name
1334+
target_element <- question_node |> rvest::html_nodes(glue::glue("#{question_id}"))
1335+
1336+
if (length(target_element) > 0) {
1337+
tag_name <- rvest::html_name(target_element)
1338+
class_attr <- rvest::html_attr(target_element, "class")
1339+
1340+
# For textarea elements, ensure we get the right type even if class is incomplete
1341+
if (tag_name == "textarea" && (is.na(class_attr) || class_attr == "form-control")) {
1342+
type <- "shiny-input-textarea form-control"
1343+
} else {
1344+
type <- class_attr
1345+
}
1346+
} else {
1347+
type <- question_node |>
1348+
rvest::html_nodes(glue::glue("#{question_id}")) |>
1349+
rvest::html_attr("class")
1350+
}
13351351

13361352
# Handle case where type is empty (e.g., for reactive questions)
13371353
if (length(type) == 0 || is.na(type)) {
@@ -1524,6 +1540,7 @@ extract_question_structure_html <- function(html_content) {
15241540
# Write question structure to YAML
15251541
write_question_structure_yaml <- function(question_structure, file_yaml) {
15261542
# Map question types to extracted html classes
1543+
# Note: Order matters - more specific patterns should come first
15271544
type_replacement <- c(
15281545
'shiny-input-text form-control' = 'text',
15291546
'shiny-input-textarea form-control' = 'textarea',
@@ -1541,12 +1558,21 @@ write_question_structure_yaml <- function(question_structure, file_yaml) {
15411558

15421559
# Loop through question structure and clean/prepare questions
15431560
question_structure <- lapply(question_structure, function(question) {
1544-
# Rename type to function option names
1545-
question$type <- type_replacement[names(type_replacement) == question$type]
1546-
if (question$is_matrix) {
1547-
question$type <- "matrix"
1561+
# Rename type to function option names using pattern matching
1562+
# This handles cases where HTML classes have additional classes beyond the expected ones
1563+
matched_type <- NULL
1564+
for (pattern in names(type_replacement)) {
1565+
if (grepl(pattern, question$type, fixed = TRUE)) {
1566+
matched_type <- type_replacement[[pattern]]
1567+
break
1568+
}
15481569
}
1549-
if (length(question$type) == 0) {
1570+
1571+
if (!is.null(matched_type)) {
1572+
question$type <- matched_type
1573+
} else if (question$is_matrix) {
1574+
question$type <- "matrix"
1575+
} else {
15501576
question$type <- "unknown"
15511577
}
15521578

0 commit comments

Comments
 (0)