11import CSS_COLOR_NAMES from "./html-colors" ;
22import toPx from "./css-length-converter" ;
3- import { element } from 'prop-types'
43
54function _getAttributeFromString ( string , method , ...data ) {
65 if ( ! string ) return false
@@ -12,16 +11,16 @@ function _getAttributeFromString(string, method, ...data) {
1211 }
1312}
1413
15- function _getColor ( border , i ) {
16- const val = border [ i ]
14+ function _getColor ( b , i ) {
15+ const val = b [ i ]
1716 // color is a hex code
1817 if ( val . toLowerCase ( ) . match ( / # ( [ 0 - 9 a - f ] { 3 } ) { 1 , 2 } / ) ) return val
1918 // color is a function (rgb, rgba, hsl, hsla)
2019 if ( val . startsWith ( 'rgb' ) || val . startsWith ( 'hsl' ) ) {
2120 let color = val ;
2221 if ( ! val . endsWith ( ')' ) )
23- for ( let j = 1 ; ! border [ i + j - 1 ] . endsWith ( ')' ) ; j ++ ) {
24- color += border [ i + j ]
22+ for ( let j = 1 ; ! b [ i + j - 1 ] . endsWith ( ')' ) ; j ++ ) {
23+ color += b [ i + j ]
2524 }
2625 if ( color [ 3 ] === 'a' )
2726 color = color . replace ( 'a' , '' ) . replace ( / , [ ^ ) , ] + \) / , ')' )
@@ -37,13 +36,13 @@ function _getColor(border, i) {
3736 return false
3837}
3938
40- function _getImage ( border , i ) {
41- const val = border [ i ]
39+ function _getImage ( b , i ) {
40+ const val = b [ i ]
4241
4342 if ( val . startsWith ( 'url' ) ) {
4443 let url = val ;
45- for ( let j = 1 ; border [ i + j ] ; j ++ ) {
46- url += border [ i + j ]
44+ for ( let j = 1 ; b [ i + j ] ; j ++ ) {
45+ url += b [ i + j ]
4746 }
4847 url = / u r l \( ( " [ ^ " ] + " | ' [ ^ ' ] + ' | [ ^ ) ] + ) \) / g. exec ( url )
4948 url = url ? url [ 1 ] : false
@@ -54,38 +53,66 @@ function _getImage(border, i) {
5453 }
5554}
5655
57- function _getImageSize ( border , i , element ) {
58- const val = border [ i ]
56+ function _getImageSize ( b , i , element ) {
57+ // "val" is always what is defined in backgrund-size ([i]th argument)
58+ const val = b [ i ]
5959
60- if ( [ 'cover' , 'contain' ] . includes ( val ) || val . endsWith ( '%' ) ) {
61- return val
60+ if ( [ 'cover' , 'contain' ] . includes ( val ) ) {
61+ return [ val , null ]
6262 }
63- unitCheck ( val , htmlBackgroundSizeNotSvgError )
64- 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 ) / ) )
65- return toPx ( element , val )
63+
64+ const returnIfCSSNumeric = ( val , throwErr ) => {
65+ if ( val ?. endsWith ( '%' ) )
66+ 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 )
69+ return toPx ( element , val )
70+ } else
71+ return null
72+ }
73+
74+ const convertedVal = returnIfCSSNumeric ( val , true ) // has null as fallback already
75+ // "background-size: 50% 50%" is different to "background-size: 50%"
76+ return [ convertedVal , returnIfCSSNumeric ( b [ i + 1 ] ) ]
6677}
6778
68- function _getOpacity ( border , i ) {
69- let val = border [ i ]
79+ function _getOpacity ( b , i ) {
80+ let val = b [ i ]
7081 if ( val . startsWith ( 'rgba' ) || val . startsWith ( 'hsla' ) ) {
7182 if ( ! val . endsWith ( ')' ) )
72- for ( let j = 1 ; ! border [ i + j - 1 ] . endsWith ( ')' ) ; j ++ ) {
73- val += border [ i + j ]
83+ for ( let j = 1 ; ! b [ i + j - 1 ] . endsWith ( ')' ) ; j ++ ) {
84+ val += b [ i + j ]
7485 }
7586 return val . replace ( / ( r g b | h s l ) a ? \( ( [ ^ , ) ] + , ) { 3 } / , '' ) . replace ( / \) $ / , '' )
7687 }
77- if ( border . length - 1 === i )
88+ if ( b . length - 1 === i )
7889 return 1
7990}
8091
92+ function _getRepeat ( b , i ) {
93+ let val = b [ i ]
94+ if ( val === 'repeat-x' )
95+ return [ 'repeat' , 'no-repeat' ]
96+ else if ( val === 'repeat-y' )
97+ return [ 'no-repeat' , 'repeat' ]
98+ else if ( [ 'repeat' , 'space' , 'round' , 'no-repeat' ] . includes ( val ) ) {
99+ if ( [ 'repeat' , 'space' , 'round' , 'no-repeat' ] . includes ( b [ i + 1 ] || '' ) )
100+ return [ val , b [ i + 1 ] ]
101+ else
102+ return [ val , val ]
103+ }
104+ }
105+
81106const htmlLengthNotSvgErrorTemplate = ( a , b ) => `<RoundDiv> ${ a } must be ${ b ? `either ${ b } , or` : '' } in one of the following units: ch, cm, em, ex, in, mm, pc, pt, px, rem, vh, vmax, vmin, vw.`
82107const htmlBorderLengthNotSvgError =
83108 new Error ( htmlLengthNotSvgErrorTemplate ( 'border lengths' , '"thin", "medium", "thick"' ) )
84109const htmlBackgroundSizeNotSvgError =
85110 new Error ( htmlLengthNotSvgErrorTemplate ( 'background size' , '"cover", "contain"' ) )
86111
87112function unitCheck ( length , err ) {
88- 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) ) throw err
113+ 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) )
114+ if ( err ) throw err
115+ else return false
89116 return length
90117}
91118
@@ -104,10 +131,17 @@ function _getWidth(border, i, element) {
104131 return false
105132}
106133
134+ /** @returns {number } */
107135const getWidth = ( s , el ) => _getAttributeFromString ( s , _getWidth , el ) ,
136+ /** @returns {string } */
108137 getImage = s => _getAttributeFromString ( s , _getImage ) ,
138+ /** @returns {Array<string|number> } */
109139 getImageSize = ( s , el ) => _getAttributeFromString ( s , _getImageSize , el ) ,
140+ /** @returns {string } */
110141 getColor = s => _getAttributeFromString ( s , _getColor ) ,
142+ /** @returns {Array<string> } */
143+ getRepeat = s => _getAttributeFromString ( s , _getRepeat ) ,
144+ /** @returns {number } */
111145 getOpacity = s => _getAttributeFromString ( s , _getOpacity )
112146
113- export { getWidth , getImage , getImageSize , getColor , getOpacity }
147+ export { getWidth , getImage , getImageSize , getColor , getRepeat , getOpacity }
0 commit comments