Problem/Motivation

Currently you can't add an element with dashes into the custom 'Styles' option for CKEditor, this is not a restriction of the editor itself and if using Entity Embed, you would have elements such as "drupal-entity".

I recreated this issue by going to the CK Editor config page in my local instance of v8.5.x-dev:
.../admin/config/content/formats

I edited the Full HTML editor configuration:
.../admin/config/content/formats/manage/full_html?destination=/admin/config/content/formats

I added the styles button to the toolbar configuration (arrow 1 in screencap), then added a new class to the styles text area (arrow 2 in screencap).

screencap of CK editor

I attempted to add a series of options: 'test-class|test-class', 'test class|test class', 'testclass|testclass', but the first two caused errors. Only the final was accepted (arrow 3 in screencap above).

Proposed resolution

Allow dashes.

Remaining tasks

User interface changes

Dashes are allowed.

API changes

None

Data model changes

None

Release notes snippet

Comments

b_sharpe created an issue. See original summary.

b_sharpe’s picture

Status: Active » Needs review
StatusFileSize
new688 bytes
michaellenahan’s picture

Issue tags: +Novice, +Vienna2017
mttsmmrssprks’s picture

I'm working on this at DrupalCon.

mttsmmrssprks’s picture

Issue summary: View changes
StatusFileSize
new109.06 KB
sutharsan’s picture

I mentored @mttsmmrssprks working on this issue at DC Vienna. Thanks Matt for working on this!

mttsmmrssprks’s picture

I need to leave DrupalCon now but will pick this up and test the patch from home this weekend.

bendev’s picture

Version: 8.3.x-dev » 8.5.x-dev
StatusFileSize
new92.15 KB

tested the patch successfully against 8.5.x (see screenshot)

wim leers’s picture

Title: Allow Dashes in custom Styles » Allow dashes in Styles dropdown's class names
Category: Bug report » Feature request
Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks for creating this issue. Makes sense! 👍

Before this can be committed, this needs test coverage. You can expand the existing test coverage at \Drupal\Tests\ckeditor\Kernel\CKEditorTest::testStylesComboGetConfig().

harsha012’s picture

Status: Needs work » Needs review
StatusFileSize
new1.7 KB

added test

wim leers’s picture

Issue tags: -Needs tests

👍

  1. +++ b/core/modules/ckeditor/tests/modules/src/Kernel/CKEditorTest.php
    @@ -346,6 +346,11 @@ public function testStylesComboGetConfig() {
    +    $settings['plugins']['stylescombo']['styles'] = "  p .test-class   |  classwithDash ";
    

    Nit: Let's add a comment above this, to be consistent with the existing tests. Something like:

    // Configuration that includes a dash in the class name.
    
  2. Can you also upload a test-only patch? The test-only patch should fail. That will then prove that there is test coverage proving that it didn't work before, and does work after.
wim leers’s picture

Status: Needs review » Needs work
harsha012’s picture

Status: Needs work » Needs review
StatusFileSize
new1.09 KB
new697 bytes
new1.76 KB

Added comment and uploaded the test-only patch

wim leers’s picture

Status: Needs review » Reviewed & tested by the community

Perfect!

borisson_’s picture

Status: Reviewed & tested by the community » Needs work

The test-only patch doesn't fail, so it looks like the testcoverage is not sufficient.

wim leers’s picture

#15++ — I RTBC'd before the test results were available, I shouldn't have done that!

Wim--

harsha012’s picture

StatusFileSize
new1.86 KB
new1.86 KB
new1.19 KB

improved on the test coverage. Please find the latest patch

harsha012’s picture

Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Needs work

The test-only patch is still passing, which means the test coverage isn't strict enough yet.

harsha012’s picture

Status: Needs work » Needs review
StatusFileSize
new1.86 KB
new1.2 KB
harsha012’s picture

b_sharpe’s picture

Don't have time to patch right now, but for what it's worth the only place it would 'fail' is in the element validate so the test would need to hit this:

    $styles_setting = $this->generateStylesSetSetting($element['#value']);
    if ($styles_setting === FALSE) {
      $form_state->setError($element, $this->t('The provided list of styles is syntactically incorrect.'));
    }

Or at very least checkout that generateStylesSetSetting() passes

harsha012’s picture

StatusFileSize
new2.18 KB

mistakely upload the wrong patch

harsha012’s picture

harsha012’s picture

StatusFileSize
new2.45 KB

hope this test fails

The last submitted patch, 23: test-only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 25: test-only-2911527-25.patch, failed testing. View results

harsha012’s picture

StatusFileSize
new3.12 KB

test only patch uploaded in #25 and this is the final patch with test

harsha012’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 28: 2911527-26.patch, failed testing. View results

wim leers’s picture

Issue tags: +php-novice

This patch still only needs to have a test-only patch that fails, and a patch that passes. It's so close! :)

b_sharpe’s picture

StatusFileSize
new2.86 KB
new3.53 KB

Finally got a lil free time for this :)

b_sharpe’s picture

Status: Needs work » Needs review

The last submitted patch, 32: test-only-2911527-32.patch, failed testing. View results

andypost’s picture

Status: Needs review » Reviewed & tested by the community

test failed as expected

wim leers’s picture

Status: Reviewed & tested by the community » Needs work

Thanks, @b_sharpe!

The test-only patch is updating both \Drupal\Tests\ckeditor\Kernel\CKEditorTest and \Drupal\Tests\ckeditor\Functional\CKEditorStylesComboAdminTest. But only the latter is failing. I thoroughly reviewed the latter, the changes to that one are correct.

So, back to NW for CKEditorTest, sorry!

b_sharpe’s picture

Odd, this fails without the patch locally, then passes with... any advice?

○ → ../vendor/bin/phpunit ./modules/ckeditor/tests/modules/src/Kernel/CKEditorTest.php 
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.

Testing Drupal\Tests\ckeditor\Kernel\CKEditorTest
....F..

Time: 4.74 seconds, Memory: 6.00MB

There was 1 failure:

1) Drupal\Tests\ckeditor\Kernel\CKEditorTest::testStylesComboGetConfig
"StylesCombo" plugin configuration built correctly for customized toolbar.
Failed asserting that Array &0 (
    'stylesSet' => Array &1 (
        0 => Array &2 (
            'name' => 'Allowing Dashes'
            'element' => 'drupal-entity'
            'attributes' => Array &3 (
                'class' => 'has-dashes'
            )
        )
    )
) is identical to Array &0 (
    'stylesSet' => false
).

/home/bryan/www/d8/wwwroot/core/tests/Drupal/KernelTests/AssertLegacyTrait.php:77
/home/bryan/www/d8/wwwroot/core/modules/ckeditor/tests/modules/src/Kernel/CKEditorTest.php:368

FAILURES!
Tests: 7, Assertions: 36, Failures: 1.

ivan berezhnov’s picture

Issue tags: +CSKyiv18

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.

tepelena’s picture

Have the same problem as well.

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.

vacho’s picture

StatusFileSize
new3.5 KB

Only patch re-roll to 8.8.x

Waldoswndrwrld’s picture

Status: Needs work » Needs review
Issue tags: +DevDaysCluj
StatusFileSize
new2.83 KB

This patch should fail.
This means that the patch in #43 works.

Status: Needs review » Needs work

The last submitted patch, 44: 2911527_44_test_only.patch, failed testing. View results

Waldoswndrwrld’s picture

Status: Needs work » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2911527_44_test_only.patch, failed testing. View results

christinlepson’s picture

StatusFileSize
new3.5 KB

Also tested the patch in #43. Looks good and @vacho solved the issue.

@Waldoswndrwrld It looks like you used underscores instead of hyphens in your patch's name. "911527_44_test_only.patch" instead of "test-only-911527-44.patch". I believe that was triggering the automatic setback to Needs Work.

I'm just re-uploading @vacho's patch in #43 and setting to RTBC.

christinlepson’s picture

Status: Needs work » Reviewed & tested by the community
wim leers’s picture

Thanks, #44 indeed proves that #36 was addressed: both updated tests are failing without the fix!

rosinegrean’s picture

Issue tags: -DevDaysCluj +DevDaysTransylvania

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 48: 2911527-43.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Needs review

Setting back to Needs Review because above test fails are unrelated.

othermachines’s picture

I could be wrong, but I don't think there is an issue here. My classes have hyphens and it is passing validation.

My configuration under "Styles dropdown" (Full HTML):

div.flex-video|Flex Video
div.content-alert-box|Box
ul.inline-list|Inline list
div.small-block-grid-1.medium-block-grid-2|Block grid 2
div.small-block-grid-2.medium-block-grid-3|Block grid 3
div.small-block-grid-2.medium-block-grid-4|Block grid 4

The configuration saves successfully and the styles appear in the dropdown.

I think test-class|test-class fails because it is missing an element. It passes with div.test-class|test-class. The second- test class|test class- fails because spaces aren't allowed. test.class|test class and div.test-class|test class both pass.

This seems to conform to the required syntax:

Enter one or more classes on each line in the format: element.classA.classB|Label. Example: h1.title|Title. Advanced example: h1.fancy.title|Fancy title.

Drupal version 8.7.4

othermachines’s picture

One more thing: 'testclass|testclass' passes validation but 'testclass' is assumed to be an element, not a class. The resulting HTML with this configuration is:

<p><testclass>Suspendisse potenti</testclass></p>

So I think this is all functionally correct but perhaps the description and (certainly) the validation messages could be clearer.

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.

chris burge’s picture

Issue tags: -
StatusFileSize
new4.78 KB
new2.85 KB

Attached patch modifies patch from #43 with the following modifications:

  • Escapes hypen in regex
  • Updates regex in ckeditor.stylescombo.admin.es6.js and ckeditor.stylescombo.admin.js
  • Addresses a code standard issue in CKEditorTest.php

@othermachines - Classes with hyphens are already allowed. This issue seeks to allow elements with hyphens, such as <drupal-entity>.

chris burge’s picture

Title: Allow dashes in Styles dropdown's class names » Allow dashes in Styles dropdown's element names
Issue tags: -Vienna2017, -CSKyiv18, -DevDaysTransylvania

Correcting title. Removing old tags.

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.

nicholasthompson’s picture

StatusFileSize
new34.98 KB

It looks like this seems to partially solve the problem?

It fixes the issue where I cannot submit the form where styles have hyphens in them, however they dont seem to apply to media entities... Not sure if this is an issue with the hyphens patch or that I'm trying to use it with media entities?

I'm using this style.
drupal-media.float-right|Float Right

It appears in the styles dropdown (see attached) when I select the Media Entity... however it does not seem to apply.

Screenshot

chris burge’s picture

@nicholasThompson, the issue you're experiencing is #3117172: Allow CKEditor styles for <drupal-media>. I'm adding it as a related issue.

jibran’s picture

StatusFileSize
new3.5 KB

I'm ignoring #57 here. Rerolled #48. What else is left here?

chris burge’s picture

Status: Needs review » Needs work

I'm ignoring #57 here.

@jibran - An explanation would be helpful.

I've reviewed #62 and #57. Even though hyphens are special characters, they don't need escaped in the given syntax, so those changes from #57 can be omitted.

The coding standards issue remains:

--- a/core/modules/ckeditor/tests/src/Kernel/CKEditorTest.php
+++ b/core/modules/ckeditor/tests/src/Kernel/CKEditorTest.php
@@ -369,6 +369,14 @@ public function testStylesComboGetConfig() {
     $editor->save();
     $expected['stylesSet'] = FALSE;
     $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');
+
+    // Configuration that includes a dash in either the element or class name.
+    $settings['plugins']['stylescombo']['styles'] = "drupal-entity.has-dashes|Allowing Dashes";
+    $editor->setSettings($settings);
+    $editor->save();
+    $expected['stylesSet'] = [['name' => 'Allowing Dashes', 'element' => 'drupal-entity', 'attributes' => ['class' => 'has-dashes']]];
+    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');
+
   }
FILE: ...-styles-hyphen/web/core/modules/ckeditor/tests/src/Kernel/CKEditorTest.php

...
 377 | ERROR   | [ ] If the line declaring an array spans longer than 80
     |         |     characters, each element should be broken into its own
     |         |     line
...

It's specifically this line:

 $expected['stylesSet'] = [['name' => 'Allowing Dashes', 'element' => 'drupal-entity', 'attributes' => ['class' => 'has-dashes']]];

Other than that, I think this patch is good for RTBC.

adityasingh’s picture

Assigned: Unassigned » adityasingh
manav’s picture

Assigned: adityasingh » manav
StatusFileSize
new9.73 KB
new6.7 KB

New changes has been applied as suggested by @Chris Burge in #63

manav’s picture

Assigned: manav » Unassigned
Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 65: 2911527-65.patch, failed testing. View results

jibran’s picture

@jibran - An explanation would be helpful.

Sorry, I didn't realize #48 was reuploaded version on #43. I should have started from #57 :)

The coding standards issue remains:

There is nothing reported in https://www.drupal.org/pift-ci-job/1751295. And now #65 has a lot of unwanted changes. :(

chris burge’s picture

Status: Needs work » Needs review
StatusFileSize
new3.62 KB
new999 bytes

It looks like a plethora of coding standards were fixed in #65, which, while desirable, is out of scope for this issue.

Attached is an updated patch that builds on #62. Only coding standards within the patch itself are addressed.

(Re coding standards, I'm using Coder, which provides Drupal coding standards for PHP_CodeSniffer.

manav’s picture

Assigned: Unassigned » manav
manav’s picture

Assigned: manav » Unassigned
Status: Needs review » Reviewed & tested by the community

@Chris: I have applied patch #69 and its working as expected.
I am able to add more style in editor settings and can use it in the content editor without any error.

Drupal ver: 9.1.x
Maradb ver: 10.3.x
Php ver: 7.3

RTBC

chris burge’s picture

@Manav - Thanks for testing and reviewing

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 69: 2911527-69.patch, failed testing. View results

chris burge’s picture

Status: Needs work » Reviewed & tested by the community

#73 was the result of an unrelated branch test failure. Tests passed on a subsequent run. Setting back to RTBC.

alexpott’s picture

Saving issue credit.

alexpott’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

Committed 0c9c57f and pushed to 9.1.x. Thanks!

I don't think we need a CR here - I think people would expect this to work.

  • alexpott committed 0c9c57f on 9.1.x
    Issue #2911527 by harsha012, b_sharpe, Chris Burge, Manav, jibran,...

Status: Fixed » Closed (fixed)

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

joseph.olstad’s picture

StatusFileSize
new2.62 MB

This patch provides a class and element but doesn't work for selected drupal-media or drupal-entity elements in my case. Perhaps I need to test with a vanilla 9.1 fresh install

see screencast:


Is there another patch somewhere to make this work for drupal-media elements or drupal-entity elements selected as per screencast?

Note: I also have tried with a drupal-entity element
no dice except to make an empty element, selecting a real one and applying the style does not do anything

 <drupal-entity class="seven-hundred">
  </drupal-entity>

  <drupal-entity alt="loft window" data-embed-button="media_browser" data-entity-embed-display="media_image" data-entity-embed-display-settings="{&quot;image_style&quot;:&quot;&quot;,&quot;image_link&quot;:&quot;&quot;}" data-entity-type="media" data-entity-uuid="83949e37-eb0b-455a-a9d0-3b76a9d2f19a" data-langcode="en" title="house construction 2009">
  </drupal-entity>

I wanted to get class="seven-hundred" on the actual embedded drupal-entity element but it take it.

similar result with drupal-media , class is not applied to the selected item

**EDIT**

I have found something promising perhaps:

https://www.drupal.org/forum/support/post-installation/2018-08-31/how-to...

however I haven't tested it fully yet.
**EDIT**