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

Add option to enable the style element rendering #12838

Closed
Mgsy opened this issue Nov 14, 2022 · 3 comments
Closed

Add option to enable the style element rendering #12838

Mgsy opened this issue Nov 14, 2022 · 3 comments
Assignees
Labels
package:engine squad:core Issue to be handled by the Core team. support:2 An issue reported by a commercially licensed client. type:feature This issue reports a feature request (an idea for a new functionality or a missing option).

Comments

@Mgsy
Copy link
Member

Mgsy commented Nov 14, 2022

📝 Provide a description of the new feature

Currently, we don't render the <style> element, but preserve it in the data. Sometimes, the loaded content (for example from CKEditor 4) might include some styles that should be preserved. 

This feature request is for introducing an option to add support for enabling <style> rendering on demand.


If you'd like to see this feature implemented, add a 👍 reaction to this post.

@Mgsy Mgsy added type:feature This issue reports a feature request (an idea for a new functionality or a missing option). package:engine support:2 An issue reported by a commercially licensed client. squad:core Issue to be handled by the Core team. labels Nov 14, 2022
@Mgsy Mgsy changed the title Add option to enable the <style> element rendering Add option to enable unsafe elements rendering Nov 16, 2022
@Mgsy Mgsy changed the title Add option to enable unsafe elements rendering Add option to enable the style element rendering Nov 16, 2022
@Reinmar
Copy link
Member

Reinmar commented Nov 24, 2022

The problem with rendering <style> elements is that their content will affect the entire page. Not only the contents of the editor.

One hack around this would be to parse the content of these <style> elements and prefix every selector with a unique class name or id name. And use the same id/class on the editable element of the editor. The problem with this is that it'd affect the specificity of those rules and might be hard to figure out what's happening. That's why I'd consider using a class instead of id because it has a smaller effect. Though, it'd still lead to some issues.

@Reinmar
Copy link
Member

Reinmar commented Mar 6, 2023

One hack around this would be to parse the content of these <style> elements and prefix every selector with a unique class name or id name.

We discussed this F2F and automat style prefixing isn't trivial. There are many scenarios like:

  • selectors utilizing media queries
  • selectors that use specifiers that are outside the editor (e.g. body p, prefixing it with #ck-sth body p would not work)
  • finally, specificity will be affected so such selectors may start working differently than in the editor output

Thus, we still believe that we can only achieve here the very basic thing which is rendering the content of the <style> element, without changing it in any way (so, with the risk that it will leak from the editor).

@Reinmar Reinmar self-assigned this Mar 6, 2023
@CKEditorBot CKEditorBot added the status:in-progress Set automatically when an issue lands in the "In progress" column. We are working on it. label Mar 6, 2023
@Reinmar
Copy link
Member

Reinmar commented Mar 17, 2023

I created a PoC showing a simple plugin that enables rendering the <style> elements within the editor: https://github.com/ckeditor/ckeditor5/compare/t/12838-render-style

The plugin is really simple:

function RenderStyleElements( editor ) {
	// Remove the 'style' name from the list of unsafe elements.
	const unsafeElements = editor.editing.view.domConverter.unsafeElements;
	const indexOfStyle = unsafeElements.indexOf( 'style' );

	if ( indexOfStyle > -1 ) {
		unsafeElements.splice( indexOfStyle, 1 );
	}
}

It has to be combined with GHS (as in the linked example) or a custom converter for <style> elements (but I recommend using GHS in this case as it's the simplest).

NOTE: We don't plan to release such a plugin officially because it's fairly simple to customize the editor with a custom plugin. Also, we don't want to promote rendering <style> elements in our documentation so no documentation work follows.

@Reinmar Reinmar closed this as completed Mar 17, 2023
@CKEditorBot CKEditorBot removed the status:in-progress Set automatically when an issue lands in the "In progress" column. We are working on it. label Mar 17, 2023
@Reinmar Reinmar added this to the iteration 61 milestone Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:engine squad:core Issue to be handled by the Core team. support:2 An issue reported by a commercially licensed client. type:feature This issue reports a feature request (an idea for a new functionality or a missing option).
Projects
None yet
Development

No branches or pull requests

3 participants