Skip to content

Commit

Permalink
Revert "fix: remove href attribute for disabled Anchor (#11121)" (#11623
Browse files Browse the repository at this point in the history
) (#11632)

This reverts commit 97db70e.
  • Loading branch information
Denis authored Aug 25, 2021
1 parent d12e31d commit 5dfc303
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public class Anchor extends HtmlContainer implements Focusable<Anchor> {
.optionalAttributeWithDefault("target",
AnchorTarget.DEFAULT.getValue());

private String disabledHref;
private AbstractStreamResource resource;

/**
* Creates a new empty anchor component.
*/
Expand Down Expand Up @@ -157,7 +154,6 @@ public void removeHref() {
* the resource value, not null
*/
public void setHref(AbstractStreamResource href) {
resource = href;
getElement().setAttribute("href", href);
}

Expand Down Expand Up @@ -249,36 +245,4 @@ public AnchorTargetValue getTargetValue() {
return AnchorTarget.DEFAULT;
}

@Override
public void onEnabledStateChanged(boolean enabled) {
super.onEnabledStateChanged(enabled);
if (enabled) {
if (resource == null) {
restoreHref();
} else {
restoreResource();
}
disabledHref = null;
} else {
disabledHref = getHref();
removeHref();
}
}

private void restoreHref() {
if (!getElement().hasAttribute("href")) {
// only set attribute value back if attribute has not been set
// somehow when component was disabled
setHref(disabledHref);
}
}

private void restoreResource() {
if (!getElement().hasAttribute("href")) {
// only set attribute value back if attribute has not been set
// somehow when component was disabled
setHref(resource);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,13 @@

import java.util.Optional;

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

import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.server.AbstractStreamResource;

public class AnchorTest extends ComponentTest {

private UI ui;

@After
public void tearDown() {
ui = null;
UI.setCurrent(null);
}

@Test
public void removeHref() {
Anchor anchor = new Anchor();
Expand Down Expand Up @@ -110,120 +99,6 @@ public void getTargetValue_useSomeStringValue_targetIsReturned() {
Assert.assertEquals("foo", anchor.getTargetValue().getValue());
}

@Test
public void disabledAnchor_hrefIsRemoved_enableAnchor_hrefIsRestored() {
Anchor anchor = new Anchor("foo", "bar");
anchor.setEnabled(false);

Assert.assertFalse(anchor.getElement().hasAttribute("href"));

anchor.setEnabled(true);
Assert.assertTrue(anchor.getElement().hasAttribute("href"));
Assert.assertEquals("foo", anchor.getHref());
}

@Test
public void disabledAnchor_setHrefWhenDisabled_enableAnchor_hrefIsPreserved() {
Anchor anchor = new Anchor("foo", "bar");
anchor.setEnabled(false);

anchor.setHref("baz");

anchor.setEnabled(true);

Assert.assertTrue(anchor.getElement().hasAttribute("href"));
Assert.assertEquals("baz", anchor.getHref());
}

@Test
public void disabledAnchor_setResourceWhenDisabled_enableAnchor_resourceIsPreserved() {
Anchor anchor = new Anchor("foo", "bar");
anchor.setEnabled(false);

mockUI();
anchor.setHref(new AbstractStreamResource() {

@Override
public String getName() {
return "baz";
}
});
String href = anchor.getHref();

anchor.setEnabled(true);

Assert.assertTrue(anchor.getElement().hasAttribute("href"));
Assert.assertEquals(href, anchor.getHref());
}

@Test
public void disabledAnchor_setResource_hrefIsRemoved_enableAnchor_hrefIsRestored() {
mockUI();
AbstractStreamResource resource = new AbstractStreamResource() {

@Override
public String getName() {
return "foo";
}

};
Anchor anchor = new Anchor(resource, "bar");
String href = anchor.getHref();
anchor.setEnabled(false);

Assert.assertFalse(anchor.getElement().hasAttribute("href"));

anchor.setEnabled(true);
Assert.assertEquals(href, anchor.getHref());
}

@Test
public void disabledAnchor_setResourceWhenDisabled_hrefIsPreserved() {
mockUI();
AbstractStreamResource resource = new AbstractStreamResource() {

@Override
public String getName() {
return "foo";
}

};
Anchor anchor = new Anchor(resource, "bar");
String href = anchor.getHref();
anchor.setEnabled(false);

anchor.setHref(new AbstractStreamResource() {

@Override
public String getName() {
return "baz";
}
});

Assert.assertTrue(anchor.getElement().hasAttribute("href"));
Assert.assertNotEquals(href, anchor.getHref());
}

@Test
public void disabledAnchor_setResource_setHrefViaElementWhenDisabled_enableAnchor_hrefIsRestored() {
mockUI();
AbstractStreamResource resource = new AbstractStreamResource() {

@Override
public String getName() {
return "foo";
}

};
Anchor anchor = new Anchor(resource, "bar");
anchor.setEnabled(false);

anchor.getElement().setAttribute("href", "baz");

anchor.setEnabled(true);
Assert.assertEquals("baz", anchor.getHref());
}

// Other test methods in super class

@Override
Expand All @@ -232,9 +107,4 @@ protected void addProperties() {
addOptionalStringProperty("target");
}

private void mockUI() {
ui = new UI();
UI.setCurrent(ui);
}

}

0 comments on commit 5dfc303

Please sign in to comment.