Bug 1924519, part 2 - Text Fragments: Refactor algorithm to find a text directive in a document into its own file. r=dom-core,farre

Similarly to the last part, this commit attempts to clean up the FragmentDirective class and have it serve more as an Interface than the implementation.

The new class TextDirectiveFinder encapsules the find code. It is instantiated lazily using unique pointers and removed when all text directives are invoked.

Differential Revision: https://phabricator.services.mozilla.com/D225535
This commit is contained in:
Jan-Niklas Jaeschke 2024-11-04 08:59:30 +00:00
parent c99222f89f
commit e4563a1d30
5 changed files with 445 additions and 344 deletions

View File

@ -10,13 +10,13 @@
#include "mozilla/Assertions.h"
#include "BasePrincipal.h"
#include "Document.h"
#include "TextDirectiveFinder.h"
#include "TextDirectiveUtil.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/BrowsingContextGroup.h"
#include "mozilla/dom/FragmentDirectiveBinding.h"
#include "mozilla/dom/FragmentOrElement.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/dom/Text.h"
#include "mozilla/PresShell.h"
#include "nsContentUtils.h"
#include "nsDocShell.h"
@ -40,11 +40,27 @@ NS_INTERFACE_MAP_END
FragmentDirective::FragmentDirective(Document* aDocument)
: mDocument(aDocument) {}
FragmentDirective::~FragmentDirective() = default;
JSObject* FragmentDirective::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return FragmentDirective_Binding::Wrap(aCx, this, aGivenProto);
}
void FragmentDirective::SetTextDirectives(
nsTArray<TextDirective>&& aTextDirectives) {
MOZ_ASSERT(mDocument);
if (!aTextDirectives.IsEmpty()) {
mFinder =
MakeUnique<TextDirectiveFinder>(*mDocument, std::move(aTextDirectives));
} else {
mFinder = nullptr;
}
}
void FragmentDirective::ClearUninvokedDirectives() { mFinder = nullptr; }
bool FragmentDirective::HasUninvokedDirectives() const { return !!mFinder; };
bool FragmentDirective::ParseAndRemoveFragmentDirectiveFromFragmentString(
nsCString& aFragment, nsTArray<TextDirective>* aTextDirectives,
nsIURI* aURI) {
@ -125,70 +141,19 @@ void FragmentDirective::ParseAndRemoveFragmentDirectiveFromFragment(
nsTArray<RefPtr<nsRange>> FragmentDirective::FindTextFragmentsInDocument() {
MOZ_ASSERT(mDocument);
auto uri = TextDirectiveUtil::ShouldLog() && mDocument->GetDocumentURI()
? mDocument->GetDocumentURI()->GetSpecOrDefault()
: nsCString();
if (mUninvokedTextDirectives.IsEmpty()) {
if (!mFinder) {
auto uri = TextDirectiveUtil::ShouldLog() && mDocument->GetDocumentURI()
? mDocument->GetDocumentURI()->GetSpecOrDefault()
: nsCString();
TEXT_FRAGMENT_LOG("No uninvoked text directives in document '%s'. Exiting.",
uri.Data());
return {};
}
TEXT_FRAGMENT_LOG("Trying to find text directives in document '%s'.",
uri.Data());
mDocument->FlushPendingNotifications(FlushType::Frames);
// https://wicg.github.io/scroll-to-text-fragment/#invoke-text-directives
// To invoke text directives, given as input a list of text directives text
// directives and a Document document, run these steps:
// 1. Let ranges be a list of ranges, initially empty.
nsTArray<RefPtr<nsRange>> textDirectiveRanges(
mUninvokedTextDirectives.Length());
// Additionally (not mentioned in the spec), remove all text directives from
// the input list to keep only the ones that are not found.
// This code runs repeatedly during a page load, so it is possible that the
// match for a text directive has not been parsed yet.
nsTArray<TextDirective> uninvokedTextDirectives(
mUninvokedTextDirectives.Length());
// 2. For each text directive directive of text directives:
for (TextDirective& textDirective : mUninvokedTextDirectives) {
// 2.1 If the result of running find a range from a text directive given
// directive and document is non-null, then append it to ranges.
if (RefPtr<nsRange> range = FindRangeForTextDirective(textDirective)) {
textDirectiveRanges.AppendElement(range);
TEXT_FRAGMENT_LOG("Found text directive '%s'",
ToString(textDirective).c_str());
} else {
uninvokedTextDirectives.AppendElement(std::move(textDirective));
}
auto textDirectives = mFinder->FindTextDirectivesInDocument();
if (!mFinder->HasUninvokedDirectives()) {
mFinder = nullptr;
}
if (TextDirectiveUtil::ShouldLog()) {
if (uninvokedTextDirectives.Length() == mUninvokedTextDirectives.Length()) {
TEXT_FRAGMENT_LOG(
"Did not find any of the %zu uninvoked text directives.",
mUninvokedTextDirectives.Length());
} else {
TEXT_FRAGMENT_LOG(
"Found %zu of %zu text directives in the document.",
mUninvokedTextDirectives.Length() - uninvokedTextDirectives.Length(),
mUninvokedTextDirectives.Length());
}
if (uninvokedTextDirectives.IsEmpty()) {
TEXT_FRAGMENT_LOG("No uninvoked text directives left.");
} else {
TEXT_FRAGMENT_LOG("There are %zu uninvoked text directives left:",
uninvokedTextDirectives.Length());
for (size_t index = 0; index < uninvokedTextDirectives.Length();
++index) {
TEXT_FRAGMENT_LOG(" [%zu]: %s", index,
ToString(uninvokedTextDirectives[index]).c_str());
}
}
}
mUninvokedTextDirectives = std::move(uninvokedTextDirectives);
// 3. Return ranges.
return textDirectiveRanges;
return textDirectives;
}
/* static */ nsresult FragmentDirective::GetSpecIgnoringFragmentDirective(
@ -457,267 +422,4 @@ void FragmentDirective::RemoveAllTextDirectives(ErrorResult& aRv) {
targetTextSelection->RemoveAllRanges(aRv);
}
RefPtr<nsRange> FragmentDirective::FindRangeForTextDirective(
const TextDirective& aTextDirective) {
TEXT_FRAGMENT_LOG("Find range for text directive '%s'.",
ToString(aTextDirective).c_str());
// 1. Let searchRange be a range with start (document, 0) and end (document,
// documents length)
ErrorResult rv;
RefPtr<nsRange> searchRange =
nsRange::Create(mDocument, 0, mDocument, mDocument->Length(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2. While searchRange is not collapsed:
while (!searchRange->Collapsed()) {
// 2.1. Let potentialMatch be null.
RefPtr<nsRange> potentialMatch;
// 2.2. If parsedValuess prefix is not null:
if (!aTextDirective.prefix.IsEmpty()) {
// 2.2.1. Let prefixMatch be the the result of running the find a string
// in range steps with query parsedValuess prefix, searchRange
// searchRange, wordStartBounded true and wordEndBounded false.
RefPtr<nsRange> prefixMatch = TextDirectiveUtil::FindStringInRange(
searchRange, aTextDirective.prefix, true, false);
// 2.2.2. If prefixMatch is null, return null.
if (!prefixMatch) {
TEXT_FRAGMENT_LOG(
"Did not find prefix '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.prefix).Data());
return nullptr;
}
TEXT_FRAGMENT_LOG("Did find prefix '%s'.",
NS_ConvertUTF16toUTF8(aTextDirective.prefix).Data());
// 2.2.3. Set searchRanges start to the first boundary point after
// prefixMatchs start
const RangeBoundary boundaryPoint =
TextDirectiveUtil::MoveRangeBoundaryOneWord(
{prefixMatch->GetStartContainer(), prefixMatch->StartOffset()},
TextScanDirection::Right);
if (!boundaryPoint.IsSetAndValid()) {
return nullptr;
}
searchRange->SetStart(boundaryPoint.AsRaw(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.2.4. Let matchRange be a range whose start is prefixMatchs end and
// end is searchRanges end.
RefPtr<nsRange> matchRange = nsRange::Create(
prefixMatch->GetEndContainer(), prefixMatch->EndOffset(),
searchRange->GetEndContainer(), searchRange->EndOffset(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.2.5. Advance matchRanges start to the next non-whitespace position.
TextDirectiveUtil::AdvanceStartToNextNonWhitespacePosition(*matchRange);
// 2.2.6. If matchRange is collapsed return null.
// (This can happen if prefixMatchs end or its subsequent non-whitespace
// position is at the end of the document.)
if (matchRange->Collapsed()) {
return nullptr;
}
// 2.2.7. Assert: matchRanges start node is a Text node.
// (matchRanges start now points to the next non-whitespace text data
// following a matched prefix.)
MOZ_ASSERT(matchRange->GetStartContainer()->IsText());
// 2.2.8. Let mustEndAtWordBoundary be true if parsedValuess end is
// non-null or parsedValuess suffix is null, false otherwise.
const bool mustEndAtWordBoundary =
!aTextDirective.end.IsEmpty() || aTextDirective.suffix.IsEmpty();
// 2.2.9. Set potentialMatch to the result of running the find a string in
// range steps with query parsedValuess start, searchRange matchRange,
// wordStartBounded false, and wordEndBounded mustEndAtWordBoundary.
potentialMatch = TextDirectiveUtil::FindStringInRange(
matchRange, aTextDirective.start, false, mustEndAtWordBoundary);
// 2.2.10. If potentialMatch is null, return null.
if (!potentialMatch) {
TEXT_FRAGMENT_LOG(
"Did not find start '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.start).Data());
return nullptr;
}
TEXT_FRAGMENT_LOG("Did find start '%s'.",
NS_ConvertUTF16toUTF8(aTextDirective.start).Data());
// 2.2.11. If potentialMatchs start is not matchRanges start, then
// continue.
// (In this case, we found a prefix but it was followed by something other
// than a matching text so well continue searching for the next instance
// of prefix.)
if (potentialMatch->StartRef() != matchRange->StartRef()) {
TEXT_FRAGMENT_LOG(
"The prefix is not directly followed by the start element. "
"Discarding this attempt.");
continue;
}
}
// 2.3. Otherwise:
else {
// 2.3.1. Let mustEndAtWordBoundary be true if parsedValuess end is
// non-null or parsedValuess suffix is null, false otherwise.
const bool mustEndAtWordBoundary =
!aTextDirective.end.IsEmpty() || aTextDirective.suffix.IsEmpty();
// 2.3.2. Set potentialMatch to the result of running the find a string in
// range steps with query parsedValuess start, searchRange searchRange,
// wordStartBounded true, and wordEndBounded mustEndAtWordBoundary.
potentialMatch = TextDirectiveUtil::FindStringInRange(
searchRange, aTextDirective.start, true, mustEndAtWordBoundary);
// 2.3.3. If potentialMatch is null, return null.
if (!potentialMatch) {
TEXT_FRAGMENT_LOG(
"Did not find start '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.start).Data());
return nullptr;
}
// 2.3.4. Set searchRanges start to the first boundary point after
// potentialMatchs start
RangeBoundary newRangeBoundary =
TextDirectiveUtil::MoveRangeBoundaryOneWord(
{potentialMatch->GetStartContainer(),
potentialMatch->StartOffset()},
TextScanDirection::Right);
if (!newRangeBoundary.IsSetAndValid()) {
return nullptr;
}
searchRange->SetStart(newRangeBoundary.AsRaw(), rv);
if (rv.Failed()) {
return nullptr;
}
}
// 2.4. Let rangeEndSearchRange be a range whose start is potentialMatchs
// end and whose end is searchRanges end.
RefPtr<nsRange> rangeEndSearchRange = nsRange::Create(
potentialMatch->GetEndContainer(), potentialMatch->EndOffset(),
searchRange->GetEndContainer(), searchRange->EndOffset(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.5. While rangeEndSearchRange is not collapsed:
while (!rangeEndSearchRange->Collapsed()) {
// 2.5.1. If parsedValuess end item is non-null, then:
if (!aTextDirective.end.IsEmpty()) {
// 2.5.1.1. Let mustEndAtWordBoundary be true if parsedValuess suffix
// is null, false otherwise.
const bool mustEndAtWordBoundary = aTextDirective.suffix.IsEmpty();
// 2.5.1.2. Let endMatch be the result of running the find a string in
// range steps with query parsedValuess end, searchRange
// rangeEndSearchRange, wordStartBounded true, and wordEndBounded
// mustEndAtWordBoundary.
RefPtr<nsRange> endMatch = TextDirectiveUtil::FindStringInRange(
rangeEndSearchRange, aTextDirective.end, true,
mustEndAtWordBoundary);
// 2.5.1.3. If endMatch is null then return null.
if (!endMatch) {
TEXT_FRAGMENT_LOG(
"Did not find end '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.end).Data());
return nullptr;
}
// 2.5.1.4. Set potentialMatchs end to endMatchs end.
potentialMatch->SetEnd(endMatch->GetEndContainer(),
endMatch->EndOffset());
}
// 2.5.2. Assert: potentialMatch is non-null, not collapsed and represents
// a range exactly containing an instance of matching text.
MOZ_ASSERT(potentialMatch && !potentialMatch->Collapsed());
// 2.5.3. If parsedValuess suffix is null, return potentialMatch.
if (aTextDirective.suffix.IsEmpty()) {
TEXT_FRAGMENT_LOG("Did find a match.");
return potentialMatch;
}
// 2.5.4. Let suffixRange be a range with start equal to potentialMatchs
// end and end equal to searchRanges end.
RefPtr<nsRange> suffixRange = nsRange::Create(
potentialMatch->GetEndContainer(), potentialMatch->EndOffset(),
searchRange->GetEndContainer(), searchRange->EndOffset(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.5.5. Advance suffixRange's start to the next non-whitespace position.
TextDirectiveUtil::AdvanceStartToNextNonWhitespacePosition(*suffixRange);
// 2.5.6. Let suffixMatch be result of running the find a string in range
// steps with query parsedValue's suffix, searchRange suffixRange,
// wordStartBounded false, and wordEndBounded true.
RefPtr<nsRange> suffixMatch = TextDirectiveUtil::FindStringInRange(
suffixRange, aTextDirective.suffix, false, true);
// 2.5.7. If suffixMatch is null, return null.
// (If the suffix doesn't appear in the remaining text of the document,
// there's no possible way to make a match.)
if (!suffixMatch) {
TEXT_FRAGMENT_LOG(
"Did not find suffix '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.suffix).Data());
return nullptr;
}
// 2.5.8. If suffixMatch's start is suffixRange's start, return
// potentialMatch.
if (suffixMatch->GetStartContainer() ==
suffixRange->GetStartContainer() &&
suffixMatch->StartOffset() == suffixRange->StartOffset()) {
TEXT_FRAGMENT_LOG("Did find a match.");
return potentialMatch;
}
// 2.5.9. If parsedValue's end item is null then break;
// (If this is an exact match and the suffix doesnt match, start
// searching for the next range start by breaking out of this loop without
// rangeEndSearchRange being collapsed. If were looking for a range
// match, well continue iterating this inner loop since the range start
// will already be correct.)
if (aTextDirective.end.IsEmpty()) {
break;
}
// 2.5.10. Set rangeEndSearchRange's start to potentialMatch's end.
// (Otherwise, it is possible that we found the correct range start, but
// not the correct range end. Continue the inner loop to keep searching
// for another matching instance of rangeEnd.)
rangeEndSearchRange->SetStart(potentialMatch->GetEndContainer(),
potentialMatch->EndOffset());
}
// 2.6. If rangeEndSearchRange is collapsed then:
if (rangeEndSearchRange->Collapsed()) {
// 2.6.1. Assert parsedValue's end item is non-null.
// (This can only happen for range matches due to the break for exact
// matches in step 9 of the above loop. If we couldnt find a valid
// rangeEnd+suffix pair anywhere in the doc then theres no possible way
// to make a match.)
// ----
// XXX(:jjaschke): Not too sure about this. If a text directive is only
// defined by a (prefix +) start element, and the start element happens to
// be at the end of the document, `rangeEndSearchRange` could be
// collapsed. Therefore, the loop in section 2.5 does not run. Also,
// if there would be either an `end` and/or a `suffix`, this would assert
// instead of returning `nullptr`, indicating that there's no match.
// Instead, the following would make the algorithm more safe:
// if there is no end or suffix, the potential match is actually a match,
// so return it. Otherwise, the text directive can't be in the document,
// therefore return nullptr.
if (aTextDirective.end.IsEmpty() && aTextDirective.suffix.IsEmpty()) {
TEXT_FRAGMENT_LOG(
"rangeEndSearchRange was collapsed, no end or suffix "
"present. Returning a match");
return potentialMatch;
}
TEXT_FRAGMENT_LOG(
"rangeEndSearchRange was collapsed, there is an end or "
"suffix. There can't be a match.");
return nullptr;
}
}
// 3. Return null.
TEXT_FRAGMENT_LOG("Did not find a match.");
return nullptr;
}
} // namespace mozilla::dom

View File

@ -9,7 +9,7 @@
#include "js/TypeDecls.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/fragmentdirectives_ffi_generated.h"
#include "nsCycleCollectionParticipant.h"
#include "nsStringFwd.h"
@ -21,6 +21,7 @@ class nsRange;
namespace mozilla::dom {
class Document;
class Text;
class TextDirectiveFinder;
/**
* @brief The `FragmentDirective` class is the C++ representation of the
@ -32,7 +33,8 @@ class Text;
* `FragmentDirective::FindTextFragmentsInDocument()`.
* To avoid Text Directives being applied multiple times, this class implements
* the `uninvoked directive` mechanism, which in the spec is defined to be part
* of the `Document` [0].
* of the `Document` [0], by encapsuling the code in a lazily constructed
* helper, which is destroyed when all text directives have been found.
*
* [0]
* https://wicg.github.io/scroll-to-text-fragment/#document-uninvoked-directives
@ -44,13 +46,9 @@ class FragmentDirective final : public nsISupports, public nsWrapperCache {
public:
explicit FragmentDirective(Document* aDocument);
FragmentDirective(Document* aDocument,
nsTArray<TextDirective>&& aTextDirectives)
: mDocument(aDocument),
mUninvokedTextDirectives(std::move(aTextDirectives)) {}
protected:
~FragmentDirective() = default;
~FragmentDirective();
public:
Document* GetParentObject() const { return mDocument; };
@ -61,19 +59,15 @@ class FragmentDirective final : public nsISupports, public nsWrapperCache {
/**
* @brief Sets Text Directives as "uninvoked directive".
*/
void SetTextDirectives(nsTArray<TextDirective>&& aTextDirectives) {
mUninvokedTextDirectives = std::move(aTextDirectives);
}
void SetTextDirectives(nsTArray<TextDirective>&& aTextDirectives);
/** Returns true if there are Text Directives that have not been applied to
* the `Document`.
*/
bool HasUninvokedDirectives() const {
return !mUninvokedTextDirectives.IsEmpty();
};
bool HasUninvokedDirectives() const;
/** Clears all uninvoked directives. */
void ClearUninvokedDirectives() { mUninvokedTextDirectives.Clear(); }
void ClearUninvokedDirectives();
/** Inserts all text directive ranges into a `eTargetText` `Selection`. */
MOZ_CAN_RUN_SCRIPT
@ -88,8 +82,7 @@ class FragmentDirective final : public nsISupports, public nsWrapperCache {
*
* This method tries to follow the specification as close as possible in how
* to find a matching range for a text directive. However, instead of using
* collator-based search, a standard case-insensitive search is used
* (`nsString::find()`).
* collator-based search, the Gecko find-in-page algorithm is used (`nsFind`).
*/
nsTArray<RefPtr<nsRange>> FindTextFragmentsInDocument();
@ -116,7 +109,7 @@ class FragmentDirective final : public nsISupports, public nsWrapperCache {
nsCString& aFragment, nsTArray<TextDirective>* aTextDirectives = nullptr,
nsIURI* aURI = nullptr);
/** Utility funciton than returns a string for `aURI` ignoring all fragment
/** Utility function than returns a string for `aURI` ignoring all fragment
* directives.
*/
static nsresult GetSpecIgnoringFragmentDirective(
@ -145,11 +138,8 @@ class FragmentDirective final : public nsISupports, public nsWrapperCache {
MOZ_CAN_RUN_SCRIPT void RemoveAllTextDirectives(ErrorResult& aRv);
private:
RefPtr<nsRange> FindRangeForTextDirective(
const TextDirective& aTextDirective);
RefPtr<Document> mDocument;
nsTArray<TextDirective> mUninvokedTextDirectives;
UniquePtr<TextDirectiveFinder> mFinder;
};
} // namespace mozilla::dom

View File

@ -0,0 +1,350 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "TextDirectiveFinder.h"
#include "Document.h"
#include "TextDirectiveUtil.h"
#include "nsRange.h"
#include "fragmentdirectives_ffi_generated.h"
namespace mozilla::dom {
TextDirectiveFinder::TextDirectiveFinder(
Document& aDocument, nsTArray<TextDirective>&& aTextDirectives)
: mDocument(aDocument),
mUninvokedTextDirectives(std::move(aTextDirectives)) {}
bool TextDirectiveFinder::HasUninvokedDirectives() const {
return !mUninvokedTextDirectives.IsEmpty();
}
nsTArray<RefPtr<nsRange>> TextDirectiveFinder::FindTextDirectivesInDocument() {
if (mUninvokedTextDirectives.IsEmpty()) {
return {};
}
auto uri = TextDirectiveUtil::ShouldLog() && mDocument.GetDocumentURI()
? mDocument.GetDocumentURI()->GetSpecOrDefault()
: nsCString();
TEXT_FRAGMENT_LOG("Trying to find text directives in document '%s'.",
uri.Data());
mDocument.FlushPendingNotifications(FlushType::Frames);
// https://wicg.github.io/scroll-to-text-fragment/#invoke-text-directives
// To invoke text directives, given as input a list of text directives text
// directives and a Document document, run these steps:
// 1. Let ranges be a list of ranges, initially empty.
nsTArray<RefPtr<nsRange>> textDirectiveRanges(
mUninvokedTextDirectives.Length());
// Additionally (not mentioned in the spec), remove all text directives from
// the input list to keep only the ones that are not found.
// This code runs repeatedly during a page load, so it is possible that the
// match for a text directive has not been parsed yet.
nsTArray<TextDirective> uninvokedTextDirectives(
mUninvokedTextDirectives.Length());
// 2. For each text directive directive of text directives:
for (TextDirective& textDirective : mUninvokedTextDirectives) {
// 2.1 If the result of running find a range from a text directive given
// directive and document is non-null, then append it to ranges.
if (RefPtr<nsRange> range = FindRangeForTextDirective(textDirective)) {
textDirectiveRanges.AppendElement(range);
TEXT_FRAGMENT_LOG("Found text directive '%s'",
ToString(textDirective).c_str());
} else {
uninvokedTextDirectives.AppendElement(std::move(textDirective));
}
}
if (TextDirectiveUtil::ShouldLog()) {
if (uninvokedTextDirectives.Length() == mUninvokedTextDirectives.Length()) {
TEXT_FRAGMENT_LOG(
"Did not find any of the %zu uninvoked text directives.",
mUninvokedTextDirectives.Length());
} else {
TEXT_FRAGMENT_LOG(
"Found %zu of %zu text directives in the document.",
mUninvokedTextDirectives.Length() - uninvokedTextDirectives.Length(),
mUninvokedTextDirectives.Length());
}
if (uninvokedTextDirectives.IsEmpty()) {
TEXT_FRAGMENT_LOG("No uninvoked text directives left.");
} else {
TEXT_FRAGMENT_LOG("There are %zu uninvoked text directives left:",
uninvokedTextDirectives.Length());
for (size_t index = 0; index < uninvokedTextDirectives.Length();
++index) {
TEXT_FRAGMENT_LOG(" [%zu]: %s", index,
ToString(uninvokedTextDirectives[index]).c_str());
}
}
}
mUninvokedTextDirectives = std::move(uninvokedTextDirectives);
// 3. Return ranges.
return textDirectiveRanges;
}
RefPtr<nsRange> TextDirectiveFinder::FindRangeForTextDirective(
const TextDirective& aTextDirective) {
TEXT_FRAGMENT_LOG("Find range for text directive '%s'.",
ToString(aTextDirective).c_str());
// 1. Let searchRange be a range with start (document, 0) and end (document,
// documents length)
ErrorResult rv;
RefPtr<nsRange> searchRange =
nsRange::Create(&mDocument, 0, &mDocument, mDocument.Length(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2. While searchRange is not collapsed:
while (!searchRange->Collapsed()) {
// 2.1. Let potentialMatch be null.
RefPtr<nsRange> potentialMatch;
// 2.2. If parsedValuess prefix is not null:
if (!aTextDirective.prefix.IsEmpty()) {
// 2.2.1. Let prefixMatch be the the result of running the find a string
// in range steps with query parsedValuess prefix, searchRange
// searchRange, wordStartBounded true and wordEndBounded false.
RefPtr<nsRange> prefixMatch = TextDirectiveUtil::FindStringInRange(
searchRange, aTextDirective.prefix, true, false);
// 2.2.2. If prefixMatch is null, return null.
if (!prefixMatch) {
TEXT_FRAGMENT_LOG(
"Did not find prefix '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.prefix).Data());
return nullptr;
}
TEXT_FRAGMENT_LOG("Did find prefix '%s'.",
NS_ConvertUTF16toUTF8(aTextDirective.prefix).Data());
// 2.2.3. Set searchRanges start to the first boundary point after
// prefixMatchs start
const RangeBoundary boundaryPoint =
TextDirectiveUtil::MoveRangeBoundaryOneWord(
{prefixMatch->GetStartContainer(), prefixMatch->StartOffset()},
TextScanDirection::Right);
if (!boundaryPoint.IsSetAndValid()) {
return nullptr;
}
searchRange->SetStart(boundaryPoint.AsRaw(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.2.4. Let matchRange be a range whose start is prefixMatchs end and
// end is searchRanges end.
RefPtr<nsRange> matchRange = nsRange::Create(
prefixMatch->GetEndContainer(), prefixMatch->EndOffset(),
searchRange->GetEndContainer(), searchRange->EndOffset(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.2.5. Advance matchRanges start to the next non-whitespace position.
TextDirectiveUtil::AdvanceStartToNextNonWhitespacePosition(*matchRange);
// 2.2.6. If matchRange is collapsed return null.
// (This can happen if prefixMatchs end or its subsequent non-whitespace
// position is at the end of the document.)
if (matchRange->Collapsed()) {
return nullptr;
}
// 2.2.7. Assert: matchRanges start node is a Text node.
// (matchRanges start now points to the next non-whitespace text data
// following a matched prefix.)
MOZ_ASSERT(matchRange->GetStartContainer()->IsText());
// 2.2.8. Let mustEndAtWordBoundary be true if parsedValuess end is
// non-null or parsedValuess suffix is null, false otherwise.
const bool mustEndAtWordBoundary =
!aTextDirective.end.IsEmpty() || aTextDirective.suffix.IsEmpty();
// 2.2.9. Set potentialMatch to the result of running the find a string in
// range steps with query parsedValuess start, searchRange matchRange,
// wordStartBounded false, and wordEndBounded mustEndAtWordBoundary.
potentialMatch = TextDirectiveUtil::FindStringInRange(
matchRange, aTextDirective.start, false, mustEndAtWordBoundary);
// 2.2.10. If potentialMatch is null, return null.
if (!potentialMatch) {
TEXT_FRAGMENT_LOG(
"Did not find start '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.start).Data());
return nullptr;
}
TEXT_FRAGMENT_LOG("Did find start '%s'.",
NS_ConvertUTF16toUTF8(aTextDirective.start).Data());
// 2.2.11. If potentialMatchs start is not matchRanges start, then
// continue.
// (In this case, we found a prefix but it was followed by something other
// than a matching text so well continue searching for the next instance
// of prefix.)
if (potentialMatch->StartRef() != matchRange->StartRef()) {
TEXT_FRAGMENT_LOG(
"The prefix is not directly followed by the start element. "
"Discarding this attempt.");
continue;
}
}
// 2.3. Otherwise:
else {
// 2.3.1. Let mustEndAtWordBoundary be true if parsedValuess end is
// non-null or parsedValuess suffix is null, false otherwise.
const bool mustEndAtWordBoundary =
!aTextDirective.end.IsEmpty() || aTextDirective.suffix.IsEmpty();
// 2.3.2. Set potentialMatch to the result of running the find a string in
// range steps with query parsedValuess start, searchRange searchRange,
// wordStartBounded true, and wordEndBounded mustEndAtWordBoundary.
potentialMatch = TextDirectiveUtil::FindStringInRange(
searchRange, aTextDirective.start, true, mustEndAtWordBoundary);
// 2.3.3. If potentialMatch is null, return null.
if (!potentialMatch) {
TEXT_FRAGMENT_LOG(
"Did not find start '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.start).Data());
return nullptr;
}
// 2.3.4. Set searchRanges start to the first boundary point after
// potentialMatchs start
RangeBoundary newRangeBoundary =
TextDirectiveUtil::MoveRangeBoundaryOneWord(
{potentialMatch->GetStartContainer(),
potentialMatch->StartOffset()},
TextScanDirection::Right);
if (!newRangeBoundary.IsSetAndValid()) {
return nullptr;
}
searchRange->SetStart(newRangeBoundary.AsRaw(), rv);
if (rv.Failed()) {
return nullptr;
}
}
// 2.4. Let rangeEndSearchRange be a range whose start is potentialMatchs
// end and whose end is searchRanges end.
RefPtr<nsRange> rangeEndSearchRange = nsRange::Create(
potentialMatch->GetEndContainer(), potentialMatch->EndOffset(),
searchRange->GetEndContainer(), searchRange->EndOffset(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.5. While rangeEndSearchRange is not collapsed:
while (!rangeEndSearchRange->Collapsed()) {
// 2.5.1. If parsedValuess end item is non-null, then:
if (!aTextDirective.end.IsEmpty()) {
// 2.5.1.1. Let mustEndAtWordBoundary be true if parsedValuess suffix
// is null, false otherwise.
const bool mustEndAtWordBoundary = aTextDirective.suffix.IsEmpty();
// 2.5.1.2. Let endMatch be the result of running the find a string in
// range steps with query parsedValuess end, searchRange
// rangeEndSearchRange, wordStartBounded true, and wordEndBounded
// mustEndAtWordBoundary.
RefPtr<nsRange> endMatch = TextDirectiveUtil::FindStringInRange(
rangeEndSearchRange, aTextDirective.end, true,
mustEndAtWordBoundary);
// 2.5.1.3. If endMatch is null then return null.
if (!endMatch) {
TEXT_FRAGMENT_LOG(
"Did not find end '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.end).Data());
return nullptr;
}
// 2.5.1.4. Set potentialMatchs end to endMatchs end.
potentialMatch->SetEnd(endMatch->GetEndContainer(),
endMatch->EndOffset());
}
// 2.5.2. Assert: potentialMatch is non-null, not collapsed and represents
// a range exactly containing an instance of matching text.
MOZ_ASSERT(potentialMatch && !potentialMatch->Collapsed());
// 2.5.3. If parsedValuess suffix is null, return potentialMatch.
if (aTextDirective.suffix.IsEmpty()) {
TEXT_FRAGMENT_LOG("Did find a match.");
return potentialMatch;
}
// 2.5.4. Let suffixRange be a range with start equal to potentialMatchs
// end and end equal to searchRanges end.
RefPtr<nsRange> suffixRange = nsRange::Create(
potentialMatch->GetEndContainer(), potentialMatch->EndOffset(),
searchRange->GetEndContainer(), searchRange->EndOffset(), rv);
if (rv.Failed()) {
return nullptr;
}
// 2.5.5. Advance suffixRange's start to the next non-whitespace position.
TextDirectiveUtil::AdvanceStartToNextNonWhitespacePosition(*suffixRange);
// 2.5.6. Let suffixMatch be result of running the find a string in range
// steps with query parsedValue's suffix, searchRange suffixRange,
// wordStartBounded false, and wordEndBounded true.
RefPtr<nsRange> suffixMatch = TextDirectiveUtil::FindStringInRange(
suffixRange, aTextDirective.suffix, false, true);
// 2.5.7. If suffixMatch is null, return null.
// (If the suffix doesn't appear in the remaining text of the document,
// there's no possible way to make a match.)
if (!suffixMatch) {
TEXT_FRAGMENT_LOG(
"Did not find suffix '%s'. The text directive does not exist "
"in the document.",
NS_ConvertUTF16toUTF8(aTextDirective.suffix).Data());
return nullptr;
}
// 2.5.8. If suffixMatch's start is suffixRange's start, return
// potentialMatch.
if (suffixMatch->GetStartContainer() ==
suffixRange->GetStartContainer() &&
suffixMatch->StartOffset() == suffixRange->StartOffset()) {
TEXT_FRAGMENT_LOG("Did find a match.");
return potentialMatch;
}
// 2.5.9. If parsedValue's end item is null then break;
// (If this is an exact match and the suffix doesnt match, start
// searching for the next range start by breaking out of this loop without
// rangeEndSearchRange being collapsed. If were looking for a range
// match, well continue iterating this inner loop since the range start
// will already be correct.)
if (aTextDirective.end.IsEmpty()) {
break;
}
// 2.5.10. Set rangeEndSearchRange's start to potentialMatch's end.
// (Otherwise, it is possible that we found the correct range start, but
// not the correct range end. Continue the inner loop to keep searching
// for another matching instance of rangeEnd.)
rangeEndSearchRange->SetStart(potentialMatch->GetEndContainer(),
potentialMatch->EndOffset());
}
// 2.6. If rangeEndSearchRange is collapsed then:
if (rangeEndSearchRange->Collapsed()) {
// 2.6.1. Assert parsedValue's end item is non-null.
// (This can only happen for range matches due to the break for exact
// matches in step 9 of the above loop. If we couldnt find a valid
// rangeEnd+suffix pair anywhere in the doc then theres no possible way
// to make a match.)
// ----
// XXX(:jjaschke): Not too sure about this. If a text directive is only
// defined by a (prefix +) start element, and the start element happens to
// be at the end of the document, `rangeEndSearchRange` could be
// collapsed. Therefore, the loop in section 2.5 does not run. Also,
// if there would be either an `end` and/or a `suffix`, this would assert
// instead of returning `nullptr`, indicating that there's no match.
// Instead, the following would make the algorithm more safe:
// if there is no end or suffix, the potential match is actually a match,
// so return it. Otherwise, the text directive can't be in the document,
// therefore return nullptr.
if (aTextDirective.end.IsEmpty() && aTextDirective.suffix.IsEmpty()) {
TEXT_FRAGMENT_LOG(
"rangeEndSearchRange was collapsed, no end or suffix "
"present. Returning a match");
return potentialMatch;
}
TEXT_FRAGMENT_LOG(
"rangeEndSearchRange was collapsed, there is an end or "
"suffix. There can't be a match.");
return nullptr;
}
}
// 3. Return null.
TEXT_FRAGMENT_LOG("Did not find a match.");
return nullptr;
}
} // namespace mozilla::dom

View File

@ -0,0 +1,58 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef DOM_TEXTDIRECTIVEFINDER_H_
#define DOM_TEXTDIRECTIVEFINDER_H_
#include "mozilla/RefPtr.h"
#include "nsTArray.h"
class nsRange;
struct TextDirective;
namespace mozilla::dom {
class Document;
/**
* @brief Finds one or more `TextDirective`s in a `Document`.
*
* This class is designed to consume the `TextDirective`s.
* Every `TextDirective` which is found is removed from the list of uninvoked
* text directives, and is returned as an `nsRange`.
*
* Internally, finding a text directive in a document uses Gecko's find-in-page
* implementation `nsFind`.
*/
class TextDirectiveFinder final {
public:
TextDirectiveFinder(Document& aDocument,
nsTArray<TextDirective>&& aTextDirectives);
/**
* @brief Attempts to convert all uninvoked text directives to ranges.
*
* This method is the main entry point of this class.
*/
nsTArray<RefPtr<nsRange>> FindTextDirectivesInDocument();
/**
* Returns true if there are text directives left which were not yet found in
* the document.
*/
bool HasUninvokedDirectives() const;
/**
* Finds a range for _one_ text directive.
*/
RefPtr<nsRange> FindRangeForTextDirective(
const TextDirective& aTextDirective);
private:
Document& mDocument;
nsTArray<TextDirective> mUninvokedTextDirectives;
};
} // namespace mozilla::dom
#endif

View File

@ -477,6 +477,7 @@ UNIFIED_SOURCES += [
"SubtleCrypto.cpp",
"TestUtils.cpp",
"Text.cpp",
"TextDirectiveFinder.cpp",
"TextDirectiveUtil.cpp",
"TextInputProcessor.cpp",
"ThirdPartyUtil.cpp",