8
0
فهرست منبع

Add more background shorthand values parsing.

Maciej Gołaszewski 6 سال پیش
والد
کامیت
b90c2b2a57
2فایلهای تغییر یافته به همراه44 افزوده شده و 8 حذف شده
  1. 29 7
      packages/ckeditor5-engine/src/view/styles.js
  2. 15 1
      packages/ckeditor5-engine/tests/view/styles.js

+ 29 - 7
packages/ckeditor5-engine/src/view/styles.js

@@ -395,17 +395,13 @@ export default class Styles {
 	 * @returns {Array.<Array.<String, String>>}
 	 */
 	_getReduceForm( styleName, normalizedValue ) {
-		let stylesArray;
-
 		if ( this.reducers.has( styleName ) ) {
 			const styleGetter = this.reducers.get( styleName );
 
-			stylesArray = styleGetter( normalizedValue );
-		} else {
-			stylesArray = [ [ styleName, normalizedValue ] ];
+			return styleGetter( normalizedValue );
 		}
 
-		return stylesArray || [];
+		return [ [ styleName, normalizedValue ] ];
 	}
 }
 
@@ -496,8 +492,18 @@ function normalizeBackground( value ) {
 	const parts = value.split( ' ' );
 
 	for ( const part of parts ) {
-		if ( isColor( part ) ) {
+		if ( isRepeat( part ) ) {
+			background.repeat = background.repeat || [];
+			background.repeat.push( part );
+		} else if ( isPosition( part ) ) {
+			background.position = background.position || [];
+			background.position.push( part );
+		} else if ( isAttachment( part ) ) {
+			background.attachment = part;
+		} else if ( isColor( part ) ) {
 			background.color = part;
+		} else if ( isURL( part ) ) {
+			background.image = part;
 		}
 	}
 
@@ -516,6 +522,22 @@ function isLength( string ) {
 	return /^[+-]?[0-9]?[.]?[0-9]+([a-z]+|%)$/.test( string );
 }
 
+function isRepeat( string ) {
+	return /^(repeat-x|repeat-y|repeat|space|round|no-repeat)$/.test( string );
+}
+
+function isPosition( string ) {
+	return /^(center|top|bottom|left|right)$/.test( string );
+}
+
+function isAttachment( string ) {
+	return /^(fixed|scroll|local)$/.test( string );
+}
+
+function isURL( string ) {
+	return /^url\(/.test( string );
+}
+
 function getBorderReducer( value ) {
 	const ret = [];
 

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

@@ -676,7 +676,21 @@ describe( 'Styles', () => {
 
 		describe( 'background', () => {
 			it( 'should normalize background', () => {
-				styles.setStyle( 'background:#f00;' );
+				// TODO: border-box given only for coverage test.
+				styles.setStyle( 'background:url("example.jpg") center #f00 repeat-y fixed border-box;' );
+
+				expect( styles.getNormalized( 'background' ) ).to.deep.equal( {
+					attachment: 'fixed',
+					image: 'url("example.jpg")',
+					position: [ 'center' ],
+					repeat: [ 'repeat-y' ],
+					color: '#f00'
+				} );
+			} );
+
+			// TODO: define what should happen with layers
+			it.skip( 'should normalize background with layers', () => {
+				styles.setStyle( 'background:url("test.jpg") repeat-y,#f00;' );
 
 				expect( styles.getNormalized( 'background' ) ).to.deep.equal( { color: '#f00' } );
 			} );