Skip to content

Commit b97347c

Browse files
committed
Calculate content size for arbitrary root element. cburgmer#78
1 parent 75de59f commit b97347c

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/browser.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,28 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
9999
};
100100
};
101101

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),
106119
top, left, originalWidth, originalHeight, rootFontSize,
107120
element, rect, contentSize;
108121

109122
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);
117124

118125
rect = element.getBoundingClientRect();
119126

@@ -136,7 +143,7 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
136143
requestedHeight,
137144
zoom);
138145

139-
rootFontSize = theWindow.getComputedStyle(doc.documentElement).fontSize;
146+
rootFontSize = theWindow.getComputedStyle(rootElement.ownerDocument.documentElement).fontSize;
140147

141148
return {
142149
left: left,
@@ -150,6 +157,12 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
150157
};
151158
};
152159

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+
153166
module.calculateDocumentContentSize = function (element, options) {
154167
var html = element.outerHTML,
155168
defer = ayepromise.defer(),
@@ -166,7 +179,7 @@ var browser = (function (util, proxies, ayepromise, sanedomparsererror, theWindo
166179
size;
167180

168181
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);
170183

171184
defer.resolve(size);
172185
} catch (e) {

test/specs/BrowserSpec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ describe("Browser functions", function () {
351351
});
352352
});
353353

354+
it("should calculate the correct size given any DOM element", function () {
355+
setHtml('<div><style>div { width: 400px; height: 400px; }</style></div>');
356+
357+
browser.calculateDocumentContentSize(doc.querySelector('div'), {width: 300, height: 200}).then(function (size) {
358+
expect(size.width).toBe(400);
359+
expect(size.height).toBe(400);
360+
});
361+
});
362+
354363
it("should remove the iframe when done calculating", function (done) {
355364
setElementWithSize({});
356365

0 commit comments

Comments
 (0)