Skip to content

Commit b9adc4b

Browse files
committed
small cleanup and optimization
1 parent e4299fa commit b9adc4b

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

src/main/java/org/htmlunit/html/HtmlRadioButtonInput.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
package org.htmlunit.html;
1616

17+
import static org.htmlunit.util.StringUtils.toRootLowerCase;
18+
1719
import java.io.IOException;
1820
import java.util.Map;
1921

@@ -105,13 +107,7 @@ public Page setChecked(final boolean isChecked) {
105107
final boolean changed = isChecked() != isChecked;
106108
checkedState_ = isChecked;
107109
if (isChecked) {
108-
final HtmlForm form = getEnclosingForm();
109-
if (form != null) {
110-
form.setCheckedRadioButton(this);
111-
}
112-
else if (page != null && page.isHtmlPage()) {
113-
setCheckedForPage((HtmlPage) page);
114-
}
110+
updateRadioButtonSelection(page);
115111
}
116112

117113
if (changed) {
@@ -132,18 +128,24 @@ else if (page != null && page.isHtmlPage()) {
132128
*/
133129
@Override
134130
protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
135-
final HtmlForm form = getEnclosingForm();
136131
final boolean changed = !isChecked();
132+
updateRadioButtonSelection(getPage());
133+
134+
super.doClickStateUpdate(shiftKey, ctrlKey);
135+
return changed;
136+
}
137137

138-
final Page page = getPage();
138+
/**
139+
* Updates radio selection within form context or page scope.
140+
*/
141+
private void updateRadioButtonSelection(final Page page) {
142+
final HtmlForm form = getEnclosingForm();
139143
if (form != null) {
140144
form.setCheckedRadioButton(this);
141145
}
142146
else if (page != null && page.isHtmlPage()) {
143147
setCheckedForPage((HtmlPage) page);
144148
}
145-
super.doClickStateUpdate(shiftKey, ctrlKey);
146-
return changed;
147149
}
148150

149151
/**
@@ -153,14 +155,9 @@ private void setCheckedForPage(final HtmlPage htmlPage) {
153155
final String name = getNameAttribute();
154156
for (final HtmlElement htmlElement : htmlPage.getHtmlElementDescendants()) {
155157
if (htmlElement instanceof HtmlRadioButtonInput radioInput) {
156-
if (name.equals(radioInput.getAttribute(NAME_ATTRIBUTE))
158+
if (name.equals(radioInput.getNameAttribute())
157159
&& radioInput.getEnclosingForm() == null) {
158-
if (radioInput == this) {
159-
setCheckedInternal(true);
160-
}
161-
else {
162-
radioInput.setCheckedInternal(false);
163-
}
160+
radioInput.setCheckedInternal(radioInput == this);
164161
}
165162
}
166163
}
@@ -255,7 +252,7 @@ void handleFocusLostValueChanged() {
255252
@Override
256253
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
257254
final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
258-
final String qualifiedNameLC = org.htmlunit.util.StringUtils.toRootLowerCase(qualifiedName);
255+
final String qualifiedNameLC = toRootLowerCase(qualifiedName);
259256

260257
if (VALUE_ATTRIBUTE.equals(qualifiedNameLC)) {
261258
super.setAttributeNS(namespaceURI, qualifiedNameLC, attributeValue, notifyAttributeChangeListeners,
@@ -284,15 +281,14 @@ public boolean isValueMissingValidityState() {
284281
if (ATTRIBUTE_NOT_DEFINED == getAttributeDirect(ATTRIBUTE_REQUIRED)) {
285282
return false;
286283
}
287-
if (ATTRIBUTE_NOT_DEFINED == getNameAttribute()) {
284+
final String name = getNameAttribute();
285+
if (ATTRIBUTE_NOT_DEFINED == name) {
288286
return false;
289287
}
290288

291-
final String name = getNameAttribute();
292289
for (final HtmlElement htmlElement : getPage().getHtmlElementDescendants()) {
293290
if (htmlElement instanceof HtmlRadioButtonInput radioInput) {
294-
if (name.equals(radioInput.getAttribute(NAME_ATTRIBUTE))
295-
&& radioInput.isChecked()) {
291+
if (name.equals(radioInput.getNameAttribute()) && radioInput.isChecked()) {
296292
return false;
297293
}
298294
}

0 commit comments

Comments
 (0)