Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page: fix page.scrollIntoViewIfNeeded method #245

Merged
merged 2 commits into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 8 additions & 20 deletions lib/puppeteer/element_handle.rb
Expand Up @@ -169,37 +169,25 @@ class ScrollIntoViewError < StandardError; end

def scroll_into_view_if_needed
js = <<~JAVASCRIPT
async(element, pageJavascriptEnabled) => {
async(element) => {
if (!element.isConnected)
return 'Node is detached from document';
if (element.nodeType !== Node.ELEMENT_NODE)
return 'Node is not of type HTMLElement';

if (element.scrollIntoViewIfNeeded) {
element.scrollIntoViewIfNeeded({block: 'center', inline: 'center', behavior: 'instant'});
} else {
// force-scroll if page's javascript is disabled.
if (!pageJavascriptEnabled) {
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
return false;
}
const visibleRatio = await new Promise(resolve => {
const observer = new IntersectionObserver(entries => {
resolve(entries[0].intersectionRatio);
observer.disconnect();
});
observer.observe(element);
});
if (visibleRatio !== 1.0)
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
}
return false;
}
JAVASCRIPT
error = evaluate(js, @page.javascript_enabled) # returns String or false
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@page.javascript_enabled should not be passed here

if error
raise ScrollIntoViewError.new(error)
end
begin
@remote_object.scroll_into_view_if_needed(@client)
rescue => err
# Just ignore 'Node does not have a layout object' for backward-compatibility.
raise unless err.message =~ /Node does not have a layout object/
end

# clickpoint is often calculated before scrolling is completed.
# So, just sleep about 10 frames
sleep 0.16
Expand Down
4 changes: 4 additions & 0 deletions lib/puppeteer/remote_object.rb
Expand Up @@ -176,4 +176,8 @@ def converted_arg
def set_file_input_files(client, files, backend_node_id)
client.send_message('DOM.setFileInputFiles', objectId: @object_id, files: files, backendNodeId: backend_node_id)
end

def scroll_into_view_if_needed(client)
client.send_message('DOM.scrollIntoViewIfNeeded', objectId: @object_id)
end
end
2 changes: 1 addition & 1 deletion spec/integration/click_spec.rb
Expand Up @@ -111,7 +111,7 @@
page.goto("#{server_prefix}/offscreenbuttons.html")
messages = []
page.on('console') do |message|
messages << message.text
messages << message.text if message.log_type == 'log'
end
11.times do |i|
# We might've scrolled to click a button - reset to (0, 0).
Expand Down