Problem/Motivation
When multiple filters of the type "FilterInterface::TYPE_HTML_RESTRICTOR" are enabled in CKEditor5, the site crashes with an error.
TypeError: Drupal\ckeditor5\HTMLRestrictions::__construct(): Argument #1 ($elements) must be of type array, Drupal\ckeditor5\HTMLRestrictions given, called in /app/web/core/modules/ckeditor5/src/Plugin/Validation/Constraint/FundamentalCompatibilityConstraintValidator.php on line 157 in Drupal\ckeditor5\HTMLRestrictions->__construct() (line 82 of core/modules/ckeditor5/src/HTMLRestrictions.php).
Steps to reproduce
Enable the FilerHTML plugin for CKEditor5 and another plugin of the type TYPE_HTML_RESTRICTOR. In my case it was a custom filter but you can use another module, like extend_html_filter (I know they are basically the same, but I needed another plugin of this type to reproduce).
When you save the text format, the error above appears.
Proposed resolution
The error is caused by the fact that the "provided" variable is overwritten. It is first an array with allowed HTML tags and attributes, but in the foreach loop that passes through all restrictor filters the variable is overwritten by a HTMLRestrictions instance.
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | 3326778-15.patch | 3.53 KB | graber |
Issue fork drupal-3326778
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
web-beestProvided patch tested on Drupal core 9.4.9
Comment #3
cilefen commented@Web-Beest Test runs cost the Drupal Association money. It is seldom necessary to test on every platform combination.
Comment #4
web-beestI'm sorry, I did not know.
Comment #5
web-beestComment #6
catchI don't yet have clear steps to reproduce, but I hit this on a 9.4 site, then updated to 9.5 to see if it was resolved, and got the same error. This was trying to change
tableto<table class>in the code editing section of the basic HTML format.Trying to do the same thing on a clean(ish) install of core it works fine.
Comment #7
graber commentedNot sure if it's related to the fix or not (we'll need to update the issue title if so), but after applying this our config failed validation where it shouldn't. Here's a fix for that as well.
Comment #8
graber commentedAnd ofc this will need tests before the gates open :|
Looking at 10.x code, it's also an issue there.
As for steps to reproduce (EDIT: Just noticed this is in the issue description - embarassed):
- add more than 1
Drupal\filter\Plugin\FilterInterface::TYPE_HTML_RESTRICTORtype filter to an editor instance- save the form.
There are 3 such filters in core: filter_html, filter_html_escape and filter_null. It doesn't make sense to use the first 2 together and the 3rd one is a fallback filter not to be used directly so there's no situation in pure core where 2 such filters are used at the same time.
As for the second bug I fixed - to reproduce it needs a TYPE_HTML_RESTRICTOR type filter that doesn't restrict anything (yes, doesn't make sense but it's technically possible to create such filter with proper type annotation and
getHTMLRestrictions()method returning FALSE or an empty array - enough not to override the base class method). Probably a proper fix would be throwing an exception in filter manager when such a filter is discovered.Comment #9
graber commentedTo think of it more.. the second fix is not complete. Imagine 2 restrictors restricting different HTML elements.. Those should be merged first in a foreach and only then checked against provided elements, otherwise we'll get errors for each such plugin. Haven't tested for that though.
Comment #10
catchI tried enabling both
filter_htmlandfilter_html_escapeon a standard install of Drupal 9.5, and wasn't able to reproduce the error that way - even though it doesn't make sense, it would have made test coverage easier (and been enough to promote this to critical).Comment #11
wim leersIMHO there is no reason to wait for test coverage for this particular issue. #2 clearly shows the problem.
This only happens in case there are >1 restrictor filters. And indeed you cannot reproduce this with
filter_html_escapebecause that disallows all markup, which means you'll never hit the call toFundamentalCompatibilityConstraintValidator::checkHtmlRestrictionsMatch()inFundamentalCompatibilityConstraintValidator::validate(). I see that's also what @Graber wrote in #9 — he's totally right! 🤓Wrote test that does just that.
Comment #13
wim leersThe interdiff is #2.
Comment #14
graber commentedWe don't need to call
$provided = new HTMLRestrictions($provided_elements);in each loop iteration since we're assigning the same value every time and nothing in the loop changes that value. Let's move this out of the loop.Comment #15
graber commentedComment #16
wim leersHah! Good catch 👍😊
@Graber: you're still able to RTBC. #15 is a trivial change, so doesn't prevent you 😇
Comment #17
graber commentedRight, test passed locally for me as well so should be fine.
Comment #18
graber commentedon 10.1.x though.. too early.
Comment #20
wim leersThat's fine — I'm confident the patch will still be green on
10.1.xand10.0.x— because it was in #13. This will need to land in the newest branch first anyway. We can worry about a9.5.x-specific reroll after it lands.Comment #21
alexpottCommitted and pushed 6e2e2cd055 to 10.1.x and c9118460e5 to 10.0.x. Thanks!
Now we can try and make the test work on Drupal 9.5.
Comment #24
diaodiallo commentedHi! anyone have this fixed in 9.5.x I am having this issue on a 9.5.9 Drupal site.
#7 is not working yet.
Thanks.
Comment #25
alpha2zee commentedJust noting that this issue occurs with the htmLawed text filter module (TYPE_HTML_RESTRICTOR) in D9 but not D10.
Comment #26
othermachines commentedAlso trying to upgrade to CKEditor 5 while still on 9.x (which is strongly recommended in the 9.5.x release notes) - Is there a workaround while we wait for a fix?
Comment #27
solideogloria commentedPatch #15 seems to work in Drupal 9.5.x for me.
Comment #28
solideogloria commentedActually, while the patch applies correctly, I can't get any of my text formats to save using CKEditor 5.
#3384398: Unclear validation errors when upgrading text formats to CKEditor 5
Comment #29
solideogloria commentedComment #30
solideogloria commentedI had to disable the filter provided by Noopener Filter, then I could save the text formats.
Comment #32
samitk commentedHello,
I am not able to reproduce it with Drupal 11.x, Looks like this issue has been resolved,
As per the comment #21changes has been pushed to 10.0.x and 10.1.x branch.
I guess we can close it.
Thanks
Samit K.
Comment #33
solideogloria commentedIt is Needs Work for Drupal 9.5.x.
Comment #35
cilefen commentedDrupal 9 is end of life so we can close this. 9 isn’t supported here.
Comment #36
quietone commentedSince this was committed to D10, so restoring that version.