Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 * (C) 2001 Dirk Mueller (mueller@kde.org) | 4 * (C) 2001 Dirk Mueller (mueller@kde.org) |
| 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) | 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
| 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
| 7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) | 7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) |
| 8 * Copyright (C) Research In Motion Limited 2010. All rights reserved. | 8 * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
| 9 * | 9 * |
| 10 * This library is free software; you can redistribute it and/or | 10 * This library is free software; you can redistribute it and/or |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 return; | 126 return; |
| 127 ASSERT(!m_markers.isEmpty()); | 127 ASSERT(!m_markers.isEmpty()); |
| 128 | 128 |
| 129 RefPtr<Range> textPiece = markedText.range(); | 129 RefPtr<Range> textPiece = markedText.range(); |
| 130 int startOffset = textPiece->startOffset(); | 130 int startOffset = textPiece->startOffset(); |
| 131 int endOffset = textPiece->endOffset(); | 131 int endOffset = textPiece->endOffset(); |
| 132 removeMarkers(textPiece->startContainer(), startOffset, endOffset - star tOffset, markerTypes, shouldRemovePartiallyOverlappingMarker); | 132 removeMarkers(textPiece->startContainer(), startOffset, endOffset - star tOffset, markerTypes, shouldRemovePartiallyOverlappingMarker); |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 static bool startsFurther(const DocumentMarker& lhv, const DocumentMarker& rhv) | |
| 137 { | |
| 138 return lhv.startOffset() < rhv.startOffset(); | |
| 139 } | |
| 140 | |
| 141 static bool notOverlaps(const DocumentMarker& lhv, const DocumentMarker& rhv) | |
|
tony
2013/08/29 22:44:45
Nit: doesNotOverlap sounds better to me than notOv
| |
| 142 { | |
| 143 return lhv.endOffset() < rhv.startOffset(); | |
| 144 } | |
| 145 | |
| 136 // Markers are stored in order sorted by their start offset. | 146 // Markers are stored in order sorted by their start offset. |
| 137 // Markers of the same type do not overlap each other. | 147 // Markers of the same type do not overlap each other. |
| 138 | 148 |
| 139 void DocumentMarkerController::addMarker(Node* node, const DocumentMarker& newMa rker) | 149 void DocumentMarkerController::addMarker(Node* node, const DocumentMarker& newMa rker) |
| 140 { | 150 { |
| 141 ASSERT(newMarker.endOffset() >= newMarker.startOffset()); | 151 ASSERT(newMarker.endOffset() >= newMarker.startOffset()); |
| 142 if (newMarker.endOffset() == newMarker.startOffset()) | 152 if (newMarker.endOffset() == newMarker.startOffset()) |
| 143 return; | 153 return; |
| 144 | 154 |
| 145 m_possiblyExistingMarkerTypes.add(newMarker.type()); | 155 m_possiblyExistingMarkerTypes.add(newMarker.type()); |
| 146 | 156 |
| 147 OwnPtr<MarkerList>& list = m_markers.add(node, nullptr).iterator->value; | 157 OwnPtr<MarkerList>& list = m_markers.add(node, nullptr).iterator->value; |
| 148 | 158 |
| 149 if (!list) { | 159 if (!list) { |
| 150 list = adoptPtr(new MarkerList); | 160 list = adoptPtr(new MarkerList); |
| 151 list->append(RenderedDocumentMarker(newMarker)); | 161 list->append(RenderedDocumentMarker(newMarker)); |
| 152 } else { | 162 } else { |
| 153 RenderedDocumentMarker toInsert(newMarker); | 163 RenderedDocumentMarker toInsert(newMarker); |
| 154 size_t numMarkers = list->size(); | 164 if (list->last().endOffset() <= newMarker.startOffset()) { |
| 155 size_t i; | 165 if (toInsert.type() != DocumentMarker::TextMatch) { |
|
tony
2013/08/29 22:44:45
Can we move this block into a helper function? Th
| |
| 156 // Iterate over all markers whose start offset is less than or equal to the new marker's. | 166 // Iterate over all markers whose start offset is less than or e qual to the new marker's. |
| 157 // If one of them is of the same type as the new marker and touches it o r intersects with it | 167 // If one of them is of the same type as the new marker and touc hes it or intersects with it |
| 158 // (there is at most one), remove it and adjust the new marker's start o ffset to encompass it. | 168 // (there is at most one), remove it and adjust the new marker's start offset to encompass it. |
| 159 for (i = 0; i < numMarkers; ++i) { | 169 size_t numMarkers = list->size(); |
| 160 DocumentMarker marker = list->at(i); | 170 MarkerList::iterator end = std::upper_bound(list->begin(), list- >end(), toInsert, startsFurther); |
| 161 if (marker.startOffset() > toInsert.startOffset()) | 171 MarkerList::iterator overlapping = list->begin(); |
| 162 break; | 172 do { |
| 163 if (marker.type() == toInsert.type() && marker.type() != DocumentMar ker::TextMatch && marker.endOffset() >= toInsert.startOffset()) { | 173 overlapping = std::lower_bound(overlapping, end, toInsert, n otOverlaps); |
| 164 toInsert.setStartOffset(marker.startOffset()); | 174 } while (overlapping != end && overlapping->type() != toInsert.t ype() && ++overlapping != end); |
| 165 list->remove(i); | 175 |
| 166 numMarkers--; | 176 size_t i; |
|
tony
2013/08/29 22:44:45
Please use a more descriptive variable than 'i'.
| |
| 167 break; | 177 if (overlapping != end) { |
| 178 toInsert.setStartOffset(overlapping->startOffset()); | |
| 179 i = overlapping - list->begin(); | |
| 180 list->remove(i); | |
| 181 numMarkers--; | |
|
tony
2013/08/29 22:44:45
Nit: --numMarkers
| |
| 182 } else { | |
| 183 i = numMarkers; | |
|
tony
2013/08/29 22:44:45
Set i = numMarkers when initializing and you can g
| |
| 184 } | |
| 185 size_t j = i; | |
| 186 // Iterate over all markers whose end offset is less than or equ al to the new marker's, | |
| 187 // removing markers of the same type as the new marker which tou ch it or intersect with it, | |
| 188 // adjusting the new marker's end offset to cover them if necess ary. | |
| 189 while (j < numMarkers) { | |
|
tony
2013/08/29 22:44:45
Looks like you could use a for loop here so that j
| |
| 190 DocumentMarker marker = list->at(j); | |
| 191 if (marker.startOffset() > toInsert.endOffset()) | |
| 192 break; | |
| 193 if (marker.type() == toInsert.type()) { | |
| 194 list->remove(j); | |
| 195 if (toInsert.endOffset() <= marker.endOffset()) { | |
| 196 toInsert.setEndOffset(marker.endOffset()); | |
| 197 break; | |
| 198 } | |
| 199 numMarkers--; | |
|
tony
2013/08/29 22:44:45
Nit: --numMarkers
| |
| 200 } else { | |
| 201 j++; | |
|
tony
2013/08/29 22:44:45
Nit: ++j
| |
| 202 } | |
| 203 } | |
| 204 // At this point i points to the node before which we want to in sert. | |
| 205 list->insert(i, RenderedDocumentMarker(toInsert)); | |
| 206 } else { | |
| 207 MarkerList::iterator pos = std::lower_bound(list->begin(), list- >end(), toInsert, startsFurther); | |
| 208 if (pos != list->end()) | |
| 209 list->insert(pos - list->begin(), RenderedDocumentMarker(toI nsert)); | |
| 210 else | |
| 211 list->append(RenderedDocumentMarker(toInsert)); | |
| 168 } | 212 } |
| 213 } else { | |
| 214 list->append(RenderedDocumentMarker(toInsert)); | |
| 169 } | 215 } |
| 170 size_t j = i; | |
| 171 // Iterate over all markers whose end offset is less than or equal to th e new marker's, | |
| 172 // removing markers of the same type as the new marker which touch it or intersect with it, | |
| 173 // adjusting the new marker's end offset to cover them if necessary. | |
| 174 while (j < numMarkers) { | |
| 175 DocumentMarker marker = list->at(j); | |
| 176 if (marker.startOffset() > toInsert.endOffset()) | |
| 177 break; | |
| 178 if (marker.type() == toInsert.type() && marker.type() != DocumentMar ker::TextMatch) { | |
| 179 list->remove(j); | |
| 180 if (toInsert.endOffset() <= marker.endOffset()) { | |
| 181 toInsert.setEndOffset(marker.endOffset()); | |
| 182 break; | |
| 183 } | |
| 184 numMarkers--; | |
| 185 } else | |
| 186 j++; | |
| 187 } | |
| 188 // At this point i points to the node before which we want to insert. | |
| 189 list->insert(i, RenderedDocumentMarker(toInsert)); | |
| 190 } | 216 } |
| 191 | 217 |
| 192 // repaint the affected node | 218 // repaint the affected node |
| 193 if (node->renderer()) | 219 if (node->renderer()) |
| 194 node->renderer()->repaint(); | 220 node->renderer()->repaint(); |
| 195 } | 221 } |
| 196 | 222 |
| 197 // copies markers from srcNode to dstNode, applying the specified shift delta to the copies. The shift is | 223 // copies markers from srcNode to dstNode, applying the specified shift delta to the copies. The shift is |
| 198 // useful if, e.g., the caller has created the dstNode from a non-prefix substri ng of the srcNode. | 224 // useful if, e.g., the caller has created the dstNode from a non-prefix substri ng of the srcNode. |
| 199 void DocumentMarkerController::copyMarkers(Node* srcNode, unsigned startOffset, int length, Node* dstNode, int delta) | 225 void DocumentMarkerController::copyMarkers(Node* srcNode, unsigned startOffset, int length, Node* dstNode, int delta) |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 670 | 696 |
| 671 } // namespace WebCore | 697 } // namespace WebCore |
| 672 | 698 |
| 673 #ifndef NDEBUG | 699 #ifndef NDEBUG |
| 674 void showDocumentMarkers(const WebCore::DocumentMarkerController* controller) | 700 void showDocumentMarkers(const WebCore::DocumentMarkerController* controller) |
| 675 { | 701 { |
| 676 if (controller) | 702 if (controller) |
| 677 controller->showMarkers(); | 703 controller->showMarkers(); |
| 678 } | 704 } |
| 679 #endif | 705 #endif |
| OLD | NEW |