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

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

Issue 2650343008: Implement Element.scrollIntoView for scroll-behavior: smooth. (Closed)
Patch Set: Rebase 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 GetDocument().EnsurePaintLocationDataValidForNode(this);
438
439 ScrollIntoViewOptions options;
440 if (arg.isBoolean()) {
441 if (arg.getAsBoolean())
442 options.setBlock("start");
443 else
444 options.setBlock("end");
445 options.setInlinePosition("nearest");
446 } else if (arg.isScrollIntoViewOptions()) {
447 options = arg.getAsScrollIntoViewOptions();
448 if (!RuntimeEnabledFeatures::cssomSmoothScrollEnabled() &&
449 options.behavior() == "smooth") {
450 options.setBehavior("instant");
451 }
452 }
453 scrollIntoViewWithOptions(options);
454 }
455
433 void Element::scrollIntoView(bool align_to_top) { 456 void Element::scrollIntoView(bool align_to_top) {
434 GetDocument().EnsurePaintLocationDataValidForNode(this); 457 GetDocument().EnsurePaintLocationDataValidForNode(this);
bokan 2017/05/15 17:15:27 The call to EnsurePaintLocationDataValidForNode sh
sunyunjia 2017/05/19 16:24:28 Done.
435 458
459 ScrollIntoViewOptions options;
460 if (align_to_top)
461 options.setBlock("start");
462 else
463 options.setBlock("end");
464 options.setInlinePosition("nearest");
465 scrollIntoViewWithOptions(options);
bokan 2017/05/15 17:15:27 Can we just call the ScrollIntoViewOptionsOrBoolea
sunyunjia 2017/05/19 16:24:28 Done.
466 }
467
468 static ScrollAlignment InputAlignmentToXYAlignment(
bokan 2017/05/15 17:15:27 IMHO, ToPhysicalAlignment would be a better name.
sunyunjia 2017/05/19 16:24:28 Done.
469 const ScrollIntoViewOptions& options,
470 bool is_x,
bokan 2017/05/15 17:15:28 Add an enum in ScrollTypes called ScrollOrientatio
sunyunjia 2017/05/19 16:24:28 Done.
471 bool is_horizontal) {
472 String alignment = options.block();
473 if ((is_x && is_horizontal) || ((!is_x) && (!is_horizontal)))
474 alignment = options.inlinePosition();
bokan 2017/05/15 17:15:28 Nit: newline after this `if`
sunyunjia 2017/05/19 16:24:28 Done.
475 if (alignment == "center")
476 return ScrollAlignment::kAlignCenterAlways;
477 if (alignment == "nearest")
478 return ScrollAlignment::kAlignToEdgeIfNeeded;
479 if (alignment == "start") {
480 return is_x ? ScrollAlignment::kAlignLeftAlways
481 : ScrollAlignment::kAlignTopAlways;
482 }
483 if (alignment == "end") {
484 return is_x ? ScrollAlignment::kAlignRightAlways
485 : ScrollAlignment::kAlignBottomAlways;
486 }
487
488 // Default values
489 if (is_horizontal) {
490 if (is_x)
bokan 2017/05/15 17:15:27 Nit: use ? : operator like above.
sunyunjia 2017/05/19 16:24:28 Done.
491 return ScrollAlignment::kAlignToEdgeIfNeeded;
492 return ScrollAlignment::kAlignTopAlways;
493 }
494 // if (is_vertical)
bokan 2017/05/15 17:15:27 Remove
sunyunjia 2017/05/19 16:24:28 Done.
495 if (is_x)
496 return ScrollAlignment::kAlignLeftAlways;
497 return ScrollAlignment::kAlignToEdgeIfNeeded;
498 }
499
500 void Element::scrollIntoViewWithOptions(const ScrollIntoViewOptions& options) {
501 GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheetsForNode(this);
bokan 2017/05/15 17:15:28 This should probably just be EnsurePaintLocationDa
sunyunjia 2017/05/19 16:24:28 Done.
502
436 if (!GetLayoutObject()) 503 if (!GetLayoutObject())
437 return; 504 return;
438 505
439 bool make_visible_in_visual_viewport = 506 bool make_visible_in_visual_viewport =
440 !GetDocument().GetPage()->GetSettings().GetInertVisualViewport(); 507 !GetDocument().GetPage()->GetSettings().GetInertVisualViewport();
441 508
509 ScrollBehavior behavior = kScrollBehaviorAuto;
510 if (options.behavior() == "smooth") {
bokan 2017/05/15 17:15:27 Nit: no braces
sunyunjia 2017/05/19 16:24:28 Done.
511 behavior = kScrollBehaviorSmooth;
512 }
513
514 bool is_horizontal = GetComputedStyle()->IsHorizontalWritingMode();
515 ScrollAlignment align_x =
516 InputAlignmentToXYAlignment(options, true, is_horizontal);
517 ScrollAlignment align_y =
518 InputAlignmentToXYAlignment(options, false, is_horizontal);
519
520 if (GetDocument().GetPage())
bokan 2017/05/15 17:15:27 We should probably abort at the top if we don't ha
sunyunjia 2017/05/19 16:24:28 Done.
521 GetDocument().GetPage()->GetSmoothScrollSequencer()->AbortAnimations();
442 LayoutRect bounds = BoundingBox(); 522 LayoutRect bounds = BoundingBox();
443 // Align to the top / bottom and to the closest edge. 523 GetLayoutObject()->ScrollRectToVisible(
444 if (align_to_top) 524 bounds, align_x, align_y, kProgrammaticScroll,
445 GetLayoutObject()->ScrollRectToVisible( 525 make_visible_in_visual_viewport, behavior);
446 bounds, ScrollAlignment::kAlignToEdgeIfNeeded, 526
447 ScrollAlignment::kAlignTopAlways, kProgrammaticScroll, 527 if (GetDocument().GetPage())
448 make_visible_in_visual_viewport); 528 GetDocument().GetPage()->GetSmoothScrollSequencer()->RunQueuedAnimations();
449 else
450 GetLayoutObject()->ScrollRectToVisible(
451 bounds, ScrollAlignment::kAlignToEdgeIfNeeded,
452 ScrollAlignment::kAlignBottomAlways, kProgrammaticScroll,
453 make_visible_in_visual_viewport);
454 529
455 GetDocument().SetSequentialFocusNavigationStartingPoint(this); 530 GetDocument().SetSequentialFocusNavigationStartingPoint(this);
456 } 531 }
457 532
458 void Element::scrollIntoViewIfNeeded(bool center_if_needed) { 533 void Element::scrollIntoViewIfNeeded(bool center_if_needed) {
459 GetDocument().EnsurePaintLocationDataValidForNode(this); 534 GetDocument().EnsurePaintLocationDataValidForNode(this);
460 535
461 if (!GetLayoutObject()) 536 if (!GetLayoutObject())
462 return; 537 return;
463 538
(...skipping 3873 matching lines...) Expand 10 before | Expand all | Expand 10 after
4337 } 4412 }
4338 4413
4339 DEFINE_TRACE_WRAPPERS(Element) { 4414 DEFINE_TRACE_WRAPPERS(Element) {
4340 if (HasRareData()) { 4415 if (HasRareData()) {
4341 visitor->TraceWrappers(GetElementRareData()); 4416 visitor->TraceWrappers(GetElementRareData());
4342 } 4417 }
4343 ContainerNode::TraceWrappers(visitor); 4418 ContainerNode::TraceWrappers(visitor);
4344 } 4419 }
4345 4420
4346 } // namespace blink 4421 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698