Skip to content

Commit b9627fc

Browse files
authored
[6.x] Add a substring parameter to the mark modifier (#15955)
1 parent 2df7e7a commit b9627fc

2 files changed

Lines changed: 107 additions & 22 deletions

File tree

‎src/Modifiers/CoreModifiers.php‎

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,16 +1653,33 @@ public function macro($value, $params, $context)
16531653
*/
16541654
public function mark($value, $params, $context)
16551655
{
1656+
[$flags, $params] = collect($params)->partition(
1657+
fn (mixed $param): bool => is_string($param) && in_array(strtolower($param), ['substring:true', 'substring:false'], true)
1658+
);
1659+
1660+
$substring = ! $flags->contains(fn (string $flag): bool => strtolower($flag) === 'substring:false');
1661+
1662+
$params = $params->values()->all();
1663+
16561664
if (! $words = $params[0] ?? $context['get']['q'] ?? null) {
16571665
return $value;
16581666
}
16591667

1660-
$params[0] = collect(preg_split('/\s+/', $words))
1661-
->map(fn ($word) => preg_quote($word, '/'))
1662-
->filter()
1668+
$pattern = collect(preg_split('/\s+/', $words))
1669+
->map(fn (string $word): string => preg_quote($word, '/'))
1670+
->filter(fn (string $word): bool => $word !== '')
16631671
->join('|');
16641672

1665-
return $this->regexMark($value, $params);
1673+
if ($substring || $pattern === '') {
1674+
$params[0] = $pattern;
1675+
1676+
return $this->regexMark($value, $params);
1677+
}
1678+
1679+
// The unicode flag lets the boundaries treat accented and non-Latin letters as word characters.
1680+
$params[0] = '(?<![\p{L}\p{N}_])(?:'.$pattern.')(?![\p{L}\p{N}_])';
1681+
1682+
return $this->markWithFlags($value, $params, 'isu');
16661683
}
16671684

16681685
/**
@@ -2090,24 +2107,7 @@ public function readTime($value, $params)
20902107
*/
20912108
public function regexMark($value, $params)
20922109
{
2093-
if (! $pattern = array_shift($params)) {
2094-
return $value;
2095-
}
2096-
2097-
$attributes = $this->buildAttributesFromParameters($params);
2098-
2099-
return Html::mapText($value, function ($text) use ($pattern, $attributes) {
2100-
$text = Html::decode($text);
2101-
2102-
return Str::mapRegex($text, "/({$pattern})/is", function ($part, $match) use ($attributes) {
2103-
$part = Html::entities($part);
2104-
if ($match) {
2105-
$part = '<mark'.Html::attributes($attributes).'>'.$part.'</mark>';
2106-
}
2107-
2108-
return $part;
2109-
});
2110-
});
2110+
return $this->markWithFlags($value, $params, 'is');
21112111
}
21122112

21132113
/**
@@ -3272,6 +3272,28 @@ private function buildAttributesFromParameters($params, $delimiter = ':')
32723272
return $attributes;
32733273
}
32743274

3275+
private function markWithFlags($value, $params, string $flags)
3276+
{
3277+
if (! $pattern = array_shift($params)) {
3278+
return $value;
3279+
}
3280+
3281+
$attributes = $this->buildAttributesFromParameters($params);
3282+
3283+
return Html::mapText($value, function ($text) use ($pattern, $attributes, $flags) {
3284+
$text = Html::decode($text);
3285+
3286+
return Str::mapRegex($text, "/({$pattern})/{$flags}", function ($part, $match) use ($attributes) {
3287+
$part = Html::entities($part);
3288+
if ($match) {
3289+
$part = '<mark'.Html::attributes($attributes).'>'.$part.'</mark>';
3290+
}
3291+
3292+
return $part;
3293+
});
3294+
});
3295+
}
3296+
32753297
private function getMathModifierNumber($params, $context)
32763298
{
32773299
$number = $params[0];

‎tests/Modifiers/MarkTest.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,64 @@ public function it_marks_with_class()
3434
$this->assertEquals($expected, $this->modify($value, $words, $param));
3535
}
3636

37+
#[Test]
38+
public function it_marks_inside_words_by_default()
39+
{
40+
$this->assertEquals('Ne<mark>two</mark>rk', $this->modify('Network', 'two'));
41+
$this->assertEquals('Ne<mark>two</mark>rk', $this->modify('Network', 'two', 'substring:true'));
42+
}
43+
44+
#[Test]
45+
public function it_marks_whole_words_when_substring_is_false()
46+
{
47+
$value = 'Passive House Network interviews Matthew at the association';
48+
$words = 'Two years in at Passive House';
49+
50+
$expected = '<mark>Passive</mark> <mark>House</mark> Network interviews Matthew <mark>at</mark> the association';
51+
52+
$this->assertEquals($expected, $this->modify($value, $words, 'substring:false'));
53+
$this->assertEquals('<mark>House</mark>,', $this->modify('House,', 'house', 'substring:false'));
54+
}
55+
56+
#[Test]
57+
public function it_marks_whole_words_with_accented_characters()
58+
{
59+
$this->assertEquals('caf&eacute;', $this->modify('café', 'caf', 'substring:false'));
60+
$this->assertEquals('&Uuml;ber', $this->modify('Über', 'ber', 'substring:false'));
61+
$this->assertEquals('Un <mark>caf&eacute;</mark> noir', $this->modify('Un café noir', 'café', 'substring:false'));
62+
}
63+
64+
#[Test]
65+
public function it_marks_whole_words_when_any_argument_is_substring_false()
66+
{
67+
$this->assertEquals('Network', $this->modify('Network', 'two', 'SUBSTRING:FALSE', 'substring:true'));
68+
$this->assertEquals('Network', $this->modify('Network', 'two', 'substring:true', 'SUBSTRING:FALSE'));
69+
}
70+
71+
#[Test]
72+
public function it_marks_whole_words_from_the_query_string()
73+
{
74+
$value = 'Passive House Network interviews Matthew at the association';
75+
76+
$expected = '<mark>Passive</mark> <mark>House</mark> Network interviews Matthew <mark>at</mark> the association';
77+
78+
$this->assertEquals($expected, $this->modifyFromQuery($value, 'Two years in at Passive House', 'substring:false'));
79+
}
80+
81+
#[Test]
82+
public function it_marks_whole_words_with_a_class()
83+
{
84+
$expected = 'Passive <mark class="highlight">House</mark> Network';
85+
86+
$this->assertEquals($expected, $this->modify('Passive House Network', 'house', 'substring:false', 'class:highlight'));
87+
}
88+
89+
#[Test]
90+
public function it_returns_the_value_when_substring_false_has_no_query()
91+
{
92+
$this->assertEquals('Network', $this->modify('Network', 'substring:false'));
93+
}
94+
3795
#[Test]
3896
public function it_marks_with_tags()
3997
{
@@ -102,4 +160,9 @@ protected function modify($arr, ...$args)
102160
{
103161
return Modify::value($arr)->mark($args)->fetch();
104162
}
163+
164+
protected function modifyFromQuery($value, string $query, ...$args)
165+
{
166+
return Modify::value($value)->context(['get' => ['q' => $query]])->mark($args)->fetch();
167+
}
105168
}

0 commit comments

Comments
 (0)