]> source.dussan.org Git - vaadin-framework.git/commitdiff
Don't clear element content in AbstractComponent.writeDesign (#17488)
authorJohannes Dahlström <johannesd@vaadin.com>
Fri, 17 Apr 2015 12:15:25 +0000 (15:15 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 17 Apr 2015 13:34:08 +0000 (13:34 +0000)
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

server/src/com/vaadin/ui/AbstractComponent.java
server/src/com/vaadin/ui/Component.java
server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java
server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AbstractOrderedLayoutDeclarativeTest.java
server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/AbstractSplitPanelDeclarativeTest.java
server/tests/src/com/vaadin/tests/server/component/button/ButtonDeclarativeTest.java
server/tests/src/com/vaadin/tests/server/component/csslayout/CssLayoutDeclarativeTest.java
server/tests/src/com/vaadin/tests/server/component/label/LabelDeclarativeTest.java
server/tests/src/com/vaadin/tests/server/component/tabsheet/TabSheetDeclarativeTest.java

index dae073904b023f7d9118d94cf6409ce3a1a12842..3ceb06b31ecb9bc655e62f773b8bf0d28906c041 100644 (file)
@@ -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
index adef4b69c5fd5786ba90998bce0a7b9003215a9b..5db48806c3d3df7f5aef32adef5ec166ef96b5e1 100644 (file)
@@ -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 <i>not</i> cleared.
      * @param designContext
+     *            The DesignContext instance used for writing the design
+     * 
      */
     public void writeDesign(Element design, DesignContext designContext);
 
index dbd945a3d8bc56f4501a70695587d0af33ec7e98..55231e54944567024300710afa48b48cfc4b163b 100644 (file)
  */
 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
index 192ea0f4f3e84cf0b58459db785d7e946bd6450b..8ccd41f797fa0d039826b8a669cdf6ba82d323ca 100644 (file)
  */
 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 = "<v-vertical-layout caption=test-layout";
index 1f2113fabcff690de66c456da5ae94362d2eba73..2d6db6d19a12ced65930b66798aaf662ed66c6bf 100644 (file)
@@ -15,9 +15,6 @@
  */
 package com.vaadin.tests.server.component.abstractsplitpanel;
 
-import static junit.framework.TestCase.assertTrue;
-
-import org.jsoup.nodes.Element;
 import org.junit.Test;
 
 import com.vaadin.server.Sizeable.Unit;
@@ -25,11 +22,9 @@ import com.vaadin.tests.design.DeclarativeTestBase;
 import com.vaadin.ui.AbstractSplitPanel;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.HorizontalSplitPanel;
-import com.vaadin.ui.Label;
 import com.vaadin.ui.Table;
 import com.vaadin.ui.VerticalLayout;
 import com.vaadin.ui.VerticalSplitPanel;
-import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * Tests declarative support for AbstractSplitPanel.
@@ -88,23 +83,4 @@ public class AbstractSplitPanelDeclarativeTest extends
         testRead(design, sp);
         testWrite(design, sp);
     }
-
-    @Test
-    public void testReSynchronize() {
-        // Test that old children and attributes are removed when an element is
-        // synchronized to a new component.
-        DesignContext ctx = new DesignContext();
-        VerticalSplitPanel sp = new VerticalSplitPanel();
-        sp.setMinSplitPosition(5.5f, Unit.PERCENTAGE);
-        sp.setMaxSplitPosition(95, Unit.PERCENTAGE);
-        sp.setFirstComponent(new Button("First slot"));
-        sp.setSecondComponent(new Label("Second slot"));
-        Element e = ctx.createElement(sp);
-        sp = new VerticalSplitPanel();
-        sp.writeDesign(e, ctx);
-        assertTrue("There should be no attributes in the node.", e.attributes()
-                .size() == 0);
-        assertTrue("There should be no child elements.",
-                e.children().size() == 0);
-    }
 }
\ No newline at end of file
index 02dfa226714b3771439da973817bb864fb0d4fbf..51abf6be964238b7bdfae250db35b4d538ea5d3f 100644 (file)
  */
 package com.vaadin.tests.server.component.button;
 
-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.event.ShortcutAction.KeyCode;
@@ -27,7 +22,6 @@ import com.vaadin.event.ShortcutAction.ModifierKey;
 import com.vaadin.tests.design.DeclarativeTestBase;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.NativeButton;
-import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * Tests declarative support for implementations of {@link Button} and
@@ -112,17 +106,4 @@ public class ButtonDeclarativeTest extends DeclarativeTestBase<Button> {
         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
index 4cb3fb13e58bfd9a59bdd7e2820d8a73776251b6..07822953101a5693787f86fd4ae0db868f9d8ea2 100644 (file)
@@ -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<CssLayout> {
         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;
-    }
 }
index a3f27e43972585a47e0479ef38633425ce259da3..c2fed34c7b84ee6376e85ff619d9364246188ae0 100644 (file)
  */
 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<Label> {
         testWrite(design, l);
     }
 
-    @Test
-    public void testWriteContentMode() {
-        // test that the plain-text attribute is overwritten by writeDesign
-        DesignContext ctx = new DesignContext();
-        Label l = new Label("label");
-        l.setContentMode(ContentMode.TEXT);
-        Element e = new Element(Tag.valueOf("v-label"), "", new Attributes());
-        l.writeDesign(e, ctx);
-        assertTrue("Label should be marked as plain text",
-                e.hasAttr("plain-text"));
-        l.setContentMode(ContentMode.HTML);
-        l.writeDesign(e, ctx);
-        assertFalse("Label should not be marked as plain text",
-                e.hasAttr("plain-text"));
-    }
-
     private Label createLabel(String content, String caption, boolean html) {
         Label label = new Label();
         label.setContentMode(html ? ContentMode.HTML : ContentMode.TEXT);
index c614e7fa1f933ee3e0c9907ff0af6fbb36d074bf..bb5669a3a16eef578c442d4e51ed1e7b01f390ed 100644 (file)
@@ -15,9 +15,6 @@
  */
 package com.vaadin.tests.server.component.tabsheet;
 
-import org.jsoup.nodes.Attributes;
-import org.jsoup.nodes.Element;
-import org.jsoup.parser.Tag;
 import org.junit.Test;
 
 import com.vaadin.server.ExternalResource;
@@ -25,7 +22,6 @@ import com.vaadin.tests.design.DeclarativeTestBase;
 import com.vaadin.ui.TabSheet;
 import com.vaadin.ui.TabSheet.Tab;
 import com.vaadin.ui.TextField;
-import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * Tests declarative support for TabSheet.
@@ -69,30 +65,4 @@ public class TabSheetDeclarativeTest extends DeclarativeTestBase<TabSheet> {
         testRead(design, ts);
         testWrite(design, ts);
     }
-
-    @Test
-    public void testWriteRemovesOldContent() {
-        // create old content that should be removed when writing
-        Element design = new Element(Tag.valueOf("v-tab-sheet"), "",
-                new Attributes());
-        design.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes()));
-        design.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes()));
-        design.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes()));
-        // create a new TabSheet with one tab
-        TabSheet ts = new TabSheet();
-        ts.setTabIndex(5);
-        ts.addTab(new TextField());
-        Tab tab = ts.getTab(0);
-        tab.setVisible(false);
-        tab.setClosable(true);
-        tab.setEnabled(false);
-        // write the design and check written contents
-        ts.writeDesign(design, new DesignContext());
-        assertEquals("There should be only one child", 1, design.children()
-                .size());
-        assertEquals("v-text-field", design.child(0).child(0).tagName());
-        assertEquals("5", design.attr("tabindex"));
-        Element tabDesign = design.child(0);
-        assertEquals("false", tabDesign.attr("visible"));
-    }
 }
\ No newline at end of file