Skip to content

Commit 56531fa

Browse files
authored
Merge pull request #15 from bit-docs/resize-iframe
Resize iframe logic should account for padding
2 parents 40913de + 280f93b commit 56531fa

File tree

4 files changed

+75
-18
lines changed

4 files changed

+75
-18
lines changed

demo_frame.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,39 @@ module.exports = function(node) {
132132
txt = txt.replace(/</g, "&lt;");
133133
return typeof prettyPrintOne !== "undefined" ? prettyPrintOne(txt) : txt;
134134
}
135-
135+
// Given the content height and the compute styles of the element,
136+
// compute the total height of the box
137+
function getTotalHeight(height, computed) {
138+
return height +
139+
parseInt(computed.marginTop, 10) +
140+
parseInt(computed.marginBottom, 10) +
141+
parseInt(computed.borderTopWidth, 10) +
142+
parseInt(computed.borderBottomWidth, 10) +
143+
parseInt(computed.paddingTop, 10) +
144+
parseInt(computed.paddingBottom, 10);
145+
}
136146
function resizeIframe() {
137147
var frame = node.getElementsByTagName("iframe")[0];
138-
var height = frame.contentWindow.document.body.scrollHeight;
139-
140-
var tolerance = 5; // pixels
141-
var low = height - tolerance;
142-
var high = height + tolerance;
148+
var computed = getComputedStyle(frame);
143149

144-
// turns "150px" to 150, and "" to 0
145-
var getCssHeight = function() {
146-
var h = frame.style.height;
147-
return Number(h.substr(0, h.length - 2) || 0);
148-
};
150+
if (!frame.contentDocument || !frame.contentDocument.body) {
151+
return;
152+
}
149153

150-
var cssHeight = getCssHeight();
154+
// compute height tolerance range
155+
var tolerance = 5; // pixels
156+
var desiredHeight = getTotalHeight(
157+
frame.contentDocument.body.scrollHeight,
158+
computed
159+
);
160+
var low = desiredHeight - tolerance;
161+
var high = desiredHeight + tolerance;
151162

152163
// Setting the height causes the next resizeIframe call to get a different
153-
// height reading (lower); The range/tolerance logic is added to prevent the
154-
// continous shrinking of the iframe
155-
if (cssHeight < low || cssHeight > high) {
164+
// height reading (lower); The range/tolerance logic is added to prevent
165+
// the continous shrinking of the iframe
166+
var currentHeight = parseInt(computed.height, 10);
167+
if (currentHeight < low || currentHeight > high) {
156168
iframe.style.height = Math.min(high, 600) + "px";
157169
}
158170

test.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,34 @@ describe("bit-docs-tag-demo", function() {
388388
});
389389
});
390390

391+
describe("resize iframe logic adds up border/padding/margin", function() {
392+
before(function() {
393+
return ctx.browser
394+
.newPage()
395+
.then(function(p) {
396+
ctx.page = p;
397+
return ctx.page.goto("http://127.0.0.1:8081/test/temp/heightBoxModel.html");
398+
})
399+
.then(function() {
400+
return ctx.page.waitForFunction(function() {
401+
var iframe = document.querySelector("iframe");
402+
return iframe && iframe.style.height === "600px";
403+
});
404+
});
405+
});
406+
407+
it("resizes height up to 600px", function() {
408+
ctx.page
409+
.evaluate(function() {
410+
return document.querySelector("iframe").style.height;
411+
})
412+
.then(function(height) {
413+
assert.equal(height, "600px");
414+
});
415+
});
416+
});
417+
418+
391419
describe("multiple instances", function() {
392420
this.timeout(8000);
393421

@@ -420,8 +448,8 @@ describe("bit-docs-tag-demo", function() {
420448
};
421449
})
422450
.then(function(r) {
423-
assert.equal(r.wrappers, 5, "four wrappers exists");
424-
assert.equal(r.injected, 5, "four injected into wrappers");
451+
assert.equal(r.wrappers, 6, "four wrappers exists");
452+
assert.equal(r.injected, 6, "four injected into wrappers");
425453
});
426454
});
427455
});

test/demos/demo-height-box-model.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div id="demo-html"></div>
2+
<script id="demo-source">
3+
for (var i = 0; i < 5; i += 1) {
4+
var div = document.createElement("div");
5+
div.style.height = "100px";
6+
div.textContent = "100px";
7+
document.body.appendChild(div);
8+
}
9+
var frame = window.frameElement;
10+
frame.style.paddingTop = "100px";
11+
frame.style.paddingBottom = "100px";
12+
</script>

test/generate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function allDemos() {
1616
"demo-without-ids",
1717
"demo-without-js",
1818
"demo-complex",
19-
"demo-height"
19+
"demo-height",
20+
"demo-height-box-model"
2021
]
2122
.map(function(demo) {
2223
return "<h2>" + demo + "</h2>" + makeWrapper(demo);
@@ -45,6 +46,10 @@ var docMap = Promise.resolve({
4546
name: "height",
4647
body: makeWrapper("demo-height")
4748
},
49+
heightBoxModel: {
50+
name: "heightBoxModel",
51+
body: makeWrapper("demo-height-box-model")
52+
},
4853
index: {
4954
name: "index",
5055
body: allDemos()

0 commit comments

Comments
 (0)