Skip to content

Commit

Permalink
fix: remove checked and selected attributes from other options
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Nov 15, 2024
1 parent dc83e65 commit bdf9eba
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions classes/question_ui_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,42 +270,47 @@ private function mangle_ids_and_names(): void {
*/
private function set_input_values_and_readonly(): void {
/** @var DOMElement $element */
foreach ($this->xpath->query("//xhtml:button | //xhtml:input | //xhtml:select | //xhtml:textarea") as $element) {
foreach ($this->xpath->query('//xhtml:button | //xhtml:input | //xhtml:select | //xhtml:textarea') as $element) {
if ($this->options->readonly) {
$element->setAttribute("disabled", "disabled");
$element->setAttribute('disabled', 'disabled');
}

// We want the unmangled name here, so this method must be called before mangle_ids_and_names.
$name = $element->getAttribute("name");
$name = $element->getAttribute('name');
if (!$name) {
continue;
}

if ($element->tagName == "input") {
$type = $element->getAttribute("type") ?: "text";
if ($element->tagName == 'input') {
$type = $element->getAttribute('type') ?: 'text';
} else {
$type = $element->tagName;
}

// Set the last saved value.
$lastvalue = $this->attempt->get_last_qt_var($name);
if (!is_null($lastvalue)) {
if ($type === "checkbox" || $type === "radio") {
if ($element->getAttribute("value") === $lastvalue) {
$element->setAttribute("checked", "checked");
if ($type === 'checkbox' || $type === 'radio') {
if ($element->getAttribute('value') === $lastvalue) {
$element->setAttribute('checked', 'checked');
} else {
$element->removeAttribute('checked');
}
} else if ($type == "select") {
} else if ($type == 'select') {
// Find the appropriate option and mark it as selected.
// TODO: Support multiselects. Seems to be non-trivial, since QT vars only deal in strings, not
// arrays.
/** @var DOMElement $option */
foreach ($element->getElementsByTagName("option") as $option) {
$optvalue = $option->hasAttribute("value") ? $option->getAttribute("value") : $option->textContent;
if ($optvalue == $lastvalue) {
$option->setAttribute("selected", "selected");
break;
foreach ($element->getElementsByTagName('option') as $option) {
$optvalue = $option->hasAttribute('value') ? $option->getAttribute('value') : $option->textContent;
if ($optvalue === $lastvalue) {
$option->setAttribute('selected', 'selected');
} else {
$option->removeAttribute('selected');
}
}
} else if ($type != "button" && $type != "submit") {
$element->setAttribute("value", $lastvalue);
} else if ($type != 'button' && $type != 'submit') {
$element->setAttribute('value', $lastvalue);
}
}
}
Expand Down

0 comments on commit bdf9eba

Please sign in to comment.