Skip to content

Commit 92f693c

Browse files
committed
unify and simplify code
1 parent 640856e commit 92f693c

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

src/main/java/org/htmlunit/javascript/HtmlUnitScriptable.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,17 @@ public void put(final String name, final Scriptable start, final Object value) {
117117
/**
118118
* Gets a named property from the object.
119119
* Normally HtmlUnit objects don't need to overwrite this method as properties are defined
120-
* on the prototypes from the XML configuration. In some cases where "content" of object
120+
* on the prototypes. In some cases where "content" of object
121121
* has priority compared to the properties consider using utility {@link #getWithPreemption(String)}.
122+
*
122123
* {@inheritDoc}
123124
*/
124125
@Override
125126
public Object get(final String name, final Scriptable start) {
126127
// Try to get property configured on object itself.
127-
Object response = super.get(name, start);
128-
if (response != NOT_FOUND) {
129-
return response;
130-
}
131-
if (this == start) {
132-
response = getWithPreemption(name);
133-
}
134-
if (response == NOT_FOUND && start instanceof Window) {
135-
response = ((Window) start).getWithFallback(name);
128+
final Object response = super.get(name, start);
129+
if (response == NOT_FOUND && this == start) {
130+
return getWithPreemption(name);
136131
}
137132
return response;
138133
}

src/main/java/org/htmlunit/javascript/host/Window.java

+24-26
Original file line numberDiff line numberDiff line change
@@ -1450,40 +1450,38 @@ private void setHandlerForJavaScript(final String eventName, final Object handle
14501450
}
14511451

14521452
/**
1453-
* To be called when the property detection fails in normal scenarios.
1454-
*
1455-
* @param name the name
1456-
* @return the found object, or {@link Scriptable#NOT_FOUND}
1453+
* {@inheritDoc}
14571454
*/
1458-
public Object getWithFallback(final String name) {
1459-
Object result = NOT_FOUND;
1460-
1455+
@Override
1456+
protected Object getWithPreemption(final String name) {
14611457
final DomNode domNode = getDomNodeOrNull();
1462-
if (domNode != null) {
1458+
if (domNode == null) {
1459+
return NOT_FOUND;
1460+
}
14631461

1464-
// May be attempting to retrieve a frame by name.
1465-
final HtmlPage page = (HtmlPage) domNode.getPage();
1466-
result = getFrameWindowByName(page, name);
14671462

1468-
if (result == NOT_FOUND) {
1469-
result = getElementsByName(page, name);
1463+
// May be attempting to retrieve a frame by name.
1464+
final HtmlPage page = (HtmlPage) domNode.getPage();
1465+
Object result = getFrameWindowByName(page, name);
14701466

1471-
if (result == NOT_FOUND) {
1472-
// May be attempting to retrieve element by ID (try map-backed operation again instead of XPath).
1473-
try {
1474-
final HtmlElement htmlElement = page.getHtmlElementById(name);
1475-
result = getScriptableFor(htmlElement);
1476-
}
1477-
catch (final ElementNotFoundException e) {
1478-
result = NOT_FOUND;
1479-
}
1467+
if (result == NOT_FOUND) {
1468+
result = getElementsByName(page, name);
1469+
1470+
if (result == NOT_FOUND) {
1471+
// May be attempting to retrieve element by ID (try map-backed operation again instead of XPath).
1472+
try {
1473+
final HtmlElement htmlElement = page.getHtmlElementById(name);
1474+
result = getScriptableFor(htmlElement);
1475+
}
1476+
catch (final ElementNotFoundException e) {
1477+
result = NOT_FOUND;
14801478
}
14811479
}
1480+
}
14821481

1483-
if (result instanceof Window) {
1484-
final WebWindow webWindow = ((Window) result).getWebWindow();
1485-
result = getProxy(webWindow);
1486-
}
1482+
if (result instanceof Window) {
1483+
final WebWindow webWindow = ((Window) result).getWebWindow();
1484+
result = getProxy(webWindow);
14871485
}
14881486

14891487
return result;

0 commit comments

Comments
 (0)