Ver Fonte

Introduced linking engine.

Oskar Wrobel há 9 anos atrás
pai
commit
6bbb57cf96

+ 44 - 0
packages/ckeditor5-link/src/engine.js

@@ -0,0 +1,44 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Feature from '../core/feature.js';
+import buildModelConverter from '../engine/conversion/buildmodelconverter.js';
+import buildViewConverter from '../engine/conversion/buildviewconverter.js';
+import AttributeElement from '../engine/view/attributeelement.js';
+
+/**
+ * The link engine feature.
+ *
+ * It introduces the `link="url"` attribute in the model which renders to the view as a `<a href="url">` element.
+ *
+ * @memberOf link
+ * @extends core.Feature
+ */
+export default class BoldEngine extends Feature {
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		const editor = this.editor;
+		const data = editor.data;
+		const editing = editor.editing;
+
+		// Allow link attribute on all inline nodes.
+		editor.document.schema.allow( { name: '$inline', attributes: [ 'link' ] } );
+
+		// Build converter from model to view for data and editing pipelines.
+		buildModelConverter().for( data.modelToView, editing.modelToView )
+			.fromAttribute( 'link' )
+			.toElement( ( href ) => new AttributeElement( 'a', { href } ) );
+
+		// Build converter from view to model for data pipeline.
+		buildViewConverter().for( data.viewToModel )
+			.fromElement( 'a' )
+			.toAttribute( ( viewElement ) => ( {
+				key: 'link',
+				value: viewElement.getAttribute( 'href' )
+			} ) );
+	}
+}

+ 51 - 0
packages/ckeditor5-link/tests/engine.js

@@ -0,0 +1,51 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import LinkEngine from '/ckeditor5/link/engine.js';
+import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
+import { getData as getModelData, setData as setModelData } from '/tests/engine/_utils/model.js';
+import { getData as getViewData } from '/tests/engine/_utils/view.js';
+
+describe( 'LinkEngine', () => {
+	let editor, doc;
+
+	beforeEach( () => {
+		return VirtualTestEditor.create( {
+				features: [ LinkEngine ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+
+				doc = editor.document;
+
+				doc.schema.allow( { name: '$text', inside: '$root' } );
+			} );
+	} );
+
+	it( 'should be loaded', () => {
+		expect( editor.plugins.get( LinkEngine ) ).to.be.instanceOf( LinkEngine );
+	} );
+
+	it( 'should set proper schema rules', () => {
+		expect( doc.schema.check( { name: '$inline', attributes: [ 'link' ] } ) ).to.be.true;
+	} );
+
+	describe( 'data pipeline conversions', () => {
+		it( 'should convert `<a href="url">` to `bold="url"` attribute', () => {
+			editor.setData( '<a href="url">foo</a>bar' );
+
+			expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<$text link="url">foo</$text>bar' );
+			expect( editor.getData() ).to.equal( '<a href="url">foo</a>bar' );
+		} );
+	} );
+
+	describe( 'editing pipeline conversion', () => {
+		it( 'should convert attribute', () => {
+			setModelData( doc, '<$text link="url">foo</$text>bar' );
+
+			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<a href="url">foo</a>bar' );
+		} );
+	} );
+} );