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

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

Issue 2650343008: Implement Element.scrollIntoView for scroll-behavior: smooth. (Closed)
Patch Set: Added web platform test. Created 3 years, 7 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 #include "core/editing/EditingUtilities.h" 88 #include "core/editing/EditingUtilities.h"
88 #include "core/editing/FrameSelection.h" 89 #include "core/editing/FrameSelection.h"
89 #include "core/editing/iterators/TextIterator.h" 90 #include "core/editing/iterators/TextIterator.h"
90 #include "core/editing/serializers/Serialization.h" 91 #include "core/editing/serializers/Serialization.h"
91 #include "core/events/EventDispatcher.h" 92 #include "core/events/EventDispatcher.h"
92 #include "core/events/FocusEvent.h" 93 #include "core/events/FocusEvent.h"
93 #include "core/frame/FrameView.h" 94 #include "core/frame/FrameView.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"
98 #include "core/frame/ScrollIntoViewOptions.h"
97 #include "core/frame/ScrollToOptions.h" 99 #include "core/frame/ScrollToOptions.h"
98 #include "core/frame/Settings.h" 100 #include "core/frame/Settings.h"
99 #include "core/frame/UseCounter.h" 101 #include "core/frame/UseCounter.h"
100 #include "core/frame/VisualViewport.h" 102 #include "core/frame/VisualViewport.h"
101 #include "core/frame/csp/ContentSecurityPolicy.h" 103 #include "core/frame/csp/ContentSecurityPolicy.h"
102 #include "core/html/ClassList.h" 104 #include "core/html/ClassList.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"
(...skipping 27 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 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 if (const Attribute* attribute = GetElementData()->Attributes().Find(name)) 426 if (const Attribute* attribute = GetElementData()->Attributes().Find(name))
424 return attribute->Value(); 427 return attribute->Value();
425 return g_null_atom; 428 return g_null_atom;
426 } 429 }
427 430
428 AtomicString Element::LowercaseIfNecessary(const AtomicString& name) const { 431 AtomicString Element::LowercaseIfNecessary(const AtomicString& name) const {
429 return IsHTMLElement() && GetDocument().IsHTMLDocument() ? name.LowerASCII() 432 return IsHTMLElement() && GetDocument().IsHTMLDocument() ? name.LowerASCII()
430 : name; 433 : name;
431 } 434 }
432 435
436 void Element::scrollIntoView(ScrollIntoViewOptionsOrBoolean arg) {
437 ScrollIntoViewOptions options;
438 if (arg.isBoolean()) {
439 if (arg.getAsBoolean())
440 options.setBlock("start");
441 else
442 options.setBlock("end");
443 options.setInlinePosition("nearest");
444 } else if (arg.isScrollIntoViewOptions()) {
445 options = arg.getAsScrollIntoViewOptions();
446 if (!RuntimeEnabledFeatures::cssomSmoothScrollEnabled() &&
447 options.behavior() == "smooth") {
448 options.setBehavior("instant");
449 }
450 }
451 scrollIntoViewWithOptions(options);
452 }
453
433 void Element::scrollIntoView(bool align_to_top) { 454 void Element::scrollIntoView(bool align_to_top) {
455 ScrollIntoViewOptions options;
456 if (align_to_top)
457 options.setBlock("start");
458 else
459 options.setBlock("end");
460 options.setInlinePosition("nearest");
461 scrollIntoViewWithOptions(options);
462 }
463
464 static ScrollAlignment InputAlignmentToXYAlignment(
465 const ScrollIntoViewOptions& options,
466 bool is_x,
467 bool is_horizontal) {
468 String alignment = options.block();
469 if ((is_x && is_horizontal) || ((!is_x) && (!is_horizontal)))
470 alignment = options.inlinePosition();
471 if (alignment == "center")
472 return ScrollAlignment::kAlignCenterAlways;
473 if (alignment == "nearest")
474 return ScrollAlignment::kAlignToEdgeIfNeeded;
475 if (alignment == "start") {
476 return is_x ? ScrollAlignment::kAlignLeftAlways
477 : ScrollAlignment::kAlignTopAlways;
478 }
479 if (alignment == "end") {
480 return is_x ? ScrollAlignment::kAlignRightAlways
481 : ScrollAlignment::kAlignBottomAlways;
482 }
483
484 // Default values
485 if (is_horizontal) {
486 if (is_x)
487 return ScrollAlignment::kAlignToEdgeIfNeeded;
488 return ScrollAlignment::kAlignTopAlways;
489 }
490 // if (is_vertical)
491 if (is_x)
492 return ScrollAlignment::kAlignLeftAlways;
493 return ScrollAlignment::kAlignToEdgeIfNeeded;
494 }
495
496 void Element::scrollIntoViewWithOptions(const ScrollIntoViewOptions& options) {
434 GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheetsForNode(this); 497 GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheetsForNode(this);
435 498
436 if (!GetLayoutObject()) 499 if (!GetLayoutObject())
437 return; 500 return;
438 501
439 bool make_visible_in_visual_viewport = 502 bool make_visible_in_visual_viewport =
440 !GetDocument().GetPage()->GetSettings().GetInertVisualViewport(); 503 !GetDocument().GetPage()->GetSettings().GetInertVisualViewport();
441 504
505 ScrollBehavior behavior = kScrollBehaviorAuto;
506 if (options.behavior() == "smooth") {
507 behavior = kScrollBehaviorSmooth;
508 }
509
510 bool is_horizontal = GetComputedStyle()->IsHorizontalWritingMode();
511 ScrollAlignment align_x =
512 InputAlignmentToXYAlignment(options, true, is_horizontal);
513 ScrollAlignment align_y =
514 InputAlignmentToXYAlignment(options, false, is_horizontal);
515
516 if (GetDocument().GetPage())
517 GetDocument().GetPage()->GetSmoothScrollSequencer()->AbortAnimations();
442 LayoutRect bounds = BoundingBox(); 518 LayoutRect bounds = BoundingBox();
443 // Align to the top / bottom and to the closest edge. 519 GetLayoutObject()->ScrollRectToVisible(
444 if (align_to_top) 520 bounds, align_x, align_y, kProgrammaticScroll,
445 GetLayoutObject()->ScrollRectToVisible( 521 make_visible_in_visual_viewport, behavior);
446 bounds, ScrollAlignment::kAlignToEdgeIfNeeded, 522
447 ScrollAlignment::kAlignTopAlways, kProgrammaticScroll, 523 if (GetDocument().GetPage())
448 make_visible_in_visual_viewport); 524 GetDocument().GetPage()->GetSmoothScrollSequencer()->RunQueuedAnimations();
449 else
450 GetLayoutObject()->ScrollRectToVisible(
451 bounds, ScrollAlignment::kAlignToEdgeIfNeeded,
452 ScrollAlignment::kAlignBottomAlways, kProgrammaticScroll,
453 make_visible_in_visual_viewport);
454 525
455 GetDocument().SetSequentialFocusNavigationStartingPoint(this); 526 GetDocument().SetSequentialFocusNavigationStartingPoint(this);
456 } 527 }
457 528
458 void Element::scrollIntoViewIfNeeded(bool center_if_needed) { 529 void Element::scrollIntoViewIfNeeded(bool center_if_needed) {
459 GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheetsForNode(this); 530 GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheetsForNode(this);
460 531
461 if (!GetLayoutObject()) 532 if (!GetLayoutObject())
462 return; 533 return;
463 534
(...skipping 3873 matching lines...) Expand 10 before | Expand all | Expand 10 after
4337 } 4408 }
4338 4409
4339 DEFINE_TRACE_WRAPPERS(Element) { 4410 DEFINE_TRACE_WRAPPERS(Element) {
4340 if (HasRareData()) { 4411 if (HasRareData()) {
4341 visitor->TraceWrappers(GetElementRareData()); 4412 visitor->TraceWrappers(GetElementRareData());
4342 } 4413 }
4343 ContainerNode::TraceWrappers(visitor); 4414 ContainerNode::TraceWrappers(visitor);
4344 } 4415 }
4345 4416
4346 } // namespace blink 4417 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698