Back to index

lightning-sunbird  0.9+nobinonly
nsXBLProtoImplField.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  *   David Hyatt <hyatt@netscape.com> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or 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 "nsIAtom.h"
00040 #include "nsString.h"
00041 #include "jsapi.h"
00042 #include "nsIContent.h"
00043 #include "nsString.h"
00044 #include "nsUnicharUtils.h"
00045 #include "nsReadableUtils.h"
00046 #include "nsXBLProtoImplField.h"
00047 #include "nsIScriptContext.h"
00048 #include "nsContentUtils.h"
00049 
00050 MOZ_DECL_CTOR_COUNTER(nsXBLProtoImplField)
00051 
00052 nsXBLProtoImplField::nsXBLProtoImplField(const PRUnichar* aName, const PRUnichar* aReadOnly)
00053   : nsXBLProtoImplMember(aName),
00054     mFieldText(nsnull),
00055     mFieldTextLength(0),
00056     mLineNumber(0)
00057 {
00058   MOZ_COUNT_CTOR(nsXBLProtoImplField);
00059   mJSAttributes = JSPROP_ENUMERATE;
00060   if (aReadOnly) {
00061     nsAutoString readOnly; readOnly.Assign(*aReadOnly);
00062     if (readOnly.LowerCaseEqualsLiteral("true"))
00063       mJSAttributes |= JSPROP_READONLY;
00064   }
00065 }
00066 
00067 nsXBLProtoImplField::~nsXBLProtoImplField()
00068 {
00069   MOZ_COUNT_DTOR(nsXBLProtoImplField);
00070   if (mFieldText)
00071     nsMemory::Free(mFieldText);
00072 }
00073 
00074 void
00075 nsXBLProtoImplField::Destroy(PRBool aIsCompiled)
00076 {
00077 }
00078 
00079 void 
00080 nsXBLProtoImplField::AppendFieldText(const nsAString& aText)
00081 {
00082   if (mFieldText) {
00083     nsDependentString fieldTextStr(mFieldText, mFieldTextLength);
00084     nsAutoString newFieldText = fieldTextStr + aText;
00085     PRUnichar* temp = mFieldText;
00086     mFieldText = ToNewUnicode(newFieldText);
00087     mFieldTextLength = newFieldText.Length();
00088     nsMemory::Free(temp);
00089   }
00090   else {
00091     mFieldText = ToNewUnicode(aText);
00092     mFieldTextLength = aText.Length();
00093   }
00094 }
00095 
00096 nsresult
00097 nsXBLProtoImplField::InstallMember(nsIScriptContext* aContext,
00098                                    nsIContent* aBoundElement, 
00099                                    void* aScriptObject,
00100                                    void* aTargetClassObject,
00101                                    const nsCString& aClassStr)
00102 {
00103   if (mFieldTextLength == 0)
00104     return NS_OK; // nothing to do.
00105 
00106   JSContext* cx = (JSContext*) aContext->GetNativeContext();
00107   JSObject * scriptObject = (JSObject *) aScriptObject;
00108   NS_ASSERTION(scriptObject, "uh-oh, script Object should NOT be null or bad things will happen");
00109   if (!scriptObject)
00110     return NS_ERROR_FAILURE;
00111 
00112   nsCAutoString bindingURI(aClassStr);
00113   PRInt32 hash = bindingURI.RFindChar('#');
00114   if (hash != kNotFound)
00115     bindingURI.Truncate(hash);
00116   
00117   // compile the literal string 
00118   jsval result = JSVAL_NULL;
00119   
00120   // EvaluateStringWithValue and JS_DefineUCProperty can both trigger GC, so
00121   // protect |result| here.
00122   nsresult rv;
00123   nsAutoGCRoot root(&result, &rv);
00124   if (NS_FAILED(rv))
00125     return rv;
00126   PRBool undefined;
00127   // XXX Need a URI here!
00128   nsCOMPtr<nsIScriptContext> context = aContext;
00129   rv = context->EvaluateStringWithValue(nsDependentString(mFieldText,
00130                                                           mFieldTextLength), 
00131                                         scriptObject,
00132                                         nsnull, bindingURI.get(),
00133                                         mLineNumber, nsnull,
00134                                         (void*) &result, &undefined);
00135   if (NS_FAILED(rv))
00136     return rv;
00137 
00138   if (!undefined) {
00139     // Define the evaluated result as a JS property
00140     nsDependentString name(mName);
00141     if (!::JS_DefineUCProperty(cx, scriptObject, NS_REINTERPRET_CAST(const jschar*, mName), 
00142                                name.Length(), result, nsnull, nsnull, mJSAttributes))
00143       return NS_ERROR_OUT_OF_MEMORY;
00144   }
00145   
00146   return NS_OK;
00147 }
00148 
00149 nsresult 
00150 nsXBLProtoImplField::CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr,
00151                                    void* aClassObject)
00152 {
00153   return NS_OK;
00154 }