TweetTextUtils { static void format(FormattedTweetText formattedTweetText, Tweet tweet) { if (TextUtils.isEmpty(tweet.text)) return; final HtmlEntities.Unescaped u = HtmlEntities.HTML40.unescape(tweet.text); final StringBuilder result = new StringBuilder(u.unescaped); adjustIndicesForEscapedChars(formattedTweetText.urlEntities, u.indices); adjustIndicesForEscapedChars(formattedTweetText.mediaEntities, u.indices); adjustIndicesForEscapedChars(formattedTweetText.hashtagEntities, u.indices); adjustIndicesForEscapedChars(formattedTweetText.mentionEntities, u.indices); adjustIndicesForEscapedChars(formattedTweetText.symbolEntities, u.indices); adjustIndicesForSupplementaryChars(result, formattedTweetText); formattedTweetText.text = result.toString(); } private  TweetTextUtils();   }
@Test public void testFormat_singleEscaping() { final FormattedTweetText formattedTweetText = setupAdjustedTweet(); final Tweet tweet = setupTweetToBeFormatted(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals(UNESCAPED_TWEET_TEXT, formattedTweetText.text); assertEquals("Hello", 1, formattedTweetText.urlEntities.get(0).start); assertEquals("Hello", 5, formattedTweetText.urlEntities.get(0).end); assertEquals("There", 7, formattedTweetText.urlEntities.get(1).start); assertEquals("There", 11, formattedTweetText.urlEntities.get(1).end); assertEquals("What", 15, formattedTweetText.urlEntities.get(2).start); assertEquals("What", 18, formattedTweetText.urlEntities.get(2).end); assertEquals("is", 20, formattedTweetText.urlEntities.get(3).start); assertEquals("is", 21, formattedTweetText.urlEntities.get(3).end); assertEquals("a", 23, formattedTweetText.urlEntities.get(4).start); assertEquals("a", 23, formattedTweetText.urlEntities.get(4).end); }
@Test public void testFormat_htmlEntityEdgeCases() { final FormattedTweetText formattedTweetText = new FormattedTweetText(); Tweet tweet = new TweetBuilder().setText("&amp;").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("&", formattedTweetText.text); tweet = new TweetBuilder().setText("&#;").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("&#;", formattedTweetText.text); tweet = new TweetBuilder().setText("&#34;").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("\"", formattedTweetText.text); tweet = new TweetBuilder().setText("&#x22;").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("\"", formattedTweetText.text); tweet = new TweetBuilder().setText("&lt; & Larry &gt; &").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("< & Larry > &", formattedTweetText.text); tweet = new TweetBuilder().setText("&&amp;").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("&&", formattedTweetText.text); tweet = new TweetBuilder().setText("&&&&&&&&amp;").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("&&&&&&&&", formattedTweetText.text); tweet = new TweetBuilder().setText("&&&&gt&&lt&&amplt;").build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals("&&&&gt&&lt&&amplt;", formattedTweetText.text); }
@Test public void testFormat_withEmojiAndEscapedHtml() { final FormattedTweetText formattedTweetText = new FormattedTweetText(); final UrlEntity url = TestFixtures.newUrlEntity(24, 47); formattedTweetText.urlEntities.add(FormattedUrlEntity.createFormattedUrlEntity(url)); final Tweet tweet = new TweetBuilder() .setText(ESCAPED_TWEET_TEXT_WITH_EMOJI) .build(); TweetTextUtils.format(formattedTweetText, tweet); assertEquals(24, formattedTweetText.urlEntities.get(0).start); assertEquals(47, formattedTweetText.urlEntities.get(0).end); }