Skip to content

Commit

Permalink
MDL-36571 qtype multichoice: don't corrupt unicode characters.
Browse files Browse the repository at this point in the history
It seems that \s includes non-breaking space, and the typical one-byte
representation of that is the same as some parts of multibyte unicode
characters. Therefore, you need to include the u modifer on the regular
expressions.

Also, remove any number of <br /> at the end of the answer.

Thanks to Joseph Rézeau and Jean-Michel Vedrine for working out what the
problem was, and how to fix it.
  • Loading branch information
timhunt committed Nov 19, 2012
1 parent 6548ce0 commit 4c1a35e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions question/type/multichoice/question.php
Expand Up @@ -126,10 +126,10 @@ public function check_file_access($qa, $options, $component, $filearea, $args, $
}

public function make_html_inline($html) {
$html = preg_replace('~\s*<p>\s*~', '', $html);
$html = preg_replace('~\s*</p>\s*~', '<br />', $html);
$html = preg_replace('~<br />$~', '', $html);
return $html;
$html = preg_replace('~\s*<p>\s*~u', '', $html);
$html = preg_replace('~\s*</p>\s*~u', '<br />', $html);
$html = preg_replace('~(<br\s*/?>)+$~u', '', $html);
return trim($html);
}
}

Expand Down
2 changes: 2 additions & 0 deletions question/type/multichoice/tests/question_test.php
Expand Up @@ -147,6 +147,8 @@ public function test_make_html_inline() {
$this->assertEquals("Frog<br />XXX <img src='http://example.com/pic.png' alt='Graph' />",
$mc->make_html_inline(" <p> Frog </p> \n\r
<p> XXX <img src='http://example.com/pic.png' alt='Graph' /> </p> "));
$this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p><p></p>'));
$this->assertEquals('Frog<br />†', $mc->make_html_inline('<p>Frog</p><p>†</p>'));
}
}

Expand Down

0 comments on commit 4c1a35e

Please sign in to comment.