Skip to content

Commit

Permalink
Remove invalid attributes from the WYSIWYG editor's allowed tags #232 (
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-cox committed Jul 23, 2024
1 parent 1e3c35b commit 775c8c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ settings:
- '<h4 id>'
- '<h5 id>'
- '<h6 id>'
- '<a hreflang data-entity-substitution data-entity-type data-entity-uuid title>'
- '<a hreflang title>'
- '<ul type>'
- '<img data-entity-type data-entity-uuid data-caption>'
- '<drupal-media data-caption title>'
Expand Down
42 changes: 42 additions & 0 deletions modules/localgov_media/localgov_media.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @file
* LocalGov Drupal Media module install file.
*/

/**
* Remove invalid attributes from the WYSIWYG editor's allowed tags.
*/
function localgov_media_update_10001() {

$invalid_attributes = [
'data-entity-type',
'data-entity-uuid',
'data-entity-substitution',
];
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('editor.editor.wysiwyg');
$allowed_tags = $config->get('settings.plugins.ckeditor5_sourceEditing.allowed_tags');
$updated = FALSE;

foreach ($allowed_tags as $key => $tag) {
if (str_starts_with($tag, '<a')) {
foreach ($invalid_attributes as $attribute) {
if (str_contains($tag, $attribute)) {
$tag = str_replace($attribute, '', $tag);
$updated = TRUE;
}
}

if ($updated) {
$tag = preg_replace('/\s+/', ' ', $tag);
$allowed_tags[$key] = $tag;
$config->set('settings.plugins.ckeditor5_sourceEditing.allowed_tags', $allowed_tags);
$config->save();
}

return;
}
}
}

0 comments on commit 775c8c3

Please sign in to comment.