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

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

Issue 2907963002: [Smart Text] Make SurroundingText work for input elements (Closed)
Patch Set: Added test 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/editing/SurroundingTextTest.cpp » ('j') | 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) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 DCHECK_EQ(start_position.GetDocument(), end_position.GetDocument()); 55 DCHECK_EQ(start_position.GetDocument(), end_position.GetDocument());
56 56
57 const unsigned half_max_length = max_length / 2; 57 const unsigned half_max_length = max_length / 2;
58 58
59 Document* document = start_position.GetDocument(); 59 Document* document = start_position.GetDocument();
60 // The position will have no document if it is null (as in no position). 60 // The position will have no document if it is null (as in no position).
61 if (!document || !document->documentElement()) 61 if (!document || !document->documentElement())
62 return; 62 return;
63 DCHECK(!document->NeedsLayoutTreeUpdate()); 63 DCHECK(!document->NeedsLayoutTreeUpdate());
64 64
65 // The forward range starts at the selection end and ends at the document's 65 // The forward range starts at the selection end and ends at the end of the
66 // end. It will then be updated to only contain the text in the text in the 66 // root container: either document's end or the end of shadow root for input
67 // right range around the selection. 67 // elements. It will then be updated to only contain the text in the text in
68 // the right range around the selection.
69
70 Node* end_root_container = end_position.ComputeContainerNode();
yosin_UTC9 2017/06/05 04:59:32 You can do this by Node* end_container = end_pos
Tima Vaisburd 2017/06/06 23:39:09 I used RootEditableElementOf() eventually, I thoug
71 while (end_root_container->parentNode())
72 end_root_container = end_root_container->parentNode();
73
74 Node* last_node = end_root_container->IsShadowRoot()
yosin_UTC9 2017/06/05 04:59:32 The name |last_node| is confusion. Usually, docume
Tima Vaisburd 2017/06/06 23:39:09 Not used anymore.
75 ? end_root_container
76 : document->documentElement();
77
68 CharacterIterator forward_iterator( 78 CharacterIterator forward_iterator(
69 end_position, 79 end_position,
70 Position::LastPositionInNode(document->documentElement()) 80 Position::LastPositionInNode(last_node).ParentAnchoredEquivalent(),
71 .ParentAnchoredEquivalent(),
72 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); 81 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build());
73 // FIXME: why do we stop going trough the text if we were not able to select 82 // FIXME: why do we stop going trough the text if we were not able to select
74 // something on the right? 83 // something on the right?
75 if (!forward_iterator.AtEnd()) 84 if (!forward_iterator.AtEnd())
76 forward_iterator.Advance(max_length - half_max_length); 85 forward_iterator.Advance(max_length - half_max_length);
77 86
78 EphemeralRange forward_range = forward_iterator.Range(); 87 EphemeralRange forward_range = forward_iterator.Range();
79 if (forward_range.IsNull() || 88 if (forward_range.IsNull() ||
80 !Range::Create(*document, end_position, forward_range.StartPosition()) 89 !Range::Create(*document, end_position, forward_range.StartPosition())
81 ->GetText() 90 ->GetText()
82 .length()) 91 .length())
83 return; 92 return;
84 93
94 Node* start_root_container = start_position.ComputeContainerNode();
95 while (start_root_container->parentNode())
96 start_root_container = start_root_container->parentNode();
97
98 Node* first_node = start_root_container->IsShadowRoot()
99 ? start_root_container
100 : document->documentElement();
101
85 // Same as with the forward range but with the backward range. The range 102 // Same as with the forward range but with the backward range. The range
86 // starts at the document's start and ends at the selection start and will 103 // starts at the document's start and ends at the selection start and will
87 // be updated. 104 // be updated.
88 BackwardsCharacterIterator backwards_iterator( 105 BackwardsCharacterIterator backwards_iterator(
89 Position::FirstPositionInNode(document->documentElement()) 106 Position::FirstPositionInNode(first_node).ParentAnchoredEquivalent(),
90 .ParentAnchoredEquivalent(),
91 start_position, 107 start_position,
92 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); 108 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build());
93 if (!backwards_iterator.AtEnd()) 109 if (!backwards_iterator.AtEnd())
94 backwards_iterator.Advance(half_max_length); 110 backwards_iterator.Advance(half_max_length);
95 111
96 const TextIteratorBehavior behavior = 112 const TextIteratorBehavior behavior =
97 TextIteratorBehavior::NoTrailingSpaceRangeLengthBehavior(); 113 TextIteratorBehavior::NoTrailingSpaceRangeLengthBehavior();
98 start_offset_in_content_ = TextIterator::RangeLength( 114 start_offset_in_content_ = TextIterator::RangeLength(
99 backwards_iterator.EndPosition(), start_position, behavior); 115 backwards_iterator.EndPosition(), start_position, behavior);
100 end_offset_in_content_ = TextIterator::RangeLength( 116 end_offset_in_content_ = TextIterator::RangeLength(
(...skipping 15 matching lines...) Expand all
116 132
117 unsigned SurroundingText::StartOffsetInContent() const { 133 unsigned SurroundingText::StartOffsetInContent() const {
118 return start_offset_in_content_; 134 return start_offset_in_content_;
119 } 135 }
120 136
121 unsigned SurroundingText::EndOffsetInContent() const { 137 unsigned SurroundingText::EndOffsetInContent() const {
122 return end_offset_in_content_; 138 return end_offset_in_content_;
123 } 139 }
124 140
125 } // namespace blink 141 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/editing/SurroundingTextTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698