Problem/Motivation

Depends on #3228334: Refactor HTMLRestrictionsUtilities to a HtmlRestrictions value object.

If a text format has partial wildcard attributes, especially data-*, the upgrade path currently will not work correctly.

This will fail in the upgrade path (i.e. will configure the "Source Editing" plugin incorrectly), but likely also fails to configure CKE5 GHS correctly: this goes further than what #3259493: [GHS] Unable to limit attribute values: ::allowedElementsStringToHtmlSupportConfig() does not generate configuration that CKEditor 5 expects fixes.

Related, but on the JS side of making data-* support happen: #3227822: [GHS] Ensure GHS works with our custom plugins, to allow adding additional attributes.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3260853

Command icon 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

Wim Leers created an issue. See original summary.

wim leers’s picture

This will result in:

Testing /Users/wim.leers/Work/d8/core/modules/ckeditor5/tests/src/Kernel
.....F.....                                                       11 / 11 (100%)

Time: 40.76 seconds, Memory: 8.00 MB

There was 1 failure:

1) Drupal\Tests\ckeditor5\Kernel\SmartDefaultSettingsTest::test with data set "basic_html_with_any_data_attr can be switched to CKEditor 5 without problems …………………………" ('basic_html_with_any_data_attr', array(), array(array(array('bold', 'italic', '|', 'link', '|', 'bulletedList', 'numberedList', '|', 'blockQuote', 'uploadImage', '|', 'heading', '|', 'sourceEditing', '|', 'code', 'textPartLanguage')), array(array(array('<cite>', '<dl>', '<dt>', '<dd>', '<a hreflang>', '<blockquote cite>', '<ul type>', '<ol start type>', '<h2 id>', '<h3 id>', '<h4 id>', '<h5 id>', '<h6 id>', '<img data-*>')), array(array('heading2', 'heading3', 'heading4', 'heading5', 'heading6')), array('un'))), '<img data-*> <span lang dir>', array(), array('The following plugins were en...</em>.', 'The following tags were permi...d&gt;.', 'The following plugins were en...</em>.', 'This format's HTML filters in...d&gt;.'))
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'<img data-*> <span lang dir>'
+'<img data-__attribute_wildcard__ data-entity-uuid data-entity-type data-caption data-align> <span lang dir>'

and:

Testing /Users/wim.leers/Work/d8/core/modules/ckeditor5/tests/src/Unit
.................F                                                18 / 18 (100%)

Time: 3.3 seconds, Memory: 8.00 MB

There was 1 failure:

1) Drupal\Tests\ckeditor5\Unit\HTMLRestrictionsTest::testParse with data set "<drupal-media data-*>" ('<drupal-media data-*>', array(array(true)))
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
     'drupal-media' => Array &1 (
-        'data-*' => true
+        'data-__attribute_wildcard__' => true
     )
 )
wim leers’s picture

wim leers’s picture

Status: Postponed » Needs review
Issue tags: +GlobalContributionWeekend2022
StatusFileSize
new2.94 KB
new6.45 KB
new122.56 KB
wim leers’s picture

Assigned: Unassigned » wim leers
Issue tags: -Needs tests
StatusFileSize
new2.64 KB
new8.78 KB
new123.27 KB

#4 already added some parsing/representation tests, but this adds the crucial tests for the SmartDefaultSettings test to actually pass: a failing test for the various operations on HtmlRestrictions.

The last submitted patch, 4: 3260853-4_on_top_of_3228334-41.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 5: 3260853-5_on_top_of_3228334-41.patch, failed testing. View results

wim leers’s picture

Title: [PP-1] Partial wildcard attributes (<foo data-*>) not yet supported » Partial wildcard attributes (<foo data-*>) not yet supported

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Needs review
lauriii’s picture

wim leers’s picture

Yes, definitely!

Want to postpone this on that then?

lauriii’s picture

Title: Partial wildcard attributes (<foo data-*>) not yet supported » [PP-1] Partial wildcard attributes (<foo data-*>) not yet supported
Status: Needs review » Postponed
wim leers’s picture

Actually, that's a very good call, because that will ensure it actually works in CKEditor 5, because I think it currently does not … 🙈 And the fix that makes that super easy to add is also in #3259493: [GHS] Unable to limit attribute values: ::allowedElementsStringToHtmlSupportConfig() does not generate configuration that CKEditor 5 expects!

wim leers’s picture

Title: [PP-1] Partial wildcard attributes (<foo data-*>) not yet supported » Partial wildcard attributes (<foo data-*>) not yet supported
Assigned: Unassigned » wim leers
Status: Postponed » Needs work
Issue tags: +Needs tests
wim leers’s picture

Title: Partial wildcard attributes (<foo data-*>) not yet supported » [GHS] Partial wildcard attributes (<foo data-*>) and attribute values (<h2 id="jump-*">)not yet supported
Issue tags: -Needs tests

Functional JS tests now added. In doing that, I realized that the issue title was not complete enough yet.

wim leers’s picture

Title: [GHS] Partial wildcard attributes (<foo data-*>) and attribute values (<h2 id="jump-*">)not yet supported » [GHS] Partial wildcard attributes (<foo data-*>) and attribute values (<h2 id="jump-*">) not yet supported
Assigned: wim leers » Unassigned
Status: Needs work » Needs review

You can now go crazy with configurations like

<p data-* class="foo-*" foo="foo-*">

in your Source Editing configuration! 🤓🥳

I had to change \Drupal\ckeditor5\HTMLRestrictions::toGeneralHtmlSupportConfig() to use the key/value syntax documented at https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/api/module_eng..., which is not listed at https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/features/gener....

Note that we do have functional JS test coverage for this now — see

      // Edge case: `data-*` with wildcard attribute value.
      '<a data-*="sub*">' => [
        '<p>The <a href="https://example.com/pirate" data-grammar="subject">pirate</a> is <a href="https://example.com/irate">irate</a>.</p>',
        '<a data-*="sub*">',
      ],

→ that's a wildcard attribute name that has wildcard attribute values allowed! And we verify that CKEditor 5 in fact makes it possible to create this kind of content, only when it is configured to allow that!

bnjmnm made their first commit to this issue’s fork.

bnjmnm’s picture

Status: Needs review » Needs work

Pretty minor stuff in my review, but setting to NW for visiblity.

wim leers’s picture

Status: Needs work » Needs review

Great feedback, thanks @bnjmnm, for your very thorough reviews! 👏

bnjmnm’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new19.7 KB

All my feedback is addressed, and the HTMLRestrictions object grows more powerful.

wim leers’s picture

Thanks!

I queued a D10/PHP8.1 test. It only failed on D10 because it was using PHP 8.0, which D10 no longer supports as of last week 🥸

lauriii’s picture

Status: Reviewed & tested by the community » Needs review

I don't fully understand how is supporting data-* sufficient for the upgrade path. It seems like that's the only use case that has explicitly test coverage. However, it seems like other use cases are supported regardless of that. Also based on documentation for \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions, data-* isn't the only supported use case. 🤔

wim leers’s picture

#23: data-* is the common case, and probably >95% of all "wildcard attribute name" uses in Drupal 8. This issue focuses on that, to allow more sites to successfully switch to CKEditor 5 ASAP. But you're right that this either needs a follow-up or we need to expand this issue's scope.

Do you have a preference?

lauriii’s picture

Issue tags: +Needs followup

I'm fine with having a stable blocking follow-up issue. I was just confused because I got a sense from this issue, that this would be the only supported wildcard use case.

wim leers’s picture

Assigned: Unassigned » wim leers
Status: Needs review » Needs work
Issue tags: -Needs followup

Spent the past hour digging into #23#25.

Turns out that doing this is relatively straightforward, so let's get this done here and now! This is pushing WAY beyond what was ever possible in CKEditor 4 🤯🤩🚀

(It's really cool to see even these pretty complex wildcard attribute restrictions appear correctly in the FilterHtml configuration, and not just that, but know that it is correctly interpreted everywhere 😊)

wim leers’s picture

Title: [GHS] Partial wildcard attributes (<foo data-*>) and attribute values (<h2 id="jump-*">) not yet supported » [GHS] Partial wildcard attributes (<foo data-*>, <foo *-bar-*>, <foo *-bar>) and attribute values (<h2 id="jump-*">) not yet supported

Updating issue title to clarify that.

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Needs review
  1. 15f3fdd9b54618c34bd76ae18a0bfbe441c5bd21 added explicit test coverage — most notably it generalizes/parametrizes the data-* operations test coverage, so we can not only test suffix wildcards (i.e. data-*), but also prefix *-foo and infix foo-*-bar). The functional JS test coverage is also updated to test infix + prefix instead of only suffix.
    This should FAIL.
  2. 878727479c0eb29f4eca56133d63f427f576edf0 adds support for arbitrary suffix (so not just data-*, but ANYTHING*) and then expands it to also support prefix and infix wildcards.
  3. c534af6a67e7eb305df6e7e03035580bee8a8115 is a small addition that #3228334: Refactor HTMLRestrictionsUtilities to a HtmlRestrictions value object overlooked: new HTMLRestrictions(['foo' => ['*' => TRUE]]) does not make sense, one should use new HTMLRestrictions(['foo' => TRUE]) in that case. I realized this while working on the 2 preceding commits, and it allows me adding a clear assertion to ::isWildcardAttributeName().
bnjmnm’s picture

Status: Needs review » Needs work

Different wildcard locations didn't require that much to be changed, nice!

Found a few things that should be fairly easy to address, threads in MR.

wim leers’s picture

Status: Needs work » Needs review

Solid remarks — even though I tried to carefully reword/rephrase existing docs, I clearly didn't do a good enough job 😔 Thanks for paying attention to the important details :)

bnjmnm’s picture

StatusFileSize
new27.88 KB
bnjmnm’s picture

Status: Needs review » Reviewed & tested by the community

It looks like both my and @lauriii's feedback has been addressed.

wim leers’s picture

Thanks @bnjmnm!

Even though this is RTBC, there's some failures here. But … they're all actually to be ignored 😄

  1. The 74953263 failure is a random failure in AjaxBlockTest, 100% unrelated.
  2. The 10.0.x failure is because it was tested with PHP 8.0 but D10 only supports 8.1. (IDK why it's still possible to run tests there 🤷‍♀️) Queued a 8.1 test run.
lauriii’s picture

Status: Reviewed & tested by the community » Needs review

Great work here 👏

+++ b/core/modules/ckeditor5/src/HTMLRestrictions.php
@@ -813,20 +959,25 @@ public function toGeneralHtmlSupportConfig(): array {
+            ? ['regexp' => ['pattern' => '/^(' . implode('|', str_replace('*', '.*', $value)) . ')$/']]
...
+            'key' => strpos($name, '*') === FALSE ? $name : ['regexp' => ['pattern' => '/^' . str_replace('*', '.*', $name) . '$/']],

Should we use static::getRegExForWildCardAttributeName() here?

wim leers’s picture

Status: Needs review » Reviewed & tested by the community

Unrelated random AjaxBlockTest failures in 9.3 and 10 😬

#34: implemented — great catch! Note that we can only replace the second line you quoted, the first is subtly different because it handles attribute values, not attribute names.

wim leers’s picture

StatusFileSize
new27.88 KB

  • lauriii committed 359a11a on 10.0.x
    Issue #3260853 by Wim Leers, bnjmnm:  [GHS] Partial wildcard attributes...

  • lauriii committed 65c3e57 on 9.4.x
    Issue #3260853 by Wim Leers, bnjmnm:  [GHS] Partial wildcard attributes...
lauriii’s picture

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

Committed 359a11a and pushed to 10.0.x. Cherry-picked to 9.4.x. Thanks!

Leaving open for 9.3.x cherry-pick once the freeze is over.

  • bnjmnm committed f7e9db4 on 9.3.x authored by lauriii
    Issue #3260853 by Wim Leers, bnjmnm:  [GHS] Partial wildcard attributes...
bnjmnm’s picture

Status: Reviewed & tested by the community » Fixed

Cherry-picked to 9.3.x now that freeze is over.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.