michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef __wsrunobject_h__ michael@0: #define __wsrunobject_h__ michael@0: michael@0: #include "nsCOMArray.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIDOMNode.h" michael@0: #include "nsIEditor.h" michael@0: #include "nsINode.h" michael@0: #include "nscore.h" michael@0: michael@0: class nsHTMLEditor; michael@0: class nsIDOMDocument; michael@0: class nsIDOMNode; michael@0: struct DOMPoint; michael@0: michael@0: // class nsWSRunObject represents the entire whitespace situation michael@0: // around a given point. It collects up a list of nodes that contain michael@0: // whitespace and categorizes in up to 3 different WSFragments (detailed michael@0: // below). Each WSFragment is a collection of whitespace that is michael@0: // either all insignificant, or that is significant. A WSFragment could michael@0: // consist of insignificant whitespace because it is after a block michael@0: // boundary or after a break. Or it could be insignificant because it michael@0: // is before a block. Or it could be significant because it is michael@0: // surrounded by text, or starts and ends with nbsps, etc. michael@0: michael@0: // Throughout I refer to LeadingWS, NormalWS, TrailingWS. LeadingWS & TrailingWS michael@0: // are runs of ascii ws that are insignificant (do not render) because they michael@0: // are adjacent to block boundaries, or after a break. NormalWS is ws that michael@0: // does cause soem rendering. Note that not all the ws in a NormalWS run need michael@0: // render. For example, two ascii spaces surrounded by text on both sides michael@0: // will only render as one space (in non-preformatted stlye html), yet both michael@0: // spaces count as NormalWS. Together, they render as the one visible space. michael@0: michael@0: /** michael@0: * A type-safe bitfield indicating various types of whitespace or other things. michael@0: * Used as a member variable in nsWSRunObject and WSFragment. michael@0: * michael@0: * XXX: If this idea is useful in other places, we should generalize it using a michael@0: * template. michael@0: */ michael@0: class WSType { michael@0: public: michael@0: enum Enum { michael@0: none = 0, michael@0: leadingWS = 1, // leading insignificant ws, ie, after block or br michael@0: trailingWS = 1 << 1, // trailing insignificant ws, ie, before block michael@0: normalWS = 1 << 2, // normal significant ws, ie, after text, image, ... michael@0: text = 1 << 3, // indicates regular (non-ws) text michael@0: special = 1 << 4, // indicates an inline non-container, like image michael@0: br = 1 << 5, // indicates a br node michael@0: otherBlock = 1 << 6, // indicates a block other than one ws run is in michael@0: thisBlock = 1 << 7, // indicates the block ws run is in michael@0: block = otherBlock | thisBlock // block found michael@0: }; michael@0: michael@0: /** michael@0: * Implicit constructor, because the enums are logically just WSTypes michael@0: * themselves, and are only a separate type because there's no other obvious michael@0: * way to name specific WSType values. michael@0: */ michael@0: WSType(const Enum& aEnum = none) : mEnum(aEnum) {} michael@0: // operator==, &, and | need to access mEnum michael@0: friend bool operator==(const WSType& aLeft, const WSType& aRight); michael@0: friend const WSType operator&(const WSType& aLeft, const WSType& aRight); michael@0: friend const WSType operator|(const WSType& aLeft, const WSType& aRight); michael@0: WSType& operator=(const WSType& aOther) { michael@0: // This handles self-assignment fine michael@0: mEnum = aOther.mEnum; michael@0: return *this; michael@0: } michael@0: WSType& operator&=(const WSType& aOther) { michael@0: mEnum &= aOther.mEnum; michael@0: return *this; michael@0: } michael@0: WSType& operator|=(const WSType& aOther) { michael@0: mEnum |= aOther.mEnum; michael@0: return *this; michael@0: } michael@0: private: michael@0: uint16_t mEnum; michael@0: void bool_conversion_helper() {} michael@0: public: michael@0: // Allow boolean conversion with no numeric conversion michael@0: typedef void (WSType::*bool_type)(); michael@0: operator bool_type() const michael@0: { michael@0: return mEnum ? &WSType::bool_conversion_helper : nullptr; michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * These are declared as global functions so "WSType::Enum == WSType" et al. michael@0: * will work using the implicit constructor. michael@0: */ michael@0: inline bool operator==(const WSType& aLeft, const WSType& aRight) michael@0: { michael@0: return aLeft.mEnum == aRight.mEnum; michael@0: } michael@0: inline bool operator!=(const WSType& aLeft, const WSType& aRight) michael@0: { michael@0: return !(aLeft == aRight); michael@0: } michael@0: inline const WSType operator&(const WSType& aLeft, const WSType& aRight) michael@0: { michael@0: WSType ret; michael@0: ret.mEnum = aLeft.mEnum & aRight.mEnum; michael@0: return ret; michael@0: } michael@0: inline const WSType operator|(const WSType& aLeft, const WSType& aRight) michael@0: { michael@0: WSType ret; michael@0: ret.mEnum = aLeft.mEnum | aRight.mEnum; michael@0: return ret; michael@0: } michael@0: michael@0: /** michael@0: * Make sure that & and | of WSType::Enum creates a WSType instead of an int, michael@0: * because operators between WSType and int shouldn't work michael@0: */ michael@0: inline const WSType operator&(const WSType::Enum& aLeft, michael@0: const WSType::Enum& aRight) michael@0: { michael@0: return WSType(aLeft) & WSType(aRight); michael@0: } michael@0: inline const WSType operator|(const WSType::Enum& aLeft, michael@0: const WSType::Enum& aRight) michael@0: { michael@0: return WSType(aLeft) | WSType(aRight); michael@0: } michael@0: michael@0: michael@0: class MOZ_STACK_CLASS nsWSRunObject michael@0: { michael@0: public: michael@0: michael@0: // public enums --------------------------------------------------------- michael@0: enum BlockBoundary michael@0: { michael@0: kBeforeBlock, michael@0: kBlockStart, michael@0: kBlockEnd, michael@0: kAfterBlock michael@0: }; michael@0: michael@0: enum {eBefore = 1}; michael@0: enum {eAfter = 1 << 1}; michael@0: enum {eBoth = eBefore | eAfter}; michael@0: michael@0: // constructor / destructor ----------------------------------------------- michael@0: nsWSRunObject(nsHTMLEditor *aEd, nsIDOMNode *aNode, int32_t aOffset); michael@0: ~nsWSRunObject(); michael@0: michael@0: // public methods --------------------------------------------------------- michael@0: michael@0: // ScrubBlockBoundary removes any non-visible whitespace at the specified michael@0: // location relative to a block node. michael@0: static nsresult ScrubBlockBoundary(nsHTMLEditor *aHTMLEd, michael@0: nsCOMPtr *aBlock, michael@0: BlockBoundary aBoundary, michael@0: int32_t *aOffset = 0); michael@0: michael@0: // PrepareToJoinBlocks fixes up ws at the end of aLeftParent and the michael@0: // beginning of aRightParent in preperation for them to be joined. michael@0: // example of fixup: trailingws in aLeftParent needs to be removed. michael@0: static nsresult PrepareToJoinBlocks(nsHTMLEditor *aEd, michael@0: nsIDOMNode *aLeftParent, michael@0: nsIDOMNode *aRightParent); michael@0: michael@0: // PrepareToDeleteRange fixes up ws before {aStartNode,aStartOffset} michael@0: // and after {aEndNode,aEndOffset} in preperation for content michael@0: // in that range to be deleted. Note that the nodes and offsets michael@0: // are adjusted in response to any dom changes we make while michael@0: // adjusting ws. michael@0: // example of fixup: trailingws before {aStartNode,aStartOffset} michael@0: // needs to be removed. michael@0: static nsresult PrepareToDeleteRange(nsHTMLEditor *aHTMLEd, michael@0: nsCOMPtr *aStartNode, michael@0: int32_t *aStartOffset, michael@0: nsCOMPtr *aEndNode, michael@0: int32_t *aEndOffset); michael@0: michael@0: // PrepareToDeleteNode fixes up ws before and after aNode in preperation michael@0: // for aNode to be deleted. michael@0: // example of fixup: trailingws before aNode needs to be removed. michael@0: static nsresult PrepareToDeleteNode(nsHTMLEditor *aHTMLEd, michael@0: nsIDOMNode *aNode); michael@0: michael@0: // PrepareToSplitAcrossBlocks fixes up ws before and after michael@0: // {aSplitNode,aSplitOffset} in preperation for a block michael@0: // parent to be split. Note that the aSplitNode and aSplitOffset michael@0: // are adjusted in response to any dom changes we make while michael@0: // adjusting ws. michael@0: // example of fixup: normalws before {aSplitNode,aSplitOffset} michael@0: // needs to end with nbsp. michael@0: static nsresult PrepareToSplitAcrossBlocks(nsHTMLEditor *aHTMLEd, michael@0: nsCOMPtr *aSplitNode, michael@0: int32_t *aSplitOffset); michael@0: michael@0: // InsertBreak inserts a br node at {aInOutParent,aInOutOffset} michael@0: // and makes any needed adjustments to ws around that point. michael@0: // example of fixup: normalws after {aInOutParent,aInOutOffset} michael@0: // needs to begin with nbsp. michael@0: nsresult InsertBreak(nsCOMPtr *aInOutParent, michael@0: int32_t *aInOutOffset, michael@0: nsCOMPtr *outBRNode, michael@0: nsIEditor::EDirection aSelect); michael@0: michael@0: // InsertText inserts a string at {aInOutParent,aInOutOffset} michael@0: // and makes any needed adjustments to ws around that point. michael@0: // example of fixup: trailingws before {aInOutParent,aInOutOffset} michael@0: // needs to be removed. michael@0: nsresult InsertText(const nsAString& aStringToInsert, michael@0: nsCOMPtr *aInOutNode, michael@0: int32_t *aInOutOffset, michael@0: nsIDOMDocument *aDoc); michael@0: michael@0: // DeleteWSBackward deletes a single visible piece of ws before michael@0: // the ws point (the point to create the wsRunObject, passed to michael@0: // its constructor). It makes any needed conversion to adjacent michael@0: // ws to retain its significance. michael@0: nsresult DeleteWSBackward(); michael@0: michael@0: // DeleteWSForward deletes a single visible piece of ws after michael@0: // the ws point (the point to create the wsRunObject, passed to michael@0: // its constructor). It makes any needed conversion to adjacent michael@0: // ws to retain its significance. michael@0: nsresult DeleteWSForward(); michael@0: michael@0: // PriorVisibleNode returns the first piece of visible thing michael@0: // before {aNode,aOffset}. If there is no visible ws qualifying michael@0: // it returns what is before the ws run. Note that michael@0: // {outVisNode,outVisOffset} is set to just AFTER the visible michael@0: // object. michael@0: void PriorVisibleNode(nsIDOMNode *aNode, michael@0: int32_t aOffset, michael@0: nsCOMPtr *outVisNode, michael@0: int32_t *outVisOffset, michael@0: WSType *outType); michael@0: michael@0: // NextVisibleNode returns the first piece of visible thing michael@0: // after {aNode,aOffset}. If there is no visible ws qualifying michael@0: // it returns what is after the ws run. Note that michael@0: // {outVisNode,outVisOffset} is set to just BEFORE the visible michael@0: // object. michael@0: void NextVisibleNode(nsIDOMNode *aNode, michael@0: int32_t aOffset, michael@0: nsCOMPtr *outVisNode, michael@0: int32_t *outVisOffset, michael@0: WSType *outType); michael@0: michael@0: // AdjustWhitespace examines the ws object for nbsp's that can michael@0: // be safely converted to regular ascii space and converts them. michael@0: nsresult AdjustWhitespace(); michael@0: michael@0: protected: michael@0: michael@0: // WSFragment struct --------------------------------------------------------- michael@0: // WSFragment represents a single run of ws (all leadingws, or all normalws, michael@0: // or all trailingws, or all leading+trailingws). Note that this single run may michael@0: // still span multiple nodes. michael@0: struct WSFragment michael@0: { michael@0: nsCOMPtr mStartNode; // node where ws run starts michael@0: nsCOMPtr mEndNode; // node where ws run ends michael@0: int32_t mStartOffset; // offset where ws run starts michael@0: int32_t mEndOffset; // offset where ws run ends michael@0: // type of ws, and what is to left and right of it michael@0: WSType mType, mLeftType, mRightType; michael@0: // other ws runs to left or right. may be null. michael@0: WSFragment *mLeft, *mRight; michael@0: michael@0: WSFragment() : mStartNode(0), mEndNode(0), michael@0: mStartOffset(0), mEndOffset(0), michael@0: mType(), mLeftType(), mRightType(), michael@0: mLeft(0), mRight(0) michael@0: { michael@0: } michael@0: }; michael@0: michael@0: // WSPoint struct ------------------------------------------------------------ michael@0: // A WSPoint struct represents a unique location within the ws run. It is michael@0: // always within a textnode that is one of the nodes stored in the list michael@0: // in the wsRunObject. For convenience, the character at that point is also michael@0: // stored in the struct. michael@0: struct MOZ_STACK_CLASS WSPoint michael@0: { michael@0: nsCOMPtr mTextNode; michael@0: uint32_t mOffset; michael@0: char16_t mChar; michael@0: michael@0: WSPoint() : mTextNode(0),mOffset(0),mChar(0) {} michael@0: WSPoint(nsIDOMNode *aNode, int32_t aOffset, char16_t aChar) : michael@0: mTextNode(do_QueryInterface(aNode)),mOffset(aOffset),mChar(aChar) michael@0: { michael@0: if (!mTextNode->IsNodeOfType(nsINode::eDATA_NODE)) { michael@0: // Not sure if this is needed, but it'll maintain the same michael@0: // functionality michael@0: mTextNode = nullptr; michael@0: } michael@0: } michael@0: WSPoint(nsIContent *aTextNode, int32_t aOffset, char16_t aChar) : michael@0: mTextNode(aTextNode),mOffset(aOffset),mChar(aChar) {} michael@0: }; michael@0: michael@0: enum AreaRestriction michael@0: { michael@0: eAnywhere, eOutsideUserSelectAll michael@0: }; michael@0: michael@0: // protected methods --------------------------------------------------------- michael@0: // tons of utility methods. michael@0: michael@0: /** michael@0: * Return the node which we will handle white-space under. This is the michael@0: * closest block within the DOM subtree we're editing, or if none is michael@0: * found, the (inline) root of the editable subtree. michael@0: */ michael@0: already_AddRefed GetWSBoundingParent(); michael@0: michael@0: nsresult GetWSNodes(); michael@0: void GetRuns(); michael@0: void ClearRuns(); michael@0: void MakeSingleWSRun(WSType aType); michael@0: nsresult PrependNodeToList(nsIDOMNode *aNode); michael@0: nsresult AppendNodeToList(nsIDOMNode *aNode); michael@0: nsresult GetPreviousWSNode(nsIDOMNode *aStartNode, michael@0: nsIDOMNode *aBlockParent, michael@0: nsCOMPtr *aPriorNode); michael@0: nsresult GetPreviousWSNode(nsIDOMNode *aStartNode, michael@0: int32_t aOffset, michael@0: nsIDOMNode *aBlockParent, michael@0: nsCOMPtr *aPriorNode); michael@0: nsresult GetPreviousWSNode(::DOMPoint aPoint, michael@0: nsIDOMNode *aBlockParent, michael@0: nsCOMPtr *aPriorNode); michael@0: nsresult GetNextWSNode(nsIDOMNode *aStartNode, michael@0: nsIDOMNode *aBlockParent, michael@0: nsCOMPtr *aNextNode); michael@0: nsresult GetNextWSNode(nsIDOMNode *aStartNode, michael@0: int32_t aOffset, michael@0: nsIDOMNode *aBlockParent, michael@0: nsCOMPtr *aNextNode); michael@0: nsresult GetNextWSNode(::DOMPoint aPoint, michael@0: nsIDOMNode *aBlockParent, michael@0: nsCOMPtr *aNextNode); michael@0: nsresult PrepareToDeleteRangePriv(nsWSRunObject* aEndObject); michael@0: nsresult PrepareToSplitAcrossBlocksPriv(); michael@0: nsresult DeleteChars(nsIDOMNode *aStartNode, int32_t aStartOffset, michael@0: nsIDOMNode *aEndNode, int32_t aEndOffset, michael@0: AreaRestriction aAR = eAnywhere); michael@0: WSPoint GetCharAfter(nsIDOMNode *aNode, int32_t aOffset); michael@0: WSPoint GetCharBefore(nsIDOMNode *aNode, int32_t aOffset); michael@0: WSPoint GetCharAfter(const WSPoint &aPoint); michael@0: WSPoint GetCharBefore(const WSPoint &aPoint); michael@0: nsresult ConvertToNBSP(WSPoint aPoint, michael@0: AreaRestriction aAR = eAnywhere); michael@0: void GetAsciiWSBounds(int16_t aDir, nsIDOMNode *aNode, int32_t aOffset, michael@0: nsCOMPtr *outStartNode, int32_t *outStartOffset, michael@0: nsCOMPtr *outEndNode, int32_t *outEndOffset); michael@0: void FindRun(nsIDOMNode *aNode, int32_t aOffset, WSFragment **outRun, bool after); michael@0: char16_t GetCharAt(nsIContent *aTextNode, int32_t aOffset); michael@0: WSPoint GetWSPointAfter(nsIDOMNode *aNode, int32_t aOffset); michael@0: WSPoint GetWSPointBefore(nsIDOMNode *aNode, int32_t aOffset); michael@0: nsresult CheckTrailingNBSPOfRun(WSFragment *aRun); michael@0: nsresult CheckTrailingNBSP(WSFragment *aRun, nsIDOMNode *aNode, int32_t aOffset); michael@0: nsresult CheckLeadingNBSP(WSFragment *aRun, nsIDOMNode *aNode, int32_t aOffset); michael@0: michael@0: static nsresult ScrubBlockBoundaryInner(nsHTMLEditor *aHTMLEd, michael@0: nsCOMPtr *aBlock, michael@0: BlockBoundary aBoundary); michael@0: nsresult Scrub(); michael@0: michael@0: // member variables --------------------------------------------------------- michael@0: michael@0: nsCOMPtr mNode; // the node passed to our constructor michael@0: int32_t mOffset; // the offset passed to our contructor michael@0: // together, the above represent the point at which we are building up ws info. michael@0: michael@0: bool mPRE; // true if we are in preformatted whitespace context michael@0: nsCOMPtr mStartNode; // node/offset where ws starts michael@0: int32_t mStartOffset; // ... michael@0: WSType mStartReason; // reason why ws starts (eText, eOtherBlock, etc) michael@0: nsCOMPtr mStartReasonNode;// the node that implicated by start reason michael@0: michael@0: nsCOMPtr mEndNode; // node/offset where ws ends michael@0: int32_t mEndOffset; // ... michael@0: WSType mEndReason; // reason why ws ends (eText, eOtherBlock, etc) michael@0: nsCOMPtr mEndReasonNode; // the node that implicated by end reason michael@0: michael@0: nsCOMPtr mFirstNBSPNode; // location of first nbsp in ws run, if any michael@0: int32_t mFirstNBSPOffset; // ... michael@0: michael@0: nsCOMPtr mLastNBSPNode; // location of last nbsp in ws run, if any michael@0: int32_t mLastNBSPOffset; // ... michael@0: michael@0: nsCOMArray mNodeArray;//the list of nodes containing ws in this run michael@0: michael@0: WSFragment *mStartRun; // the first WSFragment in the run michael@0: WSFragment *mEndRun; // the last WSFragment in the run, may be same as first michael@0: michael@0: nsHTMLEditor *mHTMLEditor; // non-owning. michael@0: michael@0: friend class nsHTMLEditRules; // opening this class up for pillaging michael@0: friend class nsHTMLEditor; // opening this class up for more pillaging michael@0: }; michael@0: michael@0: #endif michael@0: