$xpath =
new \
DOMXPath($dom);
$html_filter =
$this->filterManager->
createInstance('filter_html',
[ 'settings' =>
[ 'allowed_html' => '<a href hreflang target rel> <em> <strong> <cite> <code> <br>',
'filter_html_help' => FALSE,
'filter_html_nofollow' => FALSE,
],
]);
foreach ($xpath->
query('//*[@data-caption]'
) as $node) { // Read the data-caption attribute's value, then delete it.
$caption = Html::
escape($node->
getAttribute('data-caption'
));
$node->
removeAttribute('data-caption'
);
// Sanitize caption: decode HTML encoding, limit allowed HTML tags; only
// allow inline tags that are allowed by default, plus <br>.
$caption = Html::
decodeEntities($caption);
$raw_caption =
$caption;
$filtered_caption =
$html_filter->
process($caption,
$langcode);
$result->
addCacheableDependency($filtered_caption);
$caption = FilteredMarkup::
create($filtered_caption->
getProcessedText());
// The caption must be non-empty - however the Media Embed CKEditor
// plugin uses a single space to represent a newly added caption. The