📄 khtml_part.cpp
字号:
{ if (!d->m_doc) return false; HTMLCollectionImpl *anchors = new HTMLCollectionImpl( d->m_doc, HTMLCollectionImpl::DOC_ANCHORS); anchors->ref(); NodeImpl *n = anchors->namedItem(name); anchors->deref(); if(!n) { kdDebug(6050) << "KHTMLPart::gotoAnchor no node found" << endl; return false; } int x = 0, y = 0; HTMLElementImpl *a = static_cast<HTMLElementImpl *>(n); a->getUpperLeftCorner(x, y); d->m_view->setContentsPos(x-50, y-50); return true;}void KHTMLPart::setFontSizes( const QValueList<int> &newFontSizes ){ d->m_settings->setFontSizes( newFontSizes );}QValueList<int> KHTMLPart::fontSizes() const{ return d->m_settings->fontSizes();}void KHTMLPart::resetFontSizes(){ d->m_settings->resetFontSizes();}void KHTMLPart::setStandardFont( const QString &name ){ d->m_settings->setStdFontName(name);}void KHTMLPart::setFixedFont( const QString &name ){ d->m_settings->setFixedFontName(name);}void KHTMLPart::setURLCursor( const QCursor &c ){ d->m_linkCursor = c;}const QCursor &KHTMLPart::urlCursor() const{ return d->m_linkCursor;}bool KHTMLPart::onlyLocalReferences() const{ return d->m_onlyLocalReferences;}void KHTMLPart::setOnlyLocalReferences(bool enable){ d->m_onlyLocalReferences = enable;}void KHTMLPart::findTextBegin(){ d->m_findPos = -1; d->m_findNode = 0;}bool KHTMLPart::findTextNext( const QRegExp &exp, bool forward ){ if ( !d->m_doc ) return false; if(!d->m_findNode) { if (d->m_doc->isHTMLDocument()) d->m_findNode = static_cast<HTMLDocumentImpl*>(d->m_doc)->body(); else d->m_findNode = d->m_doc; } if ( !d->m_findNode || d->m_findNode->id() == ID_FRAMESET ) return false; while(1) { if( (d->m_findNode->nodeType() == Node::TEXT_NODE || d->m_findNode->nodeType() == Node::CDATA_SECTION_NODE) && d->m_findNode->renderer() ) { DOMStringImpl *t = (static_cast<TextImpl *>(d->m_findNode))->string(); QConstString s(t->s, t->l); d->m_findPos = s.string().find(exp, d->m_findPos+1); if(d->m_findPos != -1) { int x = 0, y = 0; khtml::RenderText *text = static_cast<khtml::RenderText *>(d->m_findNode->renderer()); text->posOfChar(d->m_findPos, x, y); d->m_view->setContentsPos(x-50, y-50); return true; } } d->m_findPos = -1; NodeImpl *next; if ( forward ) { next = d->m_findNode->firstChild(); if(!next) next = d->m_findNode->nextSibling(); while(d->m_findNode && !next) { d->m_findNode = d->m_findNode->parentNode(); if( d->m_findNode ) { next = d->m_findNode->nextSibling(); } } } else { next = d->m_findNode->lastChild(); if (!next ) next = d->m_findNode->previousSibling(); while ( d->m_findNode && !next ) { d->m_findNode = d->m_findNode->parentNode(); if( d->m_findNode ) { next = d->m_findNode->previousSibling(); } } } d->m_findNode = next; if(!d->m_findNode) return false; }}bool KHTMLPart::findTextNext( const QString &str, bool forward, bool caseSensitive ){ if ( !d->m_doc ) return false; if(!d->m_findNode) { if (d->m_doc->isHTMLDocument()) d->m_findNode = static_cast<HTMLDocumentImpl*>(d->m_doc)->body(); else d->m_findNode = d->m_doc; } if ( !d->m_findNode || d->m_findNode->id() == ID_FRAMESET ) return false; while(1) { if( (d->m_findNode->nodeType() == Node::TEXT_NODE || d->m_findNode->nodeType() == Node::CDATA_SECTION_NODE) && d->m_findNode->renderer() ) { DOMStringImpl *t = (static_cast<TextImpl *>(d->m_findNode))->string(); QConstString s(t->s, t->l); d->m_findPos = s.string().find(str, d->m_findPos+1, caseSensitive); if(d->m_findPos != -1) { int x = 0, y = 0; static_cast<khtml::RenderText *>(d->m_findNode->renderer()) ->posOfChar(d->m_findPos, x, y); d->m_view->setContentsPos(x-50, y-50); d->m_selectionStart = d->m_findNode; d->m_startOffset = d->m_findPos; d->m_selectionEnd = d->m_findNode; d->m_endOffset = d->m_findPos + str.length(); d->m_startBeforeEnd = true; d->m_doc->setSelection( d->m_selectionStart.handle(), d->m_startOffset, d->m_selectionEnd.handle(), d->m_endOffset ); emitSelectionChanged(); return true; } } d->m_findPos = -1; NodeImpl *next; if ( forward ) { next = d->m_findNode->firstChild(); if(!next) next = d->m_findNode->nextSibling(); while(d->m_findNode && !next) { d->m_findNode = d->m_findNode->parentNode(); if( d->m_findNode ) { next = d->m_findNode->nextSibling(); } } } else { next = d->m_findNode->lastChild(); if (!next ) next = d->m_findNode->previousSibling(); while ( d->m_findNode && !next ) { d->m_findNode = d->m_findNode->parentNode(); if( d->m_findNode ) { next = d->m_findNode->previousSibling(); } } } d->m_findNode = next; if(!d->m_findNode) return false; }}QString KHTMLPart::selectedText() const{ QString text; DOM::Node n = d->m_selectionStart; while(!n.isNull()) { if(n.nodeType() == DOM::Node::TEXT_NODE) { QString str = static_cast<TextImpl *>(n.handle())->data().string(); if(n == d->m_selectionStart && n == d->m_selectionEnd) text = str.mid(d->m_startOffset, d->m_endOffset - d->m_startOffset); else if(n == d->m_selectionStart) text = str.mid(d->m_startOffset); else if(n == d->m_selectionEnd) text += str.left(d->m_endOffset); else text += str; } else { // This is our simple HTML -> ASCII transformation: unsigned short id = n.elementId(); switch(id) { case ID_TD: case ID_TH: case ID_BR: case ID_HR: case ID_OL: case ID_UL: case ID_LI: case ID_DD: case ID_DL: case ID_DT: case ID_PRE: case ID_BLOCKQUOTE: text += "\n"; break; case ID_P: case ID_TR: case ID_H1: case ID_H2: case ID_H3: case ID_H4: case ID_H5: case ID_H6: text += "\n\n"; break; } } if(n == d->m_selectionEnd) break; DOM::Node next = n.firstChild(); if(next.isNull()) next = n.nextSibling(); while( next.isNull() && !n.parentNode().isNull() ) { n = n.parentNode(); next = n.nextSibling(); } n = next; } return text;}bool KHTMLPart::hasSelection() const{ return ( !d->m_selectionStart.isNull() && !d->m_selectionEnd.isNull() );}DOM::Range KHTMLPart::selection() const{ DOM::Range r = document().createRange();DOM::Range(); r.setStart( d->m_selectionStart, d->m_startOffset ); r.setEnd( d->m_selectionEnd, d->m_endOffset ); return r;}void KHTMLPart::setSelection( const DOM::Range &r ){ d->m_selectionStart = r.startContainer(); d->m_startOffset = r.startOffset(); d->m_selectionEnd = r.endContainer(); d->m_endOffset = r.endOffset(); d->m_doc->setSelection(d->m_selectionStart.handle(),d->m_startOffset, d->m_selectionEnd.handle(),d->m_endOffset);}// TODO merge with other overURL (BCI)void KHTMLPart::overURL( const QString &url, const QString &target, bool shiftPressed ){ if( d->m_kjsStatusBarText.isEmpty() || shiftPressed ) { overURL( url, target ); } else { emit onURL( url ); emit setStatusBarText( d->m_kjsStatusBarText ); d->m_kjsStatusBarText = QString::null; }}void KHTMLPart::overURL( const QString &url, const QString &target ){ emit onURL( url ); if ( url.isEmpty() ) { emit setStatusBarText(url); return; } if (url.find( QString::fromLatin1( "javascript:" ),0, false ) != -1 ) { emit setStatusBarText( url.mid( url.find( "javascript:", 0, false ) ) ); return; } KURL u = completeURL( url ); // special case for <a href=""> if ( url.isEmpty() ) u.setFileName( url ); QString com; KMimeType::Ptr typ = KMimeType::findByURL( u ); if ( typ ) com = typ->comment( u, false ); if ( u.isMalformed() ) { emit setStatusBarText(u.prettyURL()); return; } if ( u.isLocalFile() ) { // TODO : use KIO::stat() and create a KFileItem out of its result, // to use KFileItem::statusBarText() QCString path = QFile::encodeName( u.path() ); struct stat buff; bool ok = !stat( path.data(), &buff ); struct stat lbuff; if (ok) ok = !lstat( path.data(), &lbuff ); QString text = u.url(); QString text2 = text; if (ok && S_ISLNK( lbuff.st_mode ) ) { QString tmp; if ( com.isNull() ) tmp = i18n( "Symbolic Link"); else tmp = i18n("%1 (Link)").arg(com); char buff_two[1024]; text += " -> "; int n = readlink ( path.data(), buff_two, 1022); if (n == -1) { text2 += " "; text2 += tmp; emit setStatusBarText(text2); return; } buff_two[n] = 0; text += buff_two; text += " "; text += tmp; } else if ( ok && S_ISREG( buff.st_mode ) ) { if (buff.st_size < 1024) text = i18n("%2 (%1 bytes)").arg((long) buff.st_size).arg(text2); // always put the URL last, in case it contains '%' else { float d = (float) buff.st_size/1024.0; text = i18n("%1 (%2 K)").arg(text2).arg(KGlobal::locale()->formatNumber(d, 2)); // was %.2f } text += " "; text += com; } else if ( ok && S_ISDIR( buff.st_mode ) ) { text += " "; text += com; } else { text += " "; text += com; } emit setStatusBarText(text); } else { QString extra; if (target == QString::fromLatin1("_blank")) { extra = i18n(" (In new window)"); } else if (!target.isEmpty() && (target != QString::fromLatin1("_top")) && (target != QString::fromLatin1("_self")) && (target != QString::fromLatin1("_parent"))) { extra = i18n(" (In other frame)"); } if (u.protocol() == QString::fromLatin1("mailto")) { QString mailtoMsg/* = QString::fromLatin1("<img src=%1>").arg(locate("icon", QString::fromLatin1("locolor/16x16/actions/mail_send.png")))*/; mailtoMsg += i18n("Email to: ") + KURL::decode_string(u.path()); QStringList queries = QStringList::split('&', u.query().mid(1)); for (QStringList::Iterator it = queries.begin(); it != queries.end(); ++it) if ((*it).startsWith(QString::fromLatin1("subject="))) mailtoMsg += i18n(" - Subject: ") + KURL::decode_string((*it).mid(8)); else if ((*it).startsWith(QString::fromLatin1("cc="))) mailtoMsg += i18n(" - CC: ") + KURL::decode_string((*it).mid(3)); else if ((*it).startsWith(QString::fromLatin1("bcc="))) mailtoMsg += i18n(" - BCC: ") + KURL::decode_string((*it).mid(4)); emit setStatusBarText(mailtoMsg); return; } else if (u.protocol() == QString::fromLatin1("http")) { DOM::Node hrefNode = nodeUnderMouse().parentNode(); while (hrefNode.nodeName().string() != QString::fromLatin1("A") && !hrefNode.isNull()) hrefNode = hrefNode.parentNode();/* // Is this check neccessary at all? (Frerich) if (!hrefNode.isNull()) { DOM::Node hreflangNode = hrefNode.attributes().getNamedItem("HREFLANG"); if (!hreflangNode.isNull()) { QString countryCode = hreflangNode.nodeValue().string().lower(); // Map the language code to an appropriate country code. if (countryCode == QString::fromLatin1("en")) countryCode = QString::fromLatin1("gb"); QString flagImg = QString::fromLatin1("<img src=%1>").arg( locate("locale", QString::fromLatin1("l10n/") + countryCode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -