00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <khtmlview.h>
00023 #include "xml/dom2_eventsimpl.h"
00024 #include "rendering/render_canvas.h"
00025 #include "rendering/render_layer.h"
00026 #include "xml/dom_nodeimpl.h"
00027 #include "xml/dom_docimpl.h"
00028 #include "misc/htmltags.h"
00029 #include "misc/htmlattrs.h"
00030 #include "html/html_baseimpl.h"
00031 #include <kdebug.h>
00032 #include <khtml_part.h>
00033
00034 #include "kjs_dom.h"
00035 #include "kjs_html.h"
00036 #include "kjs_css.h"
00037 #include "kjs_range.h"
00038 #include "kjs_traversal.h"
00039 #include "kjs_events.h"
00040 #include "kjs_views.h"
00041 #include "kjs_window.h"
00042 #include "dom/dom_exception.h"
00043 #include "kjs_dom.lut.h"
00044 #include "khtmlpart_p.h"
00045
00046 using namespace KJS;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 DEFINE_PROTOTYPE("DOMNode",DOMNodeProto)
00073 IMPLEMENT_PROTOFUNC_DOM(DOMNodeProtoFunc)
00074 IMPLEMENT_PROTOTYPE(DOMNodeProto,DOMNodeProtoFunc)
00075
00076 const ClassInfo DOMNode::info = { "Node", 0, &DOMNodeTable, 0 };
00077
00078 DOMNode::DOMNode(ExecState *exec, const DOM::Node& n)
00079 : DOMObject(DOMNodeProto::self(exec)), node(n)
00080 {
00081 }
00082
00083 DOMNode::DOMNode(const Object& proto, const DOM::Node& n)
00084 : DOMObject(proto), node(n)
00085 {
00086 }
00087
00088 DOMNode::~DOMNode()
00089 {
00090 ScriptInterpreter::forgetDOMObject(node.handle());
00091 }
00092
00093 bool DOMNode::toBoolean(ExecState *) const
00094 {
00095 return !node.isNull();
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 Value DOMNode::tryGet(ExecState *exec, const Identifier &propertyName) const
00160 {
00161 #ifdef KJS_VERBOSE
00162 kdDebug(6070) << "DOMNode::tryGet " << propertyName.qstring() << endl;
00163 #endif
00164 return DOMObjectLookupGetValue<DOMNode, DOMObject>(exec, propertyName, &DOMNodeTable, this);
00165 }
00166
00167 Value DOMNode::getValueProperty(ExecState *exec, int token) const
00168 {
00169 switch (token) {
00170 case NodeName:
00171 return String(node.nodeName());
00172 case NodeValue:
00173 return getString(node.nodeValue());
00174 case NodeType:
00175 return Number((unsigned int)node.nodeType());
00176 case ParentNode:
00177 return getDOMNode(exec,node.parentNode());
00178 case ParentElement:
00179 return getDOMNode(exec,node.parentNode());
00180 case ChildNodes:
00181 return getDOMNodeList(exec,node.childNodes());
00182 case FirstChild:
00183 return getDOMNode(exec,node.firstChild());
00184 case LastChild:
00185 return getDOMNode(exec,node.lastChild());
00186 case PreviousSibling:
00187 return getDOMNode(exec,node.previousSibling());
00188 case NextSibling:
00189 return getDOMNode(exec,node.nextSibling());
00190 case Attributes:
00191 return getDOMNamedNodeMap(exec,node.attributes());
00192 case NamespaceURI:
00193 return getString(node.namespaceURI());
00194 case Prefix:
00195 return getString(node.prefix());
00196 case LocalName:
00197 return getString(node.localName());
00198 case OwnerDocument:
00199 return getDOMNode(exec,node.ownerDocument());
00200 case OnAbort:
00201 return getListener(DOM::EventImpl::ABORT_EVENT);
00202 case OnBlur:
00203 return getListener(DOM::EventImpl::BLUR_EVENT);
00204 case OnChange:
00205 return getListener(DOM::EventImpl::CHANGE_EVENT);
00206 case OnClick:
00207 return getListener(DOM::EventImpl::KHTML_ECMA_CLICK_EVENT);
00208 case OnDblClick:
00209 return getListener(DOM::EventImpl::KHTML_ECMA_DBLCLICK_EVENT);
00210 case OnDragDrop:
00211 return getListener(DOM::EventImpl::KHTML_DRAGDROP_EVENT);
00212 case OnError:
00213 return getListener(DOM::EventImpl::KHTML_ERROR_EVENT);
00214 case OnFocus:
00215 return getListener(DOM::EventImpl::FOCUS_EVENT);
00216 case OnKeyDown:
00217 return getListener(DOM::EventImpl::KEYDOWN_EVENT);
00218 case OnKeyPress:
00219 return getListener(DOM::EventImpl::KEYPRESS_EVENT);
00220 case OnKeyUp:
00221 return getListener(DOM::EventImpl::KEYUP_EVENT);
00222 case OnLoad:
00223 return getListener(DOM::EventImpl::LOAD_EVENT);
00224 case OnMouseDown:
00225 return getListener(DOM::EventImpl::MOUSEDOWN_EVENT);
00226 case OnMouseMove:
00227 return getListener(DOM::EventImpl::MOUSEMOVE_EVENT);
00228 case OnMouseOut:
00229 return getListener(DOM::EventImpl::MOUSEOUT_EVENT);
00230 case OnMouseOver:
00231 return getListener(DOM::EventImpl::MOUSEOVER_EVENT);
00232 case OnMouseUp:
00233 return getListener(DOM::EventImpl::MOUSEUP_EVENT);
00234 case OnMove:
00235 return getListener(DOM::EventImpl::KHTML_MOVE_EVENT);
00236 case OnReset:
00237 return getListener(DOM::EventImpl::RESET_EVENT);
00238 case OnResize:
00239 return getListener(DOM::EventImpl::RESIZE_EVENT);
00240 case OnSelect:
00241 return getListener(DOM::EventImpl::SELECT_EVENT);
00242 case OnSubmit:
00243 return getListener(DOM::EventImpl::SUBMIT_EVENT);
00244 case OnUnload:
00245 return getListener(DOM::EventImpl::UNLOAD_EVENT);
00246 case SourceIndex: {
00247
00248
00249
00250 DOM::Document doc = node.ownerDocument();
00251 if (doc.isHTMLDocument()) {
00252 DOM::HTMLCollection all = static_cast<DOM::HTMLDocument>(doc).all();
00253 unsigned long i = 0;
00254 DOM::Node n = all.firstItem();
00255 for ( ; !n.isNull() && n != node; n = all.nextItem() )
00256 ++i;
00257 Q_ASSERT( !n.isNull() );
00258 return Number(i);
00259 }
00260 }
00261 default:
00262
00263
00264
00265 DOM::DocumentImpl* docimpl = node.handle()->getDocument();
00266 if (docimpl) {
00267 docimpl->updateLayout();
00268 }
00269
00270 khtml::RenderObject *rend = node.handle()->renderer();
00271
00272 switch (token) {
00273 case OffsetLeft:
00274 return rend ? static_cast<Value>( Number( rend->offsetLeft() ) ) : Undefined();
00275 case OffsetTop:
00276 return rend ? static_cast<Value>( Number( rend->offsetTop() ) ) : Undefined();
00277 case OffsetWidth:
00278 return rend ? static_cast<Value>( Number( rend->offsetWidth() ) ) : Undefined();
00279 case OffsetHeight:
00280 return rend ? static_cast<Value>( Number( rend->offsetHeight() ) ) : Undefined();
00281 case OffsetParent:
00282 {
00283 khtml::RenderObject* par = rend ? rend->offsetParent() : 0;
00284 return getDOMNode( exec, par ? par->element() : 0 );
00285 }
00286 case ClientWidth:
00287 return rend ? static_cast<Value>( Number( rend->clientWidth() ) ) : Undefined();
00288 case ClientHeight:
00289 return rend ? static_cast<Value>( Number( rend->clientHeight() ) ) : Undefined();
00290 case ScrollWidth:
00291 return rend ? static_cast<Value>( Number(rend->scrollWidth()) ) : Undefined();
00292 case ScrollHeight:
00293 return rend ? static_cast<Value>( Number(rend->scrollHeight()) ) : Undefined();
00294 case ScrollLeft:
00295 if (rend && rend->layer()) {
00296 if (rend->isRoot() && !rend->style()->hidesOverflow())
00297 return Number( node.ownerDocument().view() ? node.ownerDocument().view()->contentsX() : 0);
00298 return Number( rend->layer()->scrollXOffset() );
00299 }
00300 return Number( 0 );
00301 case ScrollTop:
00302 if (rend && rend->layer()) {
00303 if (rend->isRoot() && !rend->style()->hidesOverflow())
00304 return Number( node.ownerDocument().view() ? node.ownerDocument().view()->contentsY() : 0);
00305 return Number( rend->layer()->scrollYOffset() );
00306 }
00307 return Number( 0 );
00308 default:
00309 kdDebug(6070) << "WARNING: Unhandled token in DOMNode::getValueProperty : " << token << endl;
00310 break;
00311 }
00312 }
00313 return Undefined();
00314 }
00315
00316
00317 void DOMNode::tryPut(ExecState *exec, const Identifier& propertyName, const Value& value, int attr)
00318 {
00319 #ifdef KJS_VERBOSE
00320 kdDebug(6070) << "DOMNode::tryPut " << propertyName.qstring() << endl;
00321 #endif
00322 DOMObjectLookupPut<DOMNode,DOMObject>(exec, propertyName, value, attr,
00323 &DOMNodeTable, this );
00324 }
00325
00326 void DOMNode::putValueProperty(ExecState *exec, int token, const Value& value, int )
00327 {
00328 switch (token) {
00329 case NodeValue:
00330 node.setNodeValue(value.toString(exec).string());
00331 break;
00332 case Prefix:
00333 node.setPrefix(value.toString(exec).string());
00334 break;
00335 case OnAbort:
00336 setListener(exec,DOM::EventImpl::ABORT_EVENT,value);
00337 break;
00338 case OnBlur:
00339 setListener(exec,DOM::EventImpl::BLUR_EVENT,value);
00340 break;
00341 case OnChange:
00342 setListener(exec,DOM::EventImpl::CHANGE_EVENT,value);
00343 break;
00344 case OnClick:
00345 setListener(exec,DOM::EventImpl::KHTML_ECMA_CLICK_EVENT,value);
00346 break;
00347 case OnDblClick:
00348 setListener(exec,DOM::EventImpl::KHTML_ECMA_DBLCLICK_EVENT,value);
00349 break;
00350 case OnDragDrop:
00351 setListener(exec,DOM::EventImpl::KHTML_DRAGDROP_EVENT,value);
00352 break;
00353 case OnError:
00354 setListener(exec,DOM::EventImpl::KHTML_ERROR_EVENT,value);
00355 break;
00356 case OnFocus:
00357 setListener(exec,DOM::EventImpl::FOCUS_EVENT,value);
00358 break;
00359 case OnKeyDown:
00360 setListener(exec,DOM::EventImpl::KEYDOWN_EVENT,value);
00361 break;
00362 case OnKeyPress:
00363 setListener(exec,DOM::EventImpl::KEYPRESS_EVENT,value);
00364 break;
00365 case OnKeyUp:
00366 setListener(exec,DOM::EventImpl::KEYUP_EVENT,value);
00367 break;
00368 case OnLoad:
00369 setListener(exec,DOM::EventImpl::LOAD_EVENT,value);
00370 break;
00371 case OnMouseDown:
00372 setListener(exec,DOM::EventImpl::MOUSEDOWN_EVENT,value);
00373 break;
00374 case OnMouseMove:
00375 setListener(exec,DOM::EventImpl::MOUSEMOVE_EVENT,value);
00376 break;
00377 case OnMouseOut:
00378 setListener(exec,DOM::EventImpl::MOUSEOUT_EVENT,value);
00379 break;
00380 case OnMouseOver:
00381 setListener(exec,DOM::EventImpl::MOUSEOVER_EVENT,value);
00382 break;
00383 case OnMouseUp:
00384 setListener(exec,DOM::EventImpl::MOUSEUP_EVENT,value);
00385 break;
00386 case OnMove:
00387 setListener(exec,DOM::EventImpl::KHTML_MOVE_EVENT,value);
00388 break;
00389 case OnReset:
00390 setListener(exec,DOM::EventImpl::RESET_EVENT,value);
00391 break;
00392 case OnResize:
00393 setListener(exec,DOM::EventImpl::RESIZE_EVENT,value);
00394 break;
00395 case OnSelect:
00396 setListener(exec,DOM::EventImpl::SELECT_EVENT,value);
00397 break;
00398 case OnSubmit:
00399 setListener(exec,DOM::EventImpl::SUBMIT_EVENT,value);
00400 break;
00401 case OnUnload:
00402 setListener(exec,DOM::EventImpl::UNLOAD_EVENT,value);
00403 break;
00404 default:
00405
00406 DOM::DocumentImpl* docimpl = node.handle()->getDocument();
00407 if (docimpl)
00408 docimpl->updateLayout();
00409
00410 khtml::RenderObject *rend = node.handle() ? node.handle()->renderer() : 0L;
00411
00412 switch (token) {
00413 case ScrollLeft:
00414 if (rend && rend->layer()) {
00415 if (rend->style()->hidesOverflow())
00416 rend->layer()->scrollToXOffset(value.toInt32(exec));
00417 else if (rend->isRoot()) {
00418 QScrollView* sview = node.ownerDocument().view();
00419 if (sview)
00420 sview->setContentsPos(value.toInt32(exec), sview->contentsY());
00421 }
00422 }
00423 break;
00424 case ScrollTop:
00425 if (rend && rend->layer()) {
00426 if (rend->style()->hidesOverflow())
00427 rend->layer()->scrollToYOffset(value.toInt32(exec));
00428 else if (rend->isRoot()) {
00429 QScrollView* sview = node.ownerDocument().view();
00430 if (sview)
00431 sview->setContentsPos(sview->contentsX(), value.toInt32(exec));
00432 }
00433 }
00434 break;
00435 default:
00436 kdDebug(6070) << "WARNING: DOMNode::putValueProperty unhandled token " << token << endl;
00437 }
00438 }
00439 }
00440
00441 Value DOMNode::toPrimitive(ExecState *exec, Type ) const
00442 {
00443 if (node.isNull())
00444 return Null();
00445
00446 return String(toString(exec));
00447 }
00448
00449 UString DOMNode::toString(ExecState *) const
00450 {
00451 if (node.isNull())
00452 return "null";
00453 UString s;
00454
00455 DOM::Element e = node;
00456 if ( !e.isNull() ) {
00457 s = e.nodeName().string();
00458 } else
00459 s = className();
00460
00461 return "[object " + s + "]";
00462 }
00463
00464 void DOMNode::setListener(ExecState *exec, int eventId, const Value& func) const
00465 {
00466 node.handle()->setHTMLEventListener(eventId,Window::retrieveActive(exec)->getJSEventListener(func,true));
00467 }
00468
00469 Value DOMNode::getListener(int eventId) const
00470 {
00471 DOM::EventListener *listener = node.handle()->getHTMLEventListener(eventId);
00472 JSEventListener *jsListener = static_cast<JSEventListener*>(listener);
00473 if ( jsListener && jsListener->listenerObjImp() )
00474 return jsListener->listenerObj();
00475 else
00476 return Null();
00477 }
00478
00479 void DOMNode::pushEventHandlerScope(ExecState *, ScopeChain &) const
00480 {
00481 }
00482
00483 Value DOMNodeProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00484 {
00485 KJS_CHECK_THIS( DOMNode, thisObj );
00486 DOM::Node node = static_cast<DOMNode *>( thisObj.imp() )->toNode();
00487 switch (id) {
00488 case DOMNode::HasAttributes:
00489 return Boolean(node.hasAttributes());
00490 case DOMNode::HasChildNodes:
00491 return Boolean(node.hasChildNodes());
00492 case DOMNode::CloneNode:
00493 return getDOMNode(exec,node.cloneNode(args[0].toBoolean(exec)));
00494 case DOMNode::Normalize:
00495 node.normalize();
00496 return Undefined();
00497 case DOMNode::IsSupported:
00498 return Boolean(node.isSupported(args[0].toString(exec).string(),args[1].toString(exec).string()));
00499 case DOMNode::AddEventListener: {
00500 JSEventListener *listener = Window::retrieveActive(exec)->getJSEventListener(args[1]);
00501 node.addEventListener(args[0].toString(exec).string(),listener,args[2].toBoolean(exec));
00502 return Undefined();
00503 }
00504 case DOMNode::RemoveEventListener: {
00505 JSEventListener *listener = Window::retrieveActive(exec)->getJSEventListener(args[1]);
00506 node.removeEventListener(args[0].toString(exec).string(),listener,args[2].toBoolean(exec));
00507 return Undefined();
00508 }
00509 case DOMNode::DispatchEvent:
00510 return Boolean(node.dispatchEvent(toEvent(args[0])));
00511 case DOMNode::AppendChild:
00512 return getDOMNode(exec,node.appendChild(toNode(args[0])));
00513 case DOMNode::RemoveChild:
00514 return getDOMNode(exec,node.removeChild(toNode(args[0])));
00515 case DOMNode::InsertBefore:
00516 return getDOMNode(exec,node.insertBefore(toNode(args[0]), toNode(args[1])));
00517 case DOMNode::ReplaceChild:
00518 return getDOMNode(exec,node.replaceChild(toNode(args[0]), toNode(args[1])));
00519 case DOMNode::Contains:
00520 {
00521 DOM::Node other = toNode(args[0]);
00522 if (!other.isNull() && node.nodeType()==DOM::Node::ELEMENT_NODE)
00523 {
00524 DOM::NodeBaseImpl *impl = static_cast<DOM::NodeBaseImpl *>(node.handle());
00525 bool retval = other.handle()->isAncestor(impl);
00526 return Boolean(retval);
00527 }
00528 return Undefined();
00529 }
00530 case DOMNode::InsertAdjacentHTML:
00531 {
00532
00533
00534 Range range = node.ownerDocument().createRange();
00535
00536 range.setStartBefore(node);
00537
00538 DocumentFragment docFrag = range.createContextualFragment(args[1].toString(exec).string());
00539
00540 DOMString where = args[0].toString(exec).string();
00541
00542 if (where == "beforeBegin" || where == "BeforeBegin")
00543 node.parentNode().insertBefore(docFrag, node);
00544 else if (where == "afterBegin" || where == "AfterBegin")
00545 node.insertBefore(docFrag, node.firstChild());
00546 else if (where == "beforeEnd" || where == "BeforeEnd")
00547 return getDOMNode(exec, node.appendChild(docFrag));
00548 else if (where == "afterEnd" || where == "AfterEnd")
00549 if (!node.nextSibling().isNull())
00550 node.parentNode().insertBefore(docFrag, node.nextSibling());
00551 else
00552 node.parentNode().appendChild(docFrag);
00553
00554 return Undefined();
00555 }
00556 case DOMNode::Item:
00557 return getDOMNode(exec, node.childNodes().item(static_cast<unsigned long>(args[0].toNumber(exec))));
00558 }
00559
00560 return Undefined();
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572 DEFINE_PROTOTYPE("DOMNodeList", DOMNodeListProto)
00573 IMPLEMENT_PROTOFUNC_DOM(DOMNodeListProtoFunc)
00574 IMPLEMENT_PROTOTYPE(DOMNodeListProto,DOMNodeListProtoFunc)
00575
00576 const ClassInfo DOMNodeList::info = { "NodeList", 0, 0, 0 };
00577
00578 DOMNodeList::DOMNodeList(ExecState *exec, const DOM::NodeList& l)
00579 : DOMObject(DOMNodeListProto::self(exec)), list(l) { }
00580
00581 DOMNodeList::~DOMNodeList()
00582 {
00583 ScriptInterpreter::forgetDOMObject(list.handle());
00584 }
00585
00586
00587
00588 bool DOMNodeList::hasProperty(ExecState *exec, const Identifier &p) const
00589 {
00590 if (p == lengthPropertyName)
00591 return true;
00592
00593 return ObjectImp::hasProperty(exec, p);
00594 }
00595
00596 Value DOMNodeList::tryGet(ExecState *exec, const Identifier &p) const
00597 {
00598 #ifdef KJS_VERBOSE
00599 kdDebug(6070) << "DOMNodeList::tryGet " << p.ascii() << endl;
00600 #endif
00601 if (p == lengthPropertyName)
00602 return Number(list.length());
00603
00604
00605 Object proto = Object::dynamicCast(prototype());
00606 if (proto.isValid() && proto.hasProperty(exec,p))
00607 return proto.get(exec,p);
00608
00609 Value result;
00610
00611
00612 bool ok;
00613 long unsigned int idx = p.toULong(&ok);
00614 if (ok)
00615 result = getDOMNode(exec,list.item(idx));
00616 else {
00617
00618 DOM::HTMLElement e;
00619 unsigned long l = list.length();
00620 bool found = false;
00621
00622 for ( unsigned long i = 0; i < l; i++ )
00623 if ( ( e = list.item( i ) ).id() == p.string() ) {
00624 result = getDOMNode(exec, list.item( i ) );
00625 found = true;
00626 break;
00627 }
00628
00629 if ( !found )
00630 result = ObjectImp::get(exec, p);
00631 }
00632
00633 return result;
00634 }
00635
00636
00637 Value DOMNodeList::call(ExecState *exec, Object &thisObj, const List &args)
00638 {
00639
00640 Value val;
00641 try {
00642 val = tryCall(exec, thisObj, args);
00643 }
00644
00645 catch (...) {
00646 Object err = Error::create(exec, GeneralError, "Exception from DOMNodeList");
00647 exec->setException(err);
00648 }
00649 return val;
00650 }
00651
00652 Value DOMNodeList::tryCall(ExecState *exec, Object &, const List &args)
00653 {
00654
00655 UString s = args[0].toString(exec);
00656
00657
00658 bool ok;
00659 unsigned int u = s.toULong(&ok);
00660 if (ok)
00661 return getDOMNode(exec,list.item(u));
00662
00663
00664
00665
00666 Value result = tryGet(exec, Identifier(s));
00667
00668 if (result.isValid())
00669 return result;
00670
00671 return Undefined();
00672 }
00673
00674
00675 Value DOMNodeListProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00676 {
00677 KJS_CHECK_THIS( KJS::DOMNodeList, thisObj );
00678 DOM::NodeList list = static_cast<DOMNodeList *>(thisObj.imp())->nodeList();
00679 switch (id) {
00680 case KJS::DOMNodeList::Item:
00681 return getDOMNode(exec, list.item(args[0].toInt32(exec)));
00682 case KJS::DOMNodeList::NamedItem:
00683 {
00684
00685
00686 DOM::HTMLElement e;
00687 unsigned long len = list.length();
00688 DOM::DOMString s = args[0].toString(exec).string();
00689
00690 for ( unsigned long i = 0; i < len; i++ )
00691 {
00692 e = list.item( i );
00693 if ( !e.isNull() && (
00694 e.id() == s || static_cast<ElementImpl *>(e.handle())->getAttribute(ATTR_NAME) == s )
00695 )
00696 {
00697 return getDOMNode(exec, e );
00698 }
00699 }
00700 return Null();
00701 }
00702 default:
00703 return Undefined();
00704 }
00705 }
00706
00707
00708
00709 const ClassInfo DOMAttr::info = { "Attr", &DOMNode::info, &DOMAttrTable, 0 };
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 Value DOMAttr::tryGet(ExecState *exec, const Identifier &propertyName) const
00720 {
00721 #ifdef KJS_VERBOSE
00722 kdDebug(6070) << "DOMAttr::tryGet " << propertyName.qstring() << endl;
00723 #endif
00724 return DOMObjectLookupGetValue<DOMAttr,DOMNode>(exec, propertyName,
00725 &DOMAttrTable, this );
00726 }
00727
00728 Value DOMAttr::getValueProperty(ExecState *exec, int token) const
00729 {
00730 switch (token) {
00731 case Name:
00732 return String(static_cast<DOM::Attr>(node).name());
00733 case Specified:
00734 return Boolean(static_cast<DOM::Attr>(node).specified());
00735 case ValueProperty:
00736 return String(static_cast<DOM::Attr>(node).value());
00737 case OwnerElement:
00738 return getDOMNode(exec,static_cast<DOM::Attr>(node).ownerElement());
00739 }
00740 return Value();
00741 }
00742
00743 void DOMAttr::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr)
00744 {
00745 #ifdef KJS_VERBOSE
00746 kdDebug(6070) << "DOMAttr::tryPut " << propertyName.qstring() << endl;
00747 #endif
00748 DOMObjectLookupPut<DOMAttr,DOMNode>(exec, propertyName, value, attr,
00749 &DOMAttrTable, this );
00750 }
00751
00752 void DOMAttr::putValueProperty(ExecState *exec, int token, const Value& value, int )
00753 {
00754 switch (token) {
00755 case ValueProperty:
00756 static_cast<DOM::Attr>(node).setValue(value.toString(exec).string());
00757 return;
00758 default:
00759 kdDebug(6070) << "WARNING: DOMAttr::putValueProperty unhandled token " << token << endl;
00760 }
00761 }
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791 DEFINE_PROTOTYPE("DOMDocument", DOMDocumentProto)
00792 IMPLEMENT_PROTOFUNC_DOM(DOMDocumentProtoFunc)
00793 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMDocumentProto, DOMDocumentProtoFunc, DOMNodeProto)
00794
00795 const ClassInfo DOMDocument::info = { "Document", &DOMNode::info, &DOMDocumentTable, 0 };
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 DOMDocument::DOMDocument(ExecState *exec, const DOM::Document& d)
00812 : DOMNode(DOMDocumentProto::self(exec), d) { }
00813
00814 DOMDocument::DOMDocument(const Object& proto, const DOM::Document& d)
00815 : DOMNode(proto, d) { }
00816
00817 DOMDocument::~DOMDocument()
00818 {
00819 ScriptInterpreter::forgetDOMObject(node.handle());
00820 }
00821
00822 Value DOMDocument::tryGet(ExecState *exec, const Identifier &propertyName) const
00823 {
00824 #ifdef KJS_VERBOSE
00825 kdDebug(6070) << "DOMDocument::tryGet " << propertyName.qstring() << endl;
00826 #endif
00827 return DOMObjectLookupGetValue<DOMDocument, DOMNode>(
00828 exec, propertyName, &DOMDocumentTable, this);
00829 }
00830
00831 Value DOMDocument::getValueProperty(ExecState *exec, int token) const
00832 {
00833 DOM::Document doc = static_cast<DOM::Document>(node);
00834
00835 switch(token) {
00836 case DocType:
00837 return getDOMNode(exec,doc.doctype());
00838 case Implementation:
00839 return getDOMDOMImplementation(exec,doc.implementation());
00840 case DocumentElement:
00841 return getDOMNode(exec,doc.documentElement());
00842 case StyleSheets:
00843
00844 return getDOMStyleSheetList(exec, doc.styleSheets(), doc);
00845 case DOMDocument::DefaultView:
00846 {
00847 KHTMLView *view = node.handle()->getDocument()->view();
00848 if (view)
00849 return Window::retrieve(view->part());
00850 return getDOMAbstractView(exec, doc.defaultView());
00851 }
00852 case PreferredStylesheetSet:
00853 return String(doc.preferredStylesheetSet());
00854 case SelectedStylesheetSet:
00855 return String(doc.selectedStylesheetSet());
00856 case ReadyState:
00857 {
00858 DOM::DocumentImpl* docimpl = node.handle()->getDocument();
00859 if ( docimpl && docimpl->view() )
00860 {
00861 KHTMLPart* part = docimpl->view()->part();
00862 if ( part ) {
00863 if (part->d->m_bComplete) return String("complete");
00864 if (docimpl->parsing()) return String("loading");
00865 return String("loaded");
00866
00867
00868 }
00869 }
00870 return Undefined();
00871 }
00872 case Async:
00873 return Boolean(doc.async());
00874 default:
00875 kdDebug(6070) << "WARNING: DOMDocument::getValueProperty unhandled token " << token << endl;
00876 return Value();
00877 }
00878 }
00879
00880 void DOMDocument::tryPut(ExecState *exec, const Identifier& propertyName, const Value& value, int attr)
00881 {
00882 #ifdef KJS_VERBOSE
00883 kdDebug(6070) << "DOMDocument::tryPut " << propertyName.qstring() << endl;
00884 #endif
00885 DOMObjectLookupPut<DOMDocument,DOMNode>(exec, propertyName, value, attr, &DOMDocumentTable, this );
00886 }
00887
00888 void DOMDocument::putValueProperty(ExecState *exec, int token, const Value& value, int )
00889 {
00890 DOM::Document doc = static_cast<DOM::Document>(node);
00891 switch (token) {
00892 case SelectedStylesheetSet: {
00893 doc.setSelectedStylesheetSet(value.toString(exec).string());
00894 break;
00895 }
00896 case Async: {
00897 doc.setAsync(value.toBoolean(exec));
00898 break;
00899 }
00900 }
00901 }
00902
00903 Value DOMDocumentProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00904 {
00905 KJS_CHECK_THIS( KJS::DOMDocument, thisObj );
00906 DOM::Node node = static_cast<DOMNode *>( thisObj.imp() )->toNode();
00907 DOM::Document doc = static_cast<DOM::Document>(node);
00908 String str = args[0].toString(exec);
00909 DOM::DOMString s = str.value().string();
00910
00911 switch(id) {
00912 case DOMDocument::CreateElement:
00913 return getDOMNode(exec,doc.createElement(s));
00914 case DOMDocument::CreateDocumentFragment:
00915 return getDOMNode(exec,doc.createDocumentFragment());
00916 case DOMDocument::CreateTextNode:
00917 return getDOMNode(exec,doc.createTextNode(s));
00918 case DOMDocument::CreateComment:
00919 return getDOMNode(exec,doc.createComment(s));
00920 case DOMDocument::CreateCDATASection:
00921 return getDOMNode(exec,doc.createCDATASection(s));
00922 case DOMDocument::CreateProcessingInstruction:
00923 return getDOMNode(exec,doc.createProcessingInstruction(args[0].toString(exec).string(),
00924 args[1].toString(exec).string()));
00925 case DOMDocument::CreateAttribute:
00926 return getDOMNode(exec,doc.createAttribute(s));
00927 case DOMDocument::CreateEntityReference:
00928 return getDOMNode(exec,doc.createEntityReference(args[0].toString(exec).string()));
00929 case DOMDocument::GetElementsByTagName:
00930 return getDOMNodeList(exec,doc.getElementsByTagName(s));
00931 case DOMDocument::ImportNode:
00932 return getDOMNode(exec,doc.importNode(toNode(args[0]), args[1].toBoolean(exec)));
00933 case DOMDocument::CreateElementNS:
00934 return getDOMNode(exec,doc.createElementNS(args[0].toString(exec).string(), args[1].toString(exec).string()));
00935 case DOMDocument::CreateAttributeNS:
00936 return getDOMNode(exec,doc.createAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
00937 case DOMDocument::GetElementsByTagNameNS:
00938 return getDOMNodeList(exec,doc.getElementsByTagNameNS(args[0].toString(exec).string(),
00939 args[1].toString(exec).string()));
00940 case DOMDocument::GetElementById:
00941 #ifdef KJS_VERBOSE
00942 kdDebug(6070) << "DOMDocument::GetElementById looking for " << args[0].toString(exec).string() << endl;
00943 #endif
00944 return getDOMNode(exec,doc.getElementById(args[0].toString(exec).string()));
00945 case DOMDocument::CreateRange:
00946 return getDOMRange(exec,doc.createRange());
00947 case DOMDocument::CreateNodeIterator:
00948 if (args[2].isA(NullType)) {
00949 DOM::NodeFilter filter;
00950 return getDOMNodeIterator(exec,
00951 doc.createNodeIterator(toNode(args[0]),
00952 (long unsigned int)(args[1].toNumber(exec)),
00953 filter,args[3].toBoolean(exec)));
00954 }
00955 else {
00956 Object obj = Object::dynamicCast(args[2]);
00957 if (obj.isValid())
00958 {
00959 DOM::CustomNodeFilter *customFilter = new JSNodeFilter(obj);
00960 DOM::NodeFilter filter = DOM::NodeFilter::createCustom(customFilter);
00961 return getDOMNodeIterator(exec,
00962 doc.createNodeIterator(
00963 toNode(args[0]),(long unsigned int)(args[1].toNumber(exec)),
00964 filter,args[3].toBoolean(exec)));
00965 }
00966 }
00967 case DOMDocument::CreateTreeWalker:
00968 return getDOMTreeWalker(exec,doc.createTreeWalker(toNode(args[0]),(long unsigned int)(args[1].toNumber(exec)),
00969 toNodeFilter(args[2]),args[3].toBoolean(exec)));
00970 case DOMDocument::CreateEvent:
00971 return getDOMEvent(exec,doc.createEvent(s));
00972 case DOMDocument::GetOverrideStyle: {
00973 DOM::Node arg0 = toNode(args[0]);
00974 if (arg0.nodeType() != DOM::Node::ELEMENT_NODE)
00975 return Undefined();
00976 else
00977 return getDOMCSSStyleDeclaration(exec,doc.getOverrideStyle(static_cast<DOM::Element>(arg0),args[1].toString(exec).string()));
00978 }
00979 case DOMDocument::Abort:
00980 doc.abort();
00981 break;
00982 case DOMDocument::Load: {
00983 Window* active = Window::retrieveActive(exec);
00984
00985
00986 KHTMLPart *khtmlpart = ::qt_cast<KHTMLPart *>(active->part());
00987 if (khtmlpart) {
00988
00989 QString dstUrl = khtmlpart->htmlDocument().completeURL(s).string();
00990 KParts::ReadOnlyPart *part = static_cast<KJS::ScriptInterpreter*>(exec->interpreter())->part();
00991 if (part->url().host() == KURL(dstUrl).host()) {
00992 kdDebug(6070) << "JavaScript: access granted for document.load() of " << dstUrl << endl;
00993 doc.load(dstUrl);
00994 }
00995 else {
00996 kdDebug(6070) << "JavaScript: access denied for document.load() of " << dstUrl << endl;
00997 }
00998 }
00999 break;
01000 }
01001 case DOMDocument::LoadXML:
01002 doc.loadXML(s);
01003 break;
01004 default:
01005 break;
01006 }
01007
01008 return Undefined();
01009 }
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032 DEFINE_PROTOTYPE("DOMElement",DOMElementProto)
01033 IMPLEMENT_PROTOFUNC_DOM(DOMElementProtoFunc)
01034 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMElementProto,DOMElementProtoFunc,DOMNodeProto)
01035
01036 const ClassInfo DOMElement::info = { "Element", &DOMNode::info, &DOMElementTable, 0 };
01037
01038
01039
01040
01041
01042
01043 DOMElement::DOMElement(ExecState *exec, const DOM::Element& e)
01044 : DOMNode(DOMElementProto::self(exec), e) { }
01045
01046 DOMElement::DOMElement(const Object& proto, const DOM::Element& e)
01047 : DOMNode(proto, e) { }
01048
01049 Value DOMElement::tryGet(ExecState *exec, const Identifier &propertyName) const
01050 {
01051 #ifdef KJS_VERBOSE
01052 kdDebug(6070) << "DOMElement::tryGet " << propertyName.qstring() << endl;
01053 #endif
01054 DOM::Element element = static_cast<DOM::Element>(node);
01055
01056 const HashEntry* entry = Lookup::findEntry(&DOMElementTable, propertyName);
01057 if (entry)
01058 {
01059 switch( entry->value ) {
01060 case TagName:
01061 return String(element.tagName());
01062 case Style:
01063 return getDOMCSSStyleDeclaration(exec,element.style());
01064 default:
01065 kdDebug(6070) << "WARNING: Unhandled token in DOMElement::tryGet : " << entry->value << endl;
01066 break;
01067 }
01068 }
01069
01070
01071
01072 if (DOMNode::hasProperty(exec, propertyName))
01073 return DOMNode::tryGet(exec, propertyName);
01074
01075 DOM::DOMString attr = element.getAttribute( propertyName.string() );
01076
01077 if ( !attr.isNull() )
01078 return String( attr );
01079
01080 return Undefined();
01081 }
01082
01083 Value DOMElementProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01084 {
01085 KJS_CHECK_THIS( KJS::DOMNode, thisObj );
01086 DOM::Node node = static_cast<DOMNode *>( thisObj.imp() )->toNode();
01087 DOM::Element element = static_cast<DOM::Element>(node);
01088
01089 switch(id) {
01090 case DOMElement::GetAttribute:
01094 return getString(element.getAttribute(args[0].toString(exec).string()));
01095 case DOMElement::SetAttribute:
01096 element.setAttribute(args[0].toString(exec).string(),args[1].toString(exec).string());
01097 return Undefined();
01098 case DOMElement::RemoveAttribute:
01099 element.removeAttribute(args[0].toString(exec).string());
01100 return Undefined();
01101 case DOMElement::GetAttributeNode:
01102 return getDOMNode(exec,element.getAttributeNode(args[0].toString(exec).string()));
01103 case DOMElement::SetAttributeNode:
01104 return getDOMNode(exec,element.setAttributeNode((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01105 case DOMElement::RemoveAttributeNode:
01106 return getDOMNode(exec,element.removeAttributeNode((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01107 case DOMElement::GetElementsByTagName:
01108 return getDOMNodeList(exec,element.getElementsByTagName(args[0].toString(exec).string()));
01109 case DOMElement::HasAttribute:
01110 return Boolean(element.hasAttribute(args[0].toString(exec).string()));
01111 case DOMElement::GetAttributeNS:
01112 return String(element.getAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01113 case DOMElement::SetAttributeNS:
01114 element.setAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string(),args[2].toString(exec).string());
01115 return Undefined();
01116 case DOMElement::RemoveAttributeNS:
01117 element.removeAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string());
01118 return Undefined();
01119 case DOMElement::GetAttributeNodeNS:
01120 return getDOMNode(exec,element.getAttributeNodeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01121 case DOMElement::SetAttributeNodeNS:
01122 return getDOMNode(exec,element.setAttributeNodeNS((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01123 case DOMElement::GetElementsByTagNameNS:
01124 return getDOMNodeList(exec,element.getElementsByTagNameNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01125 case DOMElement::HasAttributeNS:
01126 return Boolean(element.hasAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01127 default:
01128 return Undefined();
01129 }
01130 }
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144 DEFINE_PROTOTYPE("DOMImplementation",DOMDOMImplementationProto)
01145 IMPLEMENT_PROTOFUNC_DOM(DOMDOMImplementationProtoFunc)
01146 IMPLEMENT_PROTOTYPE(DOMDOMImplementationProto,DOMDOMImplementationProtoFunc)
01147
01148 const ClassInfo DOMDOMImplementation::info = { "DOMImplementation", 0, 0, 0 };
01149
01150 DOMDOMImplementation::DOMDOMImplementation(ExecState *exec, const DOM::DOMImplementation& i)
01151 : DOMObject(DOMDOMImplementationProto::self(exec)), implementation(i) { }
01152
01153 DOMDOMImplementation::~DOMDOMImplementation()
01154 {
01155 ScriptInterpreter::forgetDOMObject(implementation.handle());
01156 }
01157
01158 Value DOMDOMImplementationProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01159 {
01160 KJS_CHECK_THIS( KJS::DOMDOMImplementation, thisObj );
01161 DOM::DOMImplementation implementation = static_cast<DOMDOMImplementation *>( thisObj.imp() )->toImplementation();
01162
01163 switch(id) {
01164 case DOMDOMImplementation::HasFeature:
01165 return Boolean(implementation.hasFeature(args[0].toString(exec).string(),args[1].toString(exec).string()));
01166 case DOMDOMImplementation::CreateDocumentType:
01167 return getDOMNode(exec,implementation.createDocumentType(args[0].toString(exec).string(),args[1].toString(exec).string(),args[2].toString(exec).string()));
01168 case DOMDOMImplementation::CreateDocument: {
01169
01170
01171 KHTMLPart *part = ::qt_cast<KHTMLPart*>(static_cast<KJS::ScriptInterpreter*>(exec->interpreter())->part());
01172 if (part) {
01173 Document doc = implementation.createDocument(args[0].toString(exec).string(),args[1].toString(exec).string(),toNode(args[2]));
01174 KURL url = static_cast<DocumentImpl*>(part->document().handle())->URL();
01175 static_cast<DocumentImpl*>(doc.handle())->setURL(url.url());
01176 return getDOMNode(exec,doc);
01177 }
01178 break;
01179 }
01180 case DOMDOMImplementation::CreateCSSStyleSheet:
01181 return getDOMStyleSheet(exec,implementation.createCSSStyleSheet(args[0].toString(exec).string(),args[1].toString(exec).string()));
01182 case DOMDOMImplementation::CreateHTMLDocument:
01183 return getDOMNode(exec, implementation.createHTMLDocument(args[0].toString(exec).string()));
01184 default:
01185 break;
01186 }
01187 return Undefined();
01188 }
01189
01190
01191
01192 const ClassInfo DOMDocumentType::info = { "DocumentType", &DOMNode::info, &DOMDocumentTypeTable, 0 };
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205 DOMDocumentType::DOMDocumentType(ExecState *exec, const DOM::DocumentType& dt)
01206 : DOMNode( exec, dt ) { }
01207
01208 Value DOMDocumentType::tryGet(ExecState *exec, const Identifier &propertyName) const
01209 {
01210 #ifdef KJS_VERBOSE
01211 kdDebug(6070) << "DOMDocumentType::tryGet " << propertyName.qstring() << endl;
01212 #endif
01213 return DOMObjectLookupGetValue<DOMDocumentType, DOMNode>(exec, propertyName, &DOMDocumentTypeTable, this);
01214 }
01215
01216 Value DOMDocumentType::getValueProperty(ExecState *exec, int token) const
01217 {
01218 DOM::DocumentType type = static_cast<DOM::DocumentType>(node);
01219 switch (token) {
01220 case Name:
01221 return String(type.name());
01222 case Entities:
01223 return getDOMNamedNodeMap(exec,type.entities());
01224 case Notations:
01225 return getDOMNamedNodeMap(exec,type.notations());
01226 case PublicId:
01227 return String(type.publicId());
01228 case SystemId:
01229 return String(type.systemId());
01230 case InternalSubset:
01231 return getString(type.internalSubset());
01232 default:
01233 kdDebug(6070) << "WARNING: DOMDocumentType::getValueProperty unhandled token " << token << endl;
01234 return Value();
01235 }
01236 }
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255 DEFINE_PROTOTYPE("NamedNodeMap", DOMNamedNodeMapProto)
01256 IMPLEMENT_PROTOFUNC_DOM(DOMNamedNodeMapProtoFunc)
01257 IMPLEMENT_PROTOTYPE(DOMNamedNodeMapProto,DOMNamedNodeMapProtoFunc)
01258
01259 const ClassInfo DOMNamedNodeMap::info = { "NamedNodeMap", 0, &DOMNamedNodeMapTable, 0 };
01260
01261 DOMNamedNodeMap::DOMNamedNodeMap(ExecState *exec, const DOM::NamedNodeMap& m)
01262 : DOMObject(DOMNamedNodeMapProto::self(exec)), map(m) { }
01263
01264 DOMNamedNodeMap::~DOMNamedNodeMap()
01265 {
01266 ScriptInterpreter::forgetDOMObject(map.handle());
01267 }
01268
01269 bool DOMNamedNodeMap::hasProperty(ExecState *exec, const Identifier &p) const
01270 {
01271
01272 return DOMObject::hasProperty(exec, p);
01273 }
01274
01275 Value DOMNamedNodeMap::tryGet(ExecState* exec, const Identifier &p) const
01276 {
01277 if (p == lengthPropertyName)
01278 return Number(map.length());
01279
01280
01281 bool ok;
01282 long unsigned int idx = p.toULong(&ok);
01283 if (ok)
01284 return getDOMNode(exec,map.item(idx));
01285
01286
01287 return DOMObject::tryGet(exec, p);
01288 }
01289
01290 Value DOMNamedNodeMapProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01291 {
01292 KJS_CHECK_THIS( KJS::DOMNamedNodeMap, thisObj );
01293 DOM::NamedNodeMap map = static_cast<DOMNamedNodeMap *>(thisObj.imp())->toMap();
01294
01295 switch(id) {
01296 case DOMNamedNodeMap::GetNamedItem:
01297 return getDOMNode(exec, map.getNamedItem(args[0].toString(exec).string()));
01298 case DOMNamedNodeMap::SetNamedItem:
01299 return getDOMNode(exec, map.setNamedItem((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01300 case DOMNamedNodeMap::RemoveNamedItem:
01301 return getDOMNode(exec, map.removeNamedItem(args[0].toString(exec).string()));
01302 case DOMNamedNodeMap::Item:
01303 return getDOMNode(exec, map.item(args[0].toInt32(exec)));
01304 case DOMNamedNodeMap::GetNamedItemNS:
01305 return getDOMNode(exec, map.getNamedItemNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01306 case DOMNamedNodeMap::SetNamedItemNS:
01307 return getDOMNode(exec, map.setNamedItemNS(toNode(args[0])));
01308 case DOMNamedNodeMap::RemoveNamedItemNS:
01309 return getDOMNode(exec, map.removeNamedItemNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01310 default:
01311 break;
01312 }
01313
01314 return Undefined();
01315 }
01316
01317
01318
01319 const ClassInfo DOMProcessingInstruction::info = { "ProcessingInstruction", &DOMNode::info, &DOMProcessingInstructionTable, 0 };
01320
01321
01322
01323
01324
01325
01326
01327
01328 Value DOMProcessingInstruction::tryGet(ExecState *exec, const Identifier &propertyName) const
01329 {
01330 return DOMObjectLookupGetValue<DOMProcessingInstruction, DOMNode>(exec, propertyName, &DOMProcessingInstructionTable, this);
01331 }
01332
01333 Value DOMProcessingInstruction::getValueProperty(ExecState *exec, int token) const
01334 {
01335 switch (token) {
01336 case Target:
01337 return String(static_cast<DOM::ProcessingInstruction>(node).target());
01338 case Data:
01339 return String(static_cast<DOM::ProcessingInstruction>(node).data());
01340 case Sheet:
01341 return getDOMStyleSheet(exec,static_cast<DOM::ProcessingInstruction>(node).sheet());
01342 default:
01343 kdDebug(6070) << "WARNING: DOMProcessingInstruction::getValueProperty unhandled token " << token << endl;
01344 return Value();
01345 }
01346 }
01347
01348 void DOMProcessingInstruction::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr)
01349 {
01350
01351 if (propertyName == "data")
01352 static_cast<DOM::ProcessingInstruction>(node).setData(value.toString(exec).string());
01353 else
01354 DOMNode::tryPut(exec, propertyName,value,attr);
01355 }
01356
01357
01358
01359 const ClassInfo DOMNotation::info = { "Notation", &DOMNode::info, &DOMNotationTable, 0 };
01360
01361
01362
01363
01364
01365
01366
01367 Value DOMNotation::tryGet(ExecState *exec, const Identifier &propertyName) const
01368 {
01369 return DOMObjectLookupGetValue<DOMNotation, DOMNode>(exec, propertyName, &DOMNotationTable, this);
01370 }
01371
01372 Value DOMNotation::getValueProperty(ExecState *, int token) const
01373 {
01374 switch (token) {
01375 case PublicId:
01376 return String(static_cast<DOM::Notation>(node).publicId());
01377 case SystemId:
01378 return String(static_cast<DOM::Notation>(node).systemId());
01379 default:
01380 kdDebug(6070) << "WARNING: DOMNotation::getValueProperty unhandled token " << token << endl;
01381 return Value();
01382 }
01383 }
01384
01385
01386
01387 const ClassInfo DOMEntity::info = { "Entity", &DOMNode::info, 0, 0 };
01388
01389
01390
01391
01392
01393
01394
01395
01396 Value DOMEntity::tryGet(ExecState *exec, const Identifier &propertyName) const
01397 {
01398 return DOMObjectLookupGetValue<DOMEntity, DOMNode>(exec, propertyName, &DOMEntityTable, this);
01399 }
01400
01401 Value DOMEntity::getValueProperty(ExecState *, int token) const
01402 {
01403 switch (token) {
01404 case PublicId:
01405 return String(static_cast<DOM::Entity>(node).publicId());
01406 case SystemId:
01407 return String(static_cast<DOM::Entity>(node).systemId());
01408 case NotationName:
01409 return String(static_cast<DOM::Entity>(node).notationName());
01410 default:
01411 kdDebug(6070) << "WARNING: DOMEntity::getValueProperty unhandled token " << token << endl;
01412 return Value();
01413 }
01414 }
01415
01416
01417
01418 bool KJS::checkNodeSecurity(ExecState *exec, const DOM::Node& n)
01419 {
01420
01421 if (n.isNull())
01422 return true;
01423 KHTMLView *view = n.handle()->getDocument()->view();
01424 Window* win = view && view->part() ? Window::retrieveWindow(view->part()) : 0L;
01425 if ( !win || !win->isSafeScript(exec) )
01426 return false;
01427 return true;
01428 }
01429
01430 Value KJS::getDOMNode(ExecState *exec, const DOM::Node& n)
01431 {
01432 DOMObject *ret = 0;
01433 if (n.isNull())
01434 return Null();
01435 ScriptInterpreter* interp = static_cast<ScriptInterpreter *>(exec->interpreter());
01436 if ((ret = interp->getDOMObject(n.handle())))
01437 return Value(ret);
01438
01439 switch (n.nodeType()) {
01440 case DOM::Node::ELEMENT_NODE:
01441 if (static_cast<DOM::Element>(n).isHTMLElement())
01442 ret = new HTMLElement(exec, static_cast<DOM::HTMLElement>(n));
01443 else
01444 ret = new DOMElement(exec, static_cast<DOM::Element>(n));
01445 break;
01446 case DOM::Node::ATTRIBUTE_NODE:
01447 ret = new DOMAttr(exec, static_cast<DOM::Attr>(n));
01448 break;
01449 case DOM::Node::TEXT_NODE:
01450 case DOM::Node::CDATA_SECTION_NODE:
01451 ret = new DOMText(exec, static_cast<DOM::Text>(n));
01452 break;
01453 case DOM::Node::ENTITY_REFERENCE_NODE:
01454 ret = new DOMNode(exec, n);
01455 break;
01456 case DOM::Node::ENTITY_NODE:
01457 ret = new DOMEntity(exec, static_cast<DOM::Entity>(n));
01458 break;
01459 case DOM::Node::PROCESSING_INSTRUCTION_NODE:
01460 ret = new DOMProcessingInstruction(exec, static_cast<DOM::ProcessingInstruction>(n));
01461 break;
01462 case DOM::Node::COMMENT_NODE:
01463 ret = new DOMCharacterData(exec, static_cast<DOM::CharacterData>(n));
01464 break;
01465 case DOM::Node::DOCUMENT_NODE:
01466 if (static_cast<DOM::Document>(n).isHTMLDocument())
01467 ret = new HTMLDocument(exec, static_cast<DOM::HTMLDocument>(n));
01468 else
01469 ret = new DOMDocument(exec, static_cast<DOM::Document>(n));
01470 break;
01471 case DOM::Node::DOCUMENT_TYPE_NODE:
01472 ret = new DOMDocumentType(exec, static_cast<DOM::DocumentType>(n));
01473 break;
01474 case DOM::Node::DOCUMENT_FRAGMENT_NODE:
01475 ret = new DOMNode(exec, n);
01476 break;
01477 case DOM::Node::NOTATION_NODE:
01478 ret = new DOMNotation(exec, static_cast<DOM::Notation>(n));
01479 break;
01480 default:
01481 ret = new DOMNode(exec, n);
01482 }
01483 interp->putDOMObject(n.handle(),ret);
01484
01485 return Value(ret);
01486 }
01487
01488 Value KJS::getDOMNamedNodeMap(ExecState *exec, const DOM::NamedNodeMap& m)
01489 {
01490 return Value(cacheDOMObject<DOM::NamedNodeMap, KJS::DOMNamedNodeMap>(exec, m));
01491 }
01492
01493 Value KJS::getDOMNodeList(ExecState *exec, const DOM::NodeList& l)
01494 {
01495 return Value(cacheDOMObject<DOM::NodeList, KJS::DOMNodeList>(exec, l));
01496 }
01497
01498 Value KJS::getDOMDOMImplementation(ExecState *exec, const DOM::DOMImplementation& i)
01499 {
01500 return Value(cacheDOMObject<DOM::DOMImplementation, KJS::DOMDOMImplementation>(exec, i));
01501 }
01502
01503
01504
01505 const ClassInfo NodeConstructor::info = { "NodeConstructor", 0, &NodeConstructorTable, 0 };
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523 NodeConstructor::NodeConstructor(ExecState *exec)
01524 : DOMObject(exec->interpreter()->builtinObjectPrototype())
01525 {
01526 }
01527
01528 Value NodeConstructor::tryGet(ExecState *exec, const Identifier &propertyName) const
01529 {
01530 return DOMObjectLookupGetValue<NodeConstructor, DOMObject>(exec, propertyName, &NodeConstructorTable, this);
01531 }
01532
01533 Value NodeConstructor::getValueProperty(ExecState *, int token) const
01534 {
01535
01536 return Number((unsigned int)token);
01537 #if 0
01538 switch (token) {
01539 case ELEMENT_NODE:
01540 return Number((unsigned int)DOM::Node::ELEMENT_NODE);
01541 case ATTRIBUTE_NODE:
01542 return Number((unsigned int)DOM::Node::ATTRIBUTE_NODE);
01543 case TEXT_NODE:
01544 return Number((unsigned int)DOM::Node::TEXT_NODE);
01545 case CDATA_SECTION_NODE:
01546 return Number((unsigned int)DOM::Node::CDATA_SECTION_NODE);
01547 case ENTITY_REFERENCE_NODE:
01548 return Number((unsigned int)DOM::Node::ENTITY_REFERENCE_NODE);
01549 case ENTITY_NODE:
01550 return Number((unsigned int)DOM::Node::ENTITY_NODE);
01551 case PROCESSING_INSTRUCTION_NODE:
01552 return Number((unsigned int)DOM::Node::PROCESSING_INSTRUCTION_NODE);
01553 case COMMENT_NODE:
01554 return Number((unsigned int)DOM::Node::COMMENT_NODE);
01555 case DOCUMENT_NODE:
01556 return Number((unsigned int)DOM::Node::DOCUMENT_NODE);
01557 case DOCUMENT_TYPE_NODE:
01558 return Number((unsigned int)DOM::Node::DOCUMENT_TYPE_NODE);
01559 case DOCUMENT_FRAGMENT_NODE:
01560 return Number((unsigned int)DOM::Node::DOCUMENT_FRAGMENT_NODE);
01561 case NOTATION_NODE:
01562 return Number((unsigned int)DOM::Node::NOTATION_NODE);
01563 default:
01564 kdDebug(6070) << "WARNING: NodeConstructor::getValueProperty unhandled token " << token << endl;
01565 return Value();
01566 }
01567 #endif
01568 }
01569
01570 Object KJS::getNodeConstructor(ExecState *exec)
01571 {
01572 return Object(cacheGlobalObject<NodeConstructor>(exec, "[[node.constructor]]"));
01573 }
01574
01575
01576
01577 const ClassInfo DOMExceptionConstructor::info = { "DOMExceptionConstructor", 0, 0, 0 };
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599 DOMExceptionConstructor::DOMExceptionConstructor(ExecState* exec)
01600 : DOMObject(exec->interpreter()->builtinObjectPrototype())
01601 {
01602 }
01603
01604 Value DOMExceptionConstructor::tryGet(ExecState *exec, const Identifier &propertyName) const
01605 {
01606 return DOMObjectLookupGetValue<DOMExceptionConstructor, DOMObject>(exec, propertyName, &DOMExceptionConstructorTable, this);
01607 }
01608
01609 Value DOMExceptionConstructor::getValueProperty(ExecState *, int token) const
01610 {
01611
01612 return Number((unsigned int)token);
01613 #if 0
01614 switch (token) {
01615 case INDEX_SIZE_ERR:
01616 return Number((unsigned int)DOM::DOMException::INDEX_SIZE_ERR);
01617 case DOMSTRING_SIZE_ERR:
01618 return Number((unsigned int)DOM::DOMException::DOMSTRING_SIZE_ERR);
01619 case HIERARCHY_REQUEST_ERR:
01620 return Number((unsigned int)DOM::DOMException::HIERARCHY_REQUEST_ERR);
01621 case WRONG_DOCUMENT_ERR:
01622 return Number((unsigned int)DOM::DOMException::WRONG_DOCUMENT_ERR);
01623 case INVALID_CHARACTER_ERR:
01624 return Number((unsigned int)DOM::DOMException::INVALID_CHARACTER_ERR);
01625 case NO_DATA_ALLOWED_ERR:
01626 return Number((unsigned int)DOM::DOMException::NO_DATA_ALLOWED_ERR);
01627 case NO_MODIFICATION_ALLOWED_ERR:
01628 return Number((unsigned int)DOM::DOMException::NO_MODIFICATION_ALLOWED_ERR);
01629 case NOT_FOUND_ERR:
01630 return Number((unsigned int)DOM::DOMException::NOT_FOUND_ERR);
01631 case NOT_SUPPORTED_ERR:
01632 return Number((unsigned int)DOM::DOMException::NOT_SUPPORTED_ERR);
01633 case INUSE_ATTRIBUTE_ERR:
01634 return Number((unsigned int)DOM::DOMException::INUSE_ATTRIBUTE_ERR);
01635 case INVALID_STATE_ERR:
01636 return Number((unsigned int)DOM::DOMException::INVALID_STATE_ERR);
01637 case SYNTAX_ERR:
01638 return Number((unsigned int)DOM::DOMException::SYNTAX_ERR);
01639 case INVALID_MODIFICATION_ERR:
01640 return Number((unsigned int)DOM::DOMException::INVALID_MODIFICATION_ERR);
01641 case NAMESPACE_ERR:
01642 return Number((unsigned int)DOM::DOMException::NAMESPACE_ERR);
01643 case INVALID_ACCESS_ERR:
01644 return Number((unsigned int)DOM::DOMException::INVALID_ACCESS_ERR);
01645 default:
01646 kdDebug(6070) << "WARNING: DOMExceptionConstructor::getValueProperty unhandled token " << token << endl;
01647 return Value();
01648 }
01649 #endif
01650 }
01651
01652 Object KJS::getDOMExceptionConstructor(ExecState *exec)
01653 {
01654 return cacheGlobalObject<DOMExceptionConstructor>(exec, "[[DOMException.constructor]]");
01655 }
01656
01657
01658
01659
01660
01661
01662
01663
01664 const ClassInfo KJS::DOMNamedNodesCollection::info = { "DOMNamedNodesCollection", 0, &DOMNamedNodesCollectionTable, 0 };
01665
01666
01667
01668
01669 DOMNamedNodesCollection::DOMNamedNodesCollection(ExecState *exec, const QValueList<DOM::Node>& nodes )
01670 : DOMObject(exec->interpreter()->builtinObjectPrototype()),
01671 m_nodes(nodes)
01672 {
01673
01674 }
01675
01676 Value DOMNamedNodesCollection::tryGet(ExecState *exec, const Identifier &propertyName) const
01677 {
01678 kdDebug(6070) << k_funcinfo << propertyName.ascii() << endl;
01679 if (propertyName == lengthPropertyName)
01680 return Number(m_nodes.count());
01681
01682 bool ok;
01683 unsigned int u = propertyName.toULong(&ok);
01684 if (ok && u < m_nodes.count()) {
01685 DOM::Node node = m_nodes[u];
01686 return getDOMNode(exec,node);
01687 }
01688 return DOMObject::tryGet(exec,propertyName);
01689 }
01690
01691
01692
01693 const ClassInfo DOMCharacterData::info = { "CharacterImp",
01694 &DOMNode::info, &DOMCharacterDataTable, 0 };
01695
01696
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706
01707
01708 DEFINE_PROTOTYPE("DOMCharacterData",DOMCharacterDataProto)
01709 IMPLEMENT_PROTOFUNC_DOM(DOMCharacterDataProtoFunc)
01710 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMCharacterDataProto,DOMCharacterDataProtoFunc, DOMNodeProto)
01711
01712 DOMCharacterData::DOMCharacterData(ExecState *exec, const DOM::CharacterData& d)
01713 : DOMNode(DOMCharacterDataProto::self(exec), d) {}
01714
01715 DOMCharacterData::DOMCharacterData(const Object& proto, const DOM::CharacterData& d)
01716 : DOMNode(proto, d) {}
01717
01718 Value DOMCharacterData::tryGet(ExecState *exec, const Identifier &p) const
01719 {
01720 #ifdef KJS_VERBOSE
01721 kdDebug(6070)<<"DOMCharacterData::tryGet "<<p.string().string()<<endl;
01722 #endif
01723 return DOMObjectLookupGetValue<DOMCharacterData,DOMNode>(exec,p,&DOMCharacterDataTable,this);
01724 }
01725
01726 Value DOMCharacterData::getValueProperty(ExecState *, int token) const
01727 {
01728 DOM::CharacterData data = static_cast<DOM::CharacterData>(node);
01729 switch (token) {
01730 case Data:
01731 return String(data.data());
01732 case Length:
01733 return Number(data.length());
01734 default:
01735 kdDebug(6070) << "WARNING: Unhandled token in DOMCharacterData::getValueProperty : " << token << endl;
01736 return Value();
01737 }
01738 }
01739
01740 void DOMCharacterData::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr)
01741 {
01742 if (propertyName == "data")
01743 static_cast<DOM::CharacterData>(node).setData(value.toString(exec).string());
01744 else
01745 DOMNode::tryPut(exec, propertyName,value,attr);
01746 }
01747
01748 Value DOMCharacterDataProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01749 {
01750 KJS_CHECK_THIS( KJS::DOMCharacterData, thisObj );
01751 DOM::CharacterData data = static_cast<DOMCharacterData *>(thisObj.imp())->toData();
01752 switch(id) {
01753 case DOMCharacterData::SubstringData:
01754 return String(data.substringData(args[0].toInteger(exec),args[1].toInteger(exec)));
01755 case DOMCharacterData::AppendData:
01756 data.appendData(args[0].toString(exec).string());
01757 return Undefined();
01758 break;
01759 case DOMCharacterData::InsertData:
01760 data.insertData(args[0].toInteger(exec),args[1].toString(exec).string());
01761 return Undefined();
01762 break;
01763 case DOMCharacterData::DeleteData:
01764 data.deleteData(args[0].toInteger(exec),args[1].toInteger(exec));
01765 return Undefined();
01766 break;
01767 case DOMCharacterData::ReplaceData:
01768 data.replaceData(args[0].toInteger(exec),args[1].toInteger(exec),args[2].toString(exec).string());
01769 return Undefined();
01770 default:
01771 break;
01772 }
01773 return Undefined();
01774 }
01775
01776
01777
01778 const ClassInfo DOMText::info = { "Text",
01779 &DOMCharacterData::info, 0, 0 };
01780
01781
01782
01783
01784
01785 DEFINE_PROTOTYPE("DOMText",DOMTextProto)
01786 IMPLEMENT_PROTOFUNC_DOM(DOMTextProtoFunc)
01787 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMTextProto,DOMTextProtoFunc,DOMCharacterDataProto)
01788
01789 DOMText::DOMText(ExecState *exec, const DOM::Text& t)
01790 : DOMCharacterData(DOMTextProto::self(exec), t) { }
01791
01792 Value DOMText::tryGet(ExecState *exec, const Identifier &p) const
01793 {
01794 if (p.isEmpty())
01795 return Undefined();
01796 else
01797 return DOMCharacterData::tryGet(exec, p);
01798 }
01799
01800 Value DOMTextProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01801 {
01802 KJS_CHECK_THIS( KJS::DOMText, thisObj );
01803 DOM::Text text = static_cast<DOMText *>(thisObj.imp())->toText();
01804 switch(id) {
01805 case DOMText::SplitText:
01806 return getDOMNode(exec,text.splitText(args[0].toInteger(exec)));
01807 default:
01808 break;
01809 }
01810 return Undefined();
01811 }