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

Unified Diff: third_party/WebKit/Source/core/editing/markers/DocumentMarker.cpp

Issue 2755013004: Improve how DocumentMarkerController updates markers in response to text edits (Closed)
Patch Set: Rebase Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/markers/DocumentMarker.cpp
diff --git a/third_party/WebKit/Source/core/editing/markers/DocumentMarker.cpp b/third_party/WebKit/Source/core/editing/markers/DocumentMarker.cpp
index 05b050da46abe8031ba414e6f9d4e485f18b0d77..a57a48ef25be3e26505d70ea9583cbebabfd343e 100644
--- a/third_party/WebKit/Source/core/editing/markers/DocumentMarker.cpp
+++ b/third_party/WebKit/Source/core/editing/markers/DocumentMarker.cpp
@@ -175,6 +175,52 @@ DocumentMarker::DocumentMarker(const DocumentMarker& marker)
end_offset_(marker.EndOffset()),
details_(marker.Details()) {}
+Optional<DocumentMarker::MarkerOffsets>
+DocumentMarker::ComputeOffsetsAfterShift(unsigned offset,
+ unsigned old_length,
+ unsigned new_length) const {
+ MarkerOffsets result;
+ result.start_offset = StartOffset();
+ result.end_offset = EndOffset();
+
+ // algorithm inspired by https://dom.spec.whatwg.org/#concept-cd-replace
+ // but with some changes
+
+ // Deviation from the concept-cd-replace algorithm: second condition in the
+ // next line (don't include text inserted immediately before a marker in the
+ // marked range, but do include the new text if it's replacing text in the
+ // marked range)
+ if (StartOffset() > offset || (StartOffset() == offset && old_length == 0)) {
+ if (StartOffset() <= offset + old_length) {
+ // Marker start was in the replaced text. Move to end of new text
+ // (Deviation from the concept-cd-replace algorithm: that algorithm
+ // would move to the beginning of the new text here)
+ result.start_offset = offset + new_length;
+ } else {
+ // Marker start was after the replaced text. Shift by length
+ // difference
+ result.start_offset = StartOffset() + new_length - old_length;
+ }
+ }
+
+ if (EndOffset() > offset) {
+ // Deviation from the concept-cd-replace algorithm: < instead of <= in
+ // the next line
+ if (EndOffset() < offset + old_length) {
+ // Marker end was in the replaced text. Move to beginning of new text
+ result.end_offset = offset;
+ } else {
+ // Marker end was after the replaced text. Shift by length difference
+ result.end_offset = EndOffset() + new_length - old_length;
+ }
+ }
+
+ if (result.start_offset >= result.end_offset)
+ return WTF::kNullopt;
+
+ return result;
+}
+
void DocumentMarker::ShiftOffsets(int delta) {
start_offset_ += delta;
end_offset_ += delta;

Powered by Google App Engine
This is Rietveld 408576698