Problem/Motivation

The FilterCaption plugin currently hard-codes which tags are allowed in image captions:

      $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,
        ],
      ]);

There are many other inline HTML tags that one might want to use in image captions, so this should probably be configurable.

Proposed resolution

Make these tags configurable.

Remaining tasks

  • Allow attribute whitelisting.

User interface changes

Would add configuration for the image caption filter.

Data model changes

Adds configuration options.

CommentFileSizeAuthor
#82 2508421_ckeditor5_caption_frontend.png288.35 KBDuaelFr
#82 2508421_ckeditor5_caption_backend.png169.6 KBDuaelFr
#79 2508421-nr-bot.txt144 bytesneeds-review-queue-bot
#74 2508421-74.patch22.68 KBandregp
#74 interdiff_2508421_73-74.txt338 bytesandregp
#73 2508421-73.patch22.84 KBandregp
#73 interdiff_2508421_72-73.txt4.24 KBandregp
#72 filter-caption-tags-2508421-67-D9.2.patch22.87 KBnyanmar
#67 interdiff_61-67.txt3.29 KBraman.b
#67 2508421-67.patch19.75 KBraman.b
#65 filter-caption-tags-2508421-62-D8.9.7.patch18.76 KBdkosbob
#63 filter-caption-tags-2508421-62-D8.9.6.patch18.73 KBAaronBauman
#62 filter-caption-tags-2508421-62-D8.patch18.73 KBAaronBauman
#61 diff_reroll_2508421_54-61.txt7.99 KBankithashetty
#61 2508421-61.patch19.16 KBankithashetty
#54 2508421-54.patch18.27 KBDakwamine
#51 2508421-interdiff-45-51.txt11.17 KBDakwamine
#51 2508421-51.patch18.35 KBDakwamine
#46 interdiff-44-45.txt491 bytesDakwamine
#46 2508421-45.patch9.17 KBDakwamine
#45 2508421-44.patch9.17 KBDakwamine
#38 2508421-37.patch7.31 KBesod
#37 2508421-37.patch7.31 KBesod
#34 2508421-34.patch7.4 KBesod
#32 2508421-32.patch7.36 KBjhedstrom
#30 2508421-30.patch34.32 KBjhedstrom
#30 interdiff.txt1.13 KBjhedstrom
#24 filtercaption-allowed-tags-2508421-23.patch6.22 KBjhedstrom
#23 filtercaption-allowed-tags-2508421-23.patch6.22 KBjhedstrom
#13 filtercaption-allowed-tags-2508421-13.patch6.22 KBjhedstrom
#13 filtercaption-allowed-tags-2508421-13-TEST-ONLY.patch1.11 KBjhedstrom
#13 interdiff.txt826 bytesjhedstrom
#11 filtercaption-allowed-tags-2508421-11.patch6.21 KBjhedstrom
#10 filtercaption-allowed-tags-2508421-10.patch6.14 KBjhedstrom
#7 filtercaption-allowed-tags-2508421-06.patch6.12 KBjhedstrom
#4 filtercaption-allowed-tags-2508421-04.patch6.11 KBjhedstrom
#4 filtercaption-allowed-tags-2508421-04-TEST-ONLY.patch1.08 KBjhedstrom
#4 interdiff.txt1.08 KBjhedstrom
#3 interdiff.txt2.36 KBjhedstrom
#3 filtercaption-allowed-tags-2508421-03.patch5.03 KBjhedstrom
#1 filtercaption-allowed-tags-2508421-01.patch2.68 KBjhedstrom
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhedstrom’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
2.68 KB

This makes these tags configurable, but CKEditor isn't aware of the change since this isn't an html restrictor type, not sure how to proceed there.

jhedstrom’s picture

Issue summary: View changes
jhedstrom’s picture

Issue summary: View changes
FileSize
5.03 KB
2.36 KB
jhedstrom’s picture

jhedstrom’s picture

Issue tags: +wysiwyg

Tagging WYSIWYG for now. Tempted to tag drupalwtf since we allow the 'code' tag but not other inline elements.

The last submitted patch, 4: filtercaption-allowed-tags-2508421-04-TEST-ONLY.patch, failed testing.

jhedstrom’s picture

FileSize
6.12 KB

Reroll of #5 which no longer cleanly applied.

Status: Needs review » Needs work

The last submitted patch, 7: filtercaption-allowed-tags-2508421-06.patch, failed testing.

jhedstrom’s picture

Status: Needs work » Needs review
FileSize
6.14 KB

Re-roll.

jhedstrom’s picture

Re-roll since SafeMarkup was removed.

Status: Needs review » Needs work

The last submitted patch, 11: filtercaption-allowed-tags-2508421-11.patch, failed testing.

jhedstrom’s picture

Wim Leers’s picture

Version: 8.0.x-dev » 8.1.x-dev
Category: Bug report » Feature request

I'm sorry, but this was a conscious choice back then to KISS, to have sane defaults. Having the ability to configure this is a nice-to-have, a pure addition, that we can do in 8.1.

Wim Leers’s picture

To be clear: +1 to this, and I'm very grateful that you're working on this! :) But right now, we need to focus on getting RC1 out.

The last submitted patch, 13: filtercaption-allowed-tags-2508421-13-TEST-ONLY.patch, failed testing.

The last submitted patch, 11: filtercaption-allowed-tags-2508421-11.patch, failed testing.

The last submitted patch, 13: filtercaption-allowed-tags-2508421-13-TEST-ONLY.patch, failed testing.

The last submitted patch, 4: filtercaption-allowed-tags-2508421-04.patch, failed testing.

The last submitted patch, 13: filtercaption-allowed-tags-2508421-13-TEST-ONLY.patch, failed testing.

jhedstrom’s picture

Version: 8.1.x-dev » 8.0.x-dev
FileSize
6.22 KB

Re-roll. Temporarily back to 8.0.x to check tests (would be great if 8.1.x were up to date).

jhedstrom’s picture

Tests not queued, trying again.

Status: Needs review » Needs work

The last submitted patch, 24: filtercaption-allowed-tags-2508421-23.patch, failed testing.

jhedstrom’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs work » Needs review

Back to 8.1. That fail looks unrelated, but perhaps not.

Status: Needs review » Needs work

The last submitted patch, 24: filtercaption-allowed-tags-2508421-23.patch, failed testing.

jhedstrom’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 24: filtercaption-allowed-tags-2508421-23.patch, failed testing.

jhedstrom’s picture

This fixes the config installer test.

Wim Leers’s picture

Looks like another patch is included with that one :)

Shouldn't the setting be called something like "allowed_caption_html"?

And shouldn't it also allow for attribute whitelisting, just like the HTML filter?

jhedstrom’s picture

Issue summary: View changes
FileSize
7.36 KB

Oops, here's a proper patch. There was some confusion in my local setup regarding the 8.1.x branch.

Shouldn't the setting be called something like "allowed_caption_html"?

Possibly...but in the context of the filter caption plugin, is that overly verbose?

And shouldn't it also allow for attribute whitelisting, just like the HTML filter?

Definitely. I've updated the IS.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

esod’s picture

Rerolling Patch #32 because it won't apply to current 8.1.x. I can't do an interdiff for the same reason.

Wim Leers’s picture

+++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImageCaption.php
@@ -62,10 +62,32 @@ public function getConfig(Editor $editor) {
+   *   String of the form '<tag1> <tag2> <a>'.
...
+   *   String of the form 'tag1 tag2; a[!href]'.
...
+      $anchor_tag = '; a[!href]';

The href attribute is hardcoded.

We should not do that. Instead, we should adopt the pattern that FilterHtml uses since #2549077: Allow the "Limit allowed HTML tags" filter to also restrict HTML attributes, and only allow a small whitelist of attributes by default, with explicit whitelisting of attributes or even attribute values.

Because of that, this patch basically needs to be redone from scratch, because it is based on FilterHtml code from before that issue. It'll be easier to copy/paste the updated FilterHtml code than continuing with this patch.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

esod’s picture

FileSize
7.31 KB

Moving the test code to FilterKernelTest and rerolling the patch for 8.2.x.

Starting to look into redoing the patch with the updated FilterHtml code from Allow the "Limit allowed HTML tags" filter to also restrict HTML attributes, and only allow a small whitelist of attributes by default, which has been merged in to 8.3.x

esod’s picture

Status: Needs work » Needs review
FileSize
7.31 KB

Same patch. Changing Status to Needs review to get it tested.

Status: Needs review » Needs work

The last submitted patch, 38: 2508421-37.patch, failed testing.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Dakwamine’s picture

This patch does not apply anymore on 8.5. Allowed tags are still hardcoded in the FilterCaption filter.

Dakwamine’s picture

Status: Needs work » Needs review
FileSize
9.17 KB

Updated the previous patch for 8.7.

Noticeable code wise differences:

  1. In DrupalImageCaption.php, this line looked accidentally removed from the previous patch, so I have restored it: 'drupalImageCaption_alignFilterEnabled' => $format->filters('filter_align')->status,
  2. Used ES6 as stated here: https://www.drupal.org/node/2815083
  3. Updated config/install on umami install profile to use the same config as standard profile

I was able to patch on 8.5.4 and it works as expected on my site which uses entity embed. I have not tested with default captions on native inline images though.

Some tests may need a little refresh.

Dakwamine’s picture

Added missing blank line.

Anybody’s picture

#45 works great for me. RTBC+1

Wim Leers’s picture

Status: Needs review » Needs work

Thank you very much for working on this! 🙏 (Merci beaucoup!)

And shouldn't it also allow for attribute whitelisting, just like the HTML filter?

Definitely. I've updated the IS.

This is not yet done.

The feedback in #35 also has not yet been addressed.

Do you think you could also address that? 🙂

Dakwamine’s picture

You may expect from me an update next week if noone has already addressed the feedback until then.

Wim Leers’s picture

WOOT! 🎉 🚀

Dakwamine’s picture

Hi, sorry for the delay.

This is still a work in progress. I have tried to reuse the same filter system as FilterHtml. It works as expected, exactly the same way as FilterHtml: tags are first stripped out, then invalid attributes are removed. Here are my thoughts:

  1. The result between the old way and the new one is not 100% the same depending on some factors. My guess is that spaces before br tags are removed.
  2. What would be the best way to reduce code duplicate? The system—a set of five public and protected functions—is 99% reusable between those two filters so it could be interesting to use the same system for both FilterHtml and FilterCaption.
  3. In the original FilterHtml, there is a check on a filter config, filter_html_nofollow. This is why I had to add a new config to FilterCaption. Should we aswell let the user chose if he wants the rel="nofollow" attribute on links in the caption config? I am not sure about the consequences of this setting. Would it not be redundant with the "main" FilterHtml process if this one is also enabled?
  4. And finally, the comments in FilterCaption states that only inline tags should be allowed. But this does not seem true, at least in the current days with the current HTML5 specs. Should we restrict the allowed tags, or should we let the user freely chose what he can insert in the captions? IMO, it could be bothersome to update this part of the code to follow the developments of the HTML standard so I would prefer to let it free.
Dakwamine’s picture

Status: Needs work » Needs review

Inputs needed. :)

Status: Needs review » Needs work

The last submitted patch, 51: 2508421-51.patch, failed testing. View results

Dakwamine’s picture

Status: Needs work » Needs review
FileSize
18.27 KB

Ooops sorry, I was not up to date with the 8.7.x branch.

Status: Needs review » Needs work

The last submitted patch, 54: 2508421-54.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jonraedeke’s picture

This patch is still working well for me. I don't really know enough to give the feedback that was requested though.

phernand42’s picture

Patch in #54 doesnt seem to be applyling to core version 8.9.6.

ankithashetty’s picture

Status: Needs work » Needs review
FileSize
19.16 KB
7.99 KB

Re-rolling the patch in #54 against latest core 9.1.x dev version, so that it'll be easier to test by others and also attaching a diff_reroll file... Thanks!

AaronBauman’s picture

Here's a re-roll of #54 that applies to 8.9.x in case anyone else is not on 9 yet like me.

AaronBauman’s picture

And here's a reroll of #54 that applies to 8.9.6 (current HEAD has breaking changes)

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

dkosbob’s picture

Looks like the use declaration of Drupal\Component\Utility\Xss was removed in 8.9.7. Here is another reroll for that.

AaronBauman’s picture

Status: Needs review » Needs work

I think testbot is refusing to test because of coding standards issues.

Need a line break here between property definition and method doc:

   /**
+   * The processed HTML restrictions.
+   *
+   * @var array
+   */
+  protected $restrictions;
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsForm(array $form, FormStateInterface $form_state) {

2 other issues reported with javascript - not sure if those are blocking tests or not...

raman.b’s picture

This should give us a better idea of which tests are actually failing

jonraedeke’s picture

We'll need to do this for the drupalmedia plugin as well.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nkraft’s picture

I would love to be able use SUP and SUB tags inside my figcaptions. Anything I can do to bring this patch back to life so we can roll it into 9.3.x?
Thanks!

nyanmar’s picture

Here's the addition with the drupalmedia plugin

andregp’s picture

Status: Needs work » Needs review
FileSize
4.24 KB
22.84 KB

@rerolled for 9.4.x and fixed some CS errors from the patch.

andregp’s picture

Status: Needs review » Needs work

The last submitted patch, 74: 2508421-74.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Needs review

Looks like test failures are unrelated.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

AaronBauman’s picture

is this still an issue for ckeditor5?

DuaelFr’s picture

Yes! I can confirm it is still an issue. See \Drupal\filter\Plugin\Filter\FilterCaption::process().
I cannot find any restriction on the CKEditor side anymore, though.

Backend:

Frontend:

DuaelFr’s picture

Issue summary: View changes
Issue tags: +ckeditor5, +caption

Updated IS