diff --git a/dom/base/CharacterData.cpp b/dom/base/CharacterData.cpp index 7e6f7d3fdf87..6a1739e7356a 100644 --- a/dom/base/CharacterData.cpp +++ b/dom/base/CharacterData.cpp @@ -191,15 +191,6 @@ CharacterData::GetLength(uint32_t* aLength) return NS_OK; } -nsresult -CharacterData::SubstringData(uint32_t aStart, uint32_t aCount, - nsAString& aReturn) -{ - ErrorResult rv; - SubstringData(aStart, aCount, aReturn, rv); - return rv.StealNSResult(); -} - void CharacterData::SubstringData(uint32_t aStart, uint32_t aCount, nsAString& aReturn, ErrorResult& rv) diff --git a/dom/base/CharacterData.h b/dom/base/CharacterData.h index 217573cfecf5..410403eb7df4 100644 --- a/dom/base/CharacterData.h +++ b/dom/base/CharacterData.h @@ -89,14 +89,14 @@ public: SetFlags(NS_MAYBE_MODIFIED_FREQUENTLY); } + NS_IMPL_FROMCONTENT_HELPER(CharacterData, IsCharacterData()) + virtual void GetNodeValueInternal(nsAString& aNodeValue) override; virtual void SetNodeValueInternal(const nsAString& aNodeValue, ErrorResult& aError) override; // Implementation for nsIDOMCharacterData nsresult GetLength(uint32_t* aLength); - nsresult SubstringData(uint32_t aOffset, uint32_t aCount, - nsAString& aReturn); nsresult AppendData(const nsAString& aArg); nsresult InsertData(uint32_t aOffset, const nsAString& aArg); nsresult DeleteData(uint32_t aOffset, uint32_t aCount); diff --git a/dom/base/nsDocumentEncoder.cpp b/dom/base/nsDocumentEncoder.cpp index 279e2942c00d..90d3b023059c 100644 --- a/dom/base/nsDocumentEncoder.cpp +++ b/dom/base/nsDocumentEncoder.cpp @@ -46,6 +46,7 @@ #include "mozilla/dom/Element.h" #include "mozilla/dom/ProcessingInstruction.h" #include "mozilla/dom/ShadowRoot.h" +#include "mozilla/dom/Text.h" #include "nsLayoutUtils.h" #include "mozilla/ScopeExit.h" @@ -1599,7 +1600,7 @@ nsHTMLCopyEncoder::GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, int32_t { // some special casing for text nodes nsCOMPtr t = do_QueryInterface(aNode); - if (IsTextNode(t)) + if (auto nodeAsText = t->GetAsText()) { // if not at beginning of text node, we are done if (offset > 0) @@ -1607,9 +1608,8 @@ nsHTMLCopyEncoder::GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, int32_t // unless everything before us in just whitespace. NOTE: we need a more // general solution that truly detects all cases of non-significant // whitesace with no false alarms. - nsCOMPtr nodeAsText = do_QueryInterface(aNode); nsAutoString text; - nodeAsText->SubstringData(0, offset, text); + nodeAsText->SubstringData(0, offset, text, IgnoreErrors()); text.CompressWhitespace(); if (!text.IsEmpty()) return NS_OK; @@ -1675,7 +1675,7 @@ nsHTMLCopyEncoder::GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, int32_t { // some special casing for text nodes nsCOMPtr n = do_QueryInterface(aNode); - if (IsTextNode(n)) + if (auto nodeAsText = n->GetAsText()) { // if not at end of text node, we are done uint32_t len = n->Length(); @@ -1684,9 +1684,8 @@ nsHTMLCopyEncoder::GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, int32_t // unless everything after us in just whitespace. NOTE: we need a more // general solution that truly detects all cases of non-significant // whitespace with no false alarms. - nsCOMPtr nodeAsText = do_QueryInterface(aNode); nsAutoString text; - nodeAsText->SubstringData(offset, len-offset, text); + nodeAsText->SubstringData(offset, len-offset, text, IgnoreErrors()); text.CompressWhitespace(); if (!text.IsEmpty()) return NS_OK; diff --git a/dom/base/nsIContent.h b/dom/base/nsIContent.h index 660b2cb0f0f1..e588ed1b2989 100644 --- a/dom/base/nsIContent.h +++ b/dom/base/nsIContent.h @@ -1009,19 +1009,19 @@ inline nsIContent* nsINode::AsContent() } #define NS_IMPL_FROMCONTENT_HELPER(_class, _check) \ - static _class* FromContent(nsIContent* aContent) \ + static _class* FromContent(nsINode* aContent) \ { \ return aContent->_check ? static_cast<_class*>(aContent) : nullptr; \ } \ - static const _class* FromContent(const nsIContent* aContent) \ + static const _class* FromContent(const nsINode* aContent) \ { \ return aContent->_check ? static_cast(aContent) : nullptr; \ } \ - static _class* FromContentOrNull(nsIContent* aContent) \ + static _class* FromContentOrNull(nsINode* aContent) \ { \ return aContent ? FromContent(aContent) : nullptr; \ } \ - static const _class* FromContentOrNull(const nsIContent* aContent) \ + static const _class* FromContentOrNull(const nsINode* aContent) \ { \ return aContent ? FromContent(aContent) : nullptr; \ } diff --git a/dom/base/nsINode.h b/dom/base/nsINode.h index 71e64258f074..8c4cab81712e 100644 --- a/dom/base/nsINode.h +++ b/dom/base/nsINode.h @@ -523,6 +523,19 @@ public: return NodeType() == PROCESSING_INSTRUCTION_NODE; } + /* + * Return whether the node is a CharacterData node (text, cdata, + * comment, processing instruction) + */ + bool IsCharacterData() const + { + uint32_t nodeType = NodeType(); + return nodeType == TEXT_NODE || + nodeType == CDATA_SECTION_NODE || + nodeType == PROCESSING_INSTRUCTION_NODE || + nodeType == COMMENT_NODE; + } + virtual nsIDOMNode* AsDOMNode() = 0; /** diff --git a/dom/base/nsRange.cpp b/dom/base/nsRange.cpp index dbebf4069e42..690ccf004a31 100644 --- a/dom/base/nsRange.cpp +++ b/dom/base/nsRange.cpp @@ -2289,9 +2289,7 @@ nsRange::CutContents(DocumentFragment** aFragment) // XXX_kin: We need to also handle ProcessingInstruction // XXX_kin: according to the spec. - nsCOMPtr charData(do_QueryInterface(node)); - - if (charData) + if (auto charData = CharacterData::FromContent(node)) { uint32_t dataLength = 0; @@ -2306,10 +2304,12 @@ nsRange::CutContents(DocumentFragment** aFragment) { if (retval) { nsAutoString cutValue; - rv = charData->SubstringData(startOffset, endOffset - startOffset, - cutValue); - NS_ENSURE_SUCCESS(rv, rv); ErrorResult err; + charData->SubstringData(startOffset, endOffset - startOffset, + cutValue, err); + if (NS_WARN_IF(err.Failed())) { + return err.StealNSResult(); + } nsCOMPtr clone = node->CloneNode(false, err); if (NS_WARN_IF(err.Failed())) { return err.StealNSResult(); @@ -2340,9 +2340,11 @@ nsRange::CutContents(DocumentFragment** aFragment) if (dataLength >= startOffset) { if (retval) { nsAutoString cutValue; - rv = charData->SubstringData(startOffset, dataLength, cutValue); - NS_ENSURE_SUCCESS(rv, rv); ErrorResult err; + charData->SubstringData(startOffset, dataLength, cutValue, err); + if (NS_WARN_IF(err.Failed())) { + return err.StealNSResult(); + } nsCOMPtr clone = node->CloneNode(false, err); if (NS_WARN_IF(err.Failed())) { return err.StealNSResult(); @@ -2369,9 +2371,11 @@ nsRange::CutContents(DocumentFragment** aFragment) // Delete or extract everything before endOffset. if (retval) { nsAutoString cutValue; - rv = charData->SubstringData(0, endOffset, cutValue); - NS_ENSURE_SUCCESS(rv, rv); ErrorResult err; + charData->SubstringData(0, endOffset, cutValue, err); + if (NS_WARN_IF(err.Failed())) { + return err.StealNSResult(); + } nsCOMPtr clone = node->CloneNode(false, err); if (NS_WARN_IF(err.Failed())) { return err.StealNSResult(); diff --git a/dom/interfaces/core/nsIDOMCharacterData.idl b/dom/interfaces/core/nsIDOMCharacterData.idl index e55e22ce3aae..fc70e66ecf88 100644 --- a/dom/interfaces/core/nsIDOMCharacterData.idl +++ b/dom/interfaces/core/nsIDOMCharacterData.idl @@ -17,9 +17,6 @@ interface nsIDOMCharacterData : nsIDOMNode { readonly attribute unsigned long length; - DOMString substringData(in unsigned long offset, - in unsigned long count) - raises(DOMException); void appendData(in DOMString arg) raises(DOMException); void insertData(in unsigned long offset, diff --git a/editor/libeditor/DeleteTextTransaction.cpp b/editor/libeditor/DeleteTextTransaction.cpp index 8d563255478d..2dcc64dd5991 100644 --- a/editor/libeditor/DeleteTextTransaction.cpp +++ b/editor/libeditor/DeleteTextTransaction.cpp @@ -123,12 +123,15 @@ DeleteTextTransaction::DoTransaction() } // Get the text that we're about to delete - nsresult rv = mCharData->SubstringData(mOffset, mLengthToDelete, - mDeletedText); - MOZ_ASSERT(NS_SUCCEEDED(rv)); - rv = mCharData->DeleteData(mOffset, mLengthToDelete); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + ErrorResult err; + mCharData->SubstringData(mOffset, mLengthToDelete, mDeletedText, err); + if (NS_WARN_IF(err.Failed())) { + return err.StealNSResult(); + } + + mCharData->DeleteData(mOffset, mLengthToDelete, err); + if (NS_WARN_IF(err.Failed())) { + return err.StealNSResult(); } mEditorBase->RangeUpdaterRef(). diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index 08c4e93da7ea..ac7285c6b33c 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -3221,7 +3221,7 @@ EditorBase::SplitNodeImpl(const EditorDOMPoint& aStartOfRightNode, // Fix right node nsAutoString leftText; rightAsText->SubstringData(0, aStartOfRightNode.Offset(), - leftText); + leftText, IgnoreErrors()); rightAsText->DeleteData(0, aStartOfRightNode.Offset()); // Fix left node leftAsText->GetAsText()->SetData(leftText, IgnoreErrors());