diff options
author | Vitaly Takmazov | 2018-11-12 13:31:17 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-11-12 13:31:17 +0300 |
commit | 830f62be6710265d4731276eeddf001fc3e0f80d (patch) | |
tree | 77f48ddbaf614b357914a0bf8a47814c2ba4861c /src | |
parent | 0020a6561f537db248cff6630346bde8f20aaa1f (diff) |
parse entities before html escaping
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/juick/util/MessageUtils.java | 11 | ||||
-rw-r--r-- | src/test/java/com/juick/MessageTest.java | 10 |
2 files changed, 10 insertions, 11 deletions
diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 8db8148c..4d31a6cd 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -63,7 +63,7 @@ public class MessageUtils { private final static String urlWithWhitespacesRegex = urlWhiteSpacePrefix + urlRegex; - private final static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?<!>))*)([\\]\\}]|>)"); + private final static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?<!\\>))*)([\\]\\}]|\\>)"); private final static String replyNumberRegex = "((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))"; @@ -87,7 +87,7 @@ public class MessageUtils { private final static String underlineRegex = "((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))"; - private final static String citateRegex = "(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))"; + private final static String citateRegex = "(?:(?<=\\n)|(?<=\\A))\\> *(.*)?(\\n|(?=\\Z))"; public static List<Entity> getEntities(Message msg) { @@ -144,10 +144,6 @@ public class MessageUtils { } public static String formatMessage(String msg) { - msg = msg.replaceAll("&", "&"); - msg = msg.replaceAll("<", "<"); - msg = msg.replaceAll(">", ">"); - // -- // — msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); @@ -209,6 +205,9 @@ public class MessageUtils { msg = msg.replaceAll(citateRegex, "<q>$1</q>"); msg = msg.replaceAll("</q><q>", "\n"); + msg = msg.replaceAll("&", "&"); + msg = msg.replaceAll("<", "<"); + msg = msg.replaceAll(">", ">"); msg = msg.replaceAll("\n", "<br/>\n"); return msg; } diff --git a/src/test/java/com/juick/MessageTest.java b/src/test/java/com/juick/MessageTest.java index f76054cd..1d59a8db 100644 --- a/src/test/java/com/juick/MessageTest.java +++ b/src/test/java/com/juick/MessageTest.java @@ -24,10 +24,7 @@ import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Test; -import java.util.ArrayList; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -187,9 +184,12 @@ public class MessageTest { } @Test public void entitiesTest() { - String msg = "http://google.com - there will @ugnich ask questions from #4321, then go to http://stackoverflow.com"; + String msg = "> how ?\nhttp://google.com - there _will_ @ugnich ask questions from #4321, then go to [stackoverflow](http://stackoverflow.com)"; Message testMessage = MockUtils.mockMessage(514, MockUtils.mockUser(5432, "fmap", "secret"), msg); List<Entity> entities = MessageUtils.getEntities(testMessage); - assertThat(entities.size(), is(4)); + assertThat(entities.size(), is(6)); + assertThat(entities.stream().filter(e -> e.getType().equals("q")).count(), is(1L)); + assertThat(entities.stream().filter(e -> e.getType().equals("u")).count(), is(1L)); + assertThat(entities.stream().filter(e -> e.getType().equals("a")).count(), is(4L)); } } |