ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Support for iframes

Open hpivanov opened this issue 5 years ago • 17 comments

📝 Provide detailed reproduction steps (if any)

Paste the following code in ckeditor:

✔️ Expected result

To render the iframe.

❌ Actual result

Iframe disappears.

📃 Other details

I know that there is a plugin called Media, but the point is that I want to allow the iframe tag not only for twitter and youtube.

  • Browser: All
  • OS: All
  • CKEditor version: Latest
  • Installed CKEditor plugins: Does't matter

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

hpivanov avatar Jun 09 '20 11:06 hpivanov

I'm also interested in how can I make the iframe allowed element. Some of our clients have already put iframes in the editor (for integration with other components) and after the migration to v5 they are no longer able to use them.

DenitsaGencheva avatar Jun 11 '20 07:06 DenitsaGencheva

Hi, thanks for the report. Currently, CKEditor 5 doesn't support iframes, so I'll confirm this issue as a feature request. 

Also, you can try to implement it on your own, defining a proper schema and conversion.

Mgsy avatar Jun 12 '20 07:06 Mgsy

Could you show an example of allowing specific element, because for sure there are a lot of people that would like to understand it more clearly with simple example. I didn't find any example in the documentation.

hpivanov avatar Jun 12 '20 10:06 hpivanov

@MrManojSingh modelWriter.createElement is undefined.

dzpt avatar Sep 09 '20 08:09 dzpt

@dzpt Try modelWriter.writer.createElement.

hpivanov avatar Sep 09 '20 08:09 hpivanov

@hpivanov you saved my day, man! <3

dzpt avatar Sep 09 '20 09:09 dzpt

@hpivanov works on youtube but this plugin would freeze the editor after pasting html that contains googe adsense code. anyway to check the iframe src from youtube only?

dzpt avatar Sep 10 '20 16:09 dzpt

it still not available to support <iframe /> content officially?

bagaskarala avatar Sep 08 '21 07:09 bagaskarala

Hi guys, You can allow iframe rending in the editor just simply by copy past below code in the config

                                extraPlugins: [
				function ( editor ) {
					// Allow <iframe> elements in the model.               
					editor.model.schema.register( 'iframe', {
						allowWhere: '$text',
						allowContentOf: '$block'
					} );
					// Allow <iframe> elements in the model to have all attributes.
					editor.model.schema.addAttributeCheck( context => {
						if ( context.endsWith( 'iframe' ) ) {
							return true;
						}
					} );						
                                       // View-to-model converter converting a view <iframe> with all its attributes to the model.
					editor.conversion.for( 'upcast' ).elementToElement( {
						view: 'iframe',
						model: ( viewElement, modelWriter ) => {
							return modelWriter.createElement( 'iframe', viewElement.getAttributes() );
						}
					} );
				
					// Model-to-view converter for the <iframe> element (attributes are converted separately).
					editor.conversion.for( 'downcast' ).elementToElement( {
						model: 'iframe',
						view: 'iframe'
					} );
				
					// Model-to-view converter for <iframe> attributes.
					// Note that a lower-level, event-based API is used here.
					editor.conversion.for( 'downcast' ).add( dispatcher => {
						dispatcher.on( 'attribute', ( evt, data, conversionApi ) => {
							// Convert <iframe> attributes only.
							if ( data.item.name != 'iframe' ) {
								return;
							}
				
							const viewWriter = conversionApi.writer;
							const viewIframe = conversionApi.mapper.toViewElement( data.item );
				
							// In the model-to-view conversion we convert changes.
							// An attribute can be added or removed or changed.
							// The below code handles all 3 cases.
							if ( data.attributeNewValue ) {
								viewWriter.setAttribute( data.attributeKey, data.attributeNewValue, viewIframe );
							} else {
								viewWriter.removeAttribute( data.attributeKey, viewIframe );
							}
						} );
					} );
					},
			], 

This works when there is additional content along with iframe inside ckeditor.

When there is only iframe inside the ckeditor, if we click source the iframe html code disappears.

Anyone facing the same issue?

Ajez24 avatar Oct 28 '21 13:10 Ajez24

When there is only iframe inside the ckeditor, if we click source the iframe html code disappears. @Ajez24 I got the same issue. Did you find the solution?

sangnguyenplus avatar Nov 29 '21 03:11 sangnguyenplus

When there is only iframe inside the ckeditor, if we click source the iframe html code disappears. @Ajez24 I got the same issue. Did you find the solution?

Hi @sangnguyenplus ,

Yeah, after some research found out a way for iframe support. You can remove the piece of code inside extra plugin completely (only what we did for iframe). And add General HTML support to your CK editor. https://ckeditor.com/docs/ckeditor5/latest/features/general-html-support.html Once you add this, in your CK editor initialization, add the below piece of code

htmlSupport: { allow: [ { name: 'iframe', attributes: true, classes: true, styles: true } ] }

Note: Still I am facing the same issue for script tags, but iframe is working after this change.

Ajez24 avatar Nov 29 '21 03:11 Ajez24

@Ajez24 Thank you so much 👍

sangnguyenplus avatar Nov 29 '21 03:11 sangnguyenplus

There has been no activity on this issue for the past year. We've marked it as stale and will close it in 30 days. We understand it may still be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.

CKEditorBot avatar Oct 11 '23 05:10 CKEditorBot

I am still having trouble implementing iframe with CKEditor 5. I am using General HTML Support and trying to configure it to allow iframe.

htmlSupport: {
  allow: [
    {
      name: 'iframe',
      attributes: {
        src: /.*/,
        style: /.*/,
        width: /.*/,
        height: /.*/,
        frameborder: /.*/,
        webkitallowfullscreen: true,
        mozallowfullscreen: true,
        allowfullscreen: true,
      },
    },
  ]
}

And when I try to compile I get the following error.

      TS2417: Class static side 'typeof Editor' incorrectly extends base class static side 'typeof ClassicEditor'.
  The types of 'defaultConfig.htmlSupport.allow' are incompatible between these types.
    Type '{ name: string; attributes: { src: RegExp; style: RegExp; width: RegExp; height: RegExp; frameborder: RegExp; webkitallowfullscreen: boolean; mozallowfullscreen: boolean; allowfullscreen: boolean; }; }[]' is not assignable to type 'MatcherObjectPattern[]'.
      Type '{ name: string; attributes: { src: RegExp; style: RegExp; width: RegExp; height: RegExp; frameborder: RegExp; webkitallowfullscreen: boolean; mozallowfullscreen: boolean; allowfullscreen: boolean; }; }' is not assignable to type 'MatcherObjectPattern'.
        Types of property 'attributes' are incompatible.
          Type '{ src: RegExp; style: RegExp; width: RegExp; height: RegExp; frameborder: RegExp; webkitallowfullscreen: boolean; mozallowfullscreen: boolean; allowfullscreen: boolean; }' is not assignable to type 'AttributePatterns | undefined'.
            Type '{ src: RegExp; style: RegExp; width: RegExp; height: RegExp; frameborder: RegExp; webkitallowfullscreen: boolean; mozallowfullscreen: boolean; allowfullscreen: boolean; }' is not assignable to type 'Record<string, string | true | RegExp>'.
              Property 'webkitallowfullscreen' is incompatible with index signature.
                Type 'boolean' is not assignable to type 'string | true | RegExp'.
ts-loader-default_e3b0c44298fc1c14

webpack 5.88.2 compiled with 1 error in 12377 ms

I can kind of work around this by setting webkitallowfullscreen, mozallowfullscreen and allowfullscreen to /.*/ However, it appends ="" to these attributes in source when it saves.

a-lam avatar Nov 11 '23 01:11 a-lam

Rel: #15355

Witoso avatar Nov 21 '23 08:11 Witoso

Check that your code is not adding the sandbox="" attribute. Since ck4, it's being added and causing the issue.

acroundtree avatar Jun 21 '24 19:06 acroundtree