diff --git a/content/base/public/nsIDocumentEncoder.h b/content/base/public/nsIDocumentEncoder.h index 75e8e8231d30..66db12cb4ff8 100644 --- a/content/base/public/nsIDocumentEncoder.h +++ b/content/base/public/nsIDocumentEncoder.h @@ -32,6 +32,7 @@ class nsIDOMRange; class nsISelection; class nsIOutputStream; class nsISupportsArray; +class nsIDOMNode; #define NS_IDOCUMENT_ENCODER_IID \ @@ -56,9 +57,26 @@ class nsISupportsArray; #define NS_HTMLCOPY_TEXT_ENCODER_CID \ { 0x7f915b01, 0x98fc, 0x11d4, { 0x8e, 0xb0, 0xa8, 0x03, 0xf8, 0x0f, 0xf1, 0xbc } } - +// {0BC1FAC0-B710-11d4-959F-0020183BF181} +#define NS_IDOCUMENTENCODERNODEFIXUP_IID \ +{ 0xbc1fac0, 0xb710, 0x11d4, { 0x95, 0x9f, 0x0, 0x20, 0x18, 0x3b, 0xf1, 0x81 } } + #define NS_HTMLCOPY_ENCODER_CONTRACTID "@mozilla.org/layout/htmlCopyEncoder" +class nsIDocumentEncoderNodeFixup : public nsISupports +{ +public: + + NS_DEFINE_STATIC_IID_ACCESSOR(NS_IDOCUMENTENCODERNODEFIXUP_IID) + + /** + * Create a fixed up version of a node. This method is called before + * each node in a document is about to be persisted. The implementor + * may return a new node with fixed up attributes or nsnull. + */ + NS_IMETHOD FixupNode(nsIDOMNode *aNode, nsIDOMNode **aOutNode) = 0; +}; + class nsIDocumentEncoder : public nsISupports { public: @@ -172,6 +190,11 @@ public: NS_IMETHOD EncodeToStringWithContext(nsAWritableString& aEncodedString, nsAWritableString& aContextString, nsAWritableString& aInfoString) = 0; + + /** + * Set the fixup object associated with node persistence. + */ + NS_IMETHOD SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup) = 0; }; #endif /* nsIDocumentEncoder_h__ */ diff --git a/content/base/src/nsDocumentEncoder.cpp b/content/base/src/nsDocumentEncoder.cpp index e3df945ae8cd..9266d912843f 100644 --- a/content/base/src/nsDocumentEncoder.cpp +++ b/content/base/src/nsDocumentEncoder.cpp @@ -91,12 +91,12 @@ public: NS_IMETHOD SetRange(nsIDOMRange* aRange); NS_IMETHOD SetWrapColumn(PRUint32 aWC); NS_IMETHOD SetCharset(const nsAReadableString& aCharset); - NS_IMETHOD EncodeToStream(nsIOutputStream* aStream); NS_IMETHOD EncodeToString(nsAWritableString& aOutputString); NS_IMETHOD EncodeToStringWithContext(nsAWritableString& aEncodedString, nsAWritableString& aContextString, nsAWritableString& aInfoString); + NS_IMETHOD SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup); protected: nsresult SerializeNodeStart(nsIDOMNode* aNode, PRInt32 aStartOffset, @@ -129,6 +129,7 @@ protected: nsCOMPtr mSerializer; nsCOMPtr mUnicodeEncoder; nsCOMPtr mCommonParent; + nsCOMPtr mNodeFixup; nsString mMimeType; nsString mCharset; @@ -234,42 +235,52 @@ nsDocumentEncoder::SerializeNodeStart(nsIDOMNode* aNode, PRInt32 aStartOffset, { PRUint16 type; - aNode->GetNodeType(&type); + nsCOMPtr node; + if (mNodeFixup) + { + mNodeFixup->FixupNode(aNode, getter_AddRefs(node)); + } + if (!node) + { + node = do_QueryInterface(aNode); + } + + node->GetNodeType(&type); switch (type) { case nsIDOMNode::ELEMENT_NODE: { - nsCOMPtr element = do_QueryInterface(aNode); + nsCOMPtr element = do_QueryInterface(node); mSerializer->AppendElementStart(element, aStr); break; } case nsIDOMNode::TEXT_NODE: { - nsCOMPtr text = do_QueryInterface(aNode); + nsCOMPtr text = do_QueryInterface(node); mSerializer->AppendText(text, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::CDATA_SECTION_NODE: { - nsCOMPtr cdata = do_QueryInterface(aNode); + nsCOMPtr cdata = do_QueryInterface(node); mSerializer->AppendCDATASection(cdata, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: { - nsCOMPtr pi = do_QueryInterface(aNode); + nsCOMPtr pi = do_QueryInterface(node); mSerializer->AppendProcessingInstruction(pi, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::COMMENT_NODE: { - nsCOMPtr comment = do_QueryInterface(aNode); + nsCOMPtr comment = do_QueryInterface(node); mSerializer->AppendComment(comment, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::DOCUMENT_TYPE_NODE: { - nsCOMPtr doctype = do_QueryInterface(aNode); + nsCOMPtr doctype = do_QueryInterface(node); mSerializer->AppendDoctype(doctype, aStr); break; } @@ -922,6 +933,14 @@ nsDocumentEncoder::EncodeToStringWithContext(nsAWritableString& aEncodedString, return NS_ERROR_NOT_IMPLEMENTED; } +NS_IMETHODIMP +nsDocumentEncoder::SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup) +{ + mNodeFixup = aFixup; + return NS_OK; +} + + nsresult NS_NewTextEncoder(nsIDocumentEncoder** aResult); // make mac compiler happy nsresult diff --git a/layout/base/public/nsIDocumentEncoder.h b/layout/base/public/nsIDocumentEncoder.h index 75e8e8231d30..66db12cb4ff8 100644 --- a/layout/base/public/nsIDocumentEncoder.h +++ b/layout/base/public/nsIDocumentEncoder.h @@ -32,6 +32,7 @@ class nsIDOMRange; class nsISelection; class nsIOutputStream; class nsISupportsArray; +class nsIDOMNode; #define NS_IDOCUMENT_ENCODER_IID \ @@ -56,9 +57,26 @@ class nsISupportsArray; #define NS_HTMLCOPY_TEXT_ENCODER_CID \ { 0x7f915b01, 0x98fc, 0x11d4, { 0x8e, 0xb0, 0xa8, 0x03, 0xf8, 0x0f, 0xf1, 0xbc } } - +// {0BC1FAC0-B710-11d4-959F-0020183BF181} +#define NS_IDOCUMENTENCODERNODEFIXUP_IID \ +{ 0xbc1fac0, 0xb710, 0x11d4, { 0x95, 0x9f, 0x0, 0x20, 0x18, 0x3b, 0xf1, 0x81 } } + #define NS_HTMLCOPY_ENCODER_CONTRACTID "@mozilla.org/layout/htmlCopyEncoder" +class nsIDocumentEncoderNodeFixup : public nsISupports +{ +public: + + NS_DEFINE_STATIC_IID_ACCESSOR(NS_IDOCUMENTENCODERNODEFIXUP_IID) + + /** + * Create a fixed up version of a node. This method is called before + * each node in a document is about to be persisted. The implementor + * may return a new node with fixed up attributes or nsnull. + */ + NS_IMETHOD FixupNode(nsIDOMNode *aNode, nsIDOMNode **aOutNode) = 0; +}; + class nsIDocumentEncoder : public nsISupports { public: @@ -172,6 +190,11 @@ public: NS_IMETHOD EncodeToStringWithContext(nsAWritableString& aEncodedString, nsAWritableString& aContextString, nsAWritableString& aInfoString) = 0; + + /** + * Set the fixup object associated with node persistence. + */ + NS_IMETHOD SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup) = 0; }; #endif /* nsIDocumentEncoder_h__ */ diff --git a/layout/base/src/nsDocumentEncoder.cpp b/layout/base/src/nsDocumentEncoder.cpp index e3df945ae8cd..9266d912843f 100644 --- a/layout/base/src/nsDocumentEncoder.cpp +++ b/layout/base/src/nsDocumentEncoder.cpp @@ -91,12 +91,12 @@ public: NS_IMETHOD SetRange(nsIDOMRange* aRange); NS_IMETHOD SetWrapColumn(PRUint32 aWC); NS_IMETHOD SetCharset(const nsAReadableString& aCharset); - NS_IMETHOD EncodeToStream(nsIOutputStream* aStream); NS_IMETHOD EncodeToString(nsAWritableString& aOutputString); NS_IMETHOD EncodeToStringWithContext(nsAWritableString& aEncodedString, nsAWritableString& aContextString, nsAWritableString& aInfoString); + NS_IMETHOD SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup); protected: nsresult SerializeNodeStart(nsIDOMNode* aNode, PRInt32 aStartOffset, @@ -129,6 +129,7 @@ protected: nsCOMPtr mSerializer; nsCOMPtr mUnicodeEncoder; nsCOMPtr mCommonParent; + nsCOMPtr mNodeFixup; nsString mMimeType; nsString mCharset; @@ -234,42 +235,52 @@ nsDocumentEncoder::SerializeNodeStart(nsIDOMNode* aNode, PRInt32 aStartOffset, { PRUint16 type; - aNode->GetNodeType(&type); + nsCOMPtr node; + if (mNodeFixup) + { + mNodeFixup->FixupNode(aNode, getter_AddRefs(node)); + } + if (!node) + { + node = do_QueryInterface(aNode); + } + + node->GetNodeType(&type); switch (type) { case nsIDOMNode::ELEMENT_NODE: { - nsCOMPtr element = do_QueryInterface(aNode); + nsCOMPtr element = do_QueryInterface(node); mSerializer->AppendElementStart(element, aStr); break; } case nsIDOMNode::TEXT_NODE: { - nsCOMPtr text = do_QueryInterface(aNode); + nsCOMPtr text = do_QueryInterface(node); mSerializer->AppendText(text, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::CDATA_SECTION_NODE: { - nsCOMPtr cdata = do_QueryInterface(aNode); + nsCOMPtr cdata = do_QueryInterface(node); mSerializer->AppendCDATASection(cdata, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: { - nsCOMPtr pi = do_QueryInterface(aNode); + nsCOMPtr pi = do_QueryInterface(node); mSerializer->AppendProcessingInstruction(pi, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::COMMENT_NODE: { - nsCOMPtr comment = do_QueryInterface(aNode); + nsCOMPtr comment = do_QueryInterface(node); mSerializer->AppendComment(comment, aStartOffset, aEndOffset, aStr); break; } case nsIDOMNode::DOCUMENT_TYPE_NODE: { - nsCOMPtr doctype = do_QueryInterface(aNode); + nsCOMPtr doctype = do_QueryInterface(node); mSerializer->AppendDoctype(doctype, aStr); break; } @@ -922,6 +933,14 @@ nsDocumentEncoder::EncodeToStringWithContext(nsAWritableString& aEncodedString, return NS_ERROR_NOT_IMPLEMENTED; } +NS_IMETHODIMP +nsDocumentEncoder::SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup) +{ + mNodeFixup = aFixup; + return NS_OK; +} + + nsresult NS_NewTextEncoder(nsIDocumentEncoder** aResult); // make mac compiler happy nsresult