Skip to content

Commit df94a94

Browse files
committed
Autocomplete - Use valuebox.setAutocomplete(ON | OFF | NEW_PASSWORD) instead of valuebox.setAutocomplete(true | false) because of browser new policies.
1 parent 6d0182c commit df94a94

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

gwt-material/src/main/java/gwt/material/design/client/base/HasAutocomplete.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919
*/
2020
package gwt.material.design.client.base;
2121

22+
import gwt.material.design.client.constants.Autocomplete;
23+
24+
/**
25+
* The autocomplete attribute specifies whether or not an input field should have autocomplete enabled.
26+
*
27+
* <a href="https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion">Documentation</a>
28+
*/
2229
public interface HasAutocomplete {
2330

24-
void setAutocomplete(boolean value);
31+
void setAutocomplete(Autocomplete autocomplete);
2532

26-
boolean isAutocomplete();
33+
Autocomplete getAutocomplete();
2734
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* #%L
3+
* GwtMaterial
4+
* %%
5+
* Copyright (C) 2015 - 2020 GwtMaterialDesign
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package gwt.material.design.client.constants;
21+
22+
import java.util.Arrays;
23+
24+
public enum Autocomplete {
25+
26+
ON("on"),
27+
OFF("off"),
28+
NEW_PASSWORD("new-password");
29+
30+
private String name;
31+
32+
Autocomplete(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public static Autocomplete get(String name) {
41+
return name != null && !name.isEmpty() ? Arrays.stream(values()).filter(autocomplete -> autocomplete.getName().equals(name)).findAny().orElse(null) : null;
42+
}
43+
}

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialSearch.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
import gwt.material.design.client.base.HasActive;
3030
import gwt.material.design.client.base.HasSearchHandlers;
3131
import gwt.material.design.client.base.SearchObject;
32-
import gwt.material.design.client.constants.Color;
33-
import gwt.material.design.client.constants.CssName;
34-
import gwt.material.design.client.constants.IconType;
35-
import gwt.material.design.client.constants.InputType;
32+
import gwt.material.design.client.constants.*;
3633
import gwt.material.design.client.events.SearchFinishEvent;
3734
import gwt.material.design.client.events.SearchNoResultEvent;
3835
import gwt.material.design.client.ui.html.Label;
@@ -134,7 +131,7 @@ protected void onLoad() {
134131
super.onLoad();
135132

136133
setType(InputType.SEARCH);
137-
setAutocomplete(false);
134+
setAutocomplete(Autocomplete.OFF);
138135
label.add(iconSearch);
139136
label.getElement().setAttribute("for", "search");
140137
add(label);

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialValueBox.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -122,7 +122,7 @@ public MaterialValueBox(ValueBoxBase<T> tValueBox) {
122122
public void setup(ValueBoxBase<T> tValueBox) {
123123
valueBoxBase = tValueBox;
124124
add(valueBoxBase);
125-
setAutocomplete(false);
125+
setAutocomplete(Autocomplete.OFF);
126126
}
127127

128128
@Deprecated
@@ -565,13 +565,13 @@ public void setFieldWidth(double percentWidth) {
565565
}
566566

567567
@Override
568-
public void setAutocomplete(boolean value) {
569-
valueBoxBase.getElement().setAttribute("autocomplete", value ? "on" : "off");
568+
public void setAutocomplete(Autocomplete value) {
569+
valueBoxBase.getElement().setAttribute("autocomplete", value.getName());
570570
}
571571

572572
@Override
573-
public boolean isAutocomplete() {
574-
return valueBoxBase.getElement().getAttribute("autocomplete").equals("on");
573+
public Autocomplete getAutocomplete() {
574+
return Autocomplete.get(valueBoxBase.getElement().getAttribute("autocomplete"));
575575
}
576576

577577
@Override

gwt-material/src/test/java/gwt/material/design/client/ui/MaterialValueBoxTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.gwt.event.shared.HasHandlers;
2323
import com.google.gwt.user.client.ui.IsWidget;
2424
import com.google.gwt.user.client.ui.ValueBoxBase;
25+
import gwt.material.design.client.constants.Autocomplete;
2526
import gwt.material.design.client.constants.StatusDisplayType;
2627
import gwt.material.design.client.ui.base.AbstractValueWidgetTest;
2728

@@ -120,14 +121,18 @@ protected <W extends MaterialValueBox> void checkMandatoryField(W widget) {
120121
protected <W extends MaterialValueBox> void checkAutocomplete(W widget) {
121122
ValueBoxBase valueBoxBase = widget.getValueBoxBase();
122123

123-
widget.setAutocomplete(true);
124-
assertTrue(widget.isAutocomplete());
124+
widget.setAutocomplete(Autocomplete.ON);
125+
assertEquals(Autocomplete.ON, widget.getAutocomplete());
125126
assertEquals(valueBoxBase.getElement().getAttribute("autocomplete"), "on");
126127

127-
widget.setAutocomplete(false);
128-
assertFalse(widget.isAutocomplete());
128+
widget.setAutocomplete(Autocomplete.OFF);
129+
assertEquals(Autocomplete.OFF, widget.getAutocomplete());
129130
assertEquals(valueBoxBase.getElement().getAttribute("autocomplete"), "off");
130131

132+
widget.setAutocomplete(Autocomplete.NEW_PASSWORD);
133+
assertEquals(Autocomplete.NEW_PASSWORD, widget.getAutocomplete());
134+
assertEquals(valueBoxBase.getElement().getAttribute("autocomplete"), "new-password");
135+
131136
valueBoxBase.getElement().removeAttribute("autocomplete");
132137
}
133138

0 commit comments

Comments
 (0)