/* -*- 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/. */ #ifndef HTMLEditRules_h #define HTMLEditRules_h #include "TypeInState.h" #include "mozilla/SelectionState.h" #include "mozilla/TextEditRules.h" #include "nsCOMPtr.h" #include "nsIEditActionListener.h" #include "nsIEditor.h" #include "nsIHTMLEditor.h" #include "nsISupportsImpl.h" #include "nsTArray.h" #include "nscore.h" class nsAtom; class nsIDOMCharacterData; class nsIDOMDocument; class nsIDOMElement; class nsIDOMNode; class nsIEditor; class nsINode; class nsRange; namespace mozilla { class EditActionResult; class HTMLEditor; class RulesInfo; class TextEditor; struct EditorDOMPoint; namespace dom { class Element; class Selection; } // namespace dom struct StyleCache final : public PropItem { bool mPresent; StyleCache() : PropItem() , mPresent(false) { MOZ_COUNT_CTOR(StyleCache); } StyleCache(nsAtom* aTag, const nsAString& aAttr, const nsAString& aValue) : PropItem(aTag, aAttr, aValue) , mPresent(false) { MOZ_COUNT_CTOR(StyleCache); } StyleCache(nsAtom* aTag, const nsAString& aAttr) : PropItem(aTag, aAttr, EmptyString()) , mPresent(false) { MOZ_COUNT_CTOR(StyleCache); } ~StyleCache() { MOZ_COUNT_DTOR(StyleCache); } }; #define SIZE_STYLE_TABLE 19 class HTMLEditRules : public TextEditRules , public nsIEditActionListener { public: NS_DECL_ISUPPORTS_INHERITED NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLEditRules, TextEditRules) HTMLEditRules(); // nsIEditRules methods NS_IMETHOD Init(TextEditor* aTextEditor) override; NS_IMETHOD DetachEditor() override; NS_IMETHOD BeforeEdit(EditAction action, nsIEditor::EDirection aDirection) override; NS_IMETHOD AfterEdit(EditAction action, nsIEditor::EDirection aDirection) override; NS_IMETHOD WillDoAction(Selection* aSelection, RulesInfo* aInfo, bool* aCancel, bool* aHandled) override; NS_IMETHOD DidDoAction(Selection* aSelection, RulesInfo* aInfo, nsresult aResult) override; NS_IMETHOD_(bool) DocumentIsEmpty() override; NS_IMETHOD DocumentModified() override; nsresult GetListState(bool* aMixed, bool* aOL, bool* aUL, bool* aDL); nsresult GetListItemState(bool* aMixed, bool* aLI, bool* aDT, bool* aDD); nsresult GetIndentState(bool* aCanIndent, bool* aCanOutdent); nsresult GetAlignment(bool* aMixed, nsIHTMLEditor::EAlignment* aAlign); nsresult GetParagraphState(bool* aMixed, nsAString& outFormat); nsresult MakeSureElemStartsOrEndsOnCR(nsINode& aNode); // nsIEditActionListener methods NS_IMETHOD WillCreateNode(const nsAString& aTag, nsIDOMNode* aParent, int32_t aPosition) override; NS_IMETHOD DidCreateNode(const nsAString& aTag, nsIDOMNode* aNode, nsIDOMNode* aParent, int32_t aPosition, nsresult aResult) override; NS_IMETHOD WillInsertNode(nsIDOMNode* aNode, nsIDOMNode* aParent, int32_t aPosition) override; NS_IMETHOD DidInsertNode(nsIDOMNode* aNode, nsIDOMNode* aParent, int32_t aPosition, nsresult aResult) override; NS_IMETHOD WillDeleteNode(nsIDOMNode* aChild) override; NS_IMETHOD DidDeleteNode(nsIDOMNode* aChild, nsresult aResult) override; NS_IMETHOD WillSplitNode(nsIDOMNode* aExistingRightNode, int32_t aOffset) override; NS_IMETHOD DidSplitNode(nsIDOMNode* aExistingRightNode, int32_t aOffset, nsIDOMNode* aNewLeftNode, nsresult aResult) override; NS_IMETHOD WillJoinNodes(nsIDOMNode* aLeftNode, nsIDOMNode* aRightNode, nsIDOMNode* aParent) override; NS_IMETHOD DidJoinNodes(nsIDOMNode* aLeftNode, nsIDOMNode* aRightNode, nsIDOMNode* aParent, nsresult aResult) override; NS_IMETHOD WillInsertText(nsIDOMCharacterData* aTextNode, int32_t aOffset, const nsAString &aString) override; NS_IMETHOD DidInsertText(nsIDOMCharacterData* aTextNode, int32_t aOffset, const nsAString &aString, nsresult aResult) override; NS_IMETHOD WillDeleteText(nsIDOMCharacterData* aTextNode, int32_t aOffset, int32_t aLength) override; NS_IMETHOD DidDeleteText(nsIDOMCharacterData* aTextNode, int32_t aOffset, int32_t aLength, nsresult aResult) override; NS_IMETHOD WillDeleteSelection(nsISelection* aSelection) override; NS_IMETHOD DidDeleteSelection(nsISelection* aSelection) override; void DeleteNodeIfCollapsedText(nsINode& aNode); protected: virtual ~HTMLEditRules(); enum RulesEndpoint { kStart, kEnd }; void InitFields(); void WillInsert(Selection& aSelection, bool* aCancel); nsresult WillInsertText(EditAction aAction, Selection* aSelection, bool* aCancel, bool* aHandled, const nsAString* inString, nsAString* outString, int32_t aMaxLength); nsresult WillLoadHTML(Selection* aSelection, bool* aCancel); nsresult WillInsertBreak(Selection& aSelection, bool* aCancel, bool* aHandled); nsresult StandardBreakImpl(nsINode& aNode, int32_t aOffset, Selection& aSelection); nsresult DidInsertBreak(Selection* aSelection, nsresult aResult); nsresult SplitMailCites(Selection* aSelection, bool* aHandled); nsresult WillDeleteSelection(Selection* aSelection, nsIEditor::EDirection aAction, nsIEditor::EStripWrappers aStripWrappers, bool* aCancel, bool* aHandled); nsresult DidDeleteSelection(Selection* aSelection, nsIEditor::EDirection aDir, nsresult aResult); nsresult InsertBRIfNeeded(Selection* aSelection); /** * CanContainParagraph() returns true if aElement can have a
element as
* its child or its descendant.
*/
bool CanContainParagraph(Element& aElement) const;
/**
* Insert a normal
element or a moz-
element to aNode when
* aNode is a block and it has no children.
*
* @param aNode Reference to a block parent.
* @param aInsertMozBR true if this should insert a moz-
element.
* Otherwise, i.e., this should insert a normal
* element, false.
*/
nsresult InsertBRIfNeededInternal(nsINode& aNode, bool aInsertMozBR);
mozilla::EditorDOMPoint GetGoodSelPointForNode(nsINode& aNode,
nsIEditor::EDirection aAction);
/**
* TryToJoinBlocks() tries to join two block elements. The right element is
* always joined to the left element. If the elements are the same type and
* not nested within each other, JoinNodesSmart() is called (example, joining
* two list items together into one). If the elements are not the same type,
* or one is a descendant of the other, we instead destroy the right block
* placing its children into leftblock. DTD containment rules are followed
* throughout.
*
* @return Sets canceled to true if the operation should do
* nothing anymore even if this doesn't join the blocks.
* Sets handled to true if this actually handles the
* request. Note that this may set it to true even if this
* does not join the block. E.g., if the blocks shouldn't
* be joined or it's impossible to join them but it's not
* unexpected case, this returns true with this.
*/
EditActionResult TryToJoinBlocks(nsIContent& aLeftNode,
nsIContent& aRightNode);
/**
* MoveBlock() moves the content from aRightBlock starting from aRightOffset
* into aLeftBlock at aLeftOffset. Note that the "block" can be inline nodes
* between
s, or between blocks, etc. DTD containment rules are followed
* throughout.
*
* @return Sets handled to true if this actually joins the nodes.
* canceled is always false.
*/
EditActionResult MoveBlock(Element& aLeftBlock, Element& aRightBlock,
int32_t aLeftOffset, int32_t aRightOffset);
/**
* MoveNodeSmart() moves aNode to (aDestElement, aInOutDestOffset).
* DTD containment rules are followed throughout.
*
* @param aOffset returns the point after inserted content.
* @return Sets true to handled if this actually moves
* the nodes.
* canceled is always false.
*/
EditActionResult MoveNodeSmart(nsIContent& aNode, Element& aDestElement,
int32_t* aInOutDestOffset);
/**
* MoveContents() moves the contents of aElement to (aDestElement,
* aInOutDestOffset). DTD containment rules are followed throughout.
*
* @param aInOutDestOffset updated to point after inserted content.
* @return Sets true to handled if this actually moves
* the nodes.
* canceled is always false.
*/
EditActionResult MoveContents(Element& aElement, Element& aDestElement,
int32_t* aInOutDestOffset);
nsresult DeleteNonTableElements(nsINode* aNode);
nsresult WillMakeList(Selection* aSelection,
const nsAString* aListType,
bool aEntireList,
const nsAString* aBulletType,
bool* aCancel, bool* aHandled,
const nsAString* aItemType = nullptr);
nsresult WillRemoveList(Selection* aSelection, bool aOrdered, bool* aCancel,
bool* aHandled);
nsresult WillIndent(Selection* aSelection, bool* aCancel, bool* aHandled);
nsresult WillCSSIndent(Selection* aSelection, bool* aCancel, bool* aHandled);
nsresult WillHTMLIndent(Selection* aSelection, bool* aCancel,
bool* aHandled);
nsresult WillOutdent(Selection& aSelection, bool* aCancel, bool* aHandled);
nsresult WillAlign(Selection& aSelection, const nsAString& aAlignType,
bool* aCancel, bool* aHandled);
nsresult WillAbsolutePosition(Selection& aSelection, bool* aCancel,
bool* aHandled);
nsresult WillRemoveAbsolutePosition(Selection* aSelection, bool* aCancel,
bool* aHandled);
nsresult WillRelativeChangeZIndex(Selection* aSelection, int32_t aChange,
bool* aCancel, bool* aHandled);
nsresult WillMakeDefListItem(Selection* aSelection,
const nsAString* aBlockType, bool aEntireList,
bool* aCancel, bool* aHandled);
nsresult WillMakeBasicBlock(Selection& aSelection,
const nsAString& aBlockType,
bool* aCancel, bool* aHandled);
nsresult MakeBasicBlock(Selection& aSelection, nsAtom& aBlockType);
nsresult DidMakeBasicBlock(Selection* aSelection, RulesInfo* aInfo,
nsresult aResult);
nsresult DidAbsolutePosition();
nsresult AlignInnerBlocks(nsINode& aNode, const nsAString* alignType);
nsresult AlignBlockContents(nsIDOMNode* aNode, const nsAString* alignType);
nsresult AppendInnerFormatNodes(nsTArray but also
, , , etc.
* The nodes count as being their own descendants for this purpose, so a
* table element is its own nearest table element ancestor.
*/
bool InDifferentTableElements(nsIDOMNode* aNode1, nsIDOMNode* aNode2);
bool InDifferentTableElements(nsINode* aNode1, nsINode* aNode2);
nsresult RemoveEmptyNodes();
nsresult SelectionEndpointInNode(nsINode* aNode, bool* aResult);
nsresult UpdateDocChangeRange(nsRange* aRange);
nsresult ConfirmSelectionInBody();
/**
* Insert normal
element into aNode when aNode is a block and it has
* no children.
*/
nsresult InsertBRIfNeeded(nsINode& aNode)
{
return InsertBRIfNeededInternal(aNode, false);
}
/**
* Insert moz-
element (
) into aNode when aNode is a
* block and it has no children.
*/
nsresult InsertMozBRIfNeeded(nsINode& aNode)
{
return InsertBRIfNeededInternal(aNode, true);
}
bool IsEmptyInline(nsINode& aNode);
bool ListIsEmptyLine(nsTArray