Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #56 from ckeditor/t/47
Browse files Browse the repository at this point in the history
Fix: The `AutoMediaEmbed` should not upcast the pasted URL if a media element is disallowed at the current selection. Closes #47.
  • Loading branch information
oleq authored Oct 29, 2018
2 parents 09c387a + cdd75f2 commit 47092c6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@ckeditor/ckeditor5-clipboard": "^10.0.3",
"@ckeditor/ckeditor5-engine": "^11.0.0",
"@ckeditor/ckeditor5-enter": "^10.1.2",
"@ckeditor/ckeditor5-image": "^11.0.0",
"@ckeditor/ckeditor5-theme-lark": "^11.1.0",
"@ckeditor/ckeditor5-ui": "^11.1.0",
"@ckeditor/ckeditor5-utils": "^11.0.0",
Expand Down
9 changes: 8 additions & 1 deletion src/automediaembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ export default class AutoMediaEmbed extends Plugin {
return;
}

const mediaEmbedCommand = editor.commands.get( 'mediaEmbed' );

// Do not anything if media element cannot be inserted at the current position (#47).
if ( !mediaEmbedCommand.isEnabled ) {
return;
}

// Position won't be available in the `setTimeout` function so let's clone it.
this._positionToInsert = LivePosition.createFromPosition( leftPosition );

Expand All @@ -151,7 +158,7 @@ export default class AutoMediaEmbed extends Plugin {
writer.setSelection( this._positionToInsert );
}

editor.commands.execute( 'mediaEmbed', url );
mediaEmbedCommand.execute( url );

this._positionToInsert.detach();
this._positionToInsert = null;
Expand Down
16 changes: 15 additions & 1 deletion tests/automediaembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Typing from '@ckeditor/ckeditor5-typing/src/typing';
import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
import global from '@ckeditor/ckeditor5-utils/src/dom/global';
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

Expand All @@ -29,7 +31,7 @@ describe( 'AutoMediaEmbed - integration', () => {

return ClassicTestEditor
.create( editorElement, {
plugins: [ MediaEmbed, AutoMediaEmbed, Link, List, Bold, Typing ]
plugins: [ MediaEmbed, AutoMediaEmbed, Link, List, Bold, Typing, Image, ImageCaption ]
} )
.then( newEditor => {
editor = newEditor;
Expand Down Expand Up @@ -302,6 +304,18 @@ describe( 'AutoMediaEmbed - integration', () => {
);
} );

// #47
it( 'does not transform a valid URL into a media if the element cannot be placed in the current position', () => {
setData( editor.model, '<image src="foo.png"><caption>Foo.[]</caption></image>' );
pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );

clock.tick( 100 );

expect( getData( editor.model ) ).to.equal(
'<image src="foo.png"><caption>Foo.https://www.youtube.com/watch?v=H08tGjXNHO4[]</caption></image>'
);
} );

it( 'replaces a URL in media if pasted a link when other media element was selected', () => {
setData(
editor.model,
Expand Down

0 comments on commit 47092c6

Please sign in to comment.