Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsStubElement.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 XForms support.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsXFormsStubElement.h"
00040 #include "nsIDOMElement.h"
00041 #include "nsIDOMEventTarget.h"
00042 #include "nsIDOM3Node.h"
00043 #include "nsMemory.h"
00044 #include "nsXFormsUtils.h"
00045 #include "nsXFormsAtoms.h"
00046 #include "nsIXTFBindableElementWrapper.h"
00047 
00048 static const nsIID sScriptingIIDs[] = {
00049   NS_IDOMELEMENT_IID,
00050   NS_IDOMEVENTTARGET_IID,
00051   NS_IDOM3NODE_IID
00052 };
00053 
00054 // nsXFormsStubElement implementation
00055 
00056 NS_IMPL_ISUPPORTS2(nsXFormsStubElement, nsIXTFElement, nsIXTFGenericElement)
00057 
00058 NS_IMETHODIMP
00059 nsXFormsStubElement::OnDestroyed()
00060 {
00061   return NS_OK;
00062 }
00063 
00064 NS_IMETHODIMP
00065 nsXFormsStubElement::GetElementType(PRUint32 *aElementType)
00066 {
00067   *aElementType = nsIXTFElement::ELEMENT_TYPE_GENERIC_ELEMENT;
00068   return NS_OK;
00069 }
00070 
00071 NS_IMETHODIMP
00072 nsXFormsStubElement::GetIsAttributeHandler(PRBool *aIsAttributeHandler)
00073 {
00074   *aIsAttributeHandler = PR_FALSE;
00075   return NS_OK;
00076 }
00077 
00078 NS_IMETHODIMP
00079 nsXFormsStubElement::GetScriptingInterfaces(PRUint32 *aCount, nsIID ***aArray)
00080 {
00081   return nsXFormsUtils::CloneScriptingInterfaces(sScriptingIIDs,
00082                                                  NS_ARRAY_LENGTH(sScriptingIIDs),
00083                                                  aCount, aArray);
00084 }
00085 
00086 NS_IMETHODIMP
00087 nsXFormsStubElement::WillChangeDocument(nsIDOMDocument *aNewDocument)
00088 {
00089   return NS_OK;
00090 }
00091 
00092 NS_IMETHODIMP
00093 nsXFormsStubElement::DocumentChanged(nsIDOMDocument *aNewDocument)
00094 {
00095   mHasDoc = aNewDocument != nsnull;
00096   return NS_OK;
00097 }
00098 
00099 NS_IMETHODIMP
00100 nsXFormsStubElement::WillChangeParent(nsIDOMElement *aNewParent)
00101 {
00102   return NS_OK;
00103 }
00104 
00105 NS_IMETHODIMP
00106 nsXFormsStubElement::ParentChanged(nsIDOMElement *aNewParent)
00107 {
00108   mHasParent = aNewParent != nsnull;
00109   return NS_OK;
00110 }
00111 
00112 NS_IMETHODIMP
00113 nsXFormsStubElement::WillInsertChild(nsIDOMNode *aChild, PRUint32 aIndex)
00114 {
00115   return NS_OK;
00116 }
00117 
00118 NS_IMETHODIMP
00119 nsXFormsStubElement::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
00120 {
00121   return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP
00125 nsXFormsStubElement::WillAppendChild(nsIDOMNode *aChild)
00126 {
00127   return NS_OK;
00128 }
00129 
00130 NS_IMETHODIMP
00131 nsXFormsStubElement::ChildAppended(nsIDOMNode *aChild)
00132 {
00133   return NS_OK;
00134 }
00135 
00136 NS_IMETHODIMP
00137 nsXFormsStubElement::WillRemoveChild(PRUint32 aIndex)
00138 {
00139   return NS_OK;
00140 }
00141 
00142 NS_IMETHODIMP
00143 nsXFormsStubElement::ChildRemoved(PRUint32 aIndex)
00144 {
00145   return NS_OK;
00146 }
00147 
00148 NS_IMETHODIMP
00149 nsXFormsStubElement::WillSetAttribute(nsIAtom *aName,
00150                                       const nsAString &aNewValue)
00151 {
00152   return NS_OK;
00153 }
00154 
00155 NS_IMETHODIMP
00156 nsXFormsStubElement::AttributeSet(nsIAtom *aName, const nsAString &aNewValue)
00157 {
00158   return NS_OK;
00159 }
00160 
00161 NS_IMETHODIMP
00162 nsXFormsStubElement::WillRemoveAttribute(nsIAtom *aName)
00163 {
00164   return NS_OK;
00165 }
00166 
00167 NS_IMETHODIMP
00168 nsXFormsStubElement::AttributeRemoved(nsIAtom *aName)
00169 {
00170   return NS_OK;
00171 }
00172 
00173 NS_IMETHODIMP
00174 nsXFormsStubElement::BeginAddingChildren()
00175 {
00176   return NS_OK;
00177 }
00178 
00179 NS_IMETHODIMP
00180 nsXFormsStubElement::DoneAddingChildren()
00181 {
00182   return NS_OK;
00183 }
00184 
00185 NS_IMETHODIMP
00186 nsXFormsStubElement::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled)
00187 {
00188   *aHandled = PR_FALSE;
00189   return NS_OK;
00190 }
00191 
00192 NS_IMETHODIMP
00193 nsXFormsStubElement::OnCreated(nsIXTFGenericElementWrapper *aWrapper)
00194 {
00195   return NS_OK;
00196 }
00197 
00198 NS_IMETHODIMP
00199 nsXFormsStubElement::CloneState(nsIDOMElement *aElement)
00200 {
00201   return NS_OK;
00202 }
00203 
00204 nsRepeatState
00205 nsXFormsStubElement::GetRepeatState()
00206 {
00207   return mRepeatState;
00208 }
00209 
00210 void
00211 nsXFormsStubElement::SetRepeatState(nsRepeatState aState)
00212 {
00213   mRepeatState = aState;
00214   return;
00215 }
00216 
00217 nsRepeatState
00218 nsXFormsStubElement::UpdateRepeatState(nsIDOMNode *aParent)
00219 {
00220   // Walk up the parent chain looking to see if the this control is contained
00221   // in an item.  If it is and that item is contained in a itemset, then we
00222   // know that this control was generated as a clone from the itemset's
00223   // template.  Similarly, we'll check to see if this control lives in a
00224   // contextcontainer (meaning it was cloned from a repeat's template).
00225   // Otherwise, if neither of these are the case but it lives under a repeat
00226   // or an itemset, then this control must be part of a template.  A template
00227   // is the content of a repeat or itemset that gets cloned once for every
00228   // node in the bound nodeset.
00229   //
00230   // If none of this applies, we'll return eType_NotApplicable to show that this
00231   // control isn't bound to a repeating nodeset.
00232   nsRepeatState repeatState = eType_NotApplicable;
00233 
00234   if (!mHasDoc || !mHasParent) {
00235     // If we don't have a document or a parent, none of these tests will work
00236     // correctly so no sense doing them now.  If either of these are false the
00237     // repeat state for the object should already be eType_Unknown so just
00238     // return that now.
00239     return eType_Unknown;
00240   }
00241 
00242   nsCOMPtr<nsIDOMNode> parent = aParent;
00243   PRBool childIsItem = PR_FALSE;
00244   while (parent) {
00245     if (nsXFormsUtils::IsXFormsElement(parent,
00246                                        NS_LITERAL_STRING("contextcontainer"))) {
00247       repeatState = eType_GeneratedContent;
00248       break;
00249     }
00250     if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("repeat"))) {
00251       repeatState = eType_Template;
00252       break;
00253     }
00254     if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("itemset"))) {
00255       if (childIsItem) {
00256         repeatState = eType_GeneratedContent;
00257         break;
00258       }
00259       repeatState = eType_Template;
00260       break;
00261     }
00262 
00263     if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("item"))) {
00264       childIsItem = PR_TRUE;
00265     } else {
00266 
00267       nsCOMPtr<nsIDOMElement> parentEle(do_QueryInterface(parent));
00268       if (!parentEle) {
00269         // I don't know how this can possibly happen, but if it does I guess
00270         // we should just ignore it and coninue on our way.
00271         break;
00272       }
00273 
00274       // if this control is contained underneath an element that contains
00275       // an xforms binding attribute that introduces an anonymous xf:repeat
00276       // then the control is part of a template
00277       PRBool repeatAttr = PR_FALSE;
00278       parentEle->HasAttributeNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),
00279                                 NS_LITERAL_STRING("repeat-bind"),
00280                                 &repeatAttr);
00281       if (repeatAttr) {
00282         repeatState = eType_Template;
00283         break;
00284       }
00285 
00286       parentEle->HasAttributeNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),
00287                                 NS_LITERAL_STRING("repeat-nodeset"),
00288                                 &repeatAttr);
00289       if (repeatAttr) {
00290         repeatState = eType_Template;
00291         break;
00292       }
00293     }
00294     nsCOMPtr<nsIDOMNode> tmp;
00295     parent->GetParentNode(getter_AddRefs(tmp));
00296     parent = tmp;
00297   }
00298 
00299   SetRepeatState(repeatState);
00300   return repeatState;
00301 }
00302 nsresult
00303 NS_NewXFormsStubElement(nsIXTFElement **aResult)
00304 {
00305   *aResult = new nsXFormsStubElement();
00306   if (!*aResult)
00307     return NS_ERROR_OUT_OF_MEMORY;
00308 
00309   NS_ADDREF(*aResult);
00310   return NS_OK;
00311 }
00312 
00313 // nsXFormsBindableStub implementation
00314 
00315 NS_IMPL_ISUPPORTS2(nsXFormsBindableStub, nsIXTFElement, nsIXTFBindableElement)
00316 
00317 NS_IMETHODIMP
00318 nsXFormsBindableStub::OnDestroyed()
00319 {
00320   return NS_OK;
00321 }
00322 
00323 NS_IMETHODIMP
00324 nsXFormsBindableStub::GetElementType(PRUint32 *aElementType)
00325 {
00326   *aElementType = nsIXTFElement::ELEMENT_TYPE_BINDABLE;
00327   return NS_OK;
00328 }
00329 
00330 NS_IMETHODIMP
00331 nsXFormsBindableStub::GetIsAttributeHandler(PRBool *aIsAttributeHandler)
00332 {
00333   *aIsAttributeHandler = PR_FALSE;
00334   return NS_OK;
00335 }
00336 
00337 NS_IMETHODIMP
00338 nsXFormsBindableStub::GetScriptingInterfaces(PRUint32 *aCount, nsIID ***aArray)
00339 {
00340   return NS_OK;
00341 }
00342 
00343 NS_IMETHODIMP
00344 nsXFormsBindableStub::WillChangeDocument(nsIDOMDocument *aNewDocument)
00345 {
00346   return NS_OK;
00347 }
00348 
00349 NS_IMETHODIMP
00350 nsXFormsBindableStub::DocumentChanged(nsIDOMDocument *aNewDocument)
00351 {
00352   return NS_OK;
00353 }
00354 
00355 NS_IMETHODIMP
00356 nsXFormsBindableStub::WillChangeParent(nsIDOMElement *aNewParent)
00357 {
00358   return NS_OK;
00359 }
00360 
00361 NS_IMETHODIMP
00362 nsXFormsBindableStub::ParentChanged(nsIDOMElement *aNewParent)
00363 {
00364   return NS_OK;
00365 }
00366 
00367 NS_IMETHODIMP
00368 nsXFormsBindableStub::WillInsertChild(nsIDOMNode *aChild, PRUint32 aIndex)
00369 {
00370   return NS_OK;
00371 }
00372 
00373 NS_IMETHODIMP
00374 nsXFormsBindableStub::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
00375 {
00376   return NS_OK;
00377 }
00378 
00379 NS_IMETHODIMP
00380 nsXFormsBindableStub::WillAppendChild(nsIDOMNode *aChild)
00381 {
00382   return NS_OK;
00383 }
00384 
00385 NS_IMETHODIMP
00386 nsXFormsBindableStub::ChildAppended(nsIDOMNode *aChild)
00387 {
00388   return NS_OK;
00389 }
00390 
00391 NS_IMETHODIMP
00392 nsXFormsBindableStub::WillRemoveChild(PRUint32 aIndex)
00393 {
00394   return NS_OK;
00395 }
00396 
00397 NS_IMETHODIMP
00398 nsXFormsBindableStub::ChildRemoved(PRUint32 aIndex)
00399 {
00400   return NS_OK;
00401 }
00402 
00403 NS_IMETHODIMP
00404 nsXFormsBindableStub::WillSetAttribute(nsIAtom *aName,
00405                                       const nsAString &aNewValue)
00406 {
00407   return NS_OK;
00408 }
00409 
00410 NS_IMETHODIMP
00411 nsXFormsBindableStub::AttributeSet(nsIAtom *aName, const nsAString &aNewValue)
00412 {
00413   return NS_OK;
00414 }
00415 
00416 NS_IMETHODIMP
00417 nsXFormsBindableStub::WillRemoveAttribute(nsIAtom *aName)
00418 {
00419   return NS_OK;
00420 }
00421 
00422 NS_IMETHODIMP
00423 nsXFormsBindableStub::AttributeRemoved(nsIAtom *aName)
00424 {
00425   return NS_OK;
00426 }
00427 
00428 NS_IMETHODIMP
00429 nsXFormsBindableStub::BeginAddingChildren()
00430 {
00431   return NS_OK;
00432 }
00433 
00434 NS_IMETHODIMP
00435 nsXFormsBindableStub::DoneAddingChildren()
00436 {
00437   return NS_OK;
00438 }
00439 
00440 NS_IMETHODIMP
00441 nsXFormsBindableStub::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled)
00442 {
00443   *aHandled = PR_FALSE;
00444   return NS_OK;
00445 }
00446 
00447 NS_IMETHODIMP
00448 nsXFormsBindableStub::OnCreated(nsIXTFBindableElementWrapper *aWrapper)
00449 {
00450   NS_ENSURE_ARG(aWrapper);
00451   return aWrapper->SetClassAttributeName(nsXFormsAtoms::clazz);
00452 }
00453 
00454 NS_IMETHODIMP
00455 nsXFormsBindableStub::CloneState(nsIDOMElement *aElement)
00456 {
00457   return NS_OK;
00458 }
00459 
00460 nsresult
00461 NS_NewXFormsBindableStub(nsIXTFElement **aResult)
00462 {
00463   *aResult = new nsXFormsBindableStub();
00464   if (!*aResult)
00465     return NS_ERROR_OUT_OF_MEMORY;
00466 
00467   NS_ADDREF(*aResult);
00468   return NS_OK;
00469 }