You are here

function media_wysiwyg_filter_paragraph_fix in D7 Media 7.4

Same name and namespace in other branches
  1. 7.2 modules/media_wysiwyg/includes/media_wysiwyg.filter.inc \media_wysiwyg_filter_paragraph_fix()
  2. 7.3 modules/media_wysiwyg/includes/media_wysiwyg.filter.inc \media_wysiwyg_filter_paragraph_fix()

Filter callback to remove paragraph tags surrounding embedded media.

1 string reference to 'media_wysiwyg_filter_paragraph_fix'
media_wysiwyg_filter_info in modules/media_wysiwyg/media_wysiwyg.module
Implements hook_filter_info().

File

modules/media_wysiwyg/includes/media_wysiwyg.filter.inc, line 47
Functions related to the WYSIWYG editor and the media input filter.

Code

function media_wysiwyg_filter_paragraph_fix($text, $filter) {
  $html_dom = filter_dom_load($text);

  // Store Nodes to remove to avoid inferferring with the NodeList iteration.
  $dom_nodes_to_remove = array();
  foreach ($html_dom
    ->getElementsByTagName('p') as $paragraph) {
    if (preg_match(MEDIA_WYSIWYG_TOKEN_REGEX, $paragraph->nodeValue)) {
      if (empty($filter->settings['replace'])) {
        $sibling = $paragraph->firstChild;
        do {
          $next = $sibling->nextSibling;
          $paragraph->parentNode
            ->insertBefore($sibling, $paragraph);
        } while ($sibling = $next);
        $dom_nodes_to_remove[] = $paragraph;
      }
      else {

        // Clone the P node into a DIV node.
        $div = $html_dom
          ->createElement('div');
        $sibling = $paragraph->firstChild;
        do {
          $next = $sibling->nextSibling;
          $div
            ->appendChild($sibling);
        } while ($sibling = $next);
        $classes = array(
          'media-p',
        );
        if ($paragraph
          ->hasAttributes()) {
          foreach ($paragraph->attributes as $attr) {
            $name = $attr->nodeName;
            $value = $attr->nodeValue;
            if (strtolower($name) == 'class') {
              $classes[] = $value;
            }
            else {

              // Supressing errors with ID attribute or duplicate properties.
              @$div
                ->setAttribute($name, $value);
            }
          }
        }
        $div
          ->setAttribute('class', implode(' ', $classes));
        $paragraph->parentNode
          ->insertBefore($div, $paragraph);
        $dom_nodes_to_remove[] = $paragraph;
      }
    }
  }
  foreach ($dom_nodes_to_remove as $paragraph) {
    $paragraph->parentNode
      ->removeChild($paragraph);
  }
  $text = filter_dom_serialize($html_dom);
  return $text;
}