fix when pattern like this 'foo\r\r\n', output will be empty

by @friparia

Some issues have been detected in this pull request

Issues that can be fixed by applying a patch

Review the proposed patch then download it to apply it manually or execute the following command from the repository root directory:

curl https://fabbot.io/patch/sensiolabs/ansi-to-html/21/34c7f21e9ce521e9f877d9a95e25727e1849074e/cs.diff | patch -p0
diff -ru SensioLabs/AnsiConverter/AnsiToHtmlConverter.php SensioLabs/AnsiConverter/AnsiToHtmlConverter.php
--- SensioLabs/AnsiConverter/AnsiToHtmlConverter.php	2020-01-13 08:09:35.503344920 +0000
+++ SensioLabs/AnsiConverter/AnsiToHtmlConverter.php	2020-01-13 08:09:36.061322574 +0000
@@ -30,11 +30,11 @@
         $this->inlineStyles = $inlineStyles;
         $this->charset = $charset;
         $this->inlineColors = $this->theme->asArray();
-        $this->colorNames = array(
+        $this->colorNames = [
             'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
             '', '',
             'brblack', 'brred', 'brgreen', 'bryellow', 'brblue', 'brmagenta', 'brcyan', 'brwhite',
-        );
+        ];
     }
 
     public function convert($text)
@@ -44,7 +44,7 @@
         // remove character set sequences
         $text = preg_replace('#\e(\(|\))(A|B|[0-2])#', '', $text);
 
-        $text = htmlspecialchars($text, PHP_VERSION_ID >= 50400 ? ENT_QUOTES | ENT_SUBSTITUTE : ENT_QUOTES, $this->charset);
+        $text = htmlspecialchars($text, \PHP_VERSION_ID >= 50400 ? ENT_QUOTES | ENT_SUBSTITUTE : ENT_QUOTES, $this->charset);
 
         // carriage return
         $text = preg_replace('#\r+#m', "\r", $text);
@@ -57,7 +57,7 @@
             if ('backspace' == $token[0]) {
                 $j = $i;
                 while (--$j >= 0) {
-                    if ('text' == $tokens[$j][0] && strlen($tokens[$j][1]) > 0) {
+                    if ('text' == $tokens[$j][0] && \strlen($tokens[$j][1]) > 0) {
                         $tokens[$j][1] = substr($tokens[$j][1], 0, -1);
 
                         break;
@@ -113,16 +113,16 @@
             }
 
             // options: bold => 1, underscore => 4, blink => 5, reverse => 7, conceal => 8
-            if (in_array(1, $options)) {
+            if (\in_array(1, $options)) {
                 $fg += 10;
                 $bg += 10;
             }
 
-            if (in_array(4, $options)) {
+            if (\in_array(4, $options)) {
                 $as = '; text-decoration: underline';
             }
 
-            if (in_array(7, $options)) {
+            if (\in_array(7, $options)) {
                 $tmp = $fg;
                 $fg = $bg;
                 $bg = $tmp;
@@ -138,19 +138,19 @@
 
     protected function tokenize($text)
     {
-        $tokens = array();
+        $tokens = [];
         preg_match_all("/(?:\e\[(.*?)m|(\x08))/", $text, $matches, PREG_OFFSET_CAPTURE);
 
         $offset = 0;
         foreach ($matches[0] as $i => $match) {
             if ($match[1] - $offset > 0) {
-                $tokens[] = array('text', substr($text, $offset, $match[1] - $offset));
+                $tokens[] = ['text', substr($text, $offset, $match[1] - $offset)];
             }
-            $tokens[] = array("\x08" == $match[0] ? 'backspace' : 'color', $matches[1][$i][0]);
-            $offset = $match[1] + strlen($match[0]);
+            $tokens[] = ["\x08" == $match[0] ? 'backspace' : 'color', $matches[1][$i][0]];
+            $offset = $match[1] + \strlen($match[0]);
         }
-        if ($offset < strlen($text)) {
-            $tokens[] = array('text', substr($text, $offset));
+        if ($offset < \strlen($text)) {
+            $tokens[] = ['text', substr($text, $offset)];
         }
 
         return $tokens;
diff -ru SensioLabs/AnsiConverter/Tests/AnsiToHtmlConverterTest.php SensioLabs/AnsiConverter/Tests/AnsiToHtmlConverterTest.php
--- SensioLabs/AnsiConverter/Tests/AnsiToHtmlConverterTest.php	2020-01-13 08:09:35.707336750 +0000
+++ SensioLabs/AnsiConverter/Tests/AnsiToHtmlConverterTest.php	2020-01-13 08:09:36.082321733 +0000
@@ -26,35 +26,35 @@
 
     public function getConvertData()
     {
-        return array(
+        return [
             // text is escaped
-            array('<span style="background-color: black; color: white">foo &lt;br /&gt;</span>', 'foo <br />'),
+            ['<span style="background-color: black; color: white">foo &lt;br /&gt;</span>', 'foo <br />'],
 
             // newlines are preserved
-            array("<span style=\"background-color: black; color: white\">foo\nbar</span>", "foo\nbar"),
+            ["<span style=\"background-color: black; color: white\">foo\nbar</span>", "foo\nbar"],
 
             // backspaces
-            array('<span style="background-color: black; color: white">foo   </span>', "foobar\x08\x08\x08   "),
-            array('<span style="background-color: black; color: white">foo</span><span style="background-color: black; color: white">   </span>', "foob\e[31;41ma\e[0mr\x08\x08\x08   "),
+            ['<span style="background-color: black; color: white">foo   </span>', "foobar\x08\x08\x08   "],
+            ['<span style="background-color: black; color: white">foo</span><span style="background-color: black; color: white">   </span>', "foob\e[31;41ma\e[0mr\x08\x08\x08   "],
 
             // color
-            array('<span style="background-color: darkred; color: darkred">foo</span>', "\e[31;41mfoo\e[0m"),
+            ['<span style="background-color: darkred; color: darkred">foo</span>', "\e[31;41mfoo\e[0m"],
 
             // color with [m as a termination (equivalent to [0m])
-            array('<span style="background-color: darkred; color: darkred">foo</span>', "\e[31;41mfoo\e[m"),
+            ['<span style="background-color: darkred; color: darkred">foo</span>', "\e[31;41mfoo\e[m"],
 
             // bright color
-            array('<span style="background-color: red; color: red">foo</span>', "\e[31;41;1mfoo\e[0m"),
+            ['<span style="background-color: red; color: red">foo</span>', "\e[31;41;1mfoo\e[0m"],
 
             // carriage returns
-            array('<span style="background-color: black; color: white">foobar\r\nbar</span>', "foo\r\r\nbar"),
-            array('<span style="background-color: black; color: white">foobar</span>', "foo\rbar\rfoobar"),
+            ['<span style="background-color: black; color: white">foobar\r\nbar</span>', "foo\r\r\nbar"],
+            ['<span style="background-color: black; color: white">foobar</span>', "foo\rbar\rfoobar"],
 
             // underline
-            array('<span style="background-color: black; color: white; text-decoration: underline">foo</span>', "\e[4mfoo\e[0m"),
+            ['<span style="background-color: black; color: white; text-decoration: underline">foo</span>', "\e[4mfoo\e[0m"],
 
             // non valid unicode codepoints substitution (only available with PHP >= 5.4)
-            PHP_VERSION_ID < 50400 ?: array('<span style="background-color: black; color: white">foo '."\xEF\xBF\xBD".'</span>', "foo \xF4\xFF\xFF\xFF"),
-        );
+            \PHP_VERSION_ID < 50400 ?: ['<span style="background-color: black; color: white">foo '."\xEF\xBF\xBD".'</span>', "foo \xF4\xFF\xFF\xFF"],
+        ];
     }
 }

0
Common Typos

0
File Permissions

0
Merge Commits