@@ -129,10 +129,14 @@ function buildTestCases(testCases, testType) {
129
129
return results ;
130
130
}
131
131
132
+ function isAbsoluteLength ( unit ) {
133
+ return unit == "cm" || unit == "mm" || unit == "Q" || unit == "in" ||
134
+ unit == "pt" || unit == "pc" || unit == "px" ;
135
+ }
132
136
133
137
function buildPositionTests ( shape , valid , type , units ) {
134
138
var results = new Array ( ) ;
135
- var convert = type . indexOf ( 'computed' ) != - 1 ? true : false ;
139
+ var is_computed = type . indexOf ( 'computed' ) != - 1 ? true : false ;
136
140
137
141
if ( Object . prototype . toString . call ( units ) === '[object Array]' ) {
138
142
units . forEach ( function ( unit ) {
@@ -144,23 +148,47 @@ function buildPositionTests(shape, valid, type, units) {
144
148
validPositions . forEach ( function ( test ) {
145
149
var testCase = [ ] , testName , actual , expected ;
146
150
// skip if this isn't explicitly testing length units
147
- if ( ! ( type . indexOf ( 'lengthUnit' ) != - 1 && test [ 0 ] . indexOf ( "u1" ) == - 1 ) ) {
151
+ if ( ! ( type . indexOf ( 'lengthUnit' ) != - 1 && test [ 0 ] . indexOf ( "u1" ) == - 1 ) ) {
148
152
// actual
149
153
actual = shape + '(at ' + setUnit ( test [ 0 ] , false , units ) + ')' ;
150
154
155
+ let position = test [ 1 ] ;
156
+ let convert = is_computed ;
157
+ if ( ! is_computed ) {
158
+ // For specified values.
159
+ // Note: "[convert]" tag is used only for the specified
160
+ // value.
161
+ if ( position . includes ( '[convert]' ) ) {
162
+ // We should convert the absolute length into the
163
+ // canonical unit in calc(), for specified values.
164
+ // e.g.
165
+ // 1. "circle(at 1pt 50%)" serializes as
166
+ // "circle(at 1pt 50%)".
167
+ // 2. "circle(at calc(1pt) 50%)" serializes as
168
+ // "circle(at calc(1.33px) 50%)".
169
+ convert = isAbsoluteLength ( units ) ;
170
+ }
171
+ } else if ( test . length == 3 ) {
172
+ // Use the 3rd element as the expected computed value.
173
+ position = test [ 2 ] ;
174
+ }
175
+
176
+ // Remove the tag if there is.
177
+ position = position . replace ( '[convert] ' , '' ) ;
178
+
151
179
// expected
152
- // if(convert && shape == 'circle')
153
- // expected = shape + '(at ' + setUnit(test[1], convert, units) +')';
154
- // else if(convert && shape == 'ellipse')
155
- // expected = shape + '(at ' + setUnit(test[1], convert, units) +')';
156
- // else
157
- expected = shape + '(at ' + setUnit ( test [ 1 ] , convert , units ) + ')' ;
180
+ // if(convert && shape == 'circle')
181
+ // expected = shape + '(at ' + setUnit(test[1], convert, units) +')';
182
+ // else if(convert && shape == 'ellipse')
183
+ // expected = shape + '(at ' + setUnit(test[1], convert, units) +')';
184
+ // else
185
+ expected = shape + '(at ' + setUnit ( position , convert , units ) + ')' ;
158
186
159
187
// name
160
188
if ( type == 'lengthUnit + inline' )
161
189
testName = 'test unit (inline): ' + units + ' - ' + actual ;
162
190
else if ( type == 'lengthUnit + computed' )
163
- testName = 'test unit (computed): ' + units + ' - ' + actual ;
191
+ testName = 'test unit (computed): ' + units + ' - ' + actual ;
164
192
else
165
193
testName = ( actual + ' serializes as ' + expected + ' - ' + type ) ;
166
194
@@ -549,39 +577,35 @@ var validPositions = [
549
577
////// [ keyword length | keyword length], [ keyword length | keyword percent] x 5 keywords
550
578
[ "left 50% top 50%" , "50% 50%" ] ,
551
579
[ "left 50% top 50u1" , "50% 50u1" ] ,
552
- [ "left 50% bottom 70%" , "50% 30%" ] ,
553
- [ "left 50% bottom 70u1" , "left 50% bottom 70u1" ] ,
580
+ [ "left 50% bottom 70%" , "50% calc(30%)" , "50% 30%"] ,
581
+ [ "left 50% bottom 70u1" , "[convert] 50% calc(100% - 70u1) " ] ,
554
582
[ "left 50u1 top 50%" , "50u1 50%" ] ,
555
583
[ "left 50u1 top 50u1" , "50u1 50u1" ] ,
556
- [ "left 50u1 bottom 70%" , "50u1 30%" ] ,
557
- [ "left 50u1 bottom 70u1" , "left 50u1 bottom 70u1" ] ,
584
+ [ "left 50u1 bottom 70%" , "50u1 calc(30%)" , "50u1 30%" ] ,
558
585
559
586
[ "top 50% left 50%" , "50% 50%" ] ,
560
587
[ "top 50% left 50u1" , "50u1 50%" ] ,
561
- [ "top 50% right 80%" , "20% 50%" ] ,
562
- [ "top 50% right 80u1" , "right 80u1 top 50%" ] ,
588
+ [ "top 50% right 80%" , "calc(20%) 50%" , " 20% 50%"] ,
589
+ [ "top 50% right 80u1" , "[convert] calc(100% - 80u1) 50%" ] ,
563
590
[ "top 50u1 left 50%" , "50% 50u1" ] ,
564
591
[ "top 50u1 left 50u1" , "50u1 50u1" ] ,
565
- [ "top 50u1 right 80%" , "20% 50u1" ] ,
566
- [ "top 50u1 right 80u1" , "right 80u1 top 50u1" ] ,
567
-
568
- [ "bottom 70% left 50%" , "50% 30%" ] ,
569
- [ "bottom 70% left 50u1" , "50u1 30%" ] ,
570
- [ "bottom 70% right 80%" , "20% 30%" ] ,
571
- [ "bottom 70% right 80u1" , "right 80u1 top 30%" ] ,
572
- [ "bottom 70u1 left 50%" , "left 50% bottom 70u1" ] ,
573
- [ "bottom 70u1 left 50u1" , "left 50u1 bottom 70u1" ] ,
574
- [ "bottom 70u1 right 80%" , "left 20% bottom 70u1" ] ,
575
- [ "bottom 70u1 right 80u1" , "right 80u1 bottom 70u1" ] ,
576
-
577
- [ "right 80% top 50%" , "20% 50%" ] ,
578
- [ "right 80% top 50u1" , "20% 50u1" ] ,
579
- [ "right 80% bottom 70%" , "20% 30%" ] ,
580
- [ "right 80% bottom 70u1" , "left 20% bottom 70u1" ] ,
581
- [ "right 80u1 top 50%" , "right 80u1 top 50%" ] ,
582
- [ "right 80u1 top 50u1" , "right 80u1 top 50u1" ] ,
583
- [ "right 80u1 bottom 70%" , "right 80u1 top 30%" ] ,
584
- [ "right 80u1 bottom 70u1" , "right 80u1 bottom 70u1" ] ,
592
+ [ "top 50u1 right 80%" , "calc(20%) 50u1" , "20% 50u1" ] ,
593
+
594
+ [ "bottom 70% left 50%" , "50% calc(30%)" , "50% 30%" ] ,
595
+ [ "bottom 70% left 50u1" , "50u1 calc(30%)" , "50u1 30%" ] ,
596
+ [ "bottom 70% right 80%" , "calc(20%) calc(30%)" , "20% 30%" ] ,
597
+ [ "bottom 70% right 80u1" , "[convert] calc(100% - 80u1) calc(30%)" , "calc(100% - 80u1) 30%" ] ,
598
+ [ "bottom 70u1 left 50%" , "[convert] 50% calc(100% - 70u1)" ] ,
599
+ [ "bottom 70u1 right 50%" , "[convert] calc(50%) calc(100% - 70u1)" , "50% calc(100% - 70u1)" ] ,
600
+ [ "bottom 70u1 right 80u1" , "[convert] calc(100% - 80u1) calc(100% - 70u1)" ] ,
601
+
602
+ [ "right 80% top 50%" , "calc(20%) 50%" , "20% 50%" ] ,
603
+ [ "right 80% top 50u1" , "calc(20%) 50u1" , "20% 50u1" ] ,
604
+ [ "right 80% bottom 70%" , "calc(20%) calc(30%)" , "20% 30%" ] ,
605
+ [ "right 80% bottom 70u1" , "[convert] calc(20%) calc(100% - 70u1)" , "20% calc(100% - 70u1)" ] ,
606
+ [ "right 80u1 top 50%" , "[convert] calc(100% - 80u1) 50%" ] ,
607
+ [ "right 80u1 bottom 70%" , "[convert] calc(100% - 80u1) calc(30%)" , "calc(100% - 80u1) 30%" ] ,
608
+ [ "right 80u1 bottom 70u1" , "[convert] calc(100% - 80u1) calc(100% - 70u1)" ] ,
585
609
] ;
586
610
587
611
var invalidPositions = [
0 commit comments