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

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

Issue 2924793002: Refactor CanBe{Back,For}wardCaretPosition() (Closed)
Patch Set: 2017-06-06T17:25:09 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 | 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, 2007, 2008, 2009 Apple Inc. All rights 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights
3 * reserved. 3 * reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 2018 matching lines...) Expand 10 before | Expand all | Expand 10 after
2029 2029
2030 if (CanBeBackwardCaretPosition(text_layout_object, 2030 if (CanBeBackwardCaretPosition(text_layout_object,
2031 current_pos.OffsetInLeafNode())) { 2031 current_pos.OffsetInLeafNode())) {
2032 return current_pos.ComputePosition(); 2032 return current_pos.ComputePosition();
2033 } 2033 }
2034 } 2034 }
2035 } 2035 }
2036 return last_visible.DeprecatedComputePosition(); 2036 return last_visible.DeprecatedComputePosition();
2037 } 2037 }
2038 2038
2039 // The text continues on the next line only if the last text box is not on this
2040 // line and none of the boxes on this line have a larger start offset.
2041 static bool DoesContinueOnNextLine(const LayoutText& text_layout_object,
2042 InlineBox* box,
2043 unsigned text_offset) {
2044 InlineTextBox* const last_text_box = text_layout_object.LastTextBox();
2045 DCHECK_NE(box, last_text_box);
2046 for (InlineBox* runner = box->NextLeafChild(); runner;
2047 runner = runner->NextLeafChild()) {
2048 if (runner == last_text_box)
2049 return false;
2050 if (LineLayoutAPIShim::LayoutObjectFrom(runner->GetLineLayoutItem()) ==
2051 text_layout_object &&
2052 ToInlineTextBox(runner)->Start() >= text_offset)
2053 return false;
2054 }
2055
2056 for (InlineBox* runner = box->PrevLeafChild(); runner;
2057 runner = runner->PrevLeafChild()) {
2058 if (runner == last_text_box)
2059 return false;
2060 if (LineLayoutAPIShim::LayoutObjectFrom(runner->GetLineLayoutItem()) ==
2061 text_layout_object &&
2062 ToInlineTextBox(runner)->Start() >= text_offset)
2063 return false;
2064 }
2065
2066 return true;
2067 }
2068
2039 // TODO(editing-dev): This function is just moved out from 2069 // TODO(editing-dev): This function is just moved out from
2040 // |MostBackwardCaretPosition()|. We should study this function more and 2070 // |MostBackwardCaretPosition()|. We should study this function more and
2041 // name it appropriately. See https://trac.webkit.org/changeset/32438/ 2071 // name it appropriately. See https://trac.webkit.org/changeset/32438/
2042 // which introduce this. 2072 // which introduce this.
2043 static bool CanBeBackwardCaretPosition(const LayoutText* text_layout_object, 2073 static bool CanBeBackwardCaretPosition(const LayoutText* text_layout_object,
2044 int offset_in_node) { 2074 int offset_in_node) {
2045 const unsigned text_start_offset = text_layout_object->TextStartOffset(); 2075 const unsigned text_start_offset = text_layout_object->TextStartOffset();
2046 DCHECK_GE(offset_in_node, static_cast<int>(text_start_offset)); 2076 DCHECK_GE(offset_in_node, static_cast<int>(text_start_offset));
2047 const unsigned text_offset = offset_in_node - text_start_offset; 2077 const unsigned text_offset = offset_in_node - text_start_offset;
2048 InlineTextBox* const last_text_box = text_layout_object->LastTextBox(); 2078 InlineTextBox* const last_text_box = text_layout_object->LastTextBox();
(...skipping 19 matching lines...) Expand all
2068 } 2098 }
2069 if (text_offset <= box->Start() + box->Len()) { 2099 if (text_offset <= box->Start() + box->Len()) {
2070 if (text_offset > box->Start()) 2100 if (text_offset > box->Start())
2071 return true; 2101 return true;
2072 continue; 2102 continue;
2073 } 2103 }
2074 2104
2075 if (box == last_text_box || text_offset != box->Start() + box->Len() + 1) 2105 if (box == last_text_box || text_offset != box->Start() + box->Len() + 1)
2076 continue; 2106 continue;
2077 2107
2078 // TODO(yosin): We should move below code fragment into 2108 if (DoesContinueOnNextLine(*text_layout_object, box, text_offset + 1))
2079 // |DoesContinueOnNextLine()|. Note: |MostForwardCaretPosition()| has
2080 // same code fragment except for comparison on |text_offset|.
2081 // Backward: other_box->Start() > text_offset
2082 // Forward: other_box->Start() >= text_offset
2083 // The text continues on the next line only if the last text box is not
2084 // on this line and none of the boxes on this line have a larger start
2085 // offset.
2086 bool continues_on_next_line = true;
2087 InlineBox* other_box = box;
2088 while (continues_on_next_line) {
2089 other_box = other_box->NextLeafChild();
2090 if (!other_box)
2091 break;
2092 if (other_box == last_text_box ||
2093 (LineLayoutAPIShim::LayoutObjectFrom(
2094 other_box->GetLineLayoutItem()) == text_layout_object &&
2095 ToInlineTextBox(other_box)->Start() > text_offset))
2096 continues_on_next_line = false;
2097 }
2098
2099 other_box = box;
2100 while (continues_on_next_line) {
2101 other_box = other_box->PrevLeafChild();
2102 if (!other_box)
2103 break;
2104 if (other_box == last_text_box ||
2105 (LineLayoutAPIShim::LayoutObjectFrom(
2106 other_box->GetLineLayoutItem()) == text_layout_object &&
2107 ToInlineTextBox(other_box)->Start() > text_offset))
2108 continues_on_next_line = false;
2109 }
2110
2111 if (continues_on_next_line)
2112 return true; 2109 return true;
2113 } 2110 }
2114 return false; 2111 return false;
2115 } 2112 }
2116 2113
2117 Position MostBackwardCaretPosition(const Position& position, 2114 Position MostBackwardCaretPosition(const Position& position,
2118 EditingBoundaryCrossingRule rule) { 2115 EditingBoundaryCrossingRule rule) {
2119 return MostBackwardCaretPosition<EditingStrategy>(position, rule); 2116 return MostBackwardCaretPosition<EditingStrategy>(position, rule);
2120 } 2117 }
2121 2118
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
2240 box = box->NextTextBox()) { 2237 box = box->NextTextBox()) {
2241 if (text_offset <= box->end()) { 2238 if (text_offset <= box->end()) {
2242 if (text_offset >= box->Start()) 2239 if (text_offset >= box->Start())
2243 return true; 2240 return true;
2244 continue; 2241 continue;
2245 } 2242 }
2246 2243
2247 if (box == last_text_box || text_offset != box->Start() + box->Len()) 2244 if (box == last_text_box || text_offset != box->Start() + box->Len())
2248 continue; 2245 continue;
2249 2246
2250 // TODO(yosin): We should move below code fragment into 2247 if (DoesContinueOnNextLine(*text_layout_object, box, text_offset))
2251 // |DoesContinueOnNextLine()|. Note: |MostBackwardCaretPosition()| has
2252 // same code fragment except for comparison on |text_offset|.
2253 // Backward: other_box->Start() > text_offset
2254 // Forward: other_box->Start() >= text_offset
2255 // The text continues on the next line only if the last text box is not
2256 // on this line and none of the boxes on this line have a larger start
2257 // offset.
2258 bool continues_on_next_line = true;
2259 InlineBox* other_box = box;
2260 while (continues_on_next_line) {
2261 other_box = other_box->NextLeafChild();
2262 if (!other_box)
2263 break;
2264 if (other_box == last_text_box ||
2265 (LineLayoutAPIShim::LayoutObjectFrom(
2266 other_box->GetLineLayoutItem()) == text_layout_object &&
2267 ToInlineTextBox(other_box)->Start() >= text_offset))
2268 continues_on_next_line = false;
2269 }
2270
2271 other_box = box;
2272 while (continues_on_next_line) {
2273 other_box = other_box->PrevLeafChild();
2274 if (!other_box)
2275 break;
2276 if (other_box == last_text_box ||
2277 (LineLayoutAPIShim::LayoutObjectFrom(
2278 other_box->GetLineLayoutItem()) == text_layout_object &&
2279 ToInlineTextBox(other_box)->Start() >= text_offset))
2280 continues_on_next_line = false;
2281 }
2282
2283 if (continues_on_next_line)
2284 return true; 2248 return true;
2285 } 2249 }
2286 return false; 2250 return false;
2287 } 2251 }
2288 2252
2289 Position MostForwardCaretPosition(const Position& position, 2253 Position MostForwardCaretPosition(const Position& position,
2290 EditingBoundaryCrossingRule rule) { 2254 EditingBoundaryCrossingRule rule) {
2291 return MostForwardCaretPosition<EditingStrategy>(position, rule); 2255 return MostForwardCaretPosition<EditingStrategy>(position, rule);
2292 } 2256 }
2293 2257
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after
3165 3129
3166 IntRect ComputeTextRect(const EphemeralRangeInFlatTree& range) { 3130 IntRect ComputeTextRect(const EphemeralRangeInFlatTree& range) {
3167 return EnclosingIntRect(ComputeTextRectTemplate(range)); 3131 return EnclosingIntRect(ComputeTextRectTemplate(range));
3168 } 3132 }
3169 3133
3170 FloatRect ComputeTextFloatRect(const EphemeralRange& range) { 3134 FloatRect ComputeTextFloatRect(const EphemeralRange& range) {
3171 return ComputeTextRectTemplate(range); 3135 return ComputeTextRectTemplate(range);
3172 } 3136 }
3173 3137
3174 } // namespace blink 3138 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698