diff --git a/.travis.yml b/.travis.yml index 8358cfe21..6a542fc7b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,11 @@ sudo: required -dist: trusty +dist: xenial addons: chrome: stable firefox: latest language: node_js +services: +- xvfb node_js: - '8' cache: @@ -13,8 +15,6 @@ branches: - stable before_install: - export START_TIME=$( date +%s ) -- export DISPLAY=:99.0 -- sh -e /etc/init.d/xvfb start - npm i -g yarn install: - yarn add @ckeditor/ckeditor5-dev-tests diff --git a/README.md b/README.md index 4459e4ef7..2162e4354 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ CKEditor 5 editing engine ======================================== -[![Join the chat at https://gitter.im/ckeditor/ckeditor5](https://badges.gitter.im/ckeditor/ckeditor5.svg)](https://gitter.im/ckeditor/ckeditor5?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![npm version](https://badge.fury.io/js/%40ckeditor%2Fckeditor5-engine.svg)](https://www.npmjs.com/package/@ckeditor/ckeditor5-engine) [![Build Status](https://travis-ci.org/ckeditor/ckeditor5-engine.svg?branch=master)](https://travis-ci.org/ckeditor/ckeditor5-engine) [![Coverage Status](https://coveralls.io/repos/github/ckeditor/ckeditor5-engine/badge.svg?branch=master)](https://coveralls.io/github/ckeditor/ckeditor5-engine?branch=master) diff --git a/docs/_snippets/framework/build-extending-content-source.html b/docs/_snippets/framework/build-extending-content-source.html new file mode 100644 index 000000000..e69de29bb diff --git a/docs/_snippets/framework/build-extending-content-source.js b/docs/_snippets/framework/build-extending-content-source.js new file mode 100644 index 000000000..80cb20c9e --- /dev/null +++ b/docs/_snippets/framework/build-extending-content-source.js @@ -0,0 +1,15 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals window */ + +import ClassicEditor from '@ckeditor/ckeditor5-build-classic/src/ckeditor'; +import Code from '@ckeditor/ckeditor5-basic-styles/src/code'; +import Font from '@ckeditor/ckeditor5-font/src/font'; + +ClassicEditor.builtinPlugins.push( Code ); +ClassicEditor.builtinPlugins.push( Font ); + +window.ClassicEditor = ClassicEditor; diff --git a/docs/_snippets/framework/extending-content-add-external-link-target.html b/docs/_snippets/framework/extending-content-add-external-link-target.html new file mode 100644 index 000000000..acefb3210 --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-external-link-target.html @@ -0,0 +1,11 @@ + + + diff --git a/docs/_snippets/framework/extending-content-add-external-link-target.js b/docs/_snippets/framework/extending-content-add-external-link-target.js new file mode 100644 index 000000000..35ac0a99b --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-external-link-target.js @@ -0,0 +1,47 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +function AddTargetToExternalLinks( editor ) { + editor.conversion.for( 'downcast' ).add( dispatcher => { + dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + const viewSelection = viewWriter.document.selection; + const viewElement = viewWriter.createAttributeElement( 'a', { + target: '_blank' + }, { + priority: 5 + } ); + + if ( data.attributeNewValue.match( /ckeditor\.com/ ) ) { + viewWriter.unwrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } else { + if ( data.item.is( 'selection' ) ) { + viewWriter.wrap( viewSelection.getFirstRange(), viewElement ); + } else { + viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + } + }, { priority: 'low' } ); + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-link-external' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ AddTargetToExternalLinks ], + toolbar: { + viewportTopOffset: window.getViewportTopOffsetConfig() + } + } ) + .then( editor => { + window.editor = editor; + } ) + .catch( err => { + console.error( err.stack ); + } ); diff --git a/docs/_snippets/framework/extending-content-add-heading-class.html b/docs/_snippets/framework/extending-content-add-heading-class.html new file mode 100644 index 000000000..0ef420288 --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-heading-class.html @@ -0,0 +1,14 @@ + + +
+

Heading with .my-heading class

+

Regular heading

+

Some content.

+
diff --git a/docs/_snippets/framework/extending-content-add-heading-class.js b/docs/_snippets/framework/extending-content-add-heading-class.js new file mode 100644 index 000000000..44206c96f --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-heading-class.js @@ -0,0 +1,33 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +function AddClassToAllHeading1( editor ) { + editor.conversion.for( 'downcast' ).add( dispatcher => { + dispatcher.on( 'insert:heading1', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + + viewWriter.addClass( 'my-heading', conversionApi.mapper.toViewElement( data.item ) ); + }, { priority: 'low' } ); + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-heading-class' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ AddClassToAllHeading1 ], + toolbar: { + viewportTopOffset: window.getViewportTopOffsetConfig() + } + } ) + .then( editor => { + window.editor = editor; + } ) + .catch( err => { + console.error( err.stack ); + } ); diff --git a/docs/_snippets/framework/extending-content-add-link-class.html b/docs/_snippets/framework/extending-content-add-link-class.html new file mode 100644 index 000000000..ae65817a8 --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-link-class.html @@ -0,0 +1,14 @@ + + + diff --git a/docs/_snippets/framework/extending-content-add-link-class.js b/docs/_snippets/framework/extending-content-add-link-class.js new file mode 100644 index 000000000..d1b5fce28 --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-link-class.js @@ -0,0 +1,43 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +function AddClassToAllLinks( editor ) { + editor.conversion.for( 'downcast' ).add( dispatcher => { + dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + const viewSelection = viewWriter.document.selection; + const viewElement = viewWriter.createAttributeElement( 'a', { + class: 'my-green-link' + }, { + priority: 5 + } ); + + if ( data.item.is( 'selection' ) ) { + viewWriter.wrap( viewSelection.getFirstRange(), viewElement ); + } else { + viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + }, { priority: 'low' } ); + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-link-classes' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ AddClassToAllLinks ], + toolbar: { + viewportTopOffset: window.getViewportTopOffsetConfig() + } + } ) + .then( editor => { + window.editor = editor; + } ) + .catch( err => { + console.error( err.stack ); + } ); diff --git a/docs/_snippets/framework/extending-content-add-unsafe-link-class.html b/docs/_snippets/framework/extending-content-add-unsafe-link-class.html new file mode 100644 index 000000000..4503e57e1 --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-unsafe-link-class.html @@ -0,0 +1,12 @@ + + + diff --git a/docs/_snippets/framework/extending-content-add-unsafe-link-class.js b/docs/_snippets/framework/extending-content-add-unsafe-link-class.js new file mode 100644 index 000000000..c1a37c0d8 --- /dev/null +++ b/docs/_snippets/framework/extending-content-add-unsafe-link-class.js @@ -0,0 +1,43 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +function AddClassToUnsafeLinks( editor ) { + editor.conversion.for( 'downcast' ).add( dispatcher => { + dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + const viewSelection = viewWriter.document.selection; + const viewElement = viewWriter.createAttributeElement( 'a', { class: 'unsafe-link' }, { priority: 5 } ); + + if ( data.attributeNewValue.match( /http:\/\// ) ) { + if ( data.item.is( 'selection' ) ) { + viewWriter.wrap( viewSelection.getFirstRange(), viewElement ); + } else { + viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + } else { + viewWriter.unwrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + }, { priority: 'low' } ); + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-link-unsafe-classes' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ AddClassToUnsafeLinks ], + toolbar: { + viewportTopOffset: window.getViewportTopOffsetConfig() + } + } ) + .then( editor => { + window.editor = editor; + } ) + .catch( err => { + console.error( err.stack ); + } ); diff --git a/docs/_snippets/framework/extending-content-allow-div-attributes.html b/docs/_snippets/framework/extending-content-allow-div-attributes.html new file mode 100644 index 000000000..52d09c0b4 --- /dev/null +++ b/docs/_snippets/framework/extending-content-allow-div-attributes.html @@ -0,0 +1,12 @@ +
+
+

Special section A: it has set "style" and "id" attributes.

+
+ +

Regular content of the editor.

+ +
+

Special section B: it has set "style", "id" and spellcheck="false" attributes.

+

This section disables native browser spellchecker.

+
+
diff --git a/docs/_snippets/framework/extending-content-allow-div-attributes.js b/docs/_snippets/framework/extending-content-allow-div-attributes.js new file mode 100644 index 000000000..46d350159 --- /dev/null +++ b/docs/_snippets/framework/extending-content-allow-div-attributes.js @@ -0,0 +1,74 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +function ConvertDivAttributes( editor ) { + // Allow divs in the model. + editor.model.schema.register( 'div', { + allowWhere: '$block', + allowContentOf: '$root' + } ); + + // Allow divs in the model to have all attributes. + editor.model.schema.addAttributeCheck( context => { + if ( context.endsWith( 'div' ) ) { + return true; + } + } ); + + // View-to-model converter converting a view div with all its attributes to the model. + editor.conversion.for( 'upcast' ).elementToElement( { + view: 'div', + model: ( viewElement, modelWriter ) => { + return modelWriter.createElement( 'div', viewElement.getAttributes() ); + } + } ); + + // Model-to-view convert for the div element (attrbiutes are converted separately). + editor.conversion.for( 'downcast' ).elementToElement( { + model: 'div', + view: 'div' + } ); + + // Model-to-view converter for div attributes. + // Note that we use a lower-level, event-based API here. + editor.conversion.for( 'downcast' ).add( dispatcher => { + dispatcher.on( 'attribute', ( evt, data, conversionApi ) => { + // Convert div attributes only. + if ( data.item.name != 'div' ) { + return; + } + + const viewWriter = conversionApi.writer; + const viewDiv = 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, viewDiv ); + } else { + viewWriter.removeAttribute( data.attributeKey, viewDiv ); + } + } ); + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-div-attributes' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ ConvertDivAttributes ], + toolbar: { + viewportTopOffset: window.getViewportTopOffsetConfig() + } + } ) + .then( editor => { + window.editor = editor; + } ) + .catch( err => { + console.error( err.stack ); + } ); diff --git a/docs/_snippets/framework/extending-content-allow-link-target.html b/docs/_snippets/framework/extending-content-allow-link-target.html new file mode 100644 index 000000000..9ffaa7b30 --- /dev/null +++ b/docs/_snippets/framework/extending-content-allow-link-target.html @@ -0,0 +1,33 @@ + + + + +

Note: You can play with the content to see that different link target values are also handled.

+ + + +

+ +

diff --git a/docs/_snippets/framework/extending-content-allow-link-target.js b/docs/_snippets/framework/extending-content-allow-link-target.js new file mode 100644 index 000000000..f197bedef --- /dev/null +++ b/docs/_snippets/framework/extending-content-allow-link-target.js @@ -0,0 +1,51 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +function AllowLinkTarget( editor ) { + editor.model.schema.extend( '$text', { allowAttributes: 'linkTarget' } ); + + editor.conversion.for( 'downcast' ).attributeToElement( { + model: 'linkTarget', + view: ( attributeValue, writer ) => { + const linkElement = writer.createAttributeElement( 'a', { target: attributeValue }, { priority: 5 } ); + writer.setCustomProperty( 'link', true, linkElement ); + + return linkElement; + }, + converterPriority: 'low' + } ); + + editor.conversion.for( 'upcast' ).attributeToAttribute( { + view: { + name: 'a', + key: 'target' + }, + model: 'linkTarget', + converterPriority: 'low' + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-link-target' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ AllowLinkTarget ], + toolbar: { + viewportTopOffset: window.getViewportTopOffsetConfig() + } + } ) + .then( editor => { + window.editor = editor; + + document.querySelector( '#snippet-link-target-content-update' ).addEventListener( 'click', () => { + editor.setData( document.querySelector( '#snippet-link-target-content' ).value ); + } ); + } ) + .catch( err => { + console.error( err.stack ); + } ); diff --git a/docs/_snippets/framework/extending-content-arbitrary-attribute-values.html b/docs/_snippets/framework/extending-content-arbitrary-attribute-values.html new file mode 100644 index 000000000..73b38a591 --- /dev/null +++ b/docs/_snippets/framework/extending-content-arbitrary-attribute-values.html @@ -0,0 +1,7 @@ +
+ +
diff --git a/docs/_snippets/framework/extending-content-arbitrary-attribute-values.js b/docs/_snippets/framework/extending-content-arbitrary-attribute-values.js new file mode 100644 index 000000000..d02e36b21 --- /dev/null +++ b/docs/_snippets/framework/extending-content-arbitrary-attribute-values.js @@ -0,0 +1,68 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +function HandleFontSizeValue( editor ) { + // Add special catch-all converter for font-size feature. + editor.conversion.for( 'upcast' ).elementToAttribute( { + view: { + name: 'span', + styles: { + 'font-size': /[\s\S]+/ + } + }, + model: { + key: 'fontSize', + value: viewElement => { + const value = parseFloat( viewElement.getStyle( 'font-size' ) ).toFixed( 0 ); + + // It might be needed to further convert the value to meet business requirements. + // In the sample the font-size is configured to handle only the sizes: + // 10, 12, 14, 'default', 18, 20, 22 + // Other sizes will be converted to the model but the UI might not be aware of them. + + // The font-size feature expects numeric values to be Number not String. + return parseInt( value ); + } + }, + converterPriority: 'high' + } ); + + // Add special converter for font-size feature to convert all (even not configured) model attribute values. + editor.conversion.for( 'downcast' ).attributeToElement( { + model: { + key: 'fontSize' + }, + view: ( modelValue, viewWriter ) => { + return viewWriter.createAttributeElement( 'span', { + style: `font-size:${ modelValue }px` + } ); + }, + converterPriority: 'high' + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-arbitrary-attribute-values' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ HandleFontSizeValue ], + toolbar: { + items: [ 'heading', '|', 'bold', 'italic', '|', 'fontSize' ], + viewportTopOffset: window.getViewportTopOffsetConfig() + }, + fontSize: { + options: [ 10, 12, 14, 'default', 18, 20, 22 ] + } + } ) + .then( editor => { + window.editor = editor; + } ) + .catch( err => { + console.error( err.stack ); + } ); + diff --git a/docs/_snippets/framework/extending-content-custom-figure-attributes.html b/docs/_snippets/framework/extending-content-custom-figure-attributes.html new file mode 100644 index 000000000..335f70539 --- /dev/null +++ b/docs/_snippets/framework/extending-content-custom-figure-attributes.html @@ -0,0 +1,23 @@ + + + + +
+

Image:

+
+ bar +
Caption
+
+ +

Table:

+ + + + +
foo
+
diff --git a/docs/_snippets/framework/extending-content-custom-figure-attributes.js b/docs/_snippets/framework/extending-content-custom-figure-attributes.js new file mode 100644 index 000000000..2e4643a3b --- /dev/null +++ b/docs/_snippets/framework/extending-content-custom-figure-attributes.js @@ -0,0 +1,170 @@ +/** + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. + * For licensing, see LICENSE.md. + */ + +/* globals ClassicEditor, console, window, document */ + +import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config'; + +/** + * Plugin that converts custom attributes for elements that are wrapped in
in the view. + */ +function CustomFigureAttributes( editor ) { + // Define on which elements the css classes should be preserved: + setupCustomClassConversion( 'img', 'image', editor ); + setupCustomClassConversion( 'table', 'table', editor ); + + editor.conversion.for( 'upcast' ).add( upcastCustomClasses( 'figure' ), { priority: 'low' } ); + + // Define custom attributes that should be preserved. + setupCustomAttributeConversion( 'img', 'image', 'id', editor ); + setupCustomAttributeConversion( 'table', 'table', 'id', editor ); +} + +/** + * Setups conversion that preservers classes on img/table elements + */ +function setupCustomClassConversion( viewElementName, modelElementName, editor ) { + // The 'customClass' attribute will store custom classes from data in the model so schema definitions to allow this attribute. + editor.model.schema.extend( modelElementName, { allowAttributes: [ 'customClass' ] } ); + + // Define upcast converters for and elements with "low" priority so they are run after default converters. + editor.conversion.for( 'upcast' ).add( upcastCustomClasses( viewElementName ), { priority: 'low' } ); + + // Define downcast converters for model element with "low" priority so they are run after default converters. + editor.conversion.for( 'downcast' ).add( downcastCustomClasses( modelElementName ), { priority: 'low' } ); +} + +/** + * Setups conversion for custom attribute on view elements contained inside figure. + * + * This method: + * + * - adds proper schema rules + * - adds an upcast converter + * - adds a downcast converter + */ +function setupCustomAttributeConversion( viewElementName, modelElementName, viewAttribute, editor ) { + // Extend schema to store attribute in the model. + const modelAttribute = `custom${ viewAttribute }`; + + editor.model.schema.extend( modelElementName, { allowAttributes: [ modelAttribute ] } ); + + editor.conversion.for( 'upcast' ).add( upcastAttribute( viewElementName, viewAttribute, modelAttribute ) ); + editor.conversion.for( 'downcast' ).add( downcastAttribute( modelElementName, viewElementName, viewAttribute, modelAttribute ) ); +} + +/** + * Creates upcast converter that will pass all classes from view element to model element. + */ +function upcastCustomClasses( elementName ) { + return dispatcher => dispatcher.on( `element:${ elementName }`, ( evt, data, conversionApi ) => { + const viewItem = data.viewItem; + const modelRange = data.modelRange; + + const modelElement = modelRange && modelRange.start.nodeAfter; + + if ( !modelElement ) { + return; + } + + // The upcast conversion pick up classes from base element and from figure element also so it should be extensible. + const currentAttributeValue = modelElement.getAttribute( 'customClass' ) || []; + + currentAttributeValue.push( ...viewItem.getClassNames() ); + + conversionApi.writer.setAttribute( 'customClass', currentAttributeValue, modelElement ); + } ); +} + +/** + * Creates downcast converter that add classes defined in `customClass` attribute to given view element. + * + * This converter expects that view element is nested in figure element. + */ +function downcastCustomClasses( modelElementName ) { + return dispatcher => dispatcher.on( `insert:${ modelElementName }`, ( evt, data, conversionApi ) => { + const modelElement = data.item; + + const viewFigure = conversionApi.mapper.toViewElement( modelElement ); + + if ( !viewFigure ) { + return; + } + + // The below code assumes that classes are set on
element... + conversionApi.writer.addClass( modelElement.getAttribute( 'customClass' ), viewFigure ); + + // ... but if you preferIf the classes should be passed to the find the view element inside figure: + // + // const viewElement = findViewChild( viewFigure, viewElementName, conversionApi ); + // + // conversionApi.writer.addClass( modelElement.getAttribute( 'customClass' ), viewElement ); + } ); +} + +/** + * Helper method that search for given view element in all children of model element. + * + * @param {module:engine/view/item~Item} viewElement + * @param {String} viewElementName + * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi + * @return {module:engine/view/item~Item} + */ +function findViewChild( viewElement, viewElementName, conversionApi ) { + const viewChildren = Array.from( conversionApi.writer.createRangeIn( viewElement ).getItems() ); + + return viewChildren.find( item => item.is( viewElementName ) ); +} + +/** + * Returns custom attribute upcast converter. + */ +function upcastAttribute( viewElementName, viewAttribute, modelAttribute ) { + return dispatcher => dispatcher.on( `element:${ viewElementName }`, ( evt, data, conversionApi ) => { + const viewItem = data.viewItem; + const modelRange = data.modelRange; + + const modelElement = modelRange && modelRange.start.nodeAfter; + + if ( !modelElement ) { + return; + } + + conversionApi.writer.setAttribute( modelAttribute, viewItem.getAttribute( viewAttribute ), modelElement ); + } ); +} + +/** + * Returns custom attribute downcast converter. + */ +function downcastAttribute( modelElementName, viewElementName, viewAttribute, modelAttribute ) { + return dispatcher => dispatcher.on( `insert:${ modelElementName }`, ( evt, data, conversionApi ) => { + const modelElement = data.item; + + const viewFigure = conversionApi.mapper.toViewElement( modelElement ); + const viewElement = findViewChild( viewFigure, viewElementName, conversionApi ); + + if ( !viewElement ) { + return; + } + + conversionApi.writer.setAttribute( viewAttribute, modelElement.getAttribute( modelAttribute ), viewElement ); + } ); +} + +ClassicEditor + .create( document.querySelector( '#snippet-custom-figure-attributes' ), { + cloudServices: CS_CONFIG, + extraPlugins: [ CustomFigureAttributes ], + toolbar: { + viewportTopOffset: window.getViewportTopOffsetConfig() + } + } ) + .then( editor => { + window.editor = editor; + } ) + .catch( err => { + console.error( err.stack ); + } ); diff --git a/docs/api/engine.md b/docs/api/engine.md index 1cef12f85..d6fa199d4 100644 --- a/docs/api/engine.md +++ b/docs/api/engine.md @@ -30,5 +30,5 @@ The source code of this package is available on GitHub in https://github.com/cke * [`@ckeditor/ckeditor5-engine` on npm](https://www.npmjs.com/package/@ckeditor/ckeditor5-engine) * [`ckeditor/ckeditor5-engine` on GitHub](https://github.com/ckeditor/ckeditor5-engine) -* [Issue tracker](https://github.com/ckeditor/ckeditor5-engine/issues) +* [Issue tracker](https://github.com/ckeditor/ckeditor5/issues) * [Changelog](https://github.com/ckeditor/ckeditor5-engine/blob/master/CHANGELOG.md) diff --git a/docs/framework/guides/deep-dive/conversion-extending-output.md b/docs/framework/guides/deep-dive/conversion-extending-output.md new file mode 100644 index 000000000..ff46cae84 --- /dev/null +++ b/docs/framework/guides/deep-dive/conversion-extending-output.md @@ -0,0 +1,359 @@ +--- +category: framework-deep-dive-conversion +menu-title: Extending editor output +order: 20 +--- + +{@snippet framework/build-extending-content-source} + +# Extending editor output + +In this guide, we will focus on customization to the one–way {@link framework/guides/architecture/editing-engine#editing-pipeline "downcast"} pipeline of the editor, which transforms data from the model to the editing view and the output data only. The following examples do not customize the model and do not process the (input) data — you can picture them as post–processors (filters) applied to the output only. + +If you want to learn how to load some extra content (element, attributes, classes) into the editor, check out the {@link framework/guides/deep-dive/conversion-preserving-custom-content next guide} of this guide. + +## Before starting + +### Code architecture + +It is recommended that the code that customizes editor data and editing pipelines is delivered as {@link framework/guides/architecture/core-editor-architecture#plugins plugins} and all examples in this chapter follow this convention. + +Also for the sake of simplicity all examples use the same {@link module:editor-classic/classiceditor~ClassicEditor `ClassicEditor`} but keep in mind that code snippets will work with other editors too. + +Finally, none of the converters covered in this guide require to import any module from CKEditor 5 Framework, hence, you can write them without rebuilding the editor. In other words, such converters can easily be added to existing CKEditor 5 builds. + +### Granular converters + +You can create separate converters for data and editing (downcast) pipelines. The former (`dataDowncast`) will customize the data in the editor output (e.g. when {@link module:core/editor/utils/dataapimixin~DataApi#getData `editor.getData()`}) and the later (`editingDowncast`) will only work for the content of the editor when editing. + +If you do not want to complicate your conversion, you can just add a single (`downcast`) converter which will apply both to the data and the editing view. We did that in all examples to keep them simple but keep in mind you have options: + +```js +// Adds a conversion dispatcher for the editing downcast pipeline only. +editor.conversion.for( 'editingDowncast' ).add( dispatcher => { + // ... +} ); + +// Adds a conversion dispatcher for the data downcast pipeline only. +editor.conversion.for( 'dataDowncast' ).add( dispatcher => { + // ... +} ); + +// Adds a conversion dispatcher for both data and editing downcast pipelines. +editor.conversion.for( 'downcast' ).add( dispatcher => { + // ... +} ); +``` + +### CKEditor 5 inspector + +{@link framework/guides/development-tools#ckeditor-5-inspector CKEditor 5 inspector} is an invaluable help with working with the model and view structures. It allows browsing their structure and checking selection positions like in typical browser dev tools. Make sure to enable CKEditor 5 inspector when playing with CKEditor 5. + +## Adding a CSS class to inline elements + +In this example all links (`...`) get the `.my-green-link` CSS class. That includes all links in the editor output (`editor.getData()`) and all links in the edited content (existing and future ones). + + + Note that the same behavior can be obtained with {@link features/link#custom-link-attributes-decorators link decorators}: + + ```js + ClassicEditor + .create( ..., { + // ... + link: { + decorators: { + addGreenLink: { + mode: 'automatic', + attributes: { + class: 'my-green-link' + } + } + } + } + } ) + ``` + + +{@snippet framework/extending-content-add-link-class} + +Adding a custom CSS class to all links is made by a custom converter plugged into the downcast pipeline, following the default converters brought by the {@link features/link Link} feature: + +```js +// This plugin brings customization to the downcast pipeline of the editor. +function AddClassToAllLinks( editor ) { + // Both data and editing pipelines are affected by this conversion. + editor.conversion.for( 'downcast' ).add( dispatcher => { + // Links are represented in the model as a "linkHref" attribute. + // Use the "low" listener priority to apply the changes after the Link feature. + dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + const viewSelection = viewWriter.document.selection; + + // Adding a new CSS class is done by wrapping all link ranges and selection + // in a new attribute element with a class. + const viewElement = viewWriter.createAttributeElement( 'a', { + class: 'my-green-link' + }, { + priority: 5 + } ); + + if ( data.item.is( 'selection' ) ) { + viewWriter.wrap( viewSelection.getFirstRange(), viewElement ); + } else { + viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + }, { priority: 'low' } ); + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + extraPlugins: [ AddClassToAllLinks ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` + +Add some CSS styles for `.my-green-link` to see the customization in action: + +```css +.my-green-link { + color: #209a25; + border: 1px solid #209a25; + border-radius: 2px; + padding: 0 3px; + box-shadow: 1px 1px 0 0 #209a25; +} +``` + +## Adding an HTML attribute to certain inline elements + +In this example all links (`...`) which do not have "ckeditor.com" in their `href="..."` get the `target="_blank"` attribute. That includes all links in the editor output (`editor.getData()`) and all links in the edited content (existing and future ones). + + + Note that similar behavior can be obtained with {@link module:link/link~LinkConfig#addTargetToExternalLinks link decorators}: + + ```js + ClassicEditor + .create( ..., { + // ... + link: { + addTargetToExternalLinks: true + } + } ) + ``` + + +{@snippet framework/extending-content-add-external-link-target} + +**Note:** Edit the URL of the links including "ckeditor.com" and other domains to see them marked as "internal" or "external". + +Adding the `target` attribute to all "external" links is made by a custom converter plugged into the downcast pipeline, following the default converters brought by the {@link features/link Link} feature: + +```js +// This plugin brings customization to the downcast pipeline of the editor. +function AddTargetToExternalLinks( editor ) { + // Both data and editing pipelines are affected by this conversion. + editor.conversion.for( 'downcast' ).add( dispatcher => { + // Links are represented in the model as a "linkHref" attribute. + // Use the "low" listener priority to apply the changes after the Link feature. + dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + const viewSelection = viewWriter.document.selection; + + // Adding a new CSS class is done by wrapping all link ranges and selection + // in a new attribute element with the "target" attribute. + const viewElement = viewWriter.createAttributeElement( 'a', { + target: '_blank' + }, { + priority: 5 + } ); + + if ( data.attributeNewValue.match( /ckeditor\.com/ ) ) { + viewWriter.unwrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } else { + if ( data.item.is( 'selection' ) ) { + viewWriter.wrap( viewSelection.getFirstRange(), viewElement ); + } else { + viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + } + }, { priority: 'low' } ); + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + extraPlugins: [ AddTargetToExternalLinks ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` + +Add some CSS styles for links with `target="_blank"` to mark them with with the "⧉" symbol: + +```css +a[target="_blank"]::after { + content: '\29C9'; +} +``` + +## Adding a CSS class to certain inline elements + +In this example all links (`...`) which do not have "https://" in their `href="..."` attribute get the `.unsafe-link` CSS class. That includes all links in the editor output (`editor.getData()`) and all links in the edited content (existing and future ones). + + + Note that the same behavior can be obtained with {@link features/link#custom-link-attributes-decorators link decorators}: + + ```js + ClassicEditor + .create( ..., { + // ... + link: { + decorators: { + markUnsafeLink: { + mode: 'automatic', + callback: url => /^(http:)?\/\//.test( url ), + attributes: { + class: 'unsafe-link' + } + } + } + } + } ) + ``` + + +{@snippet framework/extending-content-add-unsafe-link-class} + +**Note:** Edit the URL of the links using "http://" or "https://" to see them marked as "safe" or "unsafe". + +Adding the `.unsafe-link` CSS class to all "unsafe" links is made by a custom converter plugged into the downcast pipeline, following the default converters brought by the {@link features/link Link} feature: + +```js +// This plugin brings customization to the downcast pipeline of the editor. +function AddClassToUnsafeLinks( editor ) { + // Both data and editing pipelines are affected by this conversion. + editor.conversion.for( 'downcast' ).add( dispatcher => { + // Links are represented in the model as a "linkHref" attribute. + // Use the "low" listener priority to apply the changes after the Link feature. + dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + const viewSelection = viewWriter.document.selection; + + // Adding a new CSS class is done by wrapping all link ranges and selection + // in a new attribute element with the "target" attribute. + const viewElement = viewWriter.createAttributeElement( 'a', { + class: 'unsafe-link' + }, { + priority: 5 + } ); + + if ( data.attributeNewValue.match( /http:\/\// ) ) { + if ( data.item.is( 'selection' ) ) { + viewWriter.wrap( viewSelection.getFirstRange(), viewElement ); + } else { + viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + } else { + viewWriter.unwrap( conversionApi.mapper.toViewRange( data.range ), viewElement ); + } + }, { priority: 'low' } ); + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + extraPlugins: [ AddClassToUnsafeLinks ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` + +Add some CSS styles for "unsafe" to make them visible: + +```css +.unsafe-link { + padding: 0 2px; + outline: 2px dashed red; + background: #ffff00; +} +``` + +## Adding a CSS class to block elements + +In this example all second–level headings (`

...

`) get the `.my-heading` CSS class. That includes all heading elements in the editor output (`editor.getData()`) and in the edited content (existing and future ones). + +{@snippet framework/extending-content-add-heading-class} + +Adding a custom CSS class to all `

...

` elements is made by a custom converter plugged into the downcast pipeline, following the default converters brought by the {@link features/headings Headings} feature: + + + The `heading1` element in the model corresponds to `

...

` in the output HTML because in the default {@link features/headings#configuring-heading-levels headings feature configuration} `

...

` is reserved for the top–most heading of a webpage. +
+ +```js +// This plugin brings customization to the downcast pipeline of the editor. +function AddClassToAllHeading1( editor ) { + // Both data and editing pipelines are affected by this conversion. + editor.conversion.for( 'downcast' ).add( dispatcher => { + // Headings are represented in the model as a "heading1" element. + // Use the "low" listener priority to apply the changes after the Headings feature. + dispatcher.on( 'insert:heading1', ( evt, data, conversionApi ) => { + const viewWriter = conversionApi.writer; + + viewWriter.addClass( 'my-heading', conversionApi.mapper.toViewElement( data.item ) ); + }, { priority: 'low' } ); + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + extraPlugins: [ AddClassToAllHeading1 ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` + +Add some CSS styles for `.my-heading` to see the customization in action: + +```css +.my-heading { + font-family: Georgia, Times, Times New Roman, serif; + border-left: 6px solid #fd0000; + padding-left: .8em; + padding: .1em .8em; +} +``` diff --git a/docs/framework/guides/deep-dive/conversion-introduction.md b/docs/framework/guides/deep-dive/conversion-introduction.md new file mode 100644 index 000000000..e925cbab1 --- /dev/null +++ b/docs/framework/guides/deep-dive/conversion-introduction.md @@ -0,0 +1,128 @@ +--- +category: framework-deep-dive-conversion +menu-title: Advanced concepts +order: 10 + +# IMPORTANT: +# This guide is meant to become "Introduction to conversion" later on, hence the file name. +# For now, due to lack of content, it is called "advanced concepts". +--- + +# Advanced conversion concepts + +This guide extends the {@link framework/guides/architecture/editing-engine introduction to CKEditor 5 editing engine architecture}. Therefore, we highly recommend reading the former guide first. + +In this guide we will dive deeper into some of the conversion concepts. + +## Inline and block content + +Generally speaking, there are two main types of the content in the editor view and data output: inline and block. + +The inline content means elements like ``, `` or ``. Unlike `

`, `

` or `
`, inline elements do not structure the data. Instead, they mark some text in a specific (visual and semantical) way. These elements are a characteristic of a text. For instance, we could say that some part of a text is bold, or a linked, etc.. This concept has its reflection in the model of the editor where `` or `` are not represented as elements. Instead, they are attributes of the text. + +For example — in the model, we might have a `` element with "Foo bar" text, where "bar" has the `bold` attribute set `true`. A pseudo–code of this *model* data structure could look as follows: + +```html + + "Foo " // no attributes + "bar" // bold=true + +``` + + + Throughout the rest of this guide we will use the following, shorter convention to represent model text attributes: + + ```html + Foo <$text bold="true">bar + ``` + + +Note that there is no `` or any other additional element there, it is just some text with an attribute. + +So, when this text becomes wrapped with a `` element? This happens during conversion to the view. It is also important to know which type of a view element needs to be used. In case of elements which represent inline formatting, this should be a {@link module:engine/view/attributeelement~AttributeElement}. + +## Conversion of multiple text attributes + +A model text node may have multiple attributes (e.g. be bolded and linked) and all of them are converted to their respective view elements by independent converters. + +Keep in mind that in the model, attributes do not have any specific order. This is contrary to the editor view or HTML output, where inline elements are nested one in another. Fortunately, the nesting happens automatically during conversion from the model to the view. This makes working in the model simpler, as features do not need to take care of breaking or rearranging elements in the model. + +For instance, consider the following model structure: + +```html + + <$text bold="true" linkHref="url">Foo + <$text linkHref="url">bar + <$text bold="true"> baz + +``` + +During conversion, it will be converted to the following view structure: + +```html +

+ Foo bar baz +

+``` + +Note, that the `` element is converted in such way that it always becomes the "topmost" element. This is intentional so that no element ever breaks a link, which would otherwise look as follows: + +```html +

+ Foo bar baz +

+``` + +There are two links with the same `href` next to each other in the generated view (editor output), which is semantically wrong. To make sure that it never happens the view element which represents a link must have a *priority* defined. Most elements, like for instance `` do not care about it and stick to the default priority (`10`). The {@link features/link link feature} ensures that all view `` elements have priority set to `5` so they are kept outside other elements. + +## Merging attribute elements during conversion + +Most of the simple view inline elements like `` or `` do not have any attributes. Some of them have just one, for instance `` has its `href`. + +But it is easy to come up with features that style a part of a text in a more complex way. An example would be a {@link features/font Font family feature}. When used, it adds the `fontFamily` attribute to a text in the model, which is later converted to a `` element with a corresponding `style` attribute. + +So what would happen if several attributes are set on the same part of a text? Take this model example where `fontSize` is used next to `fontFamily`: + +``` + + <$text fontFamily="Tahoma" fontSize="big">foo + +``` + +Editor features are implemented in a granular way, which means that e.g. the font size converter is completely independent from the font family converter. This means that the above converts as follows: + +* `fontFamily="value"` converts to ``, +* `fontSize="value"` converts to ``. + +and, in theory, we could expect the following HTML as a result: + +```html +

+ + foo + +

+``` + +But this is not the most optimal output we can get from the editor. Why not have just one `` element instead? + +```html +

+ foo +

+``` + +Obviously a single `` makes more sense. And thanks to the merging mechanism built in the conversion process, this would be the actual result of the conversion. + +Why is it so? In the above scenario, two model attributes are converted to `` elements. When the first attribute (say, `fontFamily`) is converted, there is no `` in the view yet. So the first `` is added with the `style` attribute. But then, when `fontSize` is converted, the `` is already in the view. The {@link module:engine/view/downcastwriter~DowncastWriter downcast writer} recognizes it and checks whether those elements can be merged, following these 3 rules: + +1. both elements must have the same {@link module:engine/view/element~Element#name name}, +2. both elements must have the same {@link module:engine/view/attributeelement~AttributeElement#priority priority}, +3. neither can have an {@link module:engine/view/attributeelement~AttributeElement#id id}. + +## Examples + +Once you understand more about the conversion of model attributes, you can check some examples of: + +* {@link framework/guides/deep-dive/conversion-extending-output How to extend the editor output} — extending the output of existing CKEditor 5 features. +* {@link framework/guides/deep-dive/conversion-preserving-custom-content How to extend the editor with custom content} — how to make CKEditor 5 accept more content. diff --git a/docs/framework/guides/deep-dive/conversion-preserving-custom-content.md b/docs/framework/guides/deep-dive/conversion-preserving-custom-content.md new file mode 100644 index 000000000..06c045d27 --- /dev/null +++ b/docs/framework/guides/deep-dive/conversion-preserving-custom-content.md @@ -0,0 +1,449 @@ +--- +category: framework-deep-dive-conversion +menu-title: Preserving custom content +order: 30 +--- + +{@snippet framework/build-extending-content-source} + +# Preserving custom content + +In the {@link framework/guides/deep-dive/conversion-extending-output previous guide} we focused on post–processing of the editor data output. In this one, we will also extend the editor model so custom data can be loaded into it ({@link framework/guides/architecture/editing-engine#data-pipeline "upcasted"}). This will allow you not only to "correct" the editor output but, for instance, losslessly load data unsupported by editor features. + +Eventually, this knowledge will allow you to create your custom features on top of the core features of CKEditor 5. + +## Before starting + +### Code architecture + +It is recommended that the code that customizes editor data and editing pipelines is delivered as {@link framework/guides/architecture/core-editor-architecture#plugins plugins} and all examples in this chapter follow this convention. + +Also for the sake of simplicity all examples use the same {@link module:editor-classic/classiceditor~ClassicEditor `ClassicEditor`} but keep in mind that code snippets will work with other editors too. + +Finally, none of the converters covered in this guide require to import any module from CKEditor 5 Framework, hence, you can write them without rebuilding the editor. In other words, such converters can easily be added to existing CKEditor 5 builds. + +### CKEditor 5 inspector + +{@link framework/guides/development-tools#ckeditor-5-inspector CKEditor 5 inspector} is an invaluable help with working with the model and view structures. It allows browsing their structure and checking selection positions like in typical browser dev tools. Make sure to enable CKEditor 5 inspector when playing with CKEditor 5. + +## Loading content with a custom attribute + +In this example links (`
...`) loaded in editor content will preserve their `target` attribute, which is not supported by the {@link features/link Link} feature. The DOM `target` attribute will be stored in the editor model as a `linkTarget` attribute. + +Unlike the {@link framework/guides/deep-dive/conversion-extending-output#adding-an-html-attribute-to-certain-inline-elements downcast–only solution}, this approach does not change the content loaded into the editor. Links without the `target` attribute will not get one and links with the attribute will preserve its value. + + + Note that the same behavior can be obtained with {@link features/link#custom-link-attributes-decorators link decorators}: + + ```js + ClassicEditor + .create( ..., { + // ... + link: { + decorators: { + addGreenLink: { + mode: 'automatic', + attributes: { + class: 'my-green-link' + } + } + } + } + } ) + ``` + + +{@snippet framework/extending-content-allow-link-target} + +Allowing the `target` attribute in the editor is made by two custom converters plugged into the downcast and "upcast" pipelines, following the default converters brought by the {@link features/link Link} feature: + +```js +function AllowLinkTarget( editor ) { + // Allow the "linkTarget" attribute in the editor model. + editor.model.schema.extend( '$text', { allowAttributes: 'linkTarget' } ); + + // Tell the editor that the model "linkTarget" attribute converts into + editor.conversion.for( 'downcast' ).attributeToElement( { + model: 'linkTarget', + view: ( attributeValue, writer ) => { + const linkElement = writer.createAttributeElement( 'a', { target: attributeValue }, { priority: 5 } ); + writer.setCustomProperty( 'link', true, linkElement ); + + return linkElement; + }, + converterPriority: 'low' + } ); + + // Tell the editor that converts into "linkTarget" attribute in the model. + editor.conversion.for( 'upcast' ).attributeToAttribute( { + view: { + name: 'a', + key: 'target' + }, + model: 'linkTarget', + converterPriority: 'low' + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + extraPlugins: [ AllowLinkTarget ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` + +Add some CSS styles to easily see different link targets: + +```css +a[target]::after { + content: "target=\"" attr(target) "\""; + font-size: 0.6em; + position: relative; + left: 0em; + top: -1em; + background: #00ffa6; + color: #000; + padding: 1px 3px; + border-radius: 10px; +} +``` + +## Loading content with all attributes + +In this example divs (`
...
`) loaded in editor content will preserve their attributes. All the DOM attributes will be stored in the editor model as corresponding attributes. + +{@snippet framework/extending-content-allow-div-attributes} + +Allowing all attributes on `div` elements is achieved by custom "upcast" and "downcast" converters that copies each attribute one by one. + +Allowing every possible attribute on div in the model is done by adding a {@link module:engine/model/schema~Schema#addAttributeCheck addAttributeCheck()} callback. + + + Allowing every attribute on `
` elements might introduce security issues - ise XSS attacks. The production code should use only application related attributes and/or properly encode data. + + +Adding "upcast" and "downcast" converters for the `
` element is enough for cases where its attributes does not change. If attributes in the model are modified those `elementToElement()` converters will not be called as `div` is already converted. To overcome this a lower-level API is used. + +Instead of using predefined converters the {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event-attribute `attribute`} event listener is registered for "downcast" dispatcher. + +```js +function ConvertDivAttributes( editor ) { + // Allow divs in the model. + editor.model.schema.register( 'div', { + allowWhere: '$block', + allowContentOf: '$root' + } ); + + // Allow divs in the model to have all attributes. + editor.model.schema.addAttributeCheck( context => { + if ( context.endsWith( 'div' ) ) { + return true; + } + } ); + + // View-to-model converter converting a view div with all its attributes to the model. + editor.conversion.for( 'upcast' ).elementToElement( { + view: 'div', + model: ( viewElement, modelWriter ) => { + return modelWriter.createElement( 'div', viewElement.getAttributes() ); + } + } ); + + // Model-to-view convert for the div element (attrbiutes are converted separately). + editor.conversion.for( 'downcast' ).elementToElement( { + model: 'div', + view: 'div' + } ); + + // Model-to-view converter for div attributes. + // Note that we use a lower-level, event-based API here. + editor.conversion.for( 'downcast' ).add( dispatcher => { + dispatcher.on( 'attribute', ( evt, data, conversionApi ) => { + // Convert div attributes only. + if ( data.item.name != 'div' ) { + return; + } + + const viewWriter = conversionApi.writer; + const viewDiv = 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, viewDiv ); + } else { + viewWriter.removeAttribute( data.attributeKey, viewDiv ); + } + } ); + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + extraPlugins: [ ConvertDivAttributes ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` + +## Parse attribute values + +Some features, like {@link features/font Font}, allows only specific values for inline attributes. In this example we'll add a converter that will parse any `font-size` value into one of defined values. + +{@snippet framework/extending-content-arbitrary-attribute-values} + +Parsing any font value to model requires writing adding custom "upcast" converter that will override default converter from `FontSize`. Unlike the default one, this converter parses values set in CSS nad sets them into the model. + +As the default "downcast" converter only operates on pre-defined values we're also adding a model-to-view converter that simply outputs any model value to font-size using `px` units. + +```js +function HandleFontSizeValue( editor ) { + // Add special catch-all converter for font-size feature. + editor.conversion.for( 'upcast' ).elementToAttribute( { + view: { + name: 'span', + styles: { + 'font-size': /[\s\S]+/ + } + }, + model: { + key: 'fontSize', + value: viewElement => { + const value = parseFloat( viewElement.getStyle( 'font-size' ) ).toFixed( 0 ); + + // It might be needed to further convert the value to meet business requirements. + // In the sample the font-size is configured to handle only the sizes: + // 12, 14, 'default', 18, 20, 22, 24, 26, 28, 30 + // Other sizes will be converted to the model but the UI might not be aware of them. + + // The font-size feature expects numeric values to be Number not String. + return parseInt( value ); + } + }, + converterPriority: 'high' + } ); + + // Add special converter for font-size feature to convert all (even not configured) + // model attribute values. + editor.conversion.for( 'downcast' ).attributeToElement( { + model: { + key: 'fontSize' + }, + view: ( modelValue, viewWriter ) => { + return viewWriter.createAttributeElement( 'span', { + style: `font-size:${ modelValue }px` + } ); + }, + converterPriority: 'high' + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + items: [ 'heading', '|', 'bold', 'italic', '|', 'fontSize' ], + fontSize: { + options: [ 10, 12, 14, 'default', 18, 20, 22 ] + }, + extraPlugins: [ HandleFontSizeValue ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` + +## Adding extra attributes to elements contained in a figure + +The {@link features/image Image} and {@link features/table Table} features wraps view elements (`` for Image nad `
` for Table) in `
`. During the downcast conversion the model element is mapped to `
` not the inner element. In such cases the default `conversion.attributeToAttribute()` conversion helpers could lost information on which element the attribute should be set. To overcome this limitation it is sufficient to write a custom converter that add custom attributes to elements already converted by base features. The key point is to add those converters with lower priority the base converters so they will be called after the base ones. + +{@snippet framework/extending-content-custom-figure-attributes} + +The sample below is extensible - to add own attributes to preserve just add another `setupCustomAttributeConversion()` call with desired names. + +```js +/** + * Plugin that converts custom attributes for elements that are wrapped in
in the view. + */ +function CustomFigureAttributes( editor ) { + // Define on which elements the css classes should be preserved: + setupCustomClassConversion( 'img', 'image', editor ); + setupCustomClassConversion( 'table', 'table', editor ); + + editor.conversion.for( 'upcast' ).add( upcastCustomClasses( 'figure' ), { priority: 'low' } ); + + // Define custom attributes that should be preserved. + setupCustomAttributeConversion( 'img', 'image', 'id', editor ); + setupCustomAttributeConversion( 'table', 'table', 'id', editor ); +} + +/** + * Setups conversion that preservers classes on img/table elements + */ +function setupCustomClassConversion( viewElementName, modelElementName, editor ) { + // The 'customClass' attribute will store custom classes from data in the model so schema definitions to allow this attribute. + editor.model.schema.extend( modelElementName, { allowAttributes: [ 'customClass' ] } ); + + // Define upcast converters for and
elements with "low" priority so they are run after default converters. + editor.conversion.for( 'upcast' ).add( upcastCustomClasses( viewElementName ), { priority: 'low' } ); + + // Define downcast converters for model element with "low" priority so they are run after default converters. + editor.conversion.for( 'downcast' ).add( downcastCustomClasses( modelElementName ), { priority: 'low' } ); +} + +/** + * Setups conversion for custom attribute on view elements contained inside figure. + * + * This method: + * + * - adds proper schema rules + * - adds an upcast converter + * - adds a downcast converter + */ +function setupCustomAttributeConversion( viewElementName, modelElementName, viewAttribute, editor ) { + // Extend schema to store attribute in the model. + const modelAttribute = `custom${ viewAttribute }`; + + editor.model.schema.extend( modelElementName, { allowAttributes: [ modelAttribute ] } ); + + editor.conversion.for( 'upcast' ).add( upcastAttribute( viewElementName, viewAttribute, modelAttribute ) ); + editor.conversion.for( 'downcast' ).add( downcastAttribute( modelElementName, viewElementName, viewAttribute, modelAttribute ) ); +} + +/** + * Creates upcast converter that will pass all classes from view element to model element. + */ +function upcastCustomClasses( elementName ) { + return dispatcher => dispatcher.on( `element:${ elementName }`, ( evt, data, conversionApi ) => { + const viewItem = data.viewItem; + const modelRange = data.modelRange; + + const modelElement = modelRange && modelRange.start.nodeAfter; + + if ( !modelElement ) { + return; + } + + // The upcast conversion pick up classes from base element and from figure element also so it should be extensible. + const currentAttributeValue = modelElement.getAttribute( 'customClass' ) || []; + + currentAttributeValue.push( ...viewItem.getClassNames() ); + + conversionApi.writer.setAttribute( 'customClass', currentAttributeValue, modelElement ); + } ); +} + +/** + * Creates downcast converter that add classes defined in `customClass` attribute to given view element. + * + * This converter expects that view element is nested in figure element. + */ +function downcastCustomClasses( modelElementName ) { + return dispatcher => dispatcher.on( `insert:${ modelElementName }`, ( evt, data, conversionApi ) => { + const modelElement = data.item; + + const viewFigure = conversionApi.mapper.toViewElement( modelElement ); + + if ( !viewFigure ) { + return; + } + + // The below code assumes that classes are set on
element... + conversionApi.writer.addClass( modelElement.getAttribute( 'customClass' ), viewFigure ); + + // ... but if you preferIf the classes should be passed to the find the view element inside figure: + // + // const viewElement = findViewChild( viewFigure, viewElementName, conversionApi ); + // + // conversionApi.writer.addClass( modelElement.getAttribute( 'customClass' ), viewElement ); + } ); +} + +/** + * Helper method that search for given view element in all children of model element. + * + * @param {module:engine/view/item~Item} viewElement + * @param {String} viewElementName + * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi + * @return {module:engine/view/item~Item} + */ +function findViewChild( viewElement, viewElementName, conversionApi ) { + const viewChildren = Array.from( conversionApi.writer.createRangeIn( viewElement ).getItems() ); + + return viewChildren.find( item => item.is( viewElementName ) ); +} + +/** + * Returns custom attribute upcast converter. + */ +function upcastAttribute( viewElementName, viewAttribute, modelAttribute ) { + return dispatcher => dispatcher.on( `element:${ viewElementName }`, ( evt, data, conversionApi ) => { + const viewItem = data.viewItem; + const modelRange = data.modelRange; + + const modelElement = modelRange && modelRange.start.nodeAfter; + + if ( !modelElement ) { + return; + } + + conversionApi.writer.setAttribute( modelAttribute, viewItem.getAttribute( viewAttribute ), modelElement ); + } ); +} + +/** + * Returns custom attribute downcast converter. + */ +function downcastAttribute( modelElementName, viewElementName, viewAttribute, modelAttribute ) { + return dispatcher => dispatcher.on( `insert:${ modelElementName }`, ( evt, data, conversionApi ) => { + const modelElement = data.item; + + const viewFigure = conversionApi.mapper.toViewElement( modelElement ); + const viewElement = findViewChild( viewFigure, viewElementName, conversionApi ); + + if ( !viewElement ) { + return; + } + + conversionApi.writer.setAttribute( viewAttribute, modelElement.getAttribute( modelAttribute ), viewElement ); + } ); +} +``` + +Activate the plugin in the editor: + +```js +ClassicEditor + .create( ..., { + extraPlugins: [ CustomFigureAttributes ], + } ) + .then( editor => { + // ... + } ) + .catch( err => { + console.error( err.stack ); + } ); +``` diff --git a/src/conversion/upcasthelpers.js b/src/conversion/upcasthelpers.js index a01022325..719bd7053 100644 --- a/src/conversion/upcasthelpers.js +++ b/src/conversion/upcasthelpers.js @@ -405,7 +405,7 @@ function upcastElementToElement( config ) { const converter = prepareToElementConverter( config ); - const elementName = getViewElementNameFromConfig( config ); + const elementName = getViewElementNameFromConfig( config.view ); const eventName = elementName ? 'element:' + elementName : 'element'; return dispatcher => { @@ -431,7 +431,7 @@ function upcastElementToAttribute( config ) { const converter = prepareToAttributeConverter( config, false ); - const elementName = getViewElementNameFromConfig( config ); + const elementName = getViewElementNameFromConfig( config.view ); const eventName = elementName ? 'element:' + elementName : 'element'; return dispatcher => { @@ -493,15 +493,15 @@ function upcastElementToMarker( config ) { // Helper function for from-view-element conversion. Checks if `config.view` directly specifies converted view element's name // and if so, returns it. // -// @param {Object} config Conversion config. +// @param {Object} config Conversion view config. // @returns {String|null} View element name or `null` if name is not directly set. -function getViewElementNameFromConfig( config ) { - if ( typeof config.view == 'string' ) { - return config.view; +function getViewElementNameFromConfig( viewConfig ) { + if ( typeof viewConfig == 'string' ) { + return viewConfig; } - if ( typeof config.view == 'object' && typeof config.view.name == 'string' ) { - return config.view.name; + if ( typeof viewConfig == 'object' && typeof viewConfig.name == 'string' ) { + return viewConfig.name; } return null; @@ -684,7 +684,7 @@ function prepareToAttributeConverter( config, shallow ) { return; } - if ( onlyViewNameIsDefined( config ) ) { + if ( onlyViewNameIsDefined( config.view, data.viewItem ) ) { match.match.name = true; } else { // Do not test or consume `name` consumable. @@ -714,14 +714,17 @@ function prepareToAttributeConverter( config, shallow ) { // Helper function that checks if element name should be consumed in attribute converters. // -// @param {Object} config Conversion config. +// @param {Object} config Conversion view config. // @returns {Boolean} -function onlyViewNameIsDefined( config ) { - if ( typeof config.view == 'object' && !getViewElementNameFromConfig( config ) ) { +function onlyViewNameIsDefined( viewConfig, viewItem ) { + // https://github.com/ckeditor/ckeditor5-engine/issues/1786 + const configToTest = typeof viewConfig == 'function' ? viewConfig( viewItem ) : viewConfig; + + if ( typeof configToTest == 'object' && !getViewElementNameFromConfig( configToTest ) ) { return false; } - return !config.view.classes && !config.view.attributes && !config.view.styles; + return !configToTest.classes && !configToTest.attributes && !configToTest.styles; } // Helper function for to-model-attribute converter. Sets model attribute on given range. Checks {@link module:engine/model/schema~Schema} diff --git a/src/model/documentselection.js b/src/model/documentselection.js index c40412b16..01daeaa06 100644 --- a/src/model/documentselection.js +++ b/src/model/documentselection.js @@ -233,41 +233,26 @@ export default class DocumentSelection { } /** - * Gets elements of type "block" touched by the selection. + * Gets elements of type {@link module:engine/model/schema~Schema#isBlock "block"} touched by the selection. * * This method's result can be used for example to apply block styling to all blocks covered by this selection. * - * **Note:** `getSelectedBlocks()` always returns the deepest block. + * **Note:** `getSelectedBlocks()` returns blocks that are nested in other non-block elements + * but will not return blocks nested in other blocks. * - * In this case the function will return exactly all 3 paragraphs: + * In this case the function will return exactly all 3 paragraphs (note: `
` is not a block itself): * * [a - * + *
* b - * + *
* c]d * * In this case the paragraph will also be returned, despite the collapsed selection: * * []a * - * **Special case**: If a selection ends at the beginning of a block, that block is not returned as from user perspective - * this block wasn't selected. See [#984](https://github.com/ckeditor/ckeditor5-engine/issues/984) for more details. - * - * [a - * b - * ]c // this block will not be returned - * - * @returns {Iterable.} - */ - getSelectedBlocks() { - return this._selection.getSelectedBlocks(); - } - - /** - * Returns blocks that aren't nested in other selected blocks. - * - * In this case the method will return blocks A, B and E because C & D are children of block B: + * In such a scenario, however, only blocks A, B & E will be returned as blocks C & D are nested in block B: * * [ * @@ -276,12 +261,24 @@ export default class DocumentSelection { * * ] * - * **Note:** To get all selected blocks use {@link #getSelectedBlocks `getSelectedBlocks()`}. + * If the selection is inside a block all the inner blocks (A & B) are returned: + * + * + * [a + * b] + * + * + * **Special case**: If a selection ends at the beginning of a block, that block is not returned as from user perspective + * this block wasn't selected. See [#984](https://github.com/ckeditor/ckeditor5-engine/issues/984) for more details. + * + * [a + * b + * ]c // this block will not be returned * * @returns {Iterable.} */ - getTopMostBlocks() { - return this._selection.getTopMostBlocks(); + getSelectedBlocks() { + return this._selection.getSelectedBlocks(); } /** diff --git a/src/model/selection.js b/src/model/selection.js index 2b14b49a5..11ce80365 100644 --- a/src/model/selection.js +++ b/src/model/selection.js @@ -641,24 +641,41 @@ export default class Selection { } /** - * Gets elements of type "block" touched by the selection. + * Gets elements of type {@link module:engine/model/schema~Schema#isBlock "block"} touched by the selection. * * This method's result can be used for example to apply block styling to all blocks covered by this selection. * - * **Note:** `getSelectedBlocks()` always returns the deepest block. + * **Note:** `getSelectedBlocks()` returns blocks that are nested in other non-block elements + * but will not return blocks nested in other blocks. * - * In this case the function will return exactly all 3 paragraphs: + * In this case the function will return exactly all 3 paragraphs (note: `
` is not a block itself): * * [a - * + *
* b - * + *
* c]d * * In this case the paragraph will also be returned, despite the collapsed selection: * * []a * + * In such a scenario, however, only blocks A, B & E will be returned as blocks C & D are nested in block B: + * + * [ + * + * + * + * + * ] + * + * If the selection is inside a block all the inner blocks (A & B) are returned: + * + * + * [a + * b] + * + * * **Special case**: If a selection ends at the beginning of a block, that block is not returned as from user perspective * this block wasn't selected. See [#984](https://github.com/ckeditor/ckeditor5-engine/issues/984) for more details. * @@ -672,56 +689,30 @@ export default class Selection { const visited = new WeakSet(); for ( const range of this.getRanges() ) { + // Get start block of range in case of a collapsed range. const startBlock = getParentBlock( range.start, visited ); - if ( startBlock ) { + if ( startBlock && isTopBlockInRange( startBlock, range ) ) { yield startBlock; } for ( const value of range.getWalker() ) { - if ( value.type == 'elementEnd' && isUnvisitedBlockContainer( value.item, visited ) ) { - yield value.item; + const block = value.item; + + if ( value.type == 'elementEnd' && isUnvisitedTopBlock( block, visited, range ) ) { + yield block; } } const endBlock = getParentBlock( range.end, visited ); // #984. Don't return the end block if the range ends right at its beginning. - if ( endBlock && !range.end.isTouching( Position._createAt( endBlock, 0 ) ) ) { + if ( endBlock && !range.end.isTouching( Position._createAt( endBlock, 0 ) ) && isTopBlockInRange( endBlock, range ) ) { yield endBlock; } } } - /** - * Returns blocks that aren't nested in other selected blocks. - * - * In this case the method will return blocks A, B and E because C & D are children of block B: - * - * [ - * - * - * - * - * ] - * - * **Note:** To get all selected blocks use {@link #getSelectedBlocks `getSelectedBlocks()`}. - * - * @returns {Iterable.} - */ - * getTopMostBlocks() { - const selected = Array.from( this.getSelectedBlocks() ); - - for ( const block of selected ) { - const parentBlock = findAncestorBlock( block ); - - // Filter out blocks that are nested in other selected blocks (like paragraphs in tables). - if ( !parentBlock || !selected.includes( parentBlock ) ) { - yield block; - } - } - } - /** * Checks whether the selection contains the entire content of the given element. This means that selection must start * at a position {@link module:engine/model/position~Position#isTouching touching} the element's start and ends at position @@ -831,7 +822,7 @@ mix( Selection, EmitterMixin ); // Checks whether the given element extends $block in the schema and has a parent (is not a root). // Marks it as already visited. -function isUnvisitedBlockContainer( element, visited ) { +function isUnvisitedBlock( element, visited ) { if ( visited.has( element ) ) { return false; } @@ -841,6 +832,11 @@ function isUnvisitedBlockContainer( element, visited ) { return element.document.model.schema.isBlock( element ) && element.parent; } +// Checks if the given element is a $block was not previously visited and is a top block in a range. +function isUnvisitedTopBlock( element, visited, range ) { + return isUnvisitedBlock( element, visited ) && isTopBlockInRange( element, range ); +} + // Finds the lowest element in position's ancestors which is a block. // It will search until first ancestor that is a limit element. // Marks all ancestors as already visited to not include any of them later on. @@ -859,7 +855,7 @@ function getParentBlock( position, visited ) { hasParentLimit = schema.isLimit( element ); - return !hasParentLimit && isUnvisitedBlockContainer( element, visited ); + return !hasParentLimit && isUnvisitedBlock( element, visited ); } ); // Mark all ancestors of this position's parent, because find() might've stopped early and @@ -869,6 +865,23 @@ function getParentBlock( position, visited ) { return block; } +// Checks if the blocks is not nested in other block inside a range. +// +// @param {module:engine/model/elmenent~Element} block Block to check. +// @param {module:engine/model/range~Range} range Range to check. +function isTopBlockInRange( block, range ) { + const parentBlock = findAncestorBlock( block ); + + if ( !parentBlock ) { + return true; + } + + // Add loose flag to check as parentRange can be equal to range. + const isParentInRange = range.containsRange( Range._createOn( parentBlock ), true ); + + return !isParentInRange; +} + // Returns first ancestor block of a node. // // @param {module:engine/model/node~Node} node diff --git a/src/view/downcastwriter.js b/src/view/downcastwriter.js index a930e2dda..7561e12df 100644 --- a/src/view/downcastwriter.js +++ b/src/view/downcastwriter.js @@ -788,26 +788,26 @@ export default class DowncastWriter { } /** - * Wraps elements within range with provided {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. - * If a collapsed range is provided, it will be wrapped only if it is equal to view selection. - * - * If a collapsed range was passed and is same as selection, the selection - * will be moved to the inside of the wrapped attribute element. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-invalid-range-container` - * when {@link module:engine/view/range~Range#start} - * and {@link module:engine/view/range~Range#end} positions are not placed inside same parent container. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-invalid-attribute` when passed attribute element is not - * an instance of {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-nonselection-collapsed-range` when passed range - * is collapsed and different than view selection. - * - * @param {module:engine/view/range~Range} range Range to wrap. - * @param {module:engine/view/attributeelement~AttributeElement} attribute Attribute element to use as wrapper. - * @returns {module:engine/view/range~Range} range Range after wrapping, spanning over wrapping attribute element. - */ + * Wraps elements within range with provided {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. + * If a collapsed range is provided, it will be wrapped only if it is equal to view selection. + * + * If a collapsed range was passed and is same as selection, the selection + * will be moved to the inside of the wrapped attribute element. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-invalid-range-container` + * when {@link module:engine/view/range~Range#start} + * and {@link module:engine/view/range~Range#end} positions are not placed inside same parent container. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-invalid-attribute` when passed attribute element is not + * an instance of {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-nonselection-collapsed-range` when passed range + * is collapsed and different than view selection. + * + * @param {module:engine/view/range~Range} range Range to wrap. + * @param {module:engine/view/attributeelement~AttributeElement} attribute Attribute element to use as wrapper. + * @returns {module:engine/view/range~Range} range Range after wrapping, spanning over wrapping attribute element. + */ wrap( range, attribute ) { if ( !( attribute instanceof AttributeElement ) ) { throw new CKEditorError( 'view-writer-wrap-invalid-attribute', this.document ); diff --git a/src/view/renderer.js b/src/view/renderer.js index c8bc5cc2e..8606b8b0b 100644 --- a/src/view/renderer.js +++ b/src/view/renderer.js @@ -101,6 +101,13 @@ export default class Renderer { */ this.isFocused = false; + /** + * Indicates if the composition is in progress inside the view document view. + * + * @member {Boolean} + */ + this.isComposing = false; + /** * The text node in which the inline filler was rendered. * @@ -771,6 +778,11 @@ export default class Renderer { * @returns {Boolean} */ _domSelectionNeedsUpdate( domSelection ) { + // Remain DOM selection untouched while composing. See #1782. + if ( this.isComposing ) { + return false; + } + if ( !this.domConverter.isDomSelectionCorrect( domSelection ) ) { // Current DOM selection is in incorrect position. We need to update it. return true; diff --git a/src/view/view.js b/src/view/view.js index 6101f5c41..f7f999b34 100644 --- a/src/view/view.js +++ b/src/view/view.js @@ -105,6 +105,7 @@ export default class View { */ this._renderer = new Renderer( this.domConverter, this.document.selection ); this._renderer.bind( 'isFocused' ).to( this.document ); + this._renderer.bind( 'isComposing' ).to( this.document ); /** * A DOM root attributes cache. It saves the initial values of DOM root attributes before the DOM element diff --git a/tests/conversion/conversion.js b/tests/conversion/conversion.js index e17ce29b2..659e0ae94 100644 --- a/tests/conversion/conversion.js +++ b/tests/conversion/conversion.js @@ -301,6 +301,14 @@ describe( 'Conversion', () => { } ); it( 'config.view is an object with upcastAlso defined', () => { + schema.extend( '$text', { + allowAttributes: [ 'bold', 'xBold' ] + } ); + conversion.attributeToElement( { + model: 'xBold', + view: 'x-bold' + } ); + conversion.attributeToElement( { model: 'bold', view: 'strong', @@ -310,22 +318,18 @@ describe( 'Conversion', () => { name: 'span', classes: 'bold' }, - { - name: 'span', - styles: { - 'font-weight': 'bold' - } - }, viewElement => { const fontWeight = viewElement.getStyle( 'font-weight' ); - if ( viewElement.is( 'span' ) && fontWeight && /\d+/.test( fontWeight ) && Number( fontWeight ) > 500 ) { + if ( fontWeight == 'bold' || Number( fontWeight ) > 500 ) { return { - name: true, styles: [ 'font-weight' ] }; } - } + }, + // Duplicates the `x-bold` from above to test if only one attribute would be converted. + // It should not convert to both bold & x-bold. + viewElement => viewElement.is( 'x-bold' ) ? { name: 'x-bold' } : null ] } ); @@ -363,6 +367,18 @@ describe( 'Conversion', () => { '<$text bold="true">Foo', '

Foo

' ); + + test( + '

Foo

', + '<$text bold="true">Foo', + '

Foo

' + ); + + test( + '

Foo

', + '<$text xBold="true">Foo', + '

Foo

' + ); } ); it( 'model attribute value is enumerable', () => { diff --git a/tests/model/selection.js b/tests/model/selection.js index 5897da938..061b1bf54 100644 --- a/tests/model/selection.js +++ b/tests/model/selection.js @@ -970,6 +970,12 @@ describe( 'Selection', () => { // Special block which can contain another blocks. model.schema.register( 'nestedBlock', { inheritAllFrom: '$block' } ); model.schema.extend( 'nestedBlock', { allowIn: '$block' } ); + + model.schema.register( 'table', { isBlock: true, isLimit: true, isObject: true, allowIn: '$root' } ); + model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } ); + model.schema.register( 'tableCell', { allowIn: 'tableRow', isObject: true } ); + + model.schema.extend( 'p', { allowIn: 'tableCell' } ); } ); it( 'returns an iterator', () => { @@ -981,7 +987,7 @@ describe( 'Selection', () => { it( 'returns block for a collapsed selection', () => { setData( model, '

a

[]b

c

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#b' ] ); } ); it( 'returns block for a collapsed selection (empty block)', () => { @@ -996,86 +1002,102 @@ describe( 'Selection', () => { it( 'returns block for a non collapsed selection', () => { setData( model, '

a

[b]

c

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#b' ] ); } ); it( 'returns two blocks for a non collapsed selection', () => { setData( model, '

a

[b

c]

d

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b', 'p#c' ] ); } ); it( 'returns two blocks for a non collapsed selection (starts at block end)', () => { setData( model, '

a

b[

c]

d

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b', 'p#c' ] ); } ); it( 'returns proper block for a multi-range selection', () => { setData( model, '

a

[b

c]

d

[e]

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b', 'p#c', 'p#e' ] ); } ); it( 'does not return a block twice if two ranges are anchored in it', () => { setData( model, '

[a]b[c]

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#abc' ] ); } ); it( 'returns only blocks', () => { setData( model, '

[a

b

c]

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#c' ] ); } ); it( 'gets deeper into the tree', () => { setData( model, '

[a

b

c

d]

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ) + .to.deep.equal( [ 'p#a', 'p#b', 'p#c', 'p#d' ] ); } ); it( 'gets deeper into the tree (end deeper)', () => { setData( model, '

[a

b]

c

d

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ) + .to.deep.equal( [ 'p#a', 'p#b' ] ); } ); it( 'gets deeper into the tree (start deeper)', () => { setData( model, '

a

b

[c

d]

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'c', 'd' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ) + .to.deep.equal( [ 'p#c', 'p#d' ] ); } ); it( 'returns an empty array if none of the selected elements is a block', () => { setData( model, '

a

[ab]

b

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty; + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.be.empty; } ); it( 'returns an empty array if the selected element is not a block', () => { setData( model, '

a

[]a

b

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty; + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.be.empty; } ); // Super edge case – should not happen (blocks should never be nested), // but since the code handles it already it's worth testing. - it( 'returns only the lowest block if blocks are nested', () => { + it( 'returns only the lowest block if blocks are nested (case #1)', () => { setData( model, 'a[]b' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'nestedBlock#b' ] ); } ); - // Like above but trickier. - it( 'returns only the lowest block if blocks are nested', () => { + // Like above but - with multiple ranges. + it( 'returns only the lowest block if blocks are nested (case #2)', () => { setData( model, 'a[b' + 'cd]' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ) + .to.deep.equal( [ 'nestedBlock#b', 'nestedBlock#d' ] ); + } ); + + // Like above but - with multiple collapsed ranges. + it( 'returns only the lowest block if blocks are nested (case #3)', () => { + setData( + model, + 'a[]b' + + 'cd[]' + ); + + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ) + .to.deep.equal( [ 'nestedBlock#b', 'nestedBlock#d' ] ); } ); it( 'returns nothing if directly in a root', () => { @@ -1083,26 +1105,60 @@ describe( 'Selection', () => { setData( model, 'a[b]c', { rootName: 'inlineOnlyRoot' } ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty; + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.be.empty; + } ); + + it( 'does not go cross limit elements', () => { + model.schema.register( 'blk', { allowIn: [ '$root', 'tableCell' ], isObject: true, isBlock: true } ); + + setData( model, '

foo

[

bar]

' ); + + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'blk', 'p#bar' ] ); + } ); + + it( 'returns only top most blocks (multiple selected)', () => { + setData( model, '

[foo

bar

baz]

' ); + + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#foo', 'table', 'p#baz' ] ); + } ); + + it( 'returns only top most block (one selected)', () => { + setData( model, '[

bar

]' ); + + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'table' ] ); + } ); + + it( 'returns only selected blocks even if nested in other blocks', () => { + setData( model, '

foo

[b]ar

baz

' ); + + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#bar' ] ); + } ); + + it( 'returns only selected blocks even if nested in other blocks (selection on the block)', () => { + model.schema.register( 'blk', { allowIn: [ '$root', 'tableCell' ], isObject: true, isBlock: true } ); + + setData( model, '

foo

[

bar]

' ); + + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'blk', 'p#bar' ] ); } ); describe( '#984', () => { it( 'does not return the last block if none of its content is selected', () => { setData( model, '

[a

b

]c

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b' ] ); } ); it( 'returns only the first block for a non collapsed selection if only end of selection is touching a block', () => { setData( model, '

a

b[

]c

d

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b' ] ); } ); it( 'does not return the last block if none of its content is selected (nested case)', () => { setData( model, '

[a

]b' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a' ] ); } ); // Like a super edge case, we can live with this behavior as I don't even know what we could expect here @@ -1110,20 +1166,20 @@ describe( 'Selection', () => { it( 'does not return the last block if none of its content is selected (nested case, wrapper with a content)', () => { setData( model, '

[a

b]c' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a' ] ); } ); it( 'returns the last block if at least one of its child nodes is selected', () => { setData( model, '

[a

b

]c

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b', 'p#c' ] ); } ); // I needed these last 2 cases to justify the use of isTouching() instead of simple `offset == 0` check. it( 'returns the last block if at least one of its child nodes is selected (end in an inline element)', () => { setData( model, '

[a

b

x]c

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b', 'p#c' ] ); } ); it( @@ -1132,95 +1188,10 @@ describe( 'Selection', () => { () => { setData( model, '

[a

b

]xc

' ); - expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] ); + expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b' ] ); } ); } ); - - it( 'does not go beyond limit elements', () => { - model.schema.register( 'table', { isBlock: true, isLimit: true, isObject: true, allowIn: '$root' } ); - model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } ); - model.schema.register( 'tableCell', { allowIn: 'tableRow', isObject: true } ); - - model.schema.register( 'blk', { allowIn: [ '$root', 'tableCell' ], isObject: true, isBlock: true } ); - - model.schema.extend( 'p', { allowIn: 'tableCell' } ); - - setData( model, '

foo

[

bar]

' ); - - expect( stringifyBlocks( doc.selection.getTopMostBlocks() ) ).to.deep.equal( [ 'blk', 'p#bar' ] ); - } ); - - // Map all elements to data of its first child text node. - function toText( elements ) { - return Array.from( elements ).map( el => { - return Array.from( el.getChildren() ).find( child => child.data ).data; - } ); - } - } ); - - describe( 'getTopMostBlocks()', () => { - beforeEach( () => { - model.schema.register( 'p', { inheritAllFrom: '$block' } ); - model.schema.register( 'table', { isBlock: true, isLimit: true, isObject: true, allowIn: '$root' } ); - model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } ); - model.schema.register( 'tableCell', { allowIn: 'tableRow', isObject: true } ); - - model.schema.extend( 'p', { allowIn: 'tableCell' } ); - } ); - - it( 'returns an iterator', () => { - setData( model, '

a

[]b

c

' ); - - expect( doc.selection.getTopMostBlocks().next ).to.be.a( 'function' ); - } ); - - it( 'returns block for a collapsed selection', () => { - setData( model, '

a

[]b

c

' ); - - expect( stringifyBlocks( doc.selection.getTopMostBlocks() ) ).to.deep.equal( [ 'p#b' ] ); - } ); - - it( 'returns block for a collapsed selection (empty block)', () => { - setData( model, '

a

[]

c

' ); - - const blocks = Array.from( doc.selection.getTopMostBlocks() ); - - expect( blocks ).to.have.length( 1 ); - expect( blocks[ 0 ].childCount ).to.equal( 0 ); - } ); - - it( 'returns block for a non collapsed selection', () => { - setData( model, '

a

[b]

c

' ); - - expect( stringifyBlocks( doc.selection.getTopMostBlocks() ) ).to.deep.equal( [ 'p#b' ] ); - } ); - - it( 'returns two blocks for a non collapsed selection', () => { - setData( model, '

a

[b

c]

d

' ); - - expect( stringifyBlocks( doc.selection.getTopMostBlocks() ) ).to.deep.equal( [ 'p#b', 'p#c' ] ); - } ); - - it( 'returns only top most blocks', () => { - setData( model, '[

foo

bar

baz

]' ); - - expect( stringifyBlocks( doc.selection.getTopMostBlocks() ) ).to.deep.equal( [ 'p#foo', 'table', 'p#baz' ] ); - } ); - - it( 'returns only selected blocks even if nested in other blocks', () => { - setData( model, '

foo

[b]ar

baz

' ); - - expect( stringifyBlocks( doc.selection.getTopMostBlocks() ) ).to.deep.equal( [ 'p#bar' ] ); - } ); - - it( 'returns only selected blocks even if nested in other blocks (selection on the block)', () => { - model.schema.register( 'blk', { allowIn: [ '$root', 'tableCell' ], isObject: true, isBlock: true } ); - - setData( model, '

foo

[

bar]

' ); - - expect( stringifyBlocks( doc.selection.getTopMostBlocks() ) ).to.deep.equal( [ 'blk', 'p#bar' ] ); - } ); } ); describe( 'attributes interface', () => { @@ -1402,10 +1373,15 @@ describe( 'Selection', () => { return Array.from( elements ).map( el => { const name = el.name; - const firstChild = el.getChild( 0 ); - const hasText = firstChild && firstChild.data; + let innerText = ''; + + for ( const child of el.getChildren() ) { + if ( child.is( 'text' ) ) { + innerText += child.data; + } + } - return hasText ? `${ name }#${ firstChild.data }` : name; + return innerText.length ? `${ name }#${ innerText }` : name; } ); } } ); diff --git a/tests/view/renderer.js b/tests/view/renderer.js index 617a7fda6..238589f47 100644 --- a/tests/view/renderer.js +++ b/tests/view/renderer.js @@ -3641,6 +3641,48 @@ describe( 'Renderer', () => { return viewData.repeat( repeat ); } } ); + + // #1782 + it( 'should leave dom selection untouched while composing', () => { + const { view: viewP, selection: newSelection } = parse( '[]' ); + + viewRoot._appendChild( viewP ); + selection._setTo( newSelection ); + + renderer.markToSync( 'children', viewRoot ); + renderer.render(); + + // Mock IME typing in Safari:

[c]

. + renderer.isComposing = true; + const domText = document.createTextNode( 'c' ); + domRoot.firstChild.appendChild( domText ); + const range = document.createRange(); + range.setStart( domText, 0 ); + range.setEnd( domText, 1 ); + const domSelection = document.getSelection(); + domSelection.removeAllRanges(); + domSelection.addRange( range ); + + // c[] + viewP._appendChild( new ViewText( 'c' ) ); + selection._setTo( [ + new ViewRange( new ViewPosition( viewP.getChild( 0 ), 1 ), new ViewPosition( viewP.getChild( 0 ), 1 ) ) + ] ); + + renderer.markToSync( 'children', viewP ); + renderer.render(); + + expect( domRoot.childNodes.length ).to.equal( 1 ); + expect( domRoot.firstChild.childNodes.length ).to.equal( 1 ); + expect( domRoot.firstChild.firstChild.data ).to.equal( 'c' ); + + const currentRange = domSelection.getRangeAt( 0 ); + expect( currentRange.collapsed ).to.equal( false ); + expect( currentRange.startContainer ).to.equal( domRoot.firstChild.firstChild ); + expect( currentRange.startOffset ).to.equal( 0 ); + expect( currentRange.endContainer ).to.equal( domRoot.firstChild.firstChild ); + expect( currentRange.endOffset ).to.equal( 1 ); + } ); } ); describe( '#922', () => { diff --git a/theme/placeholder.css b/theme/placeholder.css index cd73940b8..a16a9f47b 100644 --- a/theme/placeholder.css +++ b/theme/placeholder.css @@ -4,7 +4,8 @@ */ /* See ckeditor/ckeditor5#936. */ -.ck.ck-placeholder, .ck .ck-placeholder { +.ck.ck-placeholder, +.ck .ck-placeholder { &::before { content: attr(data-placeholder); @@ -12,3 +13,10 @@ pointer-events: none; } } + +/* See ckeditor/ckeditor5#1987. */ +.ck.ck-read-only .ck-placeholder { + &::before { + display: none; + } +}