From: nicolasconnault Date: Mon, 22 Jun 2009 06:07:56 +0000 (+0000) Subject: MDL-19566 Refactored some of the more complex regular expressions in their own functi... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=31c2087d1ebeb2ab75b813458eaf1a438844be91;p=moodle.git MDL-19566 Refactored some of the more complex regular expressions in their own function and added unit tests for them. --- diff --git a/lib/simpletest/testweblib.php b/lib/simpletest/testweblib.php index 0b08a44336..1c5372b9cb 100644 --- a/lib/simpletest/testweblib.php +++ b/lib/simpletest/testweblib.php @@ -72,5 +72,28 @@ class web_test extends UnitTestCase { $this->assertEqual(replace_ampersands_not_followed_by_entity("This & that  "), "This & that  "); $this->assertEqual(replace_ampersands_not_followed_by_entity("This   that  "), "This &nbsp that  "); } + + function test_strip_links() { + $this->assertEqual(strip_links('this is a link'), 'this is a link'); + } + + function test_wikify_links() { + $this->assertEqual(wikify_links('this is a link'), 'this is a link [ http://someaddress.com/query ]'); + } + + function test_fix_non_standard_entities() { + $this->assertEqual(fix_non_standard_entities('£ä'), '£ä'); + $this->assertEqual(fix_non_standard_entities('£ä'), '£ä'); + } + + function test_convert_urls_into_links() { + $string = "visit http://www.moodle.org"; + convert_urls_into_links($string); + $this->assertEqual($string, 'visit http://www.moodle.org'); + + $string = "visit www.moodle.org"; + convert_urls_into_links($string); + $this->assertEqual($string, 'visit www.moodle.org'); + } } ?> diff --git a/lib/weblib.php b/lib/weblib.php index 8ccfed3a73..bc878d96c1 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1654,7 +1654,7 @@ function format_string($string, $striplinks=true, $courseid=NULL ) { } else { // Otherwise strip just links if that is required (default) if ($striplinks) { //strip links in string - $string = preg_replace('/(]+?>)(.+?)(<\/a>)/is','$2',$string); + $string = strip_links($string); } $string = clean_text($string); } @@ -1677,6 +1677,38 @@ function replace_ampersands_not_followed_by_entity($string) { return preg_replace("/\&(?![a-zA-Z0-9#]{1,8};)/", "&", $string); } +/** + * Given a string, replaces all .* by .* and returns the string. + * + * @param string $string + * @return string + */ +function strip_links($string) { + return preg_replace('/(]+?>)(.+?)(<\/a>)/is','$2',$string); +} + +/** + * This expression turns links into something nice in a text format. (Russell Jungwirth) + * + * @param string $string + * @return string + */ +function wikify_links($string) { + return preg_replace('~(]*>([^<]*))~i','$3 [ $2 ]', $string); +} + +/** + * Replaces non-standard HTML entities + * + * @param string $string + * @return string + */ +function fix_non_standard_entities($string) { + $text = preg_replace('/(&#[0-9]+)(;?)/', '$1;', $string); + $text = preg_replace('/(&#x[0-9a-fA-F]+)(;?)/', '$1;', $text); + return $text; +} + /** * Given text in a variety of format codings, this function returns * the text as plain text suitable for plain email. @@ -1701,9 +1733,7 @@ function format_text_email($text, $format) { case FORMAT_WIKI: $text = wiki_to_html($text); - /// This expression turns links into something nice in a text format. (Russell Jungwirth) - /// From: http://php.net/manual/en/function.eregi-replace.php and simplified - $text = preg_replace('~(]*>([^<]*))~i','$3 [ $2 ]', $text); + $text = wikify_links($text); return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); break; @@ -1714,7 +1744,7 @@ function format_text_email($text, $format) { case FORMAT_MOODLE: case FORMAT_MARKDOWN: default: - $text = preg_replace('~(]*>([^<]*))~i','$3 [ $2 ]', $text); + $text = wikify_links($text); return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); break; } @@ -1859,8 +1889,7 @@ function clean_text($text, $format=FORMAT_MOODLE) { $text = purify_html($text); } else { /// Fix non standard entity notations - $text = preg_replace('/(&#[0-9]+)(;?)/', "\\1;", $text); - $text = preg_replace('/(&#x[0-9a-fA-F]+)(;?)/', "\\1;", $text); + $text = fix_non_standard_entities($text); /// Remove tags that are not allowed $text = strip_tags($text, $ALLOWED_TAGS); @@ -2186,11 +2215,11 @@ function html_to_text($html) { function convert_urls_into_links(&$text) { /// Make lone URLs into links. eg http://moodle.com/ $text = preg_replace("~([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])~i", - "\\1\\2://\\3\\4", $text); + '$1$2://$3$4', $text); /// eg www.moodle.com $text = preg_replace("~([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])~i", - "\\1www.\\2\\3", $text); + '$1www.$2$3', $text); } /**