From 4494c23749fc667f9a00b1f2d757d478a925002c Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sun, 9 Apr 2017 18:12:54 -0500 Subject: [PATCH] Ensure `Element.visible` works on nodes from other documents (e.g., IFRAMES). (Closes #319) --- src/prototype/dom/dom.js | 5 +++-- test/unit/fixtures/iframe.html | 12 ++++++++++++ test/unit/tests/dom.test.js | 11 ++++++++++- test/unit/views/tests/dom.erb | 12 ++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 test/unit/fixtures/iframe.html diff --git a/src/prototype/dom/dom.js b/src/prototype/dom/dom.js index dc9d78e19..50be59dc8 100644 --- a/src/prototype/dom/dom.js +++ b/src/prototype/dom/dom.js @@ -232,7 +232,7 @@ * // -> false **/ function visible(element) { - return $(element).getStyle('display') !== 'none'; + return Element.getStyle(element, 'display') !== 'none'; } /** @@ -2647,12 +2647,13 @@ function getStyle(element, style) { element = $(element); style = normalizeStyleName(style); + var doc = element.ownerDocument; // Try inline styles first. var value = element.style[style]; if (!value || value === 'auto') { // Reluctantly retrieve the computed style. - var css = document.defaultView.getComputedStyle(element, null); + var css = doc.defaultView.getComputedStyle(element, null); value = css ? css[style] : null; } diff --git a/test/unit/fixtures/iframe.html b/test/unit/fixtures/iframe.html new file mode 100644 index 000000000..aa2749b77 --- /dev/null +++ b/test/unit/fixtures/iframe.html @@ -0,0 +1,12 @@ + + + + + + +

visible

+

hidden

+ + diff --git a/test/unit/tests/dom.test.js b/test/unit/tests/dom.test.js index 68767d27c..1ef310a33 100644 --- a/test/unit/tests/dom.test.js +++ b/test/unit/tests/dom.test.js @@ -338,11 +338,20 @@ suite('DOM', function () { assert.equal(element.up(), wrapper); }); - test('#visible', function () { + test('#visible', function (done) { assert.notEqual('none', $('test-visible').style.display); assert($('test-visible').visible()); assert.equal('none', $('test-hidden').style.display); assert(!$('test-hidden').visible()); + assert(!$('test-hidden-by-stylesheet').visible()); + var iframe = $('iframe'); + // Wait to make sure the IFRAME has loaded. + setTimeout(function () { + var paragraphs = iframe.contentWindow.document.querySelectorAll('p'); + assert(Element.visible(paragraphs[0])); + assert(!Element.visible(paragraphs[1])); + done(); + }, 500); }); test('#toggle', function () { diff --git a/test/unit/views/tests/dom.erb b/test/unit/views/tests/dom.erb index b0c7f9460..e7c694e9f 100644 --- a/test/unit/views/tests/dom.erb +++ b/test/unit/views/tests/dom.erb @@ -83,14 +83,26 @@ div.style-test { margin-left: 1px } body { height: 40000px; } + +#test-hidden-by-stylesheet { + display: none; +} + +#iframe { + width: 1px; + height: 1px; +}

Scroll test

+ +
visible
+
hidden
visible
visible