mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/15403] Handle ellipsis at the beginning of context and add tests
PHPBB-15403
This commit is contained in:
parent
dd9267b678
commit
f4b1444248
2 changed files with 20 additions and 1 deletions
|
@ -420,7 +420,7 @@ function get_context(string $text, array $words, int $length = 400): string
|
||||||
$output[] = utf8_substr($fragment, $fragment_start, $fragment_end - $fragment_start + 1);
|
$output[] = utf8_substr($fragment, $fragment_start, $fragment_end - $fragment_start + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return htmlentities(implode(' ... ', $output)) . ($end < $text_length - 1 ? ' ...' : '');
|
return ($fragments[0][0] != 0 ? '... ' : '') . htmlentities(implode(' ... ', $output)) . ($end < $text_length - 1 ? ' ...' : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -89,6 +89,24 @@ class phpbb_functions_content_get_context_test extends TestCase
|
||||||
'length' => -10,
|
'length' => -10,
|
||||||
'expected' => '...',
|
'expected' => '...',
|
||||||
],
|
],
|
||||||
|
'ellipses_beginning' => [
|
||||||
|
'text' => 'foo foo foo foo foo foo foo foo bar',
|
||||||
|
'words' => ['bar'],
|
||||||
|
'length' => 10,
|
||||||
|
'expected' => '... foo foo bar',
|
||||||
|
],
|
||||||
|
'ellipsis_end' => [
|
||||||
|
'text' => 'bar foo foo foo foo foo foo foo foo',
|
||||||
|
'words' => ['bar'],
|
||||||
|
'length' => 10,
|
||||||
|
'expected' => 'bar foo foo ...',
|
||||||
|
],
|
||||||
|
'ellipsis_middle' => [
|
||||||
|
'text' => 'foo word1 foo foo foo foo foo foo foo foo foo word2 foo',
|
||||||
|
'words' => ['word1', 'word2'],
|
||||||
|
'length' => 10,
|
||||||
|
'expected' => '... word1 ... word2 ...',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,4 +117,5 @@ class phpbb_functions_content_get_context_test extends TestCase
|
||||||
{
|
{
|
||||||
$this->assertEquals($expected, get_context($text, $words, $length));
|
$this->assertEquals($expected, get_context($text, $words, $length));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue