Skip to content

Commit 6db64b5

Browse files
committed
fix: cannot remove ? on match tag pattern
1 parent 5fe0897 commit 6db64b5

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Color/ColorTag.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff 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';

test/ColorTagTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPUnit\Framework\TestCase;
1313
use Toolkit\Cli\Color;
1414
use Toolkit\Cli\ColorTag;
15+
use function strpos;
1516
use function vdump;
1617
use 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
}

0 commit comments

Comments
 (0)