Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should br break inline elements? #1068

Open
pomek opened this issue Jun 19, 2018 · 21 comments
Open

Should br break inline elements? #1068

pomek opened this issue Jun 19, 2018 · 21 comments
Labels
domain:v4-compatibility This issue reports a CKEditor 4 feature/option that's missing in CKEditor 5. squad:core Issue to be handled by the Core team. status:discussion support:2 An issue reported by a commercially licensed client. type:bug This issue reports a buggy (incorrect) behavior.

Comments

@pomek
Copy link
Member

pomek commented Jun 19, 2018

At this moment if you press Shift + Enter inside an inline element, <br> element won't be inserted as a chidren of the inline element. The <br> will break the inline element:

image

I'm not sure how it should behave. Is it correct? What if we would like to have the same link in both lines? Editing inline styles (like bold, highlights, font size, etc.) is not a problem. The issue appears if we would like to edit links that are split by the <br> element. But there is the separate ticket for it – https://github.com/ckeditor/ckeditor5-link/issues/192.


EDIT: See the workaround in this post.

@Reinmar Reinmar added the type:bug This issue reports a buggy (incorrect) behavior. label Jun 19, 2018
@Reinmar
Copy link
Member

Reinmar commented Jun 19, 2018

From technical perspective it's a question whether <br> should inherit attributes from the $text. But what we need to discuss are the user experience and HTML-oriented perspectives.

@Reinmar
Copy link
Member

Reinmar commented Jun 19, 2018

cc @Comandeer

@Comandeer
Copy link
Member

HTML specification allows using line break inside inline elements (or – to be more precise – inside phrasing content; block and inline elements were defined in HTML 4, HTML LS/5.x defines several other elements categories).

OTOH link is no longer only phrasing content, it's also flow one now, as it can contain flow content like div or sections.

@Reinmar
Copy link
Member

Reinmar commented Mar 5, 2019

Whether HTML specification allows line breaks inside phrasing content wasn't (I think) the question here. The question is – do we need that? From a semantic standpoint I can see cases where the content should contain e.g. <a>first address line<br>second address line</a>. Are there cases where merging e.g. subsequent <strong> elements into one that'd contain a <br> is not what the user would expect? If we don't see such scenarios, then it means that we can safely enable inline elements on soft breaks and all should be fine.

@DutchDave
Copy link

I noticed this behavior as well. And for the reason when removing the selection it would be nice to see the link disappear at once.

@Reinmar Reinmar added this to the backlog milestone Oct 29, 2019
@mguidetti
Copy link

Is there any current work-around for this or progress being made?
I have an implementation that needs line breaks within a single link as @Reinmar described

From my example (an event listing), ideally the output would be:

<a>
Event Title<br> 
Event Date
</a>

But currently if you highlight multiple lines and create a link you get:

<a>Event Title</a><br>
<a>Event Date</a>

The main issue in my case is visual, as the hover effect is applied individually to each line when applying it to the whole block would make more sense.

@pomek pomek removed this from the backlog milestone Feb 21, 2022
@jswiderski jswiderski added the domain:v4-compatibility This issue reports a CKEditor 4 feature/option that's missing in CKEditor 5. label Jun 23, 2022
@martynawierzbicka martynawierzbicka added the support:2 An issue reported by a commercially licensed client. label Jun 23, 2022
@VesterDe
Copy link

We would really benefit from having this fixed, it's affecting our product development.

@Reinmar Reinmar added the squad:core Issue to be handled by the Core team. label Jun 27, 2022
@Reinmar
Copy link
Member

Reinmar commented Jul 13, 2022

There are 3 part to this ticket.

1. Extending softBreaks schema

Adding allowAttributesOf: '$text', to softBreak's definition. This ensures data retention.

2. Fixing ShiftEnterCommand

Adding to the ShiftEnterCommand this bit:

 function insertBreak( model, writer, position ) {
-	const breakLineElement = writer.createElement( 'softBreak' );
+	const breakLineElement = writer.createElement( 'softBreak', model.document.selection.getAttributes() );

This will ensure that <br> inserted by the command is inserted inside a link (does not break it)\

TODO: Check whether insertContent() clears up attributes that might not be allowed on this <softBreak>. Ideally, the <softBreak> element inserted to the model should only inherit those attributes that are really enabled on it in the schema. It's realistic that the set of attrs allowed on <softBreak> will differ from the set of attrs allowed on a text.

In case, insertContent() does not clear disallowed attributes in this specific use scenario, use the Schema#setAllowedAttributes( softBreak, model.doc.selection.getAttributes(), writer ) to set only the allowed ones.

3. Fixing the selection attributes' discovery

Fixing the selection attributes discovery code that it reads attributes also from inline elements. Currently, it does not:

(Note: There are no selection attributes listed in the right pane)

Scope of this ticket

Let's cover the points 1 and 2 in this ticket.

The 3rd part (selection attrs discovery) is tricky and hence, worth moving to a separate issue. The result of not fixing this will not be very visible anyway.

@Reinmar
Copy link
Member

Reinmar commented Jul 13, 2022

Other considerations:

  • It should not have any effect on TC or RH. No action needed.
  • It should theoretically have no effect on 2SCM. No action needed.
  • There's a bug when there's <paragraph><$text linkHref=x>foo</$text><softBreak linkHref=x /></p> because it's not possible to place the selection after the soft break. That's due to lack of getFillerOffset() on AttributeElement. Action needed.
  • We should probably have post-fixer cleaning up text attributes from lone <softBreak>s (ones not surrounded by any content). Action needed.
  • The way the schema is defined will automatically enable this for link decorators. No action needed.
  • Any problems with GHS? Most likely not. GHS doesn't understand soft breaks. No action needed.
  • Won't enabling text styles on <br>s influence the quality of content filtering on paste? E.g. CKEditor 4 strips all text formatting if it wraps only a <br>. E.g. setData( '<p>x<strong><br></strong>x</p>' )  will load only <p>x<br>x</p>). Action needed.

@Reinmar
Copy link
Member

Reinmar commented Jul 13, 2022

Taken the other findings, it's hard to figure out what's the MVP here. Some of the "other findings" would also need to be included in the MVP. Thus, the complexity grows.

@Reinmar
Copy link
Member

Reinmar commented Jul 13, 2022

Since this is not a quick win and I don't know the ETA for a complete resolution of this issue (in a way that we may accept in the core) I'd like to propose a simple workaround for all of you dealing with data migration from e.g. CKEditor 4.

The solution is really simple – you just need to load an additional plugin to the editor.

The plugin looks like this:

class LinkableBRs {
    constructor( editor ) {
        this.editor = editor;
    }

    init() {
        this.editor.model.schema.extend( 'softBreak', {
            allowAttributesOf: '$text'
        } );
    }
}

It has no external dependencies so it can be added even to a build via config.extraPlugins.

It makes the editor keep <br>s inside <a> elements:

@rgpublic
Copy link

The LinkableBRs plugin provided by @Reinmar is useful if you edit the source code, but it doesn't fix the Shift+Enter issue. It's probably more difficult to modify the ShiftEnterCommand's insertBreak method by a plugin as described, right?

@Witoso
Copy link
Member

Witoso commented Sep 25, 2023

@rgpublic, I think you would need to listen for ShiftEnterCommand's execute event, stop it, and replace the implementation 🤔.

@jonmenard
Copy link

I am using Drupal 9/10 and CKeditor5 is built into core, does anyone know how to add the proposed class LinkableBRs {} in drupal?

Current I am trying to use hook_editor_js_settings_alter(array &$settings){}

@arfanseptianto
Copy link

I am using Drupal 9/10 and CKeditor5 is built into core, does anyone know how to add the proposed class LinkableBRs {} in drupal?

Current I am trying to use hook_editor_js_settings_alter(array &$settings){}

Have you found the solution for this? I also got this issue.. Drupal 10.2.2

@jonmenard
Copy link

I am using Drupal 9/10 and CKeditor5 is built into core, does anyone know how to add the proposed class LinkableBRs {} in drupal?
Current I am trying to use hook_editor_js_settings_alter(array &$settings){}

Have you found the solution for this? I also got this issue.. Drupal 10.2.2

Was unable to find a solution yet

@Witoso
Copy link
Member

Witoso commented Jan 25, 2024

@jonmenard @arfanseptianto could you reach Drupal community on Slack? I'm not an expert in their ecosystem, I assume you could just load somehow the plugin into the editor's plugins list.

@danielkorte
Copy link

@jonmenard and @arfanseptianto Try this: https://github.com/danielkorte/custom_linkablebrs

@jonmenard
Copy link

@jonmenard and @arfanseptianto Try this: https://github.com/danielkorte/custom_linkablebrs

The solution provided by @danielkorte worked perfectly!

Here's an overview of my project structure to give you a clearer picture. The cdlsimod represents our custom module, and custom_linkablebrs is the cloned environment with the provided repository's contents.

Screenshot 2024-01-25 at 12 33 32 PM

To integrate the solution, I executed the following commands:

drush en custom_linkablebrs
drush cr

Alternatively, if you prefer a GUI approach, you can enable the module and clear the caches directly through the website's administrative interface.

@arfanseptianto
Copy link

@jonmenard and @arfanseptianto Try this: https://github.com/danielkorte/custom_linkablebrs

Yes, works like a charm. Thank you @danielkorte

@DarkSifal

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:v4-compatibility This issue reports a CKEditor 4 feature/option that's missing in CKEditor 5. squad:core Issue to be handled by the Core team. status:discussion support:2 An issue reported by a commercially licensed client. type:bug This issue reports a buggy (incorrect) behavior.
Projects
None yet
Development

No branches or pull requests