4
4
*/
5
5
"use strict" ;
6
6
const allExtraProperties = require ( "./allExtraProperties" ) ;
7
- const { shorthandProperties } = require ( "./shorthandProperties" ) ;
8
7
const allProperties = require ( "./generated/allProperties" ) ;
9
8
const implementedProperties = require ( "./generated/implementedProperties" ) ;
10
9
const generatedProperties = require ( "./generated/properties" ) ;
11
10
const {
12
11
hasVarFunc,
12
+ isValidPropertyValue,
13
13
parseCSS,
14
14
parsePropertyValue,
15
15
parseShorthand,
16
16
prepareValue,
17
17
splitValue
18
18
} = require ( "./parsers" ) ;
19
+ const { shorthandProperties } = require ( "./shorthandProperties" ) ;
19
20
const { dashedToCamelCase } = require ( "./utils/camelize" ) ;
21
+ const {
22
+ borderProperties,
23
+ normalizeBorderProperties,
24
+ prepareBorderProperties
25
+ } = require ( "./utils/normalizeBorders" ) ;
20
26
const { getPropertyDescriptor } = require ( "./utils/propertyDescriptors" ) ;
21
27
const { asciiLowercase } = require ( "./utils/strings" ) ;
22
28
@@ -124,7 +130,6 @@ class CSSStyleDeclaration {
124
130
}
125
131
}
126
132
127
- // FIXME:
128
133
get cssText ( ) {
129
134
if ( this . _computed ) {
130
135
return "" ;
@@ -133,24 +138,31 @@ class CSSStyleDeclaration {
133
138
for ( let i = 0 ; i < this . _length ; i ++ ) {
134
139
const property = this [ i ] ;
135
140
const value = this . getPropertyValue ( property ) ;
136
- const priority = this . getPropertyPriority ( property ) ;
141
+ const priority = this . _priorities . get ( property ) ;
137
142
if ( priority === "important" ) {
138
- properties . set ( property , ` ${ property } : ${ value } ! ${ priority } ;` ) ;
143
+ properties . set ( property , { property, value, priority } ) ;
139
144
} else {
140
- properties . set ( property , `${ property } : ${ value } ;` ) ;
141
- }
142
- }
143
- for ( const [ property ] of properties ) {
144
- if ( shorthandProperties . has ( property ) ) {
145
- const longhandProperties = shorthandProperties . get ( property ) ;
146
- for ( const [ longhand ] of longhandProperties ) {
147
- if ( properties . has ( longhand ) && ! this . getPropertyPriority ( longhand ) ) {
148
- properties . delete ( longhand ) ;
145
+ if ( shorthandProperties . has ( property ) ) {
146
+ const longhandProperties = shorthandProperties . get ( property ) ;
147
+ for ( const [ longhand ] of longhandProperties ) {
148
+ if ( properties . has ( longhand ) && ! this . _priorities . get ( longhand ) ) {
149
+ properties . delete ( longhand ) ;
150
+ }
149
151
}
150
152
}
153
+ properties . set ( property , { property, value, priority : null } ) ;
151
154
}
152
155
}
153
- return [ ...properties . values ( ) ] . join ( " " ) ;
156
+ const normalizedProperties = normalizeBorderProperties ( properties ) ;
157
+ const parts = [ ] ;
158
+ for ( const { property, value, priority } of normalizedProperties . values ( ) ) {
159
+ if ( priority ) {
160
+ parts . push ( `${ property } : ${ value } !${ priority } ;` ) ;
161
+ } else {
162
+ parts . push ( `${ property } : ${ value } ;` ) ;
163
+ }
164
+ }
165
+ return parts . join ( " " ) ;
154
166
}
155
167
156
168
set cssText ( val ) {
@@ -333,20 +345,37 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
333
345
/**
334
346
* @param {string } property
335
347
* @param {object } shorthandFor
348
+ * @param {Map } initialValues
336
349
*/
337
- value ( property , shorthandFor ) {
338
- const values = [ ] ;
350
+ value ( property , shorthandFor , initialValues = new Map ( ) ) {
351
+ const obj = { } ;
352
+ const filter = initialValues . size > 0 ;
353
+ const firstKey = filter && initialValues . keys ( ) . next ( ) . value ;
339
354
for ( const key of shorthandFor . keys ( ) ) {
340
355
const val = this . getPropertyValue ( key ) ;
341
- if ( hasVarFunc ( val ) ) {
356
+ if ( val === "" || hasVarFunc ( val ) ) {
342
357
return "" ;
343
358
}
344
- if ( val !== "" ) {
345
- values . push ( val ) ;
359
+ if ( filter ) {
360
+ const initialValue = initialValues . get ( key ) ;
361
+ if ( key === firstKey ) {
362
+ obj [ key ] = val ;
363
+ } else if ( val !== initialValue ) {
364
+ obj [ key ] = val ;
365
+ if ( obj [ firstKey ] && obj [ firstKey ] === initialValues . get ( firstKey ) ) {
366
+ delete obj [ firstKey ] ;
367
+ }
368
+ }
369
+ } else {
370
+ obj [ key ] = val ;
346
371
}
347
372
}
348
- if ( values . length ) {
349
- return values . join ( " " ) ;
373
+ if ( Object . values ( obj ) . length ) {
374
+ const value = Object . values ( obj ) . join ( " " ) ;
375
+ if ( isValidPropertyValue ( property , value ) ) {
376
+ return value ;
377
+ }
378
+ return "" ;
350
379
}
351
380
if ( this . _values . has ( property ) ) {
352
381
return this . getPropertyValue ( property ) ;
@@ -358,13 +387,15 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
358
387
359
388
_implicitGetter : {
360
389
/**
361
- * @param {string } property
390
+ * @param {string } prefix
391
+ * @param {string } part
362
392
* @param {Array.<string> } positions
363
393
*/
364
- value ( property , positions = [ ] ) {
394
+ value ( prefix , part , positions = [ ] ) {
395
+ const suffix = part ? `-${ part } ` : "" ;
365
396
const values = [ ] ;
366
397
for ( const position of positions ) {
367
- const val = this . getPropertyValue ( `${ property } -${ position } ` ) ;
398
+ const val = this . getPropertyValue ( `${ prefix } -${ position } ${ suffix } ` ) ;
368
399
if ( val === "" || hasVarFunc ( val ) ) {
369
400
return "" ;
370
401
}
@@ -498,16 +529,17 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
498
529
/**
499
530
* @param {string } prefix
500
531
* @param {string } part
501
- * @param {string } val
532
+ * @param {string|Array.<string> } val
502
533
* @param {Function } parser
503
534
* @param {Array.<string> } positions
504
535
*/
505
536
value ( prefix , part , val , parser , positions = [ ] ) {
506
537
const suffix = part ? `-${ part } ` : "" ;
507
- const shorthandProp = `${ prefix } ${ suffix } ` ;
508
538
const values = [ ] ;
509
539
if ( val === "" ) {
510
540
values . push ( val ) ;
541
+ } else if ( Array . isArray ( val ) && val . length ) {
542
+ values . push ( ...val ) ;
511
543
} else {
512
544
const parsedValue = parser ( val , {
513
545
globalObject : this . _global
@@ -520,29 +552,43 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
520
552
if ( ! values . length || values . length > positions . length ) {
521
553
return ;
522
554
}
523
- this . _setProperty ( shorthandProp , values . join ( " " ) ) ;
555
+ const shorthandProp = `${ prefix } ${ suffix } ` ;
556
+ const shorthandVal = values . join ( " " ) ;
557
+ const positionValues = [ ...values ] ;
524
558
switch ( positions . length ) {
525
- case 4 :
559
+ case 4 : {
526
560
if ( values . length === 1 ) {
527
- values . push ( values [ 0 ] , values [ 0 ] , values [ 0 ] ) ;
561
+ positionValues . push ( values [ 0 ] , values [ 0 ] , values [ 0 ] ) ;
528
562
} else if ( values . length === 2 ) {
529
- values . push ( values [ 0 ] , values [ 1 ] ) ;
563
+ positionValues . push ( values [ 0 ] , values [ 1 ] ) ;
530
564
} else if ( values . length === 3 ) {
531
- values . push ( values [ 1 ] ) ;
565
+ positionValues . push ( values [ 1 ] ) ;
532
566
}
533
567
break ;
534
- case 2 :
568
+ }
569
+ case 2 : {
535
570
if ( values . length === 1 ) {
536
- values . push ( values [ 0 ] ) ;
571
+ positionValues . push ( values [ 0 ] ) ;
537
572
}
538
573
break ;
574
+ }
539
575
default :
540
576
}
577
+ const longhandValues = [ ] ;
578
+ for ( const position of positions ) {
579
+ const property = `${ prefix } -${ position } ${ suffix } ` ;
580
+ const longhandValue = this . getPropertyValue ( property ) ;
581
+ const longhandPriority = this . _priorities . get ( property ) ;
582
+ longhandValues . push ( [ longhandValue , longhandPriority ] ) ;
583
+ }
541
584
for ( let i = 0 ; i < positions . length ; i ++ ) {
542
585
const property = `${ prefix } -${ positions [ i ] } ${ suffix } ` ;
586
+ const [ longhandValue , longhandPriority ] = longhandValues [ i ] ;
587
+ const longhandVal = longhandPriority ? longhandValue : positionValues [ i ] ;
543
588
this . removeProperty ( property ) ;
544
- this . _values . set ( property , values [ i ] ) ;
589
+ this . _values . set ( property , longhandVal ) ;
545
590
}
591
+ this . _setProperty ( shorthandProp , shorthandVal ) ;
546
592
} ,
547
593
enumerable : false
548
594
} ,
@@ -568,13 +614,13 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
568
614
}
569
615
const property = `${ prefix } -${ part } ` ;
570
616
this . _setProperty ( property , parsedValue ) ;
571
- const combinedPriority = this . getPropertyPriority ( prefix ) ;
617
+ const combinedPriority = this . _priorities . get ( prefix ) ;
572
618
const subparts = [ ] ;
573
619
for ( const position of positions ) {
574
620
subparts . push ( `${ prefix } -${ position } ` ) ;
575
621
}
576
622
const parts = subparts . map ( ( subpart ) => this . _values . get ( subpart ) ) ;
577
- const priorities = subparts . map ( ( subpart ) => this . getPropertyPriority ( subpart ) ) ;
623
+ const priorities = subparts . map ( ( subpart ) => this . _priorities . get ( subpart ) ) ;
578
624
const [ priority ] = priorities ;
579
625
// Combine into a single property if all values are set and have the same
580
626
// priority.
@@ -599,6 +645,33 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
599
645
}
600
646
} ,
601
647
enumerable : false
648
+ } ,
649
+
650
+ _implicitBorderSetter : {
651
+ /**
652
+ * @param {string } prop
653
+ * @param {string } val
654
+ */
655
+ value ( prop , val ) {
656
+ const properties = new Map ( ) ;
657
+ if ( prop !== "border" ) {
658
+ for ( let i = 0 ; i < this . _length ; i ++ ) {
659
+ const property = this [ i ] ;
660
+ if ( borderProperties . has ( property ) ) {
661
+ const value = this . getPropertyValue ( property ) ;
662
+ properties . set ( property , { property, value, priority : null } ) ;
663
+ }
664
+ }
665
+ }
666
+ const parsedProperties = prepareBorderProperties ( prop , val , properties , {
667
+ globalObject : this . _global
668
+ } ) ;
669
+ for ( const [ property , item ] of parsedProperties ) {
670
+ const { value } = item ;
671
+ this . _setProperty ( property , value ) ;
672
+ }
673
+ } ,
674
+ enumerable : false
602
675
}
603
676
} ) ;
604
677
0 commit comments