-
Notifications
You must be signed in to change notification settings - Fork 1
/
PIE_uncompressed.htc
4505 lines (3969 loc) · 167 KB
/
PIE_uncompressed.htc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
PIE: CSS3 rendering for IE
Version 1.0.0
http://css3pie.com
Dual-licensed for use under the Apache License Version 2.0 or the General Public License (GPL) Version 2.
-->
<PUBLIC:COMPONENT lightWeight="true">
<!-- saved from url=(0014)about:internet -->
<PUBLIC:ATTACH EVENT="oncontentready" FOR="element" ONEVENT="init()" />
<PUBLIC:ATTACH EVENT="ondocumentready" FOR="element" ONEVENT="init()" />
<PUBLIC:ATTACH EVENT="ondetach" FOR="element" ONEVENT="cleanup()" />
<script type="text/javascript">
var doc = element.document;var PIE = window['PIE'];
if( !PIE ) {
PIE = window['PIE'] = {
CSS_PREFIX: '-pie-',
STYLE_PREFIX: 'Pie',
CLASS_PREFIX: 'pie_',
tableCellTags: {
'TD': 1,
'TH': 1
},
/**
* Lookup table of elements which cannot take custom children.
*/
childlessElements: {
'TABLE':1,
'THEAD':1,
'TBODY':1,
'TFOOT':1,
'TR':1,
'INPUT':1,
'TEXTAREA':1,
'SELECT':1,
'OPTION':1,
'IMG':1,
'HR':1
},
/**
* Elements that can receive user focus
*/
focusableElements: {
'A':1,
'INPUT':1,
'TEXTAREA':1,
'SELECT':1,
'BUTTON':1
},
/**
* Values of the type attribute for input elements displayed as buttons
*/
inputButtonTypes: {
'submit':1,
'button':1,
'reset':1
},
emptyFn: function() {}
};
// Force the background cache to be used. No reason it shouldn't be.
try {
doc.execCommand( 'BackgroundImageCache', false, true );
} catch(e) {}
(function() {
/*
* IE version detection approach by James Padolsey, with modifications -- from
* http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments/
*/
var ieVersion = 4,
div = doc.createElement('div'),
all = div.getElementsByTagName('i'),
shape;
while (
div.innerHTML = '<!--[if gt IE ' + (++ieVersion) + ']><i></i><![endif]-->',
all[0]
) {}
PIE.ieVersion = ieVersion;
// Detect IE6
if( ieVersion === 6 ) {
// IE6 can't access properties with leading dash, but can without it.
PIE.CSS_PREFIX = PIE.CSS_PREFIX.replace( /^-/, '' );
}
PIE.ieDocMode = doc.documentMode || PIE.ieVersion;
// Detect VML support (a small number of IE installs don't have a working VML engine)
div.innerHTML = '<v:shape adj="1"/>';
shape = div.firstChild;
shape.style['behavior'] = 'url(#default#VML)';
PIE.supportsVML = (typeof shape['adj'] === "object");
}());
/**
* Utility functions
*/
(function() {
var vmlCreatorDoc,
idNum = 0,
imageSizes = {};
PIE.Util = {
/**
* To create a VML element, it must be created by a Document which has the VML
* namespace set. Unfortunately, if you try to add the namespace programatically
* into the main document, you will get an "Unspecified error" when trying to
* access document.namespaces before the document is finished loading. To get
* around this, we create a DocumentFragment, which in IE land is apparently a
* full-fledged Document. It allows adding namespaces immediately, so we add the
* namespace there and then have it create the VML element.
* @param {string} tag The tag name for the VML element
* @return {Element} The new VML element
*/
createVmlElement: function( tag ) {
var vmlPrefix = 'css3vml';
if( !vmlCreatorDoc ) {
vmlCreatorDoc = doc.createDocumentFragment();
vmlCreatorDoc.namespaces.add( vmlPrefix, 'urn:schemas-microsoft-com:vml' );
}
return vmlCreatorDoc.createElement( vmlPrefix + ':' + tag );
},
/**
* Generate and return a unique ID for a given object. The generated ID is stored
* as a property of the object for future reuse.
* @param {Object} obj
*/
getUID: function( obj ) {
return obj && obj[ '_pieId' ] || ( obj[ '_pieId' ] = '_' + ++idNum );
},
/**
* Simple utility for merging objects
* @param {Object} obj1 The main object into which all others will be merged
* @param {...Object} var_args Other objects which will be merged into the first, in order
*/
merge: function( obj1 ) {
var i, len, p, objN, args = arguments;
for( i = 1, len = args.length; i < len; i++ ) {
objN = args[i];
for( p in objN ) {
if( objN.hasOwnProperty( p ) ) {
obj1[ p ] = objN[ p ];
}
}
}
return obj1;
},
/**
* Execute a callback function, passing it the dimensions of a given image once
* they are known.
* @param {string} src The source URL of the image
* @param {function({w:number, h:number})} func The callback function to be called once the image dimensions are known
* @param {Object} ctx A context object which will be used as the 'this' value within the executed callback function
*/
withImageSize: function( src, func, ctx ) {
var size = imageSizes[ src ], img, queue;
if( size ) {
// If we have a queue, add to it
if( Object.prototype.toString.call( size ) === '[object Array]' ) {
size.push( [ func, ctx ] );
}
// Already have the size cached, call func right away
else {
func.call( ctx, size );
}
} else {
queue = imageSizes[ src ] = [ [ func, ctx ] ]; //create queue
img = new Image();
img.onload = function() {
size = imageSizes[ src ] = { w: img.width, h: img.height };
for( var i = 0, len = queue.length; i < len; i++ ) {
queue[ i ][ 0 ].call( queue[ i ][ 1 ], size );
}
img.onload = null;
};
img.src = src;
}
}
};
})();/**
* Utility functions for handling gradients
*/
PIE.GradientUtil = {
getGradientMetrics: function( el, width, height, gradientInfo ) {
var angle = gradientInfo.angle,
startPos = gradientInfo.gradientStart,
startX, startY,
endX, endY,
startCornerX, startCornerY,
endCornerX, endCornerY,
deltaX, deltaY,
p, UNDEF;
// Find the "start" and "end" corners; these are the corners furthest along the gradient line.
// This is used below to find the start/end positions of the CSS3 gradient-line, and also in finding
// the total length of the VML rendered gradient-line corner to corner.
function findCorners() {
startCornerX = ( angle >= 90 && angle < 270 ) ? width : 0;
startCornerY = angle < 180 ? height : 0;
endCornerX = width - startCornerX;
endCornerY = height - startCornerY;
}
// Normalize the angle to a value between [0, 360)
function normalizeAngle() {
while( angle < 0 ) {
angle += 360;
}
angle = angle % 360;
}
// Find the start and end points of the gradient
if( startPos ) {
startPos = startPos.coords( el, width, height );
startX = startPos.x;
startY = startPos.y;
}
if( angle ) {
angle = angle.degrees();
normalizeAngle();
findCorners();
// If no start position was specified, then choose a corner as the starting point.
if( !startPos ) {
startX = startCornerX;
startY = startCornerY;
}
// Find the end position by extending a perpendicular line from the gradient-line which
// intersects the corner opposite from the starting corner.
p = PIE.GradientUtil.perpendicularIntersect( startX, startY, angle, endCornerX, endCornerY );
endX = p[0];
endY = p[1];
}
else if( startPos ) {
// Start position but no angle specified: find the end point by rotating 180deg around the center
endX = width - startX;
endY = height - startY;
}
else {
// Neither position nor angle specified; create vertical gradient from top to bottom
startX = startY = endX = 0;
endY = height;
}
deltaX = endX - startX;
deltaY = endY - startY;
if( angle === UNDEF ) {
// Get the angle based on the change in x/y from start to end point. Checks first for horizontal
// or vertical angles so they get exact whole numbers rather than what atan2 gives.
angle = ( !deltaX ? ( deltaY < 0 ? 90 : 270 ) :
( !deltaY ? ( deltaX < 0 ? 180 : 0 ) :
-Math.atan2( deltaY, deltaX ) / Math.PI * 180
)
);
normalizeAngle();
findCorners();
}
return {
angle: angle,
startX: startX,
startY: startY,
endX: endX,
endY: endY,
startCornerX: startCornerX,
startCornerY: startCornerY,
endCornerX: endCornerX,
endCornerY: endCornerY,
deltaX: deltaX,
deltaY: deltaY,
lineLength: PIE.GradientUtil.distance( startX, startY, endX, endY )
}
},
/**
* Find the point along a given line (defined by a starting point and an angle), at which
* that line is intersected by a perpendicular line extending through another point.
* @param x1 - x coord of the starting point
* @param y1 - y coord of the starting point
* @param angle - angle of the line extending from the starting point (in degrees)
* @param x2 - x coord of point along the perpendicular line
* @param y2 - y coord of point along the perpendicular line
* @return [ x, y ]
*/
perpendicularIntersect: function( x1, y1, angle, x2, y2 ) {
// Handle straight vertical and horizontal angles, for performance and to avoid
// divide-by-zero errors.
if( angle === 0 || angle === 180 ) {
return [ x2, y1 ];
}
else if( angle === 90 || angle === 270 ) {
return [ x1, y2 ];
}
else {
// General approach: determine the Ax+By=C formula for each line (the slope of the second
// line is the negative inverse of the first) and then solve for where both formulas have
// the same x/y values.
var a1 = Math.tan( -angle * Math.PI / 180 ),
c1 = a1 * x1 - y1,
a2 = -1 / a1,
c2 = a2 * x2 - y2,
d = a2 - a1,
endX = ( c2 - c1 ) / d,
endY = ( a1 * c2 - a2 * c1 ) / d;
return [ endX, endY ];
}
},
/**
* Find the distance between two points
* @param {Number} p1x
* @param {Number} p1y
* @param {Number} p2x
* @param {Number} p2y
* @return {Number} the distance
*/
distance: function( p1x, p1y, p2x, p2y ) {
var dx = p2x - p1x,
dy = p2y - p1y;
return Math.abs(
dx === 0 ? dy :
dy === 0 ? dx :
Math.sqrt( dx * dx + dy * dy )
);
}
};/**
*
*/
PIE.Observable = function() {
/**
* List of registered observer functions
*/
this.observers = [];
/**
* Hash of function ids to their position in the observers list, for fast lookup
*/
this.indexes = {};
};
PIE.Observable.prototype = {
observe: function( fn ) {
var id = PIE.Util.getUID( fn ),
indexes = this.indexes,
observers = this.observers;
if( !( id in indexes ) ) {
indexes[ id ] = observers.length;
observers.push( fn );
}
},
unobserve: function( fn ) {
var id = PIE.Util.getUID( fn ),
indexes = this.indexes;
if( id && id in indexes ) {
delete this.observers[ indexes[ id ] ];
delete indexes[ id ];
}
},
fire: function() {
var o = this.observers,
i = o.length;
while( i-- ) {
o[ i ] && o[ i ]();
}
}
};/*
* Simple heartbeat timer - this is a brute-force workaround for syncing issues caused by IE not
* always firing the onmove and onresize events when elements are moved or resized. We check a few
* times every second to make sure the elements have the correct position and size. See Element.js
* which adds heartbeat listeners based on the custom -pie-poll flag, which defaults to true in IE8-9
* and false elsewhere.
*/
PIE.Heartbeat = new PIE.Observable();
PIE.Heartbeat.run = function() {
var me = this,
interval;
if( !me.running ) {
interval = doc.documentElement.currentStyle.getAttribute( PIE.CSS_PREFIX + 'poll-interval' ) || 250;
(function beat() {
me.fire();
setTimeout(beat, interval);
})();
me.running = 1;
}
};
/**
* Create an observable listener for the onunload event
*/
(function() {
PIE.OnUnload = new PIE.Observable();
function handleUnload() {
PIE.OnUnload.fire();
window.detachEvent( 'onunload', handleUnload );
window[ 'PIE' ] = null;
}
window.attachEvent( 'onunload', handleUnload );
/**
* Attach an event which automatically gets detached onunload
*/
PIE.OnUnload.attachManagedEvent = function( target, name, handler ) {
target.attachEvent( name, handler );
this.observe( function() {
target.detachEvent( name, handler );
} );
};
})()/**
* Create a single observable listener for window resize events.
*/
PIE.OnResize = new PIE.Observable();
PIE.OnUnload.attachManagedEvent( window, 'onresize', function() { PIE.OnResize.fire(); } );
/**
* Create a single observable listener for scroll events. Used for lazy loading based
* on the viewport, and for fixed position backgrounds.
*/
(function() {
PIE.OnScroll = new PIE.Observable();
function scrolled() {
PIE.OnScroll.fire();
}
PIE.OnUnload.attachManagedEvent( window, 'onscroll', scrolled );
PIE.OnResize.observe( scrolled );
})();
/**
* Listen for printing events, destroy all active PIE instances when printing, and
* restore them afterward.
*/
(function() {
var elements;
function beforePrint() {
elements = PIE.Element.destroyAll();
}
function afterPrint() {
if( elements ) {
for( var i = 0, len = elements.length; i < len; i++ ) {
PIE[ 'attach' ]( elements[i] );
}
elements = 0;
}
}
if( PIE.ieDocMode < 9 ) {
PIE.OnUnload.attachManagedEvent( window, 'onbeforeprint', beforePrint );
PIE.OnUnload.attachManagedEvent( window, 'onafterprint', afterPrint );
}
})();/**
* Create a single observable listener for document mouseup events.
*/
PIE.OnMouseup = new PIE.Observable();
PIE.OnUnload.attachManagedEvent( doc, 'onmouseup', function() { PIE.OnMouseup.fire(); } );
/**
* Wrapper for length and percentage style values. The value is immutable. A singleton instance per unique
* value is returned from PIE.getLength() - always use that instead of instantiating directly.
* @constructor
* @param {string} val The CSS string representing the length. It is assumed that this will already have
* been validated as a valid length or percentage syntax.
*/
PIE.Length = (function() {
var lengthCalcEl = doc.createElement( 'length-calc' ),
parent = doc.body || doc.documentElement,
s = lengthCalcEl.style,
conversions = {},
units = [ 'mm', 'cm', 'in', 'pt', 'pc' ],
i = units.length,
instances = {};
s.position = 'absolute';
s.top = s.left = '-9999px';
parent.appendChild( lengthCalcEl );
while( i-- ) {
s.width = '100' + units[i];
conversions[ units[i] ] = lengthCalcEl.offsetWidth / 100;
}
parent.removeChild( lengthCalcEl );
// All calcs from here on will use 1em
s.width = '1em';
function Length( val ) {
this.val = val;
}
Length.prototype = {
/**
* Regular expression for matching the length unit
* @private
*/
unitRE: /(px|em|ex|mm|cm|in|pt|pc|%)$/,
/**
* Get the numeric value of the length
* @return {number} The value
*/
getNumber: function() {
var num = this.num,
UNDEF;
if( num === UNDEF ) {
num = this.num = parseFloat( this.val );
}
return num;
},
/**
* Get the unit of the length
* @return {string} The unit
*/
getUnit: function() {
var unit = this.unit,
m;
if( !unit ) {
m = this.val.match( this.unitRE );
unit = this.unit = ( m && m[0] ) || 'px';
}
return unit;
},
/**
* Determine whether this is a percentage length value
* @return {boolean}
*/
isPercentage: function() {
return this.getUnit() === '%';
},
/**
* Resolve this length into a number of pixels.
* @param {Element} el - the context element, used to resolve font-relative values
* @param {(function():number|number)=} pct100 - the number of pixels that equal a 100% percentage. This can be either a number or a
* function which will be called to return the number.
*/
pixels: function( el, pct100 ) {
var num = this.getNumber(),
unit = this.getUnit();
switch( unit ) {
case "px":
return num;
case "%":
return num * ( typeof pct100 === 'function' ? pct100() : pct100 ) / 100;
case "em":
return num * this.getEmPixels( el );
case "ex":
return num * this.getEmPixels( el ) / 2;
default:
return num * conversions[ unit ];
}
},
/**
* The em and ex units are relative to the font-size of the current element,
* however if the font-size is set using non-pixel units then we get that value
* rather than a pixel conversion. To get around this, we keep a floating element
* with width:1em which we insert into the target element and then read its offsetWidth.
* For elements that won't accept a child we insert into the parent node and perform
* additional calculation. If the font-size *is* specified in pixels, then we use that
* directly to avoid the expensive DOM manipulation.
* @param {Element} el
* @return {number}
*/
getEmPixels: function( el ) {
var fs = el.currentStyle.fontSize,
px, parent, me;
if( fs.indexOf( 'px' ) > 0 ) {
return parseFloat( fs );
}
else if( el.tagName in PIE.childlessElements ) {
me = this;
parent = el.parentNode;
return PIE.getLength( fs ).pixels( parent, function() {
return me.getEmPixels( parent );
} );
}
else {
el.appendChild( lengthCalcEl );
px = lengthCalcEl.offsetWidth;
if( lengthCalcEl.parentNode === el ) { //not sure how this could be false but it sometimes is
el.removeChild( lengthCalcEl );
}
return px;
}
}
};
/**
* Retrieve a PIE.Length instance for the given value. A shared singleton instance is returned for each unique value.
* @param {string} val The CSS string representing the length. It is assumed that this will already have
* been validated as a valid length or percentage syntax.
*/
PIE.getLength = function( val ) {
return instances[ val ] || ( instances[ val ] = new Length( val ) );
};
return Length;
})();
/**
* Wrapper for a CSS3 bg-position value. Takes up to 2 position keywords and 2 lengths/percentages.
* @constructor
* @param {Array.<PIE.Tokenizer.Token>} tokens The tokens making up the background position value.
*/
PIE.BgPosition = (function() {
var length_fifty = PIE.getLength( '50%' ),
vert_idents = { 'top': 1, 'center': 1, 'bottom': 1 },
horiz_idents = { 'left': 1, 'center': 1, 'right': 1 };
function BgPosition( tokens ) {
this.tokens = tokens;
}
BgPosition.prototype = {
/**
* Normalize the values into the form:
* [ xOffsetSide, xOffsetLength, yOffsetSide, yOffsetLength ]
* where: xOffsetSide is either 'left' or 'right',
* yOffsetSide is either 'top' or 'bottom',
* and x/yOffsetLength are both PIE.Length objects.
* @return {Array}
*/
getValues: function() {
if( !this._values ) {
var tokens = this.tokens,
len = tokens.length,
Tokenizer = PIE.Tokenizer,
identType = Tokenizer.Type,
length_zero = PIE.getLength( '0' ),
type_ident = identType.IDENT,
type_length = identType.LENGTH,
type_percent = identType.PERCENT,
type, value,
vals = [ 'left', length_zero, 'top', length_zero ];
// If only one value, the second is assumed to be 'center'
if( len === 1 ) {
tokens.push( new Tokenizer.Token( type_ident, 'center' ) );
len++;
}
// Two values - CSS2
if( len === 2 ) {
// If both idents, they can appear in either order, so switch them if needed
if( type_ident & ( tokens[0].tokenType | tokens[1].tokenType ) &&
tokens[0].tokenValue in vert_idents && tokens[1].tokenValue in horiz_idents ) {
tokens.push( tokens.shift() );
}
if( tokens[0].tokenType & type_ident ) {
if( tokens[0].tokenValue === 'center' ) {
vals[1] = length_fifty;
} else {
vals[0] = tokens[0].tokenValue;
}
}
else if( tokens[0].isLengthOrPercent() ) {
vals[1] = PIE.getLength( tokens[0].tokenValue );
}
if( tokens[1].tokenType & type_ident ) {
if( tokens[1].tokenValue === 'center' ) {
vals[3] = length_fifty;
} else {
vals[2] = tokens[1].tokenValue;
}
}
else if( tokens[1].isLengthOrPercent() ) {
vals[3] = PIE.getLength( tokens[1].tokenValue );
}
}
// Three or four values - CSS3
else {
// TODO
}
this._values = vals;
}
return this._values;
},
/**
* Find the coordinates of the background image from the upper-left corner of the background area.
* Note that these coordinate values are not rounded.
* @param {Element} el
* @param {number} width - the width for percentages (background area width minus image width)
* @param {number} height - the height for percentages (background area height minus image height)
* @return {Object} { x: Number, y: Number }
*/
coords: function( el, width, height ) {
var vals = this.getValues(),
pxX = vals[1].pixels( el, width ),
pxY = vals[3].pixels( el, height );
return {
x: vals[0] === 'right' ? width - pxX : pxX,
y: vals[2] === 'bottom' ? height - pxY : pxY
};
}
};
return BgPosition;
})();
/**
* Wrapper for a CSS3 background-size value.
* @constructor
* @param {String|PIE.Length} w The width parameter
* @param {String|PIE.Length} h The height parameter, if any
*/
PIE.BgSize = (function() {
var CONTAIN = 'contain',
COVER = 'cover',
AUTO = 'auto';
function BgSize( w, h ) {
this.w = w;
this.h = h;
}
BgSize.prototype = {
pixels: function( el, areaW, areaH, imgW, imgH ) {
var me = this,
w = me.w,
h = me.h,
areaRatio = areaW / areaH,
imgRatio = imgW / imgH;
if ( w === CONTAIN ) {
w = imgRatio > areaRatio ? areaW : areaH * imgRatio;
h = imgRatio > areaRatio ? areaW / imgRatio : areaH;
}
else if ( w === COVER ) {
w = imgRatio < areaRatio ? areaW : areaH * imgRatio;
h = imgRatio < areaRatio ? areaW / imgRatio : areaH;
}
else if ( w === AUTO ) {
h = ( h === AUTO ? imgH : h.pixels( el, areaH ) );
w = h * imgRatio;
}
else {
w = w.pixels( el, areaW );
h = ( h === AUTO ? w / imgRatio : h.pixels( el, areaH ) );
}
return { w: w, h: h };
}
};
BgSize.DEFAULT = new BgSize( AUTO, AUTO );
return BgSize;
})();
/**
* Wrapper for angle values; handles conversion to degrees from all allowed angle units
* @constructor
* @param {string} val The raw CSS value for the angle. It is assumed it has been pre-validated.
*/
PIE.Angle = (function() {
function Angle( val ) {
this.val = val;
}
Angle.prototype = {
unitRE: /[a-z]+$/i,
/**
* @return {string} The unit of the angle value
*/
getUnit: function() {
return this._unit || ( this._unit = this.val.match( this.unitRE )[0].toLowerCase() );
},
/**
* Get the numeric value of the angle in degrees.
* @return {number} The degrees value
*/
degrees: function() {
var deg = this._deg, u, n;
if( deg === undefined ) {
u = this.getUnit();
n = parseFloat( this.val, 10 );
deg = this._deg = ( u === 'deg' ? n : u === 'rad' ? n / Math.PI * 180 : u === 'grad' ? n / 400 * 360 : u === 'turn' ? n * 360 : 0 );
}
return deg;
}
};
return Angle;
})();/**
* Abstraction for colors values. Allows detection of rgba values. A singleton instance per unique
* value is returned from PIE.getColor() - always use that instead of instantiating directly.
* @constructor
* @param {string} val The raw CSS string value for the color
*/
PIE.Color = (function() {
var instances = {};
function Color( val ) {
this.val = val;
}
/**
* Regular expression for matching rgba colors and extracting their components
* @type {RegExp}
*/
Color.rgbaRE = /\s*rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d+|\d*\.\d+)\s*\)\s*/;
Color.names = {
"aliceblue":"F0F8FF", "antiquewhite":"FAEBD7", "aqua":"0FF",
"aquamarine":"7FFFD4", "azure":"F0FFFF", "beige":"F5F5DC",
"bisque":"FFE4C4", "black":"000", "blanchedalmond":"FFEBCD",
"blue":"00F", "blueviolet":"8A2BE2", "brown":"A52A2A",
"burlywood":"DEB887", "cadetblue":"5F9EA0", "chartreuse":"7FFF00",
"chocolate":"D2691E", "coral":"FF7F50", "cornflowerblue":"6495ED",
"cornsilk":"FFF8DC", "crimson":"DC143C", "cyan":"0FF",
"darkblue":"00008B", "darkcyan":"008B8B", "darkgoldenrod":"B8860B",
"darkgray":"A9A9A9", "darkgreen":"006400", "darkkhaki":"BDB76B",
"darkmagenta":"8B008B", "darkolivegreen":"556B2F", "darkorange":"FF8C00",
"darkorchid":"9932CC", "darkred":"8B0000", "darksalmon":"E9967A",
"darkseagreen":"8FBC8F", "darkslateblue":"483D8B", "darkslategray":"2F4F4F",
"darkturquoise":"00CED1", "darkviolet":"9400D3", "deeppink":"FF1493",
"deepskyblue":"00BFFF", "dimgray":"696969", "dodgerblue":"1E90FF",
"firebrick":"B22222", "floralwhite":"FFFAF0", "forestgreen":"228B22",
"fuchsia":"F0F", "gainsboro":"DCDCDC", "ghostwhite":"F8F8FF",
"gold":"FFD700", "goldenrod":"DAA520", "gray":"808080",
"green":"008000", "greenyellow":"ADFF2F", "honeydew":"F0FFF0",
"hotpink":"FF69B4", "indianred":"CD5C5C", "indigo":"4B0082",
"ivory":"FFFFF0", "khaki":"F0E68C", "lavender":"E6E6FA",
"lavenderblush":"FFF0F5", "lawngreen":"7CFC00", "lemonchiffon":"FFFACD",
"lightblue":"ADD8E6", "lightcoral":"F08080", "lightcyan":"E0FFFF",
"lightgoldenrodyellow":"FAFAD2", "lightgreen":"90EE90", "lightgrey":"D3D3D3",
"lightpink":"FFB6C1", "lightsalmon":"FFA07A", "lightseagreen":"20B2AA",
"lightskyblue":"87CEFA", "lightslategray":"789", "lightsteelblue":"B0C4DE",
"lightyellow":"FFFFE0", "lime":"0F0", "limegreen":"32CD32",
"linen":"FAF0E6", "magenta":"F0F", "maroon":"800000",
"mediumauqamarine":"66CDAA", "mediumblue":"0000CD", "mediumorchid":"BA55D3",
"mediumpurple":"9370D8", "mediumseagreen":"3CB371", "mediumslateblue":"7B68EE",
"mediumspringgreen":"00FA9A", "mediumturquoise":"48D1CC", "mediumvioletred":"C71585",
"midnightblue":"191970", "mintcream":"F5FFFA", "mistyrose":"FFE4E1",
"moccasin":"FFE4B5", "navajowhite":"FFDEAD", "navy":"000080",
"oldlace":"FDF5E6", "olive":"808000", "olivedrab":"688E23",
"orange":"FFA500", "orangered":"FF4500", "orchid":"DA70D6",
"palegoldenrod":"EEE8AA", "palegreen":"98FB98", "paleturquoise":"AFEEEE",
"palevioletred":"D87093", "papayawhip":"FFEFD5", "peachpuff":"FFDAB9",
"peru":"CD853F", "pink":"FFC0CB", "plum":"DDA0DD",
"powderblue":"B0E0E6", "purple":"800080", "red":"F00",
"rosybrown":"BC8F8F", "royalblue":"4169E1", "saddlebrown":"8B4513",
"salmon":"FA8072", "sandybrown":"F4A460", "seagreen":"2E8B57",
"seashell":"FFF5EE", "sienna":"A0522D", "silver":"C0C0C0",
"skyblue":"87CEEB", "slateblue":"6A5ACD", "slategray":"708090",
"snow":"FFFAFA", "springgreen":"00FF7F", "steelblue":"4682B4",
"tan":"D2B48C", "teal":"008080", "thistle":"D8BFD8",
"tomato":"FF6347", "turquoise":"40E0D0", "violet":"EE82EE",
"wheat":"F5DEB3", "white":"FFF", "whitesmoke":"F5F5F5",
"yellow":"FF0", "yellowgreen":"9ACD32"
};
Color.prototype = {
/**
* @private
*/
parse: function() {
if( !this._color ) {
var me = this,
v = me.val,
vLower,
m = v.match( Color.rgbaRE );
if( m ) {
me._color = 'rgb(' + m[1] + ',' + m[2] + ',' + m[3] + ')';
me._alpha = parseFloat( m[4] );
}
else {
if( ( vLower = v.toLowerCase() ) in Color.names ) {
v = '#' + Color.names[vLower];
}
me._color = v;
me._alpha = ( v === 'transparent' ? 0 : 1 );
}
}
},
/**
* Retrieve the value of the color in a format usable by IE natively. This will be the same as
* the raw input value, except for rgba values which will be converted to an rgb value.
* @param {Element} el The context element, used to get 'currentColor' keyword value.
* @return {string} Color value
*/
colorValue: function( el ) {
this.parse();
return this._color === 'currentColor' ? el.currentStyle.color : this._color;
},
/**
* Retrieve the alpha value of the color. Will be 1 for all values except for rgba values
* with an alpha component.
* @return {number} The alpha value, from 0 to 1.
*/
alpha: function() {
this.parse();
return this._alpha;
}
};
/**
* Retrieve a PIE.Color instance for the given value. A shared singleton instance is returned for each unique value.
* @param {string} val The CSS string representing the color. It is assumed that this will already have
* been validated as a valid color syntax.
*/
PIE.getColor = function(val) {
return instances[ val ] || ( instances[ val ] = new Color( val ) );
};
return Color;
})();/**
* A tokenizer for CSS value strings.
* @constructor
* @param {string} css The CSS value string
*/
PIE.Tokenizer = (function() {
function Tokenizer( css ) {
this.css = css;
this.ch = 0;
this.tokens = [];
this.tokenIndex = 0;
}
/**
* Enumeration of token type constants.
* @enum {number}
*/
var Type = Tokenizer.Type = {
ANGLE: 1,
CHARACTER: 2,
COLOR: 4,
DIMEN: 8,
FUNCTION: 16,
IDENT: 32,
LENGTH: 64,
NUMBER: 128,
OPERATOR: 256,
PERCENT: 512,
STRING: 1024,
URL: 2048
};
/**
* A single token
* @constructor
* @param {number} type The type of the token - see PIE.Tokenizer.Type
* @param {string} value The value of the token
*/
Tokenizer.Token = function( type, value ) {
this.tokenType = type;
this.tokenValue = value;
};
Tokenizer.Token.prototype = {
isLength: function() {
return this.tokenType & Type.LENGTH || ( this.tokenType & Type.NUMBER && this.tokenValue === '0' );
},
isLengthOrPercent: function() {
return this.isLength() || this.tokenType & Type.PERCENT;
}
};
Tokenizer.prototype = {
whitespace: /\s/,
number: /^[\+\-]?(\d*\.)?\d+/,
url: /^url\(\s*("([^"]*)"|'([^']*)'|([!#$%&*-~]*))\s*\)/i,