From b3fa09d554bb388d325b3dc5f71ee4b4bc5c08d3 Mon Sep 17 00:00:00 2001
From: Petr Skoda
|
# Special cases that are not close tags.
)
~x', '$1 ', $string); // Add a space after the close tag.
+ if ($format !== null && $format != FORMAT_PLAIN) {
+ // Match the usual text cleaning before display.
+ // Ideally we should apply multilang filter only here, other filters might add extra text.
+ $string = format_text($string, $format, ['filter' => false, 'noclean' => false, 'para' => false]);
+ }
// Now remove HTML tags.
$string = strip_tags($string);
// Decode HTML entities.
@@ -8439,9 +8445,15 @@ function count_words($string) {
*
* @category string
* @param string $string The text to be searched for letters. May be HTML.
+ * @param int|null $format
* @return int The count of letters in the specified text.
*/
-function count_letters($string) {
+function count_letters($string, $format = null) {
+ if ($format !== null && $format != FORMAT_PLAIN) {
+ // Match the usual text cleaning before display.
+ // Ideally we should apply multilang filter only here, other filters might add extra text.
+ $string = format_text($string, $format, ['filter' => false, 'noclean' => false, 'para' => false]);
+ }
$string = strip_tags($string); // Tags are out now.
$string = html_entity_decode($string, ENT_COMPAT);
$string = preg_replace('/[[:space:]]*/', '', $string); // Whitespace are out now.
diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php
index 3124b326987..7d5e88d81e7 100644
--- a/lib/tests/moodlelib_test.php
+++ b/lib/tests/moodlelib_test.php
@@ -3955,9 +3955,11 @@ EOF;
* @dataProvider count_words_testcases
* @param int $expectedcount number of words in $string.
* @param string $string the test string to count the words of.
+ * @param int|null $format
*/
- public function test_count_words(int $expectedcount, string $string): void {
- $this->assertEquals($expectedcount, count_words($string));
+ public function test_count_words(int $expectedcount, string $string, $format = null): void {
+ $this->assertEquals($expectedcount, count_words($string, $format),
+ "'$string' with format '$format' does not match count $expectedcount");
}
/**
@@ -3966,6 +3968,13 @@ EOF;
* @return array of test cases.
*/
public function count_words_testcases(): array {
+ // Copy-pasting example from MDL-64240.
+ $copypasted = <<
+EOT;
+
// The counts here should match MS Word and Libre Office.
return [
[0, ''],
@@ -4002,6 +4011,16 @@ EOF;
[1, "SO42-"],
[6, '4+4=8 i.e. O(1) a,b,c,d I’m black&blue_really'],
[1, 'ab'],
+ [1, 'ab', FORMAT_PLAIN],
+ [1, 'ab', FORMAT_HTML],
+ [1, 'ab', FORMAT_MOODLE],
+ [1, 'ab', FORMAT_MARKDOWN],
+ [1, 'aa frog
'], + [4, 'frog
', FORMAT_PLAIN], + [4, 'frog
', FORMAT_MOODLE], + [4, 'frog
', FORMAT_HTML], + [4, 'frog
', FORMAT_MARKDOWN], + [2, 'aa