Bug 1457083 - part 2: Make all observer methods of HTMLEditRules create AutoSafeEditorData r=m_kato

Except HTMLEditRules::WillJoinNodes(), observer methods of HTMLEditRules
accesses mHTMLEditor.  Therefore, we need to make them create AutoSafeEditorData
instance in the stack.

Note that for reducing EditorBase::GetSelection() calls, this patch adds
Selection& argument to all of them.

MozReview-Commit-ID: 6mjuD2gZwVp

--HG--
extra : rebase_source : 56f16747a80927f0477b9b54f6088be719ed7b01
This commit is contained in:
Masayuki Nakano 2018-04-26 23:27:50 +09:00
Родитель 5f8ba21730
Коммит 30e06e667e
4 изменённых файлов: 153 добавлений и 54 удалений

Просмотреть файл

@ -1389,9 +1389,14 @@ EditorBase::CreateNodeWithTransaction(
aPointToInsert.Offset()));
}
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidCreateNode(newElement);
if (mRules && mRules->AsHTMLEditRules() && newElement) {
Selection* selection = GetSelection();
if (selection) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidCreateNode(*selection, *newElement);
} else {
NS_WARNING("Selection has gone");
}
}
if (!mActionListeners.IsEmpty()) {
@ -1445,8 +1450,13 @@ EditorBase::InsertNodeWithTransaction(
mRangeUpdater.SelAdjInsertNode(aPointToInsert);
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidInsertNode(aContentToInsert);
Selection* selection = GetSelection();
if (selection) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidInsertNode(*selection, aContentToInsert);
} else {
NS_WARNING("Selection has gone");
}
}
if (!mActionListeners.IsEmpty()) {
@ -1513,9 +1523,15 @@ EditorBase::SplitNodeWithTransaction(
mRangeUpdater.SelAdjSplitNode(*aStartOfRightNode.GetContainerAsContent(),
newNode);
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidSplitNode(aStartOfRightNode.GetContainer(), newNode);
if (mRules && mRules->AsHTMLEditRules() && newNode) {
Selection* selection = GetSelection();
if (selection) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidSplitNode(*selection,
*aStartOfRightNode.GetContainer(), *newNode);
} else {
NS_WARNING("Selection has gone");
}
}
if (mInlineSpellChecker) {
@ -1583,8 +1599,13 @@ EditorBase::JoinNodesWithTransaction(nsINode& aLeftNode,
(int32_t)oldLeftNodeLen);
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidJoinNodes(aLeftNode, aRightNode);
Selection* selection = GetSelection();
if (selection) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidJoinNodes(*selection, aLeftNode, aRightNode);
} else {
NS_WARNING("Selection has gone");
}
}
if (mInlineSpellChecker) {
@ -1625,8 +1646,13 @@ EditorBase::DeleteNodeWithTransaction(nsINode& aNode)
nsIEditor::ePrevious);
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->WillDeleteNode(&aNode);
Selection* selection = GetSelection();
if (selection) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->WillDeleteNode(*selection, aNode);
} else {
NS_WARNING("Selection has gone");
}
}
// FYI: DeleteNodeTransaction grabs aNode while it's alive. So, it's safe
@ -2776,10 +2802,15 @@ EditorBase::InsertTextIntoTextNodeWithTransaction(
nsresult rv = DoTransaction(transaction);
EndUpdateViewBatch();
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidInsertText(insertedTextNode, insertedOffset,
aStringToInsert);
if (mRules && mRules->AsHTMLEditRules() && insertedTextNode) {
Selection* selection = GetSelection();
if (selection) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidInsertText(*selection, *insertedTextNode,
insertedOffset, aStringToInsert);
} else {
NS_WARNING("Selection has gone");
}
}
// let listeners know what happened
@ -2927,9 +2958,13 @@ EditorBase::SetTextImpl(Selection& aSelection, const nsAString& aString,
return rv;
}
RefPtr<Selection> selection = GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
{
// Create a nested scope to not overwrite rv from the outer scope.
RefPtr<Selection> selection = GetSelection();
DebugOnly<nsresult> rv = selection->Collapse(&aCharData, aString.Length());
NS_ASSERTION(NS_SUCCEEDED(rv),
"Selection could not be collapsed after insert");
@ -2941,10 +2976,10 @@ EditorBase::SetTextImpl(Selection& aSelection, const nsAString& aString,
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
if (length) {
htmlEditRules->DidDeleteText(&aCharData, 0, length);
htmlEditRules->DidDeleteText(*selection, aCharData, 0, length);
}
if (!aString.IsEmpty()) {
htmlEditRules->DidInsertText(&aCharData, 0, aString);
htmlEditRules->DidInsertText(*selection, aCharData, 0, aString);
}
}
@ -2989,8 +3024,13 @@ EditorBase::DeleteTextWithTransaction(CharacterData& aCharData,
nsresult rv = DoTransaction(transaction);
if (mRules && mRules->AsHTMLEditRules()) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidDeleteText(&aCharData, aOffset, aLength);
RefPtr<Selection> selection = GetSelection();
if (selection) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidDeleteText(*selection, aCharData, aOffset, aLength);
} else {
NS_WARNING("Selection has gone");
}
}
// Let listeners know what happened

Просмотреть файл

@ -9317,17 +9317,22 @@ HTMLEditRules::InsertBRIfNeededInternal(nsINode& aNode,
}
void
HTMLEditRules::DidCreateNode(Element* aNewElement)
HTMLEditRules::DidCreateNode(Selection& aSelection,
Element& aNewElement)
{
if (!mListenerEnabled) {
return;
}
if (NS_WARN_IF(!aNewElement)) {
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
// assumption that Join keeps the righthand node
IgnoredErrorResult error;
mUtilRange->SelectNode(*aNewElement, error);
mUtilRange->SelectNode(aNewElement, error);
if (NS_WARN_IF(error.Failed())) {
return;
}
@ -9335,11 +9340,19 @@ HTMLEditRules::DidCreateNode(Element* aNewElement)
}
void
HTMLEditRules::DidInsertNode(nsIContent& aContent)
HTMLEditRules::DidInsertNode(Selection& aSelection,
nsIContent& aContent)
{
if (!mListenerEnabled) {
return;
}
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
IgnoredErrorResult error;
mUtilRange->SelectNode(aContent, error);
if (NS_WARN_IF(error.Failed())) {
@ -9349,16 +9362,21 @@ HTMLEditRules::DidInsertNode(nsIContent& aContent)
}
void
HTMLEditRules::WillDeleteNode(nsINode* aChild)
HTMLEditRules::WillDeleteNode(Selection& aSelection,
nsINode& aChild)
{
if (!mListenerEnabled) {
return;
}
if (NS_WARN_IF(!aChild)) {
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
IgnoredErrorResult error;
mUtilRange->SelectNode(*aChild, error);
mUtilRange->SelectNode(aChild, error);
if (NS_WARN_IF(error.Failed())) {
return;
}
@ -9366,14 +9384,22 @@ HTMLEditRules::WillDeleteNode(nsINode* aChild)
}
void
HTMLEditRules::DidSplitNode(nsINode* aExistingRightNode,
nsINode* aNewLeftNode)
HTMLEditRules::DidSplitNode(Selection& aSelection,
nsINode& aExistingRightNode,
nsINode& aNewLeftNode)
{
if (!mListenerEnabled) {
return;
}
nsresult rv = mUtilRange->SetStartAndEnd(aNewLeftNode, 0,
aExistingRightNode, 0);
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
nsresult rv = mUtilRange->SetStartAndEnd(&aNewLeftNode, 0,
&aExistingRightNode, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
@ -9392,12 +9418,20 @@ HTMLEditRules::WillJoinNodes(nsINode& aLeftNode,
}
void
HTMLEditRules::DidJoinNodes(nsINode& aLeftNode,
HTMLEditRules::DidJoinNodes(Selection& aSelection,
nsINode& aLeftNode,
nsINode& aRightNode)
{
if (!mListenerEnabled) {
return;
}
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
// assumption that Join keeps the righthand node
nsresult rv = mUtilRange->CollapseTo(&aRightNode, mJoinOffset);
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -9407,16 +9441,24 @@ HTMLEditRules::DidJoinNodes(nsINode& aLeftNode,
}
void
HTMLEditRules::DidInsertText(nsINode* aTextNode,
HTMLEditRules::DidInsertText(Selection& aSelection,
nsINode& aTextNode,
int32_t aOffset,
const nsAString& aString)
{
if (!mListenerEnabled) {
return;
}
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
int32_t length = aString.Length();
nsresult rv = mUtilRange->SetStartAndEnd(aTextNode, aOffset,
aTextNode, aOffset + length);
nsresult rv = mUtilRange->SetStartAndEnd(&aTextNode, aOffset,
&aTextNode, aOffset + length);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
@ -9424,14 +9466,22 @@ HTMLEditRules::DidInsertText(nsINode* aTextNode,
}
void
HTMLEditRules::DidDeleteText(nsINode* aTextNode,
HTMLEditRules::DidDeleteText(Selection& aSelection,
nsINode& aTextNode,
int32_t aOffset,
int32_t aLength)
{
if (!mListenerEnabled) {
return;
}
nsresult rv = mUtilRange->CollapseTo(aTextNode, aOffset);
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
nsresult rv = mUtilRange->CollapseTo(&aTextNode, aOffset);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
@ -9439,19 +9489,23 @@ HTMLEditRules::DidDeleteText(nsINode* aTextNode,
}
void
HTMLEditRules::WillDeleteSelection(Selection* aSelection)
HTMLEditRules::WillDeleteSelection(Selection& aSelection)
{
if (!mListenerEnabled) {
return;
}
if (NS_WARN_IF(!aSelection)) {
if (NS_WARN_IF(!mHTMLEditor)) {
return;
}
EditorRawDOMPoint startPoint = EditorBase::GetStartPoint(aSelection);
AutoSafeEditorData setData(*this, *mHTMLEditor, aSelection);
EditorRawDOMPoint startPoint = EditorBase::GetStartPoint(&aSelection);
if (NS_WARN_IF(!startPoint.IsSet())) {
return;
}
EditorRawDOMPoint endPoint = EditorBase::GetEndPoint(aSelection);
EditorRawDOMPoint endPoint = EditorBase::GetEndPoint(&aSelection);
if (NS_WARN_IF(!endPoint.IsSet())) {
return;
}

Просмотреть файл

@ -104,17 +104,21 @@ public:
nsresult GetParagraphState(bool* aMixed, nsAString& outFormat);
nsresult MakeSureElemStartsAndEndsOnCR(nsINode& aNode);
void DidCreateNode(Element* aNewElement);
void DidInsertNode(nsIContent& aNode);
void WillDeleteNode(nsINode* aChild);
void DidSplitNode(nsINode* aExistingRightNode,
nsINode* aNewLeftNode);
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(nsINode& aLeftNode, nsINode& aRightNode);
void DidInsertText(nsINode* aTextNode, int32_t aOffset,
void DidJoinNodes(Selection& aSelection,
nsINode& aLeftNode, nsINode& aRightNode);
void DidInsertText(Selection& aSelection,
nsINode& aTextNode, int32_t aOffset,
const nsAString& aString);
void DidDeleteText(nsINode* aTextNode, int32_t aOffset, int32_t aLength);
void WillDeleteSelection(Selection* aSelection);
void DidDeleteText(Selection& aSelection,
nsINode& aTextNode, int32_t aOffset, int32_t aLength);
void WillDeleteSelection(Selection& aSelection);
void StartToListenToEditActions() { mListenerEnabled = true; }
void EndListeningToEditActions() { mListenerEnabled = false; }

Просмотреть файл

@ -750,10 +750,10 @@ TextEditor::DeleteSelectionWithTransaction(EDirection aDirection,
if (mRules && mRules->AsHTMLEditRules()) {
if (!deleteNode) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->WillDeleteSelection(selection);
htmlEditRules->WillDeleteSelection(*selection);
} else if (!deleteCharData) {
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->WillDeleteNode(deleteNode);
htmlEditRules->WillDeleteNode(*selection, *deleteNode);
}
}
@ -776,8 +776,9 @@ TextEditor::DeleteSelectionWithTransaction(EDirection aDirection,
nsresult rv = DoTransaction(deleteSelectionTransaction);
if (mRules && mRules->AsHTMLEditRules() && deleteCharData) {
MOZ_ASSERT(deleteNode);
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
htmlEditRules->DidDeleteText(deleteNode, deleteCharOffset, 1);
htmlEditRules->DidDeleteText(*selection, *deleteNode, deleteCharOffset, 1);
}
if (mTextServicesDocument && NS_SUCCEEDED(rv) &&