Ver código fonte

StylesProcessor is not singleton anymore.

Kamil Piechaczek 5 anos atrás
pai
commit
10085ac7fb

+ 15 - 6
packages/ckeditor5-engine/src/conversion/viewconsumable.js

@@ -9,7 +9,6 @@
 
 import { isArray } from 'lodash-es';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
-import StylesMap from '../view/stylesmap';
 
 /**
  * Class used for handling consumption of view {@link module:engine/view/element~Element elements},
@@ -89,7 +88,7 @@ export default class ViewConsumable {
 
 		// For elements create new ViewElementConsumables or update already existing one.
 		if ( !this._consumables.has( element ) ) {
-			elementConsumables = new ViewElementConsumables();
+			elementConsumables = new ViewElementConsumables( element );
 			this._consumables.set( element, elementConsumables );
 		} else {
 			elementConsumables = this._consumables.get( element );
@@ -239,6 +238,7 @@ export default class ViewConsumable {
 	 */
 	static consumablesFromElement( element ) {
 		const consumables = {
+			element,
 			name: true,
 			attributes: [],
 			classes: [],
@@ -284,7 +284,7 @@ export default class ViewConsumable {
 	 */
 	static createFrom( from, instance ) {
 		if ( !instance ) {
-			instance = new ViewConsumable();
+			instance = new ViewConsumable( from );
 		}
 
 		if ( from.is( 'text' ) ) {
@@ -319,8 +319,17 @@ export default class ViewConsumable {
 class ViewElementConsumables {
 	/**
 	 * Creates ViewElementConsumables instance.
+	 *
+	 * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} from View node or document fragment
+	 * from which `ViewElementConsumables` is being created.
 	 */
-	constructor() {
+	constructor( from ) {
+		/**
+		 * @readonly
+		 * @member {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
+		 */
+		this.element = from;
+
 		/**
 		 * Flag indicating if name of the element can be consumed.
 		 *
@@ -510,7 +519,7 @@ class ViewElementConsumables {
 			consumables.set( name, true );
 
 			if ( type === 'styles' ) {
-				for ( const alsoName of StylesMap.getRelatedStyles( name ) ) {
+				for ( const alsoName of this.element.document.stylesProcessor.getRelatedStyles( name ) ) {
 					consumables.set( alsoName, true );
 				}
 			}
@@ -577,7 +586,7 @@ class ViewElementConsumables {
 				consumables.set( name, false );
 
 				if ( type == 'styles' ) {
-					for ( const toConsume of StylesMap.getRelatedStyles( name ) ) {
+					for ( const toConsume of this.element.document.stylesProcessor.getRelatedStyles( name ) ) {
 						consumables.set( toConsume, false );
 					}
 				}

+ 10 - 2
packages/ckeditor5-engine/src/view/document.js

@@ -11,7 +11,7 @@ import DocumentSelection from './documentselection';
 import Collection from '@ckeditor/ckeditor5-utils/src/collection';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
-import StylesMap from './stylesmap';
+import { StylesProcessor } from './stylesmap';
 
 // @if CK_DEBUG_ENGINE // const { logDocument } = require( '../dev-utils/utils' );
 
@@ -48,6 +48,14 @@ export default class Document {
 		this.roots = new Collection( { idProperty: 'rootName' } );
 
 		/**
+		 * StylesProcessor is responsible for writing and reading a normalized styles object.
+		 *
+		 * @readonly
+		 * @member {module:engine/view/stylesmap~StylesProcessor}
+		 */
+		this.stylesProcessor = new StylesProcessor();
+
+		/**
 		 * Defines whether document is in read-only mode.
 		 *
 		 * When document is read-ony then all roots are read-only as well and caret placed inside this root is hidden.
@@ -174,7 +182,7 @@ export default class Document {
 	 * @param {Function} callback
 	 */
 	addStyleProcessorRules( callback ) {
-		callback( StylesMap._styleProcessor );
+		callback( this.stylesProcessor );
 	}
 
 	/**

+ 1 - 1
packages/ckeditor5-engine/src/view/element.js

@@ -111,7 +111,7 @@ export default class Element extends Node {
 		 * @protected
 		 * @member {module:engine/view/stylesmap~StylesMap} module:engine/view/element~Element#_styles
 		 */
-		this._styles = new StylesMap();
+		this._styles = new StylesMap( this.document.stylesProcessor );
 
 		if ( this._attrs.has( 'style' ) ) {
 			// Remove style attribute and handle it by styles map.

+ 11 - 59
packages/ckeditor5-engine/src/view/stylesmap.js

@@ -17,28 +17,28 @@ import { get, isObject, merge, set, unset } from 'lodash-es';
 export default class StylesMap {
 	/**
 	 * Creates Styles instance.
+	 *
+	 * @param {module:engine/view/stylesmap~StylesProcessor} styleProcessor
 	 */
-	constructor() {
+	constructor( styleProcessor ) {
 		/**
 		 * Keeps an internal representation of styles map. Normalized styles are kept as object tree to allow unified modification and
 		 * value access model using lodash's get, set, unset, etc methods.
 		 *
 		 * When no style processor rules are defined the it acts as simple key-value storage.
 		 *
-		 * @type {Object}
 		 * @private
+		 * @type {Object}
 		 */
 		this._styles = {};
 
-		// Hide _styleProcessor from the watchdog by making this property non-enumarable. Watchdog checks errors for their editor origin
-		// by checking if two objects are connected through properties. Using singleton is against this check as it would detect
-		// that two editors are connected through single style processor instance.
-		Object.defineProperty( this, '_styleProcessor', {
-			get() {
-				return StylesMap._styleProcessor;
-			},
-			enumerable: false
-		} );
+		/**
+		 * An instance of the {@link module:engine/view/stylesmap~StylesProcessor}.
+		 *
+		 * @private
+		 * @member {module:engine/view/stylesmap~StylesProcessor}
+		 */
+		this._styleProcessor = styleProcessor;
 	}
 
 	/**
@@ -372,28 +372,6 @@ export default class StylesMap {
 	}
 
 	/**
-	 * Returns related style names.
-	 *
-	 *		// Enable 'margin' shorthand processing:
-	 *		editor.editing.view.document.addStyleProcessorRules( addMarginRules );
-	 *
-	 *		StylesMap.getRelatedStyles( 'margin' );
-	 *		// will return: [ 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ];
-	 *
-	 *		StylesMap.getRelatedStyles( 'margin-top' );
-	 *		// will return: [ 'margin' ];
-	 *
-	 * **Note**: To define new style relations load an existing style processor (as shown above) or use
-	 * {@link module:engine/view/stylesmap~StylesProcessor#setStyleRelation `StylesProcessor.setStyleRelation()`}.
-	 *
-	 * @param {String} name
-	 * @returns {Array.<String>}
-	 */
-	static getRelatedStyles( name ) {
-		return this._styleProcessor.getRelatedStyles( name );
-	}
-
-	/**
 	 * Returns normalized styles entries for further processing.
 	 *
 	 * @private
@@ -439,32 +417,6 @@ export default class StylesMap {
 			this.remove( parentPath );
 		}
 	}
-
-	/**
-	 * Returns global StylesProcessor instance.
-	 *
-	 * @returns {module:engine/view/stylesmap~StylesProcessor}
-	 * @private
-	 */
-	static get _styleProcessor() {
-		if ( !this._processor ) {
-			this._processor = new StylesProcessor();
-		}
-
-		return this._processor;
-	}
-
-	/**
-	 * Set new StylesProcessor instance.
-	 *
-	 * This is an internal method used mostly in tests.
-	 *
-	 * @param processor
-	 * @protected
-	 */
-	static _setProcessor( processor ) {
-		this._processor = processor;
-	}
 }
 
 /**

+ 8 - 8
packages/ckeditor5-engine/tests/conversion/viewconsumable.js

@@ -8,7 +8,6 @@ import ViewElement from '../../src/view/element';
 import ViewText from '../../src/view/text';
 import ViewDocumentFragment from '../../src/view/documentfragment';
 import ViewConsumable from '../../src/conversion/viewconsumable';
-import StylesMap, { StylesProcessor } from '../../src/view/stylesmap';
 import { addBorderRules } from '../../src/view/styles/border';
 import { addMarginRules } from '../../src/view/styles/margin';
 import { addPaddingRules } from '../../src/view/styles/padding';
@@ -16,8 +15,15 @@ import { addPaddingRules } from '../../src/view/styles/padding';
 describe( 'ViewConsumable', () => {
 	let viewConsumable, el, viewDocument;
 
-	beforeEach( () => {
+	before( () => {
 		viewDocument = new ViewDocument();
+
+		addBorderRules( viewDocument.stylesProcessor );
+		addMarginRules( viewDocument.stylesProcessor );
+		addPaddingRules( viewDocument.stylesProcessor );
+	} );
+
+	beforeEach( () => {
 		viewConsumable = new ViewConsumable();
 		el = new ViewElement( viewDocument, 'p' );
 	} );
@@ -558,13 +564,7 @@ describe( 'ViewConsumable', () => {
 
 	describe( 'style shorthands handling', () => {
 		before( () => {
-			const stylesProcessor = new StylesProcessor();
-
-			StylesMap._setProcessor( stylesProcessor );
 
-			addBorderRules( stylesProcessor );
-			addMarginRules( stylesProcessor );
-			addPaddingRules( stylesProcessor );
 		} );
 
 		describe( 'add', () => {

+ 2 - 0
packages/ckeditor5-engine/tests/view/_utils/createdocumentmock.js

@@ -5,6 +5,7 @@
 
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 import DocumentSelection from '../../../src/view/documentselection';
+import { StylesProcessor } from '../../../src/view/stylesmap';
 
 /**
  * Creates {@link module:engine/view/document~Document view Document} mock.
@@ -16,6 +17,7 @@ export default function createDocumentMock() {
 	doc.set( 'isFocused', false );
 	doc.set( 'isReadOnly', false );
 	doc.selection = new DocumentSelection();
+	doc.stylesProcessor = new StylesProcessor();
 
 	return doc;
 }

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

@@ -10,7 +10,6 @@ import Document from '../../src/view/document';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import count from '@ckeditor/ckeditor5-utils/src/count';
 import createViewRoot from './_utils/createroot';
-import StylesMap from '../../src/view/stylesmap';
 
 describe( 'Document', () => {
 	let domRoot, viewDocument;
@@ -94,13 +93,13 @@ describe( 'Document', () => {
 	} );
 
 	describe( 'addStyleProcessorRules()', () => {
-		it( 'should ', () => {
+		it( 'should execute callback with an instance of StyleProcessor as the first argument', () => {
 			const spy = sinon.spy();
 
 			viewDocument.addStyleProcessorRules( spy );
 
 			sinon.assert.calledOnce( spy );
-			sinon.assert.calledWithExactly( spy, StylesMap._styleProcessor );
+			sinon.assert.calledWithExactly( spy, viewDocument.stylesProcessor );
 		} );
 	} );
 } );

+ 6 - 1
packages/ckeditor5-engine/tests/view/element.js

@@ -237,7 +237,12 @@ describe( 'Element', () => {
 	} );
 
 	describe( 'isSimilar()', () => {
-		const el = new Element( document, 'p', { foo: 'bar' } );
+		let el;
+
+		beforeEach( () => {
+			el = new Element( document, 'p', { foo: 'bar' } );
+		} );
+
 		it( 'should return false when comparing to non-element', () => {
 			expect( el.isSimilar( null ) ).to.be.false;
 			expect( el.isSimilar( {} ) ).to.be.false;

+ 3 - 4
packages/ckeditor5-engine/tests/view/styles/background.js

@@ -7,16 +7,15 @@ import StylesMap, { StylesProcessor } from '../../../src/view/stylesmap';
 import { addBackgroundRules } from '../../../src/view/styles/background';
 
 describe( 'Background styles normalization', () => {
-	let styles;
+	let styles, stylesProcessor;
 
 	before( () => {
-		const stylesProcessor = new StylesProcessor();
-		StylesMap._setProcessor( stylesProcessor );
+		stylesProcessor = new StylesProcessor();
 		addBackgroundRules( stylesProcessor );
 	} );
 
 	beforeEach( () => {
-		styles = new StylesMap();
+		styles = new StylesMap( stylesProcessor );
 	} );
 
 	it( 'should normalize background', () => {

+ 3 - 4
packages/ckeditor5-engine/tests/view/styles/border.js

@@ -7,16 +7,15 @@ import StylesMap, { StylesProcessor } from '../../../src/view/stylesmap';
 import { addBorderRules } from '../../../src/view/styles/border';
 
 describe( 'Border styles normalization', () => {
-	let styles;
+	let styles, stylesProcessor;
 
 	before( () => {
-		const stylesProcessor = new StylesProcessor();
-		StylesMap._setProcessor( stylesProcessor );
+		stylesProcessor = new StylesProcessor();
 		addBorderRules( stylesProcessor );
 	} );
 
 	beforeEach( () => {
-		styles = new StylesMap();
+		styles = new StylesMap( stylesProcessor );
 	} );
 
 	it( 'should parse border shorthand', () => {

+ 3 - 4
packages/ckeditor5-engine/tests/view/styles/margin.js

@@ -7,16 +7,15 @@ import StylesMap, { StylesProcessor } from '../../../src/view/stylesmap';
 import { addMarginRules } from '../../../src/view/styles/margin';
 
 describe( 'Margin styles normalizer', () => {
-	let styles;
+	let styles, stylesProcessor;
 
 	before( () => {
-		const stylesProcessor = new StylesProcessor();
-		StylesMap._setProcessor( stylesProcessor );
+		stylesProcessor = new StylesProcessor();
 		addMarginRules( stylesProcessor );
 	} );
 
 	beforeEach( () => {
-		styles = new StylesMap();
+		styles = new StylesMap( stylesProcessor );
 	} );
 
 	it( 'should set all margins (1 value defined)', () => {

+ 3 - 4
packages/ckeditor5-engine/tests/view/styles/padding.js

@@ -7,16 +7,15 @@ import StylesMap, { StylesProcessor } from '../../../src/view/stylesmap';
 import { addPaddingRules } from '../../../src/view/styles/padding';
 
 describe( 'Padding styles normalization', () => {
-	let styles;
+	let styles, stylesProcessor;
 
 	before( () => {
-		const stylesProcessor = new StylesProcessor();
-		StylesMap._setProcessor( stylesProcessor );
+		stylesProcessor = new StylesProcessor();
 		addPaddingRules( stylesProcessor );
 	} );
 
 	beforeEach( () => {
-		styles = new StylesMap();
+		styles = new StylesMap( stylesProcessor );
 	} );
 
 	it( 'should set all padding values (1 value defined)', () => {

+ 1 - 16
packages/ckeditor5-engine/tests/view/stylesmap.js

@@ -26,8 +26,7 @@ describe( 'StylesMap', () => {
 		stylesProcessor.setReducer( 'foo', getBoxSidesValueReducer( 'foo' ) );
 
 		addMarginRules( stylesProcessor );
-		StylesMap._setProcessor( stylesProcessor );
-		stylesMap = new StylesMap();
+		stylesMap = new StylesMap( stylesProcessor );
 	} );
 
 	describe( 'size getter', () => {
@@ -272,18 +271,4 @@ describe( 'StylesMap', () => {
 			expect( stylesMap.getStyleNames() ).to.deep.equal( [ 'foo' ] );
 		} );
 	} );
-
-	describe( 'static _styleProcessor getter', () => {
-		it( 'should return StyleProcessor instance', () => {
-			// Set undefined to reset field value for code coverage.
-			StylesMap._setProcessor( undefined );
-			expect( StylesMap._styleProcessor ).to.be.instanceOf( StylesProcessor );
-		} );
-
-		it( 'should return the same StyleProcessor instance on consecutive calls', () => {
-			const stylesProcessor = StylesMap._styleProcessor;
-
-			expect( StylesMap._styleProcessor ).to.equal( stylesProcessor );
-		} );
-	} );
 } );