Commit 33cb44b7 authored by Alan D.'s avatar Alan D. Committed by olstjos

Issue #2870965 by Alan D.: New / Alternative Media Filter P Fix filter option

parent 818f6ed5
......@@ -27,19 +27,58 @@ function media_wysiwyg_filter($text, $filter = NULL, $format = NULL, $langcode =
return $rendered_text;
}
/**
* Filter callback to configure media_filter_paragraph_fix filter.
*/
function _media_filter_paragraph_fix_settings($form, &$form_state, $filter, $format, $defaults) {
$filter->settings += $defaults;
$settings['replace'] = array(
'#type' => 'checkbox',
'#title' => t('Replace paragraph tags with DIV.media-p tags'),
'#default_value' => $filter->settings['replace'],
'#description' => t('Default behaviour is to strip out parent P tags of media elements rather than replacing these.'),
);
return $settings;
}
/**
* Filter callback to remove paragraph tags surrounding embedded media.
*/
function media_wysiwyg_filter_paragraph_fix($text) {
function media_wysiwyg_filter_paragraph_fix($text, $filter) {
$html_dom = filter_dom_load($text);
foreach ($html_dom->getElementsByTagName('p') as $paragraph) {
if (preg_match(MEDIA_WYSIWYG_TOKEN_REGEX, $paragraph->nodeValue)) {
$sibling = $paragraph->firstChild;
do {
$next = $sibling->nextSibling;
$paragraph->parentNode->insertBefore($sibling, $paragraph);
} while ($sibling = $next);
$paragraph->parentNode->removeChild($paragraph);
if (empty($filter->settings['replace'])) {
$sibling = $paragraph->firstChild;
do {
$next = $sibling->nextSibling;
$paragraph->parentNode->insertBefore($sibling, $paragraph);
} while ($sibling = $next);
$paragraph->parentNode->removeChild($paragraph);
}
else {
// Create a new DIV node and copy over all attributes.
$div = $html_dom->createElement("div", $paragraph->nodeValue);
$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));
// Switch the P with the DIV node.
$paragraph->parentNode->insertBefore($div, $paragraph);
$paragraph->parentNode->removeChild($paragraph);
}
}
}
$text = filter_dom_serialize($html_dom);
......
......@@ -12,5 +12,6 @@ test_dependencies[] = wysiwyg
files[] = media_wysiwyg.test
files[] = tests/media_wysiwyg.file_usage.test
files[] = tests/media_wysiwyg.macro.test
files[] = tests/media_wysiwyg.paragraph_fix_filter.test
configure = admin/config/media/browser
......@@ -342,8 +342,12 @@ function media_wysiwyg_filter_info() {
$filters['media_filter_paragraph_fix'] = array(
'title' => t('Ensure that embedded Media tags are not contained in paragraphs'),
'description' => t('This filter will strip any paragraph tags surrounding embedded Media tags. This helps to avoid the chopped up markup that can result from unexpectedly closed paragraph tags. This filter should be positioned above (before) the "Convert Media tags to markup" filter.'),
'description' => t('This filter will fix any paragraph tags surrounding embedded Media tags. This helps to avoid the chopped up markup that can result from unexpectedly closed paragraph tags. This filter should be positioned above (before) the "Convert Media tags to markup" filter.'),
'process callback' => 'media_wysiwyg_filter_paragraph_fix',
'settings callback' => '_media_filter_paragraph_fix_settings',
'default settings' => array(
'replace' => 0,
),
'weight' => 1,
);
......@@ -389,7 +393,7 @@ function media_wysiwyg_allowed_attributes() {
'data-delta',
));
drupal_alter('media_wysiwyg_allowed_attributes', $allowed_attributes);
return $allowed_attributes;
return $allowed_attributes;
}
/**
......@@ -603,7 +607,7 @@ function media_wysiwyg_form_file_entity_file_type_form_alter(&$form, &$form_stat
if (empty($form_state['build_info']['args'][0])) {
return;
}
$options = array();
// Add an option allowing users not to use a view mode.
......
......@@ -58,9 +58,11 @@ abstract class MediaWYSIWYGTestHelper extends DrupalWebTestCase {
);
// Create the file usage markup.
$markup .= '<p>Intro paragraph</p>';
for ($i = 1; $i <= $count; $i++) {
$markup .= '<p>[[' . drupal_json_encode($data) . ']]</p>';
}
$markup .= '<p>Finish paragraph</p>';
return $markup;
}
......
<?php
/**
* @file
* Tests for ensuring filters are working properly.
*/
/**
* Defines media macro test cases.
*/
class MediaWYSIWYGParagraphFixFilterTest extends MediaWYSIWYGTestHelper {
protected $default_regexp = '/<p[^>]*><div [^>]*media[^>]*>/i';
protected $replace_regexp = '/<div class\="media\-p"><div/i';
/**
* Provide test information.
*/
public static function getInfo() {
return array(
'name' => t('Media WYSIWYG Paragraph Filter Test'),
'description' => t('Tests that this media filter is working.'),
'group' => t('Media WYSIWYG'),
'dependencies' => array('token'),
);
}
public function setUp() {
parent::setUp('token');
// Create and log in a user.
$account = $this->drupalCreateUser(array('create article content', 'administer filters', 'use text format filtered_html'));
$this->drupalLogin($account);
// Enable the media filter for full html.
$edit = array(
'filters[media_filter][status]' => TRUE,
'filters[filter_autop][status]' => FALSE,
'filters[filter_url][status]' => FALSE,
'filters[filter_html][status]' => FALSE,
'filters[filter_htmlcorrector][status]' => FALSE,
);
$this->drupalPost('admin/config/content/formats/filtered_html', $edit, t('Save configuration'));
}
/**
* Test image media overrides.
*/
public function testMediaFilterParagraphFixDefault() {
$files = $this->drupalGetTestFiles('image');
$file = file_save($files[0]);
// Create a node to test with.
$nid = $this->createNode($file->fid);
// Check without the filter enabled.
$this->drupalGet('node/' . $nid);
$this->assertPattern($this->default_regexp, t('Nested media DIV tags within paragraphs without filter.'));
$this->assertNoPattern($this->replace_regexp, t('No replacement DIV tag found without filter.'));
// Enable the default P fix filter.
$edit = array(
'filters[media_filter_paragraph_fix][status]' => TRUE,
'filters[media_filter_paragraph_fix][settings][replace]' => FALSE,
);
$this->drupalPost('admin/config/content/formats/filtered_html', $edit, t('Save configuration'));
// Retest the content to check nested paragraphs are removed.
$nid = $this->createNode($file->fid);
$this->drupalGet('node/' . $nid);
$this->assertNoPattern($this->default_regexp, t('Nested media DIV tags within paragraphs with filter defaults.'));
$this->assertNoPattern($this->replace_regexp, t('No replacement DIV tag found with filter defaults.'));
// Enable replacement option.
$edit = array(
'filters[media_filter_paragraph_fix][settings][replace]' => TRUE,
);
$this->drupalPost('admin/config/content/formats/filtered_html', $edit, t('Save configuration'));
// Test that the replace text was found.
$nid = $this->createNode($file->fid);
$this->drupalGet('node/' . $nid);
$this->assertNoPattern($this->default_regexp, t('No nested media DIV tags within paragraphs with filter P replacement.'));
$this->assertPattern($this->replace_regexp, t('No replacement DIV tag found with filter P replacement.'));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment