📄 kjs_window.cpp
字号:
DOM::DocumentImpl *doc = static_cast<DOM::DocumentImpl*>(part->htmlDocument().handle()); if (!doc) return; doc->setHTMLWindowEventListener(eventId,getJSEventListener(func,true));}Value Window::getListener(ExecState *exec, int eventId) const{ KHTMLPart *part = ::qt_cast<KHTMLPart *>(m_frame->m_part); if (!part || !isSafeScript(exec)) return Undefined(); DOM::DocumentImpl *doc = static_cast<DOM::DocumentImpl*>(part->htmlDocument().handle()); if (!doc) return Undefined(); DOM::EventListener *listener = doc->getHTMLWindowEventListener(eventId); if (listener && static_cast<JSEventListener*>(listener)->listenerObjImp()) return static_cast<JSEventListener*>(listener)->listenerObj(); else return Null();}JSEventListener *Window::getJSEventListener(const Value& val, bool html){ // This function is so hot that it's worth coding it directly with imps. KHTMLPart *part = ::qt_cast<KHTMLPart *>(m_frame->m_part); if (!part || val.type() != ObjectType) return 0; // It's ObjectType, so it must be valid. Object listenerObject = Object::dynamicCast(val); ObjectImp *listenerObjectImp = listenerObject.imp(); // 'listener' is not a simple ecma function. (Always use sanity checks: Better safe than sorry!) if (!listenerObject.implementsCall() && part && part->jScript() && part->jScript()->interpreter()) { Interpreter *interpreter = part->jScript()->interpreter(); // 'listener' probably is an EventListener object containing a 'handleEvent' function. Value handleEventValue = listenerObject.get(interpreter->globalExec(), Identifier("handleEvent")); Object handleEventObject = Object::dynamicCast(handleEventValue); if(handleEventObject.isValid() && handleEventObject.implementsCall()) { listenerObject = handleEventObject; listenerObjectImp = handleEventObject.imp(); } } JSEventListener *existingListener = jsEventListeners[listenerObjectImp]; if (existingListener) { if ( existingListener->isHTMLEventListener() != html ) // The existingListener will have the wrong type, so onclick= will behave like addEventListener or vice versa. kdWarning() << "getJSEventListener: event listener already found but with html=" << !html << " - please report this, we thought it would never happen" << endl; return existingListener; } // Note that the JSEventListener constructor adds it to our jsEventListeners list return new JSEventListener(listenerObject, listenerObjectImp, Object(this), html);}JSLazyEventListener *Window::getJSLazyEventListener(const QString& code, const QString& name, DOM::NodeImpl *node){ return new JSLazyEventListener(code, name, Object(this), node);}void Window::clear( ExecState *exec ){ delete winq; winq = 0L; // Get rid of everything, those user vars could hold references to DOM nodes deleteAllProperties( exec ); // Break the dependency between the listeners and their object QPtrDictIterator<JSEventListener> it(jsEventListeners); for (; it.current(); ++it) it.current()->clear(); // Forget about the listeners (the DOM::NodeImpls will delete them) jsEventListeners.clear(); if (m_frame) { KJSProxy* proxy = m_frame->m_jscript; if (proxy) // i.e. JS not disabled { winq = new WindowQObject(this); // Now recreate a working global object for the next URL that will use us KJS::Interpreter *interpreter = proxy->interpreter(); interpreter->initGlobalObject(); } }}void Window::setCurrentEvent( DOM::Event *evt ){ m_evt = evt; //kdDebug(6070) << "Window " << this << " (part=" << m_part << ")::setCurrentEvent m_evt=" << evt << endl;}void Window::goURL(ExecState* exec, const QString& url, bool lockHistory){ Window* active = Window::retrieveActive(exec); KHTMLPart *part = ::qt_cast<KHTMLPart *>(m_frame->m_part); KHTMLPart *active_part = ::qt_cast<KHTMLPart *>(active->part()); // Complete the URL using the "active part" (running interpreter) if (active_part && part) { if (url[0] == QChar('#')) { part->gotoAnchor(url.mid(1)); } else { QString dstUrl = active_part->htmlDocument().completeURL(url).string(); kdDebug(6070) << "Window::goURL dstUrl=" << dstUrl << endl; // check if we're allowed to inject javascript // SYNC check with khtml_part.cpp::slotRedirect! if ( isSafeScript(exec) || dstUrl.find(QString::fromLatin1("javascript:"), 0, false) != 0 ) part->scheduleRedirection(-1, dstUrl, lockHistory); } } else if (!part && !m_frame->m_part.isNull()) { KParts::BrowserExtension *b = KParts::BrowserExtension::childObject(m_frame->m_part); if (b) b->emit openURLRequest(m_frame->m_frame->element()->getDocument()->completeURL(url)); kdDebug() << "goURL for ROPart" << endl; }}KParts::ReadOnlyPart *Window::part() const { return m_frame.isNull() ? 0L : static_cast<KParts::ReadOnlyPart *>(m_frame->m_part);}void Window::delayedGoHistory( int steps ){ m_delayed.append( DelayedAction( DelayedGoHistory, steps ) );}void Window::goHistory( int steps ){ KHTMLPart *part = ::qt_cast<KHTMLPart *>(m_frame->m_part); if(!part) // TODO history readonlypart return; KParts::BrowserExtension *ext = part->browserExtension(); if(!ext) return; KParts::BrowserInterface *iface = ext->browserInterface(); if ( !iface ) return; iface->callMethod( "goHistory(int)", steps ); //emit ext->goHistory(steps);}void KJS::Window::resizeTo(QWidget* tl, int width, int height){ KHTMLPart *part = ::qt_cast<KHTMLPart *>(m_frame->m_part); if(!part) // TODO resizeTo readonlypart return; KParts::BrowserExtension *ext = part->browserExtension(); if (!ext) { kdDebug(6070) << "Window::resizeTo found no browserExtension" << endl; return; } // Security check: within desktop limits and bigger than 100x100 (per spec) if ( width < 100 || height < 100 ) { kdDebug(6070) << "Window::resizeTo refused, window would be too small ("<<width<<","<<height<<")" << endl; return; } QRect sg = KGlobalSettings::desktopGeometry(tl); if ( width > sg.width() || height > sg.height() ) { kdDebug(6070) << "Window::resizeTo refused, window would be too big ("<<width<<","<<height<<")" << endl; return; } kdDebug(6070) << "resizing to " << width << "x" << height << endl; emit ext->resizeTopLevelWidget( width, height ); // If the window is out of the desktop, move it up/left // (maybe we should use workarea instead of sg, otherwise the window ends up below kicker) int right = tl->x() + tl->frameGeometry().width(); int bottom = tl->y() + tl->frameGeometry().height(); int moveByX = 0; int moveByY = 0; if ( right > sg.right() ) moveByX = - right + sg.right(); // always <0 if ( bottom > sg.bottom() ) moveByY = - bottom + sg.bottom(); // always <0 if ( moveByX || moveByY ) emit ext->moveTopLevelWidget( tl->x() + moveByX , tl->y() + moveByY );}Value Window::openWindow(ExecState *exec, const List& args){ KHTMLPart *part = ::qt_cast<KHTMLPart *>(m_frame->m_part); if (!part) return Undefined(); KHTMLView *widget = part->view(); Value v = args[0]; QString str = v.toString(exec).qstring(); // prepare arguments KURL url; if (!str.isEmpty()) { KHTMLPart* p = ::qt_cast<KHTMLPart *>(Window::retrieveActive(exec)->m_frame->m_part); if ( p ) url = p->htmlDocument().completeURL(str).string(); if ( !p || !static_cast<DOM::DocumentImpl*>(p->htmlDocument().handle())->isURLAllowed(url.url()) ) return Undefined(); } KHTMLSettings::KJSWindowOpenPolicy policy = part->settings()->windowOpenPolicy(part->url().host()); if ( policy == KHTMLSettings::KJSWindowOpenAsk ) { emit part->browserExtension()->requestFocus(part); QString caption; if (!part->url().host().isEmpty()) caption = part->url().host() + " - "; caption += i18n( "Confirmation: JavaScript Popup" ); if ( KMessageBox::questionYesNo(widget, str.isEmpty() ? i18n( "This site is requesting to open up a new browser " "window via JavaScript.\n" "Do you want to allow this?" ) : i18n( "<qt>This site is requesting to open<p>%1</p>in a new browser window via JavaScript.<br />" "Do you want to allow this?</qt>").arg(KStringHandler::csqueeze(url.htmlURL(), 100)), caption, i18n("Allow"), i18n("Do Not Allow") ) == KMessageBox::Yes ) policy = KHTMLSettings::KJSWindowOpenAllow; } else if ( policy == KHTMLSettings::KJSWindowOpenSmart ) { // window.open disabled unless from a key/mouse event if (static_cast<ScriptInterpreter *>(exec->interpreter())->isWindowOpenAllowed()) policy = KHTMLSettings::KJSWindowOpenAllow; } QString frameName = args.size() > 1 ? args[1].toString(exec).qstring() : QString("_blank"); v = args[2]; QString features; if (!v.isNull() && v.type() != UndefinedType && v.toString(exec).size() > 0) { features = v.toString(exec).qstring(); // Buggy scripts have ' at beginning and end, cut those if (features.startsWith("\'") && features.endsWith("\'")) features = features.mid(1, features.length()-2); } if ( policy != KHTMLSettings::KJSWindowOpenAllow ) { if ( url.isEmpty() ) part->setSuppressedPopupIndicator(true, 0); else { part->setSuppressedPopupIndicator(true, part); m_suppressedWindowInfo.append( SuppressedWindowInfo( url, frameName, features ) ); } return Undefined(); } else { return executeOpenWindow(exec, url, frameName, features); }}Value Window::executeOpenWindow(ExecState *exec, const KURL& url, const QString& frameName, const QString& features){ KHTMLPart *p = ::qt_cast<KHTMLPart *>(m_frame->m_part); KHTMLView *widget = p->view(); KParts::WindowArgs winargs; // scan feature argument if (!features.isEmpty()) { // specifying window params means false defaults winargs.menuBarVisible = false; winargs.toolBarsVisible = false; winargs.statusBarVisible = false; winargs.scrollBarsVisible = false; QStringList flist = QStringList::split(',', features); QStringList::ConstIterator it = flist.begin(); while (it != flist.end()) { QString s = *it++; QString key, val; int pos = s.find('='); if (pos >= 0) { key = s.left(pos).stripWhiteSpace().lower(); val = s.mid(pos + 1).stripWhiteSpace().lower(); QRect screen = KGlobalSettings::desktopGeometry(widget->topLevelWidget()); if (key == "left" || key == "screenx") { winargs.x = (int)val.toFloat() + screen.x(); if (winargs.x < screen.x() || winargs.x > screen.right()) winargs.x = screen.x(); // only safe choice until size is determined } else if (key == "top" || key == "screeny") { winargs.y = (int)val.toFloat() + screen.y(); if (winargs.y < screen.y() || winargs.y > screen.bottom()) winargs.y = screen.y(); // only safe choice until size is determined } else if (key == "height") { winargs.height = (int)val.toFloat() + 2*qApp->style().pixelMetric( QStyle::PM_DefaultFrameWidth ) + 2; if (winargs.height > screen.height()) // should actually check workspace winargs.height = screen.height(); if (winargs.height < 100) winargs.height = 100; } else if (key == "width") { winargs.width = (int)val.toFloat() + 2*qApp->style().pixelMetric( QStyle::PM_DefaultFrameWidth ) + 2; if (winargs.width > screen.width()) // should actually check workspace winargs.width = screen.width(); if (winargs.width < 100) winargs.width = 100; } else { goto boolargs; } continue; } else { // leaving away the value gives true key = s.stripWhiteSpace().lower(); val = "1"; } boolargs: if (key == "menubar") winargs.menuBarVisible = (val == "1" || val == "yes"); else if (key == "toolbar") winargs.toolBarsVisible = (val == "1" || val == "yes"); else if (key == "location") // ### missing in WindowArgs winargs.toolBarsVisible = (val == "1" || val == "yes"); else if (key == "status" || key == "statusbar") winargs.statusBarVisible = (val == "1" || val == "yes"); else if (key == "scrollbars") winargs.scrollBarsVisible = (val == "1" || val == "yes"); else if (key == "resizable") winargs.resizable = (val == "1" || val == "yes"); else if (key == "fullscreen") winargs.fullscreen = (val == "1" || val == "yes"); } } KParts::URLArgs uargs; uargs.frameName = frameName; if ( uargs.frameName.lower() == "_top" ) { while ( p->parentPart() ) p = p->parentPart(); Window::retrieveWindow(p)->goURL(exec, url.url(), false /*don't lock history*/); return Window::retrieve(p); } if ( uargs.frameName.lower() == "_parent" ) { if ( p->parentPart() ) p = p->parentPart(); Window::retrieveWindow(p)->goURL(exec, url.url(), false /*don't lock history*/); return Window::retrieve(p); } if ( uargs.frameName.lower() == "_self") { Window::retrieveWindow(p)->goURL(exec, url.url(), false /*don't lock history*/); return Window::retrieve(p); } if ( uargs.frameName.lower() == "replace" ) { Window::retrieveWindow(p)->goURL(exec, url.url(), true /*lock history*/); return Window::retrieve(p); } uargs.serviceType = "text/html"; // request window (new or existing if framename is set) KParts::ReadOnlyPart *newPart = 0L; emit p->browserExtension()->createNewWindow(KURL(), uargs,winargs,newPart); if (newPart && ::qt_cast<KHTMLPart*>(newPart)) { KHTMLPart *khtmlpart = static_cast<KHTMLPart*>(newPart);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -