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

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

Issue 2650343008: Implement Element.scrollIntoView for scroll-behavior: smooth. (Closed)
Patch Set: Revised according to the comments. We are still missing tests. Created 3 years, 10 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 "bindings/core/v8/DOMDataStore.h" 29 #include "bindings/core/v8/DOMDataStore.h"
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 "bindings/core/v8/V8DOMWrapper.h" 35 #include "bindings/core/v8/V8DOMWrapper.h"
35 #include "bindings/core/v8/V8PerContextData.h" 36 #include "bindings/core/v8/V8PerContextData.h"
36 #include "core/CSSValueKeywords.h" 37 #include "core/CSSValueKeywords.h"
37 #include "core/SVGNames.h" 38 #include "core/SVGNames.h"
38 #include "core/XMLNames.h" 39 #include "core/XMLNames.h"
39 #include "core/animation/AnimationTimeline.h" 40 #include "core/animation/AnimationTimeline.h"
40 #include "core/animation/CustomCompositorAnimations.h" 41 #include "core/animation/CustomCompositorAnimations.h"
41 #include "core/animation/css/CSSAnimations.h" 42 #include "core/animation/css/CSSAnimations.h"
42 #include "core/css/CSSIdentifierValue.h" 43 #include "core/css/CSSIdentifierValue.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 #include "core/editing/FrameSelection.h" 91 #include "core/editing/FrameSelection.h"
91 #include "core/editing/iterators/TextIterator.h" 92 #include "core/editing/iterators/TextIterator.h"
92 #include "core/editing/serializers/Serialization.h" 93 #include "core/editing/serializers/Serialization.h"
93 #include "core/events/EventDispatcher.h" 94 #include "core/events/EventDispatcher.h"
94 #include "core/events/FocusEvent.h" 95 #include "core/events/FocusEvent.h"
95 #include "core/frame/FrameHost.h" 96 #include "core/frame/FrameHost.h"
96 #include "core/frame/FrameView.h" 97 #include "core/frame/FrameView.h"
97 #include "core/frame/HostsUsingFeatures.h" 98 #include "core/frame/HostsUsingFeatures.h"
98 #include "core/frame/LocalDOMWindow.h" 99 #include "core/frame/LocalDOMWindow.h"
99 #include "core/frame/LocalFrame.h" 100 #include "core/frame/LocalFrame.h"
101 #include "core/frame/ScrollIntoViewOptions.h"
100 #include "core/frame/ScrollToOptions.h" 102 #include "core/frame/ScrollToOptions.h"
101 #include "core/frame/Settings.h" 103 #include "core/frame/Settings.h"
102 #include "core/frame/UseCounter.h" 104 #include "core/frame/UseCounter.h"
103 #include "core/frame/VisualViewport.h" 105 #include "core/frame/VisualViewport.h"
104 #include "core/frame/csp/ContentSecurityPolicy.h" 106 #include "core/frame/csp/ContentSecurityPolicy.h"
105 #include "core/html/ClassList.h" 107 #include "core/html/ClassList.h"
106 #include "core/html/HTMLCanvasElement.h" 108 #include "core/html/HTMLCanvasElement.h"
107 #include "core/html/HTMLCollection.h" 109 #include "core/html/HTMLCollection.h"
108 #include "core/html/HTMLDocument.h" 110 #include "core/html/HTMLDocument.h"
109 #include "core/html/HTMLElement.h" 111 #include "core/html/HTMLElement.h"
(...skipping 14 matching lines...) Expand all
124 #include "core/loader/DocumentLoader.h" 126 #include "core/loader/DocumentLoader.h"
125 #include "core/page/ChromeClient.h" 127 #include "core/page/ChromeClient.h"
126 #include "core/page/FocusController.h" 128 #include "core/page/FocusController.h"
127 #include "core/page/Page.h" 129 #include "core/page/Page.h"
128 #include "core/page/PointerLockController.h" 130 #include "core/page/PointerLockController.h"
129 #include "core/page/SpatialNavigation.h" 131 #include "core/page/SpatialNavigation.h"
130 #include "core/page/scrolling/RootScrollerController.h" 132 #include "core/page/scrolling/RootScrollerController.h"
131 #include "core/page/scrolling/ScrollCustomizationCallbacks.h" 133 #include "core/page/scrolling/ScrollCustomizationCallbacks.h"
132 #include "core/page/scrolling/ScrollState.h" 134 #include "core/page/scrolling/ScrollState.h"
133 #include "core/page/scrolling/ScrollStateCallback.h" 135 #include "core/page/scrolling/ScrollStateCallback.h"
136 #include "core/page/scrolling/ScrollingCoordinator.h"
134 #include "core/page/scrolling/TopDocumentRootScrollerController.h" 137 #include "core/page/scrolling/TopDocumentRootScrollerController.h"
135 #include "core/paint/PaintLayer.h" 138 #include "core/paint/PaintLayer.h"
136 #include "core/svg/SVGAElement.h" 139 #include "core/svg/SVGAElement.h"
137 #include "core/svg/SVGElement.h" 140 #include "core/svg/SVGElement.h"
138 #include "core/svg/SVGTreeScopeResources.h" 141 #include "core/svg/SVGTreeScopeResources.h"
139 #include "platform/EventDispatchForbiddenScope.h" 142 #include "platform/EventDispatchForbiddenScope.h"
140 #include "platform/RuntimeEnabledFeatures.h" 143 #include "platform/RuntimeEnabledFeatures.h"
141 #include "platform/graphics/CompositorMutableProperties.h" 144 #include "platform/graphics/CompositorMutableProperties.h"
142 #include "platform/graphics/CompositorMutation.h" 145 #include "platform/graphics/CompositorMutation.h"
146 #include "platform/scroll/ProgrammaticScrollCoordinator.h"
143 #include "platform/scroll/ScrollableArea.h" 147 #include "platform/scroll/ScrollableArea.h"
144 #include "wtf/BitVector.h" 148 #include "wtf/BitVector.h"
145 #include "wtf/HashFunctions.h" 149 #include "wtf/HashFunctions.h"
146 #include "wtf/text/CString.h" 150 #include "wtf/text/CString.h"
147 #include "wtf/text/StringBuilder.h" 151 #include "wtf/text/StringBuilder.h"
148 #include "wtf/text/TextPosition.h" 152 #include "wtf/text/TextPosition.h"
149 #include <memory> 153 #include <memory>
150 154
151 namespace blink { 155 namespace blink {
152 156
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 synchronizeAttribute(name); 425 synchronizeAttribute(name);
422 if (const Attribute* attribute = elementData()->attributes().find(name)) 426 if (const Attribute* attribute = elementData()->attributes().find(name))
423 return attribute->value(); 427 return attribute->value();
424 return nullAtom; 428 return nullAtom;
425 } 429 }
426 430
427 bool Element::shouldIgnoreAttributeCase() const { 431 bool Element::shouldIgnoreAttributeCase() const {
428 return isHTMLElement() && document().isHTMLDocument(); 432 return isHTMLElement() && document().isHTMLDocument();
429 } 433 }
430 434
431 void Element::scrollIntoView(bool alignToTop) { 435 void Element::scrollIntoView(ExceptionState& exception) {
436 ScrollIntoViewOptions options;
437 options.setBlock("start");
438 options.setInlinePosition("nearest");
439 scrollIntoViewWithOptions(options);
440 }
441
442 void Element::scrollIntoView(ScrollIntoViewOptionsOrBoolean arg,
443 ExceptionState& exception) {
444 ScrollIntoViewOptions options;
445 if (arg.isBoolean()) {
446 if (arg.getAsBoolean())
447 options.setBlock("start");
448 else
449 options.setBlock("end");
450 options.setInlinePosition("nearest");
451 scrollIntoViewWithOptions(options);
452 } else if (arg.isScrollIntoViewOptions()) {
453 options = arg.getAsScrollIntoViewOptions();
454 if (RuntimeEnabledFeatures::cssomSmoothScrollEnabled() ||
455 options.behavior() != "smooth") {
456 scrollIntoViewWithOptions(options);
bokan 2017/02/21 21:33:00 This scrollIntoViewWithOptions and the one above c
sunyunjia 2017/02/24 19:11:48 Done.
457 } else {
458 exception.throwTypeError(
459 "Smooth ScrollIntoView is an Experimental Web"
460 " Platform Feature, go to chrome://flags to enable it.");
461 return;
462 }
463 } else {
464 exception.throwTypeError(
465 "ScrollIntoView only supports bool or"
466 " ScrollIntoViewOptions as argument.");
467 return;
468 }
469 }
470
471 void Element::scrollIntoViewWithOptions(const ScrollIntoViewOptions& options) {
432 document().updateStyleAndLayoutIgnorePendingStylesheetsForNode(this); 472 document().updateStyleAndLayoutIgnorePendingStylesheetsForNode(this);
433 473
434 if (!layoutObject()) 474 if (!layoutObject())
435 return; 475 return;
436 476
437 bool makeVisibleInVisualViewport = 477 bool makeVisibleInVisualViewport =
438 !document().page()->settings().getInertVisualViewport(); 478 !document().page()->settings().getInertVisualViewport();
439 479
480 ScrollBehavior behavior = ScrollBehaviorAuto;
481 if (options.behavior() == "smooth") {
482 behavior = ScrollBehaviorSmooth;
483 }
484 // Block Alignment to the top / bottom and to the closest edge.
485 ScrollAlignment blockAlignment = ScrollAlignment::alignCenterAlways;
486 if (options.block() == "start")
487 blockAlignment = ScrollAlignment::alignTopAlways;
488 else if (options.block() == "end")
489 blockAlignment = ScrollAlignment::alignBottomAlways;
490 else if (options.block() == "nearest")
491 blockAlignment = ScrollAlignment::alignClosestEdgeAlways;
492
493 // Inline Alignment to the top / bottom and to the closest edge.
494 ScrollAlignment inlineAlignment = ScrollAlignment::alignCenterAlways;
495 if (options.inlinePosition() == "start")
496 inlineAlignment = ScrollAlignment::alignLeftAlways;
497 else if (options.inlinePosition() == "end")
498 inlineAlignment = ScrollAlignment::alignRightAlways;
499 else if (options.inlinePosition() == "nearest")
500 inlineAlignment = ScrollAlignment::alignClosestEdgeAlways;
bokan 2017/02/21 21:33:00 Hmm, this used to have "IfNeeded" but now everythi
sunyunjia 2017/02/24 19:11:48 Done. My bad, I should've checked the spec closer.
501
440 LayoutRect bounds = boundingBox(); 502 LayoutRect bounds = boundingBox();
441 // Align to the top / bottom and to the closest edge. 503 layoutObject()->scrollRectToVisible(bounds, inlineAlignment, blockAlignment,
442 if (alignToTop) 504 ProgrammaticScroll,
443 layoutObject()->scrollRectToVisible( 505 makeVisibleInVisualViewport, behavior);
444 bounds, ScrollAlignment::alignToEdgeIfNeeded, 506
445 ScrollAlignment::alignTopAlways, ProgrammaticScroll, 507 document()
446 makeVisibleInVisualViewport); 508 .page()
447 else 509 ->scrollingCoordinator()
448 layoutObject()->scrollRectToVisible( 510 ->programmaticScrollCoordinator()
449 bounds, ScrollAlignment::alignToEdgeIfNeeded, 511 ->runQueuedAnimations();
450 ScrollAlignment::alignBottomAlways, ProgrammaticScroll,
451 makeVisibleInVisualViewport);
452 512
453 document().setSequentialFocusNavigationStartingPoint(this); 513 document().setSequentialFocusNavigationStartingPoint(this);
454 } 514 }
455 515
456 void Element::scrollIntoViewIfNeeded(bool centerIfNeeded) { 516 void Element::scrollIntoViewIfNeeded(bool centerIfNeeded) {
457 document().updateStyleAndLayoutIgnorePendingStylesheetsForNode(this); 517 document().updateStyleAndLayoutIgnorePendingStylesheetsForNode(this);
458 518
459 if (!layoutObject()) 519 if (!layoutObject())
460 return; 520 return;
461 521
(...skipping 3666 matching lines...) Expand 10 before | Expand all | Expand 10 after
4128 } 4188 }
4129 4189
4130 DEFINE_TRACE_WRAPPERS(Element) { 4190 DEFINE_TRACE_WRAPPERS(Element) {
4131 if (hasRareData()) { 4191 if (hasRareData()) {
4132 visitor->traceWrappers(elementRareData()); 4192 visitor->traceWrappers(elementRareData());
4133 } 4193 }
4134 ContainerNode::traceWrappers(visitor); 4194 ContainerNode::traceWrappers(visitor);
4135 } 4195 }
4136 4196
4137 } // namespace blink 4197 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698