@@ -11,6 +11,10 @@ function _getAttributeFromString(string, method, ...data) {
11
11
}
12
12
}
13
13
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
+
14
18
function _getColor ( b , i ) {
15
19
const val = b [ i ]
16
20
// color is a hex code
@@ -61,17 +65,50 @@ function _getImageSize(b, i, element) {
61
65
return [ val , null ]
62
66
}
63
67
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 ) {
64
101
const returnIfCSSNumeric = ( val , throwErr ) => {
65
102
if ( val ?. endsWith ( '%' ) )
66
103
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 )
69
106
return toPx ( element , val )
70
107
} else
71
108
return null
72
109
}
73
110
74
- const convertedVal = returnIfCSSNumeric ( val , true ) // has null as fallback already
111
+ const convertedVal = returnIfCSSNumeric ( b [ i ] , true ) // has null as fallback already
75
112
// "background-size: 50% 50%" is different to "background-size: 50%"
76
113
return [ convertedVal , returnIfCSSNumeric ( b [ i + 1 ] ) ]
77
114
}
@@ -108,6 +145,8 @@ const htmlBorderLengthNotSvgError =
108
145
new Error ( htmlLengthNotSvgErrorTemplate ( 'border lengths' , '"thin", "medium", "thick"' ) )
109
146
const htmlBackgroundSizeNotSvgError =
110
147
new Error ( htmlLengthNotSvgErrorTemplate ( 'background size' , '"cover", "contain"' ) )
148
+ const htmlBackgroundPositionNotSvgError =
149
+ new Error ( htmlLengthNotSvgErrorTemplate ( 'background position' , '"top", "bottom", "left", "right", "center"' ) )
111
150
112
151
function unitCheck ( length , err ) {
113
152
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) {
126
165
if ( val . toLowerCase ( ) === 'thick' ) return 5
127
166
unitCheck ( val , htmlBorderLengthNotSvgError )
128
167
// 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 ( ) )
130
169
return toPx ( element , val )
131
170
return false
132
171
}
@@ -137,11 +176,13 @@ const getWidth = (s, el) => _getAttributeFromString(s, _getWidth, el),
137
176
getImage = s => _getAttributeFromString ( s , _getImage ) ,
138
177
/** @returns {Array<string|number> } */
139
178
getImageSize = ( s , el ) => _getAttributeFromString ( s , _getImageSize , el ) ,
179
+ /** @returns {Array<string|number> } */
180
+ getPosition = ( s , el ) => _getAttributeFromString ( s , _getPosition , el ) ,
140
181
/** @returns {string } */
141
182
getColor = s => _getAttributeFromString ( s , _getColor ) ,
142
183
/** @returns {Array<string> } */
143
184
getRepeat = s => _getAttributeFromString ( s , _getRepeat ) ,
144
185
/** @returns {number } */
145
186
getOpacity = s => _getAttributeFromString ( s , _getOpacity )
146
187
147
- export { getWidth , getImage , getImageSize , getColor , getRepeat , getOpacity }
188
+ export { getWidth , getImage , getImageSize , getPosition , getColor , getRepeat , getOpacity }
0 commit comments