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
2727 // regex used for removing color tags
2828 public const STRIP_TAG = '/<[\/]?[a-zA-Z0-9=;]+>/ ' ;
2929
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 ' ;
3233
3334 // color
3435 public const BLACK = 'black ' ;
Original file line number Diff line number Diff line change 1212use PHPUnit \Framework \TestCase ;
1313use Toolkit \Cli \Color ;
1414use Toolkit \Cli \ColorTag ;
15+ use function strpos ;
1516use function vdump ;
1617use const PHP_EOL ;
1718
@@ -85,10 +86,16 @@ public function testParse(): void
8586 echo $ text , PHP_EOL ;
8687 $ this ->assertSame ("\033[0;32mINFO \033[0m " , $ text );
8788
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+
8894 // nested Tags
89- $ text = ColorTag::parse ('<info>INFO <cyan>CYAN</cyan></info> ' );
95+ $ text = ColorTag::parse ('nested: <info>INFO <cyan>CYAN</cyan></info> ' );
9096 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 );
9299
93100 Color::resetConfig ();
94101 }
You can’t perform that action at this time.
0 commit comments