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

Side by Side Diff: third_party/WebKit/Source/core/editing/VisibleSelection.cpp

Issue 2966433002: Get rid of VisibleSelectionTemplate::has_trailing_whitespace_ (Closed)
Patch Set: 2017-07-04T18:31:35 Created 3 years, 5 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
« no previous file with comments | « third_party/WebKit/Source/core/editing/VisibleSelection.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 28 matching lines...) Expand all
39 #include "platform/wtf/text/StringBuilder.h" 39 #include "platform/wtf/text/StringBuilder.h"
40 40
41 namespace blink { 41 namespace blink {
42 42
43 template <typename Strategy> 43 template <typename Strategy>
44 VisibleSelectionTemplate<Strategy>::VisibleSelectionTemplate() 44 VisibleSelectionTemplate<Strategy>::VisibleSelectionTemplate()
45 : affinity_(TextAffinity::kDownstream), 45 : affinity_(TextAffinity::kDownstream),
46 selection_type_(kNoSelection), 46 selection_type_(kNoSelection),
47 base_is_first_(true), 47 base_is_first_(true),
48 is_directional_(false), 48 is_directional_(false),
49 granularity_(kCharacterGranularity), 49 granularity_(kCharacterGranularity) {}
50 has_trailing_whitespace_(false) {}
51 50
52 template <typename Strategy> 51 template <typename Strategy>
53 VisibleSelectionTemplate<Strategy>::VisibleSelectionTemplate( 52 VisibleSelectionTemplate<Strategy>::VisibleSelectionTemplate(
54 const SelectionTemplate<Strategy>& selection) 53 const SelectionTemplate<Strategy>& selection)
55 : base_(selection.Base()), 54 : base_(selection.Base()),
56 extent_(selection.Extent()), 55 extent_(selection.Extent()),
57 affinity_(selection.Affinity()), 56 affinity_(selection.Affinity()),
58 selection_type_(kNoSelection), 57 selection_type_(kNoSelection),
59 is_directional_(selection.IsDirectional()), 58 is_directional_(selection.IsDirectional()),
60 granularity_(selection.Granularity()), 59 granularity_(selection.Granularity()) {
61 has_trailing_whitespace_(selection.HasTrailingWhitespace()) {
62 Validate(granularity_); 60 Validate(granularity_);
63 } 61 }
64 62
65 template <typename Strategy> 63 template <typename Strategy>
66 VisibleSelectionTemplate<Strategy> VisibleSelectionTemplate<Strategy>::Create( 64 VisibleSelectionTemplate<Strategy> VisibleSelectionTemplate<Strategy>::Create(
67 const SelectionTemplate<Strategy>& selection) { 65 const SelectionTemplate<Strategy>& selection) {
68 return VisibleSelectionTemplate(selection); 66 return VisibleSelectionTemplate(selection);
69 } 67 }
70 68
71 VisibleSelection CreateVisibleSelection(const SelectionInDOMTree& selection) { 69 VisibleSelection CreateVisibleSelection(const SelectionInDOMTree& selection) {
(...skipping 25 matching lines...) Expand all
97 VisibleSelectionTemplate<Strategy>::VisibleSelectionTemplate( 95 VisibleSelectionTemplate<Strategy>::VisibleSelectionTemplate(
98 const VisibleSelectionTemplate<Strategy>& other) 96 const VisibleSelectionTemplate<Strategy>& other)
99 : base_(other.base_), 97 : base_(other.base_),
100 extent_(other.extent_), 98 extent_(other.extent_),
101 start_(other.start_), 99 start_(other.start_),
102 end_(other.end_), 100 end_(other.end_),
103 affinity_(other.affinity_), 101 affinity_(other.affinity_),
104 selection_type_(other.selection_type_), 102 selection_type_(other.selection_type_),
105 base_is_first_(other.base_is_first_), 103 base_is_first_(other.base_is_first_),
106 is_directional_(other.is_directional_), 104 is_directional_(other.is_directional_),
107 granularity_(other.granularity_), 105 granularity_(other.granularity_) {}
108 has_trailing_whitespace_(other.has_trailing_whitespace_) {}
109 106
110 template <typename Strategy> 107 template <typename Strategy>
111 VisibleSelectionTemplate<Strategy>& VisibleSelectionTemplate<Strategy>:: 108 VisibleSelectionTemplate<Strategy>& VisibleSelectionTemplate<Strategy>::
112 operator=(const VisibleSelectionTemplate<Strategy>& other) { 109 operator=(const VisibleSelectionTemplate<Strategy>& other) {
113 base_ = other.base_; 110 base_ = other.base_;
114 extent_ = other.extent_; 111 extent_ = other.extent_;
115 start_ = other.start_; 112 start_ = other.start_;
116 end_ = other.end_; 113 end_ = other.end_;
117 affinity_ = other.affinity_; 114 affinity_ = other.affinity_;
118 selection_type_ = other.selection_type_; 115 selection_type_ = other.selection_type_;
119 base_is_first_ = other.base_is_first_; 116 base_is_first_ = other.base_is_first_;
120 is_directional_ = other.is_directional_; 117 is_directional_ = other.is_directional_;
121 granularity_ = other.granularity_; 118 granularity_ = other.granularity_;
122 has_trailing_whitespace_ = other.has_trailing_whitespace_;
123 return *this; 119 return *this;
124 } 120 }
125 121
126 template <typename Strategy> 122 template <typename Strategy>
127 SelectionTemplate<Strategy> VisibleSelectionTemplate<Strategy>::AsSelection() 123 SelectionTemplate<Strategy> VisibleSelectionTemplate<Strategy>::AsSelection()
128 const { 124 const {
129 typename SelectionTemplate<Strategy>::Builder builder; 125 typename SelectionTemplate<Strategy>::Builder builder;
130 if (base_.IsNotNull()) 126 if (base_.IsNotNull())
131 builder.SetBaseAndExtent(base_, extent_); 127 builder.SetBaseAndExtent(base_, extent_);
132 return builder.SetAffinity(affinity_) 128 return builder.SetAffinity(affinity_)
133 .SetGranularity(granularity_) 129 .SetGranularity(granularity_)
134 .SetIsDirectional(is_directional_) 130 .SetIsDirectional(is_directional_)
135 .SetHasTrailingWhitespace(has_trailing_whitespace_)
136 .Build(); 131 .Build();
137 } 132 }
138 133
139 EphemeralRange FirstEphemeralRangeOf(const VisibleSelection& selection) { 134 EphemeralRange FirstEphemeralRangeOf(const VisibleSelection& selection) {
140 if (selection.IsNone()) 135 if (selection.IsNone())
141 return EphemeralRange(); 136 return EphemeralRange();
142 Position start = selection.Start().ParentAnchoredEquivalent(); 137 Position start = selection.Start().ParentAnchoredEquivalent();
143 Position end = selection.End().ParentAnchoredEquivalent(); 138 Position end = selection.End().ParentAnchoredEquivalent();
144 return EphemeralRange(start, end); 139 return EphemeralRange(start, end);
145 } 140 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 VisibleSelectionTemplate<Strategy>::AppendTrailingWhitespace() const { 179 VisibleSelectionTemplate<Strategy>::AppendTrailingWhitespace() const {
185 if (IsNone()) 180 if (IsNone())
186 return *this; 181 return *this;
187 DCHECK_EQ(granularity_, kWordGranularity); 182 DCHECK_EQ(granularity_, kWordGranularity);
188 if (!IsRange()) 183 if (!IsRange())
189 return *this; 184 return *this;
190 const PositionTemplate<Strategy>& new_end = SkipWhitespace(end_); 185 const PositionTemplate<Strategy>& new_end = SkipWhitespace(end_);
191 if (end_ == new_end) 186 if (end_ == new_end)
192 return *this; 187 return *this;
193 VisibleSelectionTemplate<Strategy> result = *this; 188 VisibleSelectionTemplate<Strategy> result = *this;
194 result.has_trailing_whitespace_ = true;
yoichio 2017/07/05 01:42:19 |has_trailing_whitespace_| would not be |true| unl
195 result.end_ = new_end; 189 result.end_ = new_end;
196 return result; 190 return result;
197 } 191 }
198 192
199 template <typename Strategy> 193 template <typename Strategy>
200 void VisibleSelectionTemplate<Strategy>::SetBaseAndExtentToDeepEquivalents() { 194 void VisibleSelectionTemplate<Strategy>::SetBaseAndExtentToDeepEquivalents() {
201 // Move the selection to rendered positions, if possible. 195 // Move the selection to rendered positions, if possible.
202 bool base_and_extent_equal = base_ == extent_; 196 bool base_and_extent_equal = base_ == extent_;
203 if (base_.IsNotNull()) { 197 if (base_.IsNotNull()) {
204 base_ = CreateVisiblePosition(base_, affinity_).DeepEquivalent(); 198 base_ = CreateVisiblePosition(base_, affinity_).DeepEquivalent();
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 if (selection_type_ != kCaretSelection) 400 if (selection_type_ != kCaretSelection)
407 affinity_ = TextAffinity::kDownstream; 401 affinity_ = TextAffinity::kDownstream;
408 } 402 }
409 403
410 template <typename Strategy> 404 template <typename Strategy>
411 void VisibleSelectionTemplate<Strategy>::Validate(TextGranularity granularity) { 405 void VisibleSelectionTemplate<Strategy>::Validate(TextGranularity granularity) {
412 DCHECK(!NeedsLayoutTreeUpdate(base_)); 406 DCHECK(!NeedsLayoutTreeUpdate(base_));
413 DCHECK(!NeedsLayoutTreeUpdate(extent_)); 407 DCHECK(!NeedsLayoutTreeUpdate(extent_));
414 // TODO(xiaochengh): Add a DocumentLifecycle::DisallowTransitionScope here. 408 // TODO(xiaochengh): Add a DocumentLifecycle::DisallowTransitionScope here.
415 409
416 granularity_ = granularity;
417 if (granularity_ != kWordGranularity)
418 has_trailing_whitespace_ = false;
419 SetBaseAndExtentToDeepEquivalents(); 410 SetBaseAndExtentToDeepEquivalents();
420 if (base_.IsNull() || extent_.IsNull()) { 411 if (base_.IsNull() || extent_.IsNull()) {
421 base_ = extent_ = start_ = end_ = PositionTemplate<Strategy>(); 412 base_ = extent_ = start_ = end_ = PositionTemplate<Strategy>();
422 UpdateSelectionType(); 413 UpdateSelectionType();
423 return; 414 return;
424 } 415 }
425 416
426 const PositionTemplate<Strategy> start = base_is_first_ ? base_ : extent_; 417 const PositionTemplate<Strategy> start = base_is_first_ ? base_ : extent_;
427 const PositionTemplate<Strategy> new_start = 418 const PositionTemplate<Strategy> new_start =
428 ComputeStartRespectingGranularity( 419 ComputeStartRespectingGranularity(
(...skipping 17 matching lines...) Expand all
446 // it is useful to make to make the selection "canonical" (if only for 437 // it is useful to make to make the selection "canonical" (if only for
447 // purposes of comparing selections). This is an ideal point of the code 438 // purposes of comparing selections). This is an ideal point of the code
448 // to do this operation, since all selection changes that result in a 439 // to do this operation, since all selection changes that result in a
449 // RANGE come through here before anyone uses it. 440 // RANGE come through here before anyone uses it.
450 // TODO(yosin) Canonicalizing is good, but haven't we already done it 441 // TODO(yosin) Canonicalizing is good, but haven't we already done it
451 // (when we set these two positions to |VisiblePosition| 442 // (when we set these two positions to |VisiblePosition|
452 // |deepEquivalent()|s above)? 443 // |deepEquivalent()|s above)?
453 start_ = MostForwardCaretPosition(start_); 444 start_ = MostForwardCaretPosition(start_);
454 end_ = MostBackwardCaretPosition(end_); 445 end_ = MostBackwardCaretPosition(end_);
455 } 446 }
456 if (!has_trailing_whitespace_)
457 return;
458 *this = AppendTrailingWhitespace();
459 } 447 }
460 448
461 template <typename Strategy> 449 template <typename Strategy>
462 bool VisibleSelectionTemplate<Strategy>::IsValidFor( 450 bool VisibleSelectionTemplate<Strategy>::IsValidFor(
463 const Document& document) const { 451 const Document& document) const {
464 if (IsNone()) 452 if (IsNone())
465 return true; 453 return true;
466 454
467 return base_.GetDocument() == &document && !base_.IsOrphan() && 455 return base_.GetDocument() == &document && !base_.IsOrphan() &&
468 !extent_.IsOrphan() && !start_.IsOrphan() && !end_.IsOrphan(); 456 !extent_.IsOrphan() && !start_.IsOrphan() && !end_.IsOrphan();
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 767
780 void showTree(const blink::VisibleSelectionInFlatTree& sel) { 768 void showTree(const blink::VisibleSelectionInFlatTree& sel) {
781 sel.ShowTreeForThis(); 769 sel.ShowTreeForThis();
782 } 770 }
783 771
784 void showTree(const blink::VisibleSelectionInFlatTree* sel) { 772 void showTree(const blink::VisibleSelectionInFlatTree* sel) {
785 if (sel) 773 if (sel)
786 sel->ShowTreeForThis(); 774 sel->ShowTreeForThis();
787 } 775 }
788 #endif 776 #endif
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/VisibleSelection.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698