Skip to content

Commit 9ac5271

Browse files
committed
Fix handling of iconSize
Fixes #246 See also Leaflet/Leaflet#4238
1 parent de207aa commit 9ac5271

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## 3.0.1
2+
* Fixed ``divIcon`` being displayed incorrectly [due to a default ``iconSize`` of ``12px``](https://github.com/Leaflet/Leaflet/issues/4238) #246
23
* Updated dependencies
34
* Updated leaflet to [``1.9.4``](https://github.com/Leaflet/Leaflet/blob/b6b56f6e707142c177fad2f67827a5007e56736a/CHANGELOG.md#194-2023-05-18) #247
45

vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/flow/data/LDivIcon.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public LDivIcon()
3030

3131
/**
3232
* <b>Ensure that you escape the html properly so that you don't accidentally introduce Serverside XSS</b>
33-
*
34-
* @param html
3533
*/
3634
public LDivIcon(final String html)
3735
{
3836
this.setHtml(html);
3937
this.setIconAnchor(0, 0);
38+
// https://github.com/Leaflet/Leaflet/issues/4238
39+
this.setIconSize(null);
4040
}
4141

4242
public String getClassName()
@@ -46,8 +46,6 @@ public String getClassName()
4646

4747
/**
4848
* Sets a className for CSS formating.
49-
*
50-
* @param className
5149
*/
5250
public void setClassName(final String className)
5351
{
@@ -63,8 +61,6 @@ public String getHtml()
6361
* Custom HTML code to put inside the div element, empty by default. Alternatively, an instance of HTMLElement.
6462
* <br/>
6563
* <b>Ensure that you escape the html properly so that you don't accidentally introduce Serverside XSS</b>
66-
*
67-
* @param html
6864
*/
6965
public void setHtml(final String html)
7066
{

vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/flow/data/LIcon.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919

2020
import java.util.ArrayList;
21+
import java.util.Collection;
2122
import java.util.List;
23+
import java.util.Optional;
2224

2325
import com.fasterxml.jackson.annotation.JsonInclude;
2426
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -28,8 +30,8 @@ public class LIcon
2830
{
2931
private String iconUrl;
3032

31-
@JsonInclude(Include.NON_EMPTY)
32-
private final List<Integer> iconSize = new ArrayList<>();
33+
@JsonInclude(value = Include.CUSTOM, valueFilter = IconSizeFilter.class)
34+
private List<Integer> iconSize = new ArrayList<>();
3335
private final List<Integer> iconAnchor = new ArrayList<>();
3436
private final List<Integer> popupAnchor = new ArrayList<>();
3537
private String shadowUrl;
@@ -68,8 +70,6 @@ public String getIconUrl()
6870

6971
/**
7072
* Sets a icon url.
71-
*
72-
* @param iconUrl
7373
*/
7474
public void setIconUrl(final String iconUrl)
7575
{
@@ -83,15 +83,15 @@ public List<Integer> getIconSize()
8383

8484
/**
8585
* Icon size with x, y in px
86-
*
87-
* @param x
88-
* @param y
8986
*/
9087
public void setIconSize(final int x, final int y)
9188
{
92-
this.iconSize.clear();
93-
this.iconSize.add(x);
94-
this.iconSize.add(y);
89+
this.setIconSize(new ArrayList<>(List.of(x, y)));
90+
}
91+
92+
public void setIconSize(final List<Integer> iconSize)
93+
{
94+
this.iconSize = iconSize;
9595
}
9696

9797
public List<Integer> getIconAnchor()
@@ -101,9 +101,6 @@ public List<Integer> getIconAnchor()
101101

102102
/**
103103
* Anchor point of the icon in x, y px.
104-
*
105-
* @param x
106-
* @param y
107104
*/
108105
public void setIconAnchor(final int x, final int y)
109106
{
@@ -119,9 +116,6 @@ public List<Integer> getPopupAnchor()
119116

120117
/**
121118
* Anchor point of the Pop-up message in x,y px.
122-
*
123-
* @param x
124-
* @param y
125119
*/
126120
public void setPopupAnchor(final int x, final int y)
127121
{
@@ -164,4 +158,28 @@ public void setShadowAnchor(final int x, final int y)
164158
this.shadowAnchor.add(x);
165159
this.shadowAnchor.add(y);
166160
}
161+
162+
public static class IconSizeFilter
163+
{
164+
@SuppressWarnings("java:S1206") // Filter only uses equals
165+
@Override
166+
public boolean equals(final Object obj)
167+
{
168+
// true = filter out; false = keep
169+
170+
// Null is allowed
171+
if(obj == null)
172+
{
173+
return false;
174+
}
175+
176+
// Filter out empty collection
177+
return Optional.of(obj)
178+
.filter(Collection.class::isInstance)
179+
.map(Collection.class::cast)
180+
.map(Collection::isEmpty)
181+
// Wrong type (not a collection)
182+
.orElse(true);
183+
}
184+
}
167185
}

vaadin-maps-leaflet-flow/src/main/resources/META-INF/resources/frontend/leaflet/leaflet-custom.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
.div-icon {
22
background: #fff;
33
border: 1px solid #666;
4-
height: auto;
5-
width: auto;
64
padding-left: 5px;
75
padding-right: 5px;
86
white-space: nowrap;

0 commit comments

Comments
 (0)