00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#include "kjs_binding.h"
00024
#include "kjs_dom.h"
00025
00026
#include "dom/dom_exception.h"
00027
#include "dom/dom2_range.h"
00028
#include "xml/dom2_eventsimpl.h"
00029
00030
#include <kdebug.h>
00031
00032
#include <assert.h>
00033
00034
using namespace KJS;
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 Value DOMObject::get(ExecState *exec,
const Identifier &p)
const
00045
{
00046 Value result;
00047
try {
00048 result = tryGet(exec,p);
00049 }
00050
catch (
DOM::DOMException e) {
00051
00052
00053
00054 Object err = Error::create(exec, GeneralError,
QString(
"DOM exception %1").arg(e.code).local8Bit());
00055 exec->setException( err );
00056 result = Undefined();
00057 }
00058
catch (...) {
00059
kdError(6070) <<
"Unknown exception in DOMObject::get()" <<
endl;
00060 result = String(
"Unknown exception");
00061 }
00062
00063
return result;
00064 }
00065
00066
void DOMObject::put(ExecState *exec,
const Identifier &propertyName,
00067
const Value &value,
int attr)
00068 {
00069
try {
00070 tryPut(exec, propertyName, value, attr);
00071 }
00072
catch (
DOM::DOMException e) {
00073 Object err = Error::create(exec, GeneralError,
QString(
"DOM exception %1").arg(e.code).local8Bit());
00074 exec->setException(err);
00075 }
00076
catch (...) {
00077
kdError(6070) <<
"Unknown exception in DOMObject::put()" <<
endl;
00078 }
00079 }
00080
00081 UString DOMObject::toString(ExecState *)
const
00082
{
00083
return "[object " + className() +
"]";
00084 }
00085
00086 Value DOMFunction::get(ExecState *exec,
const Identifier &propertyName)
const
00087
{
00088 Value result;
00089
try {
00090 result = tryGet(exec, propertyName);
00091 }
00092
catch (
DOM::DOMException e) {
00093 result = Undefined();
00094 Object err = Error::create(exec, GeneralError,
QString(
"DOM exception %1").arg(e.code).local8Bit());
00095 exec->setException(err);
00096 }
00097
catch (...) {
00098
kdError(6070) <<
"Unknown exception in DOMFunction::get()" <<
endl;
00099 result = String(
"Unknown exception");
00100 }
00101
00102
return result;
00103 }
00104
00105 Value DOMFunction::call(ExecState *exec, Object &thisObj,
const List &args)
00106 {
00107 Value val;
00108
try {
00109 val = tryCall(exec, thisObj, args);
00110 }
00111
00112
00113
00114
catch (
DOM::DOMException e) {
00115 Object err = Error::create(exec, GeneralError,
QString(
"DOM Exception %1").arg(e.code).local8Bit());
00116 err.put(exec,
"code", Number(e.code));
00117 exec->setException(err);
00118 }
00119
catch (DOM::RangeException e) {
00120 Object err = Error::create(exec, GeneralError,
QString(
"DOM Range Exception %1").arg(e.code).local8Bit());
00121 err.put(exec,
"code", Number(e.code));
00122 exec->setException(err);
00123 }
00124
catch (
DOM::CSSException e) {
00125 Object err = Error::create(exec, GeneralError,
QString(
"CSS Exception %1").arg(e.code).local8Bit());
00126 err.put(exec,
"code", Number(e.code));
00127 exec->setException(err);
00128 }
00129
catch (
DOM::EventException e) {
00130 Object err = Error::create(exec, GeneralError,
QString(
"DOM Event Exception %1").arg(e.code).local8Bit());
00131 err.put(exec,
"code", Number(e.code));
00132 exec->setException(err);
00133 }
00134
catch (...) {
00135
kdError(6070) <<
"Unknown exception in DOMFunction::call()" <<
endl;
00136 Object err = Error::create(exec, GeneralError,
"Unknown exception");
00137 exec->setException(err);
00138 }
00139
return val;
00140 }
00141
00142
typedef QPtrList<ScriptInterpreter> InterpreterList;
00143
static InterpreterList *interpreterList;
00144
00145 ScriptInterpreter::ScriptInterpreter(
const Object &global,
KHTMLPart* part )
00146 : Interpreter( global ), m_part( part ), m_domObjects(1021),
00147 m_evt( 0L ), m_inlineCode(false), m_timerCallback(false)
00148 {
00149
#ifdef KJS_VERBOSE
00150
kdDebug(6070) <<
"ScriptInterpreter::ScriptInterpreter " <<
this <<
" for part=" << m_part <<
endl;
00151
#endif
00152
if ( !interpreterList )
00153 interpreterList =
new InterpreterList;
00154 interpreterList->
append(
this );
00155 }
00156
00157 ScriptInterpreter::~ScriptInterpreter()
00158 {
00159
#ifdef KJS_VERBOSE
00160
kdDebug(6070) <<
"ScriptInterpreter::~ScriptInterpreter " <<
this <<
" for part=" << m_part <<
endl;
00161
#endif
00162
assert( interpreterList && interpreterList->
contains(
this ) );
00163 interpreterList->
remove(
this );
00164
if ( interpreterList->
isEmpty() ) {
00165
delete interpreterList;
00166 interpreterList = 0;
00167 }
00168 }
00169
00170 void ScriptInterpreter::forgetDOMObject(
void* objectHandle )
00171 {
00172
if( !interpreterList )
return;
00173
00174
QPtrListIterator<ScriptInterpreter> it( *interpreterList );
00175
while ( it.
current() ) {
00176 (*it)->deleteDOMObject( objectHandle );
00177 ++it;
00178 }
00179 }
00180
00181 void ScriptInterpreter::mark()
00182 {
00183 Interpreter::mark();
00184
#ifdef KJS_VERBOSE
00185
kdDebug(6070) <<
"ScriptInterpreter::mark " <<
this <<
" marking " << m_domObjects.
count() <<
" DOM objects" <<
endl;
00186
#endif
00187
QPtrDictIterator<DOMObject> it( m_domObjects );
00188
for( ; it.
current(); ++it )
00189 it.
current()->mark();
00190 }
00191
00192 bool ScriptInterpreter::isWindowOpenAllowed()
const
00193
{
00194
if ( m_evt )
00195 {
00196
int id = m_evt->
handle()->id();
00197
bool eventOk = (
00198
id == DOM::EventImpl::CLICK_EVENT ||
00199
id == DOM::EventImpl::MOUSEUP_EVENT ||
id == DOM::EventImpl::MOUSEDOWN_EVENT ||
00200
id == DOM::EventImpl::KHTML_ECMA_CLICK_EVENT ||
id == DOM::EventImpl::KHTML_ECMA_DBLCLICK_EVENT ||
00201
00202
id == DOM::EventImpl::KEYDOWN_EVENT ||
id == DOM::EventImpl::KHTML_KEYPRESS_EVENT ||
00203
id == DOM::EventImpl::KEYUP_EVENT ||
00204
00205
id == DOM::EventImpl::SELECT_EVENT ||
id == DOM::EventImpl::CHANGE_EVENT ||
00206
id == DOM::EventImpl::SUBMIT_EVENT );
00207
kdDebug(6070) <<
"Window.open, smart policy: id=" <<
id <<
" eventOk=" << eventOk <<
endl;
00208
if (eventOk)
00209
return true;
00210 }
else
00211 {
00212
if ( m_inlineCode && !m_timerCallback )
00213 {
00214
00215
return true;
00216
kdDebug(6070) <<
"Window.open, smart policy, no event, inline code -> ok" <<
endl;
00217 }
00218
else
00219
kdDebug(6070) <<
"Window.open, smart policy, no event, <script> tag -> refused" <<
endl;
00220 }
00221
return false;
00222 }
00223
00224
00225 UString::UString(
const QString &d)
00226 {
00227
unsigned int len = d.
length();
00228 UChar *dat =
new UChar[len];
00229 memcpy(dat, d.
unicode(), len *
sizeof(UChar));
00230 rep = UString::Rep::create(dat, len);
00231 }
00232
00233 UString::UString(
const DOM::DOMString &d)
00234 {
00235
if (d.
isNull()) {
00236 attach(&Rep::null);
00237
return;
00238 }
00239
00240
unsigned int len = d.
length();
00241 UChar *dat =
new UChar[len];
00242 memcpy(dat, d.
unicode(), len *
sizeof(UChar));
00243 rep = UString::Rep::create(dat, len);
00244 }
00245
00246
DOM::DOMString UString::string()
const
00247
{
00248
return DOM::DOMString((
QChar*) data(), size());
00249 }
00250
00251
QString UString::qstring()
const
00252
{
00253
return QString((
QChar*) data(), size());
00254 }
00255
00256
QConstString UString::qconststring()
const
00257
{
00258
return QConstString((
QChar*) data(), size());
00259 }
00260
00261
DOM::DOMString Identifier::string()
const
00262
{
00263
return DOM::DOMString((
QChar*) data(), size());
00264 }
00265
00266
QString Identifier::qstring()
const
00267
{
00268
return QString((
QChar*) data(), size());
00269 }
00270
00271
DOM::Node KJS::toNode(
const Value& val)
00272 {
00273 Object obj = Object::dynamicCast(val);
00274
if (obj.isNull() || !obj.inherits(&DOMNode::info))
00275
return DOM::Node();
00276
00277
const DOMNode *dobj = static_cast<const DOMNode*>(obj.imp());
00278
return dobj->toNode();
00279 }
00280
00281 Value KJS::getString(
DOM::DOMString s)
00282 {
00283
if (s.
isNull())
00284
return Null();
00285
else
00286
return String(s);
00287 }
00288
00289
QVariant KJS::ValueToVariant(ExecState* exec,
const Value &val) {
00290
QVariant res;
00291
switch (val.type()) {
00292
case BooleanType:
00293 res =
QVariant(val.toBoolean(exec), 0);
00294
break;
00295
case NumberType:
00296 res = QVariant(val.toNumber(exec));
00297
break;
00298
case StringType:
00299 res = QVariant(val.toString(exec).qstring());
00300
break;
00301
default:
00302
00303
break;
00304 }
00305
return res;
00306 }