diff --git a/lib/html2text.php b/lib/html2text.php index 9cc213ddd60..e360b2c47d2 100644 --- a/lib/html2text.php +++ b/lib/html2text.php @@ -543,9 +543,15 @@ class html2text */ function _convert_pre(&$text) { - while(preg_match('/
]*>(.*)<\/pre>/ismU', $text, $matches)) {
- $result = preg_replace($this->pre_search, $this->pre_replace, $matches[1]);
- $text = preg_replace('/]*>.*<\/pre>/ismU', '
' . $result . '
', $text, 1);
+ while (preg_match('/]*>(.*)<\/pre>/ismU', $text, $matches)) {
+ // convert the content
+ $this->pre_content = sprintf('
%s
',
+ preg_replace($this->pre_search, $this->pre_replace, $matches[1]));
+ // replace the content (use callback because content can contain $0 variable)
+ $text = preg_replace_callback('/]*>.*<\/pre>/ismU',
+ array('html2text', '_preg_pre_callback'), $text, 1);
+ // free memory
+ $this->pre_content = '';
}
}
@@ -573,6 +579,17 @@ class html2text
}
}
+ /**
+ * Callback function for preg_replace_callback use in PRE content handler.
+ *
+ * @param array PREG matches
+ * @return string
+ */
+ private function _preg_pre_callback($matches)
+ {
+ return $this->pre_content;
+ }
+
/**
* Strtoupper multibyte wrapper function
*
diff --git a/lib/simpletest/testweblib.php b/lib/simpletest/testweblib.php
index cf2c84bc8b5..27359ce88a5 100644
--- a/lib/simpletest/testweblib.php
+++ b/lib/simpletest/testweblib.php
@@ -139,6 +139,27 @@ class web_test extends UnitTestCase {
$this->assertIdentical('0', html_to_text('0'));
}
+ public function test_html_to_text_pre_parsing_problem() {
+ $strorig = 'Consider the following function:
void FillMeUp(char* in_string) {'.
+ '
int i = 0;
while (in_string[i] != \'\0\') {
in_string[i] = \'X\';
i++;
}
'.
+ '}What would happen if a non-terminated string were input to this function?
';
+
+ $strconv = 'Consider the following function:
+
+void FillMeUp(char* in_string) {
+ int i = 0;
+ while (in_string[i] != \'\0\') {
+ in_string[i] = \'X\';
+ i++;
+ }
+}
+What would happen if a non-terminated string were input to this function?
+
+';
+
+ $this->assertIdentical($strconv, html_to_text($strorig));
+ }
+
public function test_clean_text() {
$text = "lala ";
$this->assertEqual($text, clean_text($text, FORMAT_PLAIN));