From 2d3ed921065acd7761df07b9b403d111bedf33d9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Fri, 17 Apr 2015 15:15:25 +0300 Subject: [PATCH] Don't clear element content in AbstractComponent.writeDesign (#17488) WriteDesign now assumes the element it receives is pristine, so elements cannot be reused without someone else clearing them first. Currently only DesignContext.createElement directly invokes writeDesign, and it always passes a new element to it. Change-Id: Icb777da1d73a28150041914dd9743e7aa0b25e16 --- .../src/com/vaadin/ui/AbstractComponent.java | 2 - server/src/com/vaadin/ui/Component.java | 7 ++- .../AbstractFieldDeclarativeTest.java | 21 --------- .../AbstractOrderedLayoutDeclarativeTest.java | 45 ------------------- .../AbstractSplitPanelDeclarativeTest.java | 24 ---------- .../button/ButtonDeclarativeTest.java | 19 -------- .../csslayout/CssLayoutDeclarativeTest.java | 33 -------------- .../component/label/LabelDeclarativeTest.java | 23 ---------- .../tabsheet/TabSheetDeclarativeTest.java | 30 ------------- 9 files changed, 5 insertions(+), 199 deletions(-) diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index dae073904b..3ceb06b31e 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -1239,8 +1239,6 @@ public abstract class AbstractComponent extends AbstractClientConnector */ @Override public void writeDesign(Element design, DesignContext designContext) { - // clear element contents - DesignAttributeHandler.clearElement(design); AbstractComponent def = designContext.getDefaultInstance(this); Attributes attr = design.attributes(); // handle default attributes diff --git a/server/src/com/vaadin/ui/Component.java b/server/src/com/vaadin/ui/Component.java index adef4b69c5..5db48806c3 100644 --- a/server/src/com/vaadin/ui/Component.java +++ b/server/src/com/vaadin/ui/Component.java @@ -747,7 +747,7 @@ public interface Component extends ClientConnector, Sizeable, Serializable { * * @since 7.4 * @param design - * The design as HTML to obtain the state from + * The element to obtain the state from * @param designContext * The DesignContext instance used for parsing the design */ @@ -763,8 +763,11 @@ public interface Component extends ClientConnector, Sizeable, Serializable { * * @since 7.4 * @param design - * The design as HTML to update with the current state + * The element to write the component state to. Any previous + * attributes or child nodes are not cleared. * @param designContext + * The DesignContext instance used for writing the design + * */ public void writeDesign(Element design, DesignContext designContext); diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java index dbd945a3d8..55231e5494 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java @@ -15,18 +15,12 @@ */ package com.vaadin.tests.server.component.abstractfield; -import static org.junit.Assert.assertTrue; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; import org.junit.Test; import com.vaadin.data.util.ObjectProperty; import com.vaadin.tests.design.DeclarativeTestBase; import com.vaadin.ui.AbstractField; import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; /** * Tests declarative support for implementations of {@link AbstractField}. @@ -63,21 +57,6 @@ public class AbstractFieldDeclarativeTest extends testWrite(design, tf); } - @Test - public void testWriteRemovesOldContent() { - Attributes attr = new Attributes(); - attr.put("should_be_removed", "foo"); - Element design = new Element(Tag.valueOf("v-text-field"), "", attr); - AbstractField component = new TextField(); - component.setReadOnly(true); - component.writeDesign(design, new DesignContext()); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertTrue("Design must contain readonly", design.hasAttr("readonly")); - assertTrue("Readonly must be true", design.attr("readonly").equals("") - || design.attr("readonly").equals("true")); - } - @Test public void testModelReadOnly() { // Test that read only value coming from property data source is not diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AbstractOrderedLayoutDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AbstractOrderedLayoutDeclarativeTest.java index 192ea0f4f3..8ccd41f797 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AbstractOrderedLayoutDeclarativeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AbstractOrderedLayoutDeclarativeTest.java @@ -15,14 +15,9 @@ */ package com.vaadin.tests.server.component.abstractorderedlayout; -import static org.junit.Assert.assertTrue; - import java.util.Arrays; import java.util.List; -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; import org.junit.Test; import com.vaadin.shared.ui.label.ContentMode; @@ -32,7 +27,6 @@ import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.Label; import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.declarative.DesignContext; /** * Tests declarative support for AbstractOrderedLayout. @@ -86,45 +80,6 @@ public class AbstractOrderedLayoutDeclarativeTest extends testWrite(design, layout); } - @Test - public void testWriteRemovesOldElementContent() { - // Create an element with some content - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - Element design = new Element(Tag.valueOf("v-vertical-layout"), "", - rootAttributes); - Attributes firstChildAttributes = new Attributes(); - firstChildAttributes.put("caption", "test-label"); - Element firstChild = new Element(Tag.valueOf("v-label"), "", - firstChildAttributes); - design.appendChild(firstChild); - - Attributes secondChildAttributes = new Attributes(); - secondChildAttributes.put("caption", "test-button"); - Element secondChild = new Element(Tag.valueOf("v-button"), "", - secondChildAttributes); - design.appendChild(secondChild); - Attributes thirdChildAttributes = new Attributes(); - thirdChildAttributes.put("caption", "test-button-2"); - Element thirdChild = new Element(Tag.valueOf("v-button"), "", - thirdChildAttributes); - design.appendChild(thirdChild); - // Create and write a layout and check the new contents of the element - // node - VerticalLayout layout = new VerticalLayout(); - layout.addComponent(new Label("test-label")); - layout.getComponent(0).setCaption("test-caption"); - layout.setExpandRatio(layout.getComponent(0), 1.0f); - layout.addComponent(new Label("test-label-2")); - layout.writeDesign(design, new DesignContext()); - assertEquals(2, design.childNodes().size()); - assertEquals("v-label", ((Element) design.childNode(0)).tagName()); - assertEquals("test-caption", design.childNode(0).attr("caption")); - assertTrue(design.childNode(0).hasAttr(":expand")); - assertEquals("", design.childNode(0).attr(":expand")); - - } - private String getDesign(float expandRatio, boolean margin, String... alignments) { String result = " { testRead(design, b); testWrite(design, b); } - - @Test - public void testWriteUpdatesContentMode() { - DesignContext ctx = new DesignContext(); - Button button = new Button("OK"); - Element e = new Element(Tag.valueOf("v-button"), "", new Attributes()); - button.writeDesign(e, ctx); - assertTrue("Button is plain text by default", e.hasAttr("plain-text")); - - button.setHtmlContentAllowed(true); - button.writeDesign(e, ctx); - assertTrue("Button is updated to HTML", !e.hasAttr("plain-text")); - } } \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/CssLayoutDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/CssLayoutDeclarativeTest.java index 4cb3fb13e5..0782295310 100644 --- a/server/tests/src/com/vaadin/tests/server/component/csslayout/CssLayoutDeclarativeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/csslayout/CssLayoutDeclarativeTest.java @@ -15,9 +15,6 @@ */ package com.vaadin.tests.server.component.csslayout; -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; import org.junit.Test; import com.vaadin.shared.ui.label.ContentMode; @@ -25,7 +22,6 @@ import com.vaadin.tests.design.DeclarativeTestBase; import com.vaadin.ui.Button; import com.vaadin.ui.CssLayout; import com.vaadin.ui.Label; -import com.vaadin.ui.declarative.DesignContext; /** * Tests declarative support for CssLayout. @@ -64,33 +60,4 @@ public class CssLayoutDeclarativeTest extends DeclarativeTestBase { testRead(design, layout); testWrite(design, layout); } - - @Test - public void testWriteWithOldContents() { - // Test that any old contents of an element are removed when - // writing a design. - Element design = createDesign(); - CssLayout layout = new CssLayout(); - layout.addComponent(new Label("test-label")); - layout.getComponent(0).setCaption("test-caption"); - layout.addComponent(new Label("test-label-2")); - layout.writeDesign(design, new DesignContext()); - assertEquals(2, design.childNodes().size()); - assertEquals("v-label", ((Element) design.childNode(0)).tagName()); - assertEquals("test-caption", design.childNode(0).attr("caption")); - } - - private Element createDesign() { - // create an element with some contents - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - Element node = new Element(Tag.valueOf("v-vertical-layout"), "", - rootAttributes); - Attributes childAttributes = new Attributes(); - childAttributes.put("caption", "test-label"); - Element child = new Element(Tag.valueOf("v-label"), "", childAttributes); - node.appendChild(child); - - return node; - } } diff --git a/server/tests/src/com/vaadin/tests/server/component/label/LabelDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/label/LabelDeclarativeTest.java index a3f27e4397..c2fed34c7b 100644 --- a/server/tests/src/com/vaadin/tests/server/component/label/LabelDeclarativeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/label/LabelDeclarativeTest.java @@ -15,18 +15,11 @@ */ package com.vaadin.tests.server.component.label; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; import org.junit.Test; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.tests.design.DeclarativeTestBase; import com.vaadin.ui.Label; -import com.vaadin.ui.declarative.DesignContext; /** * Tests declarative support for implementations of {@link Label}. @@ -91,22 +84,6 @@ public class LabelDeclarativeTest extends DeclarativeTestBase