Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(664)

Side by Side Diff: third_party/WebKit/Source/core/dom/Element.cpp

Issue 2650343008: Implement Element.scrollIntoView for scroll-behavior: smooth. (Closed)
Patch Set: Fixed nits. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Peter Kelly (pmk@post.com) 4 * (C) 2001 Peter Kelly (pmk@post.com)
5 * (C) 2001 Dirk Mueller (mueller@kde.org) 5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * (C) 2007 David Smith (catfish.man@gmail.com) 6 * (C) 2007 David Smith (catfish.man@gmail.com)
7 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. 7 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc.
8 * All rights reserved. 8 * All rights reserved.
9 * (C) 2007 Eric Seidel (eric@webkit.org) 9 * (C) 2007 Eric Seidel (eric@webkit.org)
10 * 10 *
(...skipping 12 matching lines...) Expand all
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA. 24 * Boston, MA 02110-1301, USA.
25 */ 25 */
26 26
27 #include "core/dom/Element.h" 27 #include "core/dom/Element.h"
28 28
29 #include <memory> 29 #include <memory>
30 #include "bindings/core/v8/Dictionary.h" 30 #include "bindings/core/v8/Dictionary.h"
31 #include "bindings/core/v8/ExceptionMessages.h" 31 #include "bindings/core/v8/ExceptionMessages.h"
32 #include "bindings/core/v8/ExceptionState.h" 32 #include "bindings/core/v8/ExceptionState.h"
33 #include "bindings/core/v8/ScrollIntoViewOptionsOrBoolean.h"
33 #include "bindings/core/v8/V8DOMActivityLogger.h" 34 #include "bindings/core/v8/V8DOMActivityLogger.h"
34 #include "core/CSSValueKeywords.h" 35 #include "core/CSSValueKeywords.h"
35 #include "core/SVGNames.h" 36 #include "core/SVGNames.h"
36 #include "core/XMLNames.h" 37 #include "core/XMLNames.h"
37 #include "core/animation/AnimationTimeline.h" 38 #include "core/animation/AnimationTimeline.h"
38 #include "core/animation/CustomCompositorAnimations.h" 39 #include "core/animation/CustomCompositorAnimations.h"
39 #include "core/animation/css/CSSAnimations.h" 40 #include "core/animation/css/CSSAnimations.h"
40 #include "core/css/CSSIdentifierValue.h" 41 #include "core/css/CSSIdentifierValue.h"
41 #include "core/css/CSSPrimitiveValue.h" 42 #include "core/css/CSSPrimitiveValue.h"
42 #include "core/css/CSSStyleSheet.h" 43 #include "core/css/CSSStyleSheet.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 #include "core/editing/EditingUtilities.h" 89 #include "core/editing/EditingUtilities.h"
89 #include "core/editing/FrameSelection.h" 90 #include "core/editing/FrameSelection.h"
90 #include "core/editing/iterators/TextIterator.h" 91 #include "core/editing/iterators/TextIterator.h"
91 #include "core/editing/serializers/Serialization.h" 92 #include "core/editing/serializers/Serialization.h"
92 #include "core/events/EventDispatcher.h" 93 #include "core/events/EventDispatcher.h"
93 #include "core/events/FocusEvent.h" 94 #include "core/events/FocusEvent.h"
94 #include "core/frame/HostsUsingFeatures.h" 95 #include "core/frame/HostsUsingFeatures.h"
95 #include "core/frame/LocalDOMWindow.h" 96 #include "core/frame/LocalDOMWindow.h"
96 #include "core/frame/LocalFrame.h" 97 #include "core/frame/LocalFrame.h"
97 #include "core/frame/LocalFrameView.h" 98 #include "core/frame/LocalFrameView.h"
99 #include "core/frame/ScrollIntoViewOptions.h"
98 #include "core/frame/ScrollToOptions.h" 100 #include "core/frame/ScrollToOptions.h"
99 #include "core/frame/Settings.h" 101 #include "core/frame/Settings.h"
100 #include "core/frame/UseCounter.h" 102 #include "core/frame/UseCounter.h"
101 #include "core/frame/VisualViewport.h" 103 #include "core/frame/VisualViewport.h"
102 #include "core/frame/csp/ContentSecurityPolicy.h" 104 #include "core/frame/csp/ContentSecurityPolicy.h"
103 #include "core/html/HTMLCanvasElement.h" 105 #include "core/html/HTMLCanvasElement.h"
104 #include "core/html/HTMLCollection.h" 106 #include "core/html/HTMLCollection.h"
105 #include "core/html/HTMLDocument.h" 107 #include "core/html/HTMLDocument.h"
106 #include "core/html/HTMLElement.h" 108 #include "core/html/HTMLElement.h"
107 #include "core/html/HTMLFormControlsCollection.h" 109 #include "core/html/HTMLFormControlsCollection.h"
(...skipping 26 matching lines...) Expand all
134 #include "core/svg/SVGElement.h" 136 #include "core/svg/SVGElement.h"
135 #include "core/svg/SVGTreeScopeResources.h" 137 #include "core/svg/SVGTreeScopeResources.h"
136 #include "platform/EventDispatchForbiddenScope.h" 138 #include "platform/EventDispatchForbiddenScope.h"
137 #include "platform/RuntimeEnabledFeatures.h" 139 #include "platform/RuntimeEnabledFeatures.h"
138 #include "platform/bindings/DOMDataStore.h" 140 #include "platform/bindings/DOMDataStore.h"
139 #include "platform/bindings/V8DOMWrapper.h" 141 #include "platform/bindings/V8DOMWrapper.h"
140 #include "platform/bindings/V8PerContextData.h" 142 #include "platform/bindings/V8PerContextData.h"
141 #include "platform/graphics/CompositorMutableProperties.h" 143 #include "platform/graphics/CompositorMutableProperties.h"
142 #include "platform/graphics/CompositorMutation.h" 144 #include "platform/graphics/CompositorMutation.h"
143 #include "platform/scroll/ScrollableArea.h" 145 #include "platform/scroll/ScrollableArea.h"
146 #include "platform/scroll/SmoothScrollSequencer.h"
144 #include "platform/wtf/BitVector.h" 147 #include "platform/wtf/BitVector.h"
145 #include "platform/wtf/HashFunctions.h" 148 #include "platform/wtf/HashFunctions.h"
146 #include "platform/wtf/text/CString.h" 149 #include "platform/wtf/text/CString.h"
147 #include "platform/wtf/text/StringBuilder.h" 150 #include "platform/wtf/text/StringBuilder.h"
148 #include "platform/wtf/text/TextPosition.h" 151 #include "platform/wtf/text/TextPosition.h"
149 152
150 namespace blink { 153 namespace blink {
151 154
152 namespace { 155 namespace {
153 156
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 } 434 }
432 435
433 const AtomicString& Element::nonce() const { 436 const AtomicString& Element::nonce() const {
434 return HasRareData() ? GetElementRareData()->GetNonce() : g_empty_atom; 437 return HasRareData() ? GetElementRareData()->GetNonce() : g_empty_atom;
435 } 438 }
436 439
437 void Element::setNonce(const AtomicString& nonce) { 440 void Element::setNonce(const AtomicString& nonce) {
438 EnsureElementRareData().SetNonce(nonce); 441 EnsureElementRareData().SetNonce(nonce);
439 } 442 }
440 443
444 void Element::scrollIntoView(ScrollIntoViewOptionsOrBoolean arg) {
445 ScrollIntoViewOptions options;
446 if (arg.isBoolean()) {
447 if (arg.getAsBoolean())
448 options.setBlock("start");
449 else
450 options.setBlock("end");
451 options.setInlinePosition("nearest");
452 } else if (arg.isScrollIntoViewOptions()) {
453 options = arg.getAsScrollIntoViewOptions();
454 if (!RuntimeEnabledFeatures::CSSOMSmoothScrollEnabled() &&
455 options.behavior() == "smooth") {
456 options.setBehavior("instant");
457 }
458 }
459 scrollIntoViewWithOptions(options);
460 }
461
441 void Element::scrollIntoView(bool align_to_top) { 462 void Element::scrollIntoView(bool align_to_top) {
463 ScrollIntoViewOptionsOrBoolean arg;
464 arg.setBoolean(align_to_top);
465 scrollIntoView(arg);
466 }
467
468 static ScrollAlignment ToPhysicalAlignment(const ScrollIntoViewOptions& options,
469 ScrollOrientation axis,
470 bool is_horizontal_writing_mode) {
471 String alignment =
472 ((axis == kHorizontalScroll && is_horizontal_writing_mode) ||
473 (axis == kVerticalScroll && !is_horizontal_writing_mode))
474 ? options.inlinePosition()
475 : options.block();
476
477 if (alignment == "center")
478 return ScrollAlignment::kAlignCenterAlways;
479 if (alignment == "nearest")
480 return ScrollAlignment::kAlignToEdgeIfNeeded;
481 if (alignment == "start") {
482 return (axis == kHorizontalScroll) ? ScrollAlignment::kAlignLeftAlways
483 : ScrollAlignment::kAlignTopAlways;
484 }
485 if (alignment == "end") {
486 return (axis == kHorizontalScroll) ? ScrollAlignment::kAlignRightAlways
487 : ScrollAlignment::kAlignBottomAlways;
488 }
489
490 // Default values
491 if (is_horizontal_writing_mode) {
492 return (axis == kHorizontalScroll) ? ScrollAlignment::kAlignToEdgeIfNeeded
493 : ScrollAlignment::kAlignTopAlways;
494 }
495 return (axis == kHorizontalScroll) ? ScrollAlignment::kAlignLeftAlways
496 : ScrollAlignment::kAlignToEdgeIfNeeded;
497 }
498
499 void Element::scrollIntoViewWithOptions(const ScrollIntoViewOptions& options) {
442 GetDocument().EnsurePaintLocationDataValidForNode(this); 500 GetDocument().EnsurePaintLocationDataValidForNode(this);
443 501
444 if (!GetLayoutObject()) 502 if (!GetLayoutObject() || !GetDocument().GetPage())
445 return; 503 return;
446 504
447 bool make_visible_in_visual_viewport = 505 bool make_visible_in_visual_viewport =
448 !GetDocument().GetPage()->GetSettings().GetInertVisualViewport(); 506 !GetDocument().GetPage()->GetSettings().GetInertVisualViewport();
449 507
508 ScrollBehavior behavior = (options.behavior() == "smooth")
509 ? kScrollBehaviorSmooth
510 : kScrollBehaviorAuto;
511
512 bool is_horizontal_writing_mode =
513 GetComputedStyle()->IsHorizontalWritingMode();
514 ScrollAlignment align_x = ToPhysicalAlignment(options, kHorizontalScroll,
515 is_horizontal_writing_mode);
516 ScrollAlignment align_y =
517 ToPhysicalAlignment(options, kVerticalScroll, is_horizontal_writing_mode);
518
519 GetDocument().GetPage()->GetSmoothScrollSequencer()->AbortAnimations();
450 LayoutRect bounds = BoundingBox(); 520 LayoutRect bounds = BoundingBox();
451 // Align to the top / bottom and to the closest edge. 521 GetLayoutObject()->ScrollRectToVisible(
452 if (align_to_top) 522 bounds, align_x, align_y, kProgrammaticScroll,
453 GetLayoutObject()->ScrollRectToVisible( 523 make_visible_in_visual_viewport, behavior);
454 bounds, ScrollAlignment::kAlignToEdgeIfNeeded, 524
455 ScrollAlignment::kAlignTopAlways, kProgrammaticScroll, 525 if (behavior == kScrollBehaviorSmooth)
456 make_visible_in_visual_viewport); 526 GetDocument().GetPage()->GetSmoothScrollSequencer()->RunQueuedAnimations();
457 else
458 GetLayoutObject()->ScrollRectToVisible(
459 bounds, ScrollAlignment::kAlignToEdgeIfNeeded,
460 ScrollAlignment::kAlignBottomAlways, kProgrammaticScroll,
461 make_visible_in_visual_viewport);
462 527
463 GetDocument().SetSequentialFocusNavigationStartingPoint(this); 528 GetDocument().SetSequentialFocusNavigationStartingPoint(this);
464 } 529 }
465 530
466 void Element::scrollIntoViewIfNeeded(bool center_if_needed) { 531 void Element::scrollIntoViewIfNeeded(bool center_if_needed) {
467 GetDocument().EnsurePaintLocationDataValidForNode(this); 532 GetDocument().EnsurePaintLocationDataValidForNode(this);
468 533
469 if (!GetLayoutObject()) 534 if (!GetLayoutObject())
470 return; 535 return;
471 536
(...skipping 3886 matching lines...) Expand 10 before | Expand all | Expand 10 after
4358 } 4423 }
4359 4424
4360 DEFINE_TRACE_WRAPPERS(Element) { 4425 DEFINE_TRACE_WRAPPERS(Element) {
4361 if (HasRareData()) { 4426 if (HasRareData()) {
4362 visitor->TraceWrappers(GetElementRareData()); 4427 visitor->TraceWrappers(GetElementRareData());
4363 } 4428 }
4364 ContainerNode::TraceWrappers(visitor); 4429 ContainerNode::TraceWrappers(visitor);
4365 } 4430 }
4366 4431
4367 } // namespace blink 4432 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698