Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLTableSectionElement.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 "nsIDOMHTMLTableSectionElem.h"
00038 #include "nsIDOMEventReceiver.h"
00039 #include "nsMappedAttributes.h"
00040 #include "nsGenericHTMLElement.h"
00041 #include "nsHTMLAtoms.h"
00042 #include "nsHTMLParts.h"
00043 #include "nsStyleConsts.h"
00044 #include "nsPresContext.h"
00045 #include "nsContentList.h"
00046 #include "nsRuleData.h"
00047 #include "nsDOMError.h"
00048 #include "nsIDocument.h"
00049 
00050 // you will see the phrases "rowgroup" and "section" used interchangably
00051 
00052 class nsHTMLTableSectionElement : public nsGenericHTMLElement,
00053                                   public nsIDOMHTMLTableSectionElement
00054 {
00055 public:
00056   nsHTMLTableSectionElement(nsINodeInfo *aNodeInfo);
00057   virtual ~nsHTMLTableSectionElement();
00058 
00059   // nsISupports
00060   NS_DECL_ISUPPORTS_INHERITED
00061 
00062   // nsIDOMNode
00063   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLElement::)
00064 
00065   // nsIDOMElement
00066   NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLElement::)
00067 
00068   // nsIDOMHTMLElement
00069   NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLElement::)
00070 
00071   // nsIDOMHTMLTableSectionElement
00072   NS_DECL_NSIDOMHTMLTABLESECTIONELEMENT
00073 
00074   virtual PRBool ParseAttribute(nsIAtom* aAttribute,
00075                                 const nsAString& aValue,
00076                                 nsAttrValue& aResult);
00077   virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
00078   NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
00079 
00080 protected:
00081   nsRefPtr<nsContentList> mRows;
00082 };
00083 
00084 
00085 NS_IMPL_NS_NEW_HTML_ELEMENT(TableSection)
00086 
00087 
00088 nsHTMLTableSectionElement::nsHTMLTableSectionElement(nsINodeInfo *aNodeInfo)
00089   : nsGenericHTMLElement(aNodeInfo)
00090 {
00091 }
00092 
00093 nsHTMLTableSectionElement::~nsHTMLTableSectionElement()
00094 {
00095   if (mRows) {
00096     mRows->RootDestroyed();
00097   }
00098 }
00099 
00100 
00101 NS_IMPL_ADDREF_INHERITED(nsHTMLTableSectionElement, nsGenericElement) 
00102 NS_IMPL_RELEASE_INHERITED(nsHTMLTableSectionElement, nsGenericElement) 
00103 
00104 
00105 // QueryInterface implementation for nsHTMLTableSectionElement
00106 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLTableSectionElement,
00107                                     nsGenericHTMLElement)
00108   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLTableSectionElement)
00109   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLTableSectionElement)
00110 NS_HTML_CONTENT_INTERFACE_MAP_END
00111 
00112 
00113 NS_IMPL_DOM_CLONENODE(nsHTMLTableSectionElement)
00114 
00115 
00116 NS_IMPL_STRING_ATTR_DEFAULT_VALUE(nsHTMLTableSectionElement, Align, align, "left")
00117 NS_IMPL_STRING_ATTR_DEFAULT_VALUE(nsHTMLTableSectionElement, VAlign, valign, "middle")
00118 NS_IMPL_STRING_ATTR_DEFAULT_VALUE(nsHTMLTableSectionElement, Ch, _char, ".")
00119 NS_IMPL_STRING_ATTR(nsHTMLTableSectionElement, ChOff, charoff)
00120 
00121 
00122 NS_IMETHODIMP
00123 nsHTMLTableSectionElement::GetRows(nsIDOMHTMLCollection** aValue)
00124 {
00125   *aValue = nsnull;
00126 
00127   if (!mRows) {
00128     mRows = new nsContentList(GetDocument(),
00129                               nsHTMLAtoms::tr,
00130                               mNodeInfo->NamespaceID(),
00131                               this,
00132                               PR_FALSE);
00133 
00134     NS_ENSURE_TRUE(mRows, NS_ERROR_OUT_OF_MEMORY);
00135   }
00136 
00137   NS_ADDREF(*aValue = mRows);
00138   return NS_OK;
00139 }
00140 
00141 
00142 NS_IMETHODIMP
00143 nsHTMLTableSectionElement::InsertRow(PRInt32 aIndex,
00144                                      nsIDOMHTMLElement** aValue)
00145 {
00146   *aValue = nsnull;
00147 
00148   if (aIndex < -1) {
00149     return NS_ERROR_DOM_INDEX_SIZE_ERR;
00150   }
00151 
00152   nsCOMPtr<nsIDOMHTMLCollection> rows;
00153   GetRows(getter_AddRefs(rows));
00154 
00155   PRUint32 rowCount;
00156   rows->GetLength(&rowCount);
00157 
00158   if (aIndex > (PRInt32)rowCount) {
00159     return NS_ERROR_DOM_INDEX_SIZE_ERR;
00160   }
00161 
00162   PRBool doInsert = (aIndex < PRInt32(rowCount)) && (aIndex != -1);
00163 
00164   // create the row
00165   nsCOMPtr<nsINodeInfo> nodeInfo;
00166   nsContentUtils::NameChanged(mNodeInfo, nsHTMLAtoms::tr,
00167                               getter_AddRefs(nodeInfo));
00168 
00169   nsCOMPtr<nsIContent> rowContent = NS_NewHTMLTableRowElement(nodeInfo);
00170   if (!nodeInfo) {
00171     return NS_ERROR_OUT_OF_MEMORY;
00172   }
00173 
00174   nsCOMPtr<nsIDOMNode> rowNode(do_QueryInterface(rowContent));
00175   NS_ASSERTION(rowNode, "Should implement nsIDOMNode!");
00176 
00177   nsCOMPtr<nsIDOMNode> retChild;
00178 
00179   nsresult rv;
00180   if (doInsert) {
00181     nsCOMPtr<nsIDOMNode> refRow;
00182     rows->Item(aIndex, getter_AddRefs(refRow));
00183 
00184     rv = InsertBefore(rowNode, refRow, getter_AddRefs(retChild));
00185   } else {
00186     rv = AppendChild(rowNode, getter_AddRefs(retChild));
00187   }
00188 
00189   if (retChild) {
00190     CallQueryInterface(retChild, aValue);
00191   }
00192 
00193   return NS_OK;
00194 }
00195 
00196 NS_IMETHODIMP
00197 nsHTMLTableSectionElement::DeleteRow(PRInt32 aValue)
00198 {
00199   if (aValue < -1) {
00200     return NS_ERROR_DOM_INDEX_SIZE_ERR;
00201   }
00202 
00203   nsCOMPtr<nsIDOMHTMLCollection> rows;
00204   GetRows(getter_AddRefs(rows));
00205 
00206   nsresult rv;
00207   PRUint32 refIndex;
00208   if (aValue == -1) {
00209     rv = rows->GetLength(&refIndex);
00210     NS_ENSURE_SUCCESS(rv, rv);
00211 
00212     if (refIndex == 0) {
00213       return NS_OK;
00214     }
00215 
00216     --refIndex;
00217   }
00218   else {
00219     refIndex = (PRUint32)aValue;
00220   }
00221 
00222   nsCOMPtr<nsIDOMNode> row;
00223   rv = rows->Item(refIndex, getter_AddRefs(row));
00224   NS_ENSURE_SUCCESS(rv, rv);
00225 
00226   if (!row) {
00227     return NS_ERROR_DOM_INDEX_SIZE_ERR;
00228   }
00229 
00230   nsCOMPtr<nsIDOMNode> retChild;
00231   return RemoveChild(row, getter_AddRefs(retChild));
00232 }
00233 
00234 PRBool
00235 nsHTMLTableSectionElement::ParseAttribute(nsIAtom* aAttribute,
00236                                           const nsAString& aValue,
00237                                           nsAttrValue& aResult)
00238 {
00239   /* ignore these attributes, stored simply as strings
00240      ch
00241    */
00242   if (aAttribute == nsHTMLAtoms::charoff) {
00243     return aResult.ParseIntWithBounds(aValue, 0);
00244   }
00245   if (aAttribute == nsHTMLAtoms::height) {
00246     return aResult.ParseSpecialIntValue(aValue, PR_TRUE, PR_FALSE);
00247   }
00248   if (aAttribute == nsHTMLAtoms::align) {
00249     return ParseTableCellHAlignValue(aValue, aResult);
00250   }
00251   if (aAttribute == nsHTMLAtoms::bgcolor) {
00252     return aResult.ParseColor(aValue, GetOwnerDoc());
00253   }
00254   if (aAttribute == nsHTMLAtoms::valign) {
00255     return ParseTableVAlignValue(aValue, aResult);
00256   }
00257 
00258   return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
00259 }
00260 
00261 static 
00262 void MapAttributesIntoRule(const nsMappedAttributes* aAttributes, nsRuleData* aData)
00263 {
00264   if (aData->mSID == eStyleStruct_Position) {
00265     // height: value
00266     if (aData->mPositionData->mHeight.GetUnit() == eCSSUnit_Null) {
00267       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::height);
00268       if (value && value->Type() == nsAttrValue::eInteger)
00269         aData->mPositionData->mHeight.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);   
00270     }
00271   }
00272   else if (aData->mSID == eStyleStruct_Text) {
00273     if (aData->mTextData->mTextAlign.GetUnit() == eCSSUnit_Null) {
00274       // align: enum
00275       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::align);
00276       if (value && value->Type() == nsAttrValue::eEnum)
00277         aData->mTextData->mTextAlign.SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
00278     }
00279   }
00280   else if (aData->mSID == eStyleStruct_TextReset) {
00281     if (aData->mTextData->mVerticalAlign.GetUnit() == eCSSUnit_Null) {
00282       // valign: enum
00283       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::valign);
00284       if (value && value->Type() == nsAttrValue::eEnum)
00285         aData->mTextData->mVerticalAlign.SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
00286     }
00287   }
00288 
00289   nsGenericHTMLElement::MapBackgroundAttributesInto(aAttributes, aData);
00290   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
00291 }
00292 
00293 NS_IMETHODIMP_(PRBool)
00294 nsHTMLTableSectionElement::IsAttributeMapped(const nsIAtom* aAttribute) const
00295 {
00296   static const MappedAttributeEntry attributes[] = {
00297     { &nsHTMLAtoms::align }, 
00298     { &nsHTMLAtoms::valign },
00299     { &nsHTMLAtoms::height },
00300     { nsnull }
00301   };
00302 
00303   static const MappedAttributeEntry* const map[] = {
00304     attributes,
00305     sCommonAttributeMap,
00306     sBackgroundAttributeMap,
00307   };
00308 
00309   return FindAttributeDependence(aAttribute, map, NS_ARRAY_LENGTH(map));
00310 }
00311 
00312 
00313 nsMapRuleToAttributesFunc
00314 nsHTMLTableSectionElement::GetAttributeMappingFunction() const
00315 {
00316   return &MapAttributesIntoRule;
00317 }