Bug 1086349 part 3 - Clean up nsEditor::InsertTextImpl; r=ehsan

This commit is contained in:
Aryeh Gregor 2014-11-02 14:04:13 +02:00
parent 12ffa645e5
commit 2420067448
8 changed files with 72 additions and 84 deletions

View File

@ -2269,11 +2269,11 @@ NS_IMETHODIMP nsEditor::ScrollSelectionIntoView(bool aScrollToAnchor)
return NS_OK;
}
NS_IMETHODIMP
nsresult
nsEditor::InsertTextImpl(const nsAString& aStringToInsert,
nsCOMPtr<nsIDOMNode>* aInOutNode,
nsCOMPtr<nsINode>* aInOutNode,
int32_t* aInOutOffset,
nsIDOMDocument* aDoc)
nsIDocument* aDoc)
{
// NOTE: caller *must* have already used nsAutoTxnsConserveSelection
// stack-based class to turn off txn selection updating. Caller also turned
@ -2285,8 +2285,7 @@ nsEditor::InsertTextImpl(const nsAString& aStringToInsert,
return NS_OK;
}
nsCOMPtr<nsINode> node = do_QueryInterface(*aInOutNode);
NS_ENSURE_STATE(node);
nsCOMPtr<nsINode> node = *aInOutNode;
uint32_t offset = static_cast<uint32_t>(*aInOutOffset);
if (!node->IsNodeOfType(nsINode::eTEXT) && IsPlaintextEditor()) {
@ -2324,11 +2323,9 @@ nsEditor::InsertTextImpl(const nsAString& aStringToInsert,
if (mComposition) {
if (!node->IsNodeOfType(nsINode::eTEXT)) {
// create a text node
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
NS_ENSURE_STATE(doc);
nsRefPtr<nsTextNode> newNode = doc->CreateTextNode(EmptyString());
nsRefPtr<nsTextNode> newNode = aDoc->CreateTextNode(EmptyString());
// then we insert it into the dom tree
res = InsertNode(newNode->AsDOMNode(), node->AsDOMNode(), offset);
res = InsertNode(*newNode, *node, offset);
NS_ENSURE_SUCCESS(res, res);
node = newNode;
offset = 0;
@ -2347,18 +2344,16 @@ nsEditor::InsertTextImpl(const nsAString& aStringToInsert,
} else {
// we are inserting text into a non-text node. first we have to create a
// textnode (this also populates it with the text)
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
NS_ENSURE_STATE(doc);
nsRefPtr<nsTextNode> newNode = doc->CreateTextNode(aStringToInsert);
nsRefPtr<nsTextNode> newNode = aDoc->CreateTextNode(aStringToInsert);
// then we insert it into the dom tree
res = InsertNode(newNode->AsDOMNode(), node->AsDOMNode(), offset);
res = InsertNode(*newNode, *node, offset);
NS_ENSURE_SUCCESS(res, res);
node = newNode;
offset = aStringToInsert.Length();
}
}
*aInOutNode = node->AsDOMNode();
*aInOutNode = node;
*aInOutOffset = static_cast<int32_t>(offset);
return NS_OK;
}

View File

@ -202,10 +202,10 @@ public:
virtual bool IsModifiableNode(nsINode *aNode);
NS_IMETHOD InsertTextImpl(const nsAString& aStringToInsert,
nsCOMPtr<nsIDOMNode> *aInOutNode,
int32_t *aInOutOffset,
nsIDOMDocument *aDoc);
virtual nsresult InsertTextImpl(const nsAString& aStringToInsert,
nsCOMPtr<nsINode>* aInOutNode,
int32_t* aInOutOffset,
nsIDocument* aDoc);
nsresult InsertTextIntoTextNodeImpl(const nsAString& aStringToInsert,
mozilla::dom::Text& aTextNode,
int32_t aOffset,

View File

@ -1325,9 +1325,6 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
*aCancel = false;
*aHandled = true;
nsresult res;
nsCOMPtr<nsIDOMNode> selNode;
int32_t selOffset;
// If the selection isn't collapsed, delete it. Don't delete existing inline
// tags, because we're hopefully going to insert text (bug 787432).
if (!aSelection->Collapsed()) {
@ -1344,24 +1341,26 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
// we need to get the doc
NS_ENSURE_STATE(mHTMLEditor);
nsCOMPtr<nsIDOMDocument> doc = mHTMLEditor->GetDOMDocument();
nsCOMPtr<nsIDocument> doc_ = mHTMLEditor->GetDocument();
NS_ENSURE_TRUE(doc && doc_, NS_ERROR_NOT_INITIALIZED);
nsCOMPtr<nsIDocument> doc = mHTMLEditor->GetDocument();
nsCOMPtr<nsIDOMDocument> domDoc = mHTMLEditor->GetDOMDocument();
NS_ENSURE_TRUE(doc && domDoc, NS_ERROR_NOT_INITIALIZED);
// for every property that is set, insert a new inline style node
res = CreateStyleForInsertText(aSelection, doc);
res = CreateStyleForInsertText(aSelection, domDoc);
NS_ENSURE_SUCCESS(res, res);
// get the (collapsed) selection location
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(selNode), &selOffset);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(aSelection->GetRangeAt(0));
nsCOMPtr<nsINode> selNode = aSelection->GetRangeAt(0)->GetStartParent();
int32_t selOffset = aSelection->GetRangeAt(0)->StartOffset();
NS_ENSURE_STATE(selNode);
// dont put text in places that can't have it
NS_ENSURE_STATE(mHTMLEditor);
if (!mHTMLEditor->IsTextNode(selNode) &&
(!mHTMLEditor ||
!mHTMLEditor->CanContainTag(selNode, nsGkAtoms::textTagName))) {
(!mHTMLEditor || !mHTMLEditor->CanContainTag(GetAsDOMNode(selNode),
nsGkAtoms::textTagName))) {
return NS_ERROR_FAILURE;
}
@ -1371,29 +1370,28 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
if (inString->IsEmpty())
{
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->InsertTextImpl(*inString, address_of(selNode), &selOffset, doc);
res = mHTMLEditor->InsertTextImpl(*inString, address_of(selNode),
&selOffset, doc);
}
else
{
NS_ENSURE_STATE(mHTMLEditor);
nsCOMPtr<nsINode> selNode_(do_QueryInterface(selNode));
nsWSRunObject wsObj(mHTMLEditor, selNode_, selOffset);
res = wsObj.InsertText(*inString, address_of(selNode_), &selOffset, doc_);
selNode = GetAsDOMNode(selNode_);
nsWSRunObject wsObj(mHTMLEditor, selNode, selOffset);
res = wsObj.InsertText(*inString, address_of(selNode), &selOffset, doc);
}
NS_ENSURE_SUCCESS(res, res);
}
else // aAction == kInsertText
{
// find where we are
nsCOMPtr<nsIDOMNode> curNode = selNode;
nsCOMPtr<nsINode> curNode = selNode;
int32_t curOffset = selOffset;
// is our text going to be PREformatted?
// We remember this so that we know how to handle tabs.
bool isPRE;
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->IsPreformatted(selNode, &isPRE);
res = mHTMLEditor->IsPreformatted(GetAsDOMNode(selNode), &isPRE);
NS_ENSURE_SUCCESS(res, res);
// turn off the edit listener: we know how to
@ -1407,7 +1405,6 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor);
nsAutoString tString(*inString);
const char16_t *unicodeBuf = tString.get();
nsCOMPtr<nsIDOMNode> unused;
int32_t pos = 0;
NS_NAMED_LITERAL_STRING(newlineStr, LFSTR);
@ -1441,15 +1438,19 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
if (subStr.Equals(newlineStr))
{
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->CreateBRImpl(address_of(curNode), &curOffset, address_of(unused), nsIEditor::eNone);
nsCOMPtr<Element> br =
mHTMLEditor->CreateBRImpl(address_of(curNode), &curOffset,
nsIEditor::eNone);
NS_ENSURE_STATE(br);
pos++;
}
else
{
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->InsertTextImpl(subStr, address_of(curNode), &curOffset, doc);
res = mHTMLEditor->InsertTextImpl(subStr, address_of(curNode),
&curOffset, doc);
NS_ENSURE_SUCCESS(res, res);
}
NS_ENSURE_SUCCESS(res, res);
}
}
else
@ -1483,27 +1484,23 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
// is it a tab?
if (subStr.Equals(tabStr))
{
nsCOMPtr<nsINode> curNode_(do_QueryInterface(curNode));
res = wsObj.InsertText(spacesStr, address_of(curNode_), &curOffset, doc_);
curNode = GetAsDOMNode(curNode_);
res =
wsObj.InsertText(spacesStr, address_of(curNode), &curOffset, doc);
NS_ENSURE_SUCCESS(res, res);
pos++;
}
// is it a return?
else if (subStr.Equals(newlineStr))
{
nsCOMPtr<nsINode> node(do_QueryInterface(curNode));
nsCOMPtr<Element> br =
wsObj.InsertBreak(address_of(node), &curOffset, nsIEditor::eNone);
nsCOMPtr<Element> br = wsObj.InsertBreak(address_of(curNode),
&curOffset,
nsIEditor::eNone);
NS_ENSURE_TRUE(br, NS_ERROR_FAILURE);
curNode = GetAsDOMNode(node);
pos++;
}
else
{
nsCOMPtr<nsINode> curNode_(do_QueryInterface(curNode));
res = wsObj.InsertText(subStr, address_of(curNode_), &curOffset, doc_);
curNode = GetAsDOMNode(curNode_);
res = wsObj.InsertText(subStr, address_of(curNode), &curOffset, doc);
NS_ENSURE_SUCCESS(res, res);
}
NS_ENSURE_SUCCESS(res, res);
@ -1517,9 +1514,7 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
// the correct portion of the document.
if (!mDocChangeRange)
{
nsCOMPtr<nsINode> node = do_QueryInterface(selNode);
NS_ENSURE_STATE(node);
mDocChangeRange = new nsRange(node);
mDocChangeRange = new nsRange(selNode);
}
res = mDocChangeRange->SetStart(selNode, selOffset);
NS_ENSURE_SUCCESS(res, res);

View File

@ -3194,17 +3194,18 @@ nsHTMLEditor::DeleteText(nsGenericDOMDataNode& aCharData, uint32_t aOffset,
return nsEditor::DeleteText(aCharData, aOffset, aLength);
}
NS_IMETHODIMP nsHTMLEditor::InsertTextImpl(const nsAString& aStringToInsert,
nsCOMPtr<nsIDOMNode> *aInOutNode,
int32_t *aInOutOffset,
nsIDOMDocument *aDoc)
nsresult
nsHTMLEditor::InsertTextImpl(const nsAString& aStringToInsert,
nsCOMPtr<nsINode>* aInOutNode,
int32_t* aInOutOffset, nsIDocument* aDoc)
{
// do nothing if the node is read-only
// Do nothing if the node is read-only
if (!IsModifiableNode(*aInOutNode)) {
return NS_ERROR_FAILURE;
}
return nsEditor::InsertTextImpl(aStringToInsert, aInOutNode, aInOutOffset, aDoc);
return nsEditor::InsertTextImpl(aStringToInsert, aInOutNode, aInOutOffset,
aDoc);
}
void

View File

@ -313,10 +313,10 @@ public:
NS_IMETHODIMP DeleteNode(nsIDOMNode * aNode);
nsresult DeleteText(nsGenericDOMDataNode& aTextNode, uint32_t aOffset,
uint32_t aLength);
NS_IMETHOD InsertTextImpl(const nsAString& aStringToInsert,
nsCOMPtr<nsIDOMNode> *aInOutNode,
int32_t *aInOutOffset,
nsIDOMDocument *aDoc);
virtual nsresult InsertTextImpl(const nsAString& aStringToInsert,
nsCOMPtr<nsINode>* aInOutNode,
int32_t* aInOutOffset,
nsIDocument* aDoc) MOZ_OVERRIDE;
NS_IMETHOD_(bool) IsModifiableNode(nsIDOMNode *aNode);
virtual bool IsModifiableNode(nsINode *aNode);

View File

@ -770,18 +770,19 @@ NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak()
if (!cancel && !handled)
{
// get the (collapsed) selection location
nsCOMPtr<nsIDOMNode> selNode;
int32_t selOffset;
res = GetStartNodeAndOffset(selection, getter_AddRefs(selNode), &selOffset);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(selection->GetRangeAt(0));
nsCOMPtr<nsINode> selNode = selection->GetRangeAt(0)->GetStartParent();
int32_t selOffset = selection->GetRangeAt(0)->StartOffset();
NS_ENSURE_STATE(selNode);
// don't put text in places that can't have it
if (!IsTextNode(selNode) && !CanContainTag(selNode, nsGkAtoms::textTagName)) {
if (!IsTextNode(selNode) && !CanContainTag(GetAsDOMNode(selNode),
nsGkAtoms::textTagName)) {
return NS_ERROR_FAILURE;
}
// we need to get the doc
nsCOMPtr<nsIDOMDocument> doc = GetDOMDocument();
nsCOMPtr<nsIDocument> doc = GetDocument();
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
// don't spaz my selection in subtransactions
@ -803,8 +804,8 @@ NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak()
int32_t endOffset;
res = GetEndNodeAndOffset(selection, getter_AddRefs(endNode), &endOffset);
if (NS_SUCCEEDED(res) && endNode == selNode && endOffset == selOffset)
{
if (NS_SUCCEEDED(res) && endNode == GetAsDOMNode(selNode)
&& endOffset == selOffset) {
// SetInterlinePosition(true) means we want the caret to stick to the content on the "right".
// We want the caret to stick to whatever is past the break. This is
// because the break is on the same line we were on, but the next content

View File

@ -700,22 +700,21 @@ nsTextEditRules::WillInsertText(EditAction aAction,
}
// get the (collapsed) selection location
nsCOMPtr<nsIDOMNode> selNode;
int32_t selOffset;
NS_ENSURE_STATE(mEditor);
res = mEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(selNode), &selOffset);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(aSelection->GetRangeAt(0));
nsCOMPtr<nsINode> selNode = aSelection->GetRangeAt(0)->GetStartParent();
int32_t selOffset = aSelection->GetRangeAt(0)->StartOffset();
NS_ENSURE_STATE(selNode);
// don't put text in places that can't have it
NS_ENSURE_STATE(mEditor);
if (!mEditor->IsTextNode(selNode) &&
!mEditor->CanContainTag(selNode, nsGkAtoms::textTagName)) {
!mEditor->CanContainTag(GetAsDOMNode(selNode), nsGkAtoms::textTagName)) {
return NS_ERROR_FAILURE;
}
// we need to get the doc
NS_ENSURE_STATE(mEditor);
nsCOMPtr<nsIDOMDocument> doc = mEditor->GetDOMDocument();
nsCOMPtr<nsIDocument> doc = mEditor->GetDocument();
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
if (aAction == EditAction::insertIMEText) {
@ -724,7 +723,7 @@ nsTextEditRules::WillInsertText(EditAction aAction,
NS_ENSURE_SUCCESS(res, res);
} else {
// aAction == EditAction::insertText; find where we are
nsCOMPtr<nsIDOMNode> curNode = selNode;
nsCOMPtr<nsINode> curNode = selNode;
int32_t curOffset = selOffset;
// don't spaz my selection in subtransactions

View File

@ -382,11 +382,8 @@ nsWSRunObject::InsertText(const nsAString& aStringToInsert,
}
// Ready, aim, fire!
nsCOMPtr<nsIDOMNode> parent(GetAsDOMNode(*aInOutParent));
nsCOMPtr<nsIDOMDocument> doc(do_QueryInterface(aDoc));
res = mHTMLEditor->InsertTextImpl(theString, address_of(parent),
aInOutOffset, doc);
*aInOutParent = do_QueryInterface(parent);
res = mHTMLEditor->InsertTextImpl(theString, aInOutParent, aInOutOffset,
aDoc);
return NS_OK;
}