Add elementVisible(elem) and refactor visible(selector) and getElementsInfo(selector) to use it
Showing
1 changed file
with
26 additions
and
16 deletions
... | @@ -128,6 +128,24 @@ | ... | @@ -128,6 +128,24 @@ |
128 | }; | 128 | }; |
129 | 129 | ||
130 | /** | 130 | /** |
131 | * Checks if a given DOM element is visible in remove page. | ||
132 | * | ||
133 | * @param Object element DOM element | ||
134 | * @return Boolean | ||
135 | */ | ||
136 | this.elementVisible = function elementVisible(elem) { | ||
137 | try { | ||
138 | var comp = window.getComputedStyle(elem, null); | ||
139 | return comp.visiblility !== 'hidden' && | ||
140 | comp.display !== 'none' && | ||
141 | elem.offsetHeight > 0 && | ||
142 | elem.offsetWidth > 0; | ||
143 | } catch (e) { | ||
144 | return false; | ||
145 | } | ||
146 | } | ||
147 | |||
148 | /** | ||
131 | * Base64 encodes a string, even binary ones. Succeeds where | 149 | * Base64 encodes a string, even binary ones. Succeeds where |
132 | * window.btoa() fails. | 150 | * window.btoa() fails. |
133 | * | 151 | * |
... | @@ -427,8 +445,8 @@ | ... | @@ -427,8 +445,8 @@ |
427 | */ | 445 | */ |
428 | this.getElementsInfo = function getElementsInfo(selector) { | 446 | this.getElementsInfo = function getElementsInfo(selector) { |
429 | var bounds = this.getElementsBounds(selector); | 447 | var bounds = this.getElementsBounds(selector); |
430 | var visibility = this.visible(selector); | 448 | var eleVisible = this.elementVisible; |
431 | return Array.prototype.map.call(this.findAll(selector), function(element, index) { | 449 | return [].map.call(this.findAll(selector), function(element, index) { |
432 | var attributes = {}; | 450 | var attributes = {}; |
433 | [].forEach.call(element.attributes, function(attr) { | 451 | [].forEach.call(element.attributes, function(attr) { |
434 | attributes[attr.name.toLowerCase()] = attr.value; | 452 | attributes[attr.name.toLowerCase()] = attr.value; |
... | @@ -443,7 +461,7 @@ | ... | @@ -443,7 +461,7 @@ |
443 | y: bounds[index].top, | 461 | y: bounds[index].top, |
444 | width: bounds[index].width, | 462 | width: bounds[index].width, |
445 | height: bounds[index].height, | 463 | height: bounds[index].height, |
446 | visible: visibility | 464 | visible: eleVisible(element) |
447 | }; | 465 | }; |
448 | }); | 466 | }); |
449 | }; | 467 | }; |
... | @@ -801,24 +819,16 @@ | ... | @@ -801,24 +819,16 @@ |
801 | }; | 819 | }; |
802 | 820 | ||
803 | /** | 821 | /** |
804 | * Checks if a given DOM element is visible in remote page. | 822 | * Checks if any element matching a given selector is visible in remote page. |
805 | * | 823 | * |
806 | * @param String selector CSS3 selector | 824 | * @param String selector CSS3 selector |
807 | * @return Boolean | 825 | * @return Boolean |
808 | */ | 826 | */ |
809 | this.visible = function visible(selector) { | 827 | this.visible = function visible(selector) { |
810 | try { | 828 | var eleVisible = this.elementVisible; |
811 | var elems = this.findAll(selector); | 829 | return [].some.call(this.findAll(selector), function(el) { |
812 | return Array.prototype.some.call(elems, function(el) { | 830 | return eleVisible(el); |
813 | var comp = window.getComputedStyle(el, null); | 831 | }); |
814 | return comp.visibility !== 'hidden' && | ||
815 | comp.display !== 'none' && | ||
816 | el.offsetHeight > 0 && | ||
817 | el.offsetWidth > 0; | ||
818 | }); | ||
819 | } catch (e) { | ||
820 | return false; | ||
821 | } | ||
822 | }; | 832 | }; |
823 | }; | 833 | }; |
824 | })(typeof exports === "object" ? exports : window); | 834 | })(typeof exports === "object" ? exports : window); | ... | ... |
-
Please register or sign in to post a comment