Commit 33415a3c authored by Alan D.'s avatar Alan D. Committed by olstjos

Issue #2871960 by Alan D.: Paragraph filter misses some DOM Nodes

parent 73e3eabd
......@@ -46,6 +46,8 @@ function _media_filter_paragraph_fix_settings($form, &$form_state, $filter, $for
*/
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'])) {
......@@ -54,7 +56,7 @@ function media_wysiwyg_filter_paragraph_fix($text, $filter) {
$next = $sibling->nextSibling;
$paragraph->parentNode->insertBefore($sibling, $paragraph);
} while ($sibling = $next);
$paragraph->parentNode->removeChild($paragraph);
$dom_nodes_to_remove[] = $paragraph;
}
else {
// Clone the P node into a DIV node.
......@@ -82,10 +84,13 @@ function media_wysiwyg_filter_paragraph_fix($text, $filter) {
$div->setAttribute('class', implode(' ', $classes));
$paragraph->parentNode->insertBefore($div, $paragraph);
$paragraph->parentNode->removeChild($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;
}
......
......@@ -10,8 +10,27 @@
*/
class MediaWYSIWYGParagraphFixFilterTest extends MediaWYSIWYGTestHelper {
protected $default_regexp = '/<p[^>]*><div [^>]*media[^>]*>/i';
protected $replace_regexp = '/<div class\="media\-p"><div/i';
/**
* Defines the regex to test for a media tag replacement.
*
* @var string
*/
protected $regexpMediaTag = '/<div [^>]*media\-element\-container[^>]*>/i';
/**
* Defines the regex to test for in the raw body field source.
*
* @var string
*/
protected $regexpPWrapped = '/<p[^>]*><div [^>]*media\-element\-container[^>]*>/i';
/**
* Defines the regex to test for the P replacement filter.
*
* @var string
*/
protected $regexpReplaced = '/<div class\="media\-p"><div/i';
/**
* Provide test information.
*/
......@@ -24,11 +43,18 @@ class MediaWYSIWYGParagraphFixFilterTest extends MediaWYSIWYGTestHelper {
);
}
/**
* Set-up the system for testing without the filter enabled.
*/
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'));
$account = $this->drupalCreateUser(array(
'create article content',
'administer filters',
'use text format filtered_html',
));
$this->drupalLogin($account);
// Enable the media filter for full html.
......@@ -44,8 +70,64 @@ class MediaWYSIWYGParagraphFixFilterTest extends MediaWYSIWYGTestHelper {
/**
* Test image media overrides.
*/
public function testMediaFilterParagraphFixDefault() {
public function testMediaFilterParagraphFixMultipleImages() {
$files = $this->drupalGetTestFiles('image');
$file = file_save($files[0]);
// Create a node to test with 3 images.
$nid = $this->createNode($file->fid);
$node = node_load($nid);
$node->body[LANGUAGE_NONE][0]['value'] = $this->generateJsonTokenMarkup($file->fid, 3);
node_save($node);
// Check without the filter enabled.
$html = $this->drupalGet('node/' . $nid);
$count = preg_match_all($this->regexpMediaTag, $html);
$this->assertEqual($count, 3, t('Three media tags found, found @count.', array('@count' => $count)));
$count = preg_match_all($this->regexpPWrapped, $html);
$this->assertEqual($count, 3, t('Three media tags with original wrapping HTML present, found @count.', array('@count' => $count)));
$count = preg_match_all($this->regexpReplaced, $html);
$this->assertEqual($count, 0, t('No media tags with P replaced present, found @count.', array('@count' => $count)));
// Enable the default P fix filter.
$edit = array(
'filters[media_filter_paragraph_fix][status]' => TRUE,
);
$this->drupalPost('admin/config/content/formats/filtered_html', $edit, t('Save configuration'));
$html = $this->drupalGet('node/' . $nid);
$count = preg_match_all($this->regexpMediaTag, $html);
$this->assertEqual($count, 3, t('Three media tags found, found @count.', array('@count' => $count)));
$count = preg_match_all($this->regexpPWrapped, $html);
$this->assertEqual($count, 0, t('No media tags with original wrapping HTML present, found @count.', array('@count' => $count)));
$count = preg_match_all($this->regexpReplaced, $html);
$this->assertEqual($count, 0, t('No media tags with P replaced present, found @count.', array('@count' => $count)));
// Enable the replace P fix filter option.
$edit = array(
'filters[media_filter_paragraph_fix][settings][replace]' => TRUE,
);
$this->drupalPost('admin/config/content/formats/filtered_html', $edit, t('Save configuration'));
$html = $this->drupalGet('node/' . $nid);
$count = preg_match_all($this->regexpMediaTag, $html);
$this->assertEqual($count, 3, t('Three media tags found, found @count.', array('@count' => $count)));
$count = preg_match_all($this->regexpPWrapped, $html);
$this->assertEqual($count, 0, t('No media tags with original wrapping HTML present, found @count.', array('@count' => $count)));
$count = preg_match_all($this->regexpReplaced, $html);
$this->assertEqual($count, 3, t('Three media tags with P replaced present, found @count.', array('@count' => $count)));
}
/**
* Test image media overrides.
*/
public function testMediaFilterParagraphFixDefault() {
$files = $this->drupalGetTestFiles('image');
$file = file_save($files[0]);
......@@ -54,8 +136,8 @@ class MediaWYSIWYGParagraphFixFilterTest extends MediaWYSIWYGTestHelper {
// 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.'));
$this->assertPattern($this->regexpPWrapped, t('Nested media DIV tags within paragraphs without filter.'));
$this->assertNoPattern($this->regexpReplaced, t('No replacement DIV tag found without filter.'));
// Enable the default P fix filter.
$edit = array(
......@@ -65,8 +147,8 @@ class MediaWYSIWYGParagraphFixFilterTest extends MediaWYSIWYGTestHelper {
// Retest the content to check nested paragraphs are removed.
$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.'));
$this->assertNoPattern($this->regexpPWrapped, t('Nested media DIV tags within paragraphs with filter defaults.'));
$this->assertNoPattern($this->regexpReplaced, t('No replacement DIV tag found with filter defaults.'));
// Enable replacement option.
$edit = array(
......@@ -76,8 +158,8 @@ class MediaWYSIWYGParagraphFixFilterTest extends MediaWYSIWYGTestHelper {
// Test that the replace text was found.
$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.'));
$this->assertNoPattern($this->regexpPWrapped, t('No nested media DIV tags within paragraphs with filter P replacement.'));
$this->assertPattern($this->regexpReplaced, 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