You are here

public function CollapseText::filterPrepareRegexCallback in Collapse Text 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/CollapseText.php \Drupal\collapse_text\Plugin\Filter\CollapseText::filterPrepareRegexCallback()

Callback function for the prepare replacement.

Attempt to clean up poorly formatted tags.

File

src/Plugin/Filter/CollapseText.php, line 143

Class

CollapseText
Provides a filter to display Collapsible text blocks.

Namespace

Drupal\collapse_text\Plugin\Filter

Code

public function filterPrepareRegexCallback($matches) {

  // All regexes here are running against an already extracted tag.
  $tag = $matches[0];

  // Allow the [collapsed] open tag.
  $tag = preg_replace('/^                  # start of tag
        \\[                 # open bracket
        (                  # start capture
          collapsed        # the string collapsed
          (?: |\\])         # either a space or a close bracket
        )                  # end capture
      /ix', '[collapse $1', $tag);

  // Fix the collapsed element.
  $tag = preg_replace('/^\\[collapse collapsed( |\\])/i', '[collapse collapsed="collapsed"$1', $tag);

  // Fix the style element. going forward, we prefer "class=".
  $tag = preg_replace('/ style=([^"].*?)(?= collapsed=| title=|\\])/i', ' class="$1"', $tag);
  $tag = preg_replace('/ style="/i', ' class="', $tag);

  // Fix the title element.
  // Not sufficient if title includes double-quotes.
  $tag = preg_replace('/ title=([^"].*?)(?= collapsed=| class=|\\])/i', ' title="$1"', $tag);
  return $tag;
}