2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "ChangeCSSInlineStyleTxn.h"
|
|
|
|
#include "EditTxn.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/mozalloc.h"
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsColor.h"
|
2012-07-08 02:50:31 -07:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsDependentSubstring.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsEditProperty.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsHTMLCSSUtils.h"
|
|
|
|
#include "nsHTMLEditor.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMCSSStyleDeclaration.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMElementCSSInlineStyle.h"
|
2012-07-12 23:33:42 -07: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"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsReadableUtils.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsStringIterator.h"
|
|
|
|
#include "nsSubstringTuple.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
2011-06-16 17:59:29 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessBValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
if (aInputString && aInputString->EqualsLiteral("-moz-editor-invert-value")) {
|
|
|
|
aOutputString.AssignLiteral("normal");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aOutputString.AssignLiteral("bold");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessDefaultValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
CopyASCIItoUTF16(aDefaultValueString, aOutputString);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessSameValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
if (aInputString) {
|
|
|
|
aOutputString.Assign(*aInputString);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
aOutputString.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessExtendedValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
if (aPrependString) {
|
|
|
|
AppendASCIItoUTF16(aPrependString, aOutputString);
|
|
|
|
}
|
|
|
|
aOutputString.Append(*aInputString);
|
|
|
|
if (aAppendString) {
|
|
|
|
AppendASCIItoUTF16(aAppendString, aOutputString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessLengthValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
aOutputString.Append(*aInputString);
|
|
|
|
if (-1 == aOutputString.FindChar(PRUnichar('%'))) {
|
|
|
|
aOutputString.AppendLiteral("px");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessListStyleTypeValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
if (aInputString->EqualsLiteral("1")) {
|
|
|
|
aOutputString.AppendLiteral("decimal");
|
|
|
|
}
|
|
|
|
else if (aInputString->EqualsLiteral("a")) {
|
|
|
|
aOutputString.AppendLiteral("lower-alpha");
|
|
|
|
}
|
|
|
|
else if (aInputString->EqualsLiteral("A")) {
|
|
|
|
aOutputString.AppendLiteral("upper-alpha");
|
|
|
|
}
|
|
|
|
else if (aInputString->EqualsLiteral("i")) {
|
|
|
|
aOutputString.AppendLiteral("lower-roman");
|
|
|
|
}
|
|
|
|
else if (aInputString->EqualsLiteral("I")) {
|
|
|
|
aOutputString.AppendLiteral("upper-roman");
|
|
|
|
}
|
|
|
|
else if (aInputString->EqualsLiteral("square")
|
|
|
|
|| aInputString->EqualsLiteral("circle")
|
|
|
|
|| aInputString->EqualsLiteral("disc")) {
|
|
|
|
aOutputString.Append(*aInputString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessMarginLeftValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
if (aInputString->EqualsLiteral("center") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-center")) {
|
|
|
|
aOutputString.AppendLiteral("auto");
|
|
|
|
}
|
|
|
|
else if (aInputString->EqualsLiteral("right") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-right")) {
|
|
|
|
aOutputString.AppendLiteral("auto");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aOutputString.AppendLiteral("0px");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void ProcessMarginRightValue(const nsAString * aInputString, nsAString & aOutputString,
|
|
|
|
const char * aDefaultValueString,
|
|
|
|
const char * aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
if (aInputString->EqualsLiteral("center") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-center")) {
|
|
|
|
aOutputString.AppendLiteral("auto");
|
|
|
|
}
|
|
|
|
else if (aInputString->EqualsLiteral("left") ||
|
|
|
|
aInputString->EqualsLiteral("-moz-left")) {
|
|
|
|
aOutputString.AppendLiteral("auto");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aOutputString.AppendLiteral("0px");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 03:57:49 -07:00
|
|
|
static
|
|
|
|
void ProcessFontSizeValue(const nsAString* aInputString, nsAString& aOutputString,
|
|
|
|
const char* aDefaultValueString,
|
|
|
|
const char* aPrependString, const char* aAppendString)
|
|
|
|
{
|
|
|
|
aOutputString.Truncate();
|
|
|
|
if (aInputString) {
|
|
|
|
PRInt32 size = nsContentUtils::ParseLegacyFontSize(*aInputString);
|
|
|
|
switch (size) {
|
|
|
|
case 0:
|
|
|
|
// Didn't parse
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
aOutputString.AssignLiteral("x-small");
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
aOutputString.AssignLiteral("small");
|
|
|
|
return;
|
|
|
|
case 3:
|
|
|
|
aOutputString.AssignLiteral("medium");
|
|
|
|
return;
|
|
|
|
case 4:
|
|
|
|
aOutputString.AssignLiteral("large");
|
|
|
|
return;
|
|
|
|
case 5:
|
|
|
|
aOutputString.AssignLiteral("x-large");
|
|
|
|
return;
|
|
|
|
case 6:
|
|
|
|
aOutputString.AssignLiteral("xx-large");
|
|
|
|
return;
|
|
|
|
case 7:
|
|
|
|
// No corresponding CSS size
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unexpected return value from ParseLegacyFontSize");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsHTMLCSSUtils::CSSEquivTable boldEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_font_weight, ProcessBValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable italicEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_font_style, ProcessDefaultValue, "italic", nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable underlineEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_text_decoration, ProcessDefaultValue, "underline", nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable strikeEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_text_decoration, ProcessDefaultValue, "line-through", nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable ttEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_font_family, ProcessDefaultValue, "monospace", nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable fontColorEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_color, ProcessSameValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable fontFaceEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_font_family, ProcessSameValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
2012-04-24 03:57:49 -07:00
|
|
|
const nsHTMLCSSUtils::CSSEquivTable fontSizeEquivTable[] = {
|
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_font_size, ProcessFontSizeValue, nsnull, nsnull, nsnull, true, false },
|
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsHTMLCSSUtils::CSSEquivTable bgcolorEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_background_color, ProcessSameValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable backgroundImageEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_background_image, ProcessExtendedValue, nsnull, "url(", ")", true, true },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable textColorEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_color, ProcessSameValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable borderEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_border, ProcessExtendedValue, nsnull, nsnull, "px solid", true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable textAlignEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_text_align, ProcessSameValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable captionAlignEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_caption_side, ProcessSameValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable verticalAlignEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_vertical_align, ProcessSameValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable nowrapEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_whitespace, ProcessDefaultValue, "nowrap", nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable widthEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_width, ProcessLengthValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable heightEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_height, ProcessLengthValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable listStyleTypeEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_list_style_type, ProcessListStyleTypeValue, nsnull, nsnull, nsnull, true, true },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable tableAlignEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_text_align, ProcessDefaultValue, "left", nsnull, nsnull, false, false },
|
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_margin_left, ProcessMarginLeftValue, nsnull, nsnull, nsnull, true, false },
|
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_margin_right, ProcessMarginRightValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsHTMLCSSUtils::CSSEquivTable hrAlignEquivTable[] = {
|
2011-10-17 07:59:28 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_margin_left, ProcessMarginLeftValue, nsnull, nsnull, nsnull, true, false },
|
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_margin_right, ProcessMarginRightValue, nsnull, nsnull, nsnull, true, false },
|
2007-03-22 10:30:00 -07:00
|
|
|
{ nsHTMLCSSUtils::eCSSEditableProperty_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
2011-05-15 03:07:30 -07:00
|
|
|
nsHTMLCSSUtils::nsHTMLCSSUtils(nsHTMLEditor* aEditor)
|
|
|
|
: mHTMLEditor(aEditor)
|
2012-04-03 07:16:05 -07:00
|
|
|
, mIsCSSPrefChecked(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// let's retrieve the value of the "CSS editing" pref
|
2011-06-16 17:59:29 -07:00
|
|
|
mIsCSSPrefChecked = Preferences::GetBool("editor.use_css", mIsCSSPrefChecked);
|
2011-05-15 03:07:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLCSSUtils::~nsHTMLCSSUtils()
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Answers true if we have some CSS equivalence for the HTML style defined
|
|
|
|
// by aProperty and/or aAttribute for the node aNode
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-04-24 03:57:49 -07:00
|
|
|
nsHTMLCSSUtils::IsCSSEditableProperty(nsIDOMNode* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aNode, "Shouldn't you pass aNode? - Bug 214025");
|
|
|
|
|
2012-02-10 02:04:47 -08:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(content, false);
|
2012-04-24 03:57:49 -07:00
|
|
|
return IsCSSEditableProperty(content, aProperty, aAttribute, aValue);
|
2012-02-10 02:04:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsHTMLCSSUtils::IsCSSEditableProperty(nsIContent* aNode,
|
|
|
|
nsIAtom* aProperty,
|
2012-04-24 03:57:49 -07:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue)
|
2012-02-10 02:04:47 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsIContent* content = aNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
// we need an element node here
|
2012-02-10 02:04:47 -08:00
|
|
|
if (content->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
content = content->GetParent();
|
|
|
|
NS_ENSURE_TRUE(content, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom *tagName = content->Tag();
|
|
|
|
// brade: shouldn't some of the above go below the next block?
|
|
|
|
|
|
|
|
// html inline styles B I TT U STRIKE and COLOR/FACE on FONT
|
|
|
|
if (nsEditProperty::b == aProperty
|
|
|
|
|| nsEditProperty::i == aProperty
|
|
|
|
|| nsEditProperty::tt == aProperty
|
|
|
|
|| nsEditProperty::u == aProperty
|
|
|
|
|| nsEditProperty::strike == aProperty
|
|
|
|
|| ((nsEditProperty::font == aProperty) && aAttribute &&
|
|
|
|
(aAttribute->EqualsLiteral("color") ||
|
|
|
|
aAttribute->EqualsLiteral("face")))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-04-24 03:57:49 -07:00
|
|
|
// FONT SIZE doesn't work if the value is 7
|
|
|
|
if (nsEditProperty::font == aProperty && aAttribute &&
|
|
|
|
aAttribute->EqualsLiteral("size")) {
|
|
|
|
if (!aValue || aValue->IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
PRInt32 size = nsContentUtils::ParseLegacyFontSize(*aValue);
|
|
|
|
return size && size != 7;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// ALIGN attribute on elements supporting it
|
|
|
|
if (aAttribute && (aAttribute->EqualsLiteral("align")) &&
|
|
|
|
(nsEditProperty::div == tagName
|
|
|
|
|| nsEditProperty::p == tagName
|
|
|
|
|| nsEditProperty::h1 == tagName
|
|
|
|
|| nsEditProperty::h2 == tagName
|
|
|
|
|| nsEditProperty::h3 == tagName
|
|
|
|
|| nsEditProperty::h4 == tagName
|
|
|
|
|| nsEditProperty::h5 == tagName
|
|
|
|
|| nsEditProperty::h6 == tagName
|
|
|
|
|| nsEditProperty::td == tagName
|
|
|
|
|| nsEditProperty::th == tagName
|
|
|
|
|| nsEditProperty::table == tagName
|
|
|
|
|| nsEditProperty::hr == tagName
|
|
|
|
// brade: for the above, why not use nsHTMLEditUtils::SupportsAlignAttr
|
|
|
|
// brade: but it also checks for tbody, tfoot, thead
|
|
|
|
// Let's add the following elements here even if ALIGN has not
|
|
|
|
// the same meaning for them
|
|
|
|
|| nsEditProperty::legend == tagName
|
|
|
|
|| nsEditProperty::caption == tagName)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aAttribute && (aAttribute->EqualsLiteral("valign")) &&
|
|
|
|
(nsEditProperty::col == tagName
|
|
|
|
|| nsEditProperty::colgroup == tagName
|
|
|
|
|| nsEditProperty::tbody == tagName
|
|
|
|
|| nsEditProperty::td == tagName
|
|
|
|
|| nsEditProperty::th == tagName
|
|
|
|
|| nsEditProperty::tfoot == tagName
|
|
|
|
|| nsEditProperty::thead == tagName
|
|
|
|
|| nsEditProperty::tr == tagName)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// attributes TEXT, BACKGROUND and BGCOLOR on BODY
|
|
|
|
if (aAttribute && (nsEditProperty::body == tagName) &&
|
|
|
|
(aAttribute->EqualsLiteral("text")
|
|
|
|
|| aAttribute->EqualsLiteral("background")
|
|
|
|
|| aAttribute->EqualsLiteral("bgcolor"))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// attribute BGCOLOR on other elements
|
|
|
|
if (aAttribute && aAttribute->EqualsLiteral("bgcolor")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// attributes HEIGHT, WIDTH and NOWRAP on TD and TH
|
|
|
|
if (aAttribute && ((nsEditProperty::td == tagName)
|
|
|
|
|| (nsEditProperty::th == tagName)) &&
|
|
|
|
(aAttribute->EqualsLiteral("height")
|
|
|
|
|| aAttribute->EqualsLiteral("width")
|
|
|
|
|| aAttribute->EqualsLiteral("nowrap"))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// attributes HEIGHT and WIDTH on TABLE
|
|
|
|
if (aAttribute && (nsEditProperty::table == tagName) &&
|
|
|
|
(aAttribute->EqualsLiteral("height")
|
|
|
|
|| aAttribute->EqualsLiteral("width"))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// attributes SIZE and WIDTH on HR
|
|
|
|
if (aAttribute && (nsEditProperty::hr == tagName) &&
|
|
|
|
(aAttribute->EqualsLiteral("size")
|
|
|
|
|| aAttribute->EqualsLiteral("width"))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// attribute TYPE on OL UL LI
|
|
|
|
if (aAttribute && (nsEditProperty::ol == tagName
|
|
|
|
|| nsEditProperty::ul == tagName
|
|
|
|
|| nsEditProperty::li == tagName) &&
|
|
|
|
aAttribute->EqualsLiteral("type")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aAttribute && nsEditProperty::img == tagName &&
|
|
|
|
(aAttribute->EqualsLiteral("border")
|
|
|
|
|| aAttribute->EqualsLiteral("width")
|
|
|
|
|| aAttribute->EqualsLiteral("height"))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// other elements that we can align using CSS even if they
|
|
|
|
// can't carry the html ALIGN attribute
|
|
|
|
if (aAttribute && aAttribute->EqualsLiteral("align") &&
|
|
|
|
(nsEditProperty::ul == tagName
|
|
|
|
|| nsEditProperty::ol == tagName
|
|
|
|
|| nsEditProperty::dl == tagName
|
|
|
|
|| nsEditProperty::li == tagName
|
|
|
|
|| nsEditProperty::dd == tagName
|
|
|
|
|| nsEditProperty::dt == tagName
|
|
|
|
|| nsEditProperty::address == tagName
|
|
|
|
|| nsEditProperty::pre == tagName
|
|
|
|
|| nsEditProperty::ul == tagName)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// the lowest level above the transaction; adds the css declaration "aProperty : aValue" to
|
|
|
|
// the inline styles carried by aElement
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::SetCSSProperty(nsIDOMElement *aElement, nsIAtom * aProperty, const nsAString & aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<ChangeCSSInlineStyleTxn> txn;
|
|
|
|
nsresult result = CreateCSSPropertyTxn(aElement, aProperty, aValue,
|
2011-10-17 07:59:28 -07:00
|
|
|
getter_AddRefs(txn), false);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
if (aSuppressTransaction) {
|
|
|
|
result = txn->DoTransaction();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = mHTMLEditor->DoTransaction(txn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::SetCSSPropertyPixels(nsIDOMElement *aElement,
|
|
|
|
nsIAtom *aProperty,
|
|
|
|
PRInt32 aIntValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoString s;
|
|
|
|
s.AppendInt(aIntValue);
|
|
|
|
return SetCSSProperty(aElement, aProperty, s + NS_LITERAL_STRING("px"),
|
|
|
|
aSuppressTransaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::RemoveCSSProperty(nsIDOMElement *aElement, nsIAtom * aProperty, const nsAString & aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<ChangeCSSInlineStyleTxn> txn;
|
|
|
|
nsresult result = CreateCSSPropertyTxn(aElement, aProperty, aValue,
|
2011-10-17 07:59:28 -07:00
|
|
|
getter_AddRefs(txn), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
if (aSuppressTransaction) {
|
|
|
|
result = txn->DoTransaction();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = mHTMLEditor->DoTransaction(txn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::CreateCSSPropertyTxn(nsIDOMElement *aElement,
|
|
|
|
nsIAtom * aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
ChangeCSSInlineStyleTxn ** aTxn,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aRemoveProperty)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
2009-04-24 15:45:34 -07:00
|
|
|
|
|
|
|
*aTxn = new ChangeCSSInlineStyleTxn();
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(*aTxn, NS_ERROR_OUT_OF_MEMORY);
|
2009-04-24 15:45:34 -07:00
|
|
|
NS_ADDREF(*aTxn);
|
|
|
|
return (*aTxn)->Init(mHTMLEditor, aElement, aAttribute, aValue, aRemoveProperty);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::GetSpecifiedProperty(nsIDOMNode *aNode, nsIAtom *aProperty,
|
|
|
|
nsAString & aValue)
|
|
|
|
{
|
2012-07-08 02:50:31 -07:00
|
|
|
return GetCSSInlinePropertyBase(aNode, aProperty, aValue, eSpecified);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::GetComputedProperty(nsIDOMNode *aNode, nsIAtom *aProperty,
|
|
|
|
nsAString & aValue)
|
|
|
|
{
|
2012-07-08 02:50:31 -07:00
|
|
|
return GetCSSInlinePropertyBase(aNode, aProperty, aValue, eComputed);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-10 02:04:47 -08:00
|
|
|
nsresult
|
2012-07-15 03:42:31 -07:00
|
|
|
nsHTMLCSSUtils::GetCSSInlinePropertyBase(nsINode* aNode, nsIAtom* aProperty,
|
2012-02-10 02:04:47 -08:00
|
|
|
nsAString& aValue,
|
2012-07-06 04:50:07 -07:00
|
|
|
StyleType aStyleType)
|
2012-02-10 02:04:47 -08:00
|
|
|
{
|
2012-07-15 03:42:31 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode);
|
2012-07-08 02:50:31 -07:00
|
|
|
return GetCSSInlinePropertyBase(node, aProperty, aValue, aStyleType);
|
2012-02-10 02:04:47 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2012-07-15 03:42:31 -07:00
|
|
|
nsHTMLCSSUtils::GetCSSInlinePropertyBase(nsIDOMNode *aNode, nsIAtom *aProperty,
|
2011-04-23 23:54:25 -07:00
|
|
|
nsAString& aValue,
|
2012-07-06 04:50:07 -07:00
|
|
|
StyleType aStyleType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aValue.Truncate();
|
2012-07-15 03:42:31 -07:00
|
|
|
NS_ENSURE_TRUE(aProperty, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-15 03:42:31 -07:00
|
|
|
nsCOMPtr<nsIDOMElement> element = GetElementContainerOrSelf(aNode);
|
2012-02-01 02:54:22 -08:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-15 03:42:31 -07:00
|
|
|
switch (aStyleType) {
|
2012-07-06 04:50:07 -07:00
|
|
|
case eComputed:
|
2012-07-08 02:50:31 -07:00
|
|
|
if (element) {
|
2012-07-15 03:42:31 -07:00
|
|
|
nsAutoString value, propString;
|
|
|
|
aProperty->ToString(propString);
|
|
|
|
// Get the all the computed css styles attached to the element node
|
2012-07-08 02:50:31 -07:00
|
|
|
nsRefPtr<nsComputedDOMStyle> cssDecl = GetComputedStyle(element);
|
|
|
|
NS_ENSURE_STATE(cssDecl);
|
2012-07-15 03:42:31 -07:00
|
|
|
// from these declarations, get the one we want and that one only
|
2012-07-08 02:50:31 -07:00
|
|
|
nsresult res = cssDecl->GetPropertyValue(propString, value);
|
2012-07-15 03:42:31 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
aValue.Assign(value);
|
|
|
|
}
|
|
|
|
break;
|
2012-07-06 04:50:07 -07:00
|
|
|
case eSpecified:
|
2012-07-15 03:42:31 -07:00
|
|
|
if (element) {
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> cssDecl;
|
|
|
|
PRUint32 length;
|
|
|
|
nsresult res = GetInlineStyles(element, getter_AddRefs(cssDecl), &length);
|
|
|
|
if (NS_FAILED(res) || !cssDecl) return res;
|
|
|
|
nsAutoString value, propString;
|
|
|
|
aProperty->ToString(propString);
|
|
|
|
res = cssDecl->GetPropertyValue(propString, value);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
aValue.Assign(value);
|
|
|
|
}
|
|
|
|
break;
|
2012-07-08 03:31:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-08 02:50:31 -07:00
|
|
|
already_AddRefed<nsComputedDOMStyle>
|
|
|
|
nsHTMLCSSUtils::GetComputedStyle(nsIDOMElement* aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-08 02:50:31 -07:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aElement);
|
|
|
|
return GetComputedStyle(element);
|
2012-02-10 02:04:47 -08:00
|
|
|
}
|
|
|
|
|
2012-07-08 02:50:31 -07:00
|
|
|
already_AddRefed<nsComputedDOMStyle>
|
|
|
|
nsHTMLCSSUtils::GetComputedStyle(dom::Element* aElement)
|
2012-02-10 02:04:47 -08:00
|
|
|
{
|
2012-07-08 02:50:31 -07:00
|
|
|
MOZ_ASSERT(aElement);
|
2012-07-08 02:50:31 -07:00
|
|
|
|
2012-07-08 02:50:31 -07:00
|
|
|
nsIDocument* doc = aElement->GetCurrentDoc();
|
|
|
|
NS_ASSERTION(doc, "Trying to compute style of detached element");
|
|
|
|
NS_ENSURE_TRUE(doc, nsnull);
|
|
|
|
|
|
|
|
nsIPresShell* presShell = doc->GetShell();
|
|
|
|
NS_ASSERTION(presShell, "Trying to compute style without PresShell");
|
|
|
|
NS_ENSURE_TRUE(presShell, nsnull);
|
|
|
|
|
|
|
|
nsRefPtr<nsComputedDOMStyle> style =
|
|
|
|
NS_NewComputedDOMStyle(aElement, EmptyString(), presShell);
|
|
|
|
|
|
|
|
return style.forget();
|
2007-03-22 10:30:00 -07: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
|
|
|
|
nsHTMLCSSUtils::RemoveCSSInlineStyle(nsIDOMNode *aNode, nsIAtom *aProperty, const nsAString & aPropertyValue)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(aNode);
|
|
|
|
|
|
|
|
// remove the property from the style attribute
|
2011-10-17 07:59:28 -07:00
|
|
|
nsresult res = RemoveCSSProperty(elem, aProperty, aPropertyValue, false);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-10 02:04:46 -08:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aNode);
|
|
|
|
if (!element || !element->IsHTML(nsGkAtoms::span) ||
|
|
|
|
nsHTMLEditor::HasAttributes(element)) {
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-02-10 02:04:46 -08:00
|
|
|
|
|
|
|
return mHTMLEditor->RemoveContainer(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Answers true is the property can be removed by setting a "none" CSS value
|
|
|
|
// on a node
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCSSUtils::IsCSSInvertable(nsIAtom *aProperty, const nsAString *aAttribute)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
return bool(nsEditProperty::b == aProperty);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default browser background color if we need it for GetCSSBackgroundColorState
|
2011-06-16 17:59:29 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCSSUtils::GetDefaultBackgroundColor(nsAString & aColor)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
if (Preferences::GetBool("editor.use_custom_colors", false)) {
|
2011-06-16 17:59:29 -07: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
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-06-16 17:59:29 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-06-16 17:59:29 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
if (Preferences::GetBool("browser.display.use_system_colors", false)) {
|
2011-06-16 17:59:29 -07: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
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default length unit used for CSS Indent/Outdent
|
2011-06-16 17:59:29 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCSSUtils::GetDefaultLengthUnit(nsAString & aLengthUnit)
|
|
|
|
{
|
2011-06-16 17:59:29 -07: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");
|
2007-03-22 10:30:00 -07: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...
|
|
|
|
void
|
|
|
|
nsHTMLCSSUtils::ParseLength(const nsAString & aString, float * aValue, nsIAtom ** aUnit)
|
|
|
|
{
|
|
|
|
nsAString::const_iterator iter;
|
|
|
|
aString.BeginReading(iter);
|
|
|
|
|
|
|
|
float a = 10.0f , b = 1.0f, value = 0;
|
|
|
|
PRInt8 sign = 1;
|
|
|
|
PRInt32 i = 0, j = aString.Length();
|
|
|
|
PRUnichar c;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool floatingPointFound = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
c = *iter;
|
|
|
|
if (PRUnichar('-') == c) { sign = -1; iter++; i++; }
|
|
|
|
else if (PRUnichar('+') == c) { iter++; i++; }
|
|
|
|
while (i < j) {
|
|
|
|
c = *iter;
|
|
|
|
if ((PRUnichar('0') == c) ||
|
|
|
|
(PRUnichar('1') == c) ||
|
|
|
|
(PRUnichar('2') == c) ||
|
|
|
|
(PRUnichar('3') == c) ||
|
|
|
|
(PRUnichar('4') == c) ||
|
|
|
|
(PRUnichar('5') == c) ||
|
|
|
|
(PRUnichar('6') == c) ||
|
|
|
|
(PRUnichar('7') == c) ||
|
|
|
|
(PRUnichar('8') == c) ||
|
|
|
|
(PRUnichar('9') == c)) {
|
|
|
|
value = (value * a) + (b * (c - PRUnichar('0')));
|
|
|
|
b = b / 10 * a;
|
|
|
|
}
|
|
|
|
else if (!floatingPointFound && (PRUnichar('.') == c)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
floatingPointFound = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
a = 1.0f; b = 0.1f;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
iter++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
*aValue = value * sign;
|
2010-06-08 16:25:27 -07:00
|
|
|
*aUnit = NS_NewAtom(StringTail(aString, j-i));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLCSSUtils::GetCSSPropertyAtom(nsCSSEditableProperty aProperty, nsIAtom ** aAtom)
|
|
|
|
{
|
|
|
|
*aAtom = nsnull;
|
2009-07-22 15:31:01 -07:00
|
|
|
switch (aProperty) {
|
|
|
|
case eCSSEditableProperty_background_color:
|
|
|
|
*aAtom = nsEditProperty::cssBackgroundColor;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_background_image:
|
|
|
|
*aAtom = nsEditProperty::cssBackgroundImage;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_border:
|
|
|
|
*aAtom = nsEditProperty::cssBorder;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_caption_side:
|
|
|
|
*aAtom = nsEditProperty::cssCaptionSide;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_color:
|
|
|
|
*aAtom = nsEditProperty::cssColor;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_float:
|
|
|
|
*aAtom = nsEditProperty::cssFloat;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_family:
|
|
|
|
*aAtom = nsEditProperty::cssFontFamily;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_size:
|
|
|
|
*aAtom = nsEditProperty::cssFontSize;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_style:
|
|
|
|
*aAtom = nsEditProperty::cssFontStyle;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_font_weight:
|
|
|
|
*aAtom = nsEditProperty::cssFontWeight;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_height:
|
|
|
|
*aAtom = nsEditProperty::cssHeight;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_list_style_type:
|
|
|
|
*aAtom = nsEditProperty::cssListStyleType;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_margin_left:
|
|
|
|
*aAtom = nsEditProperty::cssMarginLeft;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_margin_right:
|
|
|
|
*aAtom = nsEditProperty::cssMarginRight;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_text_align:
|
|
|
|
*aAtom = nsEditProperty::cssTextAlign;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_text_decoration:
|
|
|
|
*aAtom = nsEditProperty::cssTextDecoration;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_vertical_align:
|
|
|
|
*aAtom = nsEditProperty::cssVerticalAlign;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_whitespace:
|
|
|
|
*aAtom = nsEditProperty::cssWhitespace;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_width:
|
|
|
|
*aAtom = nsEditProperty::cssWidth;
|
|
|
|
break;
|
|
|
|
case eCSSEditableProperty_NONE:
|
|
|
|
// intentionally empty
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate aProperty and aValueArray with the CSS declarations equivalent to the
|
|
|
|
// value aValue according to the equivalence table aEquivTable
|
|
|
|
void
|
2009-04-22 01:43:15 -07:00
|
|
|
nsHTMLCSSUtils::BuildCSSDeclarations(nsTArray<nsIAtom*> & aPropertyArray,
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray<nsString> & aValueArray,
|
2007-03-22 10:30:00 -07:00
|
|
|
const CSSEquivTable * aEquivTable,
|
|
|
|
const nsAString * aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aGetOrRemoveRequest)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// clear arrays
|
|
|
|
aPropertyArray.Clear();
|
|
|
|
aValueArray.Clear();
|
|
|
|
|
|
|
|
// if we have an input value, let's use it
|
|
|
|
nsAutoString value, lowerCasedValue;
|
|
|
|
if (aValue) {
|
|
|
|
value.Assign(*aValue);
|
|
|
|
lowerCasedValue.Assign(*aValue);
|
|
|
|
ToLowerCase(lowerCasedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt8 index = 0;
|
|
|
|
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
|
|
|
|
(*aEquivTable[index].processValueFunctor) ((!aGetOrRemoveRequest || aEquivTable[index].caseSensitiveValue) ? &value : &lowerCasedValue,
|
|
|
|
cssValue,
|
|
|
|
aEquivTable[index].defaultValue,
|
|
|
|
aEquivTable[index].prependValue,
|
|
|
|
aEquivTable[index].appendValue);
|
|
|
|
GetCSSPropertyAtom(cssProperty, &cssPropertyAtom);
|
|
|
|
aPropertyArray.AppendElement(cssPropertyAtom);
|
2009-01-18 12:14:14 -08:00
|
|
|
aValueArray.AppendElement(cssValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
index++;
|
|
|
|
cssProperty = aEquivTable[index].cssProperty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate cssPropertyArray and cssValueArray with the declarations equivalent
|
|
|
|
// to aHTMLProperty/aAttribute/aValue for the node aNode
|
|
|
|
void
|
2012-02-10 02:04:47 -08:00
|
|
|
nsHTMLCSSUtils::GenerateCSSDeclarationsFromHTMLStyle(dom::Element* aElement,
|
|
|
|
nsIAtom* aHTMLProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
nsTArray<nsIAtom*>& cssPropertyArray,
|
|
|
|
nsTArray<nsString>& cssValueArray,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aGetOrRemoveRequest)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-10 02:04:47 -08:00
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
nsIAtom* tagName = aElement->Tag();
|
2012-04-24 03:57:23 -07:00
|
|
|
const nsHTMLCSSUtils::CSSEquivTable* equivTable = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (nsEditProperty::b == aHTMLProperty) {
|
2012-04-24 03:57:23 -07:00
|
|
|
equivTable = boldEquivTable;
|
|
|
|
} else if (nsEditProperty::i == aHTMLProperty) {
|
|
|
|
equivTable = italicEquivTable;
|
|
|
|
} else if (nsEditProperty::u == aHTMLProperty) {
|
|
|
|
equivTable = underlineEquivTable;
|
|
|
|
} else if (nsEditProperty::strike == aHTMLProperty) {
|
|
|
|
equivTable = strikeEquivTable;
|
|
|
|
} else if (nsEditProperty::tt == aHTMLProperty) {
|
|
|
|
equivTable = ttEquivTable;
|
|
|
|
} else if (aAttribute) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nsEditProperty::font == aHTMLProperty &&
|
|
|
|
aAttribute->EqualsLiteral("color")) {
|
2012-04-24 03:57:23 -07:00
|
|
|
equivTable = fontColorEquivTable;
|
|
|
|
} else if (nsEditProperty::font == aHTMLProperty &&
|
|
|
|
aAttribute->EqualsLiteral("face")) {
|
|
|
|
equivTable = fontFaceEquivTable;
|
2012-04-24 03:57:49 -07:00
|
|
|
} else if (nsEditProperty::font == aHTMLProperty &&
|
|
|
|
aAttribute->EqualsLiteral("size")) {
|
|
|
|
equivTable = fontSizeEquivTable;
|
2012-04-24 03:57:23 -07:00
|
|
|
} 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")) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nsEditProperty::table == tagName) {
|
2012-04-24 03:57:23 -07:00
|
|
|
equivTable = tableAlignEquivTable;
|
|
|
|
} else if (nsEditProperty::hr == tagName) {
|
|
|
|
equivTable = hrAlignEquivTable;
|
|
|
|
} else if (nsEditProperty::legend == tagName ||
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditProperty::caption == tagName) {
|
2012-04-24 03:57:23 -07:00
|
|
|
equivTable = captionAlignEquivTable;
|
|
|
|
} else {
|
|
|
|
equivTable = textAlignEquivTable;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-04-24 03:57:23 -07: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") ||
|
|
|
|
(nsEditProperty::hr == tagName &&
|
|
|
|
aAttribute->EqualsLiteral("size"))) {
|
|
|
|
equivTable = heightEquivTable;
|
|
|
|
} else if (aAttribute->EqualsLiteral("type") &&
|
|
|
|
(nsEditProperty::ol == tagName
|
|
|
|
|| nsEditProperty::ul == tagName
|
|
|
|
|| nsEditProperty::li == tagName)) {
|
|
|
|
equivTable = listStyleTypeEquivTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (equivTable) {
|
|
|
|
BuildCSSDeclarations(cssPropertyArray, cssValueArray, equivTable,
|
|
|
|
aValue, aGetOrRemoveRequest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 00:12:51 -07: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
|
|
|
|
// by the call. The dom::Element version returns aCount instead.
|
|
|
|
PRInt32
|
|
|
|
nsHTMLCSSUtils::SetCSSEquivalentToHTMLStyle(dom::Element* aElement,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
bool aSuppressTransaction)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aElement && aProperty);
|
|
|
|
MOZ_ASSERT_IF(aAttribute, aValue);
|
|
|
|
PRInt32 count;
|
|
|
|
// 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.
|
|
|
|
nsresult res = SetCSSEquivalentToHTMLStyle(aElement->AsDOMNode(),
|
|
|
|
aProperty, aAttribute,
|
|
|
|
aValue, &count,
|
|
|
|
aSuppressTransaction);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res), "SetCSSEquivalentToHTMLStyle failed");
|
|
|
|
NS_ENSURE_SUCCESS(res, count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::SetCSSEquivalentToHTMLStyle(nsIDOMNode * aNode,
|
|
|
|
nsIAtom *aHTMLProperty,
|
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue,
|
|
|
|
PRInt32 * aCount,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-10 02:04:47 -08:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
*aCount = 0;
|
2012-04-24 03:57:49 -07:00
|
|
|
if (!element || !IsCSSEditableProperty(element, aHTMLProperty,
|
|
|
|
aAttribute, aValue)) {
|
2012-02-10 02:04:47 -08: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);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(element);
|
|
|
|
// set the individual CSS inline styles
|
|
|
|
*aCount = cssPropertyArray.Length();
|
|
|
|
for (PRInt32 index = 0; index < *aCount; index++) {
|
|
|
|
nsresult res = SetCSSProperty(domElement, cssPropertyArray[index],
|
|
|
|
cssValueArray[index], aSuppressTransaction);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from aNode the CSS inline style equivalent to HTMLProperty/aAttribute/aValue for the node
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode,
|
|
|
|
nsIAtom *aHTMLProperty,
|
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-10 02:04:47 -08:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aNode);
|
|
|
|
if (!element || !IsCSSEditableProperty(element, aHTMLProperty, aAttribute)) {
|
|
|
|
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,
|
|
|
|
true);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(element);
|
|
|
|
// remove the individual CSS inline styles
|
|
|
|
PRInt32 count = cssPropertyArray.Length();
|
|
|
|
for (PRInt32 index = 0; index < count; index++) {
|
|
|
|
nsresult res = RemoveCSSProperty(domElement,
|
|
|
|
cssPropertyArray[index],
|
|
|
|
cssValueArray[index],
|
|
|
|
aSuppressTransaction);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07: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
|
2012-02-10 02:04:47 -08:00
|
|
|
nsHTMLCSSUtils::GetCSSEquivalentToHTMLInlineStyleSet(nsINode* aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom *aHTMLProperty,
|
|
|
|
const nsAString *aAttribute,
|
|
|
|
nsAString & aValueString,
|
2012-07-06 04:50:07 -07:00
|
|
|
StyleType aStyleType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aValueString.Truncate();
|
2012-02-10 02:04:47 -08:00
|
|
|
nsCOMPtr<dom::Element> theElement = GetElementContainerOrSelf(aNode);
|
2012-02-01 02:54:22 -08:00
|
|
|
NS_ENSURE_TRUE(theElement, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-24 03:57:49 -07:00
|
|
|
if (!theElement || !IsCSSEditableProperty(theElement, aHTMLProperty,
|
|
|
|
aAttribute, &aValueString)) {
|
2012-02-10 02:04:47 -08: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
|
|
|
|
GenerateCSSDeclarationsFromHTMLStyle(theElement, aHTMLProperty, aAttribute, nsnull,
|
|
|
|
cssPropertyArray, cssValueArray, true);
|
|
|
|
PRInt32 count = cssPropertyArray.Length();
|
|
|
|
for (PRInt32 index = 0; index < count; index++) {
|
|
|
|
nsAutoString valueString;
|
|
|
|
// retrieve the specified/computed value of the property
|
|
|
|
nsresult res = GetCSSInlinePropertyBase(theElement, cssPropertyArray[index],
|
2012-07-08 02:50:31 -07:00
|
|
|
valueString, aStyleType);
|
2012-02-10 02:04:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
// append the value to aValueString (possibly with a leading whitespace)
|
|
|
|
if (index) {
|
|
|
|
aValueString.Append(PRUnichar(' '));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-02-10 02:04:47 -08:00
|
|
|
aValueString.Append(valueString);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-17 10:50:19 -07: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 03:15:09 -07:00
|
|
|
//
|
|
|
|
// The nsIContent variant returns aIsSet instead of using an out parameter, and
|
|
|
|
// does not modify aValue.
|
|
|
|
bool
|
|
|
|
nsHTMLCSSUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsIContent* aContent,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString& aValue,
|
2012-07-06 04:50:07 -07:00
|
|
|
StyleType aStyleType)
|
2012-05-22 03:15:09 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContent && aProperty);
|
|
|
|
bool isSet;
|
|
|
|
nsAutoString value(aValue);
|
|
|
|
nsresult res = IsCSSEquivalentToHTMLInlineStyleSet(aContent->AsDOMNode(),
|
|
|
|
aProperty, aAttribute,
|
|
|
|
isSet, value, aStyleType);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res), "IsCSSEquivalentToHTMLInlineStyleSet failed");
|
|
|
|
NS_ENSURE_SUCCESS(res, false);
|
|
|
|
return isSet;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2012-04-17 10:50:19 -07:00
|
|
|
nsHTMLCSSUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode *aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom *aHTMLProperty,
|
2012-04-17 10:50:19 -07:00
|
|
|
const nsAString *aHTMLAttribute,
|
|
|
|
bool& aIsSet,
|
|
|
|
nsAString& valueString,
|
2012-07-06 04:50:07 -07:00
|
|
|
StyleType aStyleType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsAutoString htmlValueString(valueString);
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = false;
|
2012-02-10 02:04:47 -08:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
do {
|
|
|
|
valueString.Assign(htmlValueString);
|
|
|
|
// get the value of the CSS equivalent styles
|
|
|
|
nsresult res = GetCSSEquivalentToHTMLInlineStyleSet(node, aHTMLProperty, aHTMLAttribute,
|
|
|
|
valueString, aStyleType);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// early way out if we can
|
2012-04-17 10:50:19 -07:00
|
|
|
if (valueString.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (nsEditProperty::b == aHTMLProperty) {
|
2012-04-17 10:50:19 -07:00
|
|
|
if (valueString.EqualsLiteral("bold")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = true;
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (valueString.EqualsLiteral("normal")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = false;
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (valueString.EqualsLiteral("bolder")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = true;
|
2012-04-17 10:50:19 -07:00
|
|
|
valueString.AssignLiteral("bold");
|
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 weight = 0;
|
|
|
|
PRInt32 errorCode;
|
|
|
|
nsAutoString value(valueString);
|
|
|
|
weight = value.ToInteger(&errorCode, 10);
|
|
|
|
if (400 < weight) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = true;
|
2012-04-17 10:50:19 -07:00
|
|
|
valueString.AssignLiteral("bold");
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
valueString.AssignLiteral("normal");
|
|
|
|
}
|
|
|
|
}
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (nsEditProperty::i == aHTMLProperty) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (valueString.EqualsLiteral("italic") ||
|
|
|
|
valueString.EqualsLiteral("oblique")) {
|
2012-04-17 10:50:19 -07:00
|
|
|
aIsSet = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (nsEditProperty::u == aHTMLProperty) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString val;
|
|
|
|
val.AssignLiteral("underline");
|
2011-09-28 23:19:26 -07:00
|
|
|
aIsSet = bool(ChangeCSSInlineStyleTxn::ValueIncludes(valueString, val, false));
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (nsEditProperty::strike == aHTMLProperty) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString val;
|
|
|
|
val.AssignLiteral("line-through");
|
2011-09-28 23:19:26 -07:00
|
|
|
aIsSet = bool(ChangeCSSInlineStyleTxn::ValueIncludes(valueString, val, false));
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (aHTMLAttribute &&
|
|
|
|
((nsEditProperty::font == aHTMLProperty &&
|
|
|
|
aHTMLAttribute->EqualsLiteral("color")) ||
|
|
|
|
aHTMLAttribute->EqualsLiteral("bgcolor"))) {
|
|
|
|
if (htmlValueString.IsEmpty()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = true;
|
2012-04-17 10:50:19 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
nscolor rgba;
|
|
|
|
nsAutoString subStr;
|
2012-04-17 10:50:19 -07:00
|
|
|
htmlValueString.Right(subStr, htmlValueString.Length() - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_ColorNameToRGB(htmlValueString, &rgba) ||
|
|
|
|
NS_HexToRGB(subStr, &rgba)) {
|
|
|
|
nsAutoString htmlColor, tmpStr;
|
|
|
|
|
2012-05-28 04:20:27 -07: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(");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-28 04:20:27 -07:00
|
|
|
NS_NAMED_LITERAL_STRING(comma, ", ");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-28 04:20:27 -07:00
|
|
|
tmpStr.AppendInt(NS_GET_R(rgba), 10);
|
|
|
|
htmlColor.Append(tmpStr + comma);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-28 04:20:27 -07: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);
|
|
|
|
|
|
|
|
htmlColor.Append(PRUnichar(')'));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
aIsSet = htmlColor.Equals(valueString,
|
|
|
|
nsCaseInsensitiveStringComparator());
|
2012-04-17 10:50:19 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
aIsSet = htmlValueString.Equals(valueString,
|
|
|
|
nsCaseInsensitiveStringComparator());
|
2012-04-17 10:50:19 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (nsEditProperty::tt == aHTMLProperty) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aIsSet = StringBeginsWith(valueString, NS_LITERAL_STRING("monospace"));
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (nsEditProperty::font == aHTMLProperty && aHTMLAttribute &&
|
|
|
|
aHTMLAttribute->EqualsLiteral("face")) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!htmlValueString.IsEmpty()) {
|
|
|
|
const PRUnichar commaSpace[] = { PRUnichar(','), PRUnichar(' '), 0 };
|
|
|
|
const PRUnichar comma[] = { PRUnichar(','), 0 };
|
|
|
|
htmlValueString.ReplaceSubstring(commaSpace, comma);
|
|
|
|
nsAutoString valueStringNorm(valueString);
|
|
|
|
valueStringNorm.ReplaceSubstring(commaSpace, comma);
|
|
|
|
aIsSet = htmlValueString.Equals(valueStringNorm,
|
|
|
|
nsCaseInsensitiveStringComparator());
|
2012-04-17 10:50:19 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
// ignore this, it's TT or our default
|
|
|
|
nsAutoString valueStringLower;
|
|
|
|
ToLowerCase(valueString, valueStringLower);
|
|
|
|
aIsSet = !valueStringLower.EqualsLiteral("monospace") &&
|
|
|
|
!valueStringLower.EqualsLiteral("serif");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2012-04-24 03:57:49 -07:00
|
|
|
} else if (nsEditProperty::font == aHTMLProperty && aHTMLAttribute &&
|
|
|
|
aHTMLAttribute->EqualsLiteral("size")) {
|
|
|
|
if (htmlValueString.IsEmpty()) {
|
|
|
|
aIsSet = true;
|
|
|
|
} else {
|
|
|
|
PRInt32 size = nsContentUtils::ParseLegacyFontSize(htmlValueString);
|
|
|
|
aIsSet = (size == 1 && valueString.EqualsLiteral("x-small")) ||
|
|
|
|
(size == 2 && valueString.EqualsLiteral("small")) ||
|
|
|
|
(size == 3 && valueString.EqualsLiteral("medium")) ||
|
|
|
|
(size == 4 && valueString.EqualsLiteral("large")) ||
|
|
|
|
(size == 5 && valueString.EqualsLiteral("x-large")) ||
|
|
|
|
(size == 6 && valueString.EqualsLiteral("xx-large"));
|
|
|
|
}
|
2012-04-17 10:50:19 -07:00
|
|
|
} else if (aHTMLAttribute && aHTMLAttribute->EqualsLiteral("align")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = true;
|
2012-04-17 10:50:19 -07:00
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
aIsSet = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-17 10:50:19 -07:00
|
|
|
if (!htmlValueString.IsEmpty() &&
|
|
|
|
htmlValueString.Equals(valueString,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
aIsSet = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-05-28 04:20:27 -07:00
|
|
|
if (htmlValueString.EqualsLiteral("-moz-editor-invert-value")) {
|
|
|
|
aIsSet = !aIsSet;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nsEditProperty::u == aHTMLProperty || nsEditProperty::strike == aHTMLProperty) {
|
|
|
|
// 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
|
2012-02-10 02:04:47 -08:00
|
|
|
node = node->GetElementParent(); // set to null if it's not a dom element
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} while ((nsEditProperty::u == aHTMLProperty || nsEditProperty::strike == aHTMLProperty) &&
|
|
|
|
!aIsSet && node);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:03:17 -07:00
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsHTMLCSSUtils::SetCSSEnabled(bool aIsCSSPrefChecked)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mIsCSSPrefChecked = aIsCSSPrefChecked;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCSSUtils::IsCSSPrefChecked()
|
|
|
|
{
|
|
|
|
return mIsCSSPrefChecked ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ElementsSameStyle compares two elements and checks if they have the same
|
|
|
|
// specified CSS declarations in the STYLE attribute
|
|
|
|
// The answer is always negative if at least one of them carries an ID or a class
|
2012-05-29 00:12:51 -07:00
|
|
|
bool
|
|
|
|
nsHTMLCSSUtils::ElementsSameStyle(dom::Element* aFirstNode,
|
|
|
|
dom::Element* aSecondNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFirstNode && aSecondNode);
|
|
|
|
return ElementsSameStyle(aFirstNode->AsDOMNode(), aSecondNode->AsDOMNode());
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCSSUtils::ElementsSameStyle(nsIDOMNode *aFirstNode, nsIDOMNode *aSecondNode)
|
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMElement> firstElement = do_QueryInterface(aFirstNode);
|
|
|
|
nsCOMPtr<nsIDOMElement> secondElement = do_QueryInterface(aSecondNode);
|
|
|
|
|
|
|
|
NS_ASSERTION((firstElement && secondElement), "Non element nodes passed to ElementsSameStyle.");
|
|
|
|
|
|
|
|
nsAutoString firstID, secondID;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isFirstIDSet, isSecondIDSet;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mHTMLEditor->GetAttributeValue(firstElement, NS_LITERAL_STRING("id"), firstID, &isFirstIDSet);
|
|
|
|
res = mHTMLEditor->GetAttributeValue(secondElement, NS_LITERAL_STRING("id"), secondID, &isSecondIDSet);
|
|
|
|
if (isFirstIDSet || isSecondIDSet) {
|
|
|
|
// 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 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString firstClass, secondClass;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isFirstClassSet, isSecondClassSet;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mHTMLEditor->GetAttributeValue(firstElement, NS_LITERAL_STRING("class"), firstClass, &isFirstClassSet);
|
|
|
|
res = mHTMLEditor->GetAttributeValue(secondElement, NS_LITERAL_STRING("class"), secondClass, &isSecondClassSet);
|
|
|
|
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 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isFirstClassSet || isSecondClassSet) {
|
|
|
|
// one span only carries a class, early way out
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> firstCSSDecl, secondCSSDecl;
|
|
|
|
PRUint32 firstLength, secondLength;
|
|
|
|
res = GetInlineStyles(firstElement, getter_AddRefs(firstCSSDecl), &firstLength);
|
2011-10-17 07:59:28 -07:00
|
|
|
if (NS_FAILED(res) || !firstCSSDecl) return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = GetInlineStyles(secondElement, getter_AddRefs(secondCSSDecl), &secondLength);
|
2011-10-17 07:59:28 -07:00
|
|
|
if (NS_FAILED(res) || !secondCSSDecl) return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (firstLength != secondLength) {
|
|
|
|
// early way out if we can
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (0 == firstLength) {
|
|
|
|
// no inline style !
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 i;
|
|
|
|
nsAutoString propertyNameString;
|
|
|
|
nsAutoString firstValue, secondValue;
|
|
|
|
for (i=0; i<firstLength; i++) {
|
|
|
|
firstCSSDecl->Item(i, propertyNameString);
|
|
|
|
firstCSSDecl->GetPropertyValue(propertyNameString, firstValue);
|
|
|
|
secondCSSDecl->GetPropertyValue(propertyNameString, secondValue);
|
|
|
|
if (!firstValue.Equals(secondValue)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i=0; i<secondLength; i++) {
|
|
|
|
secondCSSDecl->Item(i, propertyNameString);
|
|
|
|
secondCSSDecl->GetPropertyValue(propertyNameString, secondValue);
|
|
|
|
firstCSSDecl->GetPropertyValue(propertyNameString, firstValue);
|
|
|
|
if (!firstValue.Equals(secondValue)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::GetInlineStyles(nsIDOMElement *aElement,
|
|
|
|
nsIDOMCSSStyleDeclaration **aCssDecl,
|
|
|
|
PRUint32 *aLength)
|
|
|
|
{
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aElement && aLength, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
*aLength = 0;
|
|
|
|
nsCOMPtr<nsIDOMElementCSSInlineStyle> inlineStyles = do_QueryInterface(aElement);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(inlineStyles, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult res = inlineStyles->GetStyle(aCssDecl);
|
|
|
|
if (NS_FAILED(res) || !aCssDecl) return NS_ERROR_NULL_POINTER;
|
|
|
|
(*aCssDecl)->GetLength(aLength);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
already_AddRefed<nsIDOMElement>
|
|
|
|
nsHTMLCSSUtils::GetElementContainerOrSelf(nsIDOMNode* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-01 02:54:22 -08:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, nsnull);
|
2012-02-10 02:04:46 -08:00
|
|
|
nsCOMPtr<nsIDOMElement> element =
|
|
|
|
do_QueryInterface(GetElementContainerOrSelf(node));
|
|
|
|
return element.forget();
|
|
|
|
}
|
2009-02-18 23:55:40 -08:00
|
|
|
|
2012-02-10 02:04:46 -08:00
|
|
|
dom::Element*
|
|
|
|
nsHTMLCSSUtils::GetElementContainerOrSelf(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
if (nsIDOMNode::DOCUMENT_NODE == aNode->NodeType()) {
|
2012-02-01 02:54:22 -08:00
|
|
|
return nsnull;
|
2009-02-18 23:55:40 -08:00
|
|
|
}
|
|
|
|
|
2012-02-10 02:04:46 -08:00
|
|
|
nsINode* node = aNode;
|
2012-02-01 02:54:22 -08:00
|
|
|
// Loop until we find an element.
|
|
|
|
while (node && !node->IsElement()) {
|
|
|
|
node = node->GetNodeParent();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-02-01 02:54:22 -08:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(node, nsnull);
|
2012-02-10 02:04:46 -08:00
|
|
|
return node->AsElement();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::SetCSSProperty(nsIDOMElement * aElement,
|
|
|
|
const nsAString & aProperty,
|
|
|
|
const nsAString & aValue)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> cssDecl;
|
|
|
|
PRUint32 length;
|
|
|
|
nsresult res = GetInlineStyles(aElement, getter_AddRefs(cssDecl), &length);
|
|
|
|
if (NS_FAILED(res) || !cssDecl) return res;
|
|
|
|
|
|
|
|
return cssDecl->SetProperty(aProperty,
|
|
|
|
aValue,
|
|
|
|
EmptyString());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCSSUtils::SetCSSPropertyPixels(nsIDOMElement * aElement,
|
|
|
|
const nsAString & aProperty,
|
|
|
|
PRInt32 aIntValue)
|
|
|
|
{
|
|
|
|
nsAutoString s;
|
|
|
|
s.AppendInt(aIntValue);
|
|
|
|
return SetCSSProperty(aElement, aProperty, s + NS_LITERAL_STRING("px"));
|
|
|
|
}
|