mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #484 from simonwelsh/multipe-shortcodes
BUGFIX: Handle consecutive shortcodes.
This commit is contained in:
commit
1772355ef3
@ -153,26 +153,41 @@ class ShortcodeParser {
|
|||||||
if(!$this->shortcodes) return $content;
|
if(!$this->shortcodes) return $content;
|
||||||
|
|
||||||
$shortcodes = implode('|', array_map('preg_quote', array_keys($this->shortcodes)));
|
$shortcodes = implode('|', array_map('preg_quote', array_keys($this->shortcodes)));
|
||||||
$pattern = "/(.?)\[($shortcodes)(.*?)(\/)?\](?(4)|(?:(.+?)\[\/\s*\\2\s*\]))?(.?)/s";
|
$pattern = "/\[($shortcodes)(.*?)(\/\]|\](?(4)|(?:(.+?)\[\/\s*\\1\s*\]))|\])/s";
|
||||||
|
|
||||||
return preg_replace_callback($pattern, array($this, 'handleShortcode'), $content);
|
if(preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
|
||||||
|
$replacements = array();
|
||||||
|
foreach($matches as $match) {
|
||||||
|
$prefix = $match[0][1] ? $content[$match[0][1]-1] : '';
|
||||||
|
if(strlen($match[0][0]) + $match[0][1] < strlen($content)) {
|
||||||
|
$suffix = $content[strlen($match[0][0]) + $match[0][1]];
|
||||||
|
} else {
|
||||||
|
$suffix = '';
|
||||||
|
}
|
||||||
|
if($prefix == '[' && $suffix == ']') {
|
||||||
|
$replacements[] = array($match[0][0], $match[0][1]-1, strlen($match[0][0]) + 2);
|
||||||
|
} else {
|
||||||
|
$replacements[] = array($this->handleShortcode($match), $match[0][1], strlen($match[0][0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We reverse this so that replacements don't break offsets
|
||||||
|
foreach(array_reverse($replacements) as $replace) {
|
||||||
|
$content = substr_replace($content, $replace[0], $replace[1], $replace[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
protected function handleShortcode($matches) {
|
protected function handleShortcode($matches) {
|
||||||
$prefix = $matches[1];
|
$shortcode = $matches[1][0];
|
||||||
$suffix = $matches[6];
|
|
||||||
$shortcode = $matches[2];
|
|
||||||
|
|
||||||
// allow for escaping shortcodes by enclosing them in double brackets ([[shortcode]])
|
|
||||||
if($prefix == '[' && $suffix == ']') {
|
|
||||||
return substr($matches[0], 1, -1);
|
|
||||||
}
|
|
||||||
$attributes = array(); // Parse attributes into into this array.
|
$attributes = array(); // Parse attributes into into this array.
|
||||||
|
|
||||||
if(preg_match_all('/(\w+) *= *(?:([\'"])(.*?)\\2|([^ ,"\'>]+))/', $matches[3], $match, PREG_SET_ORDER)) {
|
if(preg_match_all('/(\w+) *= *(?:([\'"])(.*?)\\2|([^ ,"\'>]+))/', $matches[2][0], $match, PREG_SET_ORDER)) {
|
||||||
foreach($match as $attribute) {
|
foreach($match as $attribute) {
|
||||||
if(!empty($attribute[4])) {
|
if(!empty($attribute[4])) {
|
||||||
$attributes[strtolower($attribute[1])] = $attribute[4];
|
$attributes[strtolower($attribute[1])] = $attribute[4];
|
||||||
@ -182,7 +197,7 @@ class ShortcodeParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $prefix . call_user_func($this->shortcodes[$shortcode], $attributes, $matches[5], $this, $shortcode) . $suffix;
|
return call_user_func($this->shortcodes[$shortcode], $attributes, isset($matches[4][0]) ? $matches[4][0] : '', $this, $shortcode);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -101,6 +101,10 @@ class ShortcodeParserTest extends SapphireTest {
|
|||||||
$this->assertEquals(2, $this->arguments['id']);
|
$this->assertEquals(2, $this->arguments['id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testConsecutiveTags() {
|
||||||
|
$this->assertEquals('', $this->parser->parse('[test_shortcode][test_shortcode]'));
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user