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
|
/*
* Copyright (C) 2020 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 "AppHighlightStorage.h"
#include "AppHighlight.h"
#include "AppHighlightRangeData.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "Document.h"
#include "DocumentMarkerController.h"
#include "Editor.h"
#include "ElementInlines.h"
#include "HTMLBodyElement.h"
#include "HighlightRegister.h"
#include "Node.h"
#include "Position.h"
#include "RenderedDocumentMarker.h"
#include "SimpleRange.h"
#include "StaticRange.h"
#include "TextIndicator.h"
#include "TextIterator.h"
#include <wtf/UUID.h>
namespace WebCore {
#if ENABLE(APP_HIGHLIGHTS)
static constexpr unsigned textPreviewLength = 500;
static RefPtr<Node> findNodeByPathIndex(const Node& parent, unsigned pathIndex, const String& nodeName)
{
if (!is<ContainerNode>(parent))
return nullptr;
for (auto* child = downcast<ContainerNode>(parent).firstChild(); child; child = child->nextSibling()) {
if (nodeName != child->nodeName())
continue;
if (!pathIndex--)
return child;
}
return nullptr;
}
static std::pair<RefPtr<Node>, size_t> findNodeStartingAtPathComponentIndex(const AppHighlightRangeData::NodePath& path, Node& initialNode, size_t initialIndexToFollow)
{
if (initialIndexToFollow >= path.size())
return { nullptr, initialIndexToFollow };
RefPtr currentNode = &initialNode;
size_t currentPathIndex = initialIndexToFollow;
for (; currentPathIndex < path.size(); ++currentPathIndex) {
auto& component = path[currentPathIndex];
auto nextNode = findNodeByPathIndex(*currentNode, component.pathIndex, component.nodeName);
if (!nextNode)
return { nullptr, currentPathIndex };
if (is<CharacterData>(*nextNode) && downcast<CharacterData>(*nextNode).data() != component.textData)
return { nullptr, currentPathIndex };
currentNode = WTFMove(nextNode);
}
return { currentNode, currentPathIndex };
}
static RefPtr<Node> findNode(const AppHighlightRangeData::NodePath& path, Document& document)
{
if (path.isEmpty() || !document.body())
return nullptr;
auto [foundNode, nextIndex] = findNodeStartingAtPathComponentIndex(path, *document.body(), 0);
if (foundNode)
return foundNode;
while (nextIndex < path.size()) {
auto& component = path[nextIndex++];
if (component.identifier.isEmpty())
continue;
RefPtr elementWithIdentifier = document.getElementById(component.identifier);
if (!elementWithIdentifier || elementWithIdentifier->nodeName() != component.nodeName)
continue;
std::tie(foundNode, nextIndex) = findNodeStartingAtPathComponentIndex(path, *elementWithIdentifier, nextIndex);
if (foundNode)
return foundNode;
}
return nullptr;
}
static std::optional<SimpleRange> findRangeByIdentifyingStartAndEndPositions(const AppHighlightRangeData& range, Document& document)
{
auto startContainer = findNode(range.startContainer(), document);
if (!startContainer)
return std::nullopt;
auto endContainer = findNode(range.endContainer(), document);
if (!endContainer)
return std::nullopt;
auto start = makeContainerOffsetPosition(startContainer.get(), range.startOffset());
auto end = makeContainerOffsetPosition(endContainer.get(), range.endOffset());
if (start.isOrphan() || end.isOrphan())
return std::nullopt;
return makeSimpleRange(start, end);
}
static std::optional<SimpleRange> findRangeBySearchingText(const AppHighlightRangeData& range, Document& document)
{
HashSet<String> identifiersInStartPath;
for (auto& component : range.startContainer()) {
if (!component.identifier.isEmpty())
identifiersInStartPath.add(component.identifier);
}
RefPtr<Element> foundElement = document.body();
for (auto iterator = range.endContainer().rbegin(), end = range.endContainer().rend(); iterator != end; ++iterator) {
auto elementIdentifier = iterator->identifier;
if (elementIdentifier.isEmpty() || !identifiersInStartPath.contains(elementIdentifier))
continue;
foundElement = document.getElementById(elementIdentifier);
if (foundElement)
break;
}
if (!foundElement)
return std::nullopt;
auto foundElementRange = makeRangeSelectingNodeContents(*foundElement);
auto foundText = plainText(foundElementRange);
if (auto index = foundText.find(range.text()); index != notFound && index == foundText.reverseFind(range.text()))
return resolveCharacterRange(foundElementRange, { index, range.text().length() });
return std::nullopt;
}
static std::optional<SimpleRange> findRange(const AppHighlightRangeData& range, Document& document)
{
if (auto foundRange = findRangeByIdentifyingStartAndEndPositions(range, document))
return foundRange;
return findRangeBySearchingText(range, document);
}
static unsigned computePathIndex(const Node& node)
{
String nodeName = node.nodeName();
unsigned index = 0;
for (auto* previousSibling = node.previousSibling(); previousSibling; previousSibling = previousSibling->previousSibling()) {
if (previousSibling->nodeName() == nodeName)
index++;
}
return index;
}
static AppHighlightRangeData::NodePathComponent createNodePathComponent(const Node& node)
{
return {
is<Element>(node) ? downcast<Element>(node).getIdAttribute().string() : nullString(),
node.nodeName(),
is<CharacterData>(node) ? downcast<CharacterData>(node).data() : nullString(),
computePathIndex(node)
};
}
static AppHighlightRangeData::NodePath makeNodePath(RefPtr<Node>&& node)
{
AppHighlightRangeData::NodePath components;
auto body = node->document().body();
for (auto ancestor = node; ancestor && ancestor != body; ancestor = ancestor->parentNode())
components.append(createNodePathComponent(*ancestor));
components.reverse();
return { components };
}
static AppHighlightRangeData createAppHighlightRangeData(const StaticRange& range)
{
auto text = plainText(range).left(textPreviewLength);
auto identifier = createVersion4UUIDString();
return {
identifier,
text,
makeNodePath(&range.startContainer()),
range.startOffset(),
makeNodePath(&range.endContainer()),
range.endOffset()
};
}
AppHighlightStorage::AppHighlightStorage(Document& document)
: m_document(document)
{
}
AppHighlightStorage::~AppHighlightStorage() = default;
void AppHighlightStorage::storeAppHighlight(Ref<StaticRange>&& range)
{
auto data = createAppHighlightRangeData(range);
std::optional<String> text;
if (!data.text().isEmpty())
text = data.text();
AppHighlight highlight = {data.toSharedBuffer(), text, CreateNewGroupForHighlight::No, HighlightRequestOriginatedInApp::No};
m_document->page()->chrome().storeAppHighlight(WTFMove(highlight));
}
void AppHighlightStorage::restoreAndScrollToAppHighlight(Ref<FragmentedSharedBuffer>&& buffer, ScrollToHighlight scroll)
{
auto appHighlightRangeData = AppHighlightRangeData::create(buffer);
if (!appHighlightRangeData)
return;
if (!attemptToRestoreHighlightAndScroll(appHighlightRangeData.value(), scroll)) {
if (scroll == ScrollToHighlight::Yes)
m_unrestoredScrollHighlight = appHighlightRangeData;
else
m_unrestoredHighlights.append(appHighlightRangeData.value());
}
m_timeAtLastRangeSearch = MonotonicTime::now();
}
bool AppHighlightStorage::attemptToRestoreHighlightAndScroll(AppHighlightRangeData& highlight, ScrollToHighlight scroll)
{
if (!m_document)
return false;
RefPtr strongDocument = m_document.get();
auto range = findRange(highlight, *strongDocument);
if (!range)
return false;
strongDocument->appHighlightRegister().addAnnotationHighlightWithRange(StaticRange::create(*range));
if (scroll == ScrollToHighlight::Yes) {
auto textIndicator = TextIndicator::createWithRange(range.value(), { TextIndicatorOption::DoNotClipToVisibleRect }, WebCore::TextIndicatorPresentationTransition::Bounce);
if (textIndicator)
m_document->page()->chrome().client().setTextIndicator(textIndicator->data());
TemporarySelectionChange selectionChange(*strongDocument, { *range }, { TemporarySelectionOption::DelegateMainFrameScroll, TemporarySelectionOption::SmoothScroll, TemporarySelectionOption::RevealSelectionBounds, TemporarySelectionOption::UserTriggered });
}
return true;
}
void AppHighlightStorage::restoreUnrestoredAppHighlights()
{
Vector<AppHighlightRangeData> remainingRanges;
for (auto& highlight : m_unrestoredHighlights) {
if (!attemptToRestoreHighlightAndScroll(highlight, ScrollToHighlight::No))
remainingRanges.append(highlight);
}
if (m_unrestoredScrollHighlight) {
if (attemptToRestoreHighlightAndScroll(m_unrestoredScrollHighlight.value(), ScrollToHighlight::Yes))
m_unrestoredScrollHighlight.reset();
}
m_timeAtLastRangeSearch = MonotonicTime::now();
m_unrestoredHighlights = WTFMove(remainingRanges);
}
#endif
} // namespace WebCore
|