Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLPreElement.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 "nsIDOMHTMLPreElement.h"
00038 #include "nsIDOMEventReceiver.h"
00039 #include "nsGenericHTMLElement.h"
00040 #include "nsHTMLAtoms.h"
00041 #include "nsStyleConsts.h"
00042 #include "nsPresContext.h"
00043 #include "nsMappedAttributes.h"
00044 #include "nsRuleData.h"
00045 #include "nsCSSStruct.h"
00046 
00047 // XXX wrap, variable, cols, tabstop
00048 
00049 
00050 class nsHTMLPreElement : public nsGenericHTMLElement,
00051                          public nsIDOMHTMLPreElement
00052 {
00053 public:
00054   nsHTMLPreElement(nsINodeInfo *aNodeInfo);
00055   virtual ~nsHTMLPreElement();
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   // nsIDOMHTMLPreElement
00070   NS_IMETHOD GetWidth(PRInt32* aWidth);
00071   NS_IMETHOD SetWidth(PRInt32 aWidth);
00072 
00073   virtual PRBool ParseAttribute(nsIAtom* aAttribute,
00074                                 const nsAString& aValue,
00075                                 nsAttrValue& aResult);
00076   NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
00077   virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
00078 };
00079 
00080 
00081 NS_IMPL_NS_NEW_HTML_ELEMENT(Pre)
00082 
00083 
00084 nsHTMLPreElement::nsHTMLPreElement(nsINodeInfo *aNodeInfo)
00085   : nsGenericHTMLElement(aNodeInfo)
00086 {
00087 }
00088 
00089 nsHTMLPreElement::~nsHTMLPreElement()
00090 {
00091 }
00092 
00093 
00094 NS_IMPL_ADDREF_INHERITED(nsHTMLPreElement, nsGenericElement)
00095 NS_IMPL_RELEASE_INHERITED(nsHTMLPreElement, nsGenericElement)
00096 
00097 
00098 // QueryInterface implementation for nsHTMLPreElement
00099 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLPreElement, nsGenericHTMLElement)
00100   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLPreElement)
00101   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLPreElement)
00102 NS_HTML_CONTENT_INTERFACE_MAP_END
00103 
00104 
00105 NS_IMPL_DOM_CLONENODE(nsHTMLPreElement)
00106 
00107 
00108 NS_IMPL_INT_ATTR(nsHTMLPreElement, Width, width)
00109 
00110 
00111 PRBool
00112 nsHTMLPreElement::ParseAttribute(nsIAtom* aAttribute,
00113                                  const nsAString& aValue,
00114                                  nsAttrValue& aResult)
00115 {
00116   if (aAttribute == nsHTMLAtoms::cols) {
00117     return aResult.ParseIntWithBounds(aValue, 0);
00118   }
00119   if (aAttribute == nsHTMLAtoms::width) {
00120     return aResult.ParseIntWithBounds(aValue, 0);
00121   }
00122 
00123   return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
00124 }
00125 
00126 static void
00127 MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
00128                       nsRuleData* aData)
00129 {
00130   if (aData->mSID == eStyleStruct_Font) {
00131     // variable
00132     if (aAttributes->GetAttr(nsHTMLAtoms::variable))
00133       aData->mFontData->mFamily.SetStringValue(NS_LITERAL_STRING("serif"),
00134                                                eCSSUnit_String);
00135   }
00136   else if (aData->mSID == eStyleStruct_Position) {
00137     if (aData->mPositionData->mWidth.GetUnit() == eCSSUnit_Null) {
00138       // width: int (html4 attribute == nav4 cols)
00139       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::width);
00140       if (!value || value->Type() != nsAttrValue::eInteger) {
00141         // cols: int (nav4 attribute)
00142         value = aAttributes->GetAttr(nsHTMLAtoms::cols);
00143       }
00144 
00145       if (value && value->Type() == nsAttrValue::eInteger)
00146         aData->mPositionData->mWidth.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Char);
00147     }
00148   }
00149   else if (aData->mSID == eStyleStruct_Text) {
00150     if (aData->mTextData->mWhiteSpace.GetUnit() == eCSSUnit_Null) {
00151       // wrap: empty
00152       if (aAttributes->GetAttr(nsHTMLAtoms::wrap))
00153         aData->mTextData->mWhiteSpace.SetIntValue(NS_STYLE_WHITESPACE_MOZ_PRE_WRAP, eCSSUnit_Enumerated);
00154 
00155       // width: int (html4 attribute == nav4 cols)
00156       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::width);
00157       if (!value || value->Type() != nsAttrValue::eInteger) {
00158         // cols: int (nav4 attribute)
00159         value = aAttributes->GetAttr(nsHTMLAtoms::cols);
00160       }
00161 
00162       if (value && value->Type() == nsAttrValue::eInteger) {
00163         // Force wrap property on since we want to wrap at a width
00164         // boundary not just a newline.
00165         aData->mTextData->mWhiteSpace.SetIntValue(NS_STYLE_WHITESPACE_MOZ_PRE_WRAP, eCSSUnit_Enumerated);
00166       }
00167     }
00168   }
00169 
00170   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
00171 }
00172 
00173 NS_IMETHODIMP_(PRBool)
00174 nsHTMLPreElement::IsAttributeMapped(const nsIAtom* aAttribute) const
00175 {
00176   static const MappedAttributeEntry attributes[] = {
00177     { &nsHTMLAtoms::variable },
00178     { &nsHTMLAtoms::wrap },
00179     { &nsHTMLAtoms::cols },
00180     { &nsHTMLAtoms::width },
00181     { nsnull },
00182   };
00183   
00184   static const MappedAttributeEntry* const map[] = {
00185     attributes,
00186     sCommonAttributeMap,
00187   };
00188 
00189   return FindAttributeDependence(aAttribute, map, NS_ARRAY_LENGTH(map));
00190 }
00191 
00192 nsMapRuleToAttributesFunc
00193 nsHTMLPreElement::GetAttributeMappingFunction() const
00194 {
00195   return &MapAttributesIntoRule;
00196 }