Skip to content

Commit f1997c6

Browse files
committed
Also trim non-breakable spaces from auto-generated titles
1 parent c469309 commit f1997c6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

classes/autotitle.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ class autotitle {
3333
*/
3434
public static function extract_title(string $summary): string {
3535

36-
$summary = trim($summary);
37-
3836
if ($summary === '') {
3937
return '';
4038
}
4139

4240
$dom = new \DOMDocument();
4341
libxml_use_internal_errors(true);
44-
$dom->loadHTML($summary);
42+
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $summary);
4543
libxml_clear_errors();
4644

4745
return static::find_first_nonempty_text_node_value($dom);
@@ -59,7 +57,7 @@ public static function extract_title(string $summary): string {
5957
public static function find_first_nonempty_text_node_value(\DOMNode $node): string {
6058

6159
if ($node->nodeType == XML_TEXT_NODE) {
62-
$text = trim($node->textContent);
60+
$text = (string) preg_replace('/^\s+|\s+$/u', '', $node->textContent);
6361

6462
if ($text !== '') {
6563
return $text;

tests/block_course_contents_test.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public function extract_title_data(): array {
7979
'summary' => 'Hello <br> students',
8080
'title' => 'Hello',
8181
],
82+
'Multi-byte' => [
83+
'summary' => "\u{a0}µ déjà\u{a0}ěščřžýáíé &nbsp; \u{a0}",
84+
'title' => "µ déjà\u{a0}ěščřžýáíé",
85+
],
86+
'Non-breakable spaces' => [
87+
'summary' => '<h2>&nbsp;<span style="font-family:tahoma">Lesson 4: Your first game &nbsp;' . "\xC2\xA0" .
88+
"\xc2\xa0" . '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span> </h2>',
89+
'title' => 'Lesson 4: Your first game',
90+
],
8291
];
8392
}
8493
}

0 commit comments

Comments
 (0)