/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* 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 "HTMLEditor.h" #include "HTMLEditorEventListener.h" #include "HTMLEditUtils.h" #include "JoinNodeTransaction.h" #include "ReplaceTextTransaction.h" #include "SplitNodeTransaction.h" #include "TypeInState.h" #include "WSRunObject.h" #include "mozilla/ComposerCommandsUpdater.h" #include "mozilla/ContentIterator.h" #include "mozilla/DebugOnly.h" #include "mozilla/EditAction.h" #include "mozilla/EditorDOMPoint.h" #include "mozilla/EditorUtils.h" #include "mozilla/EventStates.h" #include "mozilla/InternalMutationEvent.h" #include "mozilla/mozInlineSpellChecker.h" #include "mozilla/PresShell.h" #include "mozilla/StaticPrefs_editor.h" #include "mozilla/StyleSheet.h" #include "mozilla/StyleSheetInlines.h" #include "mozilla/Telemetry.h" #include "mozilla/TextEvents.h" #include "mozilla/TextServicesDocument.h" #include "mozilla/css/Loader.h" #include "mozilla/dom/AncestorIterator.h" #include "mozilla/dom/Attr.h" #include "mozilla/dom/DocumentFragment.h" #include "mozilla/dom/DocumentInlines.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/Event.h" #include "mozilla/dom/EventTarget.h" #include "mozilla/dom/HTMLAnchorElement.h" #include "mozilla/dom/HTMLBodyElement.h" #include "mozilla/dom/Selection.h" #include "nsContentList.h" #include "nsContentUtils.h" #include "nsCRT.h" #include "nsElementTable.h" #include "nsFocusManager.h" #include "nsGenericHTMLElement.h" #include "nsGkAtoms.h" #include "nsHTMLDocument.h" #include "nsIContent.h" #include "nsIEditActionListener.h" #include "nsIFrame.h" #include "nsIPrincipal.h" #include "nsISelectionController.h" #include "nsIURI.h" #include "nsIWidget.h" #include "nsNetUtil.h" #include "nsPresContext.h" #include "nsPIDOMWindow.h" #include "nsStyledElement.h" #include "nsTextFragment.h" #include "nsUnicharUtils.h" namespace mozilla { using namespace dom; using namespace widget; using ChildBlockBoundary = HTMLEditUtils::ChildBlockBoundary; const char16_t kNBSP = 160; // Some utilities to handle overloading of "A" tag for link and named anchor. static bool IsLinkTag(const nsAtom& aTagName) { return &aTagName == nsGkAtoms::href; } static bool IsNamedAnchorTag(const nsAtom& aTagName) { return &aTagName == nsGkAtoms::anchor; } HTMLEditor::HTMLEditor() : mCRInParagraphCreatesParagraph(false), mCSSAware(false), mSelectedCellIndex(0), mIsObjectResizingEnabled( StaticPrefs::editor_resizing_enabled_by_default()), mIsResizing(false), mPreserveRatio(false), mResizedObjectIsAnImage(false), mIsAbsolutelyPositioningEnabled( StaticPrefs::editor_positioning_enabled_by_default()), mResizedObjectIsAbsolutelyPositioned(false), mGrabberClicked(false), mIsMoving(false), mSnapToGridEnabled(false), mIsInlineTableEditingEnabled( StaticPrefs::editor_inline_table_editing_enabled_by_default()), mOriginalX(0), mOriginalY(0), mResizedObjectX(0), mResizedObjectY(0), mResizedObjectWidth(0), mResizedObjectHeight(0), mResizedObjectMarginLeft(0), mResizedObjectMarginTop(0), mResizedObjectBorderLeft(0), mResizedObjectBorderTop(0), mXIncrementFactor(0), mYIncrementFactor(0), mWidthIncrementFactor(0), mHeightIncrementFactor(0), mInfoXIncrement(20), mInfoYIncrement(20), mPositionedObjectX(0), mPositionedObjectY(0), mPositionedObjectWidth(0), mPositionedObjectHeight(0), mPositionedObjectMarginLeft(0), mPositionedObjectMarginTop(0), mPositionedObjectBorderLeft(0), mPositionedObjectBorderTop(0), mGridSize(0), mDefaultParagraphSeparator( Preferences::GetBool("editor.use_div_for_default_newlines", true) ? ParagraphSeparator::div : ParagraphSeparator::br) { mIsHTMLEditorClass = true; } HTMLEditor::~HTMLEditor() { // Collect the data of `beforeinput` event only when it's enabled because // web apps should switch their behavior with feature detection with // checking `onbeforeinput` or `getTargetRanges`. if (StaticPrefs::dom_input_events_beforeinput_enabled()) { Telemetry::Accumulate( Telemetry::HTMLEDITORS_WITH_BEFOREINPUT_LISTENERS, MayHaveBeforeInputEventListenersForTelemetry() ? 1 : 0); Telemetry::Accumulate( Telemetry::HTMLEDITORS_OVERRIDDEN_BY_BEFOREINPUT_LISTENERS, mHasBeforeInputBeenCanceled ? 1 : 0); Telemetry::Accumulate( Telemetry:: HTMLEDITORS_WITH_MUTATION_LISTENERS_WITHOUT_BEFOREINPUT_LISTENERS, !MayHaveBeforeInputEventListenersForTelemetry() && MayHaveMutationEventListeners() ? 1 : 0); Telemetry::Accumulate( Telemetry:: HTMLEDITORS_WITH_MUTATION_OBSERVERS_WITHOUT_BEFOREINPUT_LISTENERS, !MayHaveBeforeInputEventListenersForTelemetry() && MutationObserverHasObservedNodeForTelemetry() ? 1 : 0); } mTypeInState = nullptr; if (mDisabledLinkHandling) { if (Document* doc = GetDocument()) { doc->SetLinkHandlingEnabled(mOldLinkHandlingEnabled); } } RemoveEventListeners(); HideAnonymousEditingUIs(); } NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLEditor) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLEditor, TextEditor) NS_IMPL_CYCLE_COLLECTION_UNLINK(mTypeInState) NS_IMPL_CYCLE_COLLECTION_UNLINK(mComposerCommandsUpdater) NS_IMPL_CYCLE_COLLECTION_UNLINK(mChangedRangeForTopLevelEditSubAction) tmp->HideAnonymousEditingUIs(); NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLEditor, TextEditor) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTypeInState) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mComposerCommandsUpdater) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChangedRangeForTopLevelEditSubAction) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTopLeftHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTopHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTopRightHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLeftHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRightHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBottomLeftHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBottomHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBottomRightHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mActivatedHandle) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizingShadow) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizingInfo) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizedObject) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAbsolutelyPositionedObject) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGrabber) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPositioningShadow) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInlineEditedCell) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddColumnBeforeButton) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRemoveColumnButton) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddColumnAfterButton) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddRowBeforeButton) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRemoveRowButton) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddRowAfterButton) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_ADDREF_INHERITED(HTMLEditor, EditorBase) NS_IMPL_RELEASE_INHERITED(HTMLEditor, EditorBase) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(HTMLEditor) NS_INTERFACE_MAP_ENTRY(nsIHTMLEditor) NS_INTERFACE_MAP_ENTRY(nsIHTMLObjectResizer) NS_INTERFACE_MAP_ENTRY(nsIHTMLAbsPosEditor) NS_INTERFACE_MAP_ENTRY(nsIHTMLInlineTableEditor) NS_INTERFACE_MAP_ENTRY(nsITableEditor) NS_INTERFACE_MAP_ENTRY(nsIMutationObserver) NS_INTERFACE_MAP_ENTRY(nsIEditorMailSupport) NS_INTERFACE_MAP_END_INHERITING(TextEditor) nsresult HTMLEditor::Init(Document& aDoc, Element* aRoot, nsISelectionController* aSelCon, uint32_t aFlags, const nsAString& aInitialValue) { MOZ_ASSERT(!mInitSucceeded, "HTMLEditor::Init() called again without calling PreDestroy()?"); MOZ_ASSERT(aInitialValue.IsEmpty(), "Non-empty initial values not supported"); nsresult rv = EditorBase::Init(aDoc, aRoot, nullptr, aFlags, aInitialValue); if (NS_FAILED(rv)) { NS_WARNING("EditorBase::Init() failed"); return rv; } // Init mutation observer aDoc.AddMutationObserverUnlessExists(this); if (!mRootElement) { UpdateRootElement(); } // disable Composer-only features if (IsMailEditor()) { DebugOnly rvIgnored = SetAbsolutePositioningEnabled(false); NS_WARNING_ASSERTION( NS_SUCCEEDED(rvIgnored), "HTMLEditor::SetAbsolutePositioningEnabled(false) failed, but ignored"); rvIgnored = SetSnapToGridEnabled(false); NS_WARNING_ASSERTION( NS_SUCCEEDED(rvIgnored), "HTMLEditor::SetSnapToGridEnabled(false) failed, but ignored"); } // Init the HTML-CSS utils mCSSEditUtils = MakeUnique(this); // disable links Document* document = GetDocument(); if (NS_WARN_IF(!document)) { return NS_ERROR_FAILURE; } if (!IsPlaintextEditor() && !IsInteractionAllowed()) { mDisabledLinkHandling = true; mOldLinkHandlingEnabled = document->LinkHandlingEnabled(); document->SetLinkHandlingEnabled(false); } // init the type-in state mTypeInState = new TypeInState(); if (!IsInteractionAllowed()) { nsCOMPtr uaURI; rv = NS_NewURI(getter_AddRefs(uaURI), "resource://gre/res/EditorOverride.css"); NS_ENSURE_SUCCESS(rv, rv); rv = document->LoadAdditionalStyleSheet(Document::eAgentSheet, uaURI); NS_ENSURE_SUCCESS(rv, rv); } // XXX `eNotEditing` is a lie since InitEditorContentAndSelection() may // insert padding `
`. AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing); if (NS_WARN_IF(!editActionData.CanHandle())) { return NS_ERROR_FAILURE; } rv = InitEditorContentAndSelection(); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::InitEditorContentAndSelection() failed"); // XXX Sholdn't we expose `NS_ERROR_EDITOR_DESTROYED` even though this // is a public method? return EditorBase::ToGenericNSResult(rv); } // Throw away the old transaction manager if this is not the first time that // we're initializing the editor. ClearUndoRedo(); EnableUndoRedo(); MOZ_ASSERT(!mInitSucceeded, "HTMLEditor::Init() shouldn't be nested"); mInitSucceeded = true; return NS_OK; } void HTMLEditor::PreDestroy(bool aDestroyingFrames) { if (mDidPreDestroy) { return; } mInitSucceeded = false; // FYI: Cannot create AutoEditActionDataSetter here. However, it does not // necessary for the methods called by the following code. RefPtr document = GetDocument(); if (document) { document->RemoveMutationObserver(this); if (!IsInteractionAllowed()) { nsCOMPtr uaURI; nsresult rv = NS_NewURI(getter_AddRefs(uaURI), "resource://gre/res/EditorOverride.css"); if (NS_SUCCEEDED(rv)) { document->RemoveAdditionalStyleSheet(Document::eAgentSheet, uaURI); } } } // Clean up after our anonymous content -- we don't want these nodes to // stay around (which they would, since the frames have an owning reference). PresShell* presShell = GetPresShell(); if (presShell && presShell->IsDestroying()) { // Just destroying PresShell now. // We have to keep UI elements of anonymous content until PresShell // is destroyed. RefPtr self = this; nsContentUtils::AddScriptRunner( NS_NewRunnableFunction("HTMLEditor::PreDestroy", [self]() { self->HideAnonymousEditingUIs(); })); } else { // PresShell is alive or already gone. HideAnonymousEditingUIs(); } EditorBase::PreDestroy(aDestroyingFrames); } NS_IMETHODIMP HTMLEditor::NotifySelectionChanged(Document* aDocument, Selection* aSelection, int16_t aReason) { if (NS_WARN_IF(!aDocument) || NS_WARN_IF(!aSelection)) { return NS_ERROR_INVALID_ARG; } AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing); if (NS_WARN_IF(!editActionData.CanHandle())) { return NS_ERROR_NOT_INITIALIZED; } if (mTypeInState) { RefPtr typeInState = mTypeInState; typeInState->OnSelectionChange(*aSelection, aReason); // We used a class which derived from nsISelectionListener to call // HTMLEditor::RefreshEditingUI(). The lifetime of the class was // exactly same as mTypeInState. So, call it only when mTypeInState // is not nullptr. if ((aReason & (nsISelectionListener::MOUSEDOWN_REASON | nsISelectionListener::KEYPRESS_REASON | nsISelectionListener::SELECTALL_REASON)) && aSelection) { // the selection changed and we need to check if we have to // hide and/or redisplay resizing handles // FYI: This is an XPCOM method. So, the caller, Selection, guarantees // the lifetime of this instance. So, don't need to grab this with // local variable. DebugOnly rv = RefreshEditingUI(); NS_WARNING_ASSERTION( NS_SUCCEEDED(rv), "HTMLEditor::RefreshEditingUI() failed, but ignored"); } } if (mComposerCommandsUpdater) { RefPtr updater = mComposerCommandsUpdater; updater->OnSelectionChange(); } nsresult rv = EditorBase::NotifySelectionChanged(aDocument, aSelection, aReason); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::NotifySelectionChanged() failed"); return rv; } void HTMLEditor::UpdateRootElement() { // Use the HTML documents body element as the editor root if we didn't // get a root element during initialization. mRootElement = GetBodyElement(); if (!mRootElement) { RefPtr doc = GetDocument(); if (doc) { // If there is no HTML body element, // we should use the document root element instead. mRootElement = doc->GetDocumentElement(); } // else leave it null, for lack of anything better. } } Element* HTMLEditor::FindSelectionRoot(nsINode* aNode) const { if (NS_WARN_IF(!aNode)) { return nullptr; } MOZ_ASSERT(aNode->IsDocument() || aNode->IsContent(), "aNode must be content or document node"); Document* document = aNode->GetComposedDoc(); if (NS_WARN_IF(!document)) { return nullptr; } if (aNode->IsInUncomposedDoc() && (document->HasFlag(NODE_IS_EDITABLE) || !aNode->IsContent())) { return document->GetRootElement(); } // XXX If we have readonly flag, shouldn't return the element which has // contenteditable="true"? However, such case isn't there without chrome // permission script. if (IsReadonly()) { // We still want to allow selection in a readonly editor. return GetRoot(); } nsIContent* content = aNode->AsContent(); if (!content->HasFlag(NODE_IS_EDITABLE)) { // If the content is in read-write state but is not editable itself, // return it as the selection root. if (content->IsElement() && content->AsElement()->State().HasState(NS_EVENT_STATE_READWRITE)) { return content->AsElement(); } return nullptr; } // For non-readonly editors we want to find the root of the editable subtree // containing aContent. return content->GetEditingHost(); } void HTMLEditor::CreateEventListeners() { // Don't create the handler twice if (!mEventListener) { mEventListener = new HTMLEditorEventListener(); } } nsresult HTMLEditor::InstallEventListeners() { if (NS_WARN_IF(!IsInitialized()) || NS_WARN_IF(!mEventListener)) { return NS_ERROR_NOT_INITIALIZED; } // NOTE: HTMLEditor doesn't need to initialize mEventTarget here because // the target must be document node and it must be referenced as weak pointer. HTMLEditorEventListener* listener = reinterpret_cast(mEventListener.get()); nsresult rv = listener->Connect(this); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "HTMLEditorEventListener::Connect() failed"); return rv; } void HTMLEditor::RemoveEventListeners() { if (!IsInitialized()) { return; } TextEditor::RemoveEventListeners(); } NS_IMETHODIMP HTMLEditor::SetFlags(uint32_t aFlags) { nsresult rv = TextEditor::SetFlags(aFlags); if (NS_FAILED(rv)) { NS_WARNING("TextEditor::SetFlags() failed"); return rv; } // Sets mCSSAware to correspond to aFlags. This toggles whether CSS is // used to style elements in the editor. Note that the editor is only CSS // aware by default in Composer and in the mail editor. mCSSAware = !NoCSS() && !IsMailEditor(); return NS_OK; } NS_IMETHODIMP HTMLEditor::BeginningOfDocument() { AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing); if (NS_WARN_IF(!editActionData.CanHandle())) { return NS_ERROR_NOT_INITIALIZED; } nsresult rv = MaybeCollapseSelectionAtFirstEditableNode(false); NS_WARNING_ASSERTION( NS_SUCCEEDED(rv), "HTMLEditor::MaybeCollapseSelectionAtFirstEditableNode(false) failed"); return rv; } void HTMLEditor::InitializeSelectionAncestorLimit( nsIContent& aAncestorLimit) const { MOZ_ASSERT(IsEditActionDataAvailable()); // Hack for initializing selection. // HTMLEditor::MaybeCollapseSelectionAtFirstEditableNode() will try to // collapse selection at first editable text node or inline element which // cannot have text nodes as its children. However, selection has already // set into the new editing host by user, we should not change it. For // solving this issue, we should do nothing if selection range is in active // editing host except it's not collapsed at start of the editing host since // aSelection.SetAncestorLimiter(aAncestorLimit) will collapse selection // at start of the new limiter if focus node of aSelection is outside of the // editing host. However, we need to check here if selection is already // collapsed at start of the editing host because it's possible JS to do it. // In such case, we should not modify selection with calling // MaybeCollapseSelectionAtFirstEditableNode(). // Basically, we should try to collapse selection at first editable node // in HTMLEditor. bool tryToCollapseSelectionAtFirstEditableNode = true; if (SelectionRefPtr()->RangeCount() == 1 && SelectionRefPtr()->IsCollapsed()) { Element* editingHost = GetActiveEditingHost(); const nsRange* range = SelectionRefPtr()->GetRangeAt(0); if (range->GetStartContainer() == editingHost && !range->StartOffset()) { // JS or user operation has already collapsed selection at start of // the editing host. So, we don't need to try to change selection // in this case. tryToCollapseSelectionAtFirstEditableNode = false; } } EditorBase::InitializeSelectionAncestorLimit(aAncestorLimit); // XXX Do we need to check if we still need to change selection? E.g., // we could have already lost focus while we're changing the ancestor // limiter because it may causes "selectionchange" event. if (tryToCollapseSelectionAtFirstEditableNode) { DebugOnly rvIgnored = MaybeCollapseSelectionAtFirstEditableNode(true); NS_WARNING_ASSERTION( NS_SUCCEEDED(rvIgnored), "HTMLEditor::MaybeCollapseSelectionAtFirstEditableNode(true) failed, " "but ignored"); } } nsresult HTMLEditor::MaybeCollapseSelectionAtFirstEditableNode( bool aIgnoreIfSelectionInEditingHost) const { MOZ_ASSERT(IsEditActionDataAvailable()); // Use editing host. If you use root element here, selection may be // moved to element, e.g., if there is a text node in