Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> REPLACEMENTS;

Expand Down Expand Up @@ -83,8 +90,9 @@ public class SmileysPlugin implements WeblogEntryPlugin {
List<String> 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);
}


Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public void titleIsEscapedAndNotMatchedAgain() {
+ "<span class=\"smiley\" title=\":&quot;&gt;\">&#x1F60A;</span>", html);
}

@Test
public void emoticonsInsideTagsAreLeftAlone() {
// an Instagram embed's SVG has xmlns:xlink, which holds ":x"
String svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" "
+ "xmlns:xlink=\"http://www.w3.org/1999/xlink\"><g></g></svg>";
assertEquals(svg, plugin.render(null, svg));
assertEquals("<a title=\"see :-)\" href=\"/x\">hi "
+ "<span class=\"smiley\" title=\":-)\">&#x1F642;</span></a>",
plugin.render(null, "<a title=\"see :-)\" href=\"/x\">hi :-)</a>"));
assertEquals("<!-- :-) -->", plugin.render(null, "<!-- :-) -->"));
}

@Test
public void lessThanInTextDoesNotHideEmoticons() {
assertEquals("1 < 2 <span class=\"smiley\" title=\":-)\">&#x1F642;</span>",
plugin.render(null, "1 < 2 :-)"));
}

@Test
public void textWithoutEmoticonsIsUnchanged() {
String text = "<p>No faces here.</p>";
Expand Down
Loading