File tree Expand file tree Collapse file tree 2 files changed +12
-4
lines changed Expand file tree Collapse file tree 2 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -27,8 +27,9 @@ class ColorTag
27
27
// regex used for removing color tags
28
28
public const STRIP_TAG = '/<[\/]?[a-zA-Z0-9=;]+>/ ' ;
29
29
30
- // Regex to match tags/
31
- public const MATCH_TAG = '/<([a-zA-Z0-9=;_]+)>(.*)<\/ \\1>/s ' ;
30
+ // Regex to match tags
31
+ // TIP: `?` - 非贪婪匹配; 若不加,会导致有多个相同标签时,第一个开始会匹配到最后一个的关闭
32
+ public const MATCH_TAG = '/<([a-zA-Z0-9=;_]+)>(.*?)<\/ \\1>/s ' ;
32
33
33
34
// color
34
35
public const BLACK = 'black ' ;
Original file line number Diff line number Diff line change 12
12
use PHPUnit \Framework \TestCase ;
13
13
use Toolkit \Cli \Color ;
14
14
use Toolkit \Cli \ColorTag ;
15
+ use function strpos ;
15
16
use function vdump ;
16
17
use const PHP_EOL ;
17
18
@@ -85,10 +86,16 @@ public function testParse(): void
85
86
echo $ text , PHP_EOL ;
86
87
$ this ->assertSame ("\033[0;32mINFO \033[0m " , $ text );
87
88
89
+ // multi
90
+ $ text = ColorTag::parse ('multi: <info>INFO</info> <cyan>CYAN</cyan> <red>RED</red> ' );
91
+ echo $ text , PHP_EOL ;
92
+ $ this ->assertFalse (strpos ($ text , '</ ' ));
93
+
88
94
// nested Tags
89
- $ text = ColorTag::parse ('<info>INFO <cyan>CYAN</cyan></info> ' );
95
+ $ text = ColorTag::parse ('nested: <info>INFO <cyan>CYAN</cyan></info> ' );
90
96
echo $ text , PHP_EOL ;
91
- $ this ->assertSame ("\033[0;32mINFO <cyan>CYAN</cyan> \033[0m " , $ text );
97
+ $ this ->assertTrue (strpos ($ text , '</ ' ) > 0 );
98
+ $ this ->assertSame ("nested: \033[0;32mINFO <cyan>CYAN</cyan> \033[0m " , $ text );
92
99
93
100
Color::resetConfig ();
94
101
}
You can’t perform that action at this time.
0 commit comments