Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #1779 of HtmlUnit - detect cycles between elements #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/org/mozilla/javascript/ScriptableObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2200,14 +2200,26 @@ public static Scriptable getClassPrototype(Scriptable scope,
* @param obj a JavaScript object
* @return the corresponding global scope
*/
public static Scriptable getTopLevelScope(Scriptable obj)
public static Scriptable getTopLevelScope(final Scriptable obj)
{
// FIXME: some classes throw NPE in toString methods!
// FIXME: some classes throw StackOverflow in toString methods!
final HashSet<Scriptable> cache = new HashSet<Scriptable>();
cache.add(obj);
Scriptable temp = obj;
for (;;) {
Scriptable parent = obj.getParentScope();
final Scriptable parent = temp.getParentScope();
// prevent indefinite loop.
// FIXME: HtmlUnit's Window handles are sometimes each other's descendants.
if (cache.contains(parent)) {
return parent;
// throw new IllegalStateException("Scriptable is it's own descendant!: " + obj);
}
if (parent == null) {
return obj;
return temp;
}
obj = parent;
temp = parent;
cache.add(parent);
}
}

Expand Down