@@ -4,43 +4,189 @@ const parsers = require("../parsers");
4
4
const borderWidth = require ( "./borderWidth" ) ;
5
5
const borderStyle = require ( "./borderStyle" ) ;
6
6
const borderColor = require ( "./borderColor" ) ;
7
+ const borderTop = require ( "./borderTop" ) ;
8
+ const borderRight = require ( "./borderRight" ) ;
9
+ const borderBottom = require ( "./borderBottom" ) ;
10
+ const borderLeft = require ( "./borderLeft" ) ;
11
+
12
+ const initialValues = new Map ( [
13
+ [ "border-width" , "medium" ] ,
14
+ [ "border-style" , "none" ] ,
15
+ [ "border-color" , "currentcolor" ]
16
+ ] ) ;
17
+
18
+ const positionShorthandFor = new Map ( [
19
+ [ "border-top" , borderTop ] ,
20
+ [ "border-right" , borderRight ] ,
21
+ [ "border-bottom" , borderBottom ] ,
22
+ [ "border-left" , borderLeft ]
23
+ ] ) ;
7
24
8
25
module . exports . shorthandFor = new Map ( [
9
26
[ "border-width" , borderWidth ] ,
10
27
[ "border-style" , borderStyle ] ,
11
28
[ "border-color" , borderColor ]
12
29
] ) ;
13
30
31
+ module . exports . parse = function parse ( v , opt = { } ) {
32
+ const { globalObject } = opt ;
33
+ if ( v === "" ) {
34
+ return v ;
35
+ }
36
+ const values = parsers . splitValue ( v ) ;
37
+ const parsedValues = new Map ( ) ;
38
+ for ( const val of values ) {
39
+ const value = parsers . parsePropertyValue ( "border" , val , {
40
+ globalObject,
41
+ inArray : true
42
+ } ) ;
43
+ if ( Array . isArray ( value ) && value . length === 1 ) {
44
+ const [ { isNumber, name, type, value : itemValue } ] = value ;
45
+ switch ( type ) {
46
+ case "Calc" : {
47
+ if ( isNumber || parsedValues . has ( "border-width" ) ) {
48
+ return ;
49
+ }
50
+ parsedValues . set ( "border-width" , `${ name } (${ itemValue } )` ) ;
51
+ break ;
52
+ }
53
+ case "Dimension" :
54
+ case "Number" : {
55
+ if ( parsedValues . has ( "border-width" ) ) {
56
+ return ;
57
+ }
58
+ const parsedValue = parsers . parseLength ( value , {
59
+ min : 0
60
+ } ) ;
61
+ if ( ! parsedValue ) {
62
+ return ;
63
+ }
64
+ parsedValues . set ( "border-width" , parsedValue ) ;
65
+ break ;
66
+ }
67
+ case "Function" : {
68
+ if ( parsedValues . has ( "border-color" ) ) {
69
+ return ;
70
+ }
71
+ const parsedValue = parsers . parseColor ( value ) ;
72
+ if ( ! parsedValue ) {
73
+ return ;
74
+ }
75
+ parsedValues . set ( "border-color" , parsedValue ) ;
76
+ break ;
77
+ }
78
+ case "GlobalKeyword" : {
79
+ if ( values . length !== 1 ) {
80
+ return ;
81
+ }
82
+ for ( const key of module . exports . shorthandFor . keys ( ) ) {
83
+ parsedValues . set ( key , name ) ;
84
+ }
85
+ break ;
86
+ }
87
+ case "Hash" : {
88
+ if ( parsedValues . has ( "border-color" ) ) {
89
+ return ;
90
+ }
91
+ const parsedValue = parsers . parseColor ( `#${ itemValue } ` ) ;
92
+ if ( ! parsedValue ) {
93
+ return ;
94
+ }
95
+ parsedValues . set ( "border-color" , parsedValue ) ;
96
+ break ;
97
+ }
98
+ case "Identifier" : {
99
+ if ( parsers . isValidPropertyValue ( "border-width" , name ) ) {
100
+ if ( parsedValues . has ( "border-width" ) ) {
101
+ return ;
102
+ }
103
+ parsedValues . set ( "border-width" , name ) ;
104
+ break ;
105
+ } else if ( parsers . isValidPropertyValue ( "border-style" , name ) ) {
106
+ if ( parsedValues . has ( "border-style" ) ) {
107
+ return ;
108
+ }
109
+ parsedValues . set ( "border-style" , name ) ;
110
+ break ;
111
+ } else if ( parsers . isValidPropertyValue ( "border-color" , name ) ) {
112
+ if ( parsedValues . has ( "border-color" ) ) {
113
+ return ;
114
+ }
115
+ parsedValues . set ( "border-color" , name ) ;
116
+ break ;
117
+ }
118
+ return ;
119
+ }
120
+ default : {
121
+ return ;
122
+ }
123
+ }
124
+ } else {
125
+ return ;
126
+ }
127
+ }
128
+ if ( parsedValues . size ) {
129
+ const keys = module . exports . shorthandFor . keys ( ) ;
130
+ const obj = { } ;
131
+ for ( const key of keys ) {
132
+ if ( parsedValues . has ( key ) ) {
133
+ obj [ key ] = parsedValues . get ( key ) ;
134
+ }
135
+ }
136
+ return obj ;
137
+ }
138
+ } ;
139
+
14
140
module . exports . definition = {
15
141
set ( v ) {
16
142
v = parsers . prepareValue ( v , this . _global ) ;
17
- if ( / ^ n o n e $ / i. test ( v ) ) {
18
- v = "" ;
19
- }
20
143
if ( parsers . hasVarFunc ( v ) ) {
21
144
for ( const [ key ] of module . exports . shorthandFor ) {
22
145
this . _setProperty ( key , "" ) ;
23
146
}
147
+ for ( const [ key , parser ] of positionShorthandFor ) {
148
+ this . _setProperty ( key , "" ) ;
149
+ for ( const [ subkey ] of parser . shorthandFor ) {
150
+ this . _setProperty ( subkey , "" ) ;
151
+ }
152
+ }
24
153
this . _setProperty ( "border" , v ) ;
154
+ this . _setProperty ( "border-image" , "none" ) ;
25
155
} else {
26
- this . _midShorthandSetter ( "border" , v , module . exports . shorthandFor , [
27
- "top" ,
28
- "right" ,
29
- "bottom" ,
30
- "left"
31
- ] ) ;
156
+ const obj = module . exports . parse ( v , {
157
+ globalObject : this . _global
158
+ } ) ;
159
+ if ( obj === "" ) {
160
+ for ( const [ key ] of module . exports . shorthandFor ) {
161
+ this . _setProperty ( key , "" ) ;
162
+ }
163
+ for ( const [ key , parser ] of positionShorthandFor ) {
164
+ this . _setProperty ( key , "" ) ;
165
+ for ( const [ subkey ] of parser . shorthandFor ) {
166
+ this . _setProperty ( subkey , "" ) ;
167
+ }
168
+ }
169
+ this . _setProperty ( "border" , "" ) ;
170
+ this . _setProperty ( "border-image" , "" ) ;
171
+ } else if ( obj ) {
172
+ const valueObj = Object . fromEntries ( initialValues ) ;
173
+ for ( const key of Object . keys ( obj ) ) {
174
+ valueObj [ key ] = obj [ key ] ;
175
+ }
176
+ const positions = [ "top" , "right" , "bottom" , "left" ] ;
177
+ for ( const [ key , value ] of Object . entries ( valueObj ) ) {
178
+ this . _setProperty ( key , value ) ;
179
+ const [ prefix , suffix ] = key . split ( "-" ) ;
180
+ const { parse : parser } = module . exports . shorthandFor . get ( key ) ;
181
+ this . _implicitSetter ( prefix , suffix , value , parser , positions ) ;
182
+ }
183
+ this . _setProperty ( "border" , [ ...Object . values ( obj ) ] . join ( " " ) ) ;
184
+ this . _setProperty ( "border-image" , "none" ) ;
185
+ }
32
186
}
33
187
} ,
34
188
get ( ) {
35
- let val = this . getPropertyValue ( "border" ) ;
36
- if ( parsers . hasVarFunc ( val ) ) {
37
- return val ;
38
- }
39
- val = this . _shorthandGetter ( "border" , module . exports . shorthandFor ) ;
40
- if ( parsers . hasVarFunc ( val ) ) {
41
- return "" ;
42
- }
43
- return val ;
189
+ return this . getPropertyValue ( "border" ) ;
44
190
} ,
45
191
enumerable : true ,
46
192
configurable : true
0 commit comments