Skip to content

Commit 1df850e

Browse files
committed
Search: Fixed formatting timeout with many term occurrences
For #5863
1 parent 7881bdd commit 1df850e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

app/Search/SearchResultsFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function getMatchPositions(string $text, array $terms): array
9191
$offset = 0;
9292
$term = mb_strtolower($term);
9393
$pos = mb_strpos($text, $term, $offset);
94-
while ($pos !== false) {
94+
while ($pos !== false && count($matchRefs) < 25) {
9595
$end = $pos + mb_strlen($term);
9696
$matchRefs[$pos] = $end;
9797
$offset = $end;

tests/Search/EntitySearchTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,21 @@ public function test_match_highlighting_works_with_multibyte_content()
378378
$search->assertSee('<strong>На</strong> <strong>мен</strong> <strong>ми</strong> <strong>трябва</strong> <strong>нещо</strong> <strong>добро</strong> test', false);
379379
}
380380

381+
public function test_match_highlighting_is_efficient_with_large_frequency_in_content()
382+
{
383+
$content = str_repeat('superbeans ', 10000);
384+
$this->entities->newPage([
385+
'name' => 'Test Page',
386+
'html' => "<p>{$content}</p>",
387+
]);
388+
389+
$time = microtime(true);
390+
$resp = $this->asEditor()->get('/search?term=' . urlencode('superbeans'));
391+
$this->assertLessThan(0.5, microtime(true) - $time);
392+
393+
$resp->assertSee('<strong>superbeans</strong>', false);
394+
}
395+
381396
public function test_html_entities_in_item_details_remains_escaped_in_search_results()
382397
{
383398
$this->entities->newPage(['name' => 'My <cool> TestPageContent', 'html' => '<p>My supercool &lt;great&gt; TestPageContent page</p>']);

0 commit comments

Comments
 (0)