2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsHTMLEditUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsHTMLEditor.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMElement.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMEventTarget.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIDOMHTMLElement.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIHTMLEditor.h"
|
|
|
|
#include "nsIHTMLObjectResizer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIPresShell.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsLiteralString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsReadableUtils.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nscore.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Uncomment the following line if you want to disable
|
|
|
|
// table deletion when the only column/row is removed
|
|
|
|
// #define DISABLE_TABLE_DELETION 1
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsHTMLEditor::SetInlineTableEditingEnabled(bool aIsEnabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mIsInlineTableEditingEnabled = aIsEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsHTMLEditor::GetInlineTableEditingEnabled(bool * aIsEnabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aIsEnabled = mIsInlineTableEditingEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::ShowInlineTableEditingUI(nsIDOMElement * aCell)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCell);
|
|
|
|
|
|
|
|
// do nothing if aCell is not a table cell...
|
|
|
|
if (!nsHTMLEditUtils::IsTableCell(aCell))
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-04-13 13:22:31 -07:00
|
|
|
if (mInlineEditedCell) {
|
|
|
|
NS_ERROR("call HideInlineTableEditingUI first");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// the resizers and the shadow will be anonymous children of the body
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement = do_QueryInterface(GetRoot());
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement,
|
|
|
|
NS_LITERAL_STRING("mozTableAddColumnBefore"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, getter_AddRefs(mAddColumnBeforeButton));
|
2007-03-22 10:30:00 -07:00
|
|
|
CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement,
|
|
|
|
NS_LITERAL_STRING("mozTableRemoveColumn"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, getter_AddRefs(mRemoveColumnButton));
|
2007-03-22 10:30:00 -07:00
|
|
|
CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement,
|
|
|
|
NS_LITERAL_STRING("mozTableAddColumnAfter"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, getter_AddRefs(mAddColumnAfterButton));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement,
|
|
|
|
NS_LITERAL_STRING("mozTableAddRowBefore"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, getter_AddRefs(mAddRowBeforeButton));
|
2007-03-22 10:30:00 -07:00
|
|
|
CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement,
|
|
|
|
NS_LITERAL_STRING("mozTableRemoveRow"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, getter_AddRefs(mRemoveRowButton));
|
2007-03-22 10:30:00 -07:00
|
|
|
CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement,
|
|
|
|
NS_LITERAL_STRING("mozTableAddRowAfter"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, getter_AddRefs(mAddRowAfterButton));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
AddMouseClickListener(mAddColumnBeforeButton);
|
|
|
|
AddMouseClickListener(mRemoveColumnButton);
|
|
|
|
AddMouseClickListener(mAddColumnAfterButton);
|
|
|
|
AddMouseClickListener(mAddRowBeforeButton);
|
|
|
|
AddMouseClickListener(mRemoveRowButton);
|
|
|
|
AddMouseClickListener(mAddRowAfterButton);
|
|
|
|
|
|
|
|
mInlineEditedCell = aCell;
|
|
|
|
return RefreshInlineTableEditingUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::HideInlineTableEditingUI()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mInlineEditedCell = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
RemoveMouseClickListener(mAddColumnBeforeButton);
|
|
|
|
RemoveMouseClickListener(mRemoveColumnButton);
|
|
|
|
RemoveMouseClickListener(mAddColumnAfterButton);
|
|
|
|
RemoveMouseClickListener(mAddRowBeforeButton);
|
|
|
|
RemoveMouseClickListener(mRemoveRowButton);
|
|
|
|
RemoveMouseClickListener(mAddRowAfterButton);
|
|
|
|
|
|
|
|
// get the presshell's document observer interface.
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-05-05 18:20:43 -07:00
|
|
|
// We allow the pres shell to be null; when it is, we presume there
|
|
|
|
// are no document observers to notify, but we still want to
|
|
|
|
// UnbindFromTree.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the root content node.
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIContent> bodyContent = GetRoot();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(bodyContent, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DeleteRefToAnonymousNode(mAddColumnBeforeButton, bodyContent, ps);
|
2012-07-30 07:20:58 -07:00
|
|
|
mAddColumnBeforeButton = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteRefToAnonymousNode(mRemoveColumnButton, bodyContent, ps);
|
2012-07-30 07:20:58 -07:00
|
|
|
mRemoveColumnButton = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteRefToAnonymousNode(mAddColumnAfterButton, bodyContent, ps);
|
2012-07-30 07:20:58 -07:00
|
|
|
mAddColumnAfterButton = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteRefToAnonymousNode(mAddRowBeforeButton, bodyContent, ps);
|
2012-07-30 07:20:58 -07:00
|
|
|
mAddRowBeforeButton = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteRefToAnonymousNode(mRemoveRowButton, bodyContent, ps);
|
2012-07-30 07:20:58 -07:00
|
|
|
mRemoveRowButton = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteRefToAnonymousNode(mAddRowAfterButton, bodyContent, ps);
|
2012-07-30 07:20:58 -07:00
|
|
|
mAddRowAfterButton = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::DoInlineTableEditingAction(nsIDOMElement * aElement)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aElement);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool anonElement = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aElement &&
|
|
|
|
NS_SUCCEEDED(aElement->HasAttribute(NS_LITERAL_STRING("_moz_anonclass"), &anonElement)) &&
|
|
|
|
anonElement) {
|
|
|
|
nsAutoString anonclass;
|
|
|
|
nsresult res = aElement->GetAttribute(NS_LITERAL_STRING("_moz_anonclass"), anonclass);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!StringBeginsWith(anonclass, NS_LITERAL_STRING("mozTable")))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> tableNode = GetEnclosingTable(mInlineEditedCell);
|
|
|
|
nsCOMPtr<nsIDOMElement> tableElement = do_QueryInterface(tableNode);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t rowCount, colCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = GetTableSize(tableElement, &rowCount, &colCount);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hideUI = false;
|
|
|
|
bool hideResizersWithInlineTableUI = (mResizedObject == tableElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (anonclass.EqualsLiteral("mozTableAddColumnBefore"))
|
2011-10-17 07:59:28 -07:00
|
|
|
InsertTableColumn(1, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (anonclass.EqualsLiteral("mozTableAddColumnAfter"))
|
2011-10-17 07:59:28 -07:00
|
|
|
InsertTableColumn(1, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (anonclass.EqualsLiteral("mozTableAddRowBefore"))
|
2011-10-17 07:59:28 -07:00
|
|
|
InsertTableRow(1, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (anonclass.EqualsLiteral("mozTableAddRowAfter"))
|
2011-10-17 07:59:28 -07:00
|
|
|
InsertTableRow(1, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (anonclass.EqualsLiteral("mozTableRemoveColumn")) {
|
|
|
|
DeleteTableColumn(1);
|
|
|
|
#ifndef DISABLE_TABLE_DELETION
|
|
|
|
hideUI = (colCount == 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (anonclass.EqualsLiteral("mozTableRemoveRow")) {
|
|
|
|
DeleteTableRow(1);
|
|
|
|
#ifndef DISABLE_TABLE_DELETION
|
|
|
|
hideUI = (rowCount == 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (hideUI) {
|
|
|
|
HideInlineTableEditingUI();
|
|
|
|
if (hideResizersWithInlineTableUI)
|
|
|
|
HideResizers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::AddMouseClickListener(nsIDOMElement * aElement)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> evtTarget(do_QueryInterface(aElement));
|
2010-03-05 20:01:28 -08:00
|
|
|
if (evtTarget) {
|
|
|
|
evtTarget->AddEventListener(NS_LITERAL_STRING("click"),
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventListener, true);
|
2010-03-05 20:01:28 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::RemoveMouseClickListener(nsIDOMElement * aElement)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> evtTarget(do_QueryInterface(aElement));
|
2010-03-05 20:01:28 -08:00
|
|
|
if (evtTarget) {
|
|
|
|
evtTarget->RemoveEventListener(NS_LITERAL_STRING("click"),
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventListener, true);
|
2010-03-05 20:01:28 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::RefreshInlineTableEditingUI()
|
|
|
|
{
|
2011-10-29 13:03:55 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLElement> htmlElement = do_QueryInterface(mInlineEditedCell);
|
|
|
|
if (!htmlElement) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t xCell, yCell, wCell, hCell;
|
2007-03-22 10:30:00 -07:00
|
|
|
GetElementOrigin(mInlineEditedCell, xCell, yCell);
|
|
|
|
|
2011-10-29 13:03:55 -07:00
|
|
|
nsresult res = htmlElement->GetOffsetWidth(&wCell);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-10-29 13:03:55 -07:00
|
|
|
res = htmlElement->GetOffsetHeight(&hCell);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t xHoriz = xCell + wCell/2;
|
|
|
|
int32_t yVert = yCell + hCell/2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> tableNode = GetEnclosingTable(mInlineEditedCell);
|
|
|
|
nsCOMPtr<nsIDOMElement> tableElement = do_QueryInterface(tableNode);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t rowCount, colCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = GetTableSize(tableElement, &rowCount, &colCount);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
SetAnonymousElementPosition(xHoriz-10, yCell-7, mAddColumnBeforeButton);
|
|
|
|
#ifdef DISABLE_TABLE_DELETION
|
|
|
|
NS_NAMED_LITERAL_STRING(classStr, "class");
|
|
|
|
|
|
|
|
if (colCount== 1) {
|
|
|
|
mRemoveColumnButton->SetAttribute(classStr,
|
|
|
|
NS_LITERAL_STRING("hidden"));
|
|
|
|
}
|
|
|
|
else {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasClass = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mRemoveColumnButton->HasAttribute(classStr, &hasClass);
|
|
|
|
if (NS_SUCCEEDED(res) && hasClass)
|
|
|
|
mRemoveColumnButton->RemoveAttribute(classStr);
|
|
|
|
#endif
|
|
|
|
SetAnonymousElementPosition(xHoriz-4, yCell-7, mRemoveColumnButton);
|
|
|
|
#ifdef DISABLE_TABLE_DELETION
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
SetAnonymousElementPosition(xHoriz+6, yCell-7, mAddColumnAfterButton);
|
|
|
|
|
|
|
|
SetAnonymousElementPosition(xCell-7, yVert-10, mAddRowBeforeButton);
|
|
|
|
#ifdef DISABLE_TABLE_DELETION
|
|
|
|
if (rowCount== 1) {
|
|
|
|
mRemoveRowButton->SetAttribute(classStr,
|
|
|
|
NS_LITERAL_STRING("hidden"));
|
|
|
|
}
|
|
|
|
else {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasClass = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mRemoveRowButton->HasAttribute(classStr, &hasClass);
|
|
|
|
if (NS_SUCCEEDED(res) && hasClass)
|
|
|
|
mRemoveRowButton->RemoveAttribute(classStr);
|
|
|
|
#endif
|
|
|
|
SetAnonymousElementPosition(xCell-7, yVert-4, mRemoveRowButton);
|
|
|
|
#ifdef DISABLE_TABLE_DELETION
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
SetAnonymousElementPosition(xCell-7, yVert+6, mAddRowAfterButton);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|