-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose HtmlComponentSerializer to API and add HtmlStripper
- Loading branch information
Showing
12 changed files
with
149 additions
and
19 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
35 changes: 35 additions & 0 deletions
35
api/src/main/java/xyz/jpenilla/squaremap/api/HtmlComponentSerializer.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,35 @@ | ||
package xyz.jpenilla.squaremap.api; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.flattener.ComponentFlattener; | ||
import net.kyori.adventure.text.serializer.ComponentEncoder; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.framework.qual.DefaultQualifier; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
/** | ||
* Safely encodes {@link Component Components} as HTML text. | ||
* | ||
* <p>Mostly useful for marker tooltips.</p> | ||
* | ||
* @see Squaremap#htmlComponentSerializer() | ||
*/ | ||
@DefaultQualifier(NonNull.class) | ||
public interface HtmlComponentSerializer extends ComponentEncoder<Component, String> { | ||
|
||
/** | ||
* Create a new {@link HtmlComponentSerializer} using the provided {@link ComponentFlattener}. | ||
* | ||
* @param flattener component flattener | ||
* @return serializer | ||
*/ | ||
static HtmlComponentSerializer withFlattener(final ComponentFlattener flattener) { | ||
return ProviderHolder.HTML_SERIALIZER.create(flattener); | ||
} | ||
|
||
@ApiStatus.Internal | ||
interface Provider { | ||
HtmlComponentSerializer create(ComponentFlattener flattener); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
api/src/main/java/xyz/jpenilla/squaremap/api/HtmlStripper.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,36 @@ | ||
package xyz.jpenilla.squaremap.api; | ||
|
||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.framework.qual.DefaultQualifier; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
/** | ||
* Strips HTML tags from text. Allows sanitizing untrusted strings before use in | ||
* marker tooltips. | ||
*/ | ||
@DefaultQualifier(NonNull.class) | ||
public interface HtmlStripper { | ||
|
||
/** | ||
* Get an {@link HtmlStripper}. | ||
* | ||
* @return HTML stripper | ||
*/ | ||
static HtmlStripper htmlStripper() { | ||
return ProviderHolder.HTML_STRIPPER.instance(); | ||
} | ||
|
||
/** | ||
* Strips HTML tags from the provided string. | ||
* | ||
* @param string untrusted string | ||
* @return sanitized string | ||
*/ | ||
String stripHtml(String string); | ||
|
||
@ApiStatus.Internal | ||
interface Provider { | ||
HtmlStripper instance(); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
api/src/main/java/xyz/jpenilla/squaremap/api/ProviderHolder.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,13 @@ | ||
package xyz.jpenilla.squaremap.api; | ||
|
||
import net.kyori.adventure.util.Services; | ||
|
||
final class ProviderHolder { | ||
static final HtmlComponentSerializer.Provider HTML_SERIALIZER = service(HtmlComponentSerializer.Provider.class); | ||
static final HtmlStripper.Provider HTML_STRIPPER = service(HtmlStripper.Provider.class); | ||
|
||
private static <T> T service(final Class<T> clazz) { | ||
return Services.service(clazz) | ||
.orElseThrow(() -> new IllegalStateException("Could not find " + clazz.getName() + " implementation")); | ||
} | ||
} |
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
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
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
15 changes: 0 additions & 15 deletions
15
common/src/main/java/xyz/jpenilla/squaremap/common/util/HtmlComponentSerializer.java
This file was deleted.
Oops, something went wrong.
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
31 changes: 31 additions & 0 deletions
31
common/src/main/java/xyz/jpenilla/squaremap/common/util/HtmlStripperImpl.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,31 @@ | ||
package xyz.jpenilla.squaremap.common.util; | ||
|
||
import java.util.Objects; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.framework.qual.DefaultQualifier; | ||
import org.owasp.html.HtmlPolicyBuilder; | ||
import org.owasp.html.PolicyFactory; | ||
import xyz.jpenilla.squaremap.api.HtmlStripper; | ||
|
||
@DefaultQualifier(NonNull.class) | ||
public final class HtmlStripperImpl implements HtmlStripper { | ||
private static final PolicyFactory SANITIZER = new HtmlPolicyBuilder().toFactory(); | ||
|
||
private HtmlStripperImpl() { | ||
} | ||
|
||
@Override | ||
public String stripHtml(final String string) { | ||
Objects.requireNonNull(string, "Parameter 'string' must not be null"); | ||
return SANITIZER.sanitize(string); | ||
} | ||
|
||
public static final class Provider implements HtmlStripper.Provider { | ||
private static final HtmlStripper INSTANCE = new HtmlStripperImpl(); | ||
|
||
@Override | ||
public HtmlStripper instance() { | ||
return INSTANCE; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...n/resources/META-INF/services/xyz.jpenilla.squaremap.api.HtmlComponentSerializer$Provider
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 @@ | ||
xyz.jpenilla.squaremap.common.util.HtmlComponentSerializerImpl$Provider |
1 change: 1 addition & 0 deletions
1
common/src/main/resources/META-INF/services/xyz.jpenilla.squaremap.api.HtmlStripper$Provider
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 @@ | ||
xyz.jpenilla.squaremap.common.util.HtmlStripperImpl$Provider |