2012-05-21 15:12:37 +04: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/. */
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
#include "mozilla/HTMLEditor.h"
|
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
#include <math.h>
|
|
|
|
|
2016-07-07 13:49:07 +03:00
|
|
|
#include "HTMLEditorObjectResizerUtils.h"
|
2016-07-07 13:05:51 +03:00
|
|
|
#include "HTMLEditRules.h"
|
2016-07-07 08:01:12 +03:00
|
|
|
#include "HTMLEditUtils.h"
|
2016-07-07 07:44:32 +03:00
|
|
|
#include "TextEditUtils.h"
|
2018-01-12 13:01:04 +03:00
|
|
|
#include "mozilla/EditAction.h"
|
2016-07-08 08:03:31 +03:00
|
|
|
#include "mozilla/EditorUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2017-12-21 08:52:32 +03:00
|
|
|
#include "mozilla/TextEditRules.h"
|
2018-01-11 11:17:57 +03:00
|
|
|
#include "mozilla/dom/CSSPrimitiveValueBinding.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/mozalloc.h"
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsAlgorithm.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2012-07-08 13:50:31 +04:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2003-06-25 12:50:48 +04:00
|
|
|
#include "nsIContent.h"
|
2012-12-14 13:14:21 +04:00
|
|
|
#include "nsROCSSPrimitiveValue.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIDOMEventTarget.h"
|
|
|
|
#include "nsIDOMNode.h"
|
2012-12-14 13:14:21 +04:00
|
|
|
#include "nsDOMCSSRGBColor.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIHTMLObjectResizer.h"
|
|
|
|
#include "nsINode.h"
|
2003-06-25 12:50:48 +04:00
|
|
|
#include "nsIPresShell.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nscore.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2011-06-17 04:59:29 +04:00
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2011-06-17 04:59:29 +04:00
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
nsresult
|
|
|
|
HTMLEditor::SetSelectionToAbsoluteOrStatic(bool aEnabled)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch beginBatching(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this,
|
|
|
|
aEnabled ? EditAction::setAbsolutePosition :
|
|
|
|
EditAction::removeAbsolutePosition,
|
|
|
|
nsIEditor::eNext);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2003-09-03 17:58:00 +04:00
|
|
|
// the line below does not match the code; should it be removed?
|
2003-06-25 12:50:48 +04:00
|
|
|
// Find out if the selection is collapsed:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2017-12-21 08:52:32 +03:00
|
|
|
RulesInfo ruleInfo(aEnabled ? EditAction::setAbsolutePosition :
|
|
|
|
EditAction::removeAbsolutePosition);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 08:52:32 +03:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (NS_FAILED(rv) || cancel) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
return rules->DidDoAction(selection, &ruleInfo, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:42:52 +03:00
|
|
|
already_AddRefed<Element>
|
|
|
|
HTMLEditor::GetAbsolutelyPositionedSelectionContainer()
|
2017-08-04 12:30:13 +03:00
|
|
|
{
|
2003-06-25 12:50:48 +04:00
|
|
|
nsAutoString positionStr;
|
2018-01-30 07:42:52 +03:00
|
|
|
RefPtr<Element> element = GetSelectionContainer();
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2018-01-30 07:42:52 +03:00
|
|
|
while (element && !element->IsHTMLElement(nsGkAtoms::html)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
2018-02-02 12:42:25 +03:00
|
|
|
CSSEditUtils::GetComputedProperty(*element, *nsGkAtoms::position,
|
|
|
|
positionStr);
|
2017-08-04 12:30:13 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2018-01-30 07:42:52 +03:00
|
|
|
return nullptr;
|
2017-08-04 12:30:13 +03:00
|
|
|
}
|
2018-01-30 07:42:52 +03:00
|
|
|
if (positionStr.EqualsLiteral("absolute")) {
|
|
|
|
return element.forget();
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
2018-01-30 07:42:52 +03:00
|
|
|
element = element->GetParentElement();
|
2008-01-22 10:39:06 +03:00
|
|
|
}
|
2018-01-30 07:42:52 +03:00
|
|
|
return nullptr;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GetAbsolutePositioningEnabled(bool* aIsEnabled)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2017-08-04 12:30:13 +03:00
|
|
|
*aIsEnabled = AbsolutePositioningEnabled();
|
2003-06-25 12:50:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SetAbsolutePositioningEnabled(bool aIsEnabled)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
mIsAbsolutelyPositioningEnabled = aIsEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
nsresult
|
|
|
|
HTMLEditor::RelativeChangeElementZIndex(Element& aElement,
|
|
|
|
int32_t aChange,
|
|
|
|
int32_t* aReturn)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aReturn);
|
|
|
|
if (!aChange) // early way out, no change
|
|
|
|
return NS_OK;
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
int32_t zIndex = GetZIndex(aElement);
|
2013-01-15 16:22:03 +04:00
|
|
|
zIndex = std::max(zIndex + aChange, 0);
|
2018-02-01 04:55:25 +03:00
|
|
|
SetZIndex(aElement, zIndex);
|
2003-06-25 12:50:48 +04:00
|
|
|
*aReturn = zIndex;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
void
|
|
|
|
HTMLEditor::SetZIndex(Element& aElement,
|
|
|
|
int32_t aZindex)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
nsAutoString zIndexStr;
|
|
|
|
zIndexStr.AppendInt(aZindex);
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
mCSSEditUtils->SetCSSProperty(aElement, *nsGkAtoms::z_index, zIndexStr);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
nsresult
|
|
|
|
HTMLEditor::AddZIndex(int32_t aChange)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch beginBatching(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this,
|
|
|
|
(aChange < 0) ? EditAction::decreaseZIndex :
|
|
|
|
EditAction::increaseZIndex,
|
|
|
|
nsIEditor::eNext);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2003-09-03 17:58:00 +04:00
|
|
|
// brade: can we get rid of this comment?
|
2003-06-25 12:50:48 +04:00
|
|
|
// Find out if the selection is collapsed:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2017-12-21 08:52:32 +03:00
|
|
|
RulesInfo ruleInfo(aChange < 0 ? EditAction::decreaseZIndex :
|
|
|
|
EditAction::increaseZIndex);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 08:52:32 +03:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
return rules->DidDoAction(selection, &ruleInfo, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
int32_t
|
|
|
|
HTMLEditor::GetZIndex(Element& aElement)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
nsAutoString zIndexStr;
|
2003-09-03 17:58:00 +04:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
2018-02-02 12:42:25 +03:00
|
|
|
CSSEditUtils::GetSpecifiedProperty(aElement, *nsGkAtoms::z_index,
|
|
|
|
zIndexStr);
|
2018-02-01 04:55:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
if (zIndexStr.EqualsLiteral("auto")) {
|
2003-06-25 12:50:48 +04:00
|
|
|
// we have to look at the positioned ancestors
|
|
|
|
// cf. CSS 2 spec section 9.9.1
|
2018-02-01 04:55:25 +03:00
|
|
|
nsCOMPtr<nsINode> node = aElement.GetParentNode();
|
2003-06-25 12:50:48 +04:00
|
|
|
nsAutoString positionStr;
|
2015-04-19 15:28:50 +03:00
|
|
|
while (node && zIndexStr.EqualsLiteral("auto") &&
|
|
|
|
!node->IsHTMLElement(nsGkAtoms::body)) {
|
2018-02-02 12:42:25 +03:00
|
|
|
rv = CSSEditUtils::GetComputedProperty(*node, *nsGkAtoms::position,
|
|
|
|
positionStr);
|
2018-02-01 04:55:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
if (positionStr.EqualsLiteral("absolute")) {
|
2003-06-25 12:50:48 +04:00
|
|
|
// ah, we found one, what's its z-index ? If its z-index is auto,
|
|
|
|
// we have to continue climbing the document's tree
|
2018-02-02 12:42:25 +03:00
|
|
|
rv = CSSEditUtils::GetComputedProperty(*node, *nsGkAtoms::z_index,
|
|
|
|
zIndexStr);
|
2018-02-01 04:55:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return 0;
|
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
node = node->GetParentNode();
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
if (zIndexStr.EqualsLiteral("auto")) {
|
|
|
|
return 0;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
nsresult errorCode;
|
|
|
|
return zIndexStr.ToInteger(&errorCode);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2017-08-02 22:37:44 +03:00
|
|
|
ManualNACPtr
|
2017-08-07 08:09:51 +03:00
|
|
|
HTMLEditor::CreateGrabber(nsIContent& aParentContent)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
// let's create a grabber through the element factory
|
2017-08-02 22:37:44 +03:00
|
|
|
ManualNACPtr ret =
|
2017-08-07 08:09:51 +03:00
|
|
|
CreateAnonymousElement(nsGkAtoms::span, aParentContent,
|
2016-11-24 12:07:47 +03:00
|
|
|
NS_LITERAL_STRING("mozGrabber"), false);
|
|
|
|
if (NS_WARN_IF(!ret)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// add the mouse listener so we can detect a click on a resizer
|
2016-11-24 12:07:47 +03:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> evtTarget = do_QueryInterface(ret);
|
2010-03-06 07:01:28 +03:00
|
|
|
evtTarget->AddEventListener(NS_LITERAL_STRING("mousedown"),
|
2011-10-17 18:59:28 +04:00
|
|
|
mEventListener, false);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2017-08-02 22:37:44 +03:00
|
|
|
return ret;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::RefreshGrabber()
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mAbsolutelyPositionedObject, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
|
|
|
GetPositionAndDimensions(
|
2017-08-07 08:09:51 +03:00
|
|
|
*mAbsolutelyPositionedObject,
|
2016-10-19 12:09:33 +03:00
|
|
|
mPositionedObjectX,
|
|
|
|
mPositionedObjectY,
|
|
|
|
mPositionedObjectWidth,
|
|
|
|
mPositionedObjectHeight,
|
|
|
|
mPositionedObjectBorderLeft,
|
|
|
|
mPositionedObjectBorderTop,
|
|
|
|
mPositionedObjectMarginLeft,
|
|
|
|
mPositionedObjectMarginTop);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
SetAnonymousElementPosition(mPositionedObjectX+12,
|
|
|
|
mPositionedObjectY-14,
|
2016-11-24 09:58:01 +03:00
|
|
|
mGrabber);
|
2003-06-25 12:50:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
void
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::HideGrabber()
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = mAbsolutelyPositionedObject->UnsetAttr(kNameSpaceID_None,
|
|
|
|
nsGkAtoms::_moz_abspos,
|
|
|
|
true);
|
2018-02-01 04:55:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mAbsolutelyPositionedObject = nullptr;
|
2018-02-01 04:55:25 +03:00
|
|
|
if (NS_WARN_IF(!mGrabber)) {
|
|
|
|
return;
|
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// get the presshell's document observer interface.
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-05-06 05:20:43 +04: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.
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2017-08-02 22:37:44 +03:00
|
|
|
DeleteRefToAnonymousNode(Move(mGrabber), ps);
|
|
|
|
DeleteRefToAnonymousNode(Move(mPositioningShadow), ps);
|
2018-01-30 07:42:52 +03:00
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2018-01-30 07:42:52 +03:00
|
|
|
nsresult
|
2018-02-01 04:55:25 +03:00
|
|
|
HTMLEditor::ShowGrabber(Element& aElement)
|
2018-01-30 07:42:52 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!IsDescendantOfEditorRoot(&aElement))) {
|
2017-09-09 17:49:02 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2008-04-14 00:22:31 +04:00
|
|
|
if (mGrabber) {
|
|
|
|
NS_ERROR("call HideGrabber first");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
nsAutoString classValue;
|
2018-01-30 07:42:52 +03:00
|
|
|
nsresult rv =
|
|
|
|
GetTemporaryStyleForFocusedPositionedElement(aElement, classValue);
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2018-01-30 07:42:52 +03:00
|
|
|
rv = aElement.SetAttr(kNameSpaceID_None, nsGkAtoms::_moz_abspos,
|
2016-10-19 12:09:33 +03:00
|
|
|
classValue, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// first, let's keep track of that element...
|
2018-01-30 07:42:52 +03:00
|
|
|
mAbsolutelyPositionedObject = &aElement;
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2018-01-30 07:42:52 +03:00
|
|
|
nsIContent* parentContent = aElement.GetParent();
|
2017-08-07 08:09:51 +03:00
|
|
|
if (NS_WARN_IF(!parentContent)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGrabber = CreateGrabber(*parentContent);
|
2015-04-19 15:28:50 +03:00
|
|
|
NS_ENSURE_TRUE(mGrabber, NS_ERROR_FAILURE);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// and set its position
|
|
|
|
return RefreshGrabber();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::StartMoving(nsIDOMElement* aHandle)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2017-08-07 08:09:51 +03:00
|
|
|
nsCOMPtr<nsIContent> parentContent = mGrabber->GetParent();
|
|
|
|
if (NS_WARN_IF(!parentContent) || NS_WARN_IF(!mAbsolutelyPositionedObject)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// now, let's create the resizing shadow
|
2017-08-07 08:09:51 +03:00
|
|
|
mPositioningShadow =
|
|
|
|
CreateShadow(*parentContent, *mAbsolutelyPositionedObject);
|
2015-04-19 15:28:50 +03:00
|
|
|
NS_ENSURE_TRUE(mPositioningShadow, NS_ERROR_FAILURE);
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = SetShadowPosition(mPositioningShadow,
|
|
|
|
mAbsolutelyPositionedObject,
|
|
|
|
mPositionedObjectX, mPositionedObjectY);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// make the shadow appear
|
2015-04-19 15:28:50 +03:00
|
|
|
mPositioningShadow->UnsetAttr(kNameSpaceID_None, nsGkAtoms::_class, true);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// position it
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->SetCSSPropertyPixels(*mPositioningShadow, *nsGkAtoms::width,
|
2003-09-03 17:58:00 +04:00
|
|
|
mPositionedObjectWidth);
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->SetCSSPropertyPixels(*mPositioningShadow, *nsGkAtoms::height,
|
2003-09-03 17:58:00 +04:00
|
|
|
mPositionedObjectHeight);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsMoving = true;
|
2016-10-19 12:09:33 +03:00
|
|
|
return NS_OK; // XXX Looks like nobody refers this result
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SnapToGrid(int32_t& newX, int32_t& newY)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
if (mSnapToGridEnabled && mGridSize) {
|
2012-08-22 19:56:38 +04:00
|
|
|
newX = (int32_t) floor( ((float)newX / (float)mGridSize) + 0.5f ) * mGridSize;
|
|
|
|
newY = (int32_t) floor( ((float)newY / (float)mGridSize) + 0.5f ) * mGridSize;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GrabberClicked()
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
// add a mouse move listener to the editor
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = NS_OK;
|
2003-06-25 12:50:48 +04:00
|
|
|
if (!mMouseMotionListenerP) {
|
2017-06-22 09:02:59 +03:00
|
|
|
mMouseMotionListenerP = new ResizerMouseMotionListener(*this);
|
2003-06-25 12:50:48 +04:00
|
|
|
if (!mMouseMotionListenerP) {return NS_ERROR_NULL_POINTER;}
|
|
|
|
|
2018-01-09 16:41:42 +03:00
|
|
|
nsIDOMEventTarget* piTarget = GetDOMEventTarget();
|
2007-05-14 13:11:38 +04:00
|
|
|
NS_ENSURE_TRUE(piTarget, NS_ERROR_FAILURE);
|
2005-03-24 22:00:01 +03:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = piTarget->AddEventListener(NS_LITERAL_STRING("mousemove"),
|
2011-06-28 03:34:54 +04:00
|
|
|
mMouseMotionListenerP,
|
2011-10-17 18:59:28 +04:00
|
|
|
false, false);
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
2005-03-24 22:00:01 +03:00
|
|
|
"failed to register mouse motion listener");
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mGrabberClicked = true;
|
2016-10-19 12:09:33 +03:00
|
|
|
return rv;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::EndMoving()
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
if (mPositioningShadow) {
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2017-08-02 22:37:44 +03:00
|
|
|
DeleteRefToAnonymousNode(Move(mPositioningShadow), ps);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mPositioningShadow = nullptr;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
2011-06-24 06:18:01 +04:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> piTarget = GetDOMEventTarget();
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2007-05-14 13:11:38 +04:00
|
|
|
if (piTarget && mMouseMotionListenerP) {
|
2016-10-19 12:09:33 +03:00
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
piTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
|
|
|
|
mMouseMotionListenerP,
|
|
|
|
false);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to remove mouse motion listener");
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mMouseMotionListenerP = nullptr;
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mGrabberClicked = false;
|
|
|
|
mIsMoving = false;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2007-06-14 12:22:47 +04:00
|
|
|
if (!selection) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
return CheckSelectionStateForAnonymousButtons(selection);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SetFinalPosition(int32_t aX,
|
|
|
|
int32_t aY)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = EndMoving();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// we have now to set the new width and height of the resized object
|
|
|
|
// we don't set the x and y position because we don't control that in
|
|
|
|
// a normal HTML layout
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newX = mPositionedObjectX + aX - mOriginalX - (mPositionedObjectBorderLeft+mPositionedObjectMarginLeft);
|
|
|
|
int32_t newY = mPositionedObjectY + aY - mOriginalY - (mPositionedObjectBorderTop+mPositionedObjectMarginTop);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
SnapToGrid(newX, newY);
|
|
|
|
|
|
|
|
nsAutoString x, y;
|
|
|
|
x.AppendInt(newX);
|
|
|
|
y.AppendInt(newY);
|
|
|
|
|
|
|
|
// we want one transaction only from a user's point of view
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch batchIt(this);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2014-08-29 15:43:24 +04:00
|
|
|
nsCOMPtr<Element> absolutelyPositionedObject =
|
|
|
|
do_QueryInterface(mAbsolutelyPositionedObject);
|
|
|
|
NS_ENSURE_STATE(absolutelyPositionedObject);
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->SetCSSPropertyPixels(*absolutelyPositionedObject,
|
2014-08-29 15:43:24 +04:00
|
|
|
*nsGkAtoms::top, newY);
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->SetCSSPropertyPixels(*absolutelyPositionedObject,
|
2014-08-29 15:43:24 +04:00
|
|
|
*nsGkAtoms::left, newX);
|
2003-06-25 12:50:48 +04:00
|
|
|
// keep track of that size
|
|
|
|
mPositionedObjectX = newX;
|
|
|
|
mPositionedObjectY = newY;
|
|
|
|
|
|
|
|
return RefreshResizers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::AddPositioningOffset(int32_t& aX,
|
|
|
|
int32_t& aY)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
// Get the positioning offset
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t positioningOffset =
|
2011-06-17 04:59:29 +04:00
|
|
|
Preferences::GetInt("editor.positioning.offset", 0);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
aX += positioningOffset;
|
|
|
|
aY += positioningOffset;
|
|
|
|
}
|
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
nsresult
|
|
|
|
HTMLEditor::SetPositionToAbsoluteOrStatic(Element& aElement,
|
|
|
|
bool aEnabled)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
nsAutoString positionStr;
|
2018-02-02 12:42:25 +03:00
|
|
|
CSSEditUtils::GetComputedProperty(aElement, *nsGkAtoms::position,
|
|
|
|
positionStr);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isPositioned = (positionStr.EqualsLiteral("absolute"));
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
// nothing to do if the element is already in the state we want
|
2018-02-01 04:55:25 +03:00
|
|
|
if (isPositioned == aEnabled) {
|
2003-06-25 12:50:48 +04:00
|
|
|
return NS_OK;
|
2018-02-01 04:55:25 +03:00
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
if (aEnabled) {
|
|
|
|
return SetPositionToAbsolute(aElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SetPositionToStatic(aElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HTMLEditor::SetPositionToAbsolute(Element& aElement)
|
|
|
|
{
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch batchIt(this);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
int32_t x, y;
|
|
|
|
GetElementOrigin(aElement, x, y);
|
|
|
|
|
|
|
|
mCSSEditUtils->SetCSSProperty(aElement, *nsGkAtoms::position,
|
|
|
|
NS_LITERAL_STRING("absolute"));
|
|
|
|
|
|
|
|
AddPositioningOffset(x, y);
|
|
|
|
SnapToGrid(x, y);
|
|
|
|
SetTopAndLeft(aElement, x, y);
|
|
|
|
|
|
|
|
// we may need to create a br if the positioned element is alone in its
|
|
|
|
// container
|
|
|
|
nsINode* parentNode = aElement.GetParentNode();
|
|
|
|
if (parentNode->GetChildCount() == 1) {
|
|
|
|
RefPtr<Selection> selection = GetSelection();
|
|
|
|
if (NS_WARN_IF(!selection)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
RefPtr<Element> newBRElement =
|
|
|
|
CreateBRImpl(*selection, EditorRawDOMPoint(parentNode, 0), eNone);
|
|
|
|
if (NS_WARN_IF(!newBRElement)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 04:55:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HTMLEditor::SetPositionToStatic(Element& aElement)
|
|
|
|
{
|
|
|
|
AutoPlaceholderBatch batchIt(this);
|
|
|
|
|
|
|
|
mCSSEditUtils->RemoveCSSProperty(aElement, *nsGkAtoms::position,
|
|
|
|
EmptyString());
|
|
|
|
mCSSEditUtils->RemoveCSSProperty(aElement, *nsGkAtoms::top,
|
|
|
|
EmptyString());
|
|
|
|
mCSSEditUtils->RemoveCSSProperty(aElement, *nsGkAtoms::left,
|
|
|
|
EmptyString());
|
|
|
|
mCSSEditUtils->RemoveCSSProperty(aElement, *nsGkAtoms::z_index,
|
|
|
|
EmptyString());
|
|
|
|
|
|
|
|
if (!HTMLEditUtils::IsImage(&aElement)) {
|
|
|
|
mCSSEditUtils->RemoveCSSProperty(aElement, *nsGkAtoms::width,
|
2014-08-29 15:43:24 +04:00
|
|
|
EmptyString());
|
2018-02-01 04:55:25 +03:00
|
|
|
mCSSEditUtils->RemoveCSSProperty(aElement, *nsGkAtoms::height,
|
2014-08-29 15:43:24 +04:00
|
|
|
EmptyString());
|
2018-02-01 04:55:25 +03:00
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2018-02-01 04:55:25 +03:00
|
|
|
if (aElement.IsHTMLElement(nsGkAtoms::div) &&
|
|
|
|
!HasStyleOrIdOrClass(&aElement)) {
|
|
|
|
RefPtr<HTMLEditRules> htmlRules =
|
|
|
|
static_cast<HTMLEditRules*>(mRules.get());
|
|
|
|
NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE);
|
|
|
|
nsresult rv = htmlRules->MakeSureElemStartsOrEndsOnCR(aElement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = RemoveContainer(&aElement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
return NS_OK;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SetSnapToGridEnabled(bool aEnabled)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
mSnapToGridEnabled = aEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GetSnapToGridEnabled(bool* aIsEnabled)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
*aIsEnabled = mSnapToGridEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SetGridSize(uint32_t aSize)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
mGridSize = aSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GetGridSize(uint32_t* aSize)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
*aSize = mGridSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// self-explanatory
|
2015-05-13 18:58:25 +03:00
|
|
|
void
|
2018-02-01 04:55:25 +03:00
|
|
|
HTMLEditor::SetTopAndLeft(Element& aElement,
|
|
|
|
int32_t aX,
|
|
|
|
int32_t aY)
|
2015-05-13 18:58:25 +03:00
|
|
|
{
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch batchIt(this);
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->SetCSSPropertyPixels(aElement, *nsGkAtoms::left, aX);
|
|
|
|
mCSSEditUtils->SetCSSPropertyPixels(aElement, *nsGkAtoms::top, aY);
|
2015-05-13 18:58:25 +03:00
|
|
|
}
|
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
// self-explanatory
|
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GetPositionedElement(nsIDOMElement** aReturn)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2015-04-19 15:28:50 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> ret =
|
2017-08-04 12:30:13 +03:00
|
|
|
static_cast<nsIDOMElement*>(GetAsDOMNode(GetPositionedElement()));
|
2015-04-19 15:28:50 +03:00
|
|
|
ret.forget(aReturn);
|
2003-06-25 12:50:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2018-01-30 07:42:52 +03:00
|
|
|
HTMLEditor::GetTemporaryStyleForFocusedPositionedElement(Element& aElement,
|
|
|
|
nsAString& aReturn)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
// we are going to outline the positioned element and bring it to the
|
|
|
|
// front to overlap any other element intersecting with it. But
|
|
|
|
// first, let's see what's the background and foreground colors of the
|
|
|
|
// positioned element.
|
|
|
|
// if background-image computed value is 'none,
|
|
|
|
// If the background color is 'auto' and R G B values of the foreground are
|
|
|
|
// each above #d0, use a black background
|
|
|
|
// If the background color is 'auto' and at least one of R G B values of
|
|
|
|
// the foreground is below #d0, use a white background
|
|
|
|
// Otherwise don't change background/foreground
|
|
|
|
aReturn.Truncate();
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
nsAutoString bgImageStr;
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
2018-02-02 12:42:25 +03:00
|
|
|
CSSEditUtils::GetComputedProperty(aElement, *nsGkAtoms::background_image,
|
|
|
|
bgImageStr);
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-01-30 07:42:52 +03:00
|
|
|
if (!bgImageStr.EqualsLiteral("none")) {
|
|
|
|
return NS_OK;
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:42:52 +03:00
|
|
|
nsAutoString bgColorStr;
|
|
|
|
rv =
|
2018-02-02 12:42:25 +03:00
|
|
|
CSSEditUtils::GetComputedProperty(aElement, *nsGkAtoms::backgroundColor,
|
|
|
|
bgColorStr);
|
2018-01-30 07:42:52 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-03-15 14:57:38 +03:00
|
|
|
if (!bgColorStr.EqualsLiteral("rgba(0, 0, 0, 0)")) {
|
2018-01-30 07:42:52 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-15 14:57:38 +03:00
|
|
|
RefPtr<nsStyleContext> style =
|
|
|
|
nsComputedDOMStyle::GetStyleContext(&aElement, nullptr);
|
|
|
|
NS_ENSURE_STATE(style);
|
2018-01-30 07:42:52 +03:00
|
|
|
|
2018-03-15 14:57:38 +03:00
|
|
|
const uint8_t kBlackBgTrigger = 0xd0;
|
2018-01-30 07:42:52 +03:00
|
|
|
|
2018-03-15 14:57:38 +03:00
|
|
|
nscolor color = style->StyleColor()->mColor;
|
|
|
|
if (NS_GET_R(color) >= kBlackBgTrigger &&
|
|
|
|
NS_GET_G(color) >= kBlackBgTrigger &&
|
|
|
|
NS_GET_B(color) >= kBlackBgTrigger) {
|
2018-01-30 07:42:52 +03:00
|
|
|
aReturn.AssignLiteral("black");
|
|
|
|
} else {
|
|
|
|
aReturn.AssignLiteral("white");
|
|
|
|
}
|
2018-03-15 14:57:38 +03:00
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-07-09 05:42:33 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|