OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 details_(TextCompositionMarkerDetails::Create(underline_color, | 168 details_(TextCompositionMarkerDetails::Create(underline_color, |
169 thick, | 169 thick, |
170 background_color)) {} | 170 background_color)) {} |
171 | 171 |
172 DocumentMarker::DocumentMarker(const DocumentMarker& marker) | 172 DocumentMarker::DocumentMarker(const DocumentMarker& marker) |
173 : type_(marker.GetType()), | 173 : type_(marker.GetType()), |
174 start_offset_(marker.StartOffset()), | 174 start_offset_(marker.StartOffset()), |
175 end_offset_(marker.EndOffset()), | 175 end_offset_(marker.EndOffset()), |
176 details_(marker.Details()) {} | 176 details_(marker.Details()) {} |
177 | 177 |
| 178 Optional<DocumentMarker::MarkerOffsets> |
| 179 DocumentMarker::ComputeOffsetsAfterShift(unsigned offset, |
| 180 unsigned old_length, |
| 181 unsigned new_length) const { |
| 182 MarkerOffsets result; |
| 183 result.start_offset = StartOffset(); |
| 184 result.end_offset = EndOffset(); |
| 185 |
| 186 // algorithm inspired by https://dom.spec.whatwg.org/#concept-cd-replace |
| 187 // but with some changes |
| 188 |
| 189 // Deviation from the concept-cd-replace algorithm: second condition in the |
| 190 // next line (don't include text inserted immediately before a marker in the |
| 191 // marked range, but do include the new text if it's replacing text in the |
| 192 // marked range) |
| 193 if (StartOffset() > offset || (StartOffset() == offset && old_length == 0)) { |
| 194 if (StartOffset() <= offset + old_length) { |
| 195 // Marker start was in the replaced text. Move to end of new text |
| 196 // (Deviation from the concept-cd-replace algorithm: that algorithm |
| 197 // would move to the beginning of the new text here) |
| 198 result.start_offset = offset + new_length; |
| 199 } else { |
| 200 // Marker start was after the replaced text. Shift by length |
| 201 // difference |
| 202 result.start_offset = StartOffset() + new_length - old_length; |
| 203 } |
| 204 } |
| 205 |
| 206 if (EndOffset() > offset) { |
| 207 // Deviation from the concept-cd-replace algorithm: < instead of <= in |
| 208 // the next line |
| 209 if (EndOffset() < offset + old_length) { |
| 210 // Marker end was in the replaced text. Move to beginning of new text |
| 211 result.end_offset = offset; |
| 212 } else { |
| 213 // Marker end was after the replaced text. Shift by length difference |
| 214 result.end_offset = EndOffset() + new_length - old_length; |
| 215 } |
| 216 } |
| 217 |
| 218 if (result.start_offset >= result.end_offset) |
| 219 return WTF::kNullopt; |
| 220 |
| 221 return result; |
| 222 } |
| 223 |
178 void DocumentMarker::ShiftOffsets(int delta) { | 224 void DocumentMarker::ShiftOffsets(int delta) { |
179 start_offset_ += delta; | 225 start_offset_ += delta; |
180 end_offset_ += delta; | 226 end_offset_ += delta; |
181 } | 227 } |
182 | 228 |
183 void DocumentMarker::SetIsActiveMatch(bool active) { | 229 void DocumentMarker::SetIsActiveMatch(bool active) { |
184 details_ = DocumentMarkerTextMatch::Create( | 230 details_ = DocumentMarkerTextMatch::Create( |
185 active ? DocumentMarker::MatchStatus::kActive | 231 active ? DocumentMarker::MatchStatus::kActive |
186 : DocumentMarker::MatchStatus::kInactive); | 232 : DocumentMarker::MatchStatus::kInactive); |
187 } | 233 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 ToTextCompositionMarkerDetails(details_.Get())) | 265 ToTextCompositionMarkerDetails(details_.Get())) |
220 return details->BackgroundColor(); | 266 return details->BackgroundColor(); |
221 return Color::kTransparent; | 267 return Color::kTransparent; |
222 } | 268 } |
223 | 269 |
224 DEFINE_TRACE(DocumentMarker) { | 270 DEFINE_TRACE(DocumentMarker) { |
225 visitor->Trace(details_); | 271 visitor->Trace(details_); |
226 } | 272 } |
227 | 273 |
228 } // namespace blink | 274 } // namespace blink |
OLD | NEW |