Skip to content

Add ability to indicate the language of styles#5474

Merged
Comandeer merged 8 commits intomasterfrom
t/5410
May 30, 2023
Merged

Add ability to indicate the language of styles#5474
Comandeer merged 8 commits intomasterfrom
t/5410

Conversation

@KarolDawidziuk
Copy link
Copy Markdown
Contributor

What is the purpose of this pull request?

New feature

Does your PR contain necessary tests?

All patches that change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.

This PR contains

  • Unit tests
  • Manual tests

Did you follow the CKEditor 4 code style guide?

Your code should follow the guidelines from the CKEditor 4 code style guide which helps keep the entire codebase consistent.

  • PR is consistent with the code style guide

What is the proposed changelog entry for this pull request?

* [#5410](https://github.com/ckeditor/ckeditor4/issues/5410): Added the ability to indicate the language of styles via [config.styleSet config variable](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_config.html#cfg-stylesSet).

What changes did you make?

PR adds the ability to set the language of the Styles dropdown elements. When the language is not explicitly set, the same language as the editor will be added.

Which issues does your PR resolve?

Closes #5410.

@Comandeer Comandeer self-assigned this May 8, 2023
Copy link
Copy Markdown
Member

@Comandeer Comandeer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall logic looks good, there is only a small issue with outputting the [lang] attribute – see my inline comments for more details.

} );
},

'test adds the default editor lang code to the Styles combo box item if it is not defined': function() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails in my browser:

Values should be the same.
Expected: en (string)
Actual:   en-gb (string)

It would probably be better to set the language to one with a low probability of usage in our team and one different than config.defaultLanguage – that's how we will have the certainty that the fallback really works.

bender.editorBot.create( {
name: 'cb_2',
config: {
lang: 'en',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lang: 'en',
language: 'en',

resultElement.setHtml( '' );

CKEDITOR.tools.array.forEach( panelItems, function( element, index ) {
resultElement.appendHtml( 'Item ' + index + ': ' + JSON.stringify( element.getAttributes() ) + '</br></br>' );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dump of all attributes in the JSON format is quite unreadable. Maybe just display the value of the [lang] attribute?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Changed.

' draggable="false"' +
' ondragstart="return false;"' + // Draggable attribute is buggy on Firefox.
' href="javascript:void(\'{val}\')" ' +
' lang="{language}"' +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the language directly in the listblock template results in outputting this attribute in each place that uses the listblock – so if the language is not set, the [lang="{language}"] will be generated (you can see it e.g. in Font dropdown).

Comment on lines +108 to +110
if ( language ) {
data.language = language;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the logic that fallbacks to the editor's language should be moved here to always output the correct [lang] attribute in various toolbar buttons.

Copy link
Copy Markdown
Contributor Author

@KarolDawidziuk KarolDawidziuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Comandeer for the review!

I've applied the changes mentioned in your inline comment and PR is ready for another review.

resultElement.setHtml( '' );

CKEDITOR.tools.array.forEach( panelItems, function( element, index ) {
resultElement.appendHtml( 'Item ' + index + ': ' + JSON.stringify( element.getAttributes() ) + '</br></br>' );
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Changed.

@KarolDawidziuk KarolDawidziuk requested a review from Comandeer May 15, 2023 10:13
Copy link
Copy Markdown
Member

@Comandeer Comandeer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KarolDawidziuk, thx for your changes! However, I'm wondering if we couldn't simplify our approach (see my inline comments for more details).

proto: {
add: function( value, html, title ) {
var id = CKEDITOR.tools.getNextId();
add: function( value, html, title, language, editor ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Five parameters are probably too many.

I'm wondering if we could replace the added ones with the options object pattern:

Suggested change
add: function( value, html, title, language, editor ) {
add: function( value, html, title, options ) {
options = CKEDITOR.object.merge( someDefaultOptions, options );

The second idea I had: maybe we could pass the editor to the list block constructor and save it as this._.editor? However, I'm not sure if a list block instance can't be reused in another editor 🤔

I see even two more solutions, probably even easier ones:

  • pass only possible language fallbacks (so basically editor.langCode) instead of the whole editor,
  • add the [lang] attribute only if it's known; it's absence should make the screen reader use the nearest available value – so the editor's container [lang] attribute.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ideas 👍
Let's go with applying the lang attribute when it's defined.

Comment on lines +89 to +91
editorLanguage = editor.config.language ||
editor.config.defaultLanguage ||
editor.langCode;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need so many options? Couldn't we use just editor.langCode? It should contain the actual language of the editor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed as we changed our approach.

title: CKEDITOR.tools.htmlEncodeAttr( title || value ),
text: html || value
text: html || value,
language: language || editorLanguage
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with the "add the [lang] attribute only if it's known" approach, then we could add the whole markup here (similar to what we do in the button plugin).

Comment on lines +11 to +12
* Item 0 should have the lang attribute set to `pl`.
* Item 1 should have the `lang` attribute set to the editor language value.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Item 0 should have the lang attribute set to `pl`.
* Item 1 should have the `lang` attribute set to the editor language value.
* Item 1 should have the lang attribute set to `pl`.
* Item 2 should have the `lang` attribute set to the editor language value.

Additionally, in my case the editor is en-gb but the second item is en 🤔 I don't know why as the code suggests they should be the same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I added lang instead of language in the editor config.

@KarolDawidziuk
Copy link
Copy Markdown
Contributor Author

Thanks @Comandeer for the review! It's ready for another one.

@KarolDawidziuk KarolDawidziuk requested a review from Comandeer May 26, 2023 11:20
Copy link
Copy Markdown
Member

@Comandeer Comandeer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I've just updated the manual test description as it became outdated.

@Comandeer Comandeer merged commit 4ec334e into master May 30, 2023
@Comandeer Comandeer deleted the t/5410 branch May 30, 2023 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to indicate the language of styles

2 participants