]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19566 Refactored some of the more complex regular expressions in their own functi...
authornicolasconnault <nicolasconnault>
Mon, 22 Jun 2009 06:07:56 +0000 (06:07 +0000)
committernicolasconnault <nicolasconnault>
Mon, 22 Jun 2009 06:07:56 +0000 (06:07 +0000)
lib/simpletest/testweblib.php
lib/weblib.php

index 0b08a44336a7d52fc13523fbbca21460bf9e6f4d..1c5372b9cbdaf7009dc1161ff3724743dfabb12c 100644 (file)
@@ -72,5 +72,28 @@ class web_test extends UnitTestCase {
         $this->assertEqual(replace_ampersands_not_followed_by_entity("This & that &nbsp;"), "This &amp; that &nbsp;");
         $this->assertEqual(replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"), "This &amp;nbsp that &nbsp;");
     }
+
+    function test_strip_links() {
+        $this->assertEqual(strip_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link');
+    }
+
+    function test_wikify_links() {
+        $this->assertEqual(wikify_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link [ http://someaddress.com/query ]');
+    }
+
+    function test_fix_non_standard_entities() {
+        $this->assertEqual(fix_non_standard_entities('&#x00A3&#0228'), '&#x00A3;&#0228;');
+        $this->assertEqual(fix_non_standard_entities('&#x00A3;&#0228;'), '&#x00A3;&#0228;');
+    }
+
+    function test_convert_urls_into_links() {
+        $string = "visit http://www.moodle.org";
+        convert_urls_into_links($string);
+        $this->assertEqual($string, 'visit <a href="http://www.moodle.org">http://www.moodle.org</a>');
+        
+        $string = "visit www.moodle.org";
+        convert_urls_into_links($string);
+        $this->assertEqual($string, 'visit <a href="http://www.moodle.org">www.moodle.org</a>');
+    }
 }
 ?>
index 8ccfed3a73d8614748ff6d3d3d8d49324f45e1a8..bc878d96c173ce6149f96cf75c0b9edae15caf74 100644 (file)
@@ -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\s[^>]+?>)(.+?)(<\/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};)/", "&amp;", $string);
 }
 
+/**
+ * Given a string, replaces all <a>.*</a> by .* and returns the string.
+ * 
+ * @param string $string
+ * @return string
+ */
+function strip_links($string) {
+    return preg_replace('/(<a\s[^>]+?>)(.+?)(<\/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('~(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)~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('~(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)~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('~(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)~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<a href=\"\\2://\\3\\4\" target=\"_blank\">\\2://\\3\\4</a>", $text);
+                          '$1<a href="$2://$3$4">$2://$3$4</a>', $text);
 
 /// eg www.moodle.com
     $text = preg_replace("~([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])~i",
-                          "\\1<a href=\"http://www.\\2\\3\" target=\"_blank\">www.\\2\\3</a>", $text);
+                          '$1<a href="http://www.$2$3">www.$2$3</a>', $text);
 }
 
 /**