Explorar el Código

Define basic conversion for non restricted text attribute.

Maciej Gołaszewski hace 6 años
padre
commit
adddcef72d

+ 11 - 1
packages/ckeditor5-restricted-editing/src/restricteddocument.js

@@ -24,6 +24,16 @@ export default class RestrictedDocument extends Plugin {
 	 * @inheritDoc
 	 */
 	init() {
-		this.editor.model.schema.extend( '$text', { allowAttributes: [ 'nonRestricted' ] } );
+		const editor = this.editor;
+
+		editor.model.schema.extend( '$text', { allowAttributes: [ 'nonRestricted' ] } );
+
+		editor.conversion.attributeToElement( {
+			model: 'nonRestricted',
+			view: {
+				name: 'span',
+				classes: 'ck-non-restricted'
+			}
+		} );
 	}
 }

+ 31 - 4
packages/ckeditor5-restricted-editing/tests/restricteddocument.js

@@ -3,21 +3,26 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+
 import testUtils from './_utils/utils';
 import VirtualTestEditor from './_utils/virtualtesteditor';
+import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
+import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import RestrictedDocument from './../src/restricteddocument';
 
 describe( 'RestrictedDocument', () => {
-	let editor;
+	let editor, model;
 
 	testUtils.createSinonSandbox();
 
 	beforeEach( () => {
 		return VirtualTestEditor
-			.create( { plugins: [ RestrictedDocument ] } )
+			.create( { plugins: [ Paragraph, RestrictedDocument ] } )
 			.then( newEditor => {
 				editor = newEditor;
+				model = editor.model;
 			} );
 	} );
 
@@ -36,8 +41,6 @@ describe( 'RestrictedDocument', () => {
 	} );
 
 	it( 'should set proper schema rules', () => {
-		const model = editor.model;
-
 		expect( model.schema.checkAttribute( [ '$root', '$text' ], 'nonRestricted' ) ).to.be.true;
 
 		expect( model.schema.checkAttribute( [ '$block', '$text' ], 'nonRestricted' ) ).to.be.true;
@@ -45,4 +48,28 @@ describe( 'RestrictedDocument', () => {
 
 		expect( model.schema.checkAttribute( [ '$block' ], 'nonRestricted' ) ).to.be.false;
 	} );
+
+	describe( 'conversion', () => {
+		describe( 'upcast', () => {
+			it( 'should convert <span class="ck-non-restricted"> to model attribute', () => {
+				editor.setData( '<p>foo <span class="ck-non-restricted">bar</span> baz</p>' );
+
+				expect( getModelData( model, { withoutSelection: true } ) )
+					.to.equal( '<paragraph>foo <$text nonRestricted="true">bar</$text> baz</paragraph>' );
+			} );
+		} );
+
+		describe( 'downcast', () => {
+			it( 'should convert model attribute to <span>', () => {
+				const expectedView = '<p>foo <span class="ck-non-restricted">bar</span> baz</p>';
+
+				setModelData( editor.model,
+					'<paragraph>foo <$text nonRestricted="true">bar</$text> baz</paragraph>'
+				);
+
+				expect( editor.getData() ).to.equal( expectedView );
+				expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
+			} );
+		} );
+	} );
 } );