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 #98 from ckeditor/t/97
Browse files Browse the repository at this point in the history
Fix: Link should not be allowed directly in the root element. Closes #97.
  • Loading branch information
Reinmar authored Apr 18, 2017
2 parents 5a83b70 + ccd8284 commit 81d4ba5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/linkengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class LinkEngine extends Plugin {
const editing = editor.editing;

// Allow link attribute on all inline nodes.
editor.document.schema.allow( { name: '$inline', attributes: 'linkHref' } );
editor.document.schema.allow( { name: '$inline', attributes: 'linkHref', inside: '$block' } );

// Build converter from model to view for data and editing pipelines.
buildModelConverter().for( data.modelToView, editing.modelToView )
Expand Down
34 changes: 24 additions & 10 deletions tests/linkengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import LinkEngine from '../src/linkengine';
import LinkCommand from '../src/linkcommand';
import LinkElement from '../src/linkelement';
import UnlinkCommand from '../src/unlinkcommand';

import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';

Expand All @@ -16,14 +18,12 @@ describe( 'LinkEngine', () => {

beforeEach( () => {
return VirtualTestEditor.create( {
plugins: [ LinkEngine ]
plugins: [ Paragraph, LinkEngine ]
} )
.then( newEditor => {
editor = newEditor;

doc = editor.document;

doc.schema.allow( { name: '$text', inside: '$root' } );
} );
} );

Expand All @@ -32,7 +32,8 @@ describe( 'LinkEngine', () => {
} );

it( 'should set proper schema rules', () => {
expect( doc.schema.check( { name: '$inline', attributes: [ 'linkHref' ] } ) ).to.be.true;
expect( doc.schema.check( { name: '$inline', attributes: [ 'linkHref' ], inside: '$root' } ) ).to.be.false;
expect( doc.schema.check( { name: '$inline', attributes: [ 'linkHref' ], inside: '$block' } ) ).to.be.true;
} );

describe( 'command', () => {
Expand All @@ -55,24 +56,37 @@ describe( 'LinkEngine', () => {

describe( 'data pipeline conversions', () => {
it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
editor.setData( '<p><a href="url">foo</a>bar</p>' );

expect( getModelData( doc, { withoutSelection: true } ) )
.to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );

expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
} );

it( 'should be integrated with autoparagraphing', () => {
// Incorrect results because autoparagraphing works incorrectly (issue in paragraph).
// https://github.com/ckeditor/ckeditor5-paragraph/issues/10

editor.setData( '<a href="url">foo</a>bar' );

expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<$text linkHref="url">foo</$text>bar' );
expect( editor.getData() ).to.equal( '<a href="url">foo</a>bar' );
expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );

expect( editor.getData() ).to.equal( '<p>foobar</p>' );
} );
} );

describe( 'editing pipeline conversion', () => {
it( 'should convert attribute', () => {
setModelData( doc, '<$text linkHref="url">foo</$text>bar' );
setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );

expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<a href="url">foo</a>bar' );
expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
} );

it( 'should convert to `LinkElement` instance', () => {
setModelData( doc, '<$text linkHref="url">foo</$text>bar' );
setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );

expect( editor.editing.view.getRoot().getChild( 0 ) ).to.be.instanceof( LinkElement );
expect( editor.editing.view.getRoot().getChild( 0 ).getChild( 0 ) ).to.be.instanceof( LinkElement );
} );
} );
} );

0 comments on commit 81d4ba5

Please sign in to comment.