gecko-dev/editor/libeditor/HTMLEditRules.h

852 строки
34 KiB
C
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 15:12:37 +04:00
/* 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/. */
1999-03-29 10:21:01 +04:00
#ifndef HTMLEditRules_h
#define HTMLEditRules_h
1999-03-29 10:21:01 +04:00
#include "TypeInState.h"
#include "mozilla/EditorDOMPoint.h" // for EditorDOMPoint
#include "mozilla/SelectionState.h"
#include "mozilla/TextEditRules.h"
#include "nsCOMPtr.h"
#include "nsIEditor.h"
#include "nsIHTMLEditor.h"
#include "nsISupportsImpl.h"
#include "nsTArray.h"
#include "nscore.h"
1999-03-29 10:21:01 +04:00
class nsAtom;
class nsIEditor;
class nsINode;
class nsRange;
namespace mozilla {
class EditActionResult;
Bug 1260651 part.56 Rename nsHTMLEditor to mozilla::HTMLEditor and related stuff r=mccr8 This patch renames classes/structs as: nsHTMLEditor -> mozilla::HTMLEditor nsHTMLEditor::BlobReader -> mozilla::HTMLEditor::BlobReader SetSelectionAfterTableEdit -> mozilla::AutoSelectionSetterAfterTableEdit nsHTMLEditor.h -> HTMLEditor.h (exposed as mozilla/editor/HTMLEditor.h) nsHTMLAbsPosition.cpp -> HTMLAbsPositionEditor.cpp nsHTMLAnonymousUtils.cpp -> HTMLAnonymousNodeEditor.cpp nsHTMLDataTransfer.cpp -> HTMLEditorDataTransfer.cpp nsHTMLEditorStyle.cpp -> HTMLStyleEditor.cpp nsHTMLInlineTableEditor.cpp -> HTMLInlineTableEditor.cpp nsHTMLObjectResizer.cpp -> HTMLEditorObjectResizer.cpp nsTableEditor.cpp -> HTMLTableEditor.cpp These new file names are clearer names which related to HTMLEditor than old names. MozReview-Commit-ID: DTWaoFvy0DF --HG-- rename : editor/libeditor/nsHTMLAbsPosition.cpp => editor/libeditor/HTMLAbsPositionEditor.cpp rename : editor/libeditor/nsHTMLAnonymousUtils.cpp => editor/libeditor/HTMLAnonymousNodeEditor.cpp rename : editor/libeditor/nsHTMLEditor.cpp => editor/libeditor/HTMLEditor.cpp rename : editor/libeditor/nsHTMLEditor.h => editor/libeditor/HTMLEditor.h rename : editor/libeditor/nsHTMLDataTransfer.cpp => editor/libeditor/HTMLEditorDataTransfer.cpp rename : editor/libeditor/nsHTMLObjectResizer.cpp => editor/libeditor/HTMLEditorObjectResizer.cpp rename : editor/libeditor/nsHTMLInlineTableEditor.cpp => editor/libeditor/HTMLInlineTableEditor.cpp rename : editor/libeditor/nsHTMLEditorStyle.cpp => editor/libeditor/HTMLStyleEditor.cpp rename : editor/libeditor/nsTableEditor.cpp => editor/libeditor/HTMLTableEditor.cpp
2016-07-09 05:42:33 +03:00
class HTMLEditor;
class RulesInfo;
class SplitNodeResult;
class TextEditor;
enum class EditAction : int32_t;
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,
nsAtom* aAttr,
const nsAString& aValue)
: PropItem(aTag, aAttr, aValue)
, mPresent(false)
{
MOZ_COUNT_CTOR(StyleCache);
}
StyleCache(nsAtom* aTag,
nsAtom* 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
1999-03-29 10:21:01 +04:00
{
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLEditRules, TextEditRules)
1999-03-29 10:21:01 +04:00
HTMLEditRules();
// TextEditRules methods
virtual nsresult Init(TextEditor* aTextEditor) override;
virtual nsresult DetachEditor() override;
virtual nsresult BeforeEdit(EditAction aAction,
nsIEditor::EDirection aDirection) override;
virtual nsresult AfterEdit(EditAction aAction,
nsIEditor::EDirection aDirection) override;
virtual nsresult WillDoAction(Selection* aSelection,
RulesInfo* aInfo,
bool* aCancel,
bool* aHandled) override;
virtual nsresult DidDoAction(Selection* aSelection,
RulesInfo* aInfo,
nsresult aResult) override;
virtual bool DocumentIsEmpty() override;
virtual nsresult DocumentModified() override;
1999-04-05 21:21:59 +04:00
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);
/**
* MakeSureElemStartsAndEndsOnCR() inserts <br> element at start (and/or end)
* of aNode if neither:
* - first (last) editable child of aNode is a block or a <br>,
* - previous (next) sibling of aNode is block or a <br>
* - nor no previous (next) sibling of aNode.
*
* @param aNode The node which may be inserted <br> elements.
*/
MOZ_MUST_USE nsresult MakeSureElemStartsAndEndsOnCR(nsINode& aNode);
void DidCreateNode(Selection& aSelection, Element& aNewElement);
void DidInsertNode(Selection& aSelection, nsIContent& aNode);
void WillDeleteNode(Selection& aSelection, nsINode& aChild);
void DidSplitNode(Selection& aSelection,
nsINode& aExistingRightNode,
nsINode& aNewLeftNode);
void WillJoinNodes(nsINode& aLeftNode, nsINode& aRightNode);
void DidJoinNodes(Selection& aSelection,
nsINode& aLeftNode, nsINode& aRightNode);
void DidInsertText(Selection& aSelection,
nsINode& aTextNode, int32_t aOffset,
const nsAString& aString);
void DidDeleteText(Selection& aSelection,
nsINode& aTextNode, int32_t aOffset, int32_t aLength);
void WillDeleteSelection(Selection& aSelection);
void StartToListenToEditActions() { mListenerEnabled = true; }
void EndListeningToEditActions() { mListenerEnabled = false; }
1999-04-05 21:21:59 +04:00
protected:
virtual ~HTMLEditRules();
1999-04-05 21:21:59 +04:00
HTMLEditor& HTMLEditorRef() const
{
MOZ_ASSERT(mData);
return mData->HTMLEditorRef();
}
enum RulesEndpoint
{
kStart,
kEnd
};
void InitFields();
void WillInsert(bool* aCancel);
nsresult WillInsertText(EditAction aAction,
bool* aCancel,
bool* aHandled,
const nsAString* inString,
nsAString* outString,
int32_t aMaxLength);
nsresult WillLoadHTML(bool* aCancel);
nsresult WillInsertBreak(bool* aCancel, bool* aHandled);
void DeleteNodeIfCollapsedText(nsINode& aNode);
/**
* InsertBRElement() inserts a <br> element into aInsertToBreak.
*
* @param aInsertToBreak The point where new <br> element will be
* inserted before.
*/
nsresult InsertBRElement(const EditorDOMPoint& aInsertToBreak);
nsresult SplitMailCites(bool* aHandled);
nsresult WillDeleteSelection(nsIEditor::EDirection aAction,
nsIEditor::EStripWrappers aStripWrappers,
bool* aCancel, bool* aHandled);
nsresult DidDeleteSelection(nsIEditor::EDirection aDir,
nsresult aResult);
nsresult InsertBRIfNeeded();
/**
* CanContainParagraph() returns true if aElement can have a <p> element as
* its child or its descendant.
*/
bool CanContainParagraph(Element& aElement) const;
/**
* Insert normal <br> element into aNode when aNode is a block and it has
* no children.
*/
MOZ_MUST_USE nsresult InsertBRIfNeeded(nsINode& aNode)
{
return InsertBRIfNeededInternal(aNode, false);
}
/**
* Insert moz-<br> element (<br type="_moz">) into aNode when aNode is a
* block and it has no children.
*/
MOZ_MUST_USE nsresult InsertMozBRIfNeeded(nsINode& aNode)
{
return InsertBRIfNeededInternal(aNode, true);
}
/**
* Insert a normal <br> element or a moz-<br> element to aNode when
* aNode is a block and it has no children. Use InsertBRIfNeeded() or
* InsertMozBRIfNeeded() instead.
*
* @param aNode Reference to a block parent.
* @param aInsertMozBR true if this should insert a moz-<br> element.
* Otherwise, i.e., this should insert a normal <br>
* element, false.
*/
MOZ_MUST_USE nsresult
InsertBRIfNeededInternal(nsINode& aNode, bool aInsertMozBR);
EditorDOMPoint GetGoodSelPointForNode(nsINode& aNode,
nsIEditor::EDirection aAction);
/**
* TryToJoinBlocksWithTransaction() 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,
* JoinEditableNodesWithTransaction() 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 TryToJoinBlocksWithTransaction(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 <br>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(const nsAString* aListType,
bool aEntireList,
const nsAString* aBulletType,
bool* aCancel, bool* aHandled,
const nsAString* aItemType = nullptr);
nsresult WillRemoveList(bool aOrdered, bool* aCancel, bool* aHandled);
nsresult WillIndent(bool* aCancel, bool* aHandled);
nsresult WillCSSIndent(bool* aCancel, bool* aHandled);
nsresult WillHTMLIndent(bool* aCancel, bool* aHandled);
nsresult WillOutdent(bool* aCancel, bool* aHandled);
nsresult WillAlign(const nsAString& aAlignType,
bool* aCancel, bool* aHandled);
/**
* Called before changing absolute positioned element to static positioned.
* This method actually changes the position property of nearest absolute
* positioned element. Therefore, this might cause destroying the HTML
* editor.
*
* @param aCancel Returns true if the operation is canceled.
* @param aHandled Returns true if the edit action is handled.
*/
MOZ_MUST_USE nsresult
WillRemoveAbsolutePosition(bool* aCancel, bool* aHandled);
/**
* Called before changing z-index.
* This method actually changes z-index of nearest absolute positioned
* element relatively. Therefore, this might cause destroying the HTML
* editor.
*
* @param aChange Amount to change z-index.
* @param aCancel Returns true if the operation is canceled.
* @param aHandled Returns true if the edit action is handled.
*/
MOZ_MUST_USE nsresult
WillRelativeChangeZIndex(int32_t aChange, bool* aCancel, bool* aHandled);
nsresult WillMakeDefListItem(const nsAString* aBlockType, bool aEntireList,
bool* aCancel, bool* aHandled);
nsresult WillMakeBasicBlock(const nsAString& aBlockType,
bool* aCancel, bool* aHandled);
nsresult MakeBasicBlock(nsAtom& aBlockType);
nsresult DidMakeBasicBlock(RulesInfo* aInfo, nsresult aResult);
/**
* Called before changing an element to absolute positioned.
* This method only prepares the operation since DidAbsolutePosition() will
* change it actually later. mNewBlock is set to the target element and
* if necessary, some ancestor nodes of selection may be split.
*
* @param aCancel Returns true if the operation is canceled.
* @param aHandled Returns true if the edit action is handled.
*/
MOZ_MUST_USE nsresult WillAbsolutePosition(bool* aCancel, bool* aHandled);
/**
* PrepareToMakeElementAbsolutePosition() is helper method of
* WillAbsolutePosition() since in some cases, needs to restore selection
* with AutoSelectionRestorer. So, all callers have to check if
* CanHandleEditAction() still returns true after a call of this method.
* XXX Should be documented outline of this method.
*
* @param aHandled Returns true if the edit action is handled.
* @param aTargetElement Returns target element which should be
* changed to absolute positioned.
*/
MOZ_MUST_USE nsresult
PrepareToMakeElementAbsolutePosition(bool* aHandled,
RefPtr<Element>* aTargetElement);
/**
* Called if nobody handles the edit action to make an element absolute
* positioned.
* This method actually changes the element which is computed by
* WillAbsolutePosition() to absolute positioned.
* Therefore, this might cause destroying the HTML editor.
*/
MOZ_MUST_USE nsresult DidAbsolutePosition();
nsresult AlignInnerBlocks(nsINode& aNode, const nsAString& aAlignType);
nsresult AlignBlockContents(nsINode& aNode, const nsAString& aAlignType);
nsresult AppendInnerFormatNodes(nsTArray<OwningNonNull<nsINode>>& aArray,
nsINode* aNode);
nsresult GetFormatString(nsINode* aNode, nsAString &outFormat);
enum class Lists { no, yes };
enum class Tables { no, yes };
void GetInnerContent(nsINode& aNode,
nsTArray<OwningNonNull<nsINode>>& aOutArrayOfNodes,
int32_t* aIndex, Lists aLists = Lists::yes,
Tables aTables = Tables::yes);
Element* IsInListItem(nsINode* aNode);
nsAtom& DefaultParagraphSeparator();
nsresult ReturnInHeader(Element& aHeader, nsINode& aNode, int32_t aOffset);
/**
* ReturnInParagraph() does the right thing for Enter key press or
* 'insertParagraph' command in aParentDivOrP. aParentDivOrP will be
* split at start of first selection range.
*
* @param aParentDivOrP The parent block. This must be <p> or <div>
* element.
* @return Returns with NS_OK if this doesn't meat any
* unexpected situation. If this method tries to
* split the paragraph, marked as handled.
*/
MOZ_MUST_USE EditActionResult ReturnInParagraph(Element& aParentDivOrP);
/**
* SplitParagraph() splits the parent block, aPara, at aSelNode - aOffset.
*
* @param aParentDivOrP The parent block to be split. This must be <p>
* or <div> element.
* @param aStartOfRightNode The point to be start of right node after
* split. This must be descendant of
* aParentDivOrP.
* @param aNextBRNode Next <br> node if there is. Otherwise, nullptr.
* If this is not nullptr, the <br> node may be
* removed.
*/
template<typename PT, typename CT>
MOZ_MUST_USE nsresult
SplitParagraph(Element& aParentDivOrP,
const EditorDOMPointBase<PT, CT>& aStartOfRightNode,
nsIContent* aBRNode);
/**
* ReturnInListItem() handles insertParagraph command (i.e., handling
* Enter key press) in a list item element.
*
* @param aListItem The list item which has the following point.
* @param aNode Typically, Selection start container, where to
* insert a break.
* @param aOffset Typically, Selection start offset in the
* start container, where to insert a break.
*/
MOZ_MUST_USE nsresult
ReturnInListItem(Element& aListItem, nsINode& aNode, int32_t aOffset);
nsresult AfterEditInner(EditAction action,
nsIEditor::EDirection aDirection);
nsresult RemovePartOfBlock(Element& aBlock, nsIContent& aStartChild,
nsIContent& aEndChild);
void SplitBlock(Element& aBlock,
nsIContent& aStartChild,
nsIContent& aEndChild,
nsIContent** aOutLeftNode = nullptr,
nsIContent** aOutRightNode = nullptr,
nsIContent** aOutMiddleNode = nullptr);
nsresult OutdentPartOfBlock(Element& aBlock,
nsIContent& aStartChild,
nsIContent& aEndChild,
bool aIsBlockIndentedWithCSS,
nsIContent** aOutLeftNode,
nsIContent** aOutRightNode);
already_AddRefed<Element> ConvertListType(Element* aList, nsAtom* aListType,
nsAtom* aItemType);
nsresult CreateStyleForInsertText(nsIDocument& aDoc);
/**
* IsEmptyBlockElement() returns true if aElement is a block level element
* and it doesn't have any visible content.
*/
enum class IgnoreSingleBR
{
eYes,
eNo
};
bool IsEmptyBlockElement(Element& aElement,
IgnoreSingleBR aIgnoreSingleBR);
nsresult CheckForEmptyBlock(nsINode* aStartNode, Element* aBodyNode,
nsIEditor::EDirection aAction, bool* aHandled);
enum class BRLocation { beforeBlock, blockEnd };
Element* CheckForInvisibleBR(Element& aBlock, BRLocation aWhere,
int32_t aOffset = 0);
nsresult ExpandSelectionForDeletion();
nsresult NormalizeSelection();
EditorDOMPoint GetPromotedPoint(RulesEndpoint aWhere, nsINode& aNode,
int32_t aOffset, EditAction actionID);
void GetPromotedRanges(nsTArray<RefPtr<nsRange>>& outArrayOfRanges,
EditAction inOperationType);
void PromoteRange(nsRange& aRange, EditAction inOperationType);
enum class TouchContent { no, yes };
nsresult GetNodesForOperation(
nsTArray<RefPtr<nsRange>>& aArrayOfRanges,
nsTArray<OwningNonNull<nsINode>>& aOutArrayOfNodes,
EditAction aOperationType,
TouchContent aTouchContent = TouchContent::yes);
void GetChildNodesForOperation(
nsINode& aNode,
nsTArray<OwningNonNull<nsINode>>& outArrayOfNodes);
nsresult GetNodesFromPoint(const EditorDOMPoint& aPoint,
EditAction aOperation,
nsTArray<OwningNonNull<nsINode>>& outArrayOfNodes,
TouchContent aTouchContent);
nsresult GetNodesFromSelection(
EditAction aOperation,
nsTArray<OwningNonNull<nsINode>>& outArrayOfNodes,
TouchContent aTouchContent = TouchContent::yes);
enum class EntireList { no, yes };
nsresult GetListActionNodes(
nsTArray<OwningNonNull<nsINode>>& aOutArrayOfNodes,
EntireList aEntireList,
TouchContent aTouchContent = TouchContent::yes);
void GetDefinitionListItemTypes(Element* aElement, bool* aDT, bool* aDD);
nsresult GetParagraphFormatNodes(
nsTArray<OwningNonNull<nsINode>>& outArrayOfNodes,
TouchContent aTouchContent = TouchContent::yes);
void LookInsideDivBQandList(nsTArray<OwningNonNull<nsINode>>& aNodeArray);
nsresult BustUpInlinesAtRangeEndpoints(RangeItem& inRange);
nsresult BustUpInlinesAtBRs(
nsIContent& aNode,
nsTArray<OwningNonNull<nsINode>>& aOutArrayOfNodes);
/**
* GetHiestInlineParent() returns the highest inline node parent between
* aNode and the editing host. Even if the editing host is an inline
* element, this method never returns the editing host as the result.
*/
nsIContent* GetHighestInlineParent(nsINode& aNode);
void MakeTransitionList(nsTArray<OwningNonNull<nsINode>>& aNodeArray,
nsTArray<bool>& aTransitionArray);
/**
* RemoveBlockStyle() removes all format blocks, table related element,
* etc in aNodeArray.
* If aNodeArray has a format node, it will be removed and its contents
* will be moved to where it was.
* If aNodeArray has a table related element, <li>, <blockquote> or <div>,
* it will removed and its contents will be moved to where it was.
*/
nsresult RemoveBlockStyle(nsTArray<OwningNonNull<nsINode>>& aNodeArray);
Bug 1406726 - HTMLEditRules::WillInsertBreak() should reset mNewNode with caret position r=m_kato HTMLEditRules::WillInsertBreak() started to use HTMLEditRules::MakeBasicBlock() to wrap existing inline elements with default paragraph separator if inline elements are children of editing host. However, HTMLEditRules::ApplyBlockStyle() called by HTMLEditRules::MakeBasicBlock() sets mNewNode to last new block node. So, if it wraps inline elements after caret position, mNewNode becomes after expected caret position and HTMLEditRules::AfterEditInner() will move caret into it. This patch make HTMLEditRules::WillInsertBreak() reset mNewNode with caret position after calling HTMLEditRules::MakeBasicBlock(). Additionally, this patch fixes a bug of HTMLEditor::IsVisibleBRElement(). That is, it uses only editable nodes to check if given <br> element is visible. However, editable state is not related to this check. If <br> element is followed by non-editable inline node (except invisible data nodes), it always visible. This bug caused getting wrong nodes with HTMLEditRules::GetNodesFromSelection() which is used by HTMLEditRules::MakeBasicBlock(). Therefore, this patch also adds lots of EditorBase::Get(Next|Previous)ElementOrText*() and HTMLEditor::Get(Next|Previous)HTMLElementOrText*() to ignore only invisible data nodes. Note that even with this fix, the range of nodes computed by HTMLEditRules::GetNodesFromSelection() is still odd if only non-editable elements follow a <br> node which is first <br> element after the caret position. However, what is expected by the execCommand spec is unclear. So, automated test added by this patch checks current inconsistent behavior for now. MozReview-Commit-ID: 2m52StwoEEH --HG-- extra : rebase_source : 6b9b2338e35c4d2e89a405fd8e1ffc7b0873ca1e
2018-02-13 13:01:42 +03:00
/**
* ApplyBlockStyle() formats all nodes in aNodeArray with block elements
* whose name is aBlockTag.
* If aNodeArray has an inline element, a block element is created and the
* inline element and following inline elements are moved into the new block
* element.
* If aNodeArray has <br> elements, they'll be removed from the DOM tree and
* new block element will be created when there are some remaining inline
* elements.
* If aNodeArray has a block element, this calls itself with children of
* the block element. Then, new block element will be created when there
* are some remaining inline elements.
*
* @param aNodeArray Must be descendants of a node.
* @param aBlockTag The element name of new block elements.
*/
MOZ_MUST_USE nsresult
ApplyBlockStyle(nsTArray<OwningNonNull<nsINode>>& aNodeArray,
nsAtom& aBlockTag);
Bug 1406726 - HTMLEditRules::WillInsertBreak() should reset mNewNode with caret position r=m_kato HTMLEditRules::WillInsertBreak() started to use HTMLEditRules::MakeBasicBlock() to wrap existing inline elements with default paragraph separator if inline elements are children of editing host. However, HTMLEditRules::ApplyBlockStyle() called by HTMLEditRules::MakeBasicBlock() sets mNewNode to last new block node. So, if it wraps inline elements after caret position, mNewNode becomes after expected caret position and HTMLEditRules::AfterEditInner() will move caret into it. This patch make HTMLEditRules::WillInsertBreak() reset mNewNode with caret position after calling HTMLEditRules::MakeBasicBlock(). Additionally, this patch fixes a bug of HTMLEditor::IsVisibleBRElement(). That is, it uses only editable nodes to check if given <br> element is visible. However, editable state is not related to this check. If <br> element is followed by non-editable inline node (except invisible data nodes), it always visible. This bug caused getting wrong nodes with HTMLEditRules::GetNodesFromSelection() which is used by HTMLEditRules::MakeBasicBlock(). Therefore, this patch also adds lots of EditorBase::Get(Next|Previous)ElementOrText*() and HTMLEditor::Get(Next|Previous)HTMLElementOrText*() to ignore only invisible data nodes. Note that even with this fix, the range of nodes computed by HTMLEditRules::GetNodesFromSelection() is still odd if only non-editable elements follow a <br> node which is first <br> element after the caret position. However, what is expected by the execCommand spec is unclear. So, automated test added by this patch checks current inconsistent behavior for now. MozReview-Commit-ID: 2m52StwoEEH --HG-- extra : rebase_source : 6b9b2338e35c4d2e89a405fd8e1ffc7b0873ca1e
2018-02-13 13:01:42 +03:00
/**
* MakeBlockquote() inserts at least one <blockquote> element and moves
* nodes in aNodeArray into new <blockquote> elements. If aNodeArray
* includes a table related element except <table>, this calls itself
* recursively to insert <blockquote> into the cell.
*
* @param aNodeArray Nodes which will be moved into created
* <blockquote> elements.
*/
MOZ_MUST_USE nsresult
MakeBlockquote(nsTArray<OwningNonNull<nsINode>>& aNodeArray);
/**
* MaybeSplitAncestorsForInsertWithTransaction() does nothing if container of
* aStartOfDeepestRightNode can have an element whose tag name is aTag.
* Otherwise, looks for an ancestor node which is or is in active editing
* host and can have an element whose name is aTag. If there is such
* ancestor, its descendants are split.
*
* Note that this may create empty elements while splitting ancestors.
*
* @param aTag The name of element to be inserted
* after calling this method.
* @param aStartOfDeepestRightNode The start point of deepest right node.
* This point must be descendant of
* active editing host.
* @return When succeeded, SplitPoint() returns
* the point to insert the element.
*/
template<typename PT, typename CT>
MOZ_MUST_USE SplitNodeResult
MaybeSplitAncestorsForInsertWithTransaction(
nsAtom& aTag, const EditorDOMPointBase<PT, CT>& aStartOfDeepestRightNode);
/**
* JoinNearestEditableNodesWithTransaction() joins two editable nodes which
* are themselves or the nearest editable node of aLeftNode and aRightNode.
* XXX This method's behavior is odd. For example, if user types Backspace
* key at the second editable paragraph in this case:
* <div contenteditable>
* <p>first editable paragraph</p>
* <p contenteditable="false">non-editable paragraph</p>
* <p>second editable paragraph</p>
* </div>
* The first editable paragraph's content will be moved into the second
* editable paragraph and the non-editable paragraph becomes the first
* paragraph of the editor. I don't think that it's expected behavior of
* any users...
*
* @param aLeftNode The node which will be removed.
* @param aRightNode The node which will be inserted the content of
* aLeftNode.
* @param aNewFirstChildOfRightNode
* The point at the first child of aRightNode.
*/
MOZ_MUST_USE nsresult
JoinNearestEditableNodesWithTransaction(
nsIContent& aLeftNode, nsIContent& aRightNode,
EditorDOMPoint* aNewFirstChildOfRightNode);
Element* GetTopEnclosingMailCite(nsINode& aNode);
/**
* PopListItem() tries to move aListItem outside its parent. If it's
* in a middle of a list element, the parent list element is split before
* aListItem. Then, moves aListItem to before its parent list element.
* I.e., moves aListItem between the 2 list elements if original parent
* was split. Then, if new parent is not a list element, the list item
* element is removed and its contents are moved to where the list item
* element was.
*
* @param aListItem Should be a <li>, <dt> or <dd> element.
* If it's not so, returns NS_ERROR_FAILURE.
* @param aOutOfList Returns true if the list item element is
* removed (i.e., unwrapped contents of
* aListItem). Otherwise, false.
*/
MOZ_MUST_USE nsresult
PopListItem(nsIContent& aListItem, bool* aOutOfList = nullptr);
/**
* RemoveListStructure() destroys the list structure of aListElement.
* If aListElement has <li>, <dl> or <dt> as a child, the element is removed
* but its descendants are moved to where the list item element was.
* If aListElement has another <ul>, <ol> or <dl> as a child, this method
* is called recursively.
* If aListElement has other nodes as its child, they are just removed.
* Finally, aListElement is removed. and its all children are moved to
* where the aListElement was.
*
* @param aListElement A <ul>, <ol> or <dl> element.
*/
MOZ_MUST_USE nsresult RemoveListStructure(Element& aListElement);
/**
* CacheInlineStyles() caches style of aNode into mCachedStyles.
* This may cause flushing layout at retrieving computed value of CSS
* properties.
*/
MOZ_MUST_USE nsresult CacheInlineStyles(nsINode* aNode);
/**
* ReapplyCachedStyles() restores some styles which are disappeared during
* handling edit action and it should be restored. This may cause flushing
* layout at retrieving computed value of CSS properties.
*/
MOZ_MUST_USE nsresult ReapplyCachedStyles();
void ClearCachedStyles();
/**
* InsertBRElementToEmptyListItemsAndTableCellsInChangedRange() inserts
* <br> element into empty list item or table cell elements.
*/
MOZ_MUST_USE nsresult
InsertBRElementToEmptyListItemsAndTableCellsInChangedRange();
/**
* AdjustWhitespace() may replace whitespaces with NBSP or something.
* See WSRunObject::AdjustWhitespace() for the detail.
*/
MOZ_MUST_USE nsresult AdjustWhitespace();
/**
* PinSelectionToNewBlock() may collapse Selection around mNewNode if it's
* necessary,
*/
MOZ_MUST_USE nsresult PinSelectionToNewBlock();
void CheckInterlinePosition();
/**
* AdjustSelection() may adjust Selection range to nearest editable content.
* Despite of the name, this may change the DOM tree. If it needs to create
* a <br> to put caret, this tries to create a <br> element.
*
* @param aAction Maybe used to look for a good point to put caret.
*/
MOZ_MUST_USE nsresult AdjustSelection(nsIEditor::EDirection aAction);
/**
* FindNearEditableNode() tries to find an editable node near aPoint.
*
* @param aPoint The DOM point where to start to search from.
* @param aDirection If nsIEditor::ePrevious is set, this searches an
* editable node from next nodes. Otherwise, from
* previous nodes.
* @return If found, returns non-nullptr. Otherwise, nullptr.
* Note that if found node is in different table element,
* this returns nullptr.
* And also if aDirection is not nsIEditor::ePrevious,
* the result may be the node pointed by aPoint.
*/
template<typename PT, typename CT>
nsIContent* FindNearEditableNode(const EditorDOMPointBase<PT, CT>& aPoint,
nsIEditor::EDirection aDirection);
/**
* Returns true if aNode1 or aNode2 or both is the descendant of some type of
* table element, but their nearest table element ancestors differ. "Table
* element" here includes not just <table> but also <td>, <tbody>, <tr>, 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(nsINode* aNode1, nsINode* aNode2);
/**
* RemoveEmptyNodesInChangedRange() removes all empty nodes in
* mDocChangeRange. However, if mail-cite node has only a <br> element,
* the node will be removed but <br> element is moved to where the
* mail-cite node was.
* XXX This method is expensive if mDocChangeRange is too wide and may
* remove unexpected empty element, e.g., it was created by JS, but
* we haven't touched it. Cannot we remove this method and make
* guarantee that empty nodes won't be created?
*/
MOZ_MUST_USE nsresult RemoveEmptyNodesInChangedRange();
nsresult SelectionEndpointInNode(nsINode* aNode, bool* aResult);
nsresult UpdateDocChangeRange(nsRange* aRange);
/**
* ConfirmSelectionInBody() makes sure that Selection is in editor root
* element typically <body> element (see HTMLEditor::UpdateRootElement())
* and only one Selection range.
* XXX This method is not necessary because even if selection is outside the
* <body> element, elements outside the <body> element should be
* editable, e.g., any element can be inserted siblings as <body> element
* and other browsers allow to edit such elements.
*/
MOZ_MUST_USE nsresult ConfirmSelectionInBody();
bool IsEmptyInline(nsINode& aNode);
bool ListIsEmptyLine(nsTArray<OwningNonNull<nsINode>>& arrayOfNodes);
/**
* RemoveAlignment() removes align attributes, text-align properties and
* <center> elements in aNode.
*
* @param aNode Alignment information of the node and/or its
* descendants will be removed.
* @param aAlignType New align value to be set only when it's in
* CSS mode and this method meets <table> or <hr>.
* XXX This is odd and not clear when you see
* caller of this method. Do you have better
* idea?
* @param aDescendantsOnly true if align information of aNode itself
* shouldn't be removed. Otherwise, false.
*/
MOZ_MUST_USE nsresult
RemoveAlignment(nsINode& aNode, const nsAString& aAlignType,
bool aDescendantsOnly);
/**
* MakeSureElemStartsOrEndsOnCR() inserts <br> element at start (end) of
* aNode if neither:
* - first (last) editable child of aNode is a block or a <br>,
* - previous (next) sibling of aNode is block or a <br>
* - nor no previous (next) sibling of aNode.
*
* @param aNode The node which may be inserted <br> element.
* @param aStarts true for trying to insert <br> to the start.
* false for trying to insert <br> to the end.
*/
MOZ_MUST_USE nsresult
MakeSureElemStartsOrEndsOnCR(nsINode& aNode, bool aStarts);
/**
* AlignBlock() resets align attribute, text-align property, etc first.
* Then, aligns contents of aElement on aAlignType.
*
* @param aElement The element whose contents will be aligned.
* @param aAlignType Boundary or "center" which contents should be
* aligned on.
* @param aResetAlignOf Resets align of whether element and its
* descendants or only descendants.
*/
enum class ResetAlignOf { ElementAndDescendants, OnlyDescendants };
MOZ_MUST_USE nsresult
AlignBlock(Element& aElement, const nsAString& aAlignType,
ResetAlignOf aResetAlignOf);
/**
* IncreaseMarginToIndent() increases the margin of aElement. See the
* document of ChangeMarginStart() for the detail.
* XXX This is not aware of vertical writing-mode.
*
* @param aElement The element to be indented.
*/
MOZ_MUST_USE nsresult IncreaseMarginToIndent(Element& aElement)
{
return ChangeMarginStart(aElement, true);
}
/**
* DecreaseMarginToOutdent() decreases the margin of aElement. See the
* document of ChangeMarginStart() for the detail.
* XXX This is not aware of vertical writing-mode.
*
* @param aElement The element to be outdented.
*/
MOZ_MUST_USE nsresult DecreaseMarginToOutdent(Element& aElement)
{
return ChangeMarginStart(aElement, false);
}
/**
* ChangeMarginStart() changes margin of aElement to indent or outdent.
* However, use IncreaseMarginToIndent() and DecreaseMarginToOutdent()
* instead. If it's rtl text, margin-right will be changed. Otherwise,
* margin-left.
* XXX This is not aware of vertical writing-mode.
*
* @param aElement The element to be indented or outdented.
* @param aIncrease true for indent, false for outdent.
*/
MOZ_MUST_USE nsresult ChangeMarginStart(Element& aElement, bool aIncrease);
void DocumentModifiedWorker();
/**
* InitStyleCacheArray() initializes aStyleCache for usable with
* GetInlineStyles().
*/
void InitStyleCacheArray(StyleCache aStyleCache[SIZE_STYLE_TABLE]);
/**
* GetInlineStyles() retrieves the style of aNode and modifies each item of
* aStyleCache. This might cause flushing layout at retrieving computed
* values of CSS properties.
*/
MOZ_MUST_USE nsresult
GetInlineStyles(nsINode* aNode, StyleCache aStyleCache[SIZE_STYLE_TABLE]);
protected:
Bug 1260651 part.56 Rename nsHTMLEditor to mozilla::HTMLEditor and related stuff r=mccr8 This patch renames classes/structs as: nsHTMLEditor -> mozilla::HTMLEditor nsHTMLEditor::BlobReader -> mozilla::HTMLEditor::BlobReader SetSelectionAfterTableEdit -> mozilla::AutoSelectionSetterAfterTableEdit nsHTMLEditor.h -> HTMLEditor.h (exposed as mozilla/editor/HTMLEditor.h) nsHTMLAbsPosition.cpp -> HTMLAbsPositionEditor.cpp nsHTMLAnonymousUtils.cpp -> HTMLAnonymousNodeEditor.cpp nsHTMLDataTransfer.cpp -> HTMLEditorDataTransfer.cpp nsHTMLEditorStyle.cpp -> HTMLStyleEditor.cpp nsHTMLInlineTableEditor.cpp -> HTMLInlineTableEditor.cpp nsHTMLObjectResizer.cpp -> HTMLEditorObjectResizer.cpp nsTableEditor.cpp -> HTMLTableEditor.cpp These new file names are clearer names which related to HTMLEditor than old names. MozReview-Commit-ID: DTWaoFvy0DF --HG-- rename : editor/libeditor/nsHTMLAbsPosition.cpp => editor/libeditor/HTMLAbsPositionEditor.cpp rename : editor/libeditor/nsHTMLAnonymousUtils.cpp => editor/libeditor/HTMLAnonymousNodeEditor.cpp rename : editor/libeditor/nsHTMLEditor.cpp => editor/libeditor/HTMLEditor.cpp rename : editor/libeditor/nsHTMLEditor.h => editor/libeditor/HTMLEditor.h rename : editor/libeditor/nsHTMLDataTransfer.cpp => editor/libeditor/HTMLEditorDataTransfer.cpp rename : editor/libeditor/nsHTMLObjectResizer.cpp => editor/libeditor/HTMLEditorObjectResizer.cpp rename : editor/libeditor/nsHTMLInlineTableEditor.cpp => editor/libeditor/HTMLInlineTableEditor.cpp rename : editor/libeditor/nsHTMLEditorStyle.cpp => editor/libeditor/HTMLStyleEditor.cpp rename : editor/libeditor/nsTableEditor.cpp => editor/libeditor/HTMLTableEditor.cpp
2016-07-09 05:42:33 +03:00
HTMLEditor* mHTMLEditor;
RefPtr<nsRange> mDocChangeRange;
bool mListenerEnabled;
bool mReturnInEmptyLIKillsList;
bool mDidDeleteSelection;
bool mDidRangedDelete;
bool mRestoreContentEditableCount;
RefPtr<nsRange> mUtilRange;
// Need to remember an int across willJoin/didJoin...
uint32_t mJoinOffset;
RefPtr<Element> mNewBlock;
RefPtr<RangeItem> mRangeItem;
// XXX In strict speaking, mCachedStyles isn't enough to cache inline styles
// because inline style can be specified with "style" attribute and/or
// CSS in <style> elements or CSS files. So, we need to look for better
// implementation about this.
StyleCache mCachedStyles[SIZE_STYLE_TABLE];
};
} // namespace mozilla
#endif // #ifndef HTMLEditRules_h
1999-03-29 10:21:01 +04:00