Skip to content

Commit

Permalink
fix: Empty alt in costructor should add property (#15292) (#15300)
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
  • Loading branch information
caalador authored Nov 29, 2022
1 parent ed13687 commit d39945e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -60,11 +63,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 @@ -76,7 +82,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,9 @@
*/
package com.vaadin.flow.component.html;

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

public class ImageTest extends ComponentTest {

// Actual test methods in super class
Expand All @@ -25,4 +28,16 @@ protected void addProperties() {
addStringProperty("src", "");
}

@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 d39945e

Please sign in to comment.