gecko-dev/content/html/content/src/nsHTMLFontElement.cpp

333 lines
10 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
1998-09-02 00:56:01 +00:00
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
1998-09-02 00:56:01 +00:00
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
1998-09-02 00:56:01 +00:00
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsCOMPtr.h"
1998-09-02 00:56:01 +00:00
#include "nsIDOMHTMLFontElement.h"
#include "nsIDOMEventReceiver.h"
#include "nsIHTMLContent.h"
1998-09-02 23:53:16 +00:00
#include "nsGenericHTMLElement.h"
1998-09-02 00:56:01 +00:00
#include "nsHTMLAtoms.h"
#include "nsIDeviceContext.h"
1998-09-02 00:56:01 +00:00
#include "nsStyleConsts.h"
#include "nsIPresContext.h"
#include "nsHTMLAttributes.h"
#include "nsCSSStruct.h"
#include "nsRuleNode.h"
1998-09-02 00:56:01 +00:00
class nsHTMLFontElement : public nsGenericHTMLContainerElement,
public nsIDOMHTMLFontElement
1998-09-02 00:56:01 +00:00
{
public:
nsHTMLFontElement();
virtual ~nsHTMLFontElement();
1998-09-02 00:56:01 +00:00
// nsISupports
NS_DECL_ISUPPORTS_INHERITED
1998-09-02 00:56:01 +00:00
// nsIDOMNode
NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLContainerElement::)
1998-09-02 00:56:01 +00:00
// nsIDOMElement
NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLContainerElement::)
1998-09-02 00:56:01 +00:00
// nsIDOMHTMLElement
NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLContainerElement::)
1998-09-02 00:56:01 +00:00
// nsIDOMHTMLFontElement
NS_DECL_NSIDOMHTMLFONTELEMENT
1998-09-02 00:56:01 +00:00
NS_IMETHOD StringToAttribute(nsIAtom* aAttribute,
const nsAString& aValue,
nsHTMLValue& aResult);
NS_IMETHOD AttributeToString(nsIAtom* aAttribute,
const nsHTMLValue& aValue,
nsAString& aResult) const;
2001-08-25 02:01:08 +00:00
NS_IMETHOD GetMappedAttributeImpact(const nsIAtom* aAttribute, PRInt32 aModType,
nsChangeHint& aHint) const;
NS_IMETHOD GetAttributeMappingFunction(nsMapRuleToAttributesFunc& aMapRuleFunc) const;
1998-09-02 00:56:01 +00:00
};
nsresult
NS_NewHTMLFontElement(nsIHTMLContent** aInstancePtrResult,
nsINodeInfo *aNodeInfo)
1998-09-02 00:56:01 +00:00
{
NS_ENSURE_ARG_POINTER(aInstancePtrResult);
nsHTMLFontElement* it = new nsHTMLFontElement();
if (!it) {
1998-09-02 00:56:01 +00:00
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = it->Init(aNodeInfo);
if (NS_FAILED(rv)) {
delete it;
return rv;
}
*aInstancePtrResult = NS_STATIC_CAST(nsIHTMLContent *, it);
NS_ADDREF(*aInstancePtrResult);
return NS_OK;
1998-09-02 00:56:01 +00:00
}
nsHTMLFontElement::nsHTMLFontElement()
1998-09-02 00:56:01 +00:00
{
}
1998-09-02 23:53:16 +00:00
nsHTMLFontElement::~nsHTMLFontElement()
1998-09-02 00:56:01 +00:00
{
}
NS_IMPL_ADDREF_INHERITED(nsHTMLFontElement, nsGenericElement);
NS_IMPL_RELEASE_INHERITED(nsHTMLFontElement, nsGenericElement);
1998-09-02 00:56:01 +00:00
// QueryInterface implementation for nsHTMLFontElement
NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLFontElement,
nsGenericHTMLContainerElement)
NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLFontElement)
NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLFontElement)
NS_HTML_CONTENT_INTERFACE_MAP_END
1998-09-02 00:56:01 +00:00
nsresult
nsHTMLFontElement::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
1998-09-02 00:56:01 +00:00
{
NS_ENSURE_ARG_POINTER(aReturn);
*aReturn = nsnull;
nsHTMLFontElement* it = new nsHTMLFontElement();
if (!it) {
1998-09-02 00:56:01 +00:00
return NS_ERROR_OUT_OF_MEMORY;
}
nsCOMPtr<nsIDOMNode> kungFuDeathGrip(it);
nsresult rv = it->Init(mNodeInfo);
if (NS_FAILED(rv))
return rv;
CopyInnerTo(this, it, aDeep);
*aReturn = NS_STATIC_CAST(nsIDOMNode *, it);
NS_ADDREF(*aReturn);
return NS_OK;
1998-09-02 00:56:01 +00:00
}
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Color, color)
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Face, face)
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Size, size)
1998-09-02 00:56:01 +00:00
1998-09-02 00:56:01 +00:00
NS_IMETHODIMP
1998-09-02 23:53:16 +00:00
nsHTMLFontElement::StringToAttribute(nsIAtom* aAttribute,
const nsAString& aValue,
1998-09-02 00:56:01 +00:00
nsHTMLValue& aResult)
{
if ((aAttribute == nsHTMLAtoms::size) ||
(aAttribute == nsHTMLAtoms::pointSize) ||
(aAttribute == nsHTMLAtoms::fontWeight)) {
nsAutoString tmp(aValue);
//rickg: fixed flaw where ToInteger error code was not being checked.
// This caused wrong default value for font size.
PRInt32 ec, v = tmp.ToInteger(&ec);
if(NS_SUCCEEDED(ec)){
tmp.CompressWhitespace(PR_TRUE, PR_FALSE);
PRUnichar ch = tmp.IsEmpty() ? 0 : tmp.First();
aResult.SetIntValue(v, ((ch == '+') || (ch == '-')) ?
eHTMLUnit_Integer : eHTMLUnit_Enumerated);
return NS_CONTENT_ATTR_HAS_VALUE;
}
}
else if (aAttribute == nsHTMLAtoms::color) {
if (aResult.ParseColor(aValue, mDocument)) {
return NS_CONTENT_ATTR_HAS_VALUE;
}
}
1998-09-02 00:56:01 +00:00
return NS_CONTENT_ATTR_NOT_THERE;
}
NS_IMETHODIMP
1998-09-02 23:53:16 +00:00
nsHTMLFontElement::AttributeToString(nsIAtom* aAttribute,
1998-12-20 01:21:23 +00:00
const nsHTMLValue& aValue,
nsAString& aResult) const
1998-09-02 00:56:01 +00:00
{
if ((aAttribute == nsHTMLAtoms::size) ||
(aAttribute == nsHTMLAtoms::pointSize) ||
(aAttribute == nsHTMLAtoms::fontWeight)) {
aResult.Truncate();
nsAutoString intVal;
if (aValue.GetUnit() == eHTMLUnit_Enumerated) {
intVal.AppendInt(aValue.GetIntValue(), 10);
aResult.Append(intVal);
return NS_CONTENT_ATTR_HAS_VALUE;
}
else if (aValue.GetUnit() == eHTMLUnit_Integer) {
PRInt32 value = aValue.GetIntValue();
if (value >= 0) {
aResult.Append(NS_LITERAL_STRING("+"));
}
intVal.AppendInt(value, 10);
aResult.Append(intVal);
return NS_CONTENT_ATTR_HAS_VALUE;
}
return NS_CONTENT_ATTR_NOT_THERE;
}
return nsGenericHTMLContainerElement::AttributeToString(aAttribute, aValue,
aResult);
1998-09-02 00:56:01 +00:00
}
static void
MapAttributesIntoRule(const nsIHTMLMappedAttributes* aAttributes,
nsRuleData* aData)
1998-09-02 00:56:01 +00:00
{
if (!aData)
return;
if (aData->mFontData) {
nsRuleDataFont& font = *(aData->mFontData);
nsHTMLValue value;
// face: string list
if (font.mFamily.GetUnit() == eCSSUnit_Null) {
aAttributes->GetAttribute(nsHTMLAtoms::face, value);
if (value.GetUnit() == eHTMLUnit_String) {
nsAutoString familyList;
value.GetStringValue(familyList);
if (!familyList.IsEmpty()) {
font.mFamily.SetStringValue(familyList, eCSSUnit_String);
font.mFamilyFromHTML = PR_TRUE;
}
}
}
// pointSize: int, enum
if (font.mSize.GetUnit() == eCSSUnit_Null) {
aAttributes->GetAttribute(nsHTMLAtoms::pointSize, value);
if (value.GetUnit() == eHTMLUnit_Integer ||
value.GetUnit() == eHTMLUnit_Enumerated) {
PRInt32 val = value.GetIntValue();
font.mSize.SetFloatValue((float)val, eCSSUnit_Point);
}
else {
// size: int, enum ,
aAttributes->GetAttribute(nsHTMLAtoms::size, value);
nsHTMLUnit unit = value.GetUnit();
if (unit == eHTMLUnit_Integer || unit == eHTMLUnit_Enumerated) {
PRInt32 size = value.GetIntValue();
if (unit == eHTMLUnit_Integer) // int (+/-)
size += 3; // XXX should be BASEFONT, not three
size = ((0 < size) ? ((size < 8) ? size : 7) : 1);
font.mSize.SetIntValue(size, eCSSUnit_Enumerated);
}
}
}
// fontWeight: int, enum
if (font.mWeight.GetUnit() == eCSSUnit_Null) {
aAttributes->GetAttribute(nsHTMLAtoms::fontWeight, value);
if (value.GetUnit() == eHTMLUnit_Integer) // +/-
font.mWeight.SetIntValue(value.GetIntValue(), eCSSUnit_Integer);
else if (value.GetUnit() == eHTMLUnit_Enumerated)
font.mWeight.SetIntValue(value.GetIntValue(), eCSSUnit_Enumerated);
}
}
else if (aData->mColorData && aData->mSID == eStyleStruct_Color) {
if (aData->mColorData->mColor.GetUnit() == eCSSUnit_Null) {
// color: color
nsHTMLValue value;
if (NS_CONTENT_ATTR_NOT_THERE !=
aAttributes->GetAttribute(nsHTMLAtoms::color, value)) {
if (((eHTMLUnit_Color == value.GetUnit())) ||
(eHTMLUnit_ColorName == value.GetUnit()))
aData->mColorData->mColor.SetColorValue(value.GetColorValue());
}
}
}
else if (aData->mTextData && aData->mSID == eStyleStruct_TextReset) {
nsHTMLValue value;
if (NS_CONTENT_ATTR_NOT_THERE !=
aAttributes->GetAttribute(nsHTMLAtoms::color, value)) {
if (((eHTMLUnit_Color == value.GetUnit())) ||
(eHTMLUnit_ColorName == value.GetUnit()))
aData->mTextData->mDecoration.SetIntValue(NS_STYLE_TEXT_DECORATION_OVERRIDE_ALL, eCSSUnit_Enumerated);
}
}
nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
}
NS_IMETHODIMP
nsHTMLFontElement::GetMappedAttributeImpact(const nsIAtom* aAttribute, PRInt32 aModType,
nsChangeHint& aHint) const
{
if (aAttribute == nsHTMLAtoms::color) {
aHint = NS_STYLE_HINT_VISUAL;
}
else if ((aAttribute == nsHTMLAtoms::face) ||
(aAttribute == nsHTMLAtoms::pointSize) ||
(aAttribute == nsHTMLAtoms::size) ||
(aAttribute == nsHTMLAtoms::fontWeight)) {
aHint = NS_STYLE_HINT_REFLOW;
}
else if (!GetCommonMappedAttributesImpact(aAttribute, aHint)) {
aHint = NS_STYLE_HINT_CONTENT;
}
return NS_OK;
}
NS_IMETHODIMP
nsHTMLFontElement::GetAttributeMappingFunction(nsMapRuleToAttributesFunc& aMapRuleFunc) const
{
aMapRuleFunc = &MapAttributesIntoRule;
return NS_OK;
1998-09-02 00:56:01 +00:00
}