Merge branch 'MDL-46688_26' of git://github.com/timhunt/moodle into MOODLE_26_STABLE
This commit is contained in:
@@ -117,15 +117,41 @@ class core_weblib_testcase extends advanced_testcase {
|
||||
}
|
||||
|
||||
public function test_highlight() {
|
||||
$this->assertSame('This is <span class="highlight">good</span>', highlight('good', 'This is good'));
|
||||
$this->assertSame('<span class="highlight">span</span>', highlight('SpaN', 'span'));
|
||||
$this->assertSame('<span class="highlight">SpaN</span>', highlight('span', 'SpaN'));
|
||||
$this->assertSame('<span><span class="highlight">span</span></span>', highlight('span', '<span>span</span>'));
|
||||
$this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>', highlight('good is', 'He is good'));
|
||||
$this->assertSame('This is <span class="highlight">good</span>', highlight('+good', 'This is good'));
|
||||
$this->assertSame('This is good', highlight('-good', 'This is good'));
|
||||
$this->assertSame('This is goodness', highlight('+good', 'This is goodness'));
|
||||
$this->assertSame('This is <span class="highlight">good</span>ness', highlight('good', 'This is goodness'));
|
||||
$this->assertSame('This is <span class="highlight">good</span>',
|
||||
highlight('good', 'This is good'));
|
||||
|
||||
$this->assertSame('<span class="highlight">span</span>',
|
||||
highlight('SpaN', 'span'));
|
||||
|
||||
$this->assertSame('<span class="highlight">SpaN</span>',
|
||||
highlight('span', 'SpaN'));
|
||||
|
||||
$this->assertSame('<span><span class="highlight">span</span></span>',
|
||||
highlight('span', '<span>span</span>'));
|
||||
|
||||
$this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>',
|
||||
highlight('good is', 'He is good'));
|
||||
|
||||
$this->assertSame('This is <span class="highlight">good</span>',
|
||||
highlight('+good', 'This is good'));
|
||||
|
||||
$this->assertSame('This is good',
|
||||
highlight('-good', 'This is good'));
|
||||
|
||||
$this->assertSame('This is goodness',
|
||||
highlight('+good', 'This is goodness'));
|
||||
|
||||
$this->assertSame('This is <span class="highlight">good</span>ness',
|
||||
highlight('good', 'This is goodness'));
|
||||
|
||||
$this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
|
||||
highlight('test 1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
|
||||
|
||||
$this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
|
||||
highlight('test +1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
|
||||
|
||||
$this->assertSame('<p><b>test</b> 1</p><p>1</p>',
|
||||
highlight('test -1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
|
||||
}
|
||||
|
||||
public function test_replace_ampersands() {
|
||||
|
||||
+15
-16
@@ -1936,24 +1936,23 @@ function highlight($needle, $haystack, $matchcase = false,
|
||||
return $haystack;
|
||||
}
|
||||
|
||||
// Find all the HTML tags in the input, and store them in a placeholders array..
|
||||
$placeholders = array();
|
||||
$matches = array();
|
||||
preg_match_all('/<[^>]*>/', $haystack, $matches);
|
||||
foreach (array_unique($matches[0]) as $key => $htmltag) {
|
||||
$placeholders['<|' . $key . '|>'] = $htmltag;
|
||||
// Split the string into HTML tags and real content.
|
||||
$chunks = preg_split('/((?:<[^>]*>)+)/', $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
|
||||
// We have an array of alternating blocks of text, then HTML tags, then text, ...
|
||||
// Loop through replacing search terms in the text, and leaving the HTML unchanged.
|
||||
$ishtmlchunk = false;
|
||||
$result = '';
|
||||
foreach ($chunks as $chunk) {
|
||||
if ($ishtmlchunk) {
|
||||
$result .= $chunk;
|
||||
} else {
|
||||
$result .= preg_replace($regexp, $prefix . '$1' . $suffix, $chunk);
|
||||
}
|
||||
$ishtmlchunk = !$ishtmlchunk;
|
||||
}
|
||||
|
||||
// In $hastack, replace each HTML tag with the corresponding placeholder.
|
||||
$haystack = str_replace($placeholders, array_keys($placeholders), $haystack);
|
||||
|
||||
// In the resulting string, Do the highlighting.
|
||||
$haystack = preg_replace($regexp, $prefix . '$1' . $suffix, $haystack);
|
||||
|
||||
// Turn the placeholders back into HTML tags.
|
||||
$haystack = str_replace(array_keys($placeholders), $placeholders, $haystack);
|
||||
|
||||
return $haystack;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user