Bug 1934825, part 2 - Text Fragments: Create chrome-only API to create text directives from a range. r=farre

Differential Revision: https://phabricator.services.mozilla.com/D231100
This commit is contained in:
Jan-Niklas Jaeschke
2025-01-23 22:27:22 +00:00
parent 1c2c16211e
commit e097878f21
3 changed files with 35 additions and 0 deletions

View File

@@ -16,6 +16,7 @@
#include "mozilla/dom/BrowsingContextGroup.h"
#include "mozilla/dom/FragmentDirectiveBinding.h"
#include "mozilla/dom/FragmentOrElement.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/PresShell.h"
#include "nsContentUtils.h"
@@ -421,5 +422,26 @@ void FragmentDirective::RemoveAllTextDirectives(ErrorResult& aRv) {
}
targetTextSelection->RemoveAllRanges(aRv);
}
already_AddRefed<Promise> FragmentDirective::CreateTextDirective(
nsRange& aRange) {
RefPtr<Promise> resultPromise =
Promise::Create(mDocument->GetOwnerGlobal(), IgnoreErrors());
if (!resultPromise) {
return nullptr;
}
if (!StaticPrefs::dom_text_fragments_create_text_fragment_enabled() ||
!StaticPrefs::dom_text_fragments_enabled()) {
TEXT_FRAGMENT_LOG("Creating text fragments is disabled.");
resultPromise->MaybeResolve(JS::NullHandleValue);
return resultPromise.forget();
}
if (aRange.Collapsed()) {
TEXT_FRAGMENT_LOG("Collapsed range. Nothing to do here...");
resultPromise->MaybeResolve(JS::NullHandleValue);
return resultPromise.forget();
}
return resultPromise.forget();
}
} // namespace mozilla::dom

View File

@@ -20,6 +20,7 @@ class nsIURI;
class nsRange;
namespace mozilla::dom {
class Document;
class Promise;
class Text;
class TextDirectiveFinder;
@@ -137,6 +138,15 @@ class FragmentDirective final : public nsISupports, public nsWrapperCache {
*/
MOZ_CAN_RUN_SCRIPT void RemoveAllTextDirectives(ErrorResult& aRv);
/** Creates a text directive string from a given range.
*
* @param aRange The input range.
*
* @return Returns the created text directive as resolved promise, or a
* rejected promise in case of an error.
*/
already_AddRefed<Promise> CreateTextDirective(nsRange& aRange);
private:
RefPtr<Document> mDocument;
UniquePtr<TextDirectiveFinder> mFinder;

View File

@@ -13,4 +13,7 @@ interface FragmentDirective {
[Pref="dom.text_fragments.enabled", ChromeOnly, Throws]
undefined removeAllTextDirectives();
[Pref="dom.text_fragments.create_text_fragment.enabled", ChromeOnly]
Promise<DOMString> createTextDirective(Range range);
};