Sfoglia il codice sorgente

Add isPercentage() CSS value check.

Maciej Gołaszewski 5 anni fa
parent
commit
ba7a82ddde

+ 12 - 0
packages/ckeditor5-engine/src/view/styles/utils.js

@@ -98,6 +98,18 @@ export function isLength( string ) {
 	return lengthRegExp.test( string );
 }
 
+const PERCENTAGE_VALUE_REGEXP = /^[+-]?[0-9]*[.]?[0-9]+%$/;
+
+/**
+ * Checks if string contains [percentage](https://developer.mozilla.org/en-US/docs/Web/CSS/percentage) CSS value.
+ *
+ * @param {String} string
+ * @returns {Boolean}
+ */
+export function isPercentage( string ) {
+	return PERCENTAGE_VALUE_REGEXP.test( string );
+}
+
 const repeatValues = [ 'repeat-x', 'repeat-y', 'repeat', 'space', 'round', 'no-repeat' ];
 
 /**

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

@@ -9,7 +9,7 @@ import {
 	getShorthandValues,
 	isColor,
 	isLength,
-	isLineStyle
+	isLineStyle, isPercentage
 } from '../../../src/view/styles/utils';
 
 describe( 'Styles utils', () => {
@@ -159,6 +159,16 @@ describe( 'Styles utils', () => {
 		} );
 	} );
 
+	describe( 'isPercentage()', () => {
+		it( 'returns true valid values', () => {
+			testValues( [ '1%', '100%', '1123.1312%', '0.9876%' ], isPercentage );
+		} );
+
+		it( 'returns false for not a percentage values', () => {
+			testValues( [ '0', '1px', '1000px', '1.1px', '345.457px', '.457px' ], value => !isPercentage( value ) );
+		} );
+	} );
+
 	describe( 'getBoxSidesShorthandValue()', () => {
 		it( 'should output one value for same values', () => {
 			expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } ) ).to.equal( 'foo' );