2009-10-11 02:06:57 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A simple parser that allows you to map BBCode-like "shortcodes" to an arbitrary callback.
|
2013-05-16 01:08:56 +02:00
|
|
|
* It is a simple regex based parser that allows you to replace simple bbcode-like tags
|
|
|
|
* within a HTMLText or HTMLVarchar field when rendered into a template. The API is inspired by and very similar to the
|
|
|
|
* [Wordpress implementation](http://codex.wordpress.org/Shortcode_API) of shortcodes.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2014-01-31 03:35:18 +01:00
|
|
|
* @see http://doc.silverstripe.org/framework/en/reference/shortcodes
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-10-11 02:06:57 +02:00
|
|
|
* @subpackage misc
|
|
|
|
*/
|
2015-04-23 22:30:29 +02:00
|
|
|
class ShortcodeParser extends Object {
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
public function img_shortcode($attrs) {
|
|
|
|
return "<img src='".$attrs['src']."'>";
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static $instances = array();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static $active_instance = 'default';
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
/**
|
|
|
|
* Registered shortcodes. Items follow this structure:
|
|
|
|
* [shortcode_name] => Array(
|
|
|
|
* [0] => class_containing_handler
|
|
|
|
* [1] => name_of_shortcode_handler_method
|
|
|
|
* )
|
|
|
|
*/
|
2009-10-11 02:06:57 +02:00
|
|
|
protected $shortcodes = array();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Get the {@link ShortcodeParser} instance that is attached to a particular identifier.
|
|
|
|
*
|
|
|
|
* @param string $identifier Defaults to "default".
|
|
|
|
* @return ShortcodeParser
|
|
|
|
*/
|
|
|
|
public static function get($identifier = 'default') {
|
|
|
|
if(!array_key_exists($identifier, self::$instances)) {
|
2015-04-23 22:30:29 +02:00
|
|
|
self::$instances[$identifier] = static::create();
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
return self::$instances[$identifier];
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Get the currently active/default {@link ShortcodeParser} instance.
|
|
|
|
*
|
|
|
|
* @return ShortcodeParser
|
|
|
|
*/
|
|
|
|
public static function get_active() {
|
2015-04-23 22:30:29 +02:00
|
|
|
return static::get(self::$active_instance);
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Set the identifier to use for the current active/default {@link ShortcodeParser} instance.
|
|
|
|
*
|
|
|
|
* @param string $identifier
|
|
|
|
*/
|
|
|
|
public static function set_active($identifier) {
|
|
|
|
self::$active_instance = (string) $identifier;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Register a shortcode, and attach it to a PHP callback.
|
|
|
|
*
|
|
|
|
* The callback for a shortcode will have the following arguments passed to it:
|
|
|
|
* - Any parameters attached to the shortcode as an associative array (keys are lower-case).
|
|
|
|
* - Any content enclosed within the shortcode (if it is an enclosing shortcode). Note that any content within
|
|
|
|
* this will not have been parsed, and can optionally be fed back into the parser.
|
|
|
|
* - The {@link ShortcodeParser} instance used to parse the content.
|
|
|
|
* - The shortcode tag name that was matched within the parsed content.
|
2013-10-09 06:02:36 +02:00
|
|
|
* - An associative array of extra information about the shortcode being parsed.
|
2009-10-11 02:06:57 +02:00
|
|
|
*
|
|
|
|
* @param string $shortcode The shortcode tag to map to the callback - normally in lowercase_underscore format.
|
|
|
|
* @param callback $callback The callback to replace the shortcode with.
|
|
|
|
*/
|
|
|
|
public function register($shortcode, $callback) {
|
|
|
|
if(is_callable($callback)) $this->shortcodes[$shortcode] = $callback;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Check if a shortcode has been registered.
|
|
|
|
*
|
|
|
|
* @param string $shortcode
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function registered($shortcode) {
|
|
|
|
return array_key_exists($shortcode, $this->shortcodes);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Remove a specific registered shortcode.
|
|
|
|
*
|
|
|
|
* @param string $shortcode
|
|
|
|
*/
|
|
|
|
public function unregister($shortcode) {
|
|
|
|
if($this->registered($shortcode)) unset($this->shortcodes[$shortcode]);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
/**
|
|
|
|
* Get an array containing information about registered shortcodes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRegisteredShortcodes() {
|
|
|
|
return $this->shortcodes;
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Remove all registered shortcodes.
|
|
|
|
*/
|
|
|
|
public function clear() {
|
|
|
|
$this->shortcodes = array();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-22 10:44:00 +02:00
|
|
|
/**
|
|
|
|
* Call a shortcode and return its replacement text
|
|
|
|
* Returns false if the shortcode isn't registered
|
|
|
|
*/
|
2013-10-09 06:02:36 +02:00
|
|
|
public function callShortcode($tag, $attributes, $content, $extra = array()) {
|
2015-06-22 10:44:00 +02:00
|
|
|
if (!$tag || !isset($this->shortcodes[$tag])) return false;
|
2013-10-09 06:02:36 +02:00
|
|
|
return call_user_func($this->shortcodes[$tag], $attributes, $content, $this, $tag, $extra);
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-22 10:44:00 +02:00
|
|
|
/**
|
|
|
|
* Return the text to insert in place of a shoprtcode.
|
|
|
|
* Behaviour in the case of missing shortcodes depends on the setting of ShortcodeParser::$error_behavior.
|
|
|
|
* @param $tag A map containing the the following keys:
|
|
|
|
* - 'open': The name of the tag
|
|
|
|
* - 'attrs': Attributes of the tag
|
|
|
|
* - 'content': Content of the tag
|
|
|
|
* @param $extra Extra-meta data
|
|
|
|
* @param $isHTMLAllowed A boolean indicating whether it's okay to insert HTML tags into the result
|
|
|
|
*/
|
|
|
|
function getShortcodeReplacementText($tag, $extra = array(), $isHTMLAllowed = true) {
|
|
|
|
$content = $this->callShortcode($tag['open'], $tag['attrs'], $tag['content'], $extra);
|
|
|
|
|
|
|
|
// Missing tag
|
|
|
|
if ($content === false) {
|
|
|
|
if(ShortcodeParser::$error_behavior == ShortcodeParser::ERROR) {
|
|
|
|
user_error('Unknown shortcode tag '.$tag['open'], E_USER_ERRROR);
|
|
|
|
}
|
|
|
|
else if (self::$error_behavior == self::WARN && $isHTMLAllowed) {
|
|
|
|
$content = '<strong class="warning">'.$tag['text'].'</strong>';
|
|
|
|
}
|
|
|
|
else if(ShortcodeParser::$error_behavior == ShortcodeParser::STRIP) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $tag['text'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
protected function removeNode($node) {
|
|
|
|
$node->parentNode->removeChild($node);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
protected function insertAfter($new, $after) {
|
|
|
|
$parent = $after->parentNode; $next = $after->nextSibling;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
if ($next) {
|
|
|
|
$parent->insertBefore($new, $next);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$parent->appendChild($new);
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
protected function insertListAfter($new, $after) {
|
|
|
|
$doc = $after->ownerDocument; $parent = $after->parentNode; $next = $after->nextSibling;
|
|
|
|
|
|
|
|
for ($i = 0; $i < $new->length; $i++) {
|
|
|
|
$imported = $doc->importNode($new->item($i), true);
|
|
|
|
|
|
|
|
if ($next) {
|
2016-01-06 00:34:58 +01:00
|
|
|
$parent->insertBefore($imported, $next);
|
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
else {
|
|
|
|
$parent->appendChild($imported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static $marker_class = '--ss-shortcode-marker';
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static $block_level_elements = array(
|
2013-02-14 03:11:10 +01:00
|
|
|
'address', 'article', 'aside', 'audio', 'blockquote', 'canvas', 'dd', 'div', 'dl', 'fieldset', 'figcaption',
|
|
|
|
'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'ol', 'output', 'p',
|
|
|
|
'pre', 'section', 'table', 'ul'
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static $attrrx = '
|
2016-01-06 00:34:58 +01:00
|
|
|
([^\s\/\'"=,]+) # Name
|
2013-02-14 03:11:10 +01:00
|
|
|
\s* = \s*
|
|
|
|
(?:
|
|
|
|
(?:\'([^\']+)\') | # Value surrounded by \'
|
|
|
|
(?:"([^"]+)") | # Value surrounded by "
|
2013-04-26 01:00:13 +02:00
|
|
|
([^\s,\]]+) # Bare value
|
2013-02-14 03:11:10 +01:00
|
|
|
)
|
2013-02-21 22:30:51 +01:00
|
|
|
';
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static function attrrx() {
|
2013-02-21 22:30:51 +01:00
|
|
|
return '/'.self::$attrrx.'/xS';
|
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static $tagrx = '
|
2013-02-21 22:30:51 +01:00
|
|
|
# HTML Tag
|
|
|
|
<(?<element>(?:"[^"]*"[\'"]*|\'[^\']*\'[\'"]*|[^\'">])+)>
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-21 22:30:51 +01:00
|
|
|
| # Opening tag
|
2016-01-06 00:34:58 +01:00
|
|
|
(?<oesc>\[?)
|
|
|
|
\[
|
|
|
|
(?<open>\w+)
|
2013-02-21 22:30:51 +01:00
|
|
|
[\s,]*
|
2016-01-06 00:34:58 +01:00
|
|
|
(?<attrs> (?: %s [\s,]*)* )
|
|
|
|
\/?\]
|
2013-02-21 22:30:51 +01:00
|
|
|
(?<cesc1>\]?)
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-21 22:30:51 +01:00
|
|
|
| # Closing tag
|
2016-01-06 00:34:58 +01:00
|
|
|
\[\/
|
|
|
|
(?<close>\w+)
|
|
|
|
\]
|
|
|
|
(?<cesc2>\]?)
|
2013-02-21 22:30:51 +01:00
|
|
|
';
|
|
|
|
|
2015-04-23 22:30:29 +02:00
|
|
|
protected static function tagrx() {
|
2013-02-21 22:30:51 +01:00
|
|
|
return '/'.sprintf(self::$tagrx, self::$attrrx).'/xS';
|
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
|
|
|
|
const WARN = 'warn';
|
|
|
|
const STRIP = 'strip';
|
|
|
|
const LEAVE = 'leave';
|
|
|
|
const ERROR = 'error';
|
|
|
|
|
|
|
|
public static $error_behavior = self::LEAVE;
|
|
|
|
|
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
2013-02-14 03:11:10 +01:00
|
|
|
* Look through a string that contains shortcode tags and pull out the locations and details
|
|
|
|
* of those tags
|
2009-10-11 02:06:57 +02:00
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* Doesn't support nested shortcode tags
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2009-10-11 02:06:57 +02:00
|
|
|
* @param string $content
|
2013-02-14 03:11:10 +01:00
|
|
|
* @return array - The list of tags found. When using an open/close pair, only one item will be in the array,
|
|
|
|
* with "content" set to the text between the tags
|
2009-10-11 02:06:57 +02:00
|
|
|
*/
|
2013-02-21 22:30:51 +01:00
|
|
|
protected function extractTags($content) {
|
2013-02-14 03:11:10 +01:00
|
|
|
$tags = array();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-22 10:44:00 +02:00
|
|
|
// Step 1: perform basic regex scan of individual tags
|
2015-04-23 22:30:29 +02:00
|
|
|
if(preg_match_all(static::tagrx(), $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
|
2012-05-23 08:18:06 +02:00
|
|
|
foreach($matches as $match) {
|
2013-02-14 03:11:10 +01:00
|
|
|
// Ignore any elements
|
|
|
|
if (empty($match['open'][0]) && empty($match['close'][0])) continue;
|
2013-05-17 13:51:54 +02:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
// Pull the attributes out into a key/value hash
|
|
|
|
$attrs = array();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
if (!empty($match['attrs'][0])) {
|
2015-04-23 22:30:29 +02:00
|
|
|
preg_match_all(static::attrrx(), $match['attrs'][0], $attrmatches, PREG_SET_ORDER);
|
2013-02-14 03:11:10 +01:00
|
|
|
|
|
|
|
foreach ($attrmatches as $attr) {
|
2016-07-01 14:39:52 +02:00
|
|
|
$name = '';
|
|
|
|
$value = '';
|
|
|
|
$parts = array_values(array_filter($attr));
|
|
|
|
//the first element in the array is the complete delcaration (`id=1`) - we don't need this
|
|
|
|
array_shift($parts);
|
|
|
|
|
|
|
|
//the next two parts are what we care about (id and 1 from `id=1`)
|
|
|
|
$name = array_shift($parts) ?: $name;
|
|
|
|
$value = array_shift($parts) ?: $value;
|
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
$attrs[$name] = $value;
|
2016-07-01 14:39:52 +02:00
|
|
|
}
|
2012-05-23 08:18:06 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
// And store the indexes, tag details, etc
|
|
|
|
$tags[] = array(
|
|
|
|
'text' => $match[0][0],
|
|
|
|
's' => $match[0][1],
|
|
|
|
'e' => $match[0][1] + strlen($match[0][0]),
|
2014-03-04 23:47:02 +01:00
|
|
|
'open' => isset($match['open'][0]) ? $match['open'][0] : null,
|
|
|
|
'close' => isset($match['close'][0]) ? $match['close'][0] : null,
|
2013-02-14 03:11:10 +01:00
|
|
|
'attrs' => $attrs,
|
2013-02-21 22:30:51 +01:00
|
|
|
'content' => '',
|
|
|
|
'escaped' => !empty($match['oesc'][0]) || !empty($match['cesc1'][0]) || !empty($match['cesc2'][0])
|
2013-02-14 03:11:10 +01:00
|
|
|
);
|
|
|
|
}
|
2015-06-22 10:44:00 +02:00
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2015-06-22 10:44:00 +02:00
|
|
|
// Step 2: cluster open/close tag pairs into single entries
|
2013-02-14 03:11:10 +01:00
|
|
|
$i = count($tags);
|
|
|
|
while($i--) {
|
2013-02-21 22:30:51 +01:00
|
|
|
if(!empty($tags[$i]['close'])) {
|
2013-02-14 03:11:10 +01:00
|
|
|
// If the tag just before this one isn't the related opening tag, throw an error
|
|
|
|
$err = null;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
if ($i == 0) {
|
|
|
|
$err = 'Close tag "'.$tags[$i]['close'].'" is the first found tag, so has no related open tag';
|
2013-08-21 08:54:05 +02:00
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
else if (!$tags[$i-1]['open']) {
|
2013-08-21 08:54:05 +02:00
|
|
|
$err = 'Close tag "'.$tags[$i]['close'].'" preceded by another close tag "'.
|
|
|
|
$tags[$i-1]['close'].'"';
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
|
|
|
else if ($tags[$i]['close'] != $tags[$i-1]['open']) {
|
2013-08-21 08:54:05 +02:00
|
|
|
$err = 'Close tag "'.$tags[$i]['close'].'" doesn\'t match preceding open tag "'.
|
|
|
|
$tags[$i-1]['open'].'"';
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
if($err) {
|
2016-01-06 00:34:58 +01:00
|
|
|
if(self::$error_behavior == self::ERROR) user_error($err, E_USER_ERRROR);
|
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
else {
|
2013-02-21 22:30:51 +01:00
|
|
|
if ($tags[$i]['escaped']) {
|
|
|
|
if (!$tags[$i-1]['escaped']) {
|
|
|
|
$tags[$i]['e'] -= 1;
|
|
|
|
$tags[$i]['escaped'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($tags[$i-1]['escaped']) {
|
|
|
|
$tags[$i-1]['s'] += 1;
|
|
|
|
$tags[$i-1]['escaped'] = false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
// Otherwise, grab content between tags, save in opening tag & delete the closing one
|
|
|
|
$tags[$i-1]['text'] = substr($content, $tags[$i-1]['s'], $tags[$i]['e'] - $tags[$i-1]['s']);
|
|
|
|
$tags[$i-1]['content'] = substr($content, $tags[$i-1]['e'], $tags[$i]['s'] - $tags[$i-1]['e']);
|
|
|
|
$tags[$i-1]['e'] = $tags[$i]['e'];
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
unset($tags[$i]);
|
2012-05-23 08:18:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
2015-06-22 12:31:12 +02:00
|
|
|
|
|
|
|
// Step 3: remove any tags that don't have handlers registered
|
|
|
|
// Only do this if self::$error_behavior == self::LEAVE
|
|
|
|
// This is optional but speeds things up.
|
|
|
|
if(self::$error_behavior == self::LEAVE) {
|
|
|
|
foreach($tags as $i => $tag) {
|
|
|
|
if(empty($this->shortcodes[$tag['open']])) {
|
|
|
|
unset($tags[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
return array_values($tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces the shortcode tags extracted by extractTags with HTML element "markers", so that
|
|
|
|
* we can parse the resulting string as HTML and easily mutate the shortcodes in the DOM
|
|
|
|
*
|
2018-04-11 10:12:38 +02:00
|
|
|
* @param string $content The HTML string with [tag] style shortcodes embedded
|
|
|
|
* @param array $tags The tags extracted by extractTags
|
|
|
|
* @param callable $generator Callback for performing the replacements
|
|
|
|
* @return string The HTML string with [tag] style shortcodes replaced by markers
|
2013-02-14 03:11:10 +01:00
|
|
|
*/
|
|
|
|
protected function replaceTagsWithText($content, $tags, $generator) {
|
|
|
|
// The string with tags replaced with markers
|
|
|
|
$str = '';
|
|
|
|
// The start index of the next tag, remembered as we step backwards through the list
|
|
|
|
$li = null;
|
|
|
|
|
|
|
|
$i = count($tags);
|
|
|
|
while($i--) {
|
|
|
|
if ($li === null) $tail = substr($content, $tags[$i]['e']);
|
|
|
|
else $tail = substr($content, $tags[$i]['e'], $li - $tags[$i]['e']);
|
|
|
|
|
2013-02-21 22:30:51 +01:00
|
|
|
if ($tags[$i]['escaped']) {
|
|
|
|
$str = substr($content, $tags[$i]['s']+1, $tags[$i]['e'] - $tags[$i]['s'] - 2) . $tail . $str;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$str = $generator($i, $tags[$i]) . $tail . $str;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
$li = $tags[$i]['s'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return substr($content, 0, $tags[0]['s']) . $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the shortcodes in attribute values with the calculated content
|
|
|
|
*
|
|
|
|
* We don't use markers with attributes because there's no point, it's easier to do all the matching
|
|
|
|
* in-DOM after the XML parse
|
|
|
|
*
|
2018-04-11 10:12:38 +02:00
|
|
|
* @param DOMDocument $htmlvalue
|
2013-02-14 03:11:10 +01:00
|
|
|
*/
|
2013-03-11 21:07:06 +01:00
|
|
|
protected function replaceAttributeTagsWithContent($htmlvalue) {
|
|
|
|
$attributes = $htmlvalue->query('//@*[contains(.,"[")][contains(.,"]")]');
|
2013-02-14 03:11:10 +01:00
|
|
|
$parser = $this;
|
|
|
|
|
|
|
|
for($i = 0; $i < $attributes->length; $i++) {
|
|
|
|
$node = $attributes->item($i);
|
|
|
|
$tags = $this->extractTags($node->nodeValue);
|
2013-10-09 06:02:36 +02:00
|
|
|
$extra = array('node' => $node, 'element' => $node->ownerElement);
|
2013-02-14 03:11:10 +01:00
|
|
|
|
|
|
|
if($tags) {
|
2013-08-21 08:54:05 +02:00
|
|
|
$node->nodeValue = $this->replaceTagsWithText($node->nodeValue, $tags,
|
2015-06-22 10:44:00 +02:00
|
|
|
function($idx, $tag) use ($parser, $extra) {
|
|
|
|
return $parser->getShortcodeReplacementText($tag, $extra, false);
|
2013-02-21 23:34:15 +01:00
|
|
|
}
|
2015-06-22 10:44:00 +02:00
|
|
|
);
|
|
|
|
}
|
2012-05-23 08:18:06 +02:00
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
/**
|
|
|
|
* Replace the element-scoped tags with markers
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
*/
|
|
|
|
protected function replaceElementTagsWithMarkers($content) {
|
|
|
|
$tags = $this->extractTags($content);
|
2015-06-22 10:44:00 +02:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
if($tags) {
|
|
|
|
$markerClass = self::$marker_class;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
$content = $this->replaceTagsWithText($content, $tags, function($idx, $tag) use ($markerClass) {
|
|
|
|
return '<img class="'.$markerClass.'" data-tagid="'.$idx.'" />';
|
|
|
|
});
|
2015-06-22 10:44:00 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
return array($content, $tags);
|
2015-06-22 10:44:00 +02:00
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
|
|
|
|
protected function findParentsForMarkers($nodes) {
|
|
|
|
$parents = array();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
foreach($nodes as $node) {
|
|
|
|
$parent = $node;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$parent = $parent->parentNode;
|
2013-05-17 13:51:54 +02:00
|
|
|
}
|
2013-08-21 08:54:05 +02:00
|
|
|
while($parent instanceof DOMElement &&
|
|
|
|
!in_array(strtolower($parent->tagName), self::$block_level_elements));
|
2013-02-14 03:11:10 +01:00
|
|
|
|
|
|
|
$node->setAttribute('data-parentid', count($parents));
|
|
|
|
$parents[] = $parent;
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-05-17 13:51:54 +02:00
|
|
|
return $parents;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
const BEFORE = 'before';
|
|
|
|
const AFTER = 'after';
|
|
|
|
const SPLIT = 'split';
|
|
|
|
const INLINE = 'inline';
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
/**
|
|
|
|
* Given a node with represents a shortcode marker and a location string, mutates the DOM to put the
|
|
|
|
* marker in the compliant location
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* For shortcodes inserted BEFORE, that location is just before the block container that
|
|
|
|
* the marker is in
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* For shortcodes inserted AFTER, that location is just after the block container that
|
|
|
|
* the marker is in
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* For shortcodes inserted SPLIT, that location is where the marker is, but the DOM
|
|
|
|
* is split around it up to the block container the marker is in - for instance,
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* <p>A<span>B<marker />C</span>D</p>
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
|
|
|
* becomes
|
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* <p>A<span>B</span></p><marker /><p><span>C</span>D</p>
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* For shortcodes inserted INLINE, no modification is needed (but in that case the shortcode handler needs to
|
|
|
|
* generate only inline blocks)
|
|
|
|
*
|
|
|
|
* @param DOMElement $node
|
2018-04-11 10:12:38 +02:00
|
|
|
* @param DOMElement $parent
|
|
|
|
* @param string $location ShortcodeParser::BEFORE, ShortcodeParser::SPLIT or ShortcodeParser::INLINE
|
2013-02-14 03:11:10 +01:00
|
|
|
*/
|
|
|
|
protected function moveMarkerToCompliantHome($node, $parent, $location) {
|
|
|
|
// Move before block parent
|
|
|
|
if($location == self::BEFORE) {
|
2013-04-06 22:52:02 +02:00
|
|
|
if (isset($parent->parentNode))
|
2013-02-14 03:11:10 +01:00
|
|
|
$parent->parentNode->insertBefore($node, $parent);
|
2013-04-06 22:52:02 +02:00
|
|
|
} else if($location == self::AFTER) {
|
|
|
|
// Move after block parent
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->insertAfter($node, $parent);
|
|
|
|
}
|
|
|
|
// Split parent at node
|
|
|
|
else if($location == self::SPLIT) {
|
|
|
|
$at = $node; $splitee = $node->parentNode;
|
|
|
|
|
|
|
|
while($splitee !== $parent->parentNode) {
|
|
|
|
$spliter = $splitee->cloneNode(false);
|
|
|
|
|
|
|
|
$this->insertAfter($spliter, $splitee);
|
|
|
|
|
|
|
|
while($at->nextSibling) {
|
|
|
|
$spliter->appendChild($at->nextSibling);
|
|
|
|
}
|
|
|
|
|
|
|
|
$at = $splitee; $splitee = $splitee->parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->insertAfter($node, $parent);
|
|
|
|
}
|
|
|
|
// Do nothing
|
|
|
|
else if($location == self::INLINE) {
|
|
|
|
if(in_array(strtolower($node->tagName), self::$block_level_elements)) {
|
|
|
|
user_error(
|
2013-08-21 08:54:05 +02:00
|
|
|
'Requested to insert block tag '.$node->tagName.
|
2016-01-06 00:34:58 +01:00
|
|
|
' inline - probably this will break HTML compliance',
|
2013-02-14 03:11:10 +01:00
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
user_error('Unknown value for $location argument '.$location, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 08:54:05 +02:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
/**
|
2013-03-11 21:07:06 +01:00
|
|
|
* Given a node with represents a shortcode marker and some information about the shortcode, call the
|
2013-02-14 03:11:10 +01:00
|
|
|
* shortcode handler & replace the marker with the actual content
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-02-14 03:11:10 +01:00
|
|
|
* @param DOMElement $node
|
|
|
|
* @param array $tag
|
|
|
|
*/
|
|
|
|
protected function replaceMarkerWithContent($node, $tag) {
|
2015-06-22 10:44:00 +02:00
|
|
|
$content = $this->getShortcodeReplacementText($tag);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
if ($content) {
|
2013-03-11 21:07:06 +01:00
|
|
|
$parsed = Injector::inst()->create('HTMLValue', $content);
|
|
|
|
$body = $parsed->getBody();
|
|
|
|
if ($body) $this->insertListAfter($body->childNodes, $node);
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->removeNode($node);
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
2013-02-14 03:11:10 +01:00
|
|
|
* Parse a string, and replace any registered shortcodes within it with the result of the mapped callback.
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @return string
|
2009-10-11 02:06:57 +02:00
|
|
|
*/
|
2013-02-14 03:11:10 +01:00
|
|
|
public function parse($content) {
|
2016-11-07 21:45:29 +01:00
|
|
|
|
|
|
|
$this->extend('onBeforeParse', $content);
|
|
|
|
|
|
|
|
$continue = true;
|
|
|
|
|
2013-02-18 05:00:42 +01:00
|
|
|
// If no shortcodes defined, don't try and parse any
|
2016-11-07 21:45:29 +01:00
|
|
|
if(!$this->shortcodes) $continue = false;
|
2013-02-18 05:00:42 +01:00
|
|
|
|
|
|
|
// If no content, don't try and parse it
|
2016-11-07 21:45:29 +01:00
|
|
|
else if (!trim($content)) $continue = false;
|
2013-02-18 05:00:42 +01:00
|
|
|
|
2016-06-30 16:59:55 +02:00
|
|
|
// If no shortcode tag, don't try and parse it
|
2016-11-07 21:45:29 +01:00
|
|
|
else if (strpos($content, '[') === false) $continue = false;
|
2016-06-30 16:59:55 +02:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
if ($continue) {
|
|
|
|
// First we operate in text mode, replacing any shortcodes with marker elements so that later we can
|
|
|
|
// use a proper DOM
|
|
|
|
list($content, $tags) = $this->replaceElementTagsWithMarkers($content);
|
2013-02-18 05:00:42 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
$htmlvalue = Injector::inst()->create('HTMLValue', $content);
|
2013-03-11 21:07:06 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
// Now parse the result into a DOM
|
|
|
|
if (!$htmlvalue->isValid()){
|
|
|
|
if(self::$error_behavior == self::ERROR) {
|
|
|
|
user_error('Couldn\'t decode HTML when processing short codes', E_USER_ERRROR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$continue = false;
|
|
|
|
}
|
2013-02-18 05:00:42 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
if ($continue) {
|
|
|
|
// First, replace any shortcodes that are in attributes
|
|
|
|
$this->replaceAttributeTagsWithContent($htmlvalue);
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
// Find all the element scoped shortcode markers
|
|
|
|
$shortcodes = $htmlvalue->query('//img[@class="'.self::$marker_class.'"]');
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
// Find the parents. Do this before DOM modification, since SPLIT might cause parents to move otherwise
|
|
|
|
$parents = $this->findParentsForMarkers($shortcodes);
|
2015-06-22 12:31:12 +02:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
foreach($shortcodes as $shortcode) {
|
|
|
|
$tag = $tags[$shortcode->getAttribute('data-tagid')];
|
|
|
|
$parent = $parents[$shortcode->getAttribute('data-parentid')];
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
$class = null;
|
|
|
|
if(!empty($tag['attrs']['location'])) $class = $tag['attrs']['location'];
|
|
|
|
else if(!empty($tag['attrs']['class'])) $class = $tag['attrs']['class'];
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
$location = self::INLINE;
|
|
|
|
if($class == 'left' || $class == 'right') $location = self::BEFORE;
|
2016-12-21 06:55:00 +01:00
|
|
|
// Note: center / leftAlone should be `self::SPLIT`, but this crashes so we fall back to self::INLINE
|
|
|
|
// if($class == 'center' || $class == 'leftAlone') $location = self::SPLIT;
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
if(!$parent) {
|
|
|
|
if($location !== self::INLINE) {
|
|
|
|
user_error("Parent block for shortcode couldn't be found, but location wasn't INLINE",
|
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2016-11-07 21:45:29 +01:00
|
|
|
else {
|
|
|
|
$this->moveMarkerToCompliantHome($shortcode, $parent, $location);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->replaceMarkerWithContent($shortcode, $tag);
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
2012-05-23 08:18:06 +02:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
$content = $htmlvalue->getContent();
|
|
|
|
|
|
|
|
// Clean up any marker classes left over, for example, those injected into <script> tags
|
|
|
|
$parser = $this;
|
|
|
|
$content = preg_replace_callback(
|
|
|
|
// Not a general-case parser; assumes that the HTML generated in replaceElementTagsWithMarkers()
|
|
|
|
// hasn't been heavily modified
|
|
|
|
'/<img[^>]+class="'.preg_quote(self::$marker_class).'"[^>]+data-tagid="([^"]+)"[^>]+>/i',
|
|
|
|
function ($matches) use ($tags, $parser) {
|
|
|
|
$tag = $tags[$matches[1]];
|
|
|
|
return $parser->getShortcodeReplacementText($tag);
|
|
|
|
},
|
|
|
|
$content
|
|
|
|
);
|
2013-02-14 03:11:10 +01:00
|
|
|
}
|
2013-03-11 21:07:06 +01:00
|
|
|
|
2016-11-07 21:45:29 +01:00
|
|
|
$this->extend('onAfterParse', $content);
|
2015-06-22 10:44:00 +02:00
|
|
|
|
|
|
|
return $content;
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2012-03-09 02:25:42 +01:00
|
|
|
}
|