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).

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
| Comment | File | Size | Author |
|---|---|---|---|
| #79 | drupal-media-class-attribute.mp4 | 2.62 MB | joseph.olstad |
| #69 | 2911527-69.patch | 3.62 KB | chris burge |
Comments
Comment #2
b_sharpe commentedComment #3
michaellenahan commentedComment #4
mttsmmrssprks commentedI'm working on this at DrupalCon.
Comment #5
mttsmmrssprks commentedComment #6
sutharsan commentedI mentored @mttsmmrssprks working on this issue at DC Vienna. Thanks Matt for working on this!
Comment #7
mttsmmrssprks commentedI need to leave DrupalCon now but will pick this up and test the patch from home this weekend.
Comment #8
bendev commentedtested the patch successfully against 8.5.x (see screenshot)
Comment #9
wim leersThanks 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().Comment #10
harsha012 commentedadded test
Comment #11
wim leers👍
Nit: Let's add a comment above this, to be consistent with the existing tests. Something like:
Comment #12
wim leersComment #13
harsha012 commentedAdded comment and uploaded the test-only patch
Comment #14
wim leersPerfect!
Comment #15
borisson_The test-only patch doesn't fail, so it looks like the testcoverage is not sufficient.
Comment #16
wim leers#15++ — I RTBC'd before the test results were available, I shouldn't have done that!
Wim--
Comment #17
harsha012 commentedimproved on the test coverage. Please find the latest patch
Comment #18
harsha012 commentedComment #19
wim leersThe test-only patch is still passing, which means the test coverage isn't strict enough yet.
Comment #20
harsha012 commentedComment #21
harsha012 commentedComment #22
b_sharpe commentedDon'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:
Or at very least checkout that generateStylesSetSetting() passes
Comment #23
harsha012 commentedmistakely upload the wrong patch
Comment #24
harsha012 commentedComment #25
harsha012 commentedhope this test fails
Comment #28
harsha012 commentedtest only patch uploaded in #25 and this is the final patch with test
Comment #29
harsha012 commentedComment #31
wim leersThis patch still only needs to have a test-only patch that fails, and a patch that passes. It's so close! :)
Comment #32
b_sharpe commentedFinally got a lil free time for this :)
Comment #33
b_sharpe commentedComment #35
andyposttest failed as expected
Comment #36
wim leersThanks, @b_sharpe!
The test-only patch is updating both
\Drupal\Tests\ckeditor\Kernel\CKEditorTestand\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!Comment #37
b_sharpe commentedOdd, this fails without the patch locally, then passes with... any advice?
Comment #38
ivan berezhnov commentedComment #41
tepelena commentedHave the same problem as well.
Comment #43
vacho commentedOnly patch re-roll to 8.8.x
Comment #44
Waldoswndrwrld commentedThis patch should fail.
This means that the patch in #43 works.
Comment #46
Waldoswndrwrld commentedPatch #2911527-43: Allow dashes in Styles dropdown's element names works
Comment #48
christinlepson commentedAlso 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.
Comment #49
christinlepson commentedComment #50
wim leersThanks, #44 indeed proves that #36 was addressed: both updated tests are failing without the fix!
Comment #51
rosinegrean commentedComment #53
yogeshmpawarSetting back to Needs Review because above test fails are unrelated.
Comment #54
othermachines commentedI 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):
The configuration saves successfully and the styles appear in the dropdown.
I think
test-class|test-classfails because it is missing an element. It passes withdiv.test-class|test-class. The second-test class|test class- fails because spaces aren't allowed.test.class|test classanddiv.test-class|test classboth pass.This seems to conform to the required syntax:
Drupal version 8.7.4
Comment #55
othermachines commentedOne 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.
Comment #57
chris burge commentedAttached patch modifies patch from #43 with the following modifications:
@othermachines - Classes with hyphens are already allowed. This issue seeks to allow elements with hyphens, such as
<drupal-entity>.Comment #58
chris burge commentedCorrecting title. Removing old tags.
Comment #60
nicholasthompsonIt 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 RightIt appears in the styles dropdown (see attached) when I select the Media Entity... however it does not seem to apply.
Comment #61
chris burge commented@nicholasThompson, the issue you're experiencing is #3117172: Allow CKEditor styles for <drupal-media>. I'm adding it as a related issue.
Comment #62
jibranI'm ignoring #57 here. Rerolled #48. What else is left here?
Comment #63
chris burge commented@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:
It's specifically this line:
Other than that, I think this patch is good for RTBC.
Comment #64
adityasingh commentedComment #65
manav commentedNew changes has been applied as suggested by @Chris Burge in #63
Comment #66
manav commentedComment #68
jibranSorry, I didn't realize #48 was reuploaded version on #43. I should have started from #57 :)
There is nothing reported in https://www.drupal.org/pift-ci-job/1751295. And now #65 has a lot of unwanted changes. :(
Comment #69
chris burge commentedIt 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.
Comment #70
manav commentedComment #71
manav commented@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
Comment #72
chris burge commented@Manav - Thanks for testing and reviewing
Comment #74
chris burge commented#73 was the result of an unrelated branch test failure. Tests passed on a subsequent run. Setting back to RTBC.
Comment #75
alexpottSaving issue credit.
Comment #76
alexpottCommitted 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.
Comment #79
joseph.olstadThis 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
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**