OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/editing/markers/DocumentMarkerList.h" |
| 6 |
| 7 #include "core/editing/markers/DocumentMarkerController.h" |
| 8 |
| 9 namespace blink { |
| 10 |
| 11 DocumentMarkerList::DocumentMarkerList( |
| 12 DocumentMarkerController* documentMarkerController) |
| 13 : m_documentMarkerController(documentMarkerController) {} |
| 14 |
| 15 bool DocumentMarkerList::endsBefore(size_t startOffset, |
| 16 const Member<DocumentMarker>& rhv) { |
| 17 return startOffset < rhv->endOffset(); |
| 18 } |
| 19 |
| 20 bool DocumentMarkerList::compareByStart(const Member<DocumentMarker>& lhv, |
| 21 const Member<DocumentMarker>& rhv) { |
| 22 return lhv->startOffset() < rhv->startOffset(); |
| 23 } |
| 24 |
| 25 DocumentMarkerList::ShiftMarkerResult |
| 26 DocumentMarkerList::getShiftedMarkerPosition(const DocumentMarker& marker, |
| 27 unsigned offset, |
| 28 unsigned oldLength, |
| 29 unsigned newLength) { |
| 30 ShiftMarkerResult result; |
| 31 result.newStartOffset = marker.startOffset(); |
| 32 result.newEndOffset = marker.endOffset(); |
| 33 result.shouldRemoveMarker = false; |
| 34 |
| 35 // algorithm inspired by https://dom.spec.whatwg.org/#concept-cd-replace |
| 36 // but with some changes |
| 37 if (marker.startOffset() > offset) { |
| 38 // Deviation from the concept-cd-replace algorithm: < instead of <= in |
| 39 // the next line |
| 40 if (marker.startOffset() < offset + oldLength) { |
| 41 // Marker start was in the replaced text. Move to end of new text |
| 42 // (Deviation from the concept-cd-replace algorithm: that algorithm |
| 43 // would move to the beginning of the new text here) |
| 44 result.newStartOffset = offset + newLength; |
| 45 } else { |
| 46 // Marker start was after the replaced text. Shift by length |
| 47 // difference |
| 48 result.newStartOffset = marker.startOffset() + newLength - oldLength; |
| 49 } |
| 50 } |
| 51 |
| 52 if (marker.endOffset() > offset) { |
| 53 // Deviation from the concept-cd-replace algorithm: < instead of <= in |
| 54 // the next line |
| 55 if (marker.endOffset() < offset + oldLength) { |
| 56 // Marker end was in the replaced text. Move to beginning of new text |
| 57 result.newEndOffset = offset; |
| 58 } else { |
| 59 // Marker end was after the replaced text. Shift by length difference |
| 60 result.newEndOffset = marker.endOffset() + newLength - oldLength; |
| 61 } |
| 62 } |
| 63 |
| 64 if (result.newStartOffset >= result.newEndOffset) |
| 65 result.shouldRemoveMarker = true; |
| 66 |
| 67 return result; |
| 68 } |
| 69 |
| 70 DEFINE_TRACE(DocumentMarkerList) { |
| 71 visitor->trace(m_documentMarkerController); |
| 72 } |
| 73 |
| 74 } // namespace blink |
OLD | NEW |