Skip to content

Commit

Permalink
Only clear all if value changes. (#7212)
Browse files Browse the repository at this point in the history
If we remove all when the innerHTML
value stays the same we will loose the innerHTML
on the client as the value is not re-sent,
but is still cleared from the client.

Fixes #4644

(cherry picked from commit efdc44a)
  • Loading branch information
caalador authored and mehdi-vaadin committed Jan 2, 2020
1 parent 05ee416 commit 5ed51d6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
8 changes: 7 additions & 1 deletion flow-server/src/main/java/com/vaadin/flow/dom/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
Expand Down Expand Up @@ -722,7 +723,12 @@ private Element setRawProperty(String name, Serializable value) {
verifySetPropertyName(name);

if ("innerHTML".equals(name)) {
removeAllChildren();
Serializable oldValue = getStateProvider()
.getProperty(getNode(), name);
if(!Objects.equals(value, oldValue)) {
// Only remove all children for value change
removeAllChildren();
}
}
getStateProvider().setProperty(getNode(), name, value, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2019 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.uitest.ui;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;

@Route(value = "com.vaadin.flow.uitest.ui.ElementInnerHtmlView", layout = ViewTestLayout.class)
public class ElementInnerHtmlView extends AbstractDivView {

Div innerHtml;

@Override
protected void onShow() {
innerHtml = new Div();
innerHtml.setId("inner-html-field");
add(createButton("Foo"), createButton("Boo"), getNullButton(),
innerHtml);

}

private NativeButton createButton(String value) {
NativeButton button = new NativeButton("Set value " + value,
click -> innerHtml.getElement().setProperty("innerHTML",
String.format("<p>%s</p>", value)));
button.setId("set-" + value.toLowerCase());
return button;
}

private NativeButton getNullButton() {
NativeButton button = new NativeButton("Set value null",
click -> innerHtml.getElement().setProperty("innerHTML", null));
button.setId("set-null");
return button;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2000-2019 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.uitest.ui;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.flow.component.html.testbench.DivElement;
import com.vaadin.flow.component.html.testbench.NativeButtonElement;
import com.vaadin.flow.testutil.ChromeBrowserTest;

public class ElementInnerHtmlIT extends ChromeBrowserTest {

@Test
public void elementInitOrder() {
open();
DivElement innerHtml = $(DivElement.class).id("inner-html-field");

Assert.assertEquals("", innerHtml.getPropertyString("innerHTML"));

$(NativeButtonElement.class).id("set-foo").click();
Assert.assertEquals("<p>Foo</p>", innerHtml.getPropertyString("innerHTML"));

$(NativeButtonElement.class).id("set-foo").click();
Assert.assertEquals("<p>Foo</p>", innerHtml.getPropertyString("innerHTML"));

$(NativeButtonElement.class).id("set-boo").click();
Assert.assertEquals("<p>Boo</p>", innerHtml.getPropertyString("innerHTML"));

$(NativeButtonElement.class).id("set-boo").click();
Assert.assertEquals("<p>Boo</p>", innerHtml.getPropertyString("innerHTML"));

$(NativeButtonElement.class).id("set-null").click();
Assert.assertEquals("", innerHtml.getPropertyString("innerHTML"));

}
}

0 comments on commit 5ed51d6

Please sign in to comment.