Skip to content

Commit bdde6a6

Browse files
author
Denis
authored
fix: Use JS equals to check whether property value is changed (#11192)
Fixes #11175
1 parent 081a1a2 commit bdde6a6

8 files changed

Lines changed: 172 additions & 4 deletions

File tree

‎flow-client/src/main/java/com/vaadin/client/WidgetUtil.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.vaadin.client;
1818

19+
import java.util.Objects;
20+
1921
import com.google.gwt.core.client.GWT;
2022
import com.google.gwt.core.client.JavaScriptObject;
2123

@@ -319,4 +321,42 @@ public static native String stringify(JsonObject payload) /*-{
319321
return value;
320322
});
321323
}-*/;
324+
325+
/**
326+
* Checks whether the objects are equal either as Java objects (considering
327+
* types and Java {@link Object#equals(Object)} method) or as JS values.
328+
*
329+
* @param obj1
330+
* an object
331+
*
332+
* @param obj2
333+
* an object to be compared with {@code a} for deep equality
334+
* @return {@code true} if the arguments are equal to each other and
335+
* {@code false} otherwise
336+
*
337+
* @see #equalsInJS(Object, Object)
338+
*/
339+
public static boolean equals(Object obj1, Object obj2) {
340+
return Objects.equals(obj1, obj2) || equalsInJS(obj1, obj2);
341+
}
342+
343+
/**
344+
* Checks whether the objects are equal as JS values.
345+
* <p>
346+
* This check ignores object types and checks the values via JS {@code ==}.
347+
* E.g. it means that an empty string equals to {@code 0}.
348+
*
349+
* @param obj1
350+
* an object
351+
*
352+
* @param obj2
353+
* an object to be compared with {@code a} for deep equality
354+
* @return {@code true} if the arguments are equal via JS {@code ==} to each
355+
* other and {@code false} otherwise
356+
*/
357+
public static native boolean equalsInJS(Object obj1, Object obj2)
358+
/*-{
359+
return obj1==obj2;
360+
}-*/;
361+
322362
}

‎flow-client/src/main/java/com/vaadin/client/flow/binding/SimpleElementBindingStrategy.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.vaadin.client.flow.binding;
1717

18-
import java.util.Objects;
1918
import java.util.function.Consumer;
2019
import java.util.function.Supplier;
2120

@@ -730,7 +729,7 @@ private void updateProperty(MapProperty mapProperty, Element element) {
730729
// which are updated on the client side, e.g. when synchronizing
731730
// properties to the server (won't work for readonly properties).
732731
if (WidgetUtil.isUndefined(domValue)
733-
|| !Objects.equals(domValue, treeValue)) {
732+
|| !WidgetUtil.equals(domValue, treeValue)) {
734733
Reactive.runWithComputation(null,
735734
() -> WidgetUtil.setJsProperty(element, name,
736735
PolymerUtils.createModelTree(treeValue)));

‎flow-client/src/test-gwt/java/com/vaadin/client/flow/GwtBasicElementBinderTest.java‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ public void testBindExistingProperty() {
7979
assertEquals("foo", element.getTitle());
8080
}
8181

82+
public void testBindExistingPropertyWithDifferentType() {
83+
//set number as a property value for DOM elememnt
84+
double value= setNumberValue(element, "bar");
85+
86+
// set string as a StateTree property value
87+
properties.getProperty("bar").setValue(""+value);
88+
89+
Binder.bind(node, element);
90+
91+
Reactive.flush();
92+
93+
// the type should not be changed
94+
assertEquals("number", getPropertyType(element, "bar"));
95+
}
96+
8297
public void testBindNewProperty() {
8398
Binder.bind(node, element);
8499

@@ -1613,7 +1628,6 @@ public void testBindHiddenElement_stateNodeChangesVisibility_elementStaysHidden(
16131628
assertEquals("", element.getStyle().getDisplay());
16141629
}
16151630

1616-
16171631
/**
16181632
* The element is in shadowroot and state node is hidden. The element gets
16191633
* attribute "hidden" and "display: none".
@@ -1654,7 +1668,6 @@ public void testBindHiddenElement_elementInShadowRootAndHasInitialDisplayPropert
16541668
assertEquals("inline-block", element.getStyle().getDisplay());
16551669
}
16561670

1657-
16581671
/**
16591672
* The StateNode is visible (the visibility is true).
16601673
*
@@ -1958,4 +1971,15 @@ private native void initPolymer(Element element)
19581971
}
19591972
}-*/;
19601973

1974+
private native String getPropertyType(Object obj, String name)
1975+
/*-{
1976+
return typeof obj[name];
1977+
}-*/;
1978+
1979+
private native double setNumberValue(Object obj, String name)
1980+
/*-{
1981+
obj[name] =2;
1982+
return 2;
1983+
}-*/;
1984+
19611985
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { LitElement, html } from "lit";
2+
3+
export class InjectedTemplate extends LitElement {
4+
static get properties() {
5+
return {
6+
value: { type: Number}
7+
};
8+
}
9+
render() {
10+
return html`
11+
<div>Injected Template</div>
12+
`;
13+
}
14+
}
15+
customElements.define("injected-lit-template", InjectedTemplate);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { LitElement, html } from "lit";
2+
import "./InjectedTemplate.js";
3+
4+
export class InjectingTemplate extends LitElement {
5+
render() {
6+
return html`
7+
<div id='container'></div>
8+
<injected-lit-template value="1.0" id='injected' ></injected-lit-template>
9+
<button id='show-type' @click="${e => this._addClientSideChild()}">Show type</button>
10+
`;
11+
}
12+
13+
_addClientSideChild() {
14+
let element = document.createElement("div");
15+
element.innerHTML = typeof this.shadowRoot.querySelector('#injected').value;
16+
element.id='type-info';
17+
this.shadowRoot.querySelector('#container').appendChild(element);
18+
}
19+
}
20+
customElements.define("injecting-lit-template", InjectingTemplate);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vaadin.flow.uitest.ui.littemplate;
2+
3+
import com.vaadin.flow.component.Tag;
4+
import com.vaadin.flow.component.dependency.JsModule;
5+
import com.vaadin.flow.component.littemplate.LitTemplate;
6+
7+
@Tag("injected-lit-template")
8+
@JsModule("./lit-templates/InjectedTemplate.js")
9+
public class InjectedTemplateView extends LitTemplate {
10+
11+
public InjectedTemplateView() {
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.vaadin.flow.uitest.ui.littemplate;
2+
3+
import com.vaadin.flow.component.Tag;
4+
import com.vaadin.flow.component.dependency.JsModule;
5+
import com.vaadin.flow.component.littemplate.LitTemplate;
6+
import com.vaadin.flow.component.template.Id;
7+
import com.vaadin.flow.router.Route;
8+
9+
@Route(value = "com.vaadin.flow.uitest.ui.littemplate.InjectingTemplateView")
10+
@Tag("injecting-lit-template")
11+
@JsModule("./lit-templates/InjectingTemplate.js")
12+
public class InjectingTemplateView extends LitTemplate {
13+
14+
@Id("injected")
15+
private InjectedTemplateView view;
16+
17+
public InjectingTemplateView() {
18+
setId("injecting");
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.uitest.ui.littemplate;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
import com.vaadin.flow.testutil.ChromeBrowserTest;
22+
import com.vaadin.testbench.TestBenchElement;
23+
24+
public class InjectingTemplateIT extends ChromeBrowserTest {
25+
26+
@Test
27+
public void mapTemplateViaIdWithNumberProperty_propertyTypeIsNotChangedAfterBidning() {
28+
open();
29+
30+
TestBenchElement template = $(TestBenchElement.class).id("injecting");
31+
template.$(TestBenchElement.class).id("show-type").click();
32+
33+
TestBenchElement container = template.$(TestBenchElement.class)
34+
.id("container");
35+
Assert.assertEquals("number", container.getText());
36+
}
37+
}

0 commit comments

Comments
 (0)