2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXBLProtoImplField.h"
|
|
|
|
#include "nsIScriptContext.h"
|
2007-09-28 06:45:01 -07:00
|
|
|
#include "nsIURI.h"
|
2011-11-03 13:39:08 -07:00
|
|
|
#include "nsXBLSerialize.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsXBLProtoImplField::nsXBLProtoImplField(const PRUnichar* aName, const PRUnichar* aReadOnly)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mNext(nullptr),
|
|
|
|
mFieldText(nullptr),
|
2007-03-22 10:30:00 -07:00
|
|
|
mFieldTextLength(0),
|
|
|
|
mLineNumber(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplField);
|
2007-09-28 06:45:01 -07:00
|
|
|
mName = NS_strdup(aName); // XXXbz make more sense to use a stringbuffer?
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mJSAttributes = JSPROP_ENUMERATE;
|
|
|
|
if (aReadOnly) {
|
2010-02-03 13:17:55 -08:00
|
|
|
nsAutoString readOnly; readOnly.Assign(aReadOnly);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (readOnly.LowerCaseEqualsLiteral("true"))
|
|
|
|
mJSAttributes |= JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 13:39:08 -07:00
|
|
|
|
2011-11-20 19:09:16 -08:00
|
|
|
nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mNext(nullptr),
|
|
|
|
mFieldText(nullptr),
|
2011-11-03 13:39:08 -07:00
|
|
|
mFieldTextLength(0),
|
|
|
|
mLineNumber(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplField);
|
|
|
|
|
|
|
|
mJSAttributes = JSPROP_ENUMERATE;
|
|
|
|
if (aIsReadOnly)
|
|
|
|
mJSAttributes |= JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXBLProtoImplField::~nsXBLProtoImplField()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsXBLProtoImplField);
|
|
|
|
if (mFieldText)
|
|
|
|
nsMemory::Free(mFieldText);
|
2007-09-28 06:45:01 -07:00
|
|
|
NS_Free(mName);
|
2008-12-15 03:33:56 -08:00
|
|
|
NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField, this, mNext);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLProtoImplField::AppendFieldText(const nsAString& aText)
|
|
|
|
{
|
|
|
|
if (mFieldText) {
|
|
|
|
nsDependentString fieldTextStr(mFieldText, mFieldTextLength);
|
|
|
|
nsAutoString newFieldText = fieldTextStr + aText;
|
|
|
|
PRUnichar* temp = mFieldText;
|
|
|
|
mFieldText = ToNewUnicode(newFieldText);
|
|
|
|
mFieldTextLength = newFieldText.Length();
|
|
|
|
nsMemory::Free(temp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mFieldText = ToNewUnicode(aText);
|
|
|
|
mFieldTextLength = aText.Length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-09-28 06:45:01 -07:00
|
|
|
nsXBLProtoImplField::InstallField(nsIScriptContext* aContext,
|
|
|
|
JSObject* aBoundNode,
|
2007-10-02 07:38:35 -07:00
|
|
|
nsIURI* aBindingDocURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aDidInstall) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-09-28 06:45:01 -07:00
|
|
|
NS_PRECONDITION(aBoundNode,
|
|
|
|
"uh-oh, bound node should NOT be null or bad things will "
|
|
|
|
"happen");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidInstall = false;
|
2007-10-02 07:38:35 -07:00
|
|
|
|
2012-05-29 12:01:30 -07:00
|
|
|
// Empty fields are treated as not actually present.
|
|
|
|
if (IsEmpty()) {
|
2007-10-02 07:38:35 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-31 09:30:13 -07:00
|
|
|
nsAutoMicroTask mt;
|
|
|
|
|
2013-01-16 18:50:26 -08:00
|
|
|
// EvaluateString and JS_DefineUCProperty can both trigger GC, so
|
2007-03-22 10:30:00 -07:00
|
|
|
// protect |result| here.
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString uriSpec;
|
2007-10-02 07:38:35 -07:00
|
|
|
aBindingDocURI->GetSpec(uriSpec);
|
2007-09-26 06:55:06 -07:00
|
|
|
|
2011-09-18 02:22:17 -07:00
|
|
|
JSContext* cx = aContext->GetNativeContext();
|
2008-03-05 12:41:27 -08:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"Shouldn't get here when an exception is pending!");
|
|
|
|
|
2007-10-02 07:38:35 -07:00
|
|
|
// compile the literal string
|
|
|
|
nsCOMPtr<nsIScriptContext> context = aContext;
|
2010-11-26 06:11:14 -08:00
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
jsval result = JSVAL_NULL;
|
2013-01-16 18:50:26 -08:00
|
|
|
|
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine(uriSpec.get(), mLineNumber)
|
|
|
|
.setVersion(JSVERSION_LATEST)
|
|
|
|
.setUserBit(true); // Flag us as XBL
|
2013-01-16 18:50:26 -08:00
|
|
|
rv = context->EvaluateString(nsDependentString(mFieldText,
|
|
|
|
mFieldTextLength),
|
|
|
|
*aBoundNode, options,
|
|
|
|
/* aCoerceToString = */ false,
|
2013-01-16 18:50:27 -08:00
|
|
|
&result);
|
2011-11-15 23:50:20 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-10-02 07:38:35 -07:00
|
|
|
return rv;
|
2011-11-15 23:50:20 -08:00
|
|
|
}
|
2007-09-26 07:39:31 -07:00
|
|
|
|
|
|
|
|
2007-09-28 06:45:01 -07:00
|
|
|
// Define the evaluated result as a JS property
|
|
|
|
nsDependentString name(mName);
|
|
|
|
if (!::JS_DefineUCProperty(cx, aBoundNode,
|
|
|
|
reinterpret_cast<const jschar*>(mName),
|
2012-07-30 07:20:58 -07:00
|
|
|
name.Length(), result, nullptr, nullptr,
|
2007-09-28 06:45:01 -07:00
|
|
|
mJSAttributes)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2007-10-02 07:38:35 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidInstall = true;
|
2007-09-28 06:45:01 -07:00
|
|
|
return NS_OK;
|
2007-09-26 07:39:31 -07:00
|
|
|
}
|
2011-11-03 13:39:08 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplField::Read(nsIScriptContext* aContext,
|
|
|
|
nsIObjectInputStream* aStream)
|
|
|
|
{
|
|
|
|
nsAutoString name;
|
|
|
|
nsresult rv = aStream->ReadString(name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mName = ToNewUnicode(name);
|
|
|
|
|
|
|
|
rv = aStream->Read32(&mLineNumber);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString fieldText;
|
|
|
|
rv = aStream->ReadString(fieldText);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mFieldTextLength = fieldText.Length();
|
|
|
|
if (mFieldTextLength)
|
|
|
|
mFieldText = ToNewUnicode(fieldText);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplField::Write(nsIScriptContext* aContext,
|
|
|
|
nsIObjectOutputStream* aStream)
|
|
|
|
{
|
|
|
|
XBLBindingSerializeDetails type = XBLBinding_Serialize_Field;
|
|
|
|
|
|
|
|
if (mJSAttributes & JSPROP_READONLY) {
|
|
|
|
type |= XBLBinding_Serialize_ReadOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = aStream->Write8(type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aStream->WriteWStringZ(mName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aStream->Write32(mLineNumber);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return aStream->WriteWStringZ(mFieldText ? mFieldText : EmptyString().get());
|
|
|
|
}
|