1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* Copyright (C) 2017-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "MarkedText.h"
#include "Document.h"
#include "DocumentInlines.h"
#include "DocumentMarkerController.h"
#include "Editor.h"
#include "ElementRuleCollector.h"
#include "HighlightRegistry.h"
#include "RenderBoxModelObject.h"
#include "RenderHighlight.h"
#include "RenderStyleInlines.h"
#include "RenderText.h"
#include "RenderedDocumentMarker.h"
#include "TextBoxSelectableRange.h"
#include <algorithm>
#include <wtf/HashSet.h>
namespace WebCore {
Vector<MarkedText> MarkedText::subdivide(const Vector<MarkedText>& markedTexts, OverlapStrategy overlapStrategy)
{
if (markedTexts.isEmpty())
return { };
struct Offset {
enum Kind { Begin, End };
Kind kind;
unsigned value; // Copy of markedText.startOffset/endOffset to avoid the need to branch based on kind.
CheckedPtr<const MarkedText> markedText;
};
// 1. Build table of all offsets.
Vector<Offset> offsets;
ASSERT(markedTexts.size() < std::numeric_limits<unsigned>::max() / 2);
unsigned numberOfMarkedTexts = markedTexts.size();
unsigned numberOfOffsets = 2 * numberOfMarkedTexts;
offsets.reserveInitialCapacity(numberOfOffsets);
for (auto& markedText : markedTexts) {
offsets.append({ Offset::Begin, markedText.startOffset, &markedText });
offsets.append({ Offset::End, markedText.endOffset, &markedText });
}
// 2. Sort offsets such that begin offsets are in paint order and end offsets are in reverse paint order.
std::sort(offsets.begin(), offsets.end(), [] (const Offset& a, const Offset& b) {
return a.value < b.value || (a.value == b.value && a.kind == b.kind && a.kind == Offset::Begin && a.markedText->type < b.markedText->type)
|| (a.value == b.value && a.kind == b.kind && a.kind == Offset::End && a.markedText->type > b.markedText->type);
});
// 3. Compute intersection.
Vector<MarkedText> result;
result.reserveInitialCapacity(numberOfMarkedTexts);
UncheckedKeyHashSet<CheckedPtr<const MarkedText>> processedMarkedTexts;
unsigned offsetSoFar = offsets[0].value;
for (unsigned i = 1; i < numberOfOffsets; ++i) {
if (offsets[i].value > offsets[i - 1].value) {
if (overlapStrategy == OverlapStrategy::Frontmost) {
std::optional<unsigned> frontmost;
for (unsigned j = 0; j < i; ++j) {
if (!processedMarkedTexts.contains(offsets[j].markedText) && (!frontmost || offsets[j].markedText->type > offsets[*frontmost].markedText->type))
frontmost = j;
}
if (frontmost)
result.append({ offsetSoFar, offsets[i].value, offsets[*frontmost].markedText->type, offsets[*frontmost].markedText->marker, offsets[*frontmost].markedText->highlightName });
} else {
// The appended marked texts may not be in paint order. We will fix this up at the end of this function.
for (unsigned j = 0; j < i; ++j) {
if (!processedMarkedTexts.contains(offsets[j].markedText))
result.append({ offsetSoFar, offsets[i].value, offsets[j].markedText->type, offsets[j].markedText->marker, offsets[j].markedText->highlightName, offsets[j].markedText->priority });
}
}
offsetSoFar = offsets[i].value;
}
if (offsets[i].kind == Offset::End)
processedMarkedTexts.add(offsets[i].markedText);
}
// Fix up; sort the marked texts so that they are in paint order.
if (overlapStrategy == OverlapStrategy::None)
std::sort(result.begin(), result.end(), [] (const MarkedText& a, const MarkedText& b) { return a.startOffset < b.startOffset || (a.startOffset == b.startOffset && a.type < b.type); });
return result;
}
Vector<MarkedText> MarkedText::collectForHighlights(const RenderText& renderer, const TextBoxSelectableRange& selectableRange, PaintPhase phase)
{
Vector<MarkedText> markedTexts;
RenderHighlight renderHighlight;
auto& parentRenderer = *renderer.parent();
auto& parentStyle = parentRenderer.style();
if (auto highlightRegistry = renderer.document().highlightRegistryIfExists()) {
for (auto& highlightName : highlightRegistry->highlightNames()) {
auto renderStyle = parentRenderer.getUncachedPseudoStyle({ PseudoId::Highlight, highlightName }, &parentStyle);
if (!renderStyle)
continue;
if (renderStyle->textDecorationsInEffect().isEmpty() && phase == PaintPhase::Decoration)
continue;
for (auto& highlightRange : highlightRegistry->map().get(highlightName)->highlightRanges()) {
if (!renderHighlight.setRenderRange(highlightRange))
continue;
if (auto* staticRange = dynamicDowncast<StaticRange>(highlightRange->range()); staticRange
&& (!staticRange->computeValidity() || staticRange->collapsed()))
continue;
// FIXME: Potentially move this check elsewhere, to where we collect this range information.
auto hasRenderer = [&] {
IntersectingNodeRange nodes(makeSimpleRange(highlightRange->range()));
for (auto& iterator : nodes) {
if (iterator.renderer())
return true;
}
return false;
}();
if (!hasRenderer)
continue;
auto [highlightStart, highlightEnd] = renderHighlight.rangeForTextBox(renderer, selectableRange);
if (highlightStart < highlightEnd) {
int currentPriority = highlightRegistry->map().get(highlightName)->priority();
// If we can just append it to the end, do that instead.
if (markedTexts.isEmpty() || markedTexts.last().priority <= currentPriority)
markedTexts.append({ highlightStart, highlightEnd, MarkedText::Type::Highlight, nullptr, highlightName, currentPriority });
else {
// Gets the first place such that it > currentPriority.
auto it = std::upper_bound(markedTexts.begin(), markedTexts.end(), currentPriority, [](const auto targetMarkedTextPriority, const auto& markedText) {
return targetMarkedTextPriority > markedText.priority;
});
unsigned insertIndex = (it == markedTexts.end() ? 0 : std::distance(markedTexts.begin(), it) - 1);
markedTexts.insert(insertIndex, { highlightStart, highlightEnd, MarkedText::Type::Highlight, nullptr, highlightName, currentPriority });
}
}
}
}
}
if (renderer.document().settings().scrollToTextFragmentEnabled()) {
if (auto fragmentHighlightRegistry = renderer.document().fragmentHighlightRegistryIfExists()) {
for (auto& highlight : fragmentHighlightRegistry->map()) {
for (auto& highlightRange : highlight.value->highlightRanges()) {
if (!renderHighlight.setRenderRange(highlightRange))
continue;
auto [highlightStart, highlightEnd] = renderHighlight.rangeForTextBox(renderer, selectableRange);
if (highlightStart < highlightEnd)
markedTexts.append({ highlightStart, highlightEnd, MarkedText::Type::FragmentHighlight });
}
}
}
}
#if ENABLE(APP_HIGHLIGHTS)
if (auto appHighlightRegistry = renderer.document().appHighlightRegistryIfExists()) {
if (appHighlightRegistry->highlightsVisibility() == HighlightVisibility::Visible) {
for (auto& highlight : appHighlightRegistry->map()) {
for (auto& highlightRange : highlight.value->highlightRanges()) {
if (!renderHighlight.setRenderRange(highlightRange))
continue;
auto [highlightStart, highlightEnd] = renderHighlight.rangeForTextBox(renderer, selectableRange);
if (highlightStart < highlightEnd)
markedTexts.append({ highlightStart, highlightEnd, MarkedText::Type::AppHighlight });
}
}
}
}
#endif
return markedTexts;
}
Vector<MarkedText> MarkedText::collectForDocumentMarkers(const RenderText& renderer, const TextBoxSelectableRange& selectableRange, PaintPhase phase)
{
if (!renderer.textNode())
return { };
CheckedPtr markerController = renderer.document().markersIfExists();
if (!markerController)
return { };
auto markers = markerController->markersFor(*renderer.textNode());
auto markedTextTypeForMarkerType = [] (DocumentMarkerType type) {
switch (type) {
case DocumentMarkerType::Spelling:
return MarkedText::Type::SpellingError;
case DocumentMarkerType::Grammar:
return MarkedText::Type::GrammarError;
case DocumentMarkerType::CorrectionIndicator:
return MarkedText::Type::Correction;
#if ENABLE(WRITING_TOOLS)
case DocumentMarkerType::WritingToolsTextSuggestion:
return MarkedText::Type::WritingToolsTextSuggestion;
#endif
case DocumentMarkerType::TextMatch:
return MarkedText::Type::TextMatch;
case DocumentMarkerType::DictationAlternatives:
return MarkedText::Type::DictationAlternatives;
#if PLATFORM(IOS_FAMILY)
case DocumentMarkerType::DictationPhraseWithAlternatives:
return MarkedText::Type::DictationPhraseWithAlternatives;
#endif
default:
return MarkedText::Type::Unmarked;
}
};
Vector<MarkedText> markedTexts;
markedTexts.reserveInitialCapacity(markers.size());
// Give any document markers that touch this run a chance to draw before the text has been drawn.
// Note end() points at the last char, not one past it like endOffset and ranges do.
for (auto& marker : markers) {
// Collect either the background markers or the foreground markers, but not both
switch (marker->type()) {
case DocumentMarkerType::Grammar:
case DocumentMarkerType::Spelling:
break;
case DocumentMarkerType::CorrectionIndicator:
#if ENABLE(WRITING_TOOLS)
case DocumentMarkerType::WritingToolsTextSuggestion:
#endif
case DocumentMarkerType::Replacement:
case DocumentMarkerType::DictationAlternatives:
#if PLATFORM(IOS_FAMILY)
// FIXME: Remove the PLATFORM(IOS_FAMILY)-guard.
case DocumentMarkerType::DictationPhraseWithAlternatives:
#endif
if (phase != MarkedText::PaintPhase::Decoration)
continue;
break;
case DocumentMarkerType::TextMatch:
if (!renderer.frame().editor().markedTextMatchesAreHighlighted())
continue;
if (phase == MarkedText::PaintPhase::Decoration)
continue;
break;
#if ENABLE(TELEPHONE_NUMBER_DETECTION)
case DocumentMarkerType::TelephoneNumber:
if (!renderer.frame().editor().markedTextMatchesAreHighlighted())
continue;
if (phase != MarkedText::PaintPhase::Background)
continue;
break;
#endif
default:
continue;
}
if (marker->endOffset() <= selectableRange.start) {
// Marker is completely before this run. This might be a marker that sits before the
// first run we draw, or markers that were within runs we skipped due to truncation.
continue;
}
if (marker->startOffset() >= selectableRange.start + selectableRange.length) {
// Marker is completely after this run, bail. A later run will paint it.
break;
}
// Marker intersects this run. Collect it.
switch (marker->type()) {
case DocumentMarkerType::Spelling:
case DocumentMarkerType::CorrectionIndicator:
#if ENABLE(WRITING_TOOLS)
case DocumentMarkerType::WritingToolsTextSuggestion: {
auto shouldPaintMarker = [&] {
if (marker->type() != DocumentMarkerType::WritingToolsTextSuggestion)
return true;
auto data = std::get<DocumentMarker::WritingToolsTextSuggestionData>(marker->data());
if (data.state != DocumentMarker::WritingToolsTextSuggestionData::State::Accepted)
return false;
if (data.decoration == DocumentMarker::WritingToolsTextSuggestionData::Decoration::None)
return false;
return true;
}();
if (!shouldPaintMarker)
break;
BFALLTHROUGH;
}
#endif
case DocumentMarkerType::DictationAlternatives:
case DocumentMarkerType::Grammar:
#if PLATFORM(IOS_FAMILY)
// FIXME: See <rdar://problem/8933352>. Also, remove the PLATFORM(IOS_FAMILY)-guard.
case DocumentMarkerType::DictationPhraseWithAlternatives:
#endif
case DocumentMarkerType::TextMatch: {
auto [clampedStart, clampedEnd] = selectableRange.clamp(marker->startOffset(), marker->endOffset());
auto markedTextType = markedTextTypeForMarkerType(marker->type());
markedTexts.append({ clampedStart, clampedEnd, markedTextType, marker.get() });
break;
}
case DocumentMarkerType::Replacement:
break;
#if ENABLE(TELEPHONE_NUMBER_DETECTION)
case DocumentMarkerType::TelephoneNumber:
break;
#endif
default:
ASSERT_NOT_REACHED();
}
}
return markedTexts;
}
Vector<MarkedText> MarkedText::collectForDraggedAndTransparentContent(const DocumentMarkerType type, const RenderText& renderer, const TextBoxSelectableRange& selectableRange)
{
auto markerTypeForDocumentMarker = [] (DocumentMarkerType type) {
switch (type) {
case DocumentMarkerType::DraggedContent:
return MarkedText::Type::DraggedContent;
case DocumentMarkerType::TransparentContent:
return MarkedText::Type::TransparentContent;
default:
return MarkedText::Type::Unmarked;
}
};
Type markerType = markerTypeForDocumentMarker(type);
if (markerType == MarkedText::Type::Unmarked) {
ASSERT_NOT_REACHED();
return { };
}
auto contentRanges = renderer.contentRangesBetweenOffsetsForType(type, selectableRange.start, selectableRange.start + selectableRange.length);
return contentRanges.map([&](const auto& range) -> MarkedText {
return { selectableRange.clamp(range.first), selectableRange.clamp(range.second), markerType };
});
}
}
|