2001-11-13 04:09:56 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 21:52:36 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-11-13 04:09:56 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-11-13 04:09:56 +00:00
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-11-13 04:09:56 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* David Hyatt <hyatt@netscape.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 21:52:36 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-11-13 04:09:56 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-17 21:52:36 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-11-13 04:09:56 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-17 21:52:36 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-11-13 04:09:56 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXBLProtoImplField.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
MOZ_DECL_CTOR_COUNTER(nsXBLProtoImplField)
|
2001-11-13 04:09:56 +00:00
|
|
|
|
2002-01-09 21:50:08 +00:00
|
|
|
nsXBLProtoImplField::nsXBLProtoImplField(const PRUnichar* aName, const PRUnichar* aReadOnly)
|
2001-11-16 02:17:04 +00:00
|
|
|
: nsXBLProtoImplMember(aName),
|
|
|
|
mFieldText(nsnull),
|
2003-07-11 22:54:36 +00:00
|
|
|
mFieldTextLength(0),
|
|
|
|
mLineNumber(0)
|
2001-11-13 04:09:56 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplField);
|
|
|
|
mJSAttributes = JSPROP_ENUMERATE;
|
|
|
|
if (aReadOnly) {
|
|
|
|
nsAutoString readOnly; readOnly.Assign(*aReadOnly);
|
2004-06-17 00:13:25 +00:00
|
|
|
if (readOnly.LowerCaseEqualsLiteral("true"))
|
2001-11-13 04:09:56 +00:00
|
|
|
mJSAttributes |= JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXBLProtoImplField::~nsXBLProtoImplField()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsXBLProtoImplField);
|
2001-11-16 02:17:04 +00:00
|
|
|
if (mFieldText)
|
|
|
|
nsMemory::Free(mFieldText);
|
2001-11-13 04:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLProtoImplField::Destroy(PRBool aIsCompiled)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-23 22:46:13 +00:00
|
|
|
nsXBLProtoImplField::AppendFieldText(const nsAString& aText)
|
2001-11-13 04:09:56 +00:00
|
|
|
{
|
2001-11-16 02:17:04 +00:00
|
|
|
if (mFieldText) {
|
|
|
|
nsDependentString fieldTextStr(mFieldText, mFieldTextLength);
|
2004-02-19 02:44:03 +00:00
|
|
|
nsAutoString newFieldText = fieldTextStr + aText;
|
2001-11-16 02:17:04 +00:00
|
|
|
PRUnichar* temp = mFieldText;
|
|
|
|
mFieldText = ToNewUnicode(newFieldText);
|
|
|
|
mFieldTextLength = newFieldText.Length();
|
|
|
|
nsMemory::Free(temp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mFieldText = ToNewUnicode(aText);
|
|
|
|
mFieldTextLength = aText.Length();
|
|
|
|
}
|
2001-11-13 04:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2003-07-11 22:54:36 +00:00
|
|
|
nsXBLProtoImplField::InstallMember(nsIScriptContext* aContext,
|
|
|
|
nsIContent* aBoundElement,
|
|
|
|
void* aScriptObject,
|
|
|
|
void* aTargetClassObject,
|
|
|
|
const nsCString& aClassStr)
|
2001-11-13 04:09:56 +00:00
|
|
|
{
|
2001-11-16 02:17:04 +00:00
|
|
|
if (mFieldTextLength == 0)
|
2001-11-13 04:09:56 +00:00
|
|
|
return NS_OK; // nothing to do.
|
|
|
|
|
|
|
|
JSContext* cx = (JSContext*) aContext->GetNativeContext();
|
|
|
|
JSObject * scriptObject = (JSObject *) aScriptObject;
|
|
|
|
NS_ASSERTION(scriptObject, "uh-oh, script Object should NOT be null or bad things will happen");
|
|
|
|
if (!scriptObject)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2003-07-11 22:54:36 +00:00
|
|
|
nsCAutoString bindingURI(aClassStr);
|
|
|
|
PRInt32 hash = bindingURI.RFindChar('#');
|
|
|
|
if (hash != kNotFound)
|
|
|
|
bindingURI.Truncate(hash);
|
|
|
|
|
2001-11-13 04:09:56 +00:00
|
|
|
// compile the literal string
|
|
|
|
jsval result = nsnull;
|
|
|
|
PRBool undefined;
|
2003-06-24 01:20:40 +00:00
|
|
|
// XXX Need a URI here!
|
2004-05-18 04:00:47 +00:00
|
|
|
nsCOMPtr<nsIScriptContext> context = aContext;
|
|
|
|
nsresult rv = context->EvaluateStringWithValue(nsDependentString(mFieldText,
|
|
|
|
mFieldTextLength),
|
|
|
|
scriptObject,
|
|
|
|
nsnull, bindingURI.get(),
|
|
|
|
mLineNumber, nsnull,
|
|
|
|
(void*) &result, &undefined);
|
2003-09-11 04:10:20 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-11-13 04:09:56 +00:00
|
|
|
if (!undefined) {
|
|
|
|
// Define the evaluated result as a JS property
|
|
|
|
nsDependentString name(mName);
|
2003-09-11 04:10:20 +00:00
|
|
|
if (!::JS_DefineUCProperty(cx, scriptObject, NS_REINTERPRET_CAST(const jschar*, mName),
|
|
|
|
name.Length(), result, nsnull, nsnull, mJSAttributes))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2001-11-13 04:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplField::CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr,
|
|
|
|
void* aClassObject)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|