8
0
Pārlūkot izejas kodu

StylesMap will return string when empty.

Maciej Gołaszewski 5 gadi atpakaļ
vecāks
revīzija
5a226f97d0

+ 4 - 2
packages/ckeditor5-engine/src/view/element.js

@@ -266,7 +266,9 @@ export default class Element extends Node {
 		}
 
 		if ( key == 'style' ) {
-			return this._styles.getInlineStyle();
+			const inlineStyle = this._styles.toString();
+
+			return inlineStyle == '' ? undefined : inlineStyle;
 		}
 
 		return this._attrs.get( key );
@@ -514,7 +516,7 @@ export default class Element extends Node {
 	 */
 	getIdentity() {
 		const classes = Array.from( this._classes ).sort().join( ',' );
-		const styles = this._styles.getInlineStyle();
+		const styles = this._styles.toString();
 		const attributes = Array.from( this._attrs ).map( i => `${ i[ 0 ] }="${ i[ 1 ] }"` ).sort().join( ' ' );
 
 		return this.name +

+ 24 - 5
packages/ckeditor5-engine/src/view/stylesmap.js

@@ -166,7 +166,7 @@ export default class StylesMap {
 	/**
 	 * Removes given style.
 	 *
-	 *		styles.setTo( 'background:#foo;margin-right:2px;' );
+	 *		styles.setTo( 'background:#f00;margin-right:2px;' );
 	 *
 	 *		styles.remove( 'background' );
 	 *
@@ -218,16 +218,35 @@ export default class StylesMap {
 	}
 
 	/**
-	 * Returns a string containing normalized styles string or undefined if no style properties are set.
+	 * Returns a normalized style string. Styles are sorted by name.
 	 *
-	 * @returns {String|undefined}
+	 *		styles.set( 'margin' , '1px' );
+	 *		styles.set( 'background', '#f00' );
+	 *
+	 *		styles.toString();
+	 *	 	// Will return: 'background:#f00;margin:1px;'
+	 *
+	 * *Note:* This method supports normalized styles if defined.
+	 *
+	 *		// Enable 'margin' shorthand processing:
+	 *		editor.editing.view.document.addStyleProcessorRules( addMarginStylesProcessor );
+	 *
+	 *		styles.set( 'margin' , '1px' );
+	 *		styles.set( 'background', '#f00' );
+	 *		styles.remove( 'margin-top' );
+	 *		styles.remove( 'margin-right' );
+	 *
+	 *		styles.toString();
+	 *	 	// Will return: 'background:#f00;margin-bottom:1px;margin-left:1px;'
+	 *
+	 * @returns {String}
 	 */
-	getInlineStyle() {
+	toString() {
 		const entries = this._getStylesEntries();
 
 		// Return undefined for empty styles map.
 		if ( !entries.length ) {
-			return;
+			return '';
 		}
 
 		return entries

+ 1 - 1
packages/ckeditor5-engine/tests/view/styles/backgroundstyles.js

@@ -44,7 +44,7 @@ describe( 'Background styles normalization', () => {
 	it( 'should output inline background-color style', () => {
 		styles.setTo( 'background:#f00;' );
 
-		expect( styles.getInlineStyle() ).to.equal( 'background-color:#f00;' );
+		expect( styles.toString() ).to.equal( 'background-color:#f00;' );
 		expect( styles.getInlineProperty( 'background-color' ) ).to.equal( '#f00' );
 	} );
 } );

+ 9 - 9
packages/ckeditor5-engine/tests/view/styles/borderstyles.js

@@ -62,7 +62,7 @@ describe( 'Border styles normalization', () => {
 	it( 'should output inline shorthand rules #1', () => {
 		styles.setTo( 'border:1px solid blue;' );
 
-		expect( styles.getInlineStyle() ).to.equal(
+		expect( styles.toString() ).to.equal(
 			'border-bottom:1px solid blue;' +
 			'border-left:1px solid blue;' +
 			'border-right:1px solid blue;' +
@@ -80,7 +80,7 @@ describe( 'Border styles normalization', () => {
 			color: { top: 'blue' }
 		} );
 
-		expect( styles.getInlineStyle( 'border' ) ).to.equal( 'border-top:blue;' );
+		expect( styles.toString( 'border' ) ).to.equal( 'border-top:blue;' );
 		// TODO: expect( styles.has( 'border-top-color' ) ).to.be.true;
 		// expect( styles.getInlineProperty( 'border-top-color' ) ).to.equal( 'blue' );
 	} );
@@ -88,7 +88,7 @@ describe( 'Border styles normalization', () => {
 	it( 'should output inline shorthand rules #2', () => {
 		styles.setTo( 'border:1px solid blue;border-left:#665511 dashed 2.7em;border-top:7px dotted #ccc;' );
 
-		expect( styles.getInlineStyle() ).to.equal(
+		expect( styles.toString() ).to.equal(
 			'border-bottom:1px solid blue;' +
 			'border-left:2.7em dashed #665511;' +
 			'border-right:1px solid blue;' +
@@ -136,7 +136,7 @@ describe( 'Border styles normalization', () => {
 		styles.set( 'border-left', '#665511 dashed 2.7em' );
 		styles.set( 'border-top', '7px dotted #ccc' );
 
-		expect( styles.getInlineStyle() ).to.equal(
+		expect( styles.toString() ).to.equal(
 			'border-bottom:1px solid blue;' +
 			'border-left:2.7em dashed #665511;' +
 			'border-right:1px solid blue;' +
@@ -152,7 +152,7 @@ describe( 'Border styles normalization', () => {
 		styles.setTo( 'border:1px solid blue;' );
 		styles.remove( 'border-color' );
 
-		expect( styles.getInlineStyle() ).to.equal(
+		expect( styles.toString() ).to.equal(
 			'border-bottom:1px solid;' +
 			'border-left:1px solid;' +
 			'border-right:1px solid;' +
@@ -172,7 +172,7 @@ describe( 'Border styles normalization', () => {
 	it( 'should output border with only style shorthand (style)', () => {
 		styles.setTo( 'border:solid;' );
 
-		expect( styles.getInlineStyle() ).to.equal(
+		expect( styles.toString() ).to.equal(
 			'border-bottom:solid;' +
 			'border-left:solid;' +
 			'border-right:solid;' +
@@ -191,7 +191,7 @@ describe( 'Border styles normalization', () => {
 	it( 'should output border with only style shorthand (color)', () => {
 		styles.setTo( 'border:#f00;' );
 
-		expect( styles.getInlineStyle() ).to.equal(
+		expect( styles.toString() ).to.equal(
 			'border-bottom:#f00;' +
 			'border-left:#f00;' +
 			'border-right:#f00;' +
@@ -210,7 +210,7 @@ describe( 'Border styles normalization', () => {
 	it( 'should output border with only style shorthand (width)', () => {
 		styles.setTo( 'border:1px;' );
 
-		expect( styles.getInlineStyle() ).to.equal(
+		expect( styles.toString() ).to.equal(
 			'border-bottom:1px;' +
 			'border-left:1px;' +
 			'border-right:1px;' +
@@ -591,7 +591,7 @@ describe( 'Border styles normalization', () => {
 				'border-right:dotted #FFC000 3.0pt;'
 			);
 
-			expect( styles.getInlineStyle() ).to.equal(
+			expect( styles.toString() ).to.equal(
 				'border-bottom:3.0pt dotted #FFC000;' +
 				'border-left:none;' +
 				'border-right:3.0pt dotted #FFC000;' +

+ 10 - 10
packages/ckeditor5-engine/tests/view/styles/marginstyles.js

@@ -62,7 +62,7 @@ describe( 'Margin styles normalizer', () => {
 	it( 'should output inline style (1 value defined)', () => {
 		styles.setTo( 'margin:1px;' );
 
-		expect( styles.getInlineStyle() ).to.equal( 'margin:1px;' );
+		expect( styles.toString() ).to.equal( 'margin:1px;' );
 		expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px' );
 		expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
 		expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
@@ -73,7 +73,7 @@ describe( 'Margin styles normalizer', () => {
 	it( 'should output inline style (2 values defined)', () => {
 		styles.setTo( 'margin:1px .34cm;' );
 
-		expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm;' );
+		expect( styles.toString() ).to.equal( 'margin:1px .34cm;' );
 		expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm' );
 		expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
 		expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
@@ -84,7 +84,7 @@ describe( 'Margin styles normalizer', () => {
 	it( 'should output inline style (3 values defined)', () => {
 		styles.setTo( 'margin:1px .34cm 90.1rem;' );
 
-		expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm 90.1rem;' );
+		expect( styles.toString() ).to.equal( 'margin:1px .34cm 90.1rem;' );
 		expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem' );
 		expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
 		expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
@@ -95,7 +95,7 @@ describe( 'Margin styles normalizer', () => {
 	it( 'should output inline style (3 values defined, only last different)', () => {
 		styles.setTo( 'margin:1px 1px 90.1rem;' );
 
-		expect( styles.getInlineStyle() ).to.equal( 'margin:1px 1px 90.1rem;' );
+		expect( styles.toString() ).to.equal( 'margin:1px 1px 90.1rem;' );
 		expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 90.1rem' );
 		expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
 		expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
@@ -106,7 +106,7 @@ describe( 'Margin styles normalizer', () => {
 	it( 'should output inline style (4 values defined)', () => {
 		styles.setTo( 'margin:1px .34cm 90.1rem thick;' );
 
-		expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm 90.1rem thick;' );
+		expect( styles.toString() ).to.equal( 'margin:1px .34cm 90.1rem thick;' );
 		expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem thick' );
 		expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
 		expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
@@ -117,7 +117,7 @@ describe( 'Margin styles normalizer', () => {
 	it( 'should output inline style (4 values defined, only last different)', () => {
 		styles.setTo( 'margin:1px 1px 1px thick;' );
 
-		expect( styles.getInlineStyle() ).to.equal( 'margin:1px 1px 1px thick;' );
+		expect( styles.toString() ).to.equal( 'margin:1px 1px 1px thick;' );
 		expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 1px thick' );
 		expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
 		expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
@@ -151,28 +151,28 @@ describe( 'Margin styles normalizer', () => {
 		it( 'should output margin-top', () => {
 			styles.setTo( 'margin-top:1px;' );
 
-			expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;' );
+			expect( styles.toString() ).to.equal( 'margin-top:1px;' );
 			expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
 		} );
 
 		it( 'should output margin-right', () => {
 			styles.setTo( 'margin-right:1px;' );
 
-			expect( styles.getInlineStyle() ).to.equal( 'margin-right:1px;' );
+			expect( styles.toString() ).to.equal( 'margin-right:1px;' );
 			expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
 		} );
 
 		it( 'should output margin-bottom', () => {
 			styles.setTo( 'margin-bottom:1px;' );
 
-			expect( styles.getInlineStyle() ).to.equal( 'margin-bottom:1px;' );
+			expect( styles.toString() ).to.equal( 'margin-bottom:1px;' );
 			expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
 		} );
 
 		it( 'should output margin-left', () => {
 			styles.setTo( 'margin-left:1px;' );
 
-			expect( styles.getInlineStyle() ).to.equal( 'margin-left:1px;' );
+			expect( styles.toString() ).to.equal( 'margin-left:1px;' );
 			expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
 		} );
 	} );

+ 4 - 4
packages/ckeditor5-engine/tests/view/stylesmap.js

@@ -116,15 +116,15 @@ describe( 'StylesMap', () => {
 		} );
 	} );
 
-	describe( 'getInlineStyle()', () => {
-		it( 'should return undefined for empty styles', () => {
-			expect( stylesMap.getInlineStyle() ).to.be.undefined;
+	describe( 'toString()', () => {
+		it( 'should return empty string for empty styles', () => {
+			expect( stylesMap.toString() ).to.equal( '' );
 		} );
 
 		it( 'should return sorted styles string if styles are set', () => {
 			stylesMap.setTo( 'margin-top:1px;color:blue;' );
 
-			expect( stylesMap.getInlineStyle() ).to.equal( 'color:blue;margin-top:1px;' );
+			expect( stylesMap.toString() ).to.equal( 'color:blue;margin-top:1px;' );
 		} );
 	} );