Skip to content

Commit 4494c23

Browse files
Ensure Element.visible works on nodes from other documents (e.g., IFRAMES). (Closes prototypejs#319)
1 parent 8b3cb4a commit 4494c23

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/prototype/dom/dom.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
* // -> false
233233
**/
234234
function visible(element) {
235-
return $(element).getStyle('display') !== 'none';
235+
return Element.getStyle(element, 'display') !== 'none';
236236
}
237237

238238
/**
@@ -2647,12 +2647,13 @@
26472647
function getStyle(element, style) {
26482648
element = $(element);
26492649
style = normalizeStyleName(style);
2650+
var doc = element.ownerDocument;
26502651

26512652
// Try inline styles first.
26522653
var value = element.style[style];
26532654
if (!value || value === 'auto') {
26542655
// Reluctantly retrieve the computed style.
2655-
var css = document.defaultView.getComputedStyle(element, null);
2656+
var css = doc.defaultView.getComputedStyle(element, null);
26562657
value = css ? css[style] : null;
26572658
}
26582659

test/unit/fixtures/iframe.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style type="text/css" media="screen">
5+
.h { display: none; }
6+
</style>
7+
</head>
8+
<body>
9+
<p>visible</p>
10+
<p class="h">hidden</p>
11+
</body>
12+
</html>

test/unit/tests/dom.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,20 @@ suite('DOM', function () {
338338
assert.equal(element.up(), wrapper);
339339
});
340340

341-
test('#visible', function () {
341+
test('#visible', function (done) {
342342
assert.notEqual('none', $('test-visible').style.display);
343343
assert($('test-visible').visible());
344344
assert.equal('none', $('test-hidden').style.display);
345345
assert(!$('test-hidden').visible());
346+
assert(!$('test-hidden-by-stylesheet').visible());
347+
var iframe = $('iframe');
348+
// Wait to make sure the IFRAME has loaded.
349+
setTimeout(function () {
350+
var paragraphs = iframe.contentWindow.document.querySelectorAll('p');
351+
assert(Element.visible(paragraphs[0]));
352+
assert(!Element.visible(paragraphs[1]));
353+
done();
354+
}, 500);
346355
});
347356

348357
test('#toggle', function () {

test/unit/views/tests/dom.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,26 @@ div.style-test { margin-left: 1px }
8383
body {
8484
height: 40000px;
8585
}
86+
87+
#test-hidden-by-stylesheet {
88+
display: none;
89+
}
90+
91+
#iframe {
92+
width: 1px;
93+
height: 1px;
94+
}
8695
</style>
8796

8897
<div id="scroll_test_1">
8998
<p id="scroll_test_2">Scroll test</p>
9099
</div>
91100

101+
<iframe src="/fixtures/iframe.html" id="iframe"></iframe>
102+
92103
<div id="test-visible">visible</div>
93104
<div id="test-hidden" style="display:none;">hidden</div>
105+
<div id="test-hidden-by-stylesheet">hidden</div>
94106
<div id="test-toggle-visible">visible</div>
95107
<div id="test-toggle-hidden" style="display:none;">hidden</div>
96108
<div id="test-hide-visible">visible</div>

0 commit comments

Comments
 (0)