-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Denis Anisimov <[email protected]>
- Loading branch information
Showing
6 changed files
with
335 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
flow-html-components/src/main/java/com/vaadin/flow/component/html/AnchorTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2000-2020 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.html; | ||
|
||
/** | ||
* Enum representing <code>target</code> attribute values for an | ||
* <code><a></code> element. | ||
* | ||
* @author Vaadin Ltd | ||
* @since | ||
*/ | ||
public enum AnchorTarget implements AnchorTargetValue { | ||
/** | ||
* Remove the target value. This has the same effect as <code>SELF</code>. | ||
*/ | ||
DEFAULT(""), | ||
/** | ||
* Open a link in the current context. | ||
*/ | ||
SELF("_self"), | ||
/** | ||
* Open a link in a new unnamed context. | ||
*/ | ||
BLANK("_blank"), | ||
/** | ||
* Open a link in the parent context, or the current context if there is no | ||
* parent context. | ||
*/ | ||
PARENT("_parent"), | ||
/** | ||
* Open a link in the top most grandparent context, or the current context | ||
* if there is no parent context. | ||
*/ | ||
TOP("_top"); | ||
|
||
private final String value; | ||
|
||
/** | ||
* @param value | ||
* the text value to use by an {@code <a>} (anchor) tag. | ||
*/ | ||
AnchorTarget(String value) { | ||
this.value = value; | ||
} | ||
|
||
/** | ||
* @return value the text value to use by an {@code <a>} (anchor) tag. | ||
*/ | ||
@Override | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
flow-html-components/src/main/java/com/vaadin/flow/component/html/AnchorTargetValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.html; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Represents <code>target</code> attribute values for an <code><a></code> | ||
* element. | ||
* | ||
* @author Vaadin Ltd | ||
* @since | ||
* | ||
* @see AnchorTarget | ||
* | ||
*/ | ||
@FunctionalInterface | ||
public interface AnchorTargetValue extends Serializable { | ||
|
||
/** | ||
* Gets the string value representation. | ||
* | ||
* @return string value representation | ||
*/ | ||
String getValue(); | ||
|
||
/** | ||
* Gets an object instance wrapping the {@code value} string representation. | ||
* | ||
* @param value | ||
* the string value representation, not {@code null} | ||
* @return an object wrapping the string value | ||
*/ | ||
public static AnchorTargetValue forString(String value) { | ||
Optional<AnchorTarget> target = Stream.of(AnchorTarget.values()).filter( | ||
type -> type.getValue().equals(Objects.requireNonNull(value))) | ||
.findFirst(); | ||
if (target.isPresent()) { | ||
return target.get(); | ||
} | ||
return new AnchorTargetValue() { | ||
|
||
@Override | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!obj.getClass().equals(getClass())) { | ||
return false; | ||
} | ||
return value.equals(((AnchorTargetValue) obj).getValue()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
}; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
flow-html-components/src/test/java/com/vaadin/flow/component/html/AnchorTargetValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.html; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class AnchorTargetValueTest { | ||
|
||
@Test | ||
public void fromString_notEnum_objectHasValueAndEquals() { | ||
AnchorTargetValue value = AnchorTargetValue.forString("foo"); | ||
Assert.assertEquals("foo", value.getValue()); | ||
|
||
AnchorTargetValue value1 = AnchorTargetValue.forString("foo"); | ||
Assert.assertEquals(value, value1); | ||
Assert.assertEquals(value.hashCode(), value1.hashCode()); | ||
} | ||
|
||
@Test | ||
public void fromString_enumValue_resultIsEnum() { | ||
AnchorTargetValue value = AnchorTargetValue | ||
.forString(AnchorTarget.TOP.getValue()); | ||
Assert.assertEquals(AnchorTarget.TOP, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.