1
- // Parser for a linear gradient:
2
- // ---
3
- // <linear-gradient> = linear-gradient(
4
- // [ [ <angle> | to <side-or-corner> ] ,]?
5
- // <color-stop>[, <color-stop>]+
6
- // )
7
- //
8
- // <side-or-corner> = [left | right] || [top | bottom]
9
- // ---
10
- // Source: https://www.w3.org/TR/css3-images/#linear-gradients
11
- // ---
12
- // Example: "to top, rgba(67, 90, 111, 0.04), white"
13
- const parseLinearGradient = ( value : string ) => {
1
+ import { BackgroundImageType } from '../type' ;
2
+
3
+ /**
4
+ * 解析线性渐变
5
+ *
6
+ * ---
7
+ * <linear-gradient> = linear-gradient(
8
+ * [ [ <angle> | to <side-or-corner> ] ,]?
9
+ * <color-stop>[, <color-stop>]+
10
+ * )
11
+ *
12
+ * <side-or-corner> = [left | right] || [top | bottom]
13
+ *
14
+ * ---
15
+ * * Example: "to top, rgba(67, 90, 111, 0.04), white"
16
+ *
17
+ * @param value
18
+ * @see https://www.w3.org/TR/css3-images/#linear-gradients
19
+ */
20
+ export const parseLinearGradient = ( value : string ) => {
14
21
const parts = [ ] ;
15
22
let currentPart = [ ] ;
16
23
let i = 0 ;
@@ -47,6 +54,12 @@ const parseLinearGradient = (value: string) => {
47
54
} ;
48
55
}
49
56
if ( parts . length > 2 ) {
57
+ // 如果 parts 的第一个对象 不包含 deg 和 to
58
+ // 那就意味着全部都是 stops
59
+ if ( ! parts [ 0 ] . includes ( 'deg' ) && ! parts [ 0 ] . includes ( 'to' ) ) {
60
+ return { angle : '180deg' , stops : parts } ;
61
+ }
62
+
50
63
// angle + n stops
51
64
const [ angle , ...stops ] = parts ;
52
65
@@ -60,23 +73,23 @@ const parseLinearGradient = (value: string) => {
60
73
return null ;
61
74
} ;
62
75
63
- type BackgroundImageType = {
64
- type : 'Image' | 'LinearGradient' ;
65
- value : any ;
66
- } ;
67
-
68
- // Parses the background-image. The structure is as follows:
69
- // (Supports images and gradients)
70
- // ---
71
- // <background- image> = <bg-image> [ , <bg- image> ]*
72
- // <bg-image> = <image> | none
73
- // <image> = <url> | <image-list> | <element-reference> | <image-combination> | <gradient>
74
- // ---
75
- // Source: https://www.w3.org/TR/css-backgrounds-3/#the-background-image
76
- // ---
77
- // These functions should be pure to make it easy
78
- // to write test cases in the future.
79
- const parseBackgroundImage = ( value : string ) : BackgroundImageType | void => {
76
+ /**
77
+ * 解析背景图片
78
+ * The structure is as follows:
79
+ * (Supports images and gradients)
80
+ *
81
+ * ---
82
+ * <background-image> = <bg-image> [ , <bg-image> ]*
83
+ * <bg-image> = <image> | none
84
+ * < image> = <url> | <image-list> | <element-reference> | < image-combination> | <gradient>
85
+ * ---
86
+ * @param value
87
+ * @see : https://www.w3.org/TR/css-backgrounds-3/#the-background-image
88
+ * ---
89
+ */
90
+ export const parseBackgroundImage = (
91
+ value : string ,
92
+ ) : BackgroundImageType | void => {
80
93
if ( value === 'none' ) {
81
94
return ;
82
95
}
@@ -112,7 +125,7 @@ const parseBackgroundImage = (value: string): BackgroundImageType | void => {
112
125
* @param {{width: number, height: number} } containerSize size of the container
113
126
* @return {{width: number, height: number} } actual image size
114
127
*/
115
- const getActualImageSize = (
128
+ export const getActualImageSize = (
116
129
backgroundSize : string ,
117
130
imageSize : { width : number ; height : number } ,
118
131
containerSize : { width : any ; height : any } ,
@@ -197,5 +210,3 @@ const getActualImageSize = (
197
210
height,
198
211
} ;
199
212
} ;
200
-
201
- export { parseBackgroundImage , getActualImageSize } ;
0 commit comments