Skip to content

Commit c622a15

Browse files
authored
fix: Always set image alt text the same way (#15757)
Fixes #15756
1 parent 5734f85 commit c622a15

File tree

2 files changed

+18
-17
lines changed
  • flow-html-components/src

2 files changed

+18
-17
lines changed

flow-html-components/src/main/java/com/vaadin/flow/component/html/Image.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
public class Image extends HtmlContainer
3737
implements ClickNotifier<Image>, HasAriaLabel {
3838

39+
private static final String ALT_ATTRIBUTE = "alt";
3940
private static final PropertyDescriptor<String, String> srcDescriptor = PropertyDescriptors
4041
.attributeWithDefault("src", "");
4142

42-
private static final PropertyDescriptor<String, Optional<String>> altDescriptor = PropertyDescriptors
43-
.optionalAttributeWithDefault("alt", "");
44-
4543
/**
4644
* Creates a new empty image.
4745
*/
@@ -65,7 +63,7 @@ public Image() {
6563
*/
6664
public Image(String src, String alt) {
6765
setSrc(src);
68-
getElement().setProperty("alt", alt);
66+
setAlt(alt);
6967
}
7068

7169
/**
@@ -84,7 +82,7 @@ public Image(String src, String alt) {
8482
*/
8583
public Image(AbstractStreamResource src, String alt) {
8684
setSrc(src);
87-
getElement().setProperty("alt", alt);
85+
setAlt(alt);
8886
}
8987

9088
/**
@@ -123,7 +121,12 @@ public void setSrc(AbstractStreamResource src) {
123121
* the alternate text
124122
*/
125123
public void setAlt(String alt) {
126-
set(altDescriptor, alt);
124+
if (alt == null) {
125+
getElement().removeAttribute(ALT_ATTRIBUTE);
126+
} else {
127+
// Also an empty string should be set as alt
128+
getElement().setAttribute(ALT_ATTRIBUTE, alt);
129+
}
127130
}
128131

129132
/**
@@ -133,6 +136,6 @@ public void setAlt(String alt) {
133136
* text has been set
134137
*/
135138
public Optional<String> getAlt() {
136-
return get(altDescriptor);
139+
return Optional.ofNullable(getElement().getAttribute(ALT_ATTRIBUTE));
137140
}
138141
}

flow-html-components/src/test/java/com/vaadin/flow/component/html/ImageTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.vaadin.flow.component.html;
1717

18+
import java.util.Optional;
19+
1820
import org.junit.Assert;
1921
import org.junit.Test;
2022

@@ -24,7 +26,6 @@ public class ImageTest extends ComponentTest {
2426

2527
@Override
2628
protected void addProperties() {
27-
addOptionalStringProperty("alt");
2829
addStringProperty("src", "");
2930
}
3031

@@ -35,15 +36,12 @@ public void testHasAriaLabelIsImplemented() {
3536
}
3637

3738
@Test
38-
public void setEmptyAltInConstructor_altPropertExists() {
39+
public void emptyAltKeepsAttribute() {
3940
Image img = new Image("test.png", "");
40-
Assert.assertTrue(
41-
"'alt' property should have been retained with constructor",
42-
img.getElement().hasProperty("alt"));
43-
44-
img.setAlt("");
45-
46-
Assert.assertTrue("'alt' property should have been cleared with setAlt",
47-
img.getElement().hasProperty("alt"));
41+
Assert.assertEquals("", img.getAlt().get());
42+
Assert.assertTrue(img.getElement().hasAttribute("alt"));
43+
img.setAlt(null);
44+
Assert.assertEquals(Optional.empty(), img.getAlt());
45+
Assert.assertFalse(img.getElement().hasAttribute("alt"));
4846
}
4947
}

0 commit comments

Comments
 (0)