File tree Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,7 @@ public function parseTweet($tweet)
6161 return new ParseResults ();
6262 }
6363
64+ $ tweet = StringUtils::normalizeLineFeed ($ tweet );
6465 $ normalizedTweet = StringUtils::normalizeFromNFC ($ tweet );
6566 $ normalizedTweetLength = StringUtils::strlen ($ normalizedTweet );
6667
Original file line number Diff line number Diff line change @@ -178,4 +178,13 @@ public static function charCount($string, $encoding = 'UTF-8')
178178
179179 return $ count ;
180180 }
181+
182+ /**
183+ * @param string $string
184+ * @return string
185+ */
186+ public static function normalizeLineFeed (string $ string ): string
187+ {
188+ return str_replace ("\r\n" , "\n" , $ string );
189+ }
181190}
Original file line number Diff line number Diff line change @@ -243,4 +243,24 @@ public function testParseTweetWithEmojiNumberWithKeycapWithoutVariantSelector()
243243 $ this ->assertSame (0 , $ result ->validRangeStart );
244244 $ this ->assertSame (1 , $ result ->validRangeEnd );
245245 }
246+
247+ /**
248+ * test for parseTweet with Carriage Return characters
249+ */
250+ public function testParseTweetWithCarriageReturn (): void
251+ {
252+ // @codingStandardsIgnoreStart
253+ $ text = "We're expanding the character limit! We want it to be easier and faster for everyone to express themselves. \r\n\r\nMore characters. More expression. More of what's happening. \r\nhttps://cards.twitter.com/cards/gsby/4ztbu " ;
254+ // @codingStandardsIgnoreEnd
255+ $ result = $ this ->parser ->parseTweet ($ text );
256+
257+ $ this ->assertInstanceOf ('\Twitter\Text\ParseResults ' , $ result );
258+ $ this ->assertSame (192 , $ result ->weightedLength );
259+ $ this ->assertSame (685 , $ result ->permillage );
260+ $ this ->assertSame (true , $ result ->valid );
261+ $ this ->assertSame (0 , $ result ->displayRangeStart );
262+ $ this ->assertSame (210 , $ result ->displayRangeEnd );
263+ $ this ->assertSame (0 , $ result ->validRangeStart );
264+ $ this ->assertSame (210 , $ result ->validRangeEnd );
265+ }
246266}
Original file line number Diff line number Diff line change @@ -39,4 +39,15 @@ public function testCharCountEmoji()
3939 $ this ->assertSame (4 , StringUtils::charCount ('🧕🏾 ' ), 'U+1F9D5 U+1F3FE woman with headscarf with medium-dark skin tone has 4 code point ' );
40 40 $ this ->assertSame (14 , StringUtils::charCount ('🏴 ' ), 'flag (England) has 14 code point ' );
4141 }
42+
43+ /**
44+ * Test for strip carriage return characters
45+ *
46+ * @return void
47+ */
48+ public function testNormalizeLineFeed (): void
49+ {
50+ $ this ->assertSame ("foo \nbar \n" , StringUtils::normalizeLineFeed ("foo \r\nbar \r\n" ), "Strip CR and leave LF " );
51+ $ this ->assertSame ("foo \rbar " , StringUtils::normalizeLineFeed ("foo \rbar " ), "Do not strip CR only " );
52+ }
4253}
You can’t perform that action at this time.
0 commit comments