From 95e563a69b540bae2616466ae25646eba50d4afe Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Wed, 12 Mar 2014 21:17:18 +0200 Subject: Prevent adding/setting parent component as child (#12720). Change-Id: I9b7b43f176e88d9edca591001297b94475a31854 --- server/src/com/vaadin/ui/AbstractComponent.java | 20 ++++++++++++++++++++ .../com/vaadin/ui/AbstractComponentContainer.java | 13 ++++--------- .../vaadin/ui/AbstractSingleComponentContainer.java | 6 ++++++ 3 files changed, 30 insertions(+), 9 deletions(-) (limited to 'server/src') 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 content component is equal to, or the + * ancestor of this component. + * + * @param content + * the potential ancestor element + * @return true 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 -- cgit v1.2.3