Bug 1467799 - part 1: Remove implementation of EditorBase::GetDocumentIsEmpty() and rename TextEditor::DocumentIsEmpty() to TextEditor::IsEmpty() r?m_kato
EditorBase::GetDocumentIsEmpty() is never called since it's overridden by
TextEditor::GetDocumentIsEmtpy() and never called directly. So, we can remove
its implementation.
Additionally, DocumentIsEmpty() is redundant. We can make it just IsEmpty().
MozReview-Commit-ID: CGsNzCHyVf
--- a/dom/html/nsTextEditorState.cpp
+++ b/dom/html/nsTextEditorState.cpp
@@ -2538,17 +2538,17 @@ nsTextEditorState::SetValue(const nsAStr
}
bool
nsTextEditorState::HasNonEmptyValue()
{
if (mTextEditor && mBoundFrame && mEditorInitialized &&
!mIsCommittingComposition) {
bool empty;
- nsresult rv = mTextEditor->DocumentIsEmpty(&empty);
+ nsresult rv = mTextEditor->IsEmpty(&empty);
if (NS_SUCCEEDED(rv)) {
return !empty;
}
}
nsAutoString value;
GetValue(value, true);
return !value.IsEmpty();
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -995,23 +995,17 @@ EditorBase::SetShouldTxnSetSelection(boo
{
mShouldTxnSetSelection = aShould;
return NS_OK;
}
NS_IMETHODIMP
EditorBase::GetDocumentIsEmpty(bool* aDocumentIsEmpty)
{
- *aDocumentIsEmpty = true;
-
- dom::Element* root = GetRoot();
- NS_ENSURE_TRUE(root, NS_ERROR_NULL_POINTER);
-
- *aDocumentIsEmpty = !root->HasChildren();
- return NS_OK;
+ return NS_ERROR_NOT_IMPLEMENTED;
}
// XXX: The rule system should tell us which node to select all on (ie, the
// root, or the body)
NS_IMETHODIMP
EditorBase::SelectAll()
{
// XXX Why doesn't this check if the document is alive?
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -392,17 +392,17 @@ TextEditRules::DidDoAction(Selection* aS
return NS_OK;
}
}
bool
TextEditRules::DocumentIsEmpty()
{
bool retVal = false;
- if (!mTextEditor || NS_FAILED(mTextEditor->DocumentIsEmpty(&retVal))) {
+ if (!mTextEditor || NS_FAILED(mTextEditor->IsEmpty(&retVal))) {
retVal = true;
}
return retVal;
}
nsresult
TextEditRules::WillInsert(bool* aCancel)
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -1312,50 +1312,56 @@ TextEditor::OnCompositionEnd(WidgetCompo
already_AddRefed<nsIContent>
TextEditor::GetInputEventTargetContent()
{
nsCOMPtr<nsIContent> target = do_QueryInterface(mEventTarget);
return target.forget();
}
nsresult
-TextEditor::DocumentIsEmpty(bool* aIsEmpty)
+TextEditor::IsEmpty(bool* aIsEmpty) const
{
- NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
+ if (NS_WARN_IF(!mRules)) {
+ return NS_ERROR_NOT_INITIALIZED;
+ }
+
+ *aIsEmpty = true;
if (mRules->HasBogusNode()) {
- *aIsEmpty = true;
return NS_OK;
}
- // Even if there is no bogus node, we should be detected as empty document
+ // Even if there is no bogus node, we should be detected as empty editor
// if all the children are text nodes and these have no content.
Element* rootElement = GetRoot();
if (!rootElement) {
- *aIsEmpty = true;
+ // XXX Why don't we return an error in such case??
return NS_OK;
}
for (nsIContent* child = rootElement->GetFirstChild();
child; child = child->GetNextSibling()) {
if (!EditorBase::IsTextNode(child) ||
static_cast<nsTextNode*>(child)->TextDataLength()) {
*aIsEmpty = false;
return NS_OK;
}
}
- *aIsEmpty = true;
return NS_OK;
}
NS_IMETHODIMP
TextEditor::GetDocumentIsEmpty(bool* aDocumentIsEmpty)
{
- return DocumentIsEmpty(aDocumentIsEmpty);
+ nsresult rv = IsEmpty(aDocumentIsEmpty);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+ return NS_OK;
}
NS_IMETHODIMP
TextEditor::GetTextLength(int32_t* aCount)
{
NS_ASSERTION(aCount, "null pointer");
// initialize out params
--- a/editor/libeditor/TextEditor.h
+++ b/editor/libeditor/TextEditor.h
@@ -93,17 +93,30 @@ public:
*/
virtual bool CanPasteTransferable(nsITransferable* aTransferable);
// Overrides of EditorBase
virtual nsresult Init(nsIDocument& aDoc, Element* aRoot,
nsISelectionController* aSelCon, uint32_t aFlags,
const nsAString& aValue) override;
- nsresult DocumentIsEmpty(bool* aIsEmpty);
+ /**
+ * IsEmpty() checks whether the editor is empty. If editor has only bogus
+ * node, returns true. If editor's root element has non-empty text nodes or
+ * other nodes like <br>, returns false.
+ */
+ nsresult IsEmpty(bool* aIsEmpty) const;
+ bool IsEmpty() const
+ {
+ bool isEmpty = false;
+ nsresult rv = IsEmpty(&isEmpty);
+ NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+ "Checking whether the editor is empty failed");
+ return NS_SUCCEEDED(rv) && isEmpty;
+ }
virtual nsresult HandleKeyPressEvent(
WidgetKeyboardEvent* aKeyboardEvent) override;
virtual dom::EventTarget* GetDOMEventTarget() override;
/**
* InsertTextAsAction() inserts aStringToInsert at selection.