2002-01-09 16:51:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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/. */
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2016-07-08 08:03:31 +03:00
|
|
|
#include "mozilla/CSSEditUtils.h"
|
2014-12-02 08:07:42 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2016-07-08 08:03:31 +03:00
|
|
|
#include "mozilla/ChangeStyleTransaction.h"
|
2016-07-09 05:42:33 +03:00
|
|
|
#include "mozilla/HTMLEditor.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-10-18 07:29:03 +03:00
|
|
|
#include "mozilla/DeclarationBlockInlines.h"
|
2012-07-08 14:31:53 +04:00
|
|
|
#include "mozilla/css/StyleRule.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/mozalloc.h"
|
|
|
|
#include "nsAString.h"
|
2002-01-09 16:51:37 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsColor.h"
|
2012-07-08 13:50:31 +04:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsDependentSubstring.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMCSSStyleDeclaration.h"
|
2002-01-09 16:51:37 +03:00
|
|
|
#include "nsIDOMElement.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2003-07-14 11:37:39 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsStringIterator.h"
|
2016-08-02 21:05:38 +03:00
|
|
|
#include "nsStyledElement.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsSubstringTuple.h"
|
2002-01-09 16:51:37 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
2011-06-17 04:59:29 +04:00
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessBValue(const nsAString* aInputString,
|
|
|
|
nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aInputString && aInputString->EqualsLiteral("-moz-editor-invert-value")) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AssignLiteral("normal");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AssignLiteral("bold");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessDefaultValue(const nsAString* aInputString,
|
|
|
|
nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2004-01-30 01:04:45 +03:00
|
|
|
CopyASCIItoUTF16(aDefaultValueString, aOutputString);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessSameValue(const nsAString* aInputString,
|
|
|
|
nsAString & aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
if (aInputString) {
|
|
|
|
aOutputString.Assign(*aInputString);
|
|
|
|
}
|
2003-09-03 17:54:36 +04:00
|
|
|
else
|
|
|
|
aOutputString.Truncate();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessExtendedValue(const nsAString* aInputString,
|
|
|
|
nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
if (aPrependString) {
|
2003-11-01 13:57:41 +03:00
|
|
|
AppendASCIItoUTF16(aPrependString, aOutputString);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2002-01-21 12:30:02 +03:00
|
|
|
aOutputString.Append(*aInputString);
|
2002-01-09 16:51:37 +03:00
|
|
|
if (aAppendString) {
|
2003-11-01 13:57:41 +03:00
|
|
|
AppendASCIItoUTF16(aAppendString, aOutputString);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessLengthValue(const nsAString* aInputString,
|
|
|
|
nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
aOutputString.Append(*aInputString);
|
2014-01-04 19:02:17 +04:00
|
|
|
if (-1 == aOutputString.FindChar(char16_t('%'))) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AppendLiteral("px");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessListStyleTypeValue(const nsAString* aInputString,
|
|
|
|
nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aInputString->EqualsLiteral("1")) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AppendLiteral("decimal");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
else if (aInputString->EqualsLiteral("a")) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AppendLiteral("lower-alpha");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
else if (aInputString->EqualsLiteral("A")) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AppendLiteral("upper-alpha");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
else if (aInputString->EqualsLiteral("i")) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AppendLiteral("lower-roman");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
else if (aInputString->EqualsLiteral("I")) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutputString.AppendLiteral("upper-roman");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
else if (aInputString->EqualsLiteral("square")
|
|
|
|
|| aInputString->EqualsLiteral("circle")
|
|
|
|
|| aInputString->EqualsLiteral("disc")) {
|
2002-01-09 16:51:37 +03:00
|
|
|
aOutputString.Append(*aInputString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessMarginLeftValue(const nsAString* aInputString,
|
|
|
|
nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aInputString->EqualsLiteral("center") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-center")) {
|
2015-05-28 18:58:42 +03:00
|
|
|
aOutputString.AppendLiteral("auto");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
else if (aInputString->EqualsLiteral("right") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-right")) {
|
2015-05-28 18:58:42 +03:00
|
|
|
aOutputString.AppendLiteral("auto");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-05-28 18:58:42 +03:00
|
|
|
aOutputString.AppendLiteral("0px");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-07-07 12:35:13 +03:00
|
|
|
void ProcessMarginRightValue(const nsAString* aInputString,
|
|
|
|
nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString,
|
|
|
|
const char* aAppendString)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aInputString->EqualsLiteral("center") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-center")) {
|
2015-05-28 18:58:42 +03:00
|
|
|
aOutputString.AppendLiteral("auto");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-05-23 02:15:22 +04:00
|
|
|
else if (aInputString->EqualsLiteral("left") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-left")) {
|
2015-05-28 18:58:42 +03:00
|
|
|
aOutputString.AppendLiteral("auto");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-05-28 18:58:42 +03:00
|
|
|
aOutputString.AppendLiteral("0px");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable boldEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_font_weight, ProcessBValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable italicEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_font_style, ProcessDefaultValue, "italic", nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable underlineEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_text_decoration, ProcessDefaultValue, "underline", nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable strikeEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_text_decoration, ProcessDefaultValue, "line-through", nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable ttEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_font_family, ProcessDefaultValue, "monospace", nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable fontColorEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_color, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable fontFaceEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_font_family, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable bgcolorEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_background_color, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable backgroundImageEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_background_image, ProcessExtendedValue, nullptr, "url(", ")", true, true },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable textColorEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_color, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable borderEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_border, ProcessExtendedValue, nullptr, nullptr, "px solid", true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable textAlignEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_text_align, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable captionAlignEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_caption_side, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable verticalAlignEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_vertical_align, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable nowrapEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_whitespace, ProcessDefaultValue, "nowrap", nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable widthEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_width, ProcessLengthValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable heightEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_height, ProcessLengthValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable listStyleTypeEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_list_style_type, ProcessListStyleTypeValue, nullptr, nullptr, nullptr, true, true },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable tableAlignEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_text_align, ProcessDefaultValue, "left", nullptr, nullptr, false, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_margin_left, ProcessMarginLeftValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_margin_right, ProcessMarginRightValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable hrAlignEquivTable[] = {
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_margin_left, ProcessMarginLeftValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_margin_right, ProcessMarginRightValue, nullptr, nullptr, nullptr, true, false },
|
|
|
|
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
|
2002-01-09 16:51:37 +03:00
|
|
|
};
|
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
CSSEditUtils::CSSEditUtils(HTMLEditor* aHTMLEditor)
|
2016-07-07 12:35:13 +03:00
|
|
|
: mHTMLEditor(aHTMLEditor)
|
2012-04-03 18:16:05 +04:00
|
|
|
, mIsCSSPrefChecked(true)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
// let's retrieve the value of the "CSS editing" pref
|
2011-06-17 04:59:29 +04:00
|
|
|
mIsCSSPrefChecked = Preferences::GetBool("editor.use_css", mIsCSSPrefChecked);
|
2011-05-15 14:07:30 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::~CSSEditUtils()
|
2011-05-15 14:07:30 +04:00
|
|
|
{
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Answers true if we have some CSS equivalence for the HTML style defined
|
|
|
|
// by aProperty and/or aAttribute for the node aNode
|
2012-02-10 14:04:47 +04:00
|
|
|
bool
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::IsCSSEditableProperty(nsINode* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute)
|
2012-02-10 14:04:47 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
nsINode* node = aNode;
|
2002-01-09 16:51:37 +03:00
|
|
|
// we need an element node here
|
2015-04-19 15:28:50 +03:00
|
|
|
if (node->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
node = node->GetParentNode();
|
|
|
|
NS_ENSURE_TRUE(node, false);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2003-07-29 18:48:44 +04:00
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
// html inline styles B I TT U STRIKE and COLOR/FACE on FONT
|
2014-08-29 15:43:24 +04:00
|
|
|
if (nsGkAtoms::b == aProperty ||
|
|
|
|
nsGkAtoms::i == aProperty ||
|
|
|
|
nsGkAtoms::tt == aProperty ||
|
|
|
|
nsGkAtoms::u == aProperty ||
|
|
|
|
nsGkAtoms::strike == aProperty ||
|
|
|
|
(nsGkAtoms::font == aProperty && aAttribute &&
|
|
|
|
(aAttribute->EqualsLiteral("color") ||
|
|
|
|
aAttribute->EqualsLiteral("face")))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ALIGN attribute on elements supporting it
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aAttribute && (aAttribute->EqualsLiteral("align")) &&
|
2015-04-19 15:28:50 +03:00
|
|
|
node->IsAnyOfHTMLElements(nsGkAtoms::div,
|
|
|
|
nsGkAtoms::p,
|
|
|
|
nsGkAtoms::h1,
|
|
|
|
nsGkAtoms::h2,
|
|
|
|
nsGkAtoms::h3,
|
|
|
|
nsGkAtoms::h4,
|
|
|
|
nsGkAtoms::h5,
|
|
|
|
nsGkAtoms::h6,
|
|
|
|
nsGkAtoms::td,
|
|
|
|
nsGkAtoms::th,
|
|
|
|
nsGkAtoms::table,
|
|
|
|
nsGkAtoms::hr,
|
|
|
|
// For the above, why not use
|
2016-07-07 08:01:12 +03:00
|
|
|
// HTMLEditUtils::SupportsAlignAttr?
|
2015-04-19 15:28:50 +03:00
|
|
|
// It also checks for tbody, tfoot, thead.
|
|
|
|
// Let's add the following elements here even
|
|
|
|
// if "align" has a different meaning for them
|
|
|
|
nsGkAtoms::legend,
|
|
|
|
nsGkAtoms::caption)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aAttribute && (aAttribute->EqualsLiteral("valign")) &&
|
2015-04-19 15:28:50 +03:00
|
|
|
node->IsAnyOfHTMLElements(nsGkAtoms::col,
|
|
|
|
nsGkAtoms::colgroup,
|
|
|
|
nsGkAtoms::tbody,
|
|
|
|
nsGkAtoms::td,
|
|
|
|
nsGkAtoms::th,
|
|
|
|
nsGkAtoms::tfoot,
|
|
|
|
nsGkAtoms::thead,
|
|
|
|
nsGkAtoms::tr)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-09-17 16:04:59 +04:00
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
// attributes TEXT, BACKGROUND and BGCOLOR on BODY
|
2015-04-19 15:28:50 +03:00
|
|
|
if (aAttribute && node->IsHTMLElement(nsGkAtoms::body) &&
|
2004-05-23 02:15:22 +04:00
|
|
|
(aAttribute->EqualsLiteral("text")
|
|
|
|
|| aAttribute->EqualsLiteral("background")
|
|
|
|
|| aAttribute->EqualsLiteral("bgcolor"))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// attribute BGCOLOR on other elements
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aAttribute && aAttribute->EqualsLiteral("bgcolor")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// attributes HEIGHT, WIDTH and NOWRAP on TD and TH
|
2015-03-03 14:08:59 +03:00
|
|
|
if (aAttribute &&
|
2015-04-19 15:28:50 +03:00
|
|
|
node->IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th) &&
|
2004-05-23 02:15:22 +04:00
|
|
|
(aAttribute->EqualsLiteral("height")
|
|
|
|
|| aAttribute->EqualsLiteral("width")
|
|
|
|
|| aAttribute->EqualsLiteral("nowrap"))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// attributes HEIGHT and WIDTH on TABLE
|
2015-04-19 15:28:50 +03:00
|
|
|
if (aAttribute && node->IsHTMLElement(nsGkAtoms::table) &&
|
2004-05-23 02:15:22 +04:00
|
|
|
(aAttribute->EqualsLiteral("height")
|
|
|
|
|| aAttribute->EqualsLiteral("width"))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// attributes SIZE and WIDTH on HR
|
2015-04-19 15:28:50 +03:00
|
|
|
if (aAttribute && node->IsHTMLElement(nsGkAtoms::hr) &&
|
2004-05-23 02:15:22 +04:00
|
|
|
(aAttribute->EqualsLiteral("size")
|
|
|
|
|| aAttribute->EqualsLiteral("width"))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// attribute TYPE on OL UL LI
|
2014-08-29 15:43:24 +04:00
|
|
|
if (aAttribute &&
|
2015-04-19 15:28:50 +03:00
|
|
|
node->IsAnyOfHTMLElements(nsGkAtoms::ol, nsGkAtoms::ul,
|
|
|
|
nsGkAtoms::li) &&
|
2015-03-03 14:08:59 +03:00
|
|
|
aAttribute->EqualsLiteral("type")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
if (aAttribute && node->IsHTMLElement(nsGkAtoms::img) &&
|
2004-05-23 02:15:22 +04:00
|
|
|
(aAttribute->EqualsLiteral("border")
|
|
|
|
|| aAttribute->EqualsLiteral("width")
|
|
|
|
|| aAttribute->EqualsLiteral("height"))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// other elements that we can align using CSS even if they
|
|
|
|
// can't carry the html ALIGN attribute
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aAttribute && aAttribute->EqualsLiteral("align") &&
|
2015-04-19 15:28:50 +03:00
|
|
|
node->IsAnyOfHTMLElements(nsGkAtoms::ul,
|
|
|
|
nsGkAtoms::ol,
|
|
|
|
nsGkAtoms::dl,
|
|
|
|
nsGkAtoms::li,
|
|
|
|
nsGkAtoms::dd,
|
|
|
|
nsGkAtoms::dt,
|
|
|
|
nsGkAtoms::address,
|
|
|
|
nsGkAtoms::pre)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2014-08-29 15:43:24 +04:00
|
|
|
// The lowest level above the transaction; adds the CSS declaration
|
|
|
|
// "aProperty : aValue" to the inline styles carried by aElement
|
2002-01-09 16:51:37 +03:00
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::SetCSSProperty(Element& aElement,
|
|
|
|
nsIAtom& aProperty,
|
|
|
|
const nsAString& aValue,
|
|
|
|
bool aSuppressTxn)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2016-07-07 08:23:25 +03:00
|
|
|
RefPtr<ChangeStyleTransaction> transaction =
|
|
|
|
CreateCSSPropertyTxn(aElement, aProperty, aValue,
|
|
|
|
ChangeStyleTransaction::eSet);
|
2014-08-29 15:43:24 +04:00
|
|
|
if (aSuppressTxn) {
|
2016-07-07 08:23:25 +03:00
|
|
|
return transaction->DoTransaction();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2016-07-07 08:23:25 +03:00
|
|
|
return mHTMLEditor->DoTransaction(transaction);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2003-09-03 17:54:36 +04:00
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::SetCSSPropertyPixels(Element& aElement,
|
|
|
|
nsIAtom& aProperty,
|
|
|
|
int32_t aIntValue)
|
2003-09-03 17:54:36 +04:00
|
|
|
{
|
|
|
|
nsAutoString s;
|
|
|
|
s.AppendInt(aIntValue);
|
|
|
|
return SetCSSProperty(aElement, aProperty, s + NS_LITERAL_STRING("px"),
|
2014-08-29 15:43:24 +04:00
|
|
|
/* suppress txn */ false);
|
2003-09-03 17:54:36 +04:00
|
|
|
}
|
|
|
|
|
2014-08-29 15:43:24 +04:00
|
|
|
// The lowest level above the transaction; removes the value aValue from the
|
|
|
|
// list of values specified for the CSS property aProperty, or totally remove
|
|
|
|
// the declaration if this property accepts only one value
|
2002-01-09 16:51:37 +03:00
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::RemoveCSSProperty(Element& aElement,
|
|
|
|
nsIAtom& aProperty,
|
|
|
|
const nsAString& aValue,
|
|
|
|
bool aSuppressTxn)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2016-07-07 08:23:25 +03:00
|
|
|
RefPtr<ChangeStyleTransaction> transaction =
|
|
|
|
CreateCSSPropertyTxn(aElement, aProperty, aValue,
|
|
|
|
ChangeStyleTransaction::eRemove);
|
2014-08-29 15:43:24 +04:00
|
|
|
if (aSuppressTxn) {
|
2016-07-07 08:23:25 +03:00
|
|
|
return transaction->DoTransaction();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2016-07-07 08:23:25 +03:00
|
|
|
return mHTMLEditor->DoTransaction(transaction);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 08:23:25 +03:00
|
|
|
already_AddRefed<ChangeStyleTransaction>
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::CreateCSSPropertyTxn(
|
|
|
|
Element& aElement,
|
|
|
|
nsIAtom& aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
ChangeStyleTransaction::EChangeType aChangeType)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2016-07-07 08:23:25 +03:00
|
|
|
RefPtr<ChangeStyleTransaction> transaction =
|
|
|
|
new ChangeStyleTransaction(aElement, aAttribute, aValue, aChangeType);
|
|
|
|
return transaction.forget();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetSpecifiedProperty(nsINode& aNode,
|
|
|
|
nsIAtom& aProperty,
|
|
|
|
nsAString& aValue)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2015-04-19 15:28:50 +03:00
|
|
|
return GetCSSInlinePropertyBase(&aNode, &aProperty, aValue, eSpecified);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetComputedProperty(nsINode& aNode,
|
|
|
|
nsIAtom& aProperty,
|
|
|
|
nsAString& aValue)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2015-04-19 15:28:50 +03:00
|
|
|
return GetCSSInlinePropertyBase(&aNode, &aProperty, aValue, eComputed);
|
2012-02-10 14:04:47 +04:00
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetCSSInlinePropertyBase(nsINode* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
nsAString& aValue,
|
|
|
|
StyleType aStyleType)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2012-07-08 14:31:53 +04:00
|
|
|
MOZ_ASSERT(aNode && aProperty);
|
2002-01-09 16:51:37 +03:00
|
|
|
aValue.Truncate();
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
nsCOMPtr<Element> element = GetElementContainerOrSelf(aNode);
|
2012-02-01 14:54:22 +04:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_NULL_POINTER);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2012-07-08 14:31:53 +04:00
|
|
|
if (aStyleType == eComputed) {
|
|
|
|
// Get the all the computed css styles attached to the element node
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsComputedDOMStyle> cssDecl = GetComputedStyle(element);
|
2012-07-08 14:31:53 +04:00
|
|
|
NS_ENSURE_STATE(cssDecl);
|
|
|
|
|
|
|
|
// from these declarations, get the one we want and that one only
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
cssDecl->GetPropertyValue(nsDependentAtomString(aProperty), aValue));
|
2012-07-08 14:31:53 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2012-07-08 14:31:53 +04:00
|
|
|
}
|
2012-07-08 14:31:53 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(aStyleType == eSpecified);
|
2016-10-18 07:29:03 +03:00
|
|
|
RefPtr<DeclarationBlock> decl = element->GetInlineStyleDeclaration();
|
2015-11-09 10:57:16 +03:00
|
|
|
if (!decl) {
|
2012-07-08 14:31:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-10-18 07:29:03 +03:00
|
|
|
if (decl->IsServo()) {
|
|
|
|
MOZ_CRASH("stylo: not implemented");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2016-08-17 04:37:48 +03:00
|
|
|
nsCSSPropertyID prop =
|
2012-07-08 14:31:53 +04:00
|
|
|
nsCSSProps::LookupProperty(nsDependentAtomString(aProperty),
|
2016-05-10 11:44:05 +03:00
|
|
|
CSSEnabledState::eForAllContent);
|
2012-07-08 14:31:53 +04:00
|
|
|
MOZ_ASSERT(prop != eCSSProperty_UNKNOWN);
|
2016-11-03 06:41:02 +03:00
|
|
|
decl->AsGecko()->GetPropertyValueByID(prop, aValue);
|
2012-07-08 14:31:53 +04:00
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-08 13:50:31 +04:00
|
|
|
already_AddRefed<nsComputedDOMStyle>
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetComputedStyle(Element* aElement)
|
2012-02-10 14:04:47 +04:00
|
|
|
{
|
2012-07-08 13:50:31 +04:00
|
|
|
MOZ_ASSERT(aElement);
|
2012-07-08 13:50:31 +04:00
|
|
|
|
2016-03-31 14:46:32 +03:00
|
|
|
nsIDocument* doc = aElement->GetUncomposedDoc();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(doc, nullptr);
|
2012-07-08 13:50:31 +04:00
|
|
|
|
|
|
|
nsIPresShell* presShell = doc->GetShell();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(presShell, nullptr);
|
2012-07-08 13:50:31 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsComputedDOMStyle> style =
|
2012-07-08 13:50:31 +04:00
|
|
|
NS_NewComputedDOMStyle(aElement, EmptyString(), presShell);
|
|
|
|
|
|
|
|
return style.forget();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the CSS style "aProperty : aPropertyValue" and possibly remove the whole node
|
|
|
|
// if it is a span and if its only attribute is _moz_dirty
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::RemoveCSSInlineStyle(nsIDOMNode* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString& aPropertyValue)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2014-08-29 15:43:24 +04:00
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_STATE(element);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// remove the property from the style attribute
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveCSSProperty(*element, *aProperty, aPropertyValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2015-03-03 14:08:59 +03:00
|
|
|
if (!element->IsHTMLElement(nsGkAtoms::span) ||
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::HasAttributes(element)) {
|
2012-02-10 14:04:46 +04:00
|
|
|
return NS_OK;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2012-02-10 14:04:46 +04:00
|
|
|
|
2014-08-20 16:25:16 +04:00
|
|
|
return mHTMLEditor->RemoveContainer(element);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Answers true if the property can be removed by setting a "none" CSS value
|
2002-01-09 16:51:37 +03:00
|
|
|
// on a node
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::IsCSSInvertible(nsIAtom& aProperty,
|
|
|
|
const nsAString* aAttribute)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2015-04-19 15:28:50 +03:00
|
|
|
return nsGkAtoms::b == &aProperty;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default browser background color if we need it for GetCSSBackgroundColorState
|
2011-06-17 04:59:29 +04:00
|
|
|
void
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetDefaultBackgroundColor(nsAString& aColor)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
if (Preferences::GetBool("editor.use_custom_colors", false)) {
|
2011-06-17 04:59:29 +04:00
|
|
|
nsresult rv = Preferences::GetString("editor.background_color", &aColor);
|
|
|
|
// XXX Why don't you validate the pref value?
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to get editor.background_color");
|
|
|
|
aColor.AssignLiteral("#ffffff"); // Default to white
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2011-06-17 04:59:29 +04:00
|
|
|
return;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2011-06-17 04:59:29 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
if (Preferences::GetBool("browser.display.use_system_colors", false)) {
|
2011-06-17 04:59:29 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
Preferences::GetString("browser.display.background_color", &aColor);
|
|
|
|
// XXX Why don't you validate the pref value?
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to get browser.display.background_color");
|
|
|
|
aColor.AssignLiteral("#ffffff"); // Default to white
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default length unit used for CSS Indent/Outdent
|
2011-06-17 04:59:29 +04:00
|
|
|
void
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetDefaultLengthUnit(nsAString& aLengthUnit)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2011-06-17 04:59:29 +04:00
|
|
|
nsresult rv =
|
|
|
|
Preferences::GetString("editor.css.default_length_unit", &aLengthUnit);
|
|
|
|
// XXX Why don't you validate the pref value?
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aLengthUnit.AssignLiteral("px");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 19:08:50 +03:00
|
|
|
// Unfortunately, CSSStyleDeclaration::GetPropertyCSSValue is not yet
|
|
|
|
// implemented... We need then a way to determine the number part and the unit
|
|
|
|
// from aString, aString being the result of a GetPropertyValue query...
|
2002-01-09 16:51:37 +03:00
|
|
|
void
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::ParseLength(const nsAString& aString,
|
|
|
|
float* aValue,
|
|
|
|
nsIAtom** aUnit)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2015-06-15 19:08:50 +03:00
|
|
|
if (aString.IsEmpty()) {
|
|
|
|
*aValue = 0;
|
2016-03-29 02:09:43 +03:00
|
|
|
*aUnit = NS_Atomize(aString).take();
|
2015-06-15 19:08:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
nsAString::const_iterator iter;
|
|
|
|
aString.BeginReading(iter);
|
|
|
|
|
|
|
|
float a = 10.0f , b = 1.0f, value = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
int8_t sign = 1;
|
|
|
|
int32_t i = 0, j = aString.Length();
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool floatingPointFound = false;
|
2002-01-09 16:51:37 +03:00
|
|
|
c = *iter;
|
2014-01-04 19:02:17 +04:00
|
|
|
if (char16_t('-') == c) { sign = -1; iter++; i++; }
|
|
|
|
else if (char16_t('+') == c) { iter++; i++; }
|
2002-01-09 16:51:37 +03:00
|
|
|
while (i < j) {
|
|
|
|
c = *iter;
|
2014-01-04 19:02:17 +04:00
|
|
|
if ((char16_t('0') == c) ||
|
|
|
|
(char16_t('1') == c) ||
|
|
|
|
(char16_t('2') == c) ||
|
|
|
|
(char16_t('3') == c) ||
|
|
|
|
(char16_t('4') == c) ||
|
|
|
|
(char16_t('5') == c) ||
|
|
|
|
(char16_t('6') == c) ||
|
|
|
|
(char16_t('7') == c) ||
|
|
|
|
(char16_t('8') == c) ||
|
|
|
|
(char16_t('9') == c)) {
|
|
|
|
value = (value * a) + (b * (c - char16_t('0')));
|
2002-01-09 16:51:37 +03:00
|
|
|
b = b / 10 * a;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
else if (!floatingPointFound && (char16_t('.') == c)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
floatingPointFound = true;
|
2002-01-09 16:51:37 +03:00
|
|
|
a = 1.0f; b = 0.1f;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
iter++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
*aValue = value * sign;
|
2016-03-29 02:09:43 +03:00
|
|
|
*aUnit = NS_Atomize(StringTail(aString, j-i)).take();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetCSSPropertyAtom(nsCSSEditableProperty aProperty,
|
|
|
|
nsIAtom** aAtom)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aAtom = nullptr;
|
2009-07-23 02:31:01 +04:00
|
|
|
switch (aProperty) {
|
|
|
|
case eCSSEditableProperty_background_color:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::backgroundColor;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_background_image:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::background_image;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_border:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::border;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_caption_side:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::caption_side;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_color:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::color;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_float:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::_float;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_family:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::font_family;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_size:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::font_size;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_style:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::font_style;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_weight:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::fontWeight;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_height:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::height;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_list_style_type:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::list_style_type;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_margin_left:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::marginLeft;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_margin_right:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::marginRight;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_text_align:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::textAlign;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_text_decoration:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::text_decoration;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_vertical_align:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::vertical_align;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_whitespace:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::white_space;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_width:
|
2014-08-29 15:43:24 +04:00
|
|
|
*aAtom = nsGkAtoms::width;
|
2009-07-23 02:31:01 +04:00
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_NONE:
|
|
|
|
// intentionally empty
|
|
|
|
break;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate aProperty and aValueArray with the CSS declarations equivalent to the
|
|
|
|
// value aValue according to the equivalence table aEquivTable
|
|
|
|
void
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::BuildCSSDeclarations(nsTArray<nsIAtom*>& aPropertyArray,
|
|
|
|
nsTArray<nsString>& aValueArray,
|
|
|
|
const CSSEquivTable* aEquivTable,
|
|
|
|
const nsAString* aValue,
|
|
|
|
bool aGetOrRemoveRequest)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
// clear arrays
|
|
|
|
aPropertyArray.Clear();
|
|
|
|
aValueArray.Clear();
|
|
|
|
|
|
|
|
// if we have an input value, let's use it
|
2002-08-23 13:24:47 +04:00
|
|
|
nsAutoString value, lowerCasedValue;
|
2002-01-09 16:51:37 +03:00
|
|
|
if (aValue) {
|
|
|
|
value.Assign(*aValue);
|
2002-08-23 13:24:47 +04:00
|
|
|
lowerCasedValue.Assign(*aValue);
|
|
|
|
ToLowerCase(lowerCasedValue);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int8_t index = 0;
|
2002-01-09 16:51:37 +03:00
|
|
|
nsCSSEditableProperty cssProperty = aEquivTable[index].cssProperty;
|
|
|
|
while (cssProperty) {
|
|
|
|
if (!aGetOrRemoveRequest|| aEquivTable[index].gettable) {
|
|
|
|
nsAutoString cssValue, cssPropertyString;
|
|
|
|
nsIAtom * cssPropertyAtom;
|
|
|
|
// find the equivalent css value for the index-th property in
|
|
|
|
// the equivalence table
|
2004-06-28 16:43:49 +04:00
|
|
|
(*aEquivTable[index].processValueFunctor) ((!aGetOrRemoveRequest || aEquivTable[index].caseSensitiveValue) ? &value : &lowerCasedValue,
|
2002-08-23 13:24:47 +04:00
|
|
|
cssValue,
|
2002-01-09 16:51:37 +03:00
|
|
|
aEquivTable[index].defaultValue,
|
|
|
|
aEquivTable[index].prependValue,
|
|
|
|
aEquivTable[index].appendValue);
|
|
|
|
GetCSSPropertyAtom(cssProperty, &cssPropertyAtom);
|
|
|
|
aPropertyArray.AppendElement(cssPropertyAtom);
|
2009-01-18 23:14:14 +03:00
|
|
|
aValueArray.AppendElement(cssValue);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
index++;
|
|
|
|
cssProperty = aEquivTable[index].cssProperty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate cssPropertyArray and cssValueArray with the declarations equivalent
|
|
|
|
// to aHTMLProperty/aAttribute/aValue for the node aNode
|
|
|
|
void
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GenerateCSSDeclarationsFromHTMLStyle(
|
|
|
|
Element* aElement,
|
|
|
|
nsIAtom* aHTMLProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
nsTArray<nsIAtom*>& cssPropertyArray,
|
|
|
|
nsTArray<nsString>& cssValueArray,
|
|
|
|
bool aGetOrRemoveRequest)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2012-02-10 14:04:47 +04:00
|
|
|
MOZ_ASSERT(aElement);
|
2016-07-07 12:35:13 +03:00
|
|
|
const CSSEditUtils::CSSEquivTable* equivTable = nullptr;
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2014-08-29 15:43:24 +04:00
|
|
|
if (nsGkAtoms::b == aHTMLProperty) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = boldEquivTable;
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::i == aHTMLProperty) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = italicEquivTable;
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::u == aHTMLProperty) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = underlineEquivTable;
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::strike == aHTMLProperty) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = strikeEquivTable;
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::tt == aHTMLProperty) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = ttEquivTable;
|
|
|
|
} else if (aAttribute) {
|
2014-08-29 15:43:24 +04:00
|
|
|
if (nsGkAtoms::font == aHTMLProperty &&
|
2004-05-23 02:15:22 +04:00
|
|
|
aAttribute->EqualsLiteral("color")) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = fontColorEquivTable;
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::font == aHTMLProperty &&
|
2012-04-24 14:57:23 +04:00
|
|
|
aAttribute->EqualsLiteral("face")) {
|
|
|
|
equivTable = fontFaceEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("bgcolor")) {
|
|
|
|
equivTable = bgcolorEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("background")) {
|
|
|
|
equivTable = backgroundImageEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("text")) {
|
|
|
|
equivTable = textColorEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("border")) {
|
|
|
|
equivTable = borderEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("align")) {
|
2015-03-03 14:08:59 +03:00
|
|
|
if (aElement->IsHTMLElement(nsGkAtoms::table)) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = tableAlignEquivTable;
|
2015-03-03 14:08:59 +03:00
|
|
|
} else if (aElement->IsHTMLElement(nsGkAtoms::hr)) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = hrAlignEquivTable;
|
2015-03-03 14:08:59 +03:00
|
|
|
} else if (aElement->IsAnyOfHTMLElements(nsGkAtoms::legend,
|
|
|
|
nsGkAtoms::caption)) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = captionAlignEquivTable;
|
|
|
|
} else {
|
|
|
|
equivTable = textAlignEquivTable;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2012-04-24 14:57:23 +04:00
|
|
|
} else if (aAttribute->EqualsLiteral("valign")) {
|
|
|
|
equivTable = verticalAlignEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("nowrap")) {
|
|
|
|
equivTable = nowrapEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("width")) {
|
|
|
|
equivTable = widthEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("height") ||
|
2015-03-03 14:08:59 +03:00
|
|
|
(aElement->IsHTMLElement(nsGkAtoms::hr) &&
|
2012-04-24 14:57:23 +04:00
|
|
|
aAttribute->EqualsLiteral("size"))) {
|
|
|
|
equivTable = heightEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("type") &&
|
2015-03-03 14:08:59 +03:00
|
|
|
aElement->IsAnyOfHTMLElements(nsGkAtoms::ol,
|
|
|
|
nsGkAtoms::ul,
|
|
|
|
nsGkAtoms::li)) {
|
2012-04-24 14:57:23 +04:00
|
|
|
equivTable = listStyleTypeEquivTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (equivTable) {
|
|
|
|
BuildCSSDeclarations(cssPropertyArray, cssValueArray, equivTable,
|
|
|
|
aValue, aGetOrRemoveRequest);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 11:12:51 +04:00
|
|
|
// Add to aNode the CSS inline style equivalent to HTMLProperty/aAttribute/
|
|
|
|
// aValue for the node, and return in aCount the number of CSS properties set
|
2016-07-07 12:35:13 +03:00
|
|
|
// by the call. The Element version returns aCount instead.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::SetCSSEquivalentToHTMLStyle(Element* aElement,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
bool aSuppressTransaction)
|
2012-05-29 11:12:51 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aElement && aProperty);
|
|
|
|
MOZ_ASSERT_IF(aAttribute, aValue);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count;
|
2012-05-29 11:12:51 +04:00
|
|
|
// This can only fail if SetCSSProperty fails, which should only happen if
|
|
|
|
// something is pretty badly wrong. In this case we assert so that hopefully
|
|
|
|
// someone will notice, but there's nothing more sensible to do than just
|
|
|
|
// return the count and carry on.
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = SetCSSEquivalentToHTMLStyle(aElement->AsDOMNode(),
|
|
|
|
aProperty, aAttribute,
|
|
|
|
aValue, &count,
|
|
|
|
aSuppressTransaction);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "SetCSSEquivalentToHTMLStyle failed");
|
|
|
|
NS_ENSURE_SUCCESS(rv, count);
|
2012-05-29 11:12:51 +04:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::SetCSSEquivalentToHTMLStyle(nsIDOMNode* aNode,
|
|
|
|
nsIAtom* aHTMLProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
int32_t* aCount,
|
|
|
|
bool aSuppressTransaction)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2016-07-07 12:35:13 +03:00
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(aNode);
|
2002-01-09 16:51:37 +03:00
|
|
|
*aCount = 0;
|
2013-02-06 10:00:59 +04:00
|
|
|
if (!element || !IsCSSEditableProperty(element, aHTMLProperty, aAttribute)) {
|
2012-02-10 14:04:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can apply the styles only if the node is an element and if we have
|
|
|
|
// an equivalence for the requested HTML style in this implementation
|
|
|
|
|
|
|
|
// Find the CSS equivalence to the HTML style
|
|
|
|
nsTArray<nsIAtom*> cssPropertyArray;
|
|
|
|
nsTArray<nsString> cssValueArray;
|
|
|
|
GenerateCSSDeclarationsFromHTMLStyle(element, aHTMLProperty, aAttribute,
|
|
|
|
aValue, cssPropertyArray, cssValueArray,
|
|
|
|
false);
|
|
|
|
|
|
|
|
// set the individual CSS inline styles
|
|
|
|
*aCount = cssPropertyArray.Length();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t index = 0; index < *aCount; index++) {
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = SetCSSProperty(*element, *cssPropertyArray[index],
|
|
|
|
cssValueArray[index], aSuppressTransaction);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from aNode the CSS inline style equivalent to HTMLProperty/aAttribute/aValue for the node
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::RemoveCSSEquivalentToHTMLStyle(nsIDOMNode* aNode,
|
|
|
|
nsIAtom* aHTMLProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
bool aSuppressTransaction)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2016-07-07 12:35:13 +03:00
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(aNode);
|
2013-03-10 11:58:34 +04:00
|
|
|
NS_ENSURE_TRUE(element, NS_OK);
|
|
|
|
|
|
|
|
return RemoveCSSEquivalentToHTMLStyle(element, aHTMLProperty, aAttribute,
|
|
|
|
aValue, aSuppressTransaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::RemoveCSSEquivalentToHTMLStyle(Element* aElement,
|
|
|
|
nsIAtom* aHTMLProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
bool aSuppressTransaction)
|
2013-03-10 11:58:34 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
|
|
|
|
if (!IsCSSEditableProperty(aElement, aHTMLProperty, aAttribute)) {
|
2012-02-10 14:04:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can apply the styles only if the node is an element and if we have
|
|
|
|
// an equivalence for the requested HTML style in this implementation
|
|
|
|
|
|
|
|
// Find the CSS equivalence to the HTML style
|
|
|
|
nsTArray<nsIAtom*> cssPropertyArray;
|
|
|
|
nsTArray<nsString> cssValueArray;
|
2013-03-10 11:58:34 +04:00
|
|
|
GenerateCSSDeclarationsFromHTMLStyle(aElement, aHTMLProperty, aAttribute,
|
2012-02-10 14:04:47 +04:00
|
|
|
aValue, cssPropertyArray, cssValueArray,
|
|
|
|
true);
|
|
|
|
|
|
|
|
// remove the individual CSS inline styles
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = cssPropertyArray.Length();
|
|
|
|
for (int32_t index = 0; index < count; index++) {
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveCSSProperty(*aElement,
|
|
|
|
*cssPropertyArray[index],
|
|
|
|
cssValueArray[index],
|
|
|
|
aSuppressTransaction);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns in aValueString the list of values for the CSS equivalences to
|
|
|
|
// the HTML style aHTMLProperty/aAttribute/aValueString for the node aNode;
|
|
|
|
// the value of aStyleType controls the styles we retrieve : specified or
|
|
|
|
// computed.
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetCSSEquivalentToHTMLInlineStyleSet(nsINode* aNode,
|
|
|
|
nsIAtom* aHTMLProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
nsAString& aValueString,
|
|
|
|
StyleType aStyleType)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2003-09-03 17:54:36 +04:00
|
|
|
aValueString.Truncate();
|
2016-07-07 12:35:13 +03:00
|
|
|
nsCOMPtr<Element> theElement = GetElementContainerOrSelf(aNode);
|
2012-02-01 14:54:22 +04:00
|
|
|
NS_ENSURE_TRUE(theElement, NS_ERROR_NULL_POINTER);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2013-02-06 10:00:59 +04:00
|
|
|
if (!theElement || !IsCSSEditableProperty(theElement, aHTMLProperty, aAttribute)) {
|
2012-02-10 14:04:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yes, the requested HTML style has a CSS equivalence in this implementation
|
|
|
|
nsTArray<nsIAtom*> cssPropertyArray;
|
|
|
|
nsTArray<nsString> cssValueArray;
|
|
|
|
// get the CSS equivalence with last param true indicating we want only the
|
|
|
|
// "gettable" properties
|
2012-07-30 18:20:58 +04:00
|
|
|
GenerateCSSDeclarationsFromHTMLStyle(theElement, aHTMLProperty, aAttribute, nullptr,
|
2012-02-10 14:04:47 +04:00
|
|
|
cssPropertyArray, cssValueArray, true);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = cssPropertyArray.Length();
|
|
|
|
for (int32_t index = 0; index < count; index++) {
|
2012-02-10 14:04:47 +04:00
|
|
|
nsAutoString valueString;
|
|
|
|
// retrieve the specified/computed value of the property
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = GetCSSInlinePropertyBase(theElement, cssPropertyArray[index],
|
|
|
|
valueString, aStyleType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-02-10 14:04:47 +04:00
|
|
|
// append the value to aValueString (possibly with a leading whitespace)
|
|
|
|
if (index) {
|
2014-01-04 19:02:17 +04:00
|
|
|
aValueString.Append(char16_t(' '));
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2012-02-10 14:04:47 +04:00
|
|
|
aValueString.Append(valueString);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-17 21:50:19 +04:00
|
|
|
// Does the node aNode (or its parent, if it's not an element node) have a CSS
|
|
|
|
// style equivalent to the HTML style aHTMLProperty/aHTMLAttribute/valueString?
|
|
|
|
// The value of aStyleType controls the styles we retrieve: specified or
|
|
|
|
// computed. The return value aIsSet is true if the CSS styles are set.
|
2012-05-22 14:15:09 +04:00
|
|
|
//
|
|
|
|
// The nsIContent variant returns aIsSet instead of using an out parameter, and
|
|
|
|
// does not modify aValue.
|
|
|
|
bool
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsINode* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
StyleType aStyleType)
|
2015-04-19 15:28:50 +03:00
|
|
|
{
|
|
|
|
// Use aValue as only an in param, not in-out
|
|
|
|
nsAutoString value(aValue);
|
|
|
|
return IsCSSEquivalentToHTMLInlineStyleSet(aNode, aProperty, aAttribute,
|
|
|
|
value, aStyleType);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsINode* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
nsAString& aValue,
|
|
|
|
StyleType aStyleType)
|
2012-05-22 14:15:09 +04:00
|
|
|
{
|
2015-04-19 15:28:50 +03:00
|
|
|
MOZ_ASSERT(aNode && aProperty);
|
2012-05-22 14:15:09 +04:00
|
|
|
bool isSet;
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = IsCSSEquivalentToHTMLInlineStyleSet(aNode->AsDOMNode(),
|
|
|
|
aProperty, aAttribute,
|
|
|
|
isSet, aValue, aStyleType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2012-05-22 14:15:09 +04:00
|
|
|
return isSet;
|
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::IsCSSEquivalentToHTMLInlineStyleSet(
|
|
|
|
nsIDOMNode* aNode,
|
|
|
|
nsIAtom* aHTMLProperty,
|
|
|
|
const nsAString* aHTMLAttribute,
|
|
|
|
bool& aIsSet,
|
|
|
|
nsAString& valueString,
|
|
|
|
StyleType aStyleType)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsAutoString htmlValueString(valueString);
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = false;
|
2012-02-10 14:04:47 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2002-01-09 16:51:37 +03:00
|
|
|
do {
|
|
|
|
valueString.Assign(htmlValueString);
|
|
|
|
// get the value of the CSS equivalent styles
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
|
|
|
GetCSSEquivalentToHTMLInlineStyleSet(node, aHTMLProperty, aHTMLAttribute,
|
|
|
|
valueString, aStyleType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// early way out if we can
|
2012-04-17 21:50:19 +04:00
|
|
|
if (valueString.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2014-08-29 15:43:24 +04:00
|
|
|
if (nsGkAtoms::b == aHTMLProperty) {
|
2012-04-17 21:50:19 +04:00
|
|
|
if (valueString.EqualsLiteral("bold")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = true;
|
2012-04-17 21:50:19 +04:00
|
|
|
} else if (valueString.EqualsLiteral("normal")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = false;
|
2012-04-17 21:50:19 +04:00
|
|
|
} else if (valueString.EqualsLiteral("bolder")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = true;
|
2012-04-17 21:50:19 +04:00
|
|
|
valueString.AssignLiteral("bold");
|
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t weight = 0;
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult errorCode;
|
2002-01-09 16:51:37 +03:00
|
|
|
nsAutoString value(valueString);
|
2013-04-24 16:36:09 +04:00
|
|
|
weight = value.ToInteger(&errorCode);
|
2002-01-09 16:51:37 +03:00
|
|
|
if (400 < weight) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = true;
|
2012-04-17 21:50:19 +04:00
|
|
|
valueString.AssignLiteral("bold");
|
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = false;
|
2004-06-17 04:13:25 +04:00
|
|
|
valueString.AssignLiteral("normal");
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::i == aHTMLProperty) {
|
2004-05-23 02:15:22 +04:00
|
|
|
if (valueString.EqualsLiteral("italic") ||
|
|
|
|
valueString.EqualsLiteral("oblique")) {
|
2012-04-17 21:50:19 +04:00
|
|
|
aIsSet = true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::u == aHTMLProperty) {
|
2002-01-09 16:51:37 +03:00
|
|
|
nsAutoString val;
|
2004-06-17 04:13:25 +04:00
|
|
|
val.AssignLiteral("underline");
|
2016-07-07 08:23:25 +03:00
|
|
|
aIsSet = ChangeStyleTransaction::ValueIncludes(valueString, val);
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::strike == aHTMLProperty) {
|
2002-01-09 16:51:37 +03:00
|
|
|
nsAutoString val;
|
2004-06-17 04:13:25 +04:00
|
|
|
val.AssignLiteral("line-through");
|
2016-07-07 08:23:25 +03:00
|
|
|
aIsSet = ChangeStyleTransaction::ValueIncludes(valueString, val);
|
2012-04-17 21:50:19 +04:00
|
|
|
} else if (aHTMLAttribute &&
|
2014-08-29 15:43:24 +04:00
|
|
|
((nsGkAtoms::font == aHTMLProperty &&
|
2012-04-17 21:50:19 +04:00
|
|
|
aHTMLAttribute->EqualsLiteral("color")) ||
|
|
|
|
aHTMLAttribute->EqualsLiteral("bgcolor"))) {
|
|
|
|
if (htmlValueString.IsEmpty()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = true;
|
2012-04-17 21:50:19 +04:00
|
|
|
} else {
|
2002-12-13 11:56:21 +03:00
|
|
|
nscolor rgba;
|
|
|
|
nsAutoString subStr;
|
2012-04-17 21:50:19 +04:00
|
|
|
htmlValueString.Right(subStr, htmlValueString.Length() - 1);
|
2002-12-13 11:56:21 +03:00
|
|
|
if (NS_ColorNameToRGB(htmlValueString, &rgba) ||
|
2016-05-09 08:16:41 +03:00
|
|
|
NS_HexToRGBA(subStr, nsHexColorType::NoAlpha, &rgba)) {
|
2002-12-13 11:56:21 +03:00
|
|
|
nsAutoString htmlColor, tmpStr;
|
|
|
|
|
2012-05-28 15:20:27 +04:00
|
|
|
if (NS_GET_A(rgba) != 255) {
|
|
|
|
// This should only be hit by the "transparent" keyword, which
|
|
|
|
// currently serializes to "transparent" (not "rgba(0, 0, 0, 0)").
|
|
|
|
MOZ_ASSERT(NS_GET_R(rgba) == 0 && NS_GET_G(rgba) == 0 &&
|
|
|
|
NS_GET_B(rgba) == 0 && NS_GET_A(rgba) == 0);
|
|
|
|
htmlColor.AppendLiteral("transparent");
|
|
|
|
} else {
|
|
|
|
htmlColor.AppendLiteral("rgb(");
|
2002-12-13 11:56:21 +03:00
|
|
|
|
2012-05-28 15:20:27 +04:00
|
|
|
NS_NAMED_LITERAL_STRING(comma, ", ");
|
2002-12-13 11:56:21 +03:00
|
|
|
|
2012-05-28 15:20:27 +04:00
|
|
|
tmpStr.AppendInt(NS_GET_R(rgba), 10);
|
|
|
|
htmlColor.Append(tmpStr + comma);
|
2002-12-13 11:56:21 +03:00
|
|
|
|
2012-05-28 15:20:27 +04:00
|
|
|
tmpStr.Truncate();
|
|
|
|
tmpStr.AppendInt(NS_GET_G(rgba), 10);
|
|
|
|
htmlColor.Append(tmpStr + comma);
|
|
|
|
|
|
|
|
tmpStr.Truncate();
|
|
|
|
tmpStr.AppendInt(NS_GET_B(rgba), 10);
|
|
|
|
htmlColor.Append(tmpStr);
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
htmlColor.Append(char16_t(')'));
|
2012-05-28 15:20:27 +04:00
|
|
|
}
|
2002-12-13 11:56:21 +03:00
|
|
|
|
2002-12-13 13:17:42 +03:00
|
|
|
aIsSet = htmlColor.Equals(valueString,
|
|
|
|
nsCaseInsensitiveStringComparator());
|
2012-04-17 21:50:19 +04:00
|
|
|
} else {
|
2002-12-13 13:17:42 +03:00
|
|
|
aIsSet = htmlValueString.Equals(valueString,
|
|
|
|
nsCaseInsensitiveStringComparator());
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
2002-12-13 11:56:21 +03:00
|
|
|
}
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::tt == aHTMLProperty) {
|
2003-07-14 11:37:39 +04:00
|
|
|
aIsSet = StringBeginsWith(valueString, NS_LITERAL_STRING("monospace"));
|
2014-08-29 15:43:24 +04:00
|
|
|
} else if (nsGkAtoms::font == aHTMLProperty && aHTMLAttribute &&
|
2012-04-17 21:50:19 +04:00
|
|
|
aHTMLAttribute->EqualsLiteral("face")) {
|
2003-04-17 11:35:55 +04:00
|
|
|
if (!htmlValueString.IsEmpty()) {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t commaSpace[] = { char16_t(','), char16_t(' '), 0 };
|
|
|
|
const char16_t comma[] = { char16_t(','), 0 };
|
2003-04-17 11:35:55 +04:00
|
|
|
htmlValueString.ReplaceSubstring(commaSpace, comma);
|
|
|
|
nsAutoString valueStringNorm(valueString);
|
|
|
|
valueStringNorm.ReplaceSubstring(commaSpace, comma);
|
|
|
|
aIsSet = htmlValueString.Equals(valueStringNorm,
|
|
|
|
nsCaseInsensitiveStringComparator());
|
2012-04-17 21:50:19 +04:00
|
|
|
} else {
|
2015-03-30 12:58:00 +03:00
|
|
|
aIsSet = true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2012-04-17 21:50:19 +04:00
|
|
|
} else if (aHTMLAttribute && aHTMLAttribute->EqualsLiteral("align")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = true;
|
2012-04-17 21:50:19 +04:00
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = false;
|
2002-01-09 16:51:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-17 21:50:19 +04:00
|
|
|
if (!htmlValueString.IsEmpty() &&
|
|
|
|
htmlValueString.Equals(valueString,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
aIsSet = true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2012-05-28 15:20:27 +04:00
|
|
|
if (htmlValueString.EqualsLiteral("-moz-editor-invert-value")) {
|
|
|
|
aIsSet = !aIsSet;
|
|
|
|
}
|
|
|
|
|
2014-08-29 15:43:24 +04:00
|
|
|
if (nsGkAtoms::u == aHTMLProperty || nsGkAtoms::strike == aHTMLProperty) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// unfortunately, the value of the text-decoration property is not inherited.
|
|
|
|
// that means that we have to look at ancestors of node to see if they are underlined
|
2013-01-11 12:43:01 +04:00
|
|
|
node = node->GetParentElement(); // set to null if it's not a dom element
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2014-08-29 15:43:24 +04:00
|
|
|
} while ((nsGkAtoms::u == aHTMLProperty ||
|
|
|
|
nsGkAtoms::strike == aHTMLProperty) && !aIsSet && node);
|
2002-01-09 16:51:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 15:03:17 +04:00
|
|
|
void
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::SetCSSEnabled(bool aIsCSSPrefChecked)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
mIsCSSPrefChecked = aIsCSSPrefChecked;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::IsCSSPrefChecked()
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
return mIsCSSPrefChecked ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ElementsSameStyle compares two elements and checks if they have the same
|
2015-05-28 18:58:42 +03:00
|
|
|
// specified CSS declarations in the STYLE attribute
|
2002-01-09 16:51:37 +03:00
|
|
|
// The answer is always negative if at least one of them carries an ID or a class
|
2012-05-29 11:12:51 +04:00
|
|
|
bool
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::ElementsSameStyle(nsIDOMNode* aFirstNode,
|
|
|
|
nsIDOMNode* aSecondNode)
|
2012-05-29 11:12:51 +04:00
|
|
|
{
|
2016-07-07 12:35:13 +03:00
|
|
|
nsCOMPtr<Element> firstElement = do_QueryInterface(aFirstNode);
|
|
|
|
nsCOMPtr<Element> secondElement = do_QueryInterface(aSecondNode);
|
2013-01-11 12:43:01 +04:00
|
|
|
|
|
|
|
NS_ASSERTION((firstElement && secondElement), "Non element nodes passed to ElementsSameStyle.");
|
|
|
|
NS_ENSURE_TRUE(firstElement, false);
|
|
|
|
NS_ENSURE_TRUE(secondElement, false);
|
|
|
|
|
|
|
|
return ElementsSameStyle(firstElement, secondElement);
|
2012-05-29 11:12:51 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::ElementsSameStyle(Element* aFirstElement,
|
|
|
|
Element* aSecondElement)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2013-01-11 12:43:01 +04:00
|
|
|
MOZ_ASSERT(aFirstElement);
|
|
|
|
MOZ_ASSERT(aSecondElement);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2013-01-11 12:43:01 +04:00
|
|
|
if (aFirstElement->HasAttr(kNameSpaceID_None, nsGkAtoms::id) ||
|
|
|
|
aSecondElement->HasAttr(kNameSpaceID_None, nsGkAtoms::id)) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// at least one of the spans carries an ID ; suspect a CSS rule applies to it and
|
|
|
|
// refuse to merge the nodes
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString firstClass, secondClass;
|
2013-01-11 12:43:01 +04:00
|
|
|
bool isFirstClassSet = aFirstElement->GetAttr(kNameSpaceID_None, nsGkAtoms::_class, firstClass);
|
|
|
|
bool isSecondClassSet = aSecondElement->GetAttr(kNameSpaceID_None, nsGkAtoms::_class, secondClass);
|
2002-01-09 16:51:37 +03:00
|
|
|
if (isFirstClassSet && isSecondClassSet) {
|
|
|
|
// both spans carry a class, let's compare them
|
|
|
|
if (!firstClass.Equals(secondClass)) {
|
|
|
|
// WARNING : technically, the comparison just above is questionable :
|
|
|
|
// from a pure HTML/CSS point of view class="a b" is NOT the same than
|
|
|
|
// class="b a" because a CSS rule could test the exact value of the class
|
|
|
|
// attribute to be "a b" for instance ; from a user's point of view, a
|
|
|
|
// wysiwyg editor should probably NOT make any difference. CSS people
|
|
|
|
// need to discuss this issue before any modification.
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2013-01-11 12:43:01 +04:00
|
|
|
} else if (isFirstClassSet || isSecondClassSet) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// one span only carries a class, early way out
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> firstCSSDecl, secondCSSDecl;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t firstLength, secondLength;
|
2013-01-11 12:43:01 +04:00
|
|
|
nsresult rv = GetInlineStyles(aFirstElement, getter_AddRefs(firstCSSDecl), &firstLength);
|
|
|
|
if (NS_FAILED(rv) || !firstCSSDecl) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rv = GetInlineStyles(aSecondElement, getter_AddRefs(secondCSSDecl), &secondLength);
|
|
|
|
if (NS_FAILED(rv) || !secondCSSDecl) {
|
|
|
|
return false;
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
if (firstLength != secondLength) {
|
|
|
|
// early way out if we can
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2013-01-11 12:43:01 +04:00
|
|
|
|
|
|
|
if (!firstLength) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// no inline style !
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString propertyNameString;
|
|
|
|
nsAutoString firstValue, secondValue;
|
2013-01-11 12:43:01 +04:00
|
|
|
for (uint32_t i = 0; i < firstLength; i++) {
|
2002-01-09 16:51:37 +03:00
|
|
|
firstCSSDecl->Item(i, propertyNameString);
|
|
|
|
firstCSSDecl->GetPropertyValue(propertyNameString, firstValue);
|
|
|
|
secondCSSDecl->GetPropertyValue(propertyNameString, secondValue);
|
|
|
|
if (!firstValue.Equals(secondValue)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
2013-01-11 12:43:01 +04:00
|
|
|
for (uint32_t i = 0; i < secondLength; i++) {
|
2002-01-09 16:51:37 +03:00
|
|
|
secondCSSDecl->Item(i, propertyNameString);
|
|
|
|
secondCSSDecl->GetPropertyValue(propertyNameString, secondValue);
|
|
|
|
firstCSSDecl->GetPropertyValue(propertyNameString, firstValue);
|
|
|
|
if (!firstValue.Equals(secondValue)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetInlineStyles(Element* aElement,
|
|
|
|
nsIDOMCSSStyleDeclaration** aCssDecl,
|
|
|
|
uint32_t* aLength)
|
2013-01-11 12:43:01 +04:00
|
|
|
{
|
|
|
|
return GetInlineStyles(static_cast<nsISupports*>(aElement), aCssDecl, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetInlineStyles(nsIDOMElement* aElement,
|
|
|
|
nsIDOMCSSStyleDeclaration** aCssDecl,
|
|
|
|
uint32_t* aLength)
|
2013-01-11 12:43:01 +04:00
|
|
|
{
|
|
|
|
return GetInlineStyles(static_cast<nsISupports*>(aElement), aCssDecl, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetInlineStyles(nsISupports* aElement,
|
|
|
|
nsIDOMCSSStyleDeclaration** aCssDecl,
|
|
|
|
uint32_t* aLength)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aElement && aLength, NS_ERROR_NULL_POINTER);
|
2002-01-09 16:51:37 +03:00
|
|
|
*aLength = 0;
|
2016-08-02 21:05:38 +03:00
|
|
|
nsCOMPtr<nsStyledElement> inlineStyles = do_QueryInterface(aElement);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(inlineStyles, NS_ERROR_NULL_POINTER);
|
2013-01-11 12:43:01 +04:00
|
|
|
|
2016-08-02 21:05:38 +03:00
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> cssDecl =
|
|
|
|
do_QueryInterface(inlineStyles->Style());
|
|
|
|
MOZ_ASSERT(cssDecl);
|
2013-01-11 12:43:01 +04:00
|
|
|
|
2016-08-02 21:05:38 +03:00
|
|
|
cssDecl.forget(aCssDecl);
|
2002-01-09 16:51:37 +03:00
|
|
|
(*aCssDecl)->GetLength(aLength);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
already_AddRefed<nsIDOMElement>
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::GetElementContainerOrSelf(nsIDOMNode* aNode)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2012-02-01 14:54:22 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(node, nullptr);
|
2012-02-10 14:04:46 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> element =
|
|
|
|
do_QueryInterface(GetElementContainerOrSelf(node));
|
|
|
|
return element.forget();
|
|
|
|
}
|
2009-02-19 10:55:40 +03:00
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
Element*
|
|
|
|
CSSEditUtils::GetElementContainerOrSelf(nsINode* aNode)
|
2012-02-10 14:04:46 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
if (nsIDOMNode::DOCUMENT_NODE == aNode->NodeType()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-02-19 10:55:40 +03:00
|
|
|
}
|
|
|
|
|
2012-02-10 14:04:46 +04:00
|
|
|
nsINode* node = aNode;
|
2012-02-01 14:54:22 +04:00
|
|
|
// Loop until we find an element.
|
|
|
|
while (node && !node->IsElement()) {
|
2012-10-09 16:31:24 +04:00
|
|
|
node = node->GetParentNode();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2012-02-01 14:54:22 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(node, nullptr);
|
2012-02-10 14:04:46 +04:00
|
|
|
return node->AsElement();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::SetCSSProperty(nsIDOMElement* aElement,
|
|
|
|
const nsAString& aProperty,
|
|
|
|
const nsAString& aValue)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> cssDecl;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length;
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = GetInlineStyles(aElement, getter_AddRefs(cssDecl), &length);
|
|
|
|
if (NS_FAILED(rv) || !cssDecl) {
|
|
|
|
return rv;
|
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
return cssDecl->SetProperty(aProperty,
|
|
|
|
aValue,
|
2004-01-30 01:04:45 +03:00
|
|
|
EmptyString());
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
2003-09-03 17:54:36 +04:00
|
|
|
|
|
|
|
nsresult
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::SetCSSPropertyPixels(nsIDOMElement* aElement,
|
|
|
|
const nsAString& aProperty,
|
|
|
|
int32_t aIntValue)
|
2003-09-03 17:54:36 +04:00
|
|
|
{
|
|
|
|
nsAutoString s;
|
|
|
|
s.AppendInt(aIntValue);
|
|
|
|
return SetCSSProperty(aElement, aProperty, s + NS_LITERAL_STRING("px"));
|
|
|
|
}
|
2016-07-07 12:35:13 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|