From aac111f507ca86140055ceead27b399afcc811dc Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Sun, 15 Nov 2026 18:24:12 -0700 Subject: [PATCH] Keep the Emoticons plugin out of HTML tags and comments The plugin replaced emoticons anywhere in an entry, so the :x in xmlns:xlink inside an embedded SVG became an emoji. The single-pass pattern now also matches tags and comments and copies them unchanged. --- CHANGES.md | 3 +++ .../business/plugins/entry/SmileysPlugin.java | 19 ++++++++++++++----- .../plugins/entry/SmileysPluginTest.java | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 31aff819b..d55576e8a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -133,6 +133,9 @@ - The Theme and Create Weblog pages work with a shared theme whose `theme.xml` names a preview image that isn't there; they show no preview instead of failing to load the theme's description. + - The Emoticons plugin no longer changes text inside HTML tags or + comments, so the `:x` in an attribute such as `xmlns:xlink` stays as it + is; in an embedded SVG it used to become an emoji. ### Behaviour changes worth reading before upgrading diff --git a/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java b/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java index e84bbb02f..ec8498445 100644 --- a/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java +++ b/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java @@ -51,9 +51,16 @@ public class SmileysPlugin implements WeblogEntryPlugin { private static final String DESCRIPTION = "Change ASCII emoticons to emoji. :-) becomes a smiling face."; - /** Matches any of the emoticons, longest first. */ + /** + * Matches an HTML tag or comment (group 1), which is kept as it is, or any + * of the emoticons, longest first. Without the tags, ":x" inside an + * attribute such as xmlns:xlink would turn into an emoji. + */ private static final Pattern SMILEY_PATTERN; + /** A comment, or a tag: "<" then a letter, "/" or "!", up to ">". */ + private static final String TAG = "(|<[A-Za-z/!][^>]*>)"; + /** The markup that replaces each emoticon. */ private static final Map REPLACEMENTS; @@ -83,8 +90,9 @@ public class SmileysPlugin implements WeblogEntryPlugin { List smileys = new ArrayList<>(replacements.keySet()); smileys.sort((a, b) -> b.length() != a.length() ? b.length() - a.length() : a.compareTo(b)); - SMILEY_PATTERN = smileys.isEmpty() ? null : Pattern.compile( - smileys.stream().map(Pattern::quote).collect(Collectors.joining("|"))); + SMILEY_PATTERN = smileys.isEmpty() ? null : Pattern.compile(TAG + "|" + + smileys.stream().map(Pattern::quote).collect(Collectors.joining("|")), + Pattern.DOTALL); } @@ -122,8 +130,9 @@ public String render(WeblogEntry entry, String text) { Matcher matcher = SMILEY_PATTERN.matcher(text); StringBuilder result = new StringBuilder(text.length()); while (matcher.find()) { - matcher.appendReplacement(result, - Matcher.quoteReplacement(REPLACEMENTS.get(matcher.group()))); + String replacement = matcher.group(1) != null + ? matcher.group(1) : REPLACEMENTS.get(matcher.group()); + matcher.appendReplacement(result, Matcher.quoteReplacement(replacement)); } matcher.appendTail(result); return result.toString(); diff --git a/app/src/test/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPluginTest.java b/app/src/test/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPluginTest.java index c642c492f..4236298b5 100644 --- a/app/src/test/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPluginTest.java +++ b/app/src/test/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPluginTest.java @@ -49,6 +49,24 @@ public void titleIsEscapedAndNotMatchedAgain() { + "😊", html); } + @Test + public void emoticonsInsideTagsAreLeftAlone() { + // an Instagram embed's SVG has xmlns:xlink, which holds ":x" + String svg = ""; + assertEquals(svg, plugin.render(null, svg)); + assertEquals("hi " + + "🙂", + plugin.render(null, "hi :-)")); + assertEquals("", plugin.render(null, "")); + } + + @Test + public void lessThanInTextDoesNotHideEmoticons() { + assertEquals("1 < 2 🙂", + plugin.render(null, "1 < 2 :-)")); + } + @Test public void textWithoutEmoticonsIsUnchanged() { String text = "

No faces here.

";