Skip to content

Commit

Permalink
fix: Empty alt in costructor should add property (#15292) (#15297)
Browse files Browse the repository at this point in the history
Giving the alternate text in the constructor
should generate a property to the image.

Fixes #14880

Co-authored-by: caalador <[email protected]>
  • Loading branch information
vaadin-bot and caalador authored Nov 28, 2022
1 parent 9ca426e commit 312a59f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public Image() {

/**
* Creates an image with the given URL and an alternative text.
* <p>
* The alternative text given to constructor is always set even if it is the
* default empty string which is not retained with {@link #setAlt(String)}.
*
* @param src
* the image URL
Expand All @@ -62,11 +65,14 @@ public Image() {
*/
public Image(String src, String alt) {
setSrc(src);
setAlt(alt);
getElement().setProperty("alt", alt);
}

/**
* Creates an image with the given stream resource and an alternative text.
* <p>
* The alternative text given to constructor is always set even if it is the
* default empty string which is not retained with {@link #setAlt(String)}.
*
* @param src
* the resource value, not null
Expand All @@ -78,7 +84,7 @@ public Image(String src, String alt) {
*/
public Image(AbstractStreamResource src, String alt) {
setSrc(src);
setAlt(alt);
getElement().setProperty("alt", alt);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.vaadin.flow.component.html;

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

public class ImageTest extends ComponentTest {
Expand All @@ -32,4 +33,17 @@ protected void addProperties() {
public void testHasAriaLabelIsImplemented() {
super.testHasAriaLabelIsImplemented();
}

@Test
public void setEmptyAltInConstructor_altPropertExists() {
Image img = new Image("test.png", "");
Assert.assertTrue(
"'alt' property should have been retained with constructor",
img.getElement().hasProperty("alt"));

img.setAlt("");

Assert.assertTrue("'alt' property should have been cleared with setAlt",
img.getElement().hasProperty("alt"));
}
}

0 comments on commit 312a59f

Please sign in to comment.