diff options
author | Denis Anisimov <denis@vaadin.com> | 2014-03-12 21:17:18 +0200 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2014-05-22 09:52:42 +0000 |
commit | 95e563a69b540bae2616466ae25646eba50d4afe (patch) | |
tree | 9a011e0417ee06f1ee7648da6975a19ec0401316 /server/src/com | |
parent | a11909909c895032ca5f8b271dfb0fa29efa2a6f (diff) | |
download | vaadin-framework-95e563a69b540bae2616466ae25646eba50d4afe.tar.gz vaadin-framework-95e563a69b540bae2616466ae25646eba50d4afe.zip |
Prevent adding/setting parent component as child (#12720).
Change-Id: I9b7b43f176e88d9edca591001297b94475a31854
Diffstat (limited to 'server/src/com')
3 files changed, 30 insertions, 9 deletions
diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index d980c27916..b6289e0b7d 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -991,4 +991,24 @@ public abstract class AbstractComponent extends AbstractClientConnector actionManager.removeAction(shortcut); } } + + /** + * Determine whether a <code>content</code> component is equal to, or the + * ancestor of this component. + * + * @param content + * the potential ancestor element + * @return <code>true</code> if the relationship holds + */ + protected boolean isOrHasAncestor(Component content) { + if (content instanceof HasComponents) { + for (Component parent = this; parent != null; parent = parent + .getParent()) { + if (parent == content) { + return true; + } + } + } + return false; + } } diff --git a/server/src/com/vaadin/ui/AbstractComponentContainer.java b/server/src/com/vaadin/ui/AbstractComponentContainer.java index 0aa185d557..b1e69ba76b 100644 --- a/server/src/com/vaadin/ui/AbstractComponentContainer.java +++ b/server/src/com/vaadin/ui/AbstractComponentContainer.java @@ -196,15 +196,10 @@ public abstract class AbstractComponentContainer extends AbstractComponent */ @Override public void addComponent(Component c) { - if (c instanceof ComponentContainer) { - // Make sure we're not adding the component inside it's own content - for (Component parent = this; parent != null; parent = parent - .getParent()) { - if (parent == c) { - throw new IllegalArgumentException( - "Component cannot be added inside it's own content"); - } - } + // Make sure we're not adding the component inside it's own content + if (isOrHasAncestor(c)) { + throw new IllegalArgumentException( + "Component cannot be added inside it's own content"); } if (c.getParent() != null) { diff --git a/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java b/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java index de1bb29846..ba108fc302 100644 --- a/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java +++ b/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java @@ -123,6 +123,12 @@ public abstract class AbstractSingleComponentContainer extends */ @Override public void setContent(Component content) { + // Make sure we're not adding the component inside it's own content + if (isOrHasAncestor(content)) { + throw new IllegalArgumentException( + "Component cannot be added inside it's own content"); + } + Component oldContent = getContent(); if (oldContent == content) { // do not set the same content twice |