Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLFontElement.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsCOMPtr.h"
00038 #include "nsIDOMHTMLFontElement.h"
00039 #include "nsIDOMEventReceiver.h"
00040 #include "nsGenericHTMLElement.h"
00041 #include "nsHTMLAtoms.h"
00042 #include "nsIDeviceContext.h"
00043 #include "nsStyleConsts.h"
00044 #include "nsPresContext.h"
00045 #include "nsMappedAttributes.h"
00046 #include "nsCSSStruct.h"
00047 #include "nsRuleData.h"
00048 #include "nsIDocument.h"
00049 
00050 class nsHTMLFontElement : public nsGenericHTMLElement,
00051                           public nsIDOMHTMLFontElement
00052 {
00053 public:
00054   nsHTMLFontElement(nsINodeInfo *aNodeInfo);
00055   virtual ~nsHTMLFontElement();
00056 
00057   // nsISupports
00058   NS_DECL_ISUPPORTS_INHERITED
00059 
00060   // nsIDOMNode
00061   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLElement::)
00062 
00063   // nsIDOMElement
00064   NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLElement::)
00065 
00066   // nsIDOMHTMLElement
00067   NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLElement::)
00068 
00069   // nsIDOMHTMLFontElement
00070   NS_DECL_NSIDOMHTMLFONTELEMENT
00071 
00072   virtual PRBool ParseAttribute(nsIAtom* aAttribute,
00073                                 const nsAString& aValue,
00074                                 nsAttrValue& aResult);
00075   NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
00076   virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
00077 };
00078 
00079 
00080 NS_IMPL_NS_NEW_HTML_ELEMENT(Font)
00081 
00082 
00083 nsHTMLFontElement::nsHTMLFontElement(nsINodeInfo *aNodeInfo)
00084   : nsGenericHTMLElement(aNodeInfo)
00085 {
00086 }
00087 
00088 nsHTMLFontElement::~nsHTMLFontElement()
00089 {
00090 }
00091 
00092 NS_IMPL_ADDREF_INHERITED(nsHTMLFontElement, nsGenericElement)
00093 NS_IMPL_RELEASE_INHERITED(nsHTMLFontElement, nsGenericElement)
00094 
00095 
00096 // QueryInterface implementation for nsHTMLFontElement
00097 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLFontElement, nsGenericHTMLElement)
00098   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLFontElement)
00099   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLFontElement)
00100 NS_HTML_CONTENT_INTERFACE_MAP_END
00101 
00102 
00103 NS_IMPL_DOM_CLONENODE(nsHTMLFontElement)
00104 
00105 
00106 NS_IMPL_STRING_ATTR(nsHTMLFontElement, Color, color)
00107 NS_IMPL_STRING_ATTR(nsHTMLFontElement, Face, face)
00108 NS_IMPL_STRING_ATTR(nsHTMLFontElement, Size, size)
00109 
00110 static const nsAttrValue::EnumTable kRelFontSizeTable[] = {
00111   { "-10", -10 },
00112   { "-9", -9 },
00113   { "-8", -8 },
00114   { "-7", -7 },
00115   { "-6", -6 },
00116   { "-5", -5 },
00117   { "-4", -4 },
00118   { "-3", -3 },
00119   { "-2", -2 },
00120   { "-1", -1 },
00121   { "-0", 0 },
00122   { "+0", 0 },
00123   { "+1", 1 },
00124   { "+2", 2 },
00125   { "+3", 3 },
00126   { "+4", 4 },
00127   { "+5", 5 },
00128   { "+6", 6 },
00129   { "+7", 7 },
00130   { "+8", 8 },
00131   { "+9", 9 },
00132   { "+10", 10 },
00133   { 0 }
00134 };
00135 
00136 
00137 PRBool
00138 nsHTMLFontElement::ParseAttribute(nsIAtom* aAttribute,
00139                                   const nsAString& aValue,
00140                                   nsAttrValue& aResult)
00141 {
00142   if (aAttribute == nsHTMLAtoms::size) {
00143     nsAutoString tmp(aValue);
00144     tmp.CompressWhitespace(PR_TRUE, PR_TRUE);
00145     PRUnichar ch = tmp.IsEmpty() ? 0 : tmp.First();
00146     if ((ch == '+' || ch == '-') &&
00147         aResult.ParseEnumValue(aValue, kRelFontSizeTable)) {
00148       return PR_TRUE;
00149     }
00150 
00151     return aResult.ParseIntValue(aValue);
00152   }
00153   if (aAttribute == nsHTMLAtoms::pointSize ||
00154       aAttribute == nsHTMLAtoms::fontWeight) {
00155     return aResult.ParseIntValue(aValue);
00156   }
00157   if (aAttribute == nsHTMLAtoms::color) {
00158     return aResult.ParseColor(aValue, GetOwnerDoc());
00159   }
00160 
00161   return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
00162 }
00163 
00164 static void
00165 MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
00166                       nsRuleData* aData)
00167 {
00168   if (aData->mSID == eStyleStruct_Font) {
00169     nsRuleDataFont& font = *(aData->mFontData);
00170     
00171     // face: string list
00172     if (font.mFamily.GetUnit() == eCSSUnit_Null) {
00173       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::face);
00174       if (value && value->Type() == nsAttrValue::eString &&
00175           !value->IsEmptyString()) {
00176         font.mFamily.SetStringValue(value->GetStringValue(), eCSSUnit_String);
00177         font.mFamilyFromHTML = PR_TRUE;
00178       }
00179     }
00180 
00181     // pointSize: int
00182     if (font.mSize.GetUnit() == eCSSUnit_Null) {
00183       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::pointSize);
00184       if (value && value->Type() == nsAttrValue::eInteger)
00185         font.mSize.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Point);
00186       else {
00187         // size: int, enum , 
00188         value = aAttributes->GetAttr(nsHTMLAtoms::size);
00189         if (value) {
00190           nsAttrValue::ValueType unit = value->Type();
00191           if (unit == nsAttrValue::eInteger || unit == nsAttrValue::eEnum) { 
00192             PRInt32 size;
00193             if (unit == nsAttrValue::eEnum) // int (+/-)
00194               size = value->GetEnumValue() + 3;  // XXX should be BASEFONT, not three see bug 3875
00195             else
00196               size = value->GetIntegerValue();
00197 
00198             size = ((0 < size) ? ((size < 8) ? size : 7) : 1); 
00199             font.mSize.SetIntValue(size, eCSSUnit_Enumerated);
00200           }
00201         }
00202       }
00203     }
00204 
00205     // fontWeight: int
00206     if (font.mWeight.GetUnit() == eCSSUnit_Null) {
00207       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::fontWeight);
00208       if (value && value->Type() == nsAttrValue::eInteger) // +/-
00209         font.mWeight.SetIntValue(value->GetIntegerValue(), eCSSUnit_Integer);
00210     }
00211   }
00212   else if (aData->mSID == eStyleStruct_Color) {
00213     if (aData->mColorData->mColor.GetUnit() == eCSSUnit_Null) {
00214       // color: color
00215       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::color);
00216       nscolor color;
00217       if (value && value->GetColorValue(color)) {
00218         aData->mColorData->mColor.SetColorValue(color);
00219       }
00220     }
00221   }
00222   else if (aData->mSID == eStyleStruct_TextReset) {
00223     // Make <a><font color="red">text</font></a> give the text a red underline
00224     // in quirks mode.  The NS_STYLE_TEXT_DECORATION_OVERRIDE_ALL flag only
00225     // affects quirks mode rendering.
00226     const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::color);
00227     nscolor color;
00228     if (value && value->GetColorValue(color)) {
00229       nsCSSValue& decoration = aData->mTextData->mDecoration;
00230       PRInt32 newValue = NS_STYLE_TEXT_DECORATION_OVERRIDE_ALL;
00231       if (decoration.GetUnit() == eCSSUnit_Enumerated) {
00232         newValue |= decoration.GetIntValue();
00233       }
00234       decoration.SetIntValue(newValue, eCSSUnit_Enumerated);
00235     }
00236   }
00237 
00238   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
00239 }
00240 
00241 NS_IMETHODIMP_(PRBool)
00242 nsHTMLFontElement::IsAttributeMapped(const nsIAtom* aAttribute) const
00243 {
00244   static const MappedAttributeEntry attributes[] = {
00245     { &nsHTMLAtoms::face },
00246     { &nsHTMLAtoms::pointSize },
00247     { &nsHTMLAtoms::size },
00248     { &nsHTMLAtoms::fontWeight },
00249     { &nsHTMLAtoms::color },
00250     { nsnull }
00251   };
00252 
00253   static const MappedAttributeEntry* const map[] = {
00254     attributes,
00255     sCommonAttributeMap,
00256   };
00257 
00258   return FindAttributeDependence(aAttribute, map, NS_ARRAY_LENGTH(map));
00259 }
00260 
00261 
00262 nsMapRuleToAttributesFunc
00263 nsHTMLFontElement::GetAttributeMappingFunction() const
00264 {
00265   return &MapAttributesIntoRule;
00266 }