dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 | * (C) 2001 Dirk Mueller (mueller@kde.org) |
| 5 | * (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 6 | * Copyright (C) 2004-2020 Apple Inc. All rights reserved. |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) |
| 8 | * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Library General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Library General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Library General Public License |
| 21 | * along with this library; see the file COPYING.LIB. If not, write to |
| 22 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 23 | * Boston, MA 02110-1301, USA. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "DocumentMarkerController.h" |
| 29 | |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 30 | #include "Chrome.h" |
| 31 | #include "ChromeClient.h" |
cdumez@apple.com | c518f7e | 2018-04-03 18:01:41 +0000 | [diff] [blame] | 32 | #include "Frame.h" |
antti@apple.com | 5d47b58 | 2012-12-11 00:13:29 +0000 | [diff] [blame] | 33 | #include "NodeTraversal.h" |
achristensen@apple.com | e594567 | 2016-06-13 21:58:53 +0000 | [diff] [blame] | 34 | #include "Page.h" |
antti@apple.com | ee542e0 | 2014-05-22 13:05:20 +0000 | [diff] [blame] | 35 | #include "RenderBlockFlow.h" |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 36 | #include "RenderLayer.h" |
antti@apple.com | ee542e0 | 2014-05-22 13:05:20 +0000 | [diff] [blame] | 37 | #include "RenderText.h" |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 38 | #include "RenderedDocumentMarker.h" |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 39 | #include "TextIterator.h" |
rniwa@webkit.org | b015660 | 2011-06-23 01:20:45 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
rniwa@webkit.org | b015660 | 2011-06-23 01:20:45 +0000 | [diff] [blame] | 41 | |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 42 | namespace WebCore { |
| 43 | |
dbates@webkit.org | bad49a4 | 2017-07-18 02:43:07 +0000 | [diff] [blame] | 44 | inline bool DocumentMarkerController::possiblyHasMarkers(OptionSet<DocumentMarker::MarkerType> types) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 45 | { |
antti@apple.com | a10740e | 2018-08-13 19:16:10 +0000 | [diff] [blame] | 46 | return m_possiblyExistingMarkerTypes.containsAny(types); |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 47 | } |
| 48 | |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 49 | DocumentMarkerController::DocumentMarkerController(Document& document) |
| 50 | : m_document(document) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
dbates@webkit.org | f21f3ae | 2017-10-19 23:48:45 +0000 | [diff] [blame] | 54 | DocumentMarkerController::~DocumentMarkerController() = default; |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 55 | |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 56 | void DocumentMarkerController::detach() |
| 57 | { |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 58 | m_markers.clear(); |
dbates@webkit.org | bad49a4 | 2017-07-18 02:43:07 +0000 | [diff] [blame] | 59 | m_possiblyExistingMarkerTypes = { }; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 60 | } |
| 61 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 62 | auto DocumentMarkerController::collectTextRanges(const SimpleRange& range) -> Vector<TextRange> |
antti@apple.com | f6a8d6f | 2019-09-18 15:41:09 +0000 | [diff] [blame] | 63 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 64 | Vector<TextRange> ranges; |
| 65 | for (TextIterator iterator(range); !iterator.atEnd(); iterator.advance()) { |
| 66 | auto currentRange = iterator.range(); |
| 67 | ranges.append({ WTFMove(currentRange.start.container), { currentRange.start.offset, currentRange.end.offset } }); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 68 | } |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 69 | return ranges; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 70 | } |
| 71 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 72 | void DocumentMarkerController::addMarker(const SimpleRange& range, DocumentMarker::MarkerType type, const DocumentMarker::Data& data) |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 73 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 74 | for (auto& textPiece : collectTextRanges(range)) |
| 75 | addMarker(textPiece.node, { type, textPiece.range, DocumentMarker::Data { data } }); |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 78 | void DocumentMarkerController::addMarker(Text& node, unsigned startOffset, unsigned length, DocumentMarker::MarkerType type, DocumentMarker::Data&& data) |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 79 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 80 | addMarker(node, { type, { startOffset, startOffset + length }, WTFMove(data) }); |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 81 | } |
| 82 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 83 | void DocumentMarkerController::addDraggedContentMarker(const SimpleRange& range) |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 84 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 85 | // FIXME: Since the marker is already stored in a map keyed by node, we can probably change things around so we don't have to also store the node in the marker. |
| 86 | for (auto& textPiece : collectTextRanges(range)) |
| 87 | addMarker(textPiece.node, { DocumentMarker::DraggedContent, textPiece.range, RefPtr<Node> { textPiece.node.ptr() } }); |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 88 | } |
darin@apple.com | 961f2a5 | 2016-12-31 09:51:29 +0000 | [diff] [blame] | 89 | |
darin@apple.com | fed758c | 2020-07-26 19:12:30 +0000 | [diff] [blame] | 90 | void DocumentMarkerController::removeMarkers(const SimpleRange& range, OptionSet<DocumentMarker::MarkerType> types, RemovePartiallyOverlappingMarker overlapRule) |
wenson_hsieh@apple.com | 3acff53 | 2017-05-04 22:28:22 +0000 | [diff] [blame] | 91 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 92 | filterMarkers(range, nullptr, types, overlapRule); |
wenson_hsieh@apple.com | 3acff53 | 2017-05-04 22:28:22 +0000 | [diff] [blame] | 93 | } |
| 94 | |
wenson_hsieh@apple.com | 65fd03b | 2022-03-12 00:56:15 +0000 | [diff] [blame] | 95 | void DocumentMarkerController::filterMarkers(const SimpleRange& range, const Function<FilterMarkerResult(const DocumentMarker&)>& filter, OptionSet<DocumentMarker::MarkerType> types, RemovePartiallyOverlappingMarker overlapRule) |
timothy_horton@apple.com | acbaa98 | 2019-03-20 07:32:40 +0000 | [diff] [blame] | 96 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 97 | for (auto& textPiece : collectTextRanges(range)) { |
| 98 | if (!possiblyHasMarkers(types)) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 99 | return; |
| 100 | ASSERT(!m_markers.isEmpty()); |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 101 | removeMarkers(textPiece.node, textPiece.range, types, filter, overlapRule); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 105 | static void updateRenderedRectsForMarker(RenderedDocumentMarker& marker, Node& node) |
| 106 | { |
| 107 | ASSERT(!node.document().view() || !node.document().view()->needsLayout()); |
darin@apple.com | 888df88 | 2020-08-22 16:16:03 +0000 | [diff] [blame] | 108 | marker.setUnclippedAbsoluteRects(boundingBoxes(RenderObject::absoluteTextQuads(makeSimpleRange(node, marker), RenderObject::BoundingRectBehavior::UseSelectionHeight))); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void DocumentMarkerController::invalidateRectsForAllMarkers() |
| 112 | { |
| 113 | if (!hasMarkers()) |
| 114 | return; |
| 115 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 116 | for (auto& nodeMarkers : m_markers.values()) { |
| 117 | for (auto& marker : *nodeMarkers) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 118 | marker.invalidate(); |
| 119 | } |
| 120 | |
| 121 | if (Page* page = m_document.page()) |
| 122 | page->chrome().client().didInvalidateDocumentMarkerRects(); |
| 123 | } |
| 124 | |
| 125 | void DocumentMarkerController::invalidateRectsForMarkersInNode(Node& node) |
| 126 | { |
| 127 | if (!hasMarkers()) |
| 128 | return; |
| 129 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 130 | auto markers = m_markers.get(&node); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 131 | ASSERT(markers); |
| 132 | |
| 133 | for (auto& marker : *markers) |
| 134 | marker.invalidate(); |
| 135 | |
| 136 | if (Page* page = m_document.page()) |
| 137 | page->chrome().client().didInvalidateDocumentMarkerRects(); |
| 138 | } |
| 139 | |
| 140 | static void updateMainFrameLayoutIfNeeded(Document& document) |
| 141 | { |
| 142 | Frame* frame = document.frame(); |
| 143 | if (!frame) |
| 144 | return; |
| 145 | |
| 146 | FrameView* mainFrameView = frame->mainFrame().view(); |
| 147 | if (!mainFrameView) |
| 148 | return; |
| 149 | |
| 150 | mainFrameView->updateLayoutAndStyleIfNeededRecursive(); |
| 151 | } |
| 152 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 153 | void DocumentMarkerController::updateRectsForInvalidatedMarkersOfType(DocumentMarker::MarkerType type) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 154 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 155 | if (!possiblyHasMarkers(type)) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 156 | return; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 157 | ASSERT(!m_markers.isEmpty()); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 158 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 159 | bool updatedLayout = false; |
| 160 | for (auto& nodeMarkers : m_markers) { |
| 161 | for (auto& marker : *nodeMarkers.value) { |
| 162 | if (marker.type() != type || marker.isValid()) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 163 | continue; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 164 | if (!updatedLayout) { |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 165 | updateMainFrameLayoutIfNeeded(m_document); |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 166 | updatedLayout = true; |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 167 | } |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 168 | updateRenderedRectsForMarker(marker, *nodeMarkers.key); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 173 | Vector<FloatRect> DocumentMarkerController::renderedRectsForMarkers(DocumentMarker::MarkerType type) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 174 | { |
| 175 | Vector<FloatRect> result; |
| 176 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 177 | if (!possiblyHasMarkers(type)) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 178 | return result; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 179 | ASSERT(!m_markers.isEmpty()); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 180 | |
jiewen_tan@apple.com | d975567 | 2017-10-06 20:10:25 +0000 | [diff] [blame] | 181 | RefPtr<Frame> frame = m_document.frame(); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 182 | if (!frame) |
| 183 | return result; |
| 184 | FrameView* frameView = frame->view(); |
| 185 | if (!frameView) |
| 186 | return result; |
| 187 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 188 | updateRectsForInvalidatedMarkersOfType(type); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 189 | |
| 190 | bool isSubframe = !frame->isMainFrame(); |
| 191 | IntRect subframeClipRect; |
| 192 | if (isSubframe) |
| 193 | subframeClipRect = frameView->windowToContents(frameView->windowClipRect()); |
| 194 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 195 | for (auto& nodeMarkers : m_markers) { |
| 196 | auto renderer = nodeMarkers.key->renderer(); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 197 | FloatRect overflowClipRect; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 198 | if (renderer) |
clord@igalia.com | e75ff1e | 2021-05-20 18:08:36 +0000 | [diff] [blame] | 199 | overflowClipRect = renderer->absoluteClippedOverflowRectForRepaint(); |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 200 | for (auto& marker : *nodeMarkers.value) { |
| 201 | if (marker.type() != type) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 202 | continue; |
| 203 | |
| 204 | auto renderedRects = marker.unclippedAbsoluteRects(); |
| 205 | |
| 206 | // Clip document markers by their overflow clip. |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 207 | if (renderer) { |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 208 | for (auto& rect : renderedRects) |
| 209 | rect.intersect(overflowClipRect); |
| 210 | } |
| 211 | |
| 212 | // Clip subframe document markers by their frame. |
| 213 | if (isSubframe) { |
| 214 | for (auto& rect : renderedRects) |
| 215 | rect.intersect(subframeClipRect); |
| 216 | } |
| 217 | |
| 218 | for (const auto& rect : renderedRects) { |
| 219 | if (!rect.isEmpty()) |
| 220 | result.append(rect); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return result; |
| 226 | } |
| 227 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 228 | static bool shouldInsertAsSeparateMarker(const DocumentMarker& marker) |
wenson_hsieh@apple.com | 3acff53 | 2017-05-04 22:28:22 +0000 | [diff] [blame] | 229 | { |
timothy_horton@apple.com | acbaa98 | 2019-03-20 07:32:40 +0000 | [diff] [blame] | 230 | #if ENABLE(PLATFORM_DRIVEN_TEXT_CHECKING) |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 231 | if (marker.type() == DocumentMarker::PlatformTextChecking) |
timothy_horton@apple.com | acbaa98 | 2019-03-20 07:32:40 +0000 | [diff] [blame] | 232 | return true; |
| 233 | #endif |
| 234 | |
ap@apple.com | 1e847592 | 2018-10-18 21:38:50 +0000 | [diff] [blame] | 235 | #if PLATFORM(IOS_FAMILY) |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 236 | if (marker.type() == DocumentMarker::DictationPhraseWithAlternatives || marker.type() == DocumentMarker::DictationResult) |
wenson_hsieh@apple.com | 3acff53 | 2017-05-04 22:28:22 +0000 | [diff] [blame] | 237 | return true; |
| 238 | #endif |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 239 | |
| 240 | if (marker.type() == DocumentMarker::DraggedContent) |
commit-queue@webkit.org | 3543f36 | 2021-10-13 16:17:09 +0000 | [diff] [blame] | 241 | return is<RenderReplaced>(std::get<RefPtr<Node>>(marker.data())->renderer()); |
wenson_hsieh@apple.com | 3acff53 | 2017-05-04 22:28:22 +0000 | [diff] [blame] | 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 246 | // Markers are stored in order sorted by their start offset. |
| 247 | // Markers of the same type do not overlap each other. |
| 248 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 249 | void DocumentMarkerController::addMarker(Node& node, DocumentMarker&& newMarker) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 250 | { |
morrita@google.com | 6391bb3 | 2011-05-19 01:38:03 +0000 | [diff] [blame] | 251 | ASSERT(newMarker.endOffset() >= newMarker.startOffset()); |
| 252 | if (newMarker.endOffset() == newMarker.startOffset()) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 253 | return; |
| 254 | |
antti@apple.com | 9223bcc | 2018-08-31 17:59:01 +0000 | [diff] [blame] | 255 | m_possiblyExistingMarkerTypes.add(newMarker.type()); |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 256 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 257 | auto& list = m_markers.add(&node, nullptr).iterator->value; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 258 | |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 259 | if (!list) { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 260 | list = makeUnique<Vector<RenderedDocumentMarker>>(); |
| 261 | list->append(RenderedDocumentMarker(WTFMove(newMarker))); |
wenson_hsieh@apple.com | 3acff53 | 2017-05-04 22:28:22 +0000 | [diff] [blame] | 262 | } else if (shouldInsertAsSeparateMarker(newMarker)) { |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 263 | // We don't merge dictation markers. |
| 264 | size_t i; |
| 265 | size_t numberOfMarkers = list->size(); |
| 266 | for (i = 0; i < numberOfMarkers; ++i) { |
| 267 | DocumentMarker marker = list->at(i); |
| 268 | if (marker.startOffset() > newMarker.startOffset()) |
| 269 | break; |
| 270 | } |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 271 | list->insert(i, RenderedDocumentMarker(WTFMove(newMarker))); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 272 | } else { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 273 | RenderedDocumentMarker toInsert(WTFMove(newMarker)); |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 274 | size_t numMarkers = list->size(); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 275 | size_t i; |
| 276 | // Iterate over all markers whose start offset is less than or equal to the new marker's. |
| 277 | // If one of them is of the same type as the new marker and touches it or intersects with it |
| 278 | // (there is at most one), remove it and adjust the new marker's start offset to encompass it. |
| 279 | for (i = 0; i < numMarkers; ++i) { |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 280 | DocumentMarker marker = list->at(i); |
morrita@google.com | 6391bb3 | 2011-05-19 01:38:03 +0000 | [diff] [blame] | 281 | if (marker.startOffset() > toInsert.startOffset()) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 282 | break; |
morrita@google.com | 6391bb3 | 2011-05-19 01:38:03 +0000 | [diff] [blame] | 283 | if (marker.type() == toInsert.type() && marker.endOffset() >= toInsert.startOffset()) { |
| 284 | toInsert.setStartOffset(marker.startOffset()); |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 285 | list->remove(i); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 286 | numMarkers--; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | size_t j = i; |
| 291 | // Iterate over all markers whose end offset is less than or equal to the new marker's, |
| 292 | // removing markers of the same type as the new marker which touch it or intersect with it, |
| 293 | // adjusting the new marker's end offset to cover them if necessary. |
| 294 | while (j < numMarkers) { |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 295 | DocumentMarker marker = list->at(j); |
morrita@google.com | 6391bb3 | 2011-05-19 01:38:03 +0000 | [diff] [blame] | 296 | if (marker.startOffset() > toInsert.endOffset()) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 297 | break; |
morrita@google.com | 6391bb3 | 2011-05-19 01:38:03 +0000 | [diff] [blame] | 298 | if (marker.type() == toInsert.type()) { |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 299 | list->remove(j); |
morrita@google.com | 6391bb3 | 2011-05-19 01:38:03 +0000 | [diff] [blame] | 300 | if (toInsert.endOffset() <= marker.endOffset()) { |
| 301 | toInsert.setEndOffset(marker.endOffset()); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | numMarkers--; |
| 305 | } else |
| 306 | j++; |
| 307 | } |
| 308 | // At this point i points to the node before which we want to insert. |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 309 | list->insert(i, RenderedDocumentMarker(toInsert)); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 310 | } |
| 311 | |
darin@apple.com | 9ee6097 | 2019-01-21 19:01:19 +0000 | [diff] [blame] | 312 | if (node.renderer()) |
| 313 | node.renderer()->repaint(); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 314 | |
darin@apple.com | 9ee6097 | 2019-01-21 19:01:19 +0000 | [diff] [blame] | 315 | invalidateRectsForMarkersInNode(node); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 316 | } |
| 317 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 318 | // Copies markers from source to destination, applying the specified shift delta to the copies. The shift is |
| 319 | // useful if, e.g., the caller has created the destination from a non-prefix substring of the source. |
| 320 | void DocumentMarkerController::copyMarkers(Node& source, OffsetRange range, Node& destination) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 321 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 322 | if (range.start >= range.end) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 323 | return; |
| 324 | |
dbates@webkit.org | bad49a4 | 2017-07-18 02:43:07 +0000 | [diff] [blame] | 325 | if (!possiblyHasMarkers(DocumentMarker::allMarkers())) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 326 | return; |
| 327 | ASSERT(!m_markers.isEmpty()); |
| 328 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 329 | auto list = m_markers.get(&source); |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 330 | if (!list) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 331 | return; |
| 332 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 333 | bool needRepaint = false; |
hs85.jeong@samsung.com | 2210229 | 2015-10-30 17:04:07 +0000 | [diff] [blame] | 334 | for (auto& marker : *list) { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 335 | // Stop if we are now past the specified range. |
| 336 | if (marker.startOffset() >= range.end) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 337 | break; |
| 338 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 339 | // Skip marker that is before the specified range. |
| 340 | if (marker.endOffset() < range.start) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 341 | continue; |
| 342 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 343 | // Pin the marker to the specified range and apply the shift delta. |
| 344 | auto copiedMarker = marker; |
| 345 | if (copiedMarker.startOffset() < range.start) |
| 346 | copiedMarker.setStartOffset(range.start); |
| 347 | if (copiedMarker.endOffset() >= range.end) |
| 348 | copiedMarker.setEndOffset(range.end); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 349 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 350 | addMarker(destination, WTFMove(copiedMarker)); |
| 351 | needRepaint = true; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 352 | } |
| 353 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 354 | if (needRepaint) { |
| 355 | if (auto renderer = destination.renderer()) |
| 356 | renderer->repaint(); |
| 357 | } |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 358 | } |
| 359 | |
wenson_hsieh@apple.com | 65fd03b | 2022-03-12 00:56:15 +0000 | [diff] [blame] | 360 | void DocumentMarkerController::removeMarkers(Node& node, OffsetRange range, OptionSet<DocumentMarker::MarkerType> types, const Function<FilterMarkerResult(const DocumentMarker&)>& filter, RemovePartiallyOverlappingMarker overlapRule) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 361 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 362 | if (range.start >= range.end) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 363 | return; |
| 364 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 365 | if (!possiblyHasMarkers(types)) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 366 | return; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 367 | ASSERT(!m_markers.isEmpty()); |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 368 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 369 | auto list = m_markers.get(&node); |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 370 | if (!list) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 371 | return; |
| 372 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 373 | bool needRepaint = false; |
| 374 | for (size_t i = 0; i < list->size(); ) { |
| 375 | auto& marker = list->at(i); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 376 | |
| 377 | // markers are returned in order, so stop if we are now past the specified range |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 378 | if (marker.startOffset() >= range.end) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | // skip marker that is wrong type or before target |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 382 | if (marker.endOffset() <= range.start || !types.contains(marker.type())) { |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 383 | i++; |
| 384 | continue; |
| 385 | } |
| 386 | |
wenson_hsieh@apple.com | 65fd03b | 2022-03-12 00:56:15 +0000 | [diff] [blame] | 387 | if (filter && filter(marker) == FilterMarkerResult::Keep) { |
timothy_horton@apple.com | acbaa98 | 2019-03-20 07:32:40 +0000 | [diff] [blame] | 388 | i++; |
| 389 | continue; |
| 390 | } |
| 391 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 392 | // At this point we know that marker and target intersect in some way. |
| 393 | needRepaint = true; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 394 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 395 | DocumentMarker copiedMarker = marker; |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 396 | list->remove(i); |
darin@apple.com | fed758c | 2020-07-26 19:12:30 +0000 | [diff] [blame] | 397 | if (overlapRule == RemovePartiallyOverlappingMarker::Yes) |
mitz@apple.com | d157c06 | 2011-02-04 01:40:37 +0000 | [diff] [blame] | 398 | continue; |
| 399 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 400 | // Add either of the resulting slices that remain after removing target. |
| 401 | if (range.start > copiedMarker.startOffset()) { |
| 402 | auto newLeft = copiedMarker; |
| 403 | newLeft.setEndOffset(range.start); |
| 404 | list->insert(i, RenderedDocumentMarker(WTFMove(newLeft))); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 405 | i++; |
| 406 | } |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 407 | if (copiedMarker.endOffset() > range.end) { |
| 408 | copiedMarker.setStartOffset(range.end); |
| 409 | list->insert(i, RenderedDocumentMarker(WTFMove(copiedMarker))); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 410 | i++; |
| 411 | } |
| 412 | } |
| 413 | |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 414 | if (list->isEmpty()) { |
darin@apple.com | 9ee6097 | 2019-01-21 19:01:19 +0000 | [diff] [blame] | 415 | m_markers.remove(&node); |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 416 | if (m_markers.isEmpty()) |
dbates@webkit.org | bad49a4 | 2017-07-18 02:43:07 +0000 | [diff] [blame] | 417 | m_possiblyExistingMarkerTypes = { }; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 418 | } |
| 419 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 420 | if (needRepaint) { |
| 421 | if (auto renderer = node.renderer()) |
| 422 | renderer->repaint(); |
| 423 | } |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 424 | } |
| 425 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 426 | DocumentMarker* DocumentMarkerController::markerContainingPoint(const LayoutPoint& point, DocumentMarker::MarkerType type) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 427 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 428 | if (!possiblyHasMarkers(type)) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 429 | return nullptr; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 430 | ASSERT(!m_markers.isEmpty()); |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 431 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 432 | updateRectsForInvalidatedMarkersOfType(type); |
| 433 | for (auto& nodeMarkers : m_markers.values()) { |
| 434 | for (auto& marker : *nodeMarkers) { |
| 435 | if (marker.type() == type && marker.contains(point)) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 436 | return ▮ |
| 437 | } |
| 438 | } |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 439 | return nullptr; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 440 | } |
| 441 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 442 | Vector<RenderedDocumentMarker*> DocumentMarkerController::markersFor(Node& node, OptionSet<DocumentMarker::MarkerType> types) |
morrita@google.com | c07dd58 | 2011-05-27 07:56:25 +0000 | [diff] [blame] | 443 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 444 | if (!possiblyHasMarkers(types)) |
wenson_hsieh@apple.com | 3acff53 | 2017-05-04 22:28:22 +0000 | [diff] [blame] | 445 | return { }; |
| 446 | |
cdumez@apple.com | 426611c | 2014-10-20 05:17:06 +0000 | [diff] [blame] | 447 | Vector<RenderedDocumentMarker*> result; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 448 | auto list = m_markers.get(&node); |
morrita@google.com | c07dd58 | 2011-05-27 07:56:25 +0000 | [diff] [blame] | 449 | if (!list) |
| 450 | return result; |
| 451 | |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 452 | for (auto& marker : *list) { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 453 | if (types.contains(marker.type())) |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 454 | result.append(&marker); |
commit-queue@webkit.org | 35045ab | 2011-11-10 06:10:59 +0000 | [diff] [blame] | 455 | } |
morrita@google.com | c07dd58 | 2011-05-27 07:56:25 +0000 | [diff] [blame] | 456 | |
| 457 | return result; |
| 458 | } |
| 459 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 460 | void DocumentMarkerController::forEach(const SimpleRange& range, OptionSet<DocumentMarker::MarkerType> types, Function<bool(RenderedDocumentMarker&)> function) |
commit-queue@webkit.org | aad7ca2 | 2011-04-06 16:09:28 +0000 | [diff] [blame] | 461 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 462 | if (!possiblyHasMarkers(types)) |
| 463 | return; |
| 464 | ASSERT(!m_markers.isEmpty()); |
commit-queue@webkit.org | aad7ca2 | 2011-04-06 16:09:28 +0000 | [diff] [blame] | 465 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 466 | for (auto& node : intersectingNodes(range)) { |
| 467 | if (auto list = m_markers.get(&node)) { |
| 468 | auto offsetRange = characterDataOffsetRange(range, node); |
| 469 | for (auto& marker : *list) { |
| 470 | // Markers are stored in order, so stop if we are now past the specified range. |
| 471 | if (marker.startOffset() >= offsetRange.end) |
| 472 | break; |
| 473 | if (marker.endOffset() > offsetRange.start && types.contains(marker.type())) { |
| 474 | if (function(marker)) |
| 475 | return; |
| 476 | } |
| 477 | } |
commit-queue@webkit.org | aad7ca2 | 2011-04-06 16:09:28 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
commit-queue@webkit.org | aad7ca2 | 2011-04-06 16:09:28 +0000 | [diff] [blame] | 480 | } |
| 481 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 482 | Vector<RenderedDocumentMarker*> DocumentMarkerController::markersInRange(const SimpleRange& range, OptionSet<DocumentMarker::MarkerType> types) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 483 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 484 | // FIXME: Consider making forEach public and changing callers to use that function instead of this one. |
| 485 | Vector<RenderedDocumentMarker*> markers; |
| 486 | forEach(range, types, [&] (RenderedDocumentMarker& marker) { |
| 487 | markers.append(&marker); |
| 488 | return false; |
| 489 | }); |
| 490 | return markers; |
| 491 | } |
| 492 | |
| 493 | void DocumentMarkerController::removeMarkers(Node& node, OptionSet<DocumentMarker::MarkerType> types) |
| 494 | { |
| 495 | if (!possiblyHasMarkers(types)) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 496 | return; |
| 497 | ASSERT(!m_markers.isEmpty()); |
| 498 | |
darin@apple.com | 9ee6097 | 2019-01-21 19:01:19 +0000 | [diff] [blame] | 499 | auto iterator = m_markers.find(&node); |
commit-queue@webkit.org | 08abac6 | 2010-10-28 23:45:26 +0000 | [diff] [blame] | 500 | if (iterator != m_markers.end()) |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 501 | removeMarkersFromList(iterator, types); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 502 | } |
| 503 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 504 | void DocumentMarkerController::removeMarkers(OptionSet<DocumentMarker::MarkerType> types) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 505 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 506 | if (!possiblyHasMarkers(types)) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 507 | return; |
| 508 | ASSERT(!m_markers.isEmpty()); |
| 509 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 510 | for (auto& node : copyToVector(m_markers.keys())) |
| 511 | removeMarkersFromList(m_markers.find(node), types); |
| 512 | m_possiblyExistingMarkerTypes.remove(types); |
commit-queue@webkit.org | 08abac6 | 2010-10-28 23:45:26 +0000 | [diff] [blame] | 513 | } |
| 514 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 515 | void DocumentMarkerController::removeMarkersFromList(MarkerMap::iterator iterator, OptionSet<DocumentMarker::MarkerType> types) |
commit-queue@webkit.org | 08abac6 | 2010-10-28 23:45:26 +0000 | [diff] [blame] | 516 | { |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 517 | bool needsRepainting = false; |
| 518 | bool listCanBeRemoved; |
| 519 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 520 | if (types == DocumentMarker::allMarkers()) { |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 521 | needsRepainting = true; |
| 522 | listCanBeRemoved = true; |
commit-queue@webkit.org | 08abac6 | 2010-10-28 23:45:26 +0000 | [diff] [blame] | 523 | } else { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 524 | auto list = iterator->value.get(); |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 525 | |
| 526 | for (size_t i = 0; i != list->size(); ) { |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 527 | DocumentMarker marker = list->at(i); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 528 | |
| 529 | // skip nodes that are not of the specified type |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 530 | if (!types.contains(marker.type())) { |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 531 | ++i; |
| 532 | continue; |
| 533 | } |
| 534 | |
| 535 | // pitch the old marker |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 536 | list->remove(i); |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 537 | needsRepainting = true; |
commit-queue@webkit.org | 08abac6 | 2010-10-28 23:45:26 +0000 | [diff] [blame] | 538 | // i now is the index of the next marker |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 539 | } |
| 540 | |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 541 | listCanBeRemoved = list->isEmpty(); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 542 | } |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 543 | |
| 544 | if (needsRepainting) { |
darin@apple.com | 8cdf712 | 2013-09-30 02:40:50 +0000 | [diff] [blame] | 545 | if (auto renderer = iterator->key->renderer()) |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 546 | renderer->repaint(); |
| 547 | } |
| 548 | |
| 549 | if (listCanBeRemoved) { |
| 550 | m_markers.remove(iterator); |
| 551 | if (m_markers.isEmpty()) |
dbates@webkit.org | bad49a4 | 2017-07-18 02:43:07 +0000 | [diff] [blame] | 552 | m_possiblyExistingMarkerTypes = { }; |
darin@apple.com | 10abec9 | 2013-05-07 00:45:31 +0000 | [diff] [blame] | 553 | } |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 554 | } |
| 555 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 556 | void DocumentMarkerController::repaintMarkers(OptionSet<DocumentMarker::MarkerType> types) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 557 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 558 | if (!possiblyHasMarkers(types)) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 559 | return; |
| 560 | ASSERT(!m_markers.isEmpty()); |
| 561 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 562 | for (auto& nodeMarkers : m_markers) { |
| 563 | for (auto& marker : *nodeMarkers.value) { |
| 564 | if (types.contains(marker.type())) { |
| 565 | if (auto renderer = nodeMarkers.key->renderer()) |
| 566 | renderer->repaint(); |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | } |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
darin@apple.com | 9ee6097 | 2019-01-21 19:01:19 +0000 | [diff] [blame] | 573 | void DocumentMarkerController::shiftMarkers(Node& node, unsigned startOffset, int delta) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 574 | { |
dbates@webkit.org | bad49a4 | 2017-07-18 02:43:07 +0000 | [diff] [blame] | 575 | if (!possiblyHasMarkers(DocumentMarker::allMarkers())) |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 576 | return; |
| 577 | ASSERT(!m_markers.isEmpty()); |
| 578 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 579 | auto list = m_markers.get(&node); |
morrita@google.com | 6455005 | 2011-04-27 23:09:01 +0000 | [diff] [blame] | 580 | if (!list) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 581 | return; |
| 582 | |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 583 | bool didShiftMarker = false; |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 584 | for (size_t i = 0; i != list->size(); ) { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 585 | auto& marker = list->at(i); |
| 586 | |
ap@apple.com | 1e847592 | 2018-10-18 21:38:50 +0000 | [diff] [blame] | 587 | #if PLATFORM(IOS_FAMILY) |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 588 | // FIXME: No obvious reason this should be iOS-specific. Remove the #if at some point. |
darin@apple.com | b04482e | 2020-05-20 03:13:49 +0000 | [diff] [blame] | 589 | auto targetStartOffset = clampTo<unsigned>(static_cast<int>(marker.startOffset()) + delta); |
| 590 | auto targetEndOffset = clampTo<unsigned>(static_cast<int>(marker.endOffset()) + delta); |
| 591 | if (targetStartOffset >= node.length() || targetEndOffset <= 0) { |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 592 | list->remove(i); |
| 593 | continue; |
| 594 | } |
| 595 | #endif |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 596 | |
morrita@google.com | 6391bb3 | 2011-05-19 01:38:03 +0000 | [diff] [blame] | 597 | if (marker.startOffset() >= startOffset) { |
| 598 | ASSERT((int)marker.startOffset() + delta >= 0); |
| 599 | marker.shiftOffsets(delta); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 600 | didShiftMarker = true; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 601 | } |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 602 | #if PLATFORM(IOS_FAMILY) |
| 603 | // FIXME: No obvious reason this should be iOS-specific. Remove the #if at some point. |
| 604 | else if (marker.endOffset() > startOffset) { |
darin@apple.com | b04482e | 2020-05-20 03:13:49 +0000 | [diff] [blame] | 605 | if (targetEndOffset <= marker.startOffset()) { |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 606 | list->remove(i); |
| 607 | continue; |
| 608 | } |
darin@apple.com | b04482e | 2020-05-20 03:13:49 +0000 | [diff] [blame] | 609 | marker.setEndOffset(std::min(targetEndOffset, node.length())); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 610 | didShiftMarker = true; |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 611 | } |
| 612 | #endif |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 613 | |
dbates@webkit.org | 252c409 | 2013-12-17 01:32:12 +0000 | [diff] [blame] | 614 | ++i; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 615 | } |
| 616 | |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 617 | if (didShiftMarker) { |
darin@apple.com | 9ee6097 | 2019-01-21 19:01:19 +0000 | [diff] [blame] | 618 | invalidateRectsForMarkersInNode(node); |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 619 | if (auto renderer = node.renderer()) |
| 620 | renderer->repaint(); |
timothy_horton@apple.com | 7c91b65 | 2015-09-30 19:36:37 +0000 | [diff] [blame] | 621 | } |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 622 | } |
| 623 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 624 | bool DocumentMarkerController::hasMarkers(const SimpleRange& range, OptionSet<DocumentMarker::MarkerType> types) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 625 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 626 | bool foundMarker = false; |
| 627 | forEach(range, types, [&] (RenderedDocumentMarker&) { |
| 628 | foundMarker = true; |
| 629 | return true; |
| 630 | }); |
| 631 | return foundMarker; |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 632 | } |
| 633 | |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 634 | void DocumentMarkerController::clearDescriptionOnMarkersIntersectingRange(const SimpleRange& range, OptionSet<DocumentMarker::MarkerType> types) |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 635 | { |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 636 | forEach(range, types, [&] (RenderedDocumentMarker& marker) { |
| 637 | marker.clearData(); |
commit-queue@webkit.org | 08abac6 | 2010-10-28 23:45:26 +0000 | [diff] [blame] | 638 | return false; |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 639 | }); |
commit-queue@webkit.org | 5396fd9 | 2011-03-01 20:31:40 +0000 | [diff] [blame] | 640 | } |
| 641 | |
darin@apple.com | fed758c | 2020-07-26 19:12:30 +0000 | [diff] [blame] | 642 | void addMarker(const SimpleRange& range, DocumentMarker::MarkerType type, const DocumentMarker::Data& data) |
| 643 | { |
darin@apple.com | c8b8949 | 2020-08-28 21:37:08 +0000 | [diff] [blame] | 644 | range.start.document().markers().addMarker(range, type, data); |
darin@apple.com | fed758c | 2020-07-26 19:12:30 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void addMarker(Text& node, unsigned startOffset, unsigned length, DocumentMarker::MarkerType type, DocumentMarker::Data&& data) |
| 648 | { |
| 649 | node.document().markers().addMarker(node, startOffset, length, type, WTFMove(data)); |
| 650 | } |
| 651 | |
| 652 | void removeMarkers(const SimpleRange& range, OptionSet<DocumentMarker::MarkerType> types, RemovePartiallyOverlappingMarker policy) |
| 653 | { |
darin@apple.com | c8b8949 | 2020-08-28 21:37:08 +0000 | [diff] [blame] | 654 | range.start.document().markers().removeMarkers(range, types, policy); |
darin@apple.com | fed758c | 2020-07-26 19:12:30 +0000 | [diff] [blame] | 655 | } |
| 656 | |
darin@apple.com | f91ef17 | 2020-08-03 00:47:52 +0000 | [diff] [blame] | 657 | SimpleRange makeSimpleRange(Node& node, const DocumentMarker& marker) |
darin@apple.com | 5d3decc | 2020-08-01 15:50:36 +0000 | [diff] [blame] | 658 | { |
| 659 | unsigned startOffset = marker.startOffset(); |
| 660 | unsigned endOffset = marker.endOffset(); |
| 661 | return { { node, startOffset }, { node, endOffset } }; |
| 662 | } |
| 663 | |
simon.fraser@apple.com | c9f9613 | 2015-03-06 18:20:40 +0000 | [diff] [blame] | 664 | #if ENABLE(TREE_DEBUGGING) |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 665 | |
morrita@google.com | 7a18fee | 2010-12-13 06:06:19 +0000 | [diff] [blame] | 666 | void DocumentMarkerController::showMarkers() const |
| 667 | { |
zalan@apple.com | 29eeb9d | 2017-06-28 16:45:14 +0000 | [diff] [blame] | 668 | fprintf(stderr, "%d nodes have markers:\n", m_markers.size()); |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 669 | for (auto& nodeMarkers : m_markers) { |
| 670 | fprintf(stderr, "%p", nodeMarkers.key.get()); |
| 671 | for (auto& marker : *nodeMarkers.value) |
| 672 | fprintf(stderr, " %d:[%d:%d]", marker.type(), marker.startOffset(), marker.endOffset()); |
| 673 | fputc('\n', stderr); |
morrita@google.com | 7a18fee | 2010-12-13 06:06:19 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 676 | |
morrita@google.com | 7a18fee | 2010-12-13 06:06:19 +0000 | [diff] [blame] | 677 | #endif |
| 678 | |
dbates@webkit.org | 858331b | 2010-08-22 21:41:37 +0000 | [diff] [blame] | 679 | } // namespace WebCore |
morrita@google.com | 7a18fee | 2010-12-13 06:06:19 +0000 | [diff] [blame] | 680 | |
simon.fraser@apple.com | c9f9613 | 2015-03-06 18:20:40 +0000 | [diff] [blame] | 681 | #if ENABLE(TREE_DEBUGGING) |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 682 | |
morrita@google.com | 7a18fee | 2010-12-13 06:06:19 +0000 | [diff] [blame] | 683 | void showDocumentMarkers(const WebCore::DocumentMarkerController* controller) |
| 684 | { |
| 685 | if (controller) |
| 686 | controller->showMarkers(); |
| 687 | } |
darin@apple.com | b13b7de | 2020-04-06 17:21:02 +0000 | [diff] [blame] | 688 | |
morrita@google.com | 7a18fee | 2010-12-13 06:06:19 +0000 | [diff] [blame] | 689 | #endif |