Skip to content

Commit 9d662f3

Browse files
committed
Resize iframe to fit content up to 600px
Closes #4
1 parent e7f3fc4 commit 9d662f3

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

demo_frame.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = function(node) {
4040
show(node.querySelector("[data-tab=js]"));
4141
}
4242

43+
resizeIframe();
4344
tabs();
4445
}
4546

@@ -131,4 +132,30 @@ module.exports = function(node) {
131132
txt = txt.replace(/</g, "&lt;");
132133
return typeof prettyPrintOne !== "undefined" ? prettyPrintOne(txt) : txt;
133134
}
135+
136+
function resizeIframe() {
137+
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;
143+
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+
};
149+
150+
var cssHeight = getCssHeight();
151+
152+
// 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) {
156+
iframe.style.height = Math.min(high, 600) + "px";
157+
}
158+
159+
setTimeout(resizeIframe, 1000);
160+
}
134161
};

test.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,33 @@ describe("bit-docs-tag-demo", function() {
359359
});
360360
});
361361

362+
describe("resize iframe to fit content up to 600px", function() {
363+
before(function() {
364+
return ctx.browser
365+
.newPage()
366+
.then(function(p) {
367+
ctx.page = p;
368+
return ctx.page.goto("http://127.0.0.1:8081/test/temp/height.html");
369+
})
370+
.then(function() {
371+
return ctx.page.waitForFunction(function() {
372+
var iframe = document.querySelector("iframe");
373+
return iframe && iframe.style.height === "600px";
374+
});
375+
});
376+
});
377+
378+
it("resizes height up to 600px", function() {
379+
ctx.page
380+
.evaluate(function() {
381+
return document.querySelector("iframe").style.height;
382+
})
383+
.then(function(height) {
384+
assert.equal(height, "600px");
385+
});
386+
});
387+
});
388+
362389
describe("multiple instances", function() {
363390
this.timeout(8000);
364391

@@ -391,8 +418,8 @@ describe("bit-docs-tag-demo", function() {
391418
};
392419
})
393420
.then(function(r) {
394-
assert.equal(r.wrappers, 4, "four wrappers exists");
395-
assert.equal(r.injected, 4, "four injected into wrappers");
421+
assert.equal(r.wrappers, 5, "four wrappers exists");
422+
assert.equal(r.injected, 5, "four injected into wrappers");
396423
});
397424
});
398425
});

test/demos/demo-height.html

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

test/generate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function allDemos() {
1515
"demo-with-ids",
1616
"demo-without-ids",
1717
"demo-without-js",
18-
"demo-complex"
18+
"demo-complex",
19+
"demo-height"
1920
]
2021
.map(function(demo) {
2122
return "<h2>" + demo + "</h2>" + makeWrapper(demo);
@@ -40,6 +41,10 @@ var docMap = Promise.resolve({
4041
name: "complex",
4142
body: makeWrapper("demo-complex")
4243
},
44+
height: {
45+
name: "height",
46+
body: makeWrapper("demo-height")
47+
},
4348
index: {
4449
name: "index",
4550
body: allDemos()

0 commit comments

Comments
 (0)