Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLTableColElement.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 "nsIDOMHTMLTableColElement.h"
00038 #include "nsIDOMEventReceiver.h"
00039 #include "nsMappedAttributes.h"
00040 #include "nsGenericHTMLElement.h"
00041 #include "nsHTMLAtoms.h"
00042 #include "nsStyleConsts.h"
00043 #include "nsPresContext.h"
00044 #include "nsRuleData.h"
00045 
00046 // use the same protection as ancient code did 
00047 // http://lxr.mozilla.org/classic/source/lib/layout/laytable.c#46
00048 #define MAX_COLSPAN 1000
00049 
00050 class nsHTMLTableColElement : public nsGenericHTMLElement,
00051                               public nsIDOMHTMLTableColElement
00052 {
00053 public:
00054   nsHTMLTableColElement(nsINodeInfo *aNodeInfo);
00055   virtual ~nsHTMLTableColElement();
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   // nsIDOMHTMLTableColElement
00070   NS_DECL_NSIDOMHTMLTABLECOLELEMENT
00071 
00072   virtual PRBool ParseAttribute(nsIAtom* aAttribute,
00073                                 const nsAString& aValue,
00074                                 nsAttrValue& aResult);
00075   nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
00076   NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
00077 };
00078 
00079 
00080 NS_IMPL_NS_NEW_HTML_ELEMENT(TableCol)
00081 
00082 
00083 nsHTMLTableColElement::nsHTMLTableColElement(nsINodeInfo *aNodeInfo)
00084   : nsGenericHTMLElement(aNodeInfo)
00085 {
00086 }
00087 
00088 nsHTMLTableColElement::~nsHTMLTableColElement()
00089 {
00090 }
00091 
00092 
00093 NS_IMPL_ADDREF_INHERITED(nsHTMLTableColElement, nsGenericElement) 
00094 NS_IMPL_RELEASE_INHERITED(nsHTMLTableColElement, nsGenericElement) 
00095 
00096 
00097 // QueryInterface implementation for nsHTMLTableColElement
00098 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLTableColElement,
00099                                     nsGenericHTMLElement)
00100   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLTableColElement)
00101   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLTableColElement)
00102 NS_HTML_CONTENT_INTERFACE_MAP_END
00103 
00104 
00105 NS_IMPL_DOM_CLONENODE(nsHTMLTableColElement)
00106 
00107 
00108 NS_IMPL_STRING_ATTR_DEFAULT_VALUE(nsHTMLTableColElement, Align, align, "left")
00109 NS_IMPL_STRING_ATTR_DEFAULT_VALUE(nsHTMLTableColElement, Ch, _char, ".")
00110 NS_IMPL_STRING_ATTR(nsHTMLTableColElement, ChOff, charoff)
00111 NS_IMPL_INT_ATTR_DEFAULT_VALUE(nsHTMLTableColElement, Span, span, 1)
00112 NS_IMPL_STRING_ATTR_DEFAULT_VALUE(nsHTMLTableColElement, VAlign, valign, "middle")
00113 NS_IMPL_STRING_ATTR(nsHTMLTableColElement, Width, width)
00114 
00115 
00116 PRBool
00117 nsHTMLTableColElement::ParseAttribute(nsIAtom* aAttribute,
00118                                       const nsAString& aValue,
00119                                       nsAttrValue& aResult)
00120 {
00121   /* ignore these attributes, stored simply as strings ch */
00122   if (aAttribute == nsHTMLAtoms::charoff) {
00123     return aResult.ParseSpecialIntValue(aValue, PR_TRUE, PR_FALSE);
00124   }
00125   if (aAttribute == nsHTMLAtoms::span) {
00126     /* protection from unrealistic large colspan values */
00127     return aResult.ParseIntWithBounds(aValue, 1, MAX_COLSPAN);
00128   }
00129   if (aAttribute == nsHTMLAtoms::width) {
00130     return aResult.ParseSpecialIntValue(aValue, PR_TRUE, PR_TRUE);
00131   }
00132   if (aAttribute == nsHTMLAtoms::align) {
00133     return ParseTableCellHAlignValue(aValue, aResult);
00134   }
00135   if (aAttribute == nsHTMLAtoms::valign) {
00136     return ParseTableVAlignValue(aValue, aResult);
00137   }
00138 
00139   return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
00140 }
00141 
00142 static 
00143 void MapAttributesIntoRule(const nsMappedAttributes* aAttributes, nsRuleData* aData)
00144 {
00145   if (aData->mSID == eStyleStruct_Position &&
00146       aData->mPositionData->mWidth.GetUnit() == eCSSUnit_Null) {
00147     // width
00148     const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::width);
00149     if (value) {
00150       switch (value->Type()) {
00151       case nsAttrValue::ePercent: {
00152         aData->mPositionData->mWidth.SetPercentValue(value->GetPercentValue());
00153         break;
00154       }
00155       case nsAttrValue::eInteger: {
00156         aData->mPositionData->mWidth.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);
00157         break;
00158       }
00159       case nsAttrValue::eProportional: {
00160         aData->mPositionData->mWidth.SetFloatValue((float)value->GetProportionalValue(), eCSSUnit_Proportional);
00161         break;
00162       }
00163       default:
00164         break;
00165       }
00166     }
00167   }
00168   else if (aData->mSID == eStyleStruct_Text) {
00169     if (aData->mTextData->mTextAlign.GetUnit() == eCSSUnit_Null) {
00170       // align: enum
00171       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::align);
00172       if (value && value->Type() == nsAttrValue::eEnum)
00173         aData->mTextData->mTextAlign.SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
00174     }
00175   }
00176   else if (aData->mSID == eStyleStruct_TextReset) {
00177     if (aData->mTextData->mVerticalAlign.GetUnit() == eCSSUnit_Null) {
00178       // valign: enum
00179       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::valign);
00180       if (value && value->Type() == nsAttrValue::eEnum)
00181         aData->mTextData->mVerticalAlign.SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
00182     }
00183   }
00184 
00185   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
00186 }
00187 
00188 static 
00189 void ColMapAttributesIntoRule(const nsMappedAttributes* aAttributes,
00190                               nsRuleData* aData)
00191 {
00192   if (aData->mSID == eStyleStruct_Table && 
00193       aData->mTableData->mSpan.GetUnit() == eCSSUnit_Null) {
00194     // span: int
00195     const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::span);
00196     if (value && value->Type() == nsAttrValue::eInteger)
00197       aData->mTableData->mSpan.SetIntValue(value->GetIntegerValue(),
00198                                            eCSSUnit_Integer);
00199   }
00200 
00201   MapAttributesIntoRule(aAttributes, aData);
00202 }
00203 
00204 NS_IMETHODIMP_(PRBool)
00205 nsHTMLTableColElement::IsAttributeMapped(const nsIAtom* aAttribute) const
00206 {
00207   static const MappedAttributeEntry attributes[] = {
00208     { &nsHTMLAtoms::width },
00209     { &nsHTMLAtoms::align },
00210     { &nsHTMLAtoms::valign },
00211     { nsnull }
00212   };
00213 
00214   static const MappedAttributeEntry span_attribute[] = {
00215     { &nsHTMLAtoms::span },
00216     { nsnull }
00217   };
00218 
00219   static const MappedAttributeEntry* const col_map[] = {
00220     attributes,
00221     span_attribute,
00222     sCommonAttributeMap,
00223   };
00224 
00225   static const MappedAttributeEntry* const colspan_map[] = {
00226     attributes,
00227     sCommonAttributeMap,
00228   };
00229 
00230   // we only match "span" if we're a <col>
00231   if (mNodeInfo->Equals(nsHTMLAtoms::col))
00232     return FindAttributeDependence(aAttribute, col_map,
00233                                    NS_ARRAY_LENGTH(col_map));
00234   return FindAttributeDependence(aAttribute, colspan_map,
00235                                  NS_ARRAY_LENGTH(colspan_map));
00236 }
00237 
00238 
00239 nsMapRuleToAttributesFunc
00240 nsHTMLTableColElement::GetAttributeMappingFunction() const
00241 {
00242   if (mNodeInfo->Equals(nsHTMLAtoms::col)) {
00243     return &ColMapAttributesIntoRule;
00244   }
00245 
00246   return &MapAttributesIntoRule;
00247 }