Skip to content
Snippets Groups Projects
Commit 243e2f32 authored by Benjamin Melançon's avatar Benjamin Melançon
Browse files

Issue #3410145 by mlncn: Paragraph tags are not stripped if the p tag has...

Issue #3410145 by mlncn: Paragraph tags are not stripped if the p tag has attributes like dir=ltr or title
parent 11e370c6
No related branches found
No related tags found
No related merge requests found
......@@ -24,9 +24,6 @@ class StripParagraphTags extends FilterBase {
* {@inheritdoc}
*/
public function process($text, $langcode) {
// For the moment, we heavily rely on the fact that we do not allow any classes or other attributes on our paragraph tags.
// A more robust approach could use regex or Simple HTML DOM: https://stackoverflow.com/a/11229741/1028376
return new FilterProcessResult(str_replace(['<p>', '</p>'], '', $text));
return new FilterProcessResult(strippfilter_process($text));
}
}
<?php
use Drupal\Core\Routing\RouteMatchInterface;
use \DOMDocument;
/**
* Implements hook_help().
......@@ -18,3 +19,31 @@ function strippfilter_help($route_name, RouteMatchInterface $route_match) {
default:
}
}
function strippfilter_process($text) {
// We CANNOT rely on not allowing attributes on our paragraph tags.
// CKEDitor5 sabatoges us AGAIN and keeps any dir, title, etc that comes
// from pasting from somewhere unshakeably right on the paragraph tag.
// So a super simple and efficient str_replace(['<p>', '</p>'] is out, but
// we always sort of knew that. We use DOMDocument for a robust approach.
$dom = new DOMDocument;
$dom->loadHTML($text);
$paragraphs = $dom->getElementsByTagName("p");
$innerHTML = '';
foreach ($paragraphs as $p) {
$innerHTML .= DOMinnerHTML($p);
}
return $innerHTML;
}
function DOMinnerHTML($element) {
$innerHTML = '';
$children = $element->childNodes;
foreach ($children as $child) {
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
// We trim the newlines this process adds to keep the result looking nice.
$innerHTML .= trim($tmp_dom->saveHTML(), "\n");
}
return $innerHTML;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment