浏览代码

Introduce blockFillerMode in DomConverter.

Maciej Gołaszewski 6 年之前
父节点
当前提交
24a143c326

+ 1 - 2
packages/ckeditor5-engine/src/dataprocessor/htmldataprocessor.js

@@ -11,7 +11,6 @@
 
 import BasicHtmlWriter from './basichtmlwriter';
 import DomConverter from '../view/domconverter';
-import { NBSP_FILLER } from '../view/filler';
 
 /**
  * The HTML data processor class.
@@ -38,7 +37,7 @@ export default class HtmlDataProcessor {
 		 * @private
 		 * @member {module:engine/view/domconverter~DomConverter}
 		 */
-		this._domConverter = new DomConverter( { blockFiller: NBSP_FILLER } );
+		this._domConverter = new DomConverter( { blockFillerMode: 'nbsp' } );
 
 		/**
 		 * A basic HTML writer instance used to convert DOM elements to an HTML string.

+ 1 - 2
packages/ckeditor5-engine/src/dataprocessor/xmldataprocessor.js

@@ -11,7 +11,6 @@
 
 import BasicHtmlWriter from './basichtmlwriter';
 import DomConverter from '../view/domconverter';
-import { NBSP_FILLER } from '../view/filler';
 
 /**
  * The XML data processor class.
@@ -54,7 +53,7 @@ export default class XmlDataProcessor {
 		 * @private
 		 * @member {module:engine/view/domconverter~DomConverter}
 		 */
-		this._domConverter = new DomConverter( { blockFiller: NBSP_FILLER } );
+		this._domConverter = new DomConverter( { blockFillerMode: 'nbsp' } );
 
 		/**
 		 * A basic HTML writer instance used to convert DOM elements to an XML string.

+ 23 - 7
packages/ckeditor5-engine/src/view/domconverter.js

@@ -16,7 +16,15 @@ import ViewRange from './range';
 import ViewSelection from './selection';
 import ViewDocumentFragment from './documentfragment';
 import ViewTreeWalker from './treewalker';
-import { BR_FILLER, INLINE_FILLER_LENGTH, isBlockFiller, isInlineFiller, startsWithFiller, getDataWithoutFiller } from './filler';
+import {
+	BR_FILLER,
+	NBSP_FILLER,
+	INLINE_FILLER_LENGTH,
+	isBlockFiller,
+	isInlineFiller,
+	startsWithFiller,
+	getDataWithoutFiller
+} from './filler';
 
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 import indexOf from '@ckeditor/ckeditor5-utils/src/dom/indexof';
@@ -42,7 +50,7 @@ export default class DomConverter {
 	 * Creates DOM converter.
 	 *
 	 * @param {Object} options Object with configuration options.
-	 * @param {Function} [options.blockFiller=module:engine/view/filler~BR_FILLER] Block filler creator.
+	 * @param {Function} [options.blockFillerMode='br'] Block filler mode - either 'br' or 'nbsp'.
 	 */
 	constructor( options = {} ) {
 		// Using WeakMap prevent memory leaks: when the converter will be destroyed all referenced between View and DOM
@@ -56,13 +64,21 @@ export default class DomConverter {
 		// I've been here. Seen stuff. Afraid of code now.
 
 		/**
+		 *
+		 * @readonly
+		 * @member {String} module:engine/view/domconverter~DomConverter#blockFillerMode
+		 */
+		this.blockFillerMode = options.blockFillerMode || 'br';
+
+		/**
 		 * Block {@link module:engine/view/filler filler} creator, which is used to create all block fillers during the
 		 * view to DOM conversion and to recognize block fillers during the DOM to view conversion.
 		 *
 		 * @readonly
+		 * @private
 		 * @member {Function} module:engine/view/domconverter~DomConverter#blockFiller
 		 */
-		this.blockFiller = options.blockFiller || BR_FILLER;
+		this._blockFiller = this.blockFillerMode == 'br' ? BR_FILLER : NBSP_FILLER;
 
 		/**
 		 * Tag names of DOM `Element`s which are considered pre-formatted elements.
@@ -254,7 +270,7 @@ export default class DomConverter {
 
 		for ( const childView of viewElement.getChildren() ) {
 			if ( fillerPositionOffset === offset ) {
-				yield this.blockFiller( domDocument );
+				yield this._blockFiller( domDocument );
 			}
 
 			yield this.viewToDom( childView, domDocument, options );
@@ -263,7 +279,7 @@ export default class DomConverter {
 		}
 
 		if ( fillerPositionOffset === offset ) {
-			yield this.blockFiller( domDocument );
+			yield this._blockFiller( domDocument );
 		}
 	}
 
@@ -370,7 +386,7 @@ export default class DomConverter {
 	 * or `null` if DOM node is a {@link module:engine/view/filler filler} or the given node is an empty text node.
 	 */
 	domToView( domNode, options = {} ) {
-		if ( isBlockFiller( domNode, this.blockFiller ) ) {
+		if ( isBlockFiller( domNode, this.blockFillerMode ) ) {
 			const isSingle = domNode.parentNode && domNode.parentNode.childNodes.length <= 1;
 
 			if ( isText( domNode ) ) {
@@ -536,7 +552,7 @@ export default class DomConverter {
 	 * @returns {module:engine/view/position~Position} viewPosition View position.
 	 */
 	domPositionToView( domParent, domOffset ) {
-		if ( isBlockFiller( domParent, this.blockFiller ) ) {
+		if ( isBlockFiller( domParent, this.blockFillerMode ) ) {
 			return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
 		}
 

+ 3 - 1
packages/ckeditor5-engine/src/view/filler.js

@@ -132,7 +132,9 @@ const templateBlockFillers = new WeakMap();
  * @param {Function} blockFiller Block filler creator.
  * @returns {Boolean} True if text node contains only {@link module:engine/view/filler~INLINE_FILLER inline filler}.
  */
-export function isBlockFiller( domNode, blockFiller ) {
+export function isBlockFiller( domNode, blockFillerMode ) {
+	const blockFiller = blockFillerMode == 'br' ? BR_FILLER : NBSP_FILLER;
+
 	let templateBlockFiller = templateBlockFillers.get( blockFiller );
 
 	if ( !templateBlockFiller ) {

+ 5 - 5
packages/ckeditor5-engine/src/view/renderer.js

@@ -583,7 +583,7 @@ export default class Renderer {
 	_diffNodeLists( actualDomChildren, expectedDomChildren ) {
 		actualDomChildren = filterOutFakeSelectionContainer( actualDomChildren, this._fakeSelectionContainer );
 
-		return diff( actualDomChildren, expectedDomChildren, sameNodes.bind( null, this.domConverter.blockFiller ) );
+		return diff( actualDomChildren, expectedDomChildren, sameNodes.bind( null, this.domConverter.blockFillerMode ) );
 	}
 
 	/**
@@ -910,11 +910,11 @@ function areSimilar( node1, node2 ) {
 //		* Two block filler elements.
 //
 // @private
-// @param {Function} blockFiller Block filler creator function, see {@link module:engine/view/domconverter~DomConverter#blockFiller}.
+// @param {String} blockFillerMode Block filler mode, see {@link module:engine/view/domconverter~DomConverter#blockFillerMode}.
 // @param {Node} node1
 // @param {Node} node2
 // @returns {Boolean}
-function sameNodes( blockFiller, actualDomChild, expectedDomChild ) {
+function sameNodes( blockFillerMode, actualDomChild, expectedDomChild ) {
 	// Elements.
 	if ( actualDomChild === expectedDomChild ) {
 		return true;
@@ -924,8 +924,8 @@ function sameNodes( blockFiller, actualDomChild, expectedDomChild ) {
 		return actualDomChild.data === expectedDomChild.data;
 	}
 	// Block fillers.
-	else if ( isBlockFiller( actualDomChild, blockFiller ) &&
-		isBlockFiller( expectedDomChild, blockFiller ) ) {
+	else if ( isBlockFiller( actualDomChild, blockFillerMode ) &&
+		isBlockFiller( expectedDomChild, blockFillerMode ) ) {
 		return true;
 	}
 

+ 6 - 4
packages/ckeditor5-engine/tests/view/domconverter/dom-to-view.js

@@ -9,7 +9,7 @@ import ViewElement from '../../../src/view/element';
 import ViewDocumentSelection from '../../../src/view/documentselection';
 import DomConverter from '../../../src/view/domconverter';
 import ViewDocumentFragment from '../../../src/view/documentfragment';
-import { INLINE_FILLER, INLINE_FILLER_LENGTH, NBSP_FILLER } from '../../../src/view/filler';
+import { INLINE_FILLER, INLINE_FILLER_LENGTH, NBSP_FILLER, BR_FILLER } from '../../../src/view/filler';
 
 import { parse, stringify } from '../../../src/dev-utils/view';
 
@@ -157,7 +157,8 @@ describe( 'DomConverter', () => {
 		} );
 
 		it( 'should return null for block filler', () => {
-			const domFiller = converter.blockFiller( document );
+			// eslint-disable-next-line new-cap
+			const domFiller = BR_FILLER( document );
 
 			expect( converter.domToView( domFiller ) ).to.be.null;
 		} );
@@ -682,7 +683,8 @@ describe( 'DomConverter', () => {
 		} );
 
 		it( 'should skip filler', () => {
-			const domFiller = converter.blockFiller( document );
+			// eslint-disable-next-line new-cap
+			const domFiller = BR_FILLER( document );
 			const domP = createElement( document, 'p', null, domFiller );
 
 			const viewChildren = Array.from( converter.domChildrenToView( domP ) );
@@ -761,7 +763,7 @@ describe( 'DomConverter', () => {
 		} );
 
 		it( 'should converter position inside block filler', () => {
-			const converter = new DomConverter( { blockFiller: NBSP_FILLER } );
+			const converter = new DomConverter( { blockFillerMode: 'nbsp' } );
 			const domFiller = NBSP_FILLER( document ); // eslint-disable-line new-cap
 			const domP = createElement( document, 'p', null, domFiller );
 

+ 6 - 6
packages/ckeditor5-engine/tests/view/domconverter/domconverter.js

@@ -10,7 +10,7 @@ import ViewEditable from '../../../src/view/editableelement';
 import ViewDocument from '../../../src/view/document';
 import ViewUIElement from '../../../src/view/uielement';
 import ViewContainerElement from '../../../src/view/containerelement';
-import { BR_FILLER, NBSP_FILLER, INLINE_FILLER, INLINE_FILLER_LENGTH } from '../../../src/view/filler';
+import { INLINE_FILLER, INLINE_FILLER_LENGTH } from '../../../src/view/filler';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 
@@ -24,13 +24,13 @@ describe( 'DomConverter', () => {
 	} );
 
 	describe( 'constructor()', () => {
-		it( 'should create converter with BR block filler by default', () => {
-			expect( converter.blockFiller ).to.equal( BR_FILLER );
+		it( 'should create converter with BR block filler mode by default', () => {
+			expect( converter.blockFillerMode ).to.equal( 'br' );
 		} );
 
-		it( 'should create converter with defined block filler', () => {
-			converter = new DomConverter( { blockFiller: NBSP_FILLER } );
-			expect( converter.blockFiller ).to.equal( NBSP_FILLER );
+		it( 'should create converter with defined block mode filler', () => {
+			converter = new DomConverter( { blockFillerMode: 'nbsp' } );
+			expect( converter.blockFillerMode ).to.equal( 'nbsp' );
 		} );
 	} );
 

+ 2 - 2
packages/ckeditor5-engine/tests/view/domconverter/view-to-dom.js

@@ -634,7 +634,7 @@ describe( 'DomConverter', () => {
 			const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
 
 			expect( domChildren.length ).to.equal( 1 );
-			expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
+			expect( isBlockFiller( domChildren[ 0 ], converter.blockFillerMode ) ).to.be.true;
 		} );
 
 		it( 'should add filler according to fillerPositionOffset', () => {
@@ -644,7 +644,7 @@ describe( 'DomConverter', () => {
 			const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
 
 			expect( domChildren.length ).to.equal( 2 );
-			expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
+			expect( isBlockFiller( domChildren[ 0 ], converter.blockFillerMode ) ).to.be.true;
 			expect( domChildren[ 1 ].data ).to.equal( 'foo' );
 		} );
 

+ 6 - 6
packages/ckeditor5-engine/tests/view/filler.js

@@ -127,22 +127,22 @@ describe( 'filler', () => {
 		it( 'should return true if the node is an instance of the BR block filler', () => {
 			const brFillerInstance = BR_FILLER( document ); // eslint-disable-line new-cap
 
-			expect( isBlockFiller( brFillerInstance, BR_FILLER ) ).to.be.true;
+			expect( isBlockFiller( brFillerInstance, 'br' ) ).to.be.true;
 			// Check it twice to ensure that caching breaks nothing.
-			expect( isBlockFiller( brFillerInstance, BR_FILLER ) ).to.be.true;
+			expect( isBlockFiller( brFillerInstance, 'br' ) ).to.be.true;
 		} );
 
 		it( 'should return true if the node is an instance of the NBSP block filler', () => {
 			const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
 
-			expect( isBlockFiller( nbspFillerInstance, NBSP_FILLER ) ).to.be.true;
+			expect( isBlockFiller( nbspFillerInstance, 'nbsp' ) ).to.be.true;
 			// Check it twice to ensure that caching breaks nothing.
-			expect( isBlockFiller( nbspFillerInstance, NBSP_FILLER ) ).to.be.true;
+			expect( isBlockFiller( nbspFillerInstance, 'nbsp' ) ).to.be.true;
 		} );
 
 		it( 'should return false for inline filler', () => {
-			expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), BR_FILLER ) ).to.be.false;
-			expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), NBSP_FILLER ) ).to.be.false;
+			expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), 'br' ) ).to.be.false;
+			expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), 'nbsp' ) ).to.be.false;
 		} );
 	} );
 } );

+ 3 - 3
packages/ckeditor5-engine/tests/view/renderer.js

@@ -804,7 +804,7 @@ describe( 'Renderer', () => {
 			// Step 3: Check whether in the first paragraph there's a <br> filler and that
 			// in the second one there are two <b> tags.
 			expect( domP.childNodes.length ).to.equal( 1 );
-			expect( isBlockFiller( domP.childNodes[ 0 ], BR_FILLER ) ).to.be.true;
+			expect( isBlockFiller( domP.childNodes[ 0 ], 'br' ) ).to.be.true;
 
 			expect( domP2.childNodes.length ).to.equal( 2 );
 			expect( domP2.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
@@ -886,7 +886,7 @@ describe( 'Renderer', () => {
 			const domP = domRoot.childNodes[ 0 ];
 
 			expect( domP.childNodes.length ).to.equal( 1 );
-			expect( isBlockFiller( domP.childNodes[ 0 ], BR_FILLER ) ).to.be.true;
+			expect( isBlockFiller( domP.childNodes[ 0 ], 'br' ) ).to.be.true;
 
 			expect( domSelection.rangeCount ).to.equal( 1 );
 			expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP );
@@ -925,7 +925,7 @@ describe( 'Renderer', () => {
 			const domP = domRoot.childNodes[ 0 ];
 
 			expect( domP.childNodes.length ).to.equal( 1 );
-			expect( isBlockFiller( domP.childNodes[ 0 ], BR_FILLER ) ).to.be.true;
+			expect( isBlockFiller( domP.childNodes[ 0 ], 'br' ) ).to.be.true;
 
 			expect( domSelection.rangeCount ).to.equal( 1 );
 			expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP );

+ 2 - 2
packages/ckeditor5-engine/tests/view/view/view.js

@@ -18,7 +18,7 @@ import ViewRange from '../../../src/view/range';
 import ViewElement from '../../../src/view/element';
 import ViewPosition from '../../../src/view/position';
 import ViewSelection from '../../../src/view/selection';
-import { isBlockFiller, BR_FILLER } from '../../../src/view/filler';
+import { isBlockFiller } from '../../../src/view/filler';
 
 import count from '@ckeditor/ckeditor5-utils/src/count';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
@@ -565,7 +565,7 @@ describe( 'view', () => {
 			view.forceRender();
 
 			expect( domDiv.childNodes.length ).to.equal( 1 );
-			expect( isBlockFiller( domDiv.childNodes[ 0 ], BR_FILLER ) ).to.be.true;
+			expect( isBlockFiller( domDiv.childNodes[ 0 ], 'br' ) ).to.be.true;
 
 			view.destroy();
 			domDiv.remove();