@@ -99,21 +99,28 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
99
99
} ;
100
100
} ;
101
101
102
- var calculateContentSize = function ( doc , selector , requestedWidth , requestedHeight , zoom ) {
103
- // clientWidth/clientHeight needed for PhantomJS
104
- var actualViewportWidth = Math . max ( doc . documentElement . scrollWidth , doc . body . clientWidth ) ,
105
- actualViewportHeight = Math . max ( doc . documentElement . scrollHeight , doc . body . scrollHeight , doc . body . clientHeight ) ,
102
+ var selectElementOrDescendant = function ( element , selector ) {
103
+ var descendant = element . querySelector ( selector ) ;
104
+ if ( descendant ) {
105
+ return descendant ;
106
+ } else if ( element . ownerDocument . querySelector ( selector ) === element ) {
107
+ return element ;
108
+ }
109
+
110
+ throw {
111
+ message : "Clipping selector not found"
112
+ } ;
113
+ } ;
114
+
115
+ var calculateContentSize = function ( rootElement , selector , requestedWidth , requestedHeight , zoom ) {
116
+ // clientWidth/clientHeight needed for PhantomJS
117
+ var actualViewportWidth = Math . max ( rootElement . scrollWidth , rootElement . clientWidth ) ,
118
+ actualViewportHeight = Math . max ( rootElement . scrollHeight , rootElement . clientHeight ) ,
106
119
top , left , originalWidth , originalHeight , rootFontSize ,
107
120
element , rect , contentSize ;
108
121
109
122
if ( selector ) {
110
- element = doc . querySelector ( selector ) ;
111
-
112
- if ( ! element ) {
113
- throw {
114
- message : "Clipping selector not found"
115
- } ;
116
- }
123
+ element = selectElementOrDescendant ( rootElement , selector ) ;
117
124
118
125
rect = element . getBoundingClientRect ( ) ;
119
126
@@ -136,7 +143,7 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
136
143
requestedHeight ,
137
144
zoom ) ;
138
145
139
- rootFontSize = theWindow . getComputedStyle ( doc . documentElement ) . fontSize ;
146
+ rootFontSize = theWindow . getComputedStyle ( rootElement . ownerDocument . documentElement ) . fontSize ;
140
147
141
148
return {
142
149
left : left ,
@@ -150,6 +157,12 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
150
157
} ;
151
158
} ;
152
159
160
+ var findCorrelatingElement = function ( element , documentClone ) {
161
+ var tagName = element . tagName ;
162
+ // Stupid but simple method: find first match. Should work for a single HTML element, and any other element given as root
163
+ return documentClone . querySelector ( tagName ) ;
164
+ } ;
165
+
153
166
module . calculateDocumentContentSize = function ( element , options ) {
154
167
var html = element . outerHTML ,
155
168
defer = ayepromise . defer ( ) ,
@@ -166,7 +179,7 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
166
179
size ;
167
180
168
181
try {
169
- size = calculateContentSize ( doc , options . clip , options . width , options . height , zoom ) ;
182
+ size = calculateContentSize ( findCorrelatingElement ( element , doc ) , options . clip , options . width , options . height , zoom ) ;
170
183
171
184
defer . resolve ( size ) ;
172
185
} catch ( e ) {
0 commit comments