44 http://www.twitter.com/niklasvh
55
66 Released under MIT License
7- */
7+ */
88"use strict" ;
99
1010var _html2canvas = { } ,
@@ -55,72 +55,98 @@ _html2canvas.Util.Bounds = function getBounds (el) {
5555
5656 return bounds ;
5757
58- } /*else{
59-
60-
61- p = $(el).offset();
62-
63- return {
64- left: p.left + getCSS(el,"borderLeftWidth", true),
65- top: p.top + getCSS(el,"borderTopWidth", true),
66- width:$(el).innerWidth(),
67- height:$(el).innerHeight()
68- };
69-
70-
71- } */
58+ }
7259} ;
7360
7461_html2canvas . Util . getCSS = function ( el , attribute ) {
75- // return jQuery(el).css(attribute);
76- /*
77- var val,
78- left,
79- rsLeft = el.runtimeStyle && el.runtimeStyle[ attribute ],
80- style = el.style;
62+ // return $(el).css(attribute);
8163
82- if ( el.currentStyle ) {
83- val = el.currentStyle[ attribute ];
84- } else if (window.getComputedStyle) {
85- val = document.defaultView.getComputedStyle(el, null)[ attribute ];
64+ var val ;
65+
66+ function toPX ( attribute , val ) {
67+ var rsLeft = el . runtimeStyle && el . runtimeStyle [ attribute ] ,
68+ left ,
69+ style = el . style ;
70+
71+ // Check if we are not dealing with pixels, (Opera has issues with this)
72+ // Ported from jQuery css.js
73+ // From the awesome hack by Dean Edwards
74+ // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
75+
76+ // If we're not dealing with a regular pixel number
77+ // but a number that has a weird ending, we need to convert it to pixels
78+ if ( ! / ^ - ? \d + (?: p x ) ? $ / i. test ( val ) && / ^ - ? \d / . test ( val ) ) {
79+
80+ // Remember the original values
81+ left = style . left ;
82+
83+ // Put in the new values to get a computed value out
84+ if ( rsLeft ) {
85+ el . runtimeStyle . left = el . currentStyle . left ;
86+ }
87+ style . left = attribute === "fontSize" ? "1em" : ( val || 0 ) ;
88+ val = style . pixelLeft + "px" ;
89+
90+ // Revert the changed values
91+ style . left = left ;
92+ if ( rsLeft ) {
93+ el . runtimeStyle . left = rsLeft ;
94+ }
95+
96+ }
97+ return val ;
8698 }
87- */
88- // Check if we are not dealing with pixels, (Opera has issues with this)
89- // Ported from jQuery css.js
90- // From the awesome hack by Dean Edwards
91- // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
92-
93- // If we're not dealing with a regular pixel number
94- // but a number that has a weird ending, we need to convert it to pixels
9599
96- // if ( !/^-?\d+(?:px)?$/i.test( val ) && /^-?\d/.test( val ) ) {
97- /*
98- // Remember the original values
99- left = style.left;
100-
101- // Put in the new values to get a computed value out
102- if ( rsLeft ) {
103- el.runtimeStyle.left = el.currentStyle.left;
100+
101+ if ( window . getComputedStyle ) {
102+ val = document . defaultView . getComputedStyle ( el , null ) [ attribute ] ;
103+
104+ if ( attribute === "backgroundPosition" ) {
105+
106+ val = ( val . split ( "," ) [ 0 ] || "0 0" ) . split ( " " ) ;
107+
108+ val [ 0 ] = ( val [ 0 ] . indexOf ( "%" ) === - 1 ) ? toPX ( attribute + "X" , val [ 0 ] ) : val [ 0 ] ;
109+ val [ 1 ] = ( val [ 1 ] === undefined ) ? val [ 0 ] : val [ 1 ] ; // IE 9 doesn't return double digit always
110+ val [ 1 ] = ( val [ 1 ] . indexOf ( "%" ) === - 1 ) ? toPX ( attribute + "Y" , val [ 1 ] ) : val [ 1 ] ;
111+ }
112+
113+ } else if ( el . currentStyle ) {
114+ // IE 9>
115+ if ( attribute === "backgroundPosition" ) {
116+ // Older IE uses -x and -y
117+ val = [ toPX ( attribute + "X" , el . currentStyle [ attribute + "X" ] ) , toPX ( attribute + "Y" , el . currentStyle [ attribute + "X" ] ) ] ;
118+
119+ } else {
120+
121+ val = toPX ( attribute , el . currentStyle [ attribute ] ) ;
122+
123+ if ( / ^ ( b o r d e r ) / i. test ( attribute ) && / ^ ( m e d i u m | t h i n | t h i c k ) $ / i. test ( val ) ) {
124+ switch ( val ) {
125+ case "thin" :
126+ val = "1px" ;
127+ break ;
128+ case "medium" :
129+ val = "0px" ; // this is wrong, it should be 3px but IE uses medium for no border as well.. TODO find a work around
130+ break ;
131+ case "thick" :
132+ val = "5px" ;
133+ break ;
134+ }
135+ }
104136 }
105- style.left = attribute === "fontSize" ? "1em" : (val || 0);
106- val = style.pixelLeft + "px";
107-
108- // Revert the changed values
109- style.left = left;
110- if ( rsLeft ) {
111- el.runtimeStyle.left = rsLeft;
112- }*/
113- // val = $(el).css(attribute);
114- // }
137+
138+
139+
140+ }
141+
142+
143+
115144
116- /*
117- var val = $(el).css(attribute);
145+ return val ;
118146
119- if (val === "medium") {
120- val = 3;
121- }*/
147+
122148
123- return $ ( el ) . css ( attribute ) ;
149+ // return $(el).css(attribute);
124150
125151
126152} ;
@@ -129,17 +155,7 @@ _html2canvas.Util.getCSS = function (el, attribute) {
129155_html2canvas . Util . BackgroundPosition = function ( el , bounds , image ) {
130156 // TODO add support for multi image backgrounds
131157
132- var bgposition = ( function ( bgp ) {
133-
134- if ( bgp !== undefined ) {
135- return ( bgp . split ( "," ) [ 0 ] || "0 0" ) . split ( " " ) ;
136- } else {
137- // Older IE uses -x and -y
138- return [ _html2canvas . Util . getCSS ( el , "backgroundPositionX" ) , _html2canvas . Util . getCSS ( el , "backgroundPositionY" ) ] ;
139- }
140-
141-
142- } ) ( _html2canvas . Util . getCSS ( el , "backgroundPosition" ) ) ,
158+ var bgposition = _html2canvas . Util . getCSS ( el , "backgroundPosition" ) ,
143159 topPos ,
144160 left ,
145161 percentage ,
0 commit comments