@@ -124,54 +124,42 @@ def inspect
124
124
end
125
125
126
126
def find_position ( x : nil , y : nil , position : :top )
127
- quads = node_quads ( x , y )
128
- get_position ( quads , x , y )
129
- end
130
-
131
- private
127
+ prev = get_content_quads
132
128
133
- def get_content_quads
134
- result = page . command ( "DOM.getContentQuads" , nodeId : node_id )
135
- raise "Node is either not visible or not an HTMLElement" if result [ "quads" ] . size == 0
136
- result
137
- end
138
-
139
- def node_quads ( x , y )
140
- prev_result = get_content_quads
141
-
142
- result = Ferrum . with_attempts ( errors : NodeIsMovingError , max : MOVING_ATTEMPTS , wait : 0 ) do
129
+ # FIXME: Case when a few quads returned
130
+ points = Ferrum . with_attempts ( errors : NodeIsMovingError , max : MOVING_ATTEMPTS , wait : 0 ) do
143
131
sleep ( MOVING_WAIT )
144
- current_result = get_content_quads
145
-
146
- if current_result [ "quads" ] != prev_result [ "quads" ]
147
- prev_pos = get_position ( prev_result , x , y )
148
- current_pos = get_position ( current_result , x , y )
132
+ current = get_content_quads
149
133
150
- prev_result = current_result
151
- raise NodeIsMovingError . new ( self , prev_pos , current_pos )
134
+ if current != prev
135
+ error = NodeIsMovingError . new ( self , prev , current )
136
+ prev = current
137
+ raise ( error )
152
138
end
153
139
154
- current_result
155
- end
140
+ current
141
+ end . map { | q | to_points ( q ) } . first
156
142
157
- # FIXME: Case when a few quads returned
158
- result [ "quads" ] . map do |quad |
159
- [ { x : quad [ 0 ] , y : quad [ 1 ] } ,
160
- { x : quad [ 2 ] , y : quad [ 3 ] } ,
161
- { x : quad [ 4 ] , y : quad [ 5 ] } ,
162
- { x : quad [ 6 ] , y : quad [ 7 ] } ]
163
- end . first
143
+ get_position ( points , x , y , position )
164
144
end
165
145
166
- def get_position ( quads , offset_x , offset_y )
146
+ private
147
+
148
+ def get_content_quads
149
+ quads = page . command ( "DOM.getContentQuads" , nodeId : node_id ) [ "quads" ]
150
+ raise "Node is either not visible or not an HTMLElement" if quads . size == 0
151
+ quads
152
+ end
153
+
154
+ def get_position ( points , offset_x , offset_y , position )
167
155
x = y = nil
168
156
169
157
if offset_x && offset_y && position == :top
170
- point = quads . first
158
+ point = points . first
171
159
x = point [ :x ] + offset_x . to_i
172
160
y = point [ :y ] + offset_y . to_i
173
161
else
174
- x , y = quads . inject ( [ 0 , 0 ] ) do |memo , point |
162
+ x , y = points . inject ( [ 0 , 0 ] ) do |memo , point |
175
163
[ memo [ 0 ] + point [ :x ] ,
176
164
memo [ 1 ] + point [ :y ] ]
177
165
end
@@ -187,5 +175,12 @@ def get_position(quads, offset_x, offset_y)
187
175
188
176
[ x , y ]
189
177
end
178
+
179
+ def to_points ( quad )
180
+ [ { x : quad [ 0 ] , y : quad [ 1 ] } ,
181
+ { x : quad [ 2 ] , y : quad [ 3 ] } ,
182
+ { x : quad [ 4 ] , y : quad [ 5 ] } ,
183
+ { x : quad [ 6 ] , y : quad [ 7 ] } ]
184
+ end
190
185
end
191
186
end
0 commit comments