@@ -11,6 +11,10 @@ function _getAttributeFromString(string, method, ...data) {
1111 }
1212}
1313
14+ // eslint-disable-next-line no-extend-native
15+ String . prototype . matchesValidCSSLength = ( ) =>
16+ this . match ( / ( \d + ( \. \d + ) ? ( c h | c m | e m | e x | i n | m m | p c | p t | p x | r e m | v h | v m a x | v m i n | v w ) | 0 ) / )
17+
1418function _getColor ( b , i ) {
1519 const val = b [ i ]
1620 // color is a hex code
@@ -61,17 +65,50 @@ function _getImageSize(b, i, element) {
6165 return [ val , null ]
6266 }
6367
68+ return __getTwoNumerics ( b , i , element , htmlBackgroundSizeNotSvgError )
69+ }
70+
71+ function _getPosition ( b , i , element ) {
72+ // "val" is always what is defined in backgrund-size ([i]th argument)
73+ const val = b [ i ]
74+ const allWords = [ 'top' , 'bottom' , 'left' , 'right' , 'center' ]
75+
76+ if ( b . length === 1 && allWords . includes ( val ) ) {
77+ if ( val === 'center' )
78+ return [ 'center' , 'center' ]
79+ else if ( [ 'left' , 'right' ] . includes ( val ) )
80+ return [ val , 0 ]
81+ else if ( [ 'top' , 'bottom' ] . includes ( val ) )
82+ return [ 0 , val ]
83+ }
84+
85+ const a = [ 0 , 0 ]
86+
87+ if ( allWords . includes ( val ) ) {
88+ if ( b [ i + 1 ] . matchesValidCSSLength ( ) ) {
89+
90+ }
91+ }
92+
93+ /*if (['cover', 'contain'].includes(val)) {
94+ return [val, null]
95+ }*/
96+
97+ return __getTwoNumerics ( b , i , element , htmlBackgroundPositionNotSvgError )
98+ }
99+
100+ function __getTwoNumerics ( b , i , element , err ) {
64101 const returnIfCSSNumeric = ( val , throwErr ) => {
65102 if ( val ?. endsWith ( '%' ) )
66103 return val
67- else if ( val ?. match ( / ( \d + ( \. \d + ) ? ( c h | c m | e m | e x | i n | m m | p c | p t | p x | r e m | v h | v m a x | v m i n | v w ) | 0 ) / ) ) {
68- unitCheck ( val , throwErr ? htmlBackgroundSizeNotSvgError : undefined )
104+ else if ( val ?. matchesValidCSSLength ( ) ) {
105+ unitCheck ( val , throwErr ? err : undefined )
69106 return toPx ( element , val )
70107 } else
71108 return null
72109 }
73110
74- const convertedVal = returnIfCSSNumeric ( val , true ) // has null as fallback already
111+ const convertedVal = returnIfCSSNumeric ( b [ i ] , true ) // has null as fallback already
75112 // "background-size: 50% 50%" is different to "background-size: 50%"
76113 return [ convertedVal , returnIfCSSNumeric ( b [ i + 1 ] ) ]
77114}
@@ -108,6 +145,8 @@ const htmlBorderLengthNotSvgError =
108145 new Error ( htmlLengthNotSvgErrorTemplate ( 'border lengths' , '"thin", "medium", "thick"' ) )
109146const htmlBackgroundSizeNotSvgError =
110147 new Error ( htmlLengthNotSvgErrorTemplate ( 'background size' , '"cover", "contain"' ) )
148+ const htmlBackgroundPositionNotSvgError =
149+ new Error ( htmlLengthNotSvgErrorTemplate ( 'background position' , '"top", "bottom", "left", "right", "center"' ) )
111150
112151function unitCheck ( length , err ) {
113152 if ( length ?. match ( / ( c a p | i c | l h | r l h | v i | v m | v b | Q | m o z m m ) / g) )
@@ -126,7 +165,7 @@ function _getWidth(border, i, element) {
126165 if ( val . toLowerCase ( ) === 'thick' ) return 5
127166 unitCheck ( val , htmlBorderLengthNotSvgError )
128167 // width is <length>
129- if ( val . match ( / ( \d + ( \. \d + ) ? ( c h | c m | e m | e x | i n | m m | p c | p t | p x | r e m | v h | v m a x | v m i n | v w ) | 0 ) / ) )
168+ if ( val . matchesValidCSSLength ( ) )
130169 return toPx ( element , val )
131170 return false
132171}
@@ -137,11 +176,13 @@ const getWidth = (s, el) => _getAttributeFromString(s, _getWidth, el),
137176 getImage = s => _getAttributeFromString ( s , _getImage ) ,
138177 /** @returns {Array<string|number> } */
139178 getImageSize = ( s , el ) => _getAttributeFromString ( s , _getImageSize , el ) ,
179+ /** @returns {Array<string|number> } */
180+ getPosition = ( s , el ) => _getAttributeFromString ( s , _getPosition , el ) ,
140181 /** @returns {string } */
141182 getColor = s => _getAttributeFromString ( s , _getColor ) ,
142183 /** @returns {Array<string> } */
143184 getRepeat = s => _getAttributeFromString ( s , _getRepeat ) ,
144185 /** @returns {number } */
145186 getOpacity = s => _getAttributeFromString ( s , _getOpacity )
146187
147- export { getWidth , getImage , getImageSize , getColor , getRepeat , getOpacity }
188+ export { getWidth , getImage , getImageSize , getPosition , getColor , getRepeat , getOpacity }
0 commit comments