1 /* This file is part of the KDE project
3 * Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
4 * 1999 Lars Knoll <knoll@kde.org>
5 * 1999 Antti Koivisto <koivisto@kde.org>
6 * 2000 Simon Hausmann <hausmann@kde.org>
7 * 2000 Stefan Schimanski <1Stein@gmx.de>
8 * 2001 George Staikos <staikos@kde.org>
9 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
10 * Copyright (C) 2005 Alexey Proskuryakov <ap@nypop.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public License
23 * along with this library; see the file COPYING.LIB. If not, write to
24 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
30 #include "FramePrivate.h"
32 #include "ApplyStyleCommand.h"
33 #include "CSSComputedStyleDeclaration.h"
34 #include "CSSProperty.h"
35 #include "CSSPropertyNames.h"
37 #include "CachedCSSStyleSheet.h"
38 #include "DOMImplementation.h"
39 #include "DOMWindow.h"
41 #include "DocLoader.h"
42 #include "DocumentType.h"
43 #include "EditingText.h"
45 #include "EventNames.h"
46 #include "FloatRect.h"
48 #include "GraphicsContext.h"
49 #include "HTMLFormElement.h"
50 #include "HTMLFrameElement.h"
51 #include "HTMLGenericFormElement.h"
52 #include "HTMLInputElement.h"
53 #include "HTMLNames.h"
54 #include "HTMLObjectElement.h"
55 #include "HTMLViewSourceDocument.h"
56 #include "ImageDocument.h"
57 #include "loader/icon/IconDatabase.h"
58 #include "loader/icon/IconLoader.h"
59 #include "MediaFeatureNames.h"
60 #include "MouseEventWithHitTestResults.h"
63 #include "PlatformScrollBar.h"
64 #include "PlugInInfoStore.h"
66 #include "PluginDocument.h"
67 #include "RenderListBox.h"
68 #include "RenderPart.h"
69 #include "RenderTextControl.h"
70 #include "RenderTheme.h"
71 #include "RenderView.h"
72 #include "SegmentedString.h"
73 #include "TextDocument.h"
74 #include "TextIterator.h"
75 #include "TypingCommand.h"
76 #include "XMLTokenizer.h"
77 #include "cssstyleselector.h"
78 #include "htmlediting.h"
79 #include "kjs_window.h"
81 #include "visible_units.h"
82 #include "xmlhttprequest.h"
84 #include <sys/types.h>
85 #include <wtf/Platform.h>
97 #include "XLinkNames.h"
99 #include "SVGDocument.h"
100 #include "SVGDocumentExtensions.h"
108 using KJS::PausedTimeouts;
109 using KJS::SavedProperties;
110 using KJS::SavedBuiltins;
116 using namespace EventNames;
117 using namespace HTMLNames;
119 const double caretBlinkFrequency = 0.5;
120 const double autoscrollInterval = 0.1;
122 class UserStyleSheetLoader : public CachedResourceClient {
124 UserStyleSheetLoader(Frame* frame, const String& url, DocLoader* dl)
126 , m_cachedSheet(Cache::requestCSSStyleSheet(dl, url, false, 0, ""))
128 m_cachedSheet->ref(this);
130 ~UserStyleSheetLoader()
132 m_cachedSheet->deref(this);
135 virtual void setCSSStyleSheet(const String& /*URL*/, const String& /*charset*/, const String& sheet)
137 m_frame->setUserStyleSheet(sheet);
140 CachedCSSStyleSheet* m_cachedSheet;
144 struct FrameCounter {
146 ~FrameCounter() { if (count != 0) fprintf(stderr, "LEAK: %d Frame\n", count); }
148 int FrameCounter::count = 0;
149 static FrameCounter frameCounter;
152 static inline Frame* parentFromOwnerElement(Element* ownerElement)
156 return ownerElement->document()->frame();
159 Frame::Frame(Page* page, Element* ownerElement)
160 : d(new FramePrivate(page, parentFromOwnerElement(ownerElement), this, ownerElement))
162 AtomicString::init();
166 QualifiedName::init();
167 MediaFeatureNames::init();
175 if (d->m_ownerElement)
176 d->m_page->incrementFrameCount();
178 // FIXME: Frames were originally created with a refcount of 1, leave this
179 // ref call here until we can straighten that out.
182 ++FrameCounter::count;
188 // FIXME: We should not be doing all this work inside the destructor
190 ASSERT(!d->m_lifeSupportTimer.isActive());
193 --FrameCounter::count;
196 if (d->m_jscript && d->m_jscript->haveInterpreter())
197 if (Window* w = Window::retrieveWindow(this)) {
198 w->disconnectFrame();
199 // Must clear the window pointer, otherwise we will not
200 // garbage-collect collect the window (inside the call to
205 disconnectOwnerElement();
208 d->m_domWindow->disconnectFrame();
211 HashSet<Frame*> openedBy = d->m_openedFrames;
212 HashSet<Frame*>::iterator end = openedBy.end();
213 for (HashSet<Frame*>::iterator it = openedBy.begin(); it != end; ++it)
218 d->m_view->m_frame = 0;
221 ASSERT(!d->m_lifeSupportTimer.isActive());
223 delete d->m_userStyleSheetLoader;
228 KURL Frame::iconURL()
230 // If this isn't a top level frame, return nothing
231 if (tree() && tree()->parent())
234 // If we have an iconURL from a Link element, return that
235 if (document() && !document()->iconURL().isEmpty())
236 return KURL(document()->iconURL().deprecatedString());
238 // Don't return a favicon iconURL unless we're http or https
239 if (d->m_url.protocol() != "http" && d->m_url.protocol() != "https")
243 url.setProtocol(d->m_url.protocol());
244 url.setHost(d->m_url.host());
245 url.setPath("/favicon.ico");
249 bool Frame::didOpenURL(const KURL& url)
251 if (d->m_scheduledRedirection == locationChangeScheduledDuringLoad) {
252 // A redirect was shceduled before the document was created. This can happen
253 // when one frame changes another frame's location.
259 // clear last edit command
260 d->m_lastEditCommand = 0;
264 d->m_bComplete = false;
265 d->m_bLoadingMainResource = true;
266 d->m_bLoadEventEmitted = false;
268 d->m_kjsStatusBarText = String();
269 d->m_kjsDefaultStatusBarText = String();
271 d->m_bJScriptEnabled = d->m_settings->isJavaScriptEnabled();
272 d->m_bJavaEnabled = d->m_settings->isJavaEnabled();
273 d->m_bPluginsEnabled = d->m_settings->isPluginsEnabled();
275 // initializing d->m_url to the new url breaks relative links when opening such a link after this call and _before_ begin() is called (when the first
276 // data arrives) (Simon)
278 if (d->m_url.protocol().startsWith("http") && !d->m_url.host().isEmpty() && d->m_url.path().isEmpty())
279 d->m_url.setPath("/");
280 d->m_workingURL = d->m_url;
287 void Frame::didExplicitOpen()
289 d->m_bComplete = false;
290 d->m_bLoadEventEmitted = false;
292 // Prevent window.open(url) -- eg window.open("about:blank") -- from blowing away results
293 // from a subsequent window.document.open / window.document.write call.
294 // Cancelling redirection here works for all cases because document.open
295 // implicitly precedes document.write.
297 d->m_url = d->m_doc->URL();
300 void Frame::stopLoading(bool sendUnload)
302 if (d->m_doc && d->m_doc->tokenizer())
303 d->m_doc->tokenizer()->stopParsing();
305 d->m_metaData.clear();
309 if (d->m_bLoadEventEmitted && !d->m_bUnloadEventEmitted) {
310 Node* currentFocusNode = d->m_doc->focusNode();
311 if (currentFocusNode)
312 currentFocusNode->aboutToUnload();
313 d->m_doc->dispatchWindowEvent(unloadEvent, false, false);
315 d->m_doc->updateRendering();
316 d->m_bUnloadEventEmitted = true;
320 if (d->m_doc && !d->m_doc->inPageCache())
321 d->m_doc->removeAllEventListenersFromAllNodes();
324 d->m_bComplete = true; // to avoid calling completed() in finishedParsing() (David)
325 d->m_bLoadingMainResource = false;
326 d->m_bLoadEventEmitted = true; // don't want that one either
327 d->m_cachePolicy = CachePolicyVerify; // Why here?
329 if (d->m_doc && d->m_doc->parsing()) {
331 d->m_doc->setParsing(false);
334 d->m_workingURL = KURL();
336 if (Document *doc = d->m_doc.get()) {
337 if (DocLoader *docLoader = doc->docLoader())
338 Cache::loader()->cancelRequests(docLoader);
339 XMLHttpRequest::cancelRequests(doc);
342 // tell all subframes to stop as well
343 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
344 child->stopLoading(sendUnload);
346 d->m_bPendingChildRedirection = false;
351 BrowserExtension *Frame::browserExtension() const
353 return d->m_extension;
356 FrameView* Frame::view() const
358 return d->m_view.get();
361 void Frame::setView(FrameView* view)
363 // Detach the document now, so any onUnload handlers get run - if
364 // we wait until the view is destroyed, then things won't be
365 // hooked up enough for some JavaScript calls to work.
366 if (d->m_doc && view == 0)
372 bool Frame::jScriptEnabled() const
374 return d->m_bJScriptEnabled;
377 KJSProxy *Frame::jScript()
379 if (!d->m_bJScriptEnabled)
383 d->m_jscript = new KJSProxy(this);
388 static bool getString(JSValue* result, String& string)
394 if (!result->getString(ustring))
400 void Frame::replaceContentsWithScriptResult(const KURL& url)
402 JSValue* ret = executeScript(0, KURL::decode_string(url.url().mid(strlen("javascript:"))));
404 if (getString(ret, scriptResult)) {
411 JSValue* Frame::executeScript(Node* n, const String& script, bool forceUserGesture)
413 KJSProxy *proxy = jScript();
418 d->m_runningScripts++;
419 // If forceUserGesture is true, then make the script interpreter
420 // treat it as if triggered by a user gesture even if there is no
421 // current DOM event being processed.
422 JSValue* ret = proxy->evaluate(forceUserGesture ? DeprecatedString::null : d->m_url.url(), 0, script, n);
423 d->m_runningScripts--;
425 if (!d->m_runningScripts)
428 Document::updateDocumentsRendering();
433 bool Frame::javaEnabled() const
435 return d->m_settings->isJavaEnabled();
438 bool Frame::pluginsEnabled() const
440 return d->m_bPluginsEnabled;
443 void Frame::setAutoloadImages(bool enable)
445 if (d->m_doc && d->m_doc->docLoader()->autoloadImages() == enable)
449 d->m_doc->docLoader()->setAutoloadImages(enable);
452 bool Frame::autoloadImages() const
455 return d->m_doc->docLoader()->autoloadImages();
460 void Frame::cancelAndClear()
470 void Frame::clear(bool clearWindowProperties)
474 d->m_bCleared = true;
475 d->m_mousePressNode = 0;
478 // FIXME: This is a temporary hack to work around a mismatch between WebCore and WebKit
479 // regarding frame lifetime. The proper solution is to move all frame management
480 // into WebCore, so frames can work the same way on all platforms.
485 d->m_doc->cancelParsing();
489 // Moving past doc so that onUnload works.
490 if (clearWindowProperties && d->m_jscript)
491 d->m_jscript->clear();
496 // do not drop the document before the jscript and view are cleared, as some destructors
497 // might still try to access the document.
501 d->m_plugins.clear();
502 cleanupPluginObjects();
504 d->m_scheduledRedirection = noRedirectionScheduled;
505 d->m_delayRedirect = 0;
506 d->m_redirectURL = DeprecatedString::null;
507 d->m_redirectReferrer = String();
508 d->m_redirectLockHistory = true;
509 d->m_redirectUserGesture = false;
510 d->m_bHTTPRefresh = false;
511 d->m_bFirstData = true;
513 d->m_bMousePressed = false;
515 if (!d->m_haveEncoding)
516 d->m_encoding = String();
519 Document *Frame::document() const
522 return d->m_doc.get();
526 void Frame::setDocument(Document* newDoc)
537 void Frame::receivedFirstData()
539 begin(d->m_workingURL);
541 d->m_doc->docLoader()->setCachePolicy(d->m_cachePolicy);
542 d->m_workingURL = KURL();
544 // When the first data arrives, the metadata has just been made available
545 DeprecatedString qData;
547 // Support for http-refresh
548 qData = d->m_metaData.get("http-refresh").deprecatedString();
549 if (!qData.isEmpty()) {
551 int pos = qData.find(';');
553 pos = qData.find(',');
556 delay = qData.stripWhiteSpace().toDouble();
557 // We want a new history item if the refresh timeout > 1 second
558 scheduleRedirection(delay, d->m_url.url(), delay <= 1);
560 int end_pos = qData.length();
561 delay = qData.left(pos).stripWhiteSpace().toDouble();
562 while (qData[++pos] == ' ');
563 if (qData.find("url", pos, false) == pos) {
565 while (qData[pos] == ' ' || qData[pos] == '=')
567 if (qData[pos] == '"') {
569 int index = end_pos-1;
570 while (index > pos) {
571 if (qData[index] == '"')
579 // We want a new history item if the refresh timeout > 1 second
580 scheduleRedirection(delay, d->m_doc->completeURL(qData.mid(pos, end_pos)), delay <= 1);
582 d->m_bHTTPRefresh = true;
585 // Support for http last-modified
586 d->m_lastModified = d->m_metaData.get("modified");
589 void Frame::childBegin()
591 // We need to do this when the child is created so as to avoid the parent thining the child
592 // is complete before it has even started loading.
593 // FIXME: do we really still need this?
594 d->m_bComplete = false;
597 void Frame::setResourceRequest(const ResourceRequest& request)
599 d->m_request = request;
602 const ResourceRequest& Frame::resourceRequest() const
607 void Frame::setResponseMIMEType(const String& contentType)
609 d->m_responseMIMEType = contentType;
612 const String& Frame::responseMIMEType() const
614 return d->m_responseMIMEType;
617 void Frame::begin(const KURL& url)
619 if (d->m_workingURL.isEmpty())
620 createEmptyDocument(); // Creates an empty document if we don't have one already
623 partClearedInBegin();
625 d->m_bCleared = false;
626 d->m_bComplete = false;
627 d->m_bLoadEventEmitted = false;
628 d->m_bLoadingMainResource = true;
631 ref.setUser(DeprecatedString());
632 ref.setPass(DeprecatedString());
633 ref.setRef(DeprecatedString());
634 d->m_referrer = ref.url();
638 // We don't need KDE chained URI handling or window caption setting
639 if (!d->m_url.isEmpty())
643 if (d->m_responseMIMEType == "image/svg+xml")
644 d->m_doc = DOMImplementation::instance()->createSVGDocument(d->m_view.get());
647 if (DOMImplementation::isXMLMIMEType(d->m_responseMIMEType))
648 d->m_doc = DOMImplementation::instance()->createDocument(d->m_view.get());
649 else if (DOMImplementation::isTextMIMEType(d->m_responseMIMEType))
650 d->m_doc = new TextDocument(DOMImplementation::instance(), d->m_view.get());
651 else if ((d->m_responseMIMEType == "application/pdf" || d->m_responseMIMEType == "text/pdf") && PlugInInfoStore::supportsMIMEType(d->m_responseMIMEType))
652 d->m_doc = new PluginDocument(DOMImplementation::instance(), d->m_view.get());
653 else if (Image::supportsType(d->m_responseMIMEType))
654 d->m_doc = new ImageDocument(DOMImplementation::instance(), d->m_view.get());
655 else if (PlugInInfoStore::supportsMIMEType(d->m_responseMIMEType))
656 d->m_doc = new PluginDocument(DOMImplementation::instance(), d->m_view.get());
657 else if (inViewSourceMode())
658 d->m_doc = new HTMLViewSourceDocument(DOMImplementation::instance(), d->m_view.get());
660 d->m_doc = DOMImplementation::instance()->createHTMLDocument(d->m_view.get());
662 if (!d->m_doc->attached())
664 d->m_doc->setURL(d->m_url.url());
665 // We prefer m_baseURL over d->m_url because d->m_url changes when we are
666 // about to load a new page.
667 d->m_doc->setBaseURL(baseurl.url());
669 d->m_doc->setDecoder(d->m_decoder.get());
671 updatePolicyBaseURL();
673 setAutoloadImages(d->m_settings->autoLoadImages());
674 const KURL& userStyleSheet = d->m_settings->userStyleSheetLocation();
676 if (!userStyleSheet.isEmpty())
677 setUserStyleSheetLocation(KURL(userStyleSheet));
679 restoreDocumentState();
681 d->m_doc->implicitOpen();
684 d->m_view->resizeContents(0, 0);
687 void Frame::write(const char* str, int len)
695 if (Tokenizer* t = d->m_doc->tokenizer()) {
696 if (t->wantsRawData()) {
697 t->writeRawData(str, len);
703 d->m_decoder = new Decoder(d->m_responseMIMEType, settings()->encoding());
704 if (!d->m_encoding.isNull())
705 d->m_decoder->setEncoding(d->m_encoding,
706 d->m_haveEncoding ? Decoder::UserChosenEncoding : Decoder::EncodingFromHTTPHeader);
708 d->m_doc->setDecoder(d->m_decoder.get());
711 String decoded = d->m_decoder->decode(str, len);
713 if (decoded.isEmpty())
716 if (d->m_bFirstData) {
717 d->m_bFirstData = false;
718 d->m_doc->determineParseMode(decoded);
719 if (d->m_decoder->encoding().usesVisualOrdering())
720 d->m_doc->setVisuallyOrdered();
721 d->m_doc->recalcStyle(Node::Force);
724 if (Tokenizer* t = d->m_doc->tokenizer()) {
725 ASSERT(!t->wantsRawData());
726 t->write(decoded, true);
730 void Frame::write(const String& str)
735 if (d->m_bFirstData) {
736 // determine the parse mode
737 d->m_doc->setParseMode(Document::Strict);
738 d->m_bFirstData = false;
740 Tokenizer* t = d->m_doc->tokenizer();
747 d->m_bLoadingMainResource = false;
751 void Frame::endIfNotLoading()
753 // http://bugzilla.opendarwin.org/show_bug.cgi?id=10854
754 // The frame's last ref may be remove and it be deleted by checkCompleted(),
755 // so we'll add a protective refcount
756 RefPtr<Frame> protector(this);
758 if (d->m_bLoadingMainResource)
761 // make sure nothing's left in there...
764 String decoded = d->m_decoder->flush();
765 if (d->m_bFirstData) {
766 d->m_bFirstData = false;
767 d->m_doc->determineParseMode(decoded);
771 d->m_doc->finishParsing();
773 // WebKit partially uses WebCore when loading non-HTML docs. In these cases doc==nil, but
774 // WebCore is enough involved that we need to checkCompleted() in order for m_bComplete to
775 // become true. An example is when a subframe is a pure text doc, and that subframe is the
776 // last one to complete.
779 // FIXME - Right now, we kick off the icon loader when the frame is done receiving all its main resource.
780 // We could kick off the icon loader after we're done parsing the HEAD element, if that becomes convinient to find
783 // Don't load an icon if -
784 // 1) This is not the main frame
785 // 2) The database is disabled
786 // 3) We have no valid icon URL
787 // 4) We already have an unexpired icon
789 if (tree()->parent())
793 if (IconDatabase* sharedIconDatabase = IconDatabase::sharedIconDatabase()) {
794 if (!sharedIconDatabase->enabled())
797 String url(iconURL().url());
801 // If we already have an unexpired icon, we won't kick off a load but we *will* map the appropriate URLs to it
802 if (sharedIconDatabase->hasEntryForIconURL(url) && !isLoadTypeReload() && !sharedIconDatabase->isIconExpiredForIconURL(url)) {
803 commitIconURLToIconDatabase();
807 if (!d->m_iconLoader)
808 d->m_iconLoader = IconLoader::createForFrame(this);
809 d->m_iconLoader->startLoading();
813 void Frame::commitIconURLToIconDatabase()
815 KURL icon = iconURL();
817 IconDatabase* iconDatabase = IconDatabase::sharedIconDatabase();
818 ASSERT(iconDatabase);
819 iconDatabase->setIconURLForPageURL(icon.url(), this->url().url());
820 iconDatabase->setIconURLForPageURL(icon.url(), originalRequestURL().url());
825 // http://bugzilla.opendarwin.org/show_bug.cgi?id=10854
826 // The frame's last ref may be remove and it be deleted by checkCompleted(),
827 // so we'll add a protective refcount
828 RefPtr<Frame> protector(this);
831 if (d->m_doc->tokenizer())
832 d->m_doc->tokenizer()->stopParsing();
833 d->m_doc->finishParsing();
835 // WebKit partially uses WebCore when loading non-HTML docs. In these cases doc==nil, but
836 // WebCore is enough involved that we need to checkCompleted() in order for m_bComplete to
837 // become true. An example is when a subframe is a pure text doc, and that subframe is the
838 // last one to complete.
841 d->m_iconLoader->stopLoading();
844 void Frame::gotoAnchor()
846 // If our URL has no ref, then we have no place we need to jump to.
847 if (!d->m_url.hasRef())
850 DeprecatedString ref = d->m_url.encodedHtmlRef();
851 if (!gotoAnchor(ref)) {
852 // Can't use htmlRef() here because it doesn't know which encoding to use to decode.
853 // Decoding here has to match encoding in completeURL, which means it has to use the
854 // page's encoding rather than UTF-8.
856 gotoAnchor(KURL::decode_string(ref, d->m_decoder->encoding()));
860 void Frame::finishedParsing()
862 // This method can be called from our destructor, in which case we shouldn't protect ourselves
863 // because doing so will cause us to re-enter our destructor when protector goes out of scope.
864 RefPtr<Frame> protector = refCount() > 0 ? this : 0;
868 return; // We are being destroyed by something checkCompleted called.
870 // check if the scrollbars are really needed for the content
871 // if not, remove them, relayout, and repaint
873 d->m_view->restoreScrollbar();
877 void Frame::loadDone()
883 void Frame::checkCompleted()
885 // Any frame that hasn't completed yet ?
886 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
887 if (!child->d->m_bComplete)
890 // Have we completed before?
894 // Are we still parsing?
895 if (d->m_doc && d->m_doc->parsing())
898 // Still waiting for images/scripts from the loader ?
900 if (d->m_doc && d->m_doc->docLoader())
901 requests = Cache::loader()->numRequests(d->m_doc->docLoader());
907 // Now do what should be done when we are really completed.
908 d->m_bComplete = true;
910 checkEmitLoadEvent(); // if we didn't do it before
912 if (d->m_scheduledRedirection != noRedirectionScheduled) {
913 // Do not start redirection for frames here! That action is
914 // deferred until the parent emits a completed signal.
915 if (!tree()->parent())
916 startRedirectionTimer();
920 completed(d->m_bPendingChildRedirection);
924 void Frame::checkEmitLoadEvent()
926 if (d->m_bLoadEventEmitted || !d->m_doc || d->m_doc->parsing())
929 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
930 if (!child->d->m_bComplete) // still got a frame running -> too early
933 // All frames completed -> set their domain to the frameset's domain
934 // This must only be done when loading the frameset initially (#22039),
935 // not when following a link in a frame (#44162).
937 String domain = d->m_doc->domain();
938 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
940 child->d->m_doc->setDomain(domain);
943 d->m_bLoadEventEmitted = true;
944 d->m_bUnloadEventEmitted = false;
946 d->m_doc->implicitClose();
949 const Settings *Frame::settings() const
951 return d->m_settings;
954 KURL Frame::baseURL() const
958 return d->m_doc->baseURL();
961 String Frame::baseTarget() const
965 return d->m_doc->baseTarget();
968 KURL Frame::completeURL(const DeprecatedString& url)
973 return KURL(d->m_doc->completeURL(url));
976 void Frame::scheduleRedirection(double delay, const DeprecatedString& url, bool doLockHistory)
978 if (delay < 0 || delay > INT_MAX / 1000)
980 if (d->m_scheduledRedirection == noRedirectionScheduled || delay <= d->m_delayRedirect)
982 d->m_scheduledRedirection = redirectionScheduled;
983 d->m_delayRedirect = delay;
984 d->m_redirectURL = url;
985 d->m_redirectReferrer = String();
986 d->m_redirectLockHistory = doLockHistory;
987 d->m_redirectUserGesture = false;
989 stopRedirectionTimer();
991 startRedirectionTimer();
995 void Frame::scheduleLocationChange(const DeprecatedString& url, const String& referrer, bool lockHistory, bool userGesture)
999 // If the URL we're going to navigate to is the same as the current one, except for the
1000 // fragment part, we don't need to schedule the location change.
1001 if (u.hasRef() && equalIgnoringRef(d->m_url, u)) {
1002 changeLocation(url, referrer, lockHistory, userGesture);
1006 // Handle a location change of a page with no document as a special case.
1007 // This may happen when a frame changes the location of another frame.
1008 d->m_scheduledRedirection = d->m_doc ? locationChangeScheduled : locationChangeScheduledDuringLoad;
1010 // If a redirect was scheduled during a load, then stop the current load.
1011 // Otherwise when the current load transitions from a provisional to a
1012 // committed state, pending redirects may be cancelled.
1013 if (d->m_scheduledRedirection == locationChangeScheduledDuringLoad) {
1017 d->m_delayRedirect = 0;
1018 d->m_redirectURL = url;
1019 d->m_redirectReferrer = referrer;
1020 d->m_redirectLockHistory = lockHistory;
1021 d->m_redirectUserGesture = userGesture;
1022 stopRedirectionTimer();
1024 startRedirectionTimer();
1027 void Frame::scheduleRefresh(bool userGesture)
1029 // Handle a location change of a page with no document as a special case.
1030 // This may happen when a frame requests a refresh of another frame.
1031 d->m_scheduledRedirection = d->m_doc ? locationChangeScheduled : locationChangeScheduledDuringLoad;
1033 // If a refresh was scheduled during a load, then stop the current load.
1034 // Otherwise when the current load transitions from a provisional to a
1035 // committed state, pending redirects may be cancelled.
1036 if (d->m_scheduledRedirection == locationChangeScheduledDuringLoad)
1039 d->m_delayRedirect = 0;
1040 d->m_redirectURL = url().url();
1041 d->m_redirectReferrer = referrer();
1042 d->m_redirectLockHistory = true;
1043 d->m_redirectUserGesture = userGesture;
1044 d->m_cachePolicy = CachePolicyRefresh;
1045 stopRedirectionTimer();
1047 startRedirectionTimer();
1050 bool Frame::isScheduledLocationChangePending() const
1052 switch (d->m_scheduledRedirection) {
1053 case noRedirectionScheduled:
1054 case redirectionScheduled:
1056 case historyNavigationScheduled:
1057 case locationChangeScheduled:
1058 case locationChangeScheduledDuringLoad:
1064 void Frame::scheduleHistoryNavigation(int steps)
1066 // navigation will always be allowed in the 0 steps case, which is OK because
1067 // that's supposed to force a reload.
1068 if (!canGoBackOrForward(steps)) {
1069 cancelRedirection();
1073 // If the URL we're going to navigate to is the same as the current one, except for the
1074 // fragment part, we don't need to schedule the navigation.
1075 if (d->m_extension) {
1076 KURL u = d->m_extension->historyURL(steps);
1078 if (equalIgnoringRef(d->m_url, u)) {
1079 d->m_extension->goBackOrForward(steps);
1084 d->m_scheduledRedirection = historyNavigationScheduled;
1085 d->m_delayRedirect = 0;
1086 d->m_redirectURL = DeprecatedString::null;
1087 d->m_redirectReferrer = String();
1088 d->m_scheduledHistoryNavigationSteps = steps;
1089 stopRedirectionTimer();
1091 startRedirectionTimer();
1094 void Frame::cancelRedirection(bool cancelWithLoadInProgress)
1097 d->m_cancelWithLoadInProgress = cancelWithLoadInProgress;
1098 d->m_scheduledRedirection = noRedirectionScheduled;
1099 stopRedirectionTimer();
1103 void Frame::changeLocation(const DeprecatedString& URL, const String& referrer, bool lockHistory, bool userGesture)
1105 if (URL.find("javascript:", 0, false) == 0) {
1106 String script = KURL::decode_string(URL.mid(11));
1107 JSValue* result = executeScript(0, script, userGesture);
1108 String scriptResult;
1109 if (getString(result, scriptResult)) {
1111 write(scriptResult);
1117 ResourceRequest request(completeURL(URL));
1118 request.setLockHistory(lockHistory);
1119 if (!referrer.isEmpty())
1120 request.setReferrer(referrer);
1122 request.reload = (d->m_cachePolicy == CachePolicyReload) || (d->m_cachePolicy == CachePolicyRefresh);
1124 urlSelected(request, "_self");
1127 void Frame::redirectionTimerFired(Timer<Frame>*)
1129 if (d->m_scheduledRedirection == historyNavigationScheduled) {
1130 d->m_scheduledRedirection = noRedirectionScheduled;
1132 // Special case for go(0) from a frame -> reload only the frame
1133 // go(i!=0) from a frame navigates into the history of the frame only,
1134 // in both IE and NS (but not in Mozilla).... we can't easily do that
1136 if (d->m_scheduledHistoryNavigationSteps == 0) // add && parent() to get only frames, but doesn't matter
1137 openURL(url()); /// ## need args.reload=true?
1139 if (d->m_extension) {
1140 d->m_extension->goBackOrForward(d->m_scheduledHistoryNavigationSteps);
1146 DeprecatedString URL = d->m_redirectURL;
1147 String referrer = d->m_redirectReferrer;
1148 bool lockHistory = d->m_redirectLockHistory;
1149 bool userGesture = d->m_redirectUserGesture;
1151 d->m_scheduledRedirection = noRedirectionScheduled;
1152 d->m_delayRedirect = 0;
1153 d->m_redirectURL = DeprecatedString::null;
1154 d->m_redirectReferrer = String();
1156 changeLocation(URL, referrer, lockHistory, userGesture);
1159 String Frame::encoding() const
1161 if (d->m_haveEncoding && !d->m_encoding.isEmpty())
1162 return d->m_encoding;
1163 if (d->m_decoder && d->m_decoder->encoding().isValid())
1164 return d->m_decoder->encoding().name();
1165 return settings()->encoding();
1168 void Frame::setUserStyleSheetLocation(const KURL& url)
1170 delete d->m_userStyleSheetLoader;
1171 d->m_userStyleSheetLoader = 0;
1172 if (d->m_doc && d->m_doc->docLoader())
1173 d->m_userStyleSheetLoader = new UserStyleSheetLoader(this, url.url(), d->m_doc->docLoader());
1176 void Frame::setUserStyleSheet(const String& styleSheet)
1178 delete d->m_userStyleSheetLoader;
1179 d->m_userStyleSheetLoader = 0;
1181 d->m_doc->setUserStyleSheet(styleSheet);
1184 bool Frame::gotoAnchor(const String& name)
1189 Node *n = d->m_doc->getElementById(AtomicString(name));
1191 n = d->m_doc->anchors()->namedItem(name, !d->m_doc->inCompatMode());
1193 d->m_doc->setCSSTarget(n); // Setting to null will clear the current target.
1195 // Implement the rule that "" and "top" both mean top of page as in other browsers.
1196 if (!n && !(name.isEmpty() || name.lower() == "top"))
1199 // We need to update the layout before scrolling, otherwise we could
1200 // really mess things up if an anchor scroll comes at a bad moment.
1202 d->m_doc->updateRendering();
1203 // Only do a layout if changes have occurred that make it necessary.
1204 if (d->m_view && d->m_doc->renderer() && d->m_doc->renderer()->needsLayout()) {
1205 d->m_view->layout();
1209 // Scroll nested layers and frames to reveal the anchor.
1210 RenderObject *renderer;
1213 renderer = n->renderer();
1214 rect = n->getRect();
1216 // If there's no node, we should scroll to the top of the document.
1217 renderer = d->m_doc->renderer();
1222 // Align to the top and to the closest side (this matches other browsers).
1223 renderer->enclosingLayer()->scrollRectToVisible(rect, RenderLayer::gAlignToEdgeIfNeeded, RenderLayer::gAlignTopAlways);
1229 void Frame::setStandardFont(const String& name)
1231 d->m_settings->setStdFontName(AtomicString(name));
1234 void Frame::setFixedFont(const String& name)
1236 d->m_settings->setFixedFontName(AtomicString(name));
1239 String Frame::selectedText() const
1241 return plainText(selectionController()->toRange().get());
1244 bool Frame::hasSelection() const
1246 return selectionController()->isCaretOrRange();
1249 SelectionController* Frame::selectionController() const
1251 return &(d->m_selectionController);
1254 CommandByName* Frame::command() const
1256 return &(d->m_command);
1259 TextGranularity Frame::selectionGranularity() const
1261 return d->m_selectionGranularity;
1264 void Frame::setSelectionGranularity(TextGranularity granularity) const
1266 d->m_selectionGranularity = granularity;
1269 SelectionController* Frame::dragCaretController() const
1271 return d->m_page->dragCaretController();
1274 const Selection& Frame::mark() const
1279 void Frame::setMark(const Selection& s)
1281 ASSERT(!s.base().node() || s.base().node()->document() == document());
1282 ASSERT(!s.extent().node() || s.extent().node()->document() == document());
1283 ASSERT(!s.start().node() || s.start().node()->document() == document());
1284 ASSERT(!s.end().node() || s.end().node()->document() == document());
1289 void Frame::notifyRendererOfSelectionChange(bool userTriggered)
1291 RenderObject* renderer = 0;
1292 if (selectionController()->rootEditableElement())
1293 renderer = selectionController()->rootEditableElement()->shadowAncestorNode()->renderer();
1295 // If the current selection is in a textfield or textarea, notify the renderer that the selection has changed
1296 if (renderer && (renderer->isTextArea() || renderer->isTextField()))
1297 static_cast<RenderTextControl*>(renderer)->selectionChanged(userTriggered);
1300 void Frame::invalidateSelection()
1302 selectionController()->setNeedsLayout();
1303 selectionLayoutChanged();
1306 void Frame::setCaretVisible(bool flag)
1308 if (d->m_caretVisible == flag)
1310 clearCaretRectIfNeeded();
1312 setFocusNodeIfNeeded();
1313 d->m_caretVisible = flag;
1314 selectionLayoutChanged();
1318 void Frame::clearCaretRectIfNeeded()
1320 if (d->m_caretPaint) {
1321 d->m_caretPaint = false;
1322 selectionController()->invalidateCaretRect();
1326 // Helper function that tells whether a particular node is an element that has an entire
1327 // Frame and FrameView, a <frame>, <iframe>, or <object>.
1328 static bool isFrameElement(const Node *n)
1332 RenderObject *renderer = n->renderer();
1333 if (!renderer || !renderer->isWidget())
1335 Widget* widget = static_cast<RenderWidget*>(renderer)->widget();
1336 return widget && widget->isFrameView();
1339 void Frame::setFocusNodeIfNeeded()
1341 if (!document() || selectionController()->isNone() || !d->m_isActive)
1344 Node* target = selectionController()->rootEditableElement();
1346 RenderObject* renderer = target->renderer();
1348 // Walk up the render tree to search for a node to focus.
1349 // Walking up the DOM tree wouldn't work for shadow trees, like those behind the engine-based text fields.
1351 // We don't want to set focus on a subframe when selecting in a parent frame,
1352 // so add the !isFrameElement check here. There's probably a better way to make this
1353 // work in the long term, but this is the safest fix at this time.
1354 if (target && target->isMouseFocusable() && !isFrameElement(target)) {
1355 document()->setFocusNode(target);
1358 renderer = renderer->parent();
1360 target = renderer->element();
1362 document()->setFocusNode(0);
1366 void Frame::selectionLayoutChanged()
1368 bool caretRectChanged = selectionController()->recomputeCaretRect();
1370 bool shouldBlink = d->m_caretVisible
1371 && selectionController()->isCaret() && selectionController()->isContentEditable();
1373 // If the caret moved, stop the blink timer so we can restart with a
1374 // black caret in the new location.
1375 if (caretRectChanged || !shouldBlink)
1376 d->m_caretBlinkTimer.stop();
1378 // Start blinking with a black caret. Be sure not to restart if we're
1379 // already blinking in the right location.
1380 if (shouldBlink && !d->m_caretBlinkTimer.isActive()) {
1381 d->m_caretBlinkTimer.startRepeating(caretBlinkFrequency);
1382 d->m_caretPaint = true;
1386 d->m_doc->updateSelection();
1389 void Frame::setXPosForVerticalArrowNavigation(int x)
1391 d->m_xPosForVerticalArrowNavigation = x;
1394 int Frame::xPosForVerticalArrowNavigation() const
1396 return d->m_xPosForVerticalArrowNavigation;
1399 void Frame::caretBlinkTimerFired(Timer<Frame>*)
1401 ASSERT(d->m_caretVisible);
1402 ASSERT(selectionController()->isCaret());
1403 bool caretPaint = d->m_caretPaint;
1404 if (d->m_bMousePressed && caretPaint)
1406 d->m_caretPaint = !caretPaint;
1407 selectionController()->invalidateCaretRect();
1410 void Frame::paintCaret(GraphicsContext* p, const IntRect& rect) const
1412 if (d->m_caretPaint)
1413 selectionController()->paintCaret(p, rect);
1416 void Frame::paintDragCaret(GraphicsContext* p, const IntRect& rect) const
1418 SelectionController* dragCaretController = d->m_page->dragCaretController();
1419 assert(dragCaretController->selection().isCaret());
1420 if (dragCaretController->selection().start().node()->document()->frame() == this)
1421 dragCaretController->paintCaret(p, rect);
1424 void Frame::urlSelected(const DeprecatedString& url, const String& target)
1426 urlSelected(ResourceRequest(completeURL(url)), target);
1429 void Frame::urlSelected(const ResourceRequest& request, const String& _target)
1431 String target = _target;
1432 if (target.isEmpty() && d->m_doc)
1433 target = d->m_doc->baseTarget();
1435 const KURL& url = request.url();
1437 if (url.url().startsWith("javascript:", false)) {
1438 executeScript(0, KURL::decode_string(url.url().mid(11)), true);
1443 // ### ERROR HANDLING
1446 ResourceRequest requestCopy = request;
1447 requestCopy.frameName = target;
1449 if (d->m_bHTTPRefresh)
1450 d->m_bHTTPRefresh = false;
1452 if (!d->m_referrer.isEmpty())
1453 requestCopy.setReferrer(d->m_referrer);
1455 urlSelected(requestCopy);
1458 bool Frame::requestFrame(Element* ownerElement, const String& urlParam, const AtomicString& frameName)
1460 DeprecatedString _url = urlParam.deprecatedString();
1461 // Support for <frame src="javascript:string">
1464 if (_url.startsWith("javascript:", false)) {
1466 url = "about:blank";
1468 url = completeURL(_url);
1470 Frame* frame = tree()->child(frameName);
1472 ResourceRequest request(url);
1473 request.setReferrer(d->m_referrer);
1474 request.reload = (d->m_cachePolicy == CachePolicyReload) || (d->m_cachePolicy == CachePolicyRefresh);
1475 frame->openURLRequest(request);
1477 frame = loadSubframe(ownerElement, url, frameName, d->m_referrer);
1482 if (!scriptURL.isEmpty())
1483 frame->replaceContentsWithScriptResult(scriptURL);
1488 bool Frame::requestObject(RenderPart* renderer, const String& url, const AtomicString& frameName,
1489 const String& mimeType, const Vector<String>& paramNames, const Vector<String>& paramValues)
1493 completedURL = completeURL(url.deprecatedString());
1495 if (url.isEmpty() && mimeType.isEmpty())
1499 if (shouldUsePlugin(renderer->element(), completedURL, mimeType, renderer->hasFallbackContent(), useFallback))
1500 return loadPlugin(renderer, completedURL, mimeType, paramNames, paramValues, useFallback);
1502 ASSERT(renderer->node()->hasTagName(objectTag) || renderer->node()->hasTagName(embedTag));
1503 AtomicString uniqueFrameName = tree()->uniqueChildName(frameName);
1504 static_cast<HTMLPlugInElement*>(renderer->node())->setFrameName(uniqueFrameName);
1506 // FIXME: ok to always make a new one? when does the old frame get removed?
1507 return loadSubframe(static_cast<Element*>(renderer->node()), completedURL, uniqueFrameName, d->m_referrer);
1510 bool Frame::shouldUsePlugin(Node* element, const KURL& url, const String& mimeType, bool hasFallback, bool& useFallback)
1512 useFallback = false;
1513 ObjectContentType objectType = objectContentType(url, mimeType);
1515 // if an object's content can't be handled and it has no fallback, let
1516 // it be handled as a plugin to show the broken plugin icon
1517 if (objectType == ObjectContentNone && hasFallback)
1520 return objectType == ObjectContentNone || objectType == ObjectContentPlugin;
1524 bool Frame::loadPlugin(RenderPart *renderer, const KURL& url, const String& mimeType,
1525 const Vector<String>& paramNames, const Vector<String>& paramValues, bool useFallback)
1528 checkEmitLoadEvent();
1532 Element *pluginElement;
1533 if (renderer && renderer->node() && renderer->node()->isElementNode())
1534 pluginElement = static_cast<Element*>(renderer->node());
1538 Plugin* plugin = createPlugin(pluginElement, url, paramNames, paramValues, mimeType);
1540 checkEmitLoadEvent();
1543 d->m_plugins.append(plugin);
1545 if (renderer && plugin->view())
1546 renderer->setWidget(plugin->view());
1548 checkEmitLoadEvent();
1553 Frame* Frame::loadSubframe(Element* ownerElement, const KURL& url, const String& name, const String& referrer)
1555 Frame* frame = createFrame(url, name, ownerElement, referrer);
1557 checkEmitLoadEvent();
1561 frame->childBegin();
1563 if (ownerElement->renderer() && frame->view())
1564 static_cast<RenderWidget*>(ownerElement->renderer())->setWidget(frame->view());
1566 checkEmitLoadEvent();
1568 // In these cases, the synchronous load would have finished
1569 // before we could connect the signals, so make sure to send the
1570 // completed() signal for the child by hand
1571 // FIXME: In this case the Frame will have finished loading before
1572 // it's being added to the child list. It would be a good idea to
1573 // create the child first, then invoke the loader separately.
1574 if (url.isEmpty() || url == "about:blank") {
1575 frame->completed(false);
1576 frame->checkCompleted();
1582 void Frame::clearRecordedFormValues()
1584 d->m_formAboutToBeSubmitted = 0;
1585 d->m_formValuesAboutToBeSubmitted.clear();
1588 void Frame::recordFormValue(const String& name, const String& value, PassRefPtr<HTMLFormElement> element)
1590 d->m_formAboutToBeSubmitted = element;
1591 d->m_formValuesAboutToBeSubmitted.set(name, value);
1594 void Frame::submitFormAgain()
1596 FramePrivate::SubmitForm* form = d->m_submitForm;
1597 d->m_submitForm = 0;
1598 if (d->m_doc && !d->m_doc->parsing() && form)
1599 submitForm(form->submitAction, form->submitUrl, form->submitFormData,
1600 form->target, form->submitContentType, form->submitBoundary);
1604 void Frame::submitForm(const char *action, const String& url, const FormData& formData, const String& _target, const String& contentType, const String& boundary)
1606 KURL u = completeURL(url.deprecatedString());
1609 // ### ERROR HANDLING!
1612 DeprecatedString urlstring = u.url();
1613 if (urlstring.startsWith("javascript:", false)) {
1614 urlstring = KURL::decode_string(urlstring);
1615 d->m_executingJavaScriptFormAction = true;
1616 executeScript(0, urlstring.mid(11));
1617 d->m_executingJavaScriptFormAction = false;
1621 ResourceRequest request;
1623 if (!d->m_referrer.isEmpty())
1624 request.setReferrer(d->m_referrer);
1626 request.frameName = _target.isEmpty() ? d->m_doc->baseTarget() : _target ;
1628 // Handle mailto: forms
1629 if (u.protocol() == "mailto") {
1631 DeprecatedString bodyEnc;
1632 if (contentType.lower() == "multipart/form-data")
1633 // FIXME: is this correct? I suspect not
1634 bodyEnc = KURL::encode_string(formData.flattenToString().deprecatedString());
1635 else if (contentType.lower() == "text/plain") {
1636 // Convention seems to be to decode, and s/&/\n/
1637 DeprecatedString tmpbody = formData.flattenToString().deprecatedString();
1638 tmpbody.replace('&', '\n');
1639 tmpbody.replace('+', ' ');
1640 tmpbody = KURL::decode_string(tmpbody); // Decode the rest of it
1641 bodyEnc = KURL::encode_string(tmpbody); // Recode for the URL
1643 bodyEnc = KURL::encode_string(formData.flattenToString().deprecatedString());
1645 DeprecatedString query = u.query();
1646 if (!query.isEmpty())
1648 query += "body=" + bodyEnc;
1653 if (strcmp(action, "get") == 0) {
1654 if (u.protocol() != "mailto")
1655 u.setQuery(formData.flattenToString().deprecatedString());
1656 request.setDoPost(false);
1658 request.postData = formData;
1659 request.setDoPost(true);
1661 // construct some user headers if necessary
1662 if (contentType.isNull() || contentType == "application/x-www-form-urlencoded")
1663 request.setContentType("Content-Type: application/x-www-form-urlencoded");
1664 else // contentType must be "multipart/form-data"
1665 request.setContentType("Content-Type: " + contentType + "; boundary=" + boundary);
1668 if (d->m_runningScripts > 0) {
1669 if (d->m_submitForm)
1671 d->m_submitForm = new FramePrivate::SubmitForm;
1672 d->m_submitForm->submitAction = action;
1673 d->m_submitForm->submitUrl = url;
1674 d->m_submitForm->submitFormData = formData;
1675 d->m_submitForm->target = _target;
1676 d->m_submitForm->submitContentType = contentType;
1677 d->m_submitForm->submitBoundary = boundary;
1680 submitForm(request);
1684 void Frame::parentCompleted()
1686 if (d->m_scheduledRedirection != noRedirectionScheduled && !d->m_redirectionTimer.isActive())
1687 startRedirectionTimer();
1690 void Frame::childCompleted(bool complete)
1692 if (complete && !tree()->parent())
1693 d->m_bPendingChildRedirection = true;
1697 int Frame::zoomFactor() const
1699 return d->m_zoomFactor;
1702 void Frame::setZoomFactor(int percent)
1704 if (d->m_zoomFactor == percent)
1707 d->m_zoomFactor = percent;
1710 d->m_doc->recalcStyle(Node::Force);
1712 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
1713 child->setZoomFactor(d->m_zoomFactor);
1715 if (d->m_doc && d->m_doc->renderer() && d->m_doc->renderer()->needsLayout())
1719 void Frame::setJSStatusBarText(const String& text)
1721 d->m_kjsStatusBarText = text;
1722 setStatusBarText(d->m_kjsStatusBarText);
1725 void Frame::setJSDefaultStatusBarText(const String& text)
1727 d->m_kjsDefaultStatusBarText = text;
1728 setStatusBarText(d->m_kjsDefaultStatusBarText);
1731 String Frame::jsStatusBarText() const
1733 return d->m_kjsStatusBarText;
1736 String Frame::jsDefaultStatusBarText() const
1738 return d->m_kjsDefaultStatusBarText;
1741 String Frame::referrer() const
1743 return d->m_referrer;
1746 String Frame::lastModified() const
1748 return d->m_lastModified;
1751 void Frame::reparseConfiguration()
1753 setAutoloadImages(d->m_settings->autoLoadImages());
1755 d->m_bJScriptEnabled = d->m_settings->isJavaScriptEnabled();
1756 d->m_bJavaEnabled = d->m_settings->isJavaEnabled();
1757 d->m_bPluginsEnabled = d->m_settings->isPluginsEnabled();
1759 const KURL& userStyleSheetLocation = d->m_settings->userStyleSheetLocation();
1760 if (!userStyleSheetLocation.isEmpty())
1761 setUserStyleSheetLocation(userStyleSheetLocation);
1763 setUserStyleSheet(String());
1765 // FIXME: It's not entirely clear why the following is needed.
1766 // The document automatically does this as required when you set the style sheet.
1767 // But we had problems when this code was removed. Details are in
1768 // <http://bugzilla.opendarwin.org/show_bug.cgi?id=8079>.
1770 d->m_doc->updateStyleSelector();
1773 bool Frame::shouldDragAutoNode(Node *node, const IntPoint& point) const
1779 bool Frame::isPointInsideSelection(const IntPoint& point)
1781 // Treat a collapsed selection like no selection.
1782 if (!selectionController()->isRange())
1784 if (!document()->renderer())
1787 RenderObject::NodeInfo nodeInfo(true, true);
1788 document()->renderer()->layer()->hitTest(nodeInfo, point);
1789 Node *innerNode = nodeInfo.innerNode();
1790 if (!innerNode || !innerNode->renderer())
1793 Position pos(innerNode->renderer()->positionForPoint(point).deepEquivalent());
1797 Node *n = selectionController()->start().node();
1799 if (n == pos.node()) {
1800 if ((n == selectionController()->start().node() && pos.offset() < selectionController()->start().offset()) ||
1801 (n == selectionController()->end().node() && pos.offset() > selectionController()->end().offset())) {
1806 if (n == selectionController()->end().node())
1808 n = n->traverseNextNode();
1814 void Frame::selectClosestWordFromMouseEvent(const PlatformMouseEvent& mouse, Node *innerNode)
1816 Selection newSelection;
1818 if (innerNode && innerNode->renderer() && mouseDownMayStartSelect() && innerNode->renderer()->shouldSelect()) {
1819 IntPoint vPoint = view()->windowToContents(mouse.pos());
1820 VisiblePosition pos(innerNode->renderer()->positionForPoint(vPoint));
1821 if (pos.isNotNull()) {
1822 newSelection = Selection(pos);
1823 newSelection.expandUsingGranularity(WordGranularity);
1827 if (newSelection.isRange()) {
1828 d->m_selectionGranularity = WordGranularity;
1829 d->m_beganSelectingText = true;
1832 if (shouldChangeSelection(newSelection))
1833 selectionController()->setSelection(newSelection);
1836 void Frame::handleMousePressEventDoubleClick(const MouseEventWithHitTestResults& event)
1838 if (event.event().button() == LeftButton) {
1839 if (selectionController()->isRange())
1840 // A double-click when range is already selected
1841 // should not change the selection. So, do not call
1842 // selectClosestWordFromMouseEvent, but do set
1843 // m_beganSelectingText to prevent handleMouseReleaseEvent
1844 // from setting caret selection.
1845 d->m_beganSelectingText = true;
1847 selectClosestWordFromMouseEvent(event.event(), event.targetNode());
1851 void Frame::handleMousePressEventTripleClick(const MouseEventWithHitTestResults& event)
1853 Node *innerNode = event.targetNode();
1855 if (event.event().button() == LeftButton && innerNode && innerNode->renderer() &&
1856 mouseDownMayStartSelect() && innerNode->renderer()->shouldSelect()) {
1857 Selection newSelection;
1858 IntPoint vPoint = view()->windowToContents(event.event().pos());
1859 VisiblePosition pos(innerNode->renderer()->positionForPoint(vPoint));
1860 if (pos.isNotNull()) {
1861 newSelection = Selection(pos);
1862 newSelection.expandUsingGranularity(ParagraphGranularity);
1864 if (newSelection.isRange()) {
1865 d->m_selectionGranularity = ParagraphGranularity;
1866 d->m_beganSelectingText = true;
1869 if (shouldChangeSelection(newSelection))
1870 selectionController()->setSelection(newSelection);
1874 void Frame::handleMousePressEventSingleClick(const MouseEventWithHitTestResults& event)
1876 Node *innerNode = event.targetNode();
1878 if (event.event().button() == LeftButton) {
1879 if (innerNode && innerNode->renderer() &&
1880 mouseDownMayStartSelect() && innerNode->renderer()->shouldSelect()) {
1882 // Extend the selection if the Shift key is down, unless the click is in a link.
1883 bool extendSelection = event.event().shiftKey() && !event.isOverLink();
1885 // Don't restart the selection when the mouse is pressed on an
1886 // existing selection so we can allow for text dragging.
1887 IntPoint vPoint = view()->windowToContents(event.event().pos());
1888 if (!extendSelection && isPointInsideSelection(vPoint))
1891 VisiblePosition visiblePos(innerNode->renderer()->positionForPoint(vPoint));
1892 if (visiblePos.isNull())
1893 visiblePos = VisiblePosition(innerNode, innerNode->caretMinOffset(), DOWNSTREAM);
1894 Position pos = visiblePos.deepEquivalent();
1896 Selection newSelection = selectionController()->selection();
1897 if (extendSelection && newSelection.isCaretOrRange()) {
1898 selectionController()->clearModifyBias();
1900 // See <rdar://problem/3668157> REGRESSION (Mail): shift-click deselects when selection
1901 // was created right-to-left
1902 Position start = newSelection.start();
1903 Position end = newSelection.end();
1904 short before = Range::compareBoundaryPoints(pos.node(), pos.offset(), start.node(), start.offset());
1906 newSelection = Selection(pos, end);
1908 newSelection = Selection(start, pos);
1910 if (d->m_selectionGranularity != CharacterGranularity)
1911 newSelection.expandUsingGranularity(d->m_selectionGranularity);
1912 d->m_beganSelectingText = true;
1914 newSelection = Selection(visiblePos);
1915 d->m_selectionGranularity = CharacterGranularity;
1918 if (shouldChangeSelection(newSelection))
1919 selectionController()->setSelection(newSelection);
1924 void Frame::handleMousePressEvent(const MouseEventWithHitTestResults& event)
1926 Node *innerNode = event.targetNode();
1928 d->m_mousePressNode = innerNode;
1929 d->m_dragStartPos = event.event().pos();
1931 if (event.event().button() == LeftButton || event.event().button() == MiddleButton) {
1932 d->m_bMousePressed = true;
1933 d->m_beganSelectingText = false;
1935 if (event.event().clickCount() == 2) {
1936 handleMousePressEventDoubleClick(event);
1939 if (event.event().clickCount() >= 3) {
1940 handleMousePressEventTripleClick(event);
1943 handleMousePressEventSingleClick(event);
1946 setMouseDownMayStartAutoscroll(mouseDownMayStartSelect() ||
1947 (d->m_mousePressNode && d->m_mousePressNode->renderer() && d->m_mousePressNode->renderer()->shouldAutoscroll()));
1950 void Frame::handleMouseMoveEvent(const MouseEventWithHitTestResults& event)
1952 // Mouse not pressed. Do nothing.
1953 if (!d->m_bMousePressed)
1956 Node *innerNode = event.targetNode();
1958 if (event.event().button() != 0 || !innerNode || !innerNode->renderer())
1961 if (mouseDownMayStartAutoscroll()) {
1962 // If the selection is contained in a layer that can scroll, that layer should handle the autoscroll
1963 // Otherwise, let the bridge handle it so the view can scroll itself.
1964 RenderObject* renderer = innerNode->renderer();
1965 while (renderer && !renderer->shouldAutoscroll())
1966 renderer = renderer->parent();
1968 handleAutoscroll(renderer);
1970 setMouseDownMayStartDrag(false);
1971 view()->invalidateClick();
1974 if (mouseDownMayStartSelect() && innerNode->renderer()->shouldSelect()) {
1975 // handle making selection
1976 IntPoint vPoint = view()->windowToContents(event.event().pos());
1977 VisiblePosition pos(innerNode->renderer()->positionForPoint(vPoint));
1979 updateSelectionForMouseDragOverPosition(pos);
1984 void Frame::updateSelectionForMouseDragOverPosition(const VisiblePosition& pos)
1986 // Don't modify the selection if we're not on a node.
1990 // Restart the selection if this is the first mouse move. This work is usually
1991 // done in handleMousePressEvent, but not if the mouse press was on an existing selection.
1992 Selection newSelection = selectionController()->selection();
1993 selectionController()->clearModifyBias();
1995 if (!d->m_beganSelectingText) {
1996 d->m_beganSelectingText = true;
1997 newSelection = Selection(pos);
2000 newSelection.setExtent(pos);
2001 if (d->m_selectionGranularity != CharacterGranularity)
2002 newSelection.expandUsingGranularity(d->m_selectionGranularity);
2004 if (shouldChangeSelection(newSelection))
2005 selectionController()->setSelection(newSelection);
2008 void Frame::handleMouseReleaseEvent(const MouseEventWithHitTestResults& event)
2010 stopAutoscrollTimer();
2012 // Used to prevent mouseMoveEvent from initiating a drag before
2013 // the mouse is pressed again.
2014 d->m_bMousePressed = false;
2016 // Clear the selection if the mouse didn't move after the last mouse press.
2017 // We do this so when clicking on the selection, the selection goes away.
2018 // However, if we are editing, place the caret.
2019 if (mouseDownMayStartSelect() && !d->m_beganSelectingText
2020 && d->m_dragStartPos == event.event().pos()
2021 && selectionController()->isRange()) {
2022 Selection newSelection;
2023 Node *node = event.targetNode();
2024 if (node && node->isContentEditable() && node->renderer()) {
2025 IntPoint vPoint = view()->windowToContents(event.event().pos());
2026 VisiblePosition pos = node->renderer()->positionForPoint(vPoint);
2027 newSelection = Selection(pos);
2029 if (shouldChangeSelection(newSelection))
2030 selectionController()->setSelection(newSelection);
2033 notifyRendererOfSelectionChange(true);
2035 selectFrameElementInParentIfFullySelected();
2038 void Frame::selectAll()
2043 Node* root = selectionController()->isContentEditable() ? selectionController()->rootEditableElement() : d->m_doc->documentElement();
2044 selectContentsOfNode(root);
2045 selectFrameElementInParentIfFullySelected();
2046 notifyRendererOfSelectionChange(true);
2049 bool Frame::selectContentsOfNode(Node* node)
2051 Selection newSelection(Selection::selectionFromContentsOfNode(node));
2052 if (shouldChangeSelection(newSelection)) {
2053 selectionController()->setSelection(newSelection);
2059 bool Frame::shouldChangeSelection(const Selection& newSelection) const
2061 return shouldChangeSelection(selectionController()->selection(), newSelection, newSelection.affinity(), false);
2064 bool Frame::shouldDeleteSelection(const Selection& newSelection) const
2069 bool Frame::shouldBeginEditing(const Range *range) const
2074 bool Frame::shouldEndEditing(const Range *range) const
2079 bool Frame::isContentEditable() const
2083 return d->m_doc->inDesignMode();
2086 void Frame::textFieldDidBeginEditing(Element* input)
2090 void Frame::textFieldDidEndEditing(Element* input)
2094 void Frame::textDidChangeInTextField(Element* input)
2098 bool Frame::doTextFieldCommandFromEvent(Element* input, const PlatformKeyboardEvent* evt)
2103 void Frame::textWillBeDeletedInTextField(Element* input)
2107 void Frame::textDidChangeInTextArea(Element* input)
2111 bool Frame::isSelectionInPasswordField()
2113 Node* startNode = selectionController()->start().node();
2115 startNode = startNode->shadowAncestorNode();
2116 return startNode && startNode->hasTagName(inputTag) && static_cast<HTMLInputElement*>(startNode)->inputType() == HTMLInputElement::PASSWORD;
2119 EditCommand* Frame::lastEditCommand()
2121 return d->m_lastEditCommand.get();
2124 static void dispatchEditableContentChangedEvents(const EditCommand& command)
2126 Element* startRoot = command.startingRootEditableElement();
2127 Element* endRoot = command.endingRootEditableElement();
2130 startRoot->dispatchEvent(new Event(khtmlEditableContentChangedEvent, false, false), ec, true);
2131 if (endRoot && endRoot != startRoot)
2132 endRoot->dispatchEvent(new Event(khtmlEditableContentChangedEvent, false, false), ec, true);
2135 void Frame::appliedEditing(PassRefPtr<EditCommand> cmd)
2137 dispatchEditableContentChangedEvents(*cmd);
2139 Selection newSelection(cmd->endingSelection());
2140 if (shouldChangeSelection(newSelection))
2141 selectionController()->setSelection(newSelection, false);
2143 // Now set the typing style from the command. Clear it when done.
2144 // This helps make the case work where you completely delete a piece
2145 // of styled text and then type a character immediately after.
2146 // That new character needs to take on the style of the just-deleted text.
2147 // FIXME: Improve typing style.
2148 // See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
2149 if (cmd->typingStyle()) {
2150 setTypingStyle(cmd->typingStyle());
2151 cmd->setTypingStyle(0);
2154 // Command will be equal to last edit command only in the case of typing
2155 if (d->m_lastEditCommand == cmd)
2156 assert(cmd->isTypingCommand());
2158 // Only register a new undo command if the command passed in is
2159 // different from the last command
2160 d->m_lastEditCommand = cmd.get();
2161 registerCommandForUndo(cmd);
2163 respondToChangedContents(newSelection);
2166 void Frame::unappliedEditing(PassRefPtr<EditCommand> cmd)
2168 dispatchEditableContentChangedEvents(*cmd);
2170 Selection newSelection(cmd->startingSelection());
2171 if (shouldChangeSelection(newSelection))
2172 selectionController()->setSelection(newSelection, true);
2174 d->m_lastEditCommand = 0;
2175 registerCommandForRedo(cmd);
2176 respondToChangedContents(newSelection);
2179 void Frame::reappliedEditing(PassRefPtr<EditCommand> cmd)
2181 dispatchEditableContentChangedEvents(*cmd);
2183 Selection newSelection(cmd->endingSelection());
2184 if (shouldChangeSelection(newSelection))
2185 selectionController()->setSelection(newSelection, true);
2187 d->m_lastEditCommand = 0;
2188 registerCommandForUndo(cmd);
2189 respondToChangedContents(newSelection);
2192 CSSMutableStyleDeclaration *Frame::typingStyle() const
2194 return d->m_typingStyle.get();
2197 void Frame::setTypingStyle(CSSMutableStyleDeclaration *style)
2199 d->m_typingStyle = style;
2202 void Frame::clearTypingStyle()
2204 d->m_typingStyle = 0;
2207 JSValue* Frame::executeScript(const String& filename, int baseLine, Node* n, const String& script)
2209 // FIXME: This is missing stuff that the other executeScript has.
2210 // --> d->m_runningScripts and submitFormAgain.
2212 KJSProxy* proxy = jScript();
2215 JSValue* ret = proxy->evaluate(filename, baseLine, script, n);
2216 Document::updateDocumentsRendering();
2220 Frame *Frame::opener()
2225 void Frame::setOpener(Frame* opener)
2228 d->m_opener->d->m_openedFrames.remove(this);
2230 opener->d->m_openedFrames.add(this);
2231 d->m_opener = opener;
2234 bool Frame::openedByJS()
2236 return d->m_openedByJS;
2239 void Frame::setOpenedByJS(bool _openedByJS)
2241 d->m_openedByJS = _openedByJS;
2244 bool Frame::tabsToLinks() const
2249 bool Frame::tabsToAllControls() const
2254 void Frame::copyToPasteboard()
2259 void Frame::cutToPasteboard()
2264 void Frame::pasteFromPasteboard()
2266 issuePasteCommand();
2269 void Frame::pasteAndMatchStyle()
2271 issuePasteAndMatchStyleCommand();
2274 bool Frame::mayCopy()
2276 return !isSelectionInPasswordField();
2279 void Frame::transpose()
2281 issueTransposeCommand();
2295 void Frame::computeAndSetTypingStyle(CSSStyleDeclaration *style, EditAction editingAction)
2297 if (!style || style->length() == 0) {
2302 // Calculate the current typing style.
2303 RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
2304 if (typingStyle()) {
2305 typingStyle()->merge(mutableStyle.get());
2306 mutableStyle = typingStyle();
2309 Node *node = selectionController()->selection().visibleStart().deepEquivalent().node();
2310 CSSComputedStyleDeclaration computedStyle(node);
2311 computedStyle.diff(mutableStyle.get());
2313 // Handle block styles, substracting these from the typing style.
2314 RefPtr<CSSMutableStyleDeclaration> blockStyle = mutableStyle->copyBlockProperties();
2315 blockStyle->diff(mutableStyle.get());
2316 if (document() && blockStyle->length() > 0)
2317 applyCommand(new ApplyStyleCommand(document(), blockStyle.get(), editingAction));
2319 // Set the remaining style as the typing style.
2320 d->m_typingStyle = mutableStyle.release();
2323 void Frame::applyStyle(CSSStyleDeclaration *style, EditAction editingAction)
2325 switch (selectionController()->state()) {
2326 case Selection::NONE:
2329 case Selection::CARET: {
2330 computeAndSetTypingStyle(style, editingAction);
2333 case Selection::RANGE:
2334 if (document() && style)
2335 applyCommand(new ApplyStyleCommand(document(), style, editingAction));
2340 void Frame::applyParagraphStyle(CSSStyleDeclaration *style, EditAction editingAction)
2342 switch (selectionController()->state()) {
2343 case Selection::NONE:
2346 case Selection::CARET:
2347 case Selection::RANGE:
2348 if (document() && style)
2349 applyCommand(new ApplyStyleCommand(document(), style, editingAction, ApplyStyleCommand::ForceBlockProperties));
2354 static void updateState(CSSMutableStyleDeclaration *desiredStyle, CSSComputedStyleDeclaration *computedStyle, bool& atStart, Frame::TriState& state)
2356 DeprecatedValueListConstIterator<CSSProperty> end;
2357 for (DeprecatedValueListConstIterator<CSSProperty> it = desiredStyle->valuesIterator(); it != end; ++it) {
2358 int propertyID = (*it).id();
2359 String desiredProperty = desiredStyle->getPropertyValue(propertyID);
2360 String computedProperty = computedStyle->getPropertyValue(propertyID);
2361 Frame::TriState propertyState = equalIgnoringCase(desiredProperty, computedProperty)
2362 ? Frame::trueTriState : Frame::falseTriState;
2364 state = propertyState;
2366 } else if (state != propertyState) {
2367 state = Frame::mixedTriState;
2373 Frame::TriState Frame::selectionListState() const
2375 TriState state = falseTriState;
2377 if (!selectionController()->isRange()) {
2378 Node* selectionNode = selectionController()->selection().start().node();
2379 if (enclosingList(selectionNode))
2380 return trueTriState;
2382 //FIXME: Support ranges
2388 Frame::TriState Frame::selectionHasStyle(CSSStyleDeclaration *style) const
2390 bool atStart = true;
2391 TriState state = falseTriState;
2393 RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
2395 if (!selectionController()->isRange()) {
2397 RefPtr<CSSComputedStyleDeclaration> selectionStyle = selectionComputedStyle(nodeToRemove);
2398 if (!selectionStyle)
2399 return falseTriState;
2400 updateState(mutableStyle.get(), selectionStyle.get(), atStart, state);
2402 ExceptionCode ec = 0;
2403 nodeToRemove->remove(ec);
2407 for (Node* node = selectionController()->start().node(); node; node = node->traverseNextNode()) {
2408 RefPtr<CSSComputedStyleDeclaration> computedStyle = new CSSComputedStyleDeclaration(node);
2410 updateState(mutableStyle.get(), computedStyle.get(), atStart, state);
2411 if (state == mixedTriState)
2413 if (node == selectionController()->end().node())
2421 bool Frame::selectionStartHasStyle(CSSStyleDeclaration *style) const
2424 RefPtr<CSSStyleDeclaration> selectionStyle = selectionComputedStyle(nodeToRemove);
2425 if (!selectionStyle)
2428 RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
2431 DeprecatedValueListConstIterator<CSSProperty> end;
2432 for (DeprecatedValueListConstIterator<CSSProperty> it = mutableStyle->valuesIterator(); it != end; ++it) {
2433 int propertyID = (*it).id();
2434 if (!equalIgnoringCase(mutableStyle->getPropertyValue(propertyID), selectionStyle->getPropertyValue(propertyID))) {
2441 ExceptionCode ec = 0;
2442 nodeToRemove->remove(ec);
2449 String Frame::selectionStartStylePropertyValue(int stylePropertyID) const
2452 RefPtr<CSSStyleDeclaration> selectionStyle = selectionComputedStyle(nodeToRemove);
2453 if (!selectionStyle)
2456 String value = selectionStyle->getPropertyValue(stylePropertyID);
2459 ExceptionCode ec = 0;
2460 nodeToRemove->remove(ec);
2467 CSSComputedStyleDeclaration *Frame::selectionComputedStyle(Node *&nodeToRemove) const
2474 if (selectionController()->isNone())
2477 RefPtr<Range> range(selectionController()->toRange());
2478 Position pos = range->editingStartPosition();
2480 Element *elem = pos.element();
2484 RefPtr<Element> styleElement = elem;
2485 ExceptionCode ec = 0;
2487 if (d->m_typingStyle) {
2488 styleElement = document()->createElementNS(xhtmlNamespaceURI, "span", ec);
2491 styleElement->setAttribute(styleAttr, d->m_typingStyle->cssText().impl(), ec);
2494 styleElement->appendChild(document()->createEditingTextNode(""), ec);
2497 if (elem->renderer() && elem->renderer()->canHaveChildren()) {
2498 elem->appendChild(styleElement, ec);
2500 Node *parent = elem->parent();
2501 Node *next = elem->nextSibling();
2504 parent->insertBefore(styleElement, next, ec);
2506 parent->appendChild(styleElement, ec);
2511 nodeToRemove = styleElement.get();
2514 return new CSSComputedStyleDeclaration(styleElement);
2517 void Frame::applyEditingStyleToBodyElement() const
2522 RefPtr<NodeList> list = d->m_doc->getElementsByTagName("body");
2523 unsigned len = list->length();
2524 for (unsigned i = 0; i < len; i++) {
2525 applyEditingStyleToElement(static_cast<Element*>(list->item(i)));
2529 void Frame::removeEditingStyleFromBodyElement() const
2534 RefPtr<NodeList> list = d->m_doc->getElementsByTagName("body");
2535 unsigned len = list->length();
2536 for (unsigned i = 0; i < len; i++) {
2537 removeEditingStyleFromElement(static_cast<Element*>(list->item(i)));
2541 void Frame::applyEditingStyleToElement(Element* element) const
2546 CSSStyleDeclaration* style = element->style();
2549 ExceptionCode ec = 0;
2550 style->setProperty(CSS_PROP_WORD_WRAP, "break-word", false, ec);
2552 style->setProperty(CSS_PROP__WEBKIT_NBSP_MODE, "space", false, ec);
2554 style->setProperty(CSS_PROP__WEBKIT_LINE_BREAK, "after-white-space", false, ec);
2558 void Frame::removeEditingStyleFromElement(Element*) const
2562 bool Frame::isCharacterSmartReplaceExempt(UChar, bool)
2569 static HashSet<Frame*> lifeSupportSet;
2572 void Frame::endAllLifeSupport()
2575 HashSet<Frame*> lifeSupportCopy = lifeSupportSet;
2576 HashSet<Frame*>::iterator end = lifeSupportCopy.end();
2577 for (HashSet<Frame*>::iterator it = lifeSupportCopy.begin(); it != end; ++it)
2578 (*it)->endLifeSupport();
2582 void Frame::keepAlive()
2584 if (d->m_lifeSupportTimer.isActive())
2588 lifeSupportSet.add(this);
2590 d->m_lifeSupportTimer.startOneShot(0);
2593 void Frame::endLifeSupport()
2595 if (!d->m_lifeSupportTimer.isActive())
2597 d->m_lifeSupportTimer.stop();
2599 lifeSupportSet.remove(this);
2604 void Frame::lifeSupportTimerFired(Timer<Frame>*)
2607 lifeSupportSet.remove(this);
2612 // Workaround for the fact that it's hard to delete a frame.
2613 // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
2614 // Can't do this implicitly as part of every setSelection call because in some contexts it might not be good
2615 // for the focus to move to another frame. So instead we call it from places where we are selecting with the
2616 // mouse or the keyboard after setting the selection.
2617 void Frame::selectFrameElementInParentIfFullySelected()
2619 // Find the parent frame; if there is none, then we have nothing to do.
2620 Frame *parent = tree()->parent();
2623 FrameView *parentView = parent->view();
2627 // Check if the selection contains the entire frame contents; if not, then there is nothing to do.
2628 if (!selectionController()->isRange())
2630 if (!isStartOfDocument(selectionController()->selection().visibleStart()))
2632 if (!isEndOfDocument(selectionController()->selection().visibleEnd()))
2635 // Get to the <iframe> or <frame> (or even <object>) element in the parent frame.
2636 Document *doc = document();
2639 Element *ownerElement = doc->ownerElement();
2642 Node *ownerElementParent = ownerElement->parentNode();
2643 if (!ownerElementParent)
2646 // This method's purpose is it to make it easier to select iframes (in order to delete them). Don't do anything if the iframe isn't deletable.
2647 if (!ownerElementParent->isContentEditable())
2650 // Create compute positions before and after the element.
2651 unsigned ownerElementNodeIndex = ownerElement->nodeIndex();
2652 VisiblePosition beforeOwnerElement(VisiblePosition(ownerElementParent, ownerElementNodeIndex, SEL_DEFAULT_AFFINITY));
2653 VisiblePosition afterOwnerElement(VisiblePosition(ownerElementParent, ownerElementNodeIndex + 1, VP_UPSTREAM_IF_POSSIBLE));
2655 // Focus on the parent frame, and then select from before this element to after.
2656 Selection newSelection(beforeOwnerElement, afterOwnerElement);
2657 if (parent->shouldChangeSelection(newSelection)) {
2658 parentView->setFocus();
2659 parent->selectionController()->setSelection(newSelection);
2663 bool Frame::mouseDownMayStartAutoscroll() const
2665 return d->m_mouseDownMayStartAutoscroll;
2668 void Frame::setMouseDownMayStartAutoscroll(bool b)
2670 d->m_mouseDownMayStartAutoscroll = b;
2673 bool Frame::mouseDownMayStartDrag() const
2675 return d->m_mouseDownMayStartDrag;
2678 void Frame::setMouseDownMayStartDrag(bool b)
2680 d->m_mouseDownMayStartDrag = b;
2683 void Frame::handleFallbackContent()
2685 Element* owner = ownerElement();
2686 if (!owner || !owner->hasTagName(objectTag))
2688 static_cast<HTMLObjectElement*>(owner)->renderFallbackContent();
2691 void Frame::setSettings(Settings *settings)
2693 d->m_settings = settings;
2696 void Frame::provisionalLoadStarted()
2698 // we don't want to wait until we get an actual http response back
2699 // to cancel pending redirects, otherwise they might fire before
2701 cancelRedirection(true);
2704 bool Frame::userGestureHint()
2706 Frame *rootFrame = this;
2707 while (rootFrame->tree()->parent())
2708 rootFrame = rootFrame->tree()->parent();
2710 if (rootFrame->jScript())
2711 return rootFrame->jScript()->interpreter()->wasRunByUserGesture();
2713 return true; // If JavaScript is disabled, a user gesture must have initiated the navigation
2716 RenderObject *Frame::renderer() const
2718 Document *doc = document();
2719 return doc ? doc->renderer() : 0;
2722 Element* Frame::ownerElement()
2724 return d->m_ownerElement;
2727 RenderPart* Frame::ownerRenderer()
2729 Element* ownerElement = d->m_ownerElement;
2732 return static_cast<RenderPart*>(ownerElement->renderer());
2735 IntRect Frame::selectionRect() const
2737 RenderView *root = static_cast<RenderView*>(renderer());
2741 return root->selectionRect();
2744 // returns FloatRect because going through IntRect would truncate any floats
2745 FloatRect Frame::visibleSelectionRect() const
2750 return intersection(selectionRect(), d->m_view->visibleContentRect());
2753 bool Frame::isFrameSet() const
2755 Document* document = d->m_doc.get();
2756 if (!document || !document->isHTMLDocument())
2758 Node *body = static_cast<HTMLDocument*>(document)->body();
2759 return body && body->renderer() && body->hasTagName(framesetTag);
2762 bool Frame::openURL(const KURL& URL)
2764 ASSERT_NOT_REACHED();
2768 void Frame::didNotOpenURL(const KURL& URL)
2770 if (d->m_submittedFormURL == URL)
2771 d->m_submittedFormURL = KURL();
2774 // Scans logically forward from "start", including any child frames
2775 static HTMLFormElement *scanForForm(Node *start)
2778 for (n = start; n; n = n->traverseNextNode()) {
2779 if (n->hasTagName(formTag))
2780 return static_cast<HTMLFormElement*>(n);
2781 else if (n->isHTMLElement() && static_cast<HTMLElement*>(n)->isGenericFormElement())
2782 return static_cast<HTMLGenericFormElement*>(n)->form();
2783 else if (n->hasTagName(frameTag) || n->hasTagName(iframeTag)) {
2784 Node *childDoc = static_cast<HTMLFrameElement*>(n)->contentDocument();
2785 if (HTMLFormElement *frameResult = scanForForm(childDoc))
2792 // We look for either the form containing the current focus, or for one immediately after it
2793 HTMLFormElement *Frame::currentForm() const
2795 // start looking either at the active (first responder) node, or where the selection is
2796 Node *start = d->m_doc ? d->m_doc->focusNode() : 0;
2798 start = selectionController()->start().node();
2800 // try walking up the node tree to find a form element
2802 for (n = start; n; n = n->parentNode()) {
2803 if (n->hasTagName(formTag))
2804 return static_cast<HTMLFormElement*>(n);
2805 else if (n->isHTMLElement()
2806 && static_cast<HTMLElement*>(n)->isGenericFormElement())
2807 return static_cast<HTMLGenericFormElement*>(n)->form();
2810 // try walking forward in the node tree to find a form element
2811 return start ? scanForForm(start) : 0;
2814 void Frame::setEncoding(const String& name, bool userChosen)
2816 if (!d->m_workingURL.isEmpty())
2817 receivedFirstData();
2818 d->m_encoding = name;
2819 d->m_haveEncoding = userChosen;
2822 void Frame::addData(const char *bytes, int length)
2824 ASSERT(d->m_workingURL.isEmpty());
2826 ASSERT(d->m_doc->parsing());
2827 write(bytes, length);
2830 // FIXME: should this go in SelectionController?
2831 void Frame::revealSelection(const RenderLayer::ScrollAlignment& alignment) const
2835 switch (selectionController()->state()) {
2836 case Selection::NONE:
2839 case Selection::CARET:
2840 rect = selectionController()->caretRect();
2843 case Selection::RANGE:
2844 rect = selectionRect();
2848 Position start = selectionController()->start();
2849 Position end = selectionController()->end();
2851 ASSERT(start.node());
2852 if (start.node() && start.node()->renderer()) {
2853 RenderLayer *layer = start.node()->renderer()->enclosingLayer();
2855 ASSERT(!end.node() || !end.node()->renderer()
2856 || (end.node()->renderer()->enclosingLayer() == layer));
2857 layer->scrollRectToVisible(rect, alignment, alignment);
2862 void Frame::revealCaret(const RenderLayer::ScrollAlignment& alignment) const
2864 if (!hasSelection())
2867 Position extent = selectionController()->extent();
2868 if (extent.node() && extent.node()->renderer()) {
2869 IntRect extentRect = VisiblePosition(extent).caretRect();
2870 RenderLayer* layer = extent.node()->renderer()->enclosingLayer();
2872 layer->scrollRectToVisible(extentRect, alignment, alignment);
2876 // FIXME: should this be here?
2877 bool Frame::scrollOverflow(ScrollDirection direction, ScrollGranularity granularity)
2883 Node *node = document()->focusNode();
2885 node = d->m_mousePressNode.get();
2889 RenderObject *r = node->renderer();
2890 if (r != 0 && !r->isListBox()) {
2891 return r->scroll(direction, granularity);
2898 void Frame::handleAutoscroll(RenderObject* renderer)
2900 if (d->m_autoscrollTimer.isActive())
2902 setAutoscrollRenderer(renderer);
2903 startAutoscrollTimer();
2906 void Frame::autoscrollTimerFired(Timer<Frame>*)
2908 if (!d->m_bMousePressed){
2909 stopAutoscrollTimer();
2912 if (RenderObject* r = autoscrollRenderer())
2916 RenderObject* Frame::autoscrollRenderer() const
2918 return d->m_autoscrollRenderer;
2921 void Frame::setAutoscrollRenderer(RenderObject* renderer)
2923 d->m_autoscrollRenderer = renderer;
2926 RenderObject::NodeInfo Frame::nodeInfoAtPoint(const IntPoint& point, bool allowShadowContent)
2928 RenderObject::NodeInfo nodeInfo(true, true);
2929 renderer()->layer()->hitTest(nodeInfo, point);
2933 IntPoint widgetPoint(point);
2936 n = nodeInfo.innerNode();
2937 if (!n || !n->renderer() || !n->renderer()->isWidget())
2939 widget = static_cast<RenderWidget*>(n->renderer())->widget();
2940 if (!widget || !widget->isFrameView())
2942 Frame* frame = static_cast<HTMLFrameElement*>(n)->contentFrame();
2943 if (!frame || !frame->renderer())
2946 n->renderer()->absolutePosition(absX, absY, true);
2947 FrameView *view = static_cast<FrameView*>(widget);
2948 widgetPoint.setX(widgetPoint.x() - absX + view->contentsX());
2949 widgetPoint.setY(widgetPoint.y() - absY + view->contentsY());
2951 RenderObject::NodeInfo widgetNodeInfo(true, true);
2952 frame->renderer()->layer()->hitTest(widgetNodeInfo, widgetPoint);
2953 nodeInfo = widgetNodeInfo;
2956 if (!allowShadowContent) {
2957 Node* node = nodeInfo.innerNode();
2959 node = node->shadowAncestorNode();
2960 nodeInfo.setInnerNode(node);
2961 node = nodeInfo.innerNonSharedNode();
2963 node = node->shadowAncestorNode();
2964 nodeInfo.setInnerNonSharedNode(node);
2969 bool Frame::hasSelection()
2971 if (selectionController()->isNone())
2974 // If a part has a selection, it should also have a document.
2980 void Frame::startAutoscrollTimer()
2982 d->m_autoscrollTimer.startRepeating(autoscrollInterval);
2985 void Frame::stopAutoscrollTimer()
2987 setAutoscrollRenderer(0);
2988 d->m_autoscrollTimer.stop();
2991 // FIXME: why is this here instead of on the FrameView?
2992 void Frame::paint(GraphicsContext* p, const IntRect& rect)
2996 if (!document() || document()->printing())
2997 fillWithRed = false; // Printing, don't fill with red (can't remember why).
2998 else if (document()->ownerElement())
2999 fillWithRed = false; // Subframe, don't fill with red.
3000 else if (view() && view()->isTransparent())
3001 fillWithRed = false; // Transparent, don't fill with red.
3002 else if (d->m_paintRestriction == PaintRestrictionSelectionOnly || d->m_paintRestriction == PaintRestrictionSelectionOnlyWhiteText)
3003 fillWithRed = false; // Selections are transparent, don't fill with red.
3004 else if (d->m_elementToDraw)
3005 fillWithRed = false; // Element images are transparent, don't fill with red.
3010 p->fillRect(rect, Color(0xFF, 0, 0));
3014 // d->m_elementToDraw is used to draw only one element
3015 RenderObject *eltRenderer = d->m_elementToDraw ? d->m_elementToDraw->renderer() : 0;
3016 if (d->m_paintRestriction == PaintRestrictionNone)
3017 renderer()->document()->invalidateRenderedRectsForMarkersInRect(rect);
3018 renderer()->layer()->paint(p, rect, d->m_paintRestriction, eltRenderer);
3021 // Regions may have changed as a result of the visibility/z-index of element changing.
3022 if (renderer()->document()->dashboardRegionsDirty())
3023 renderer()->view()->frameView()->updateDashboardRegions();
3026 LOG_ERROR("called Frame::paint with nil renderer");
3031 void Frame::adjustPageHeight(float *newBottom, float oldTop, float oldBottom, float bottomLimit)
3033 RenderView *root = static_cast<RenderView*>(document()->renderer());
3035 // Use a context with painting disabled.
3036 GraphicsContext context(0);
3037 root->setTruncatedAt((int)floorf(oldBottom));
3038 IntRect dirtyRect(0, (int)floorf(oldTop), root->docWidth(), (int)ceilf(oldBottom - oldTop));
3039 root->layer()->paint(&context, dirtyRect);
3040 *newBottom = root->bestTruncatedAt();
3041 if (*newBottom == 0)
3042 *newBottom = oldBottom;
3044 *newBottom = oldBottom;
3049 PausedTimeouts *Frame::pauseTimeouts()
3052 if (d->m_doc && d->m_doc->svgExtensions())
3053 d->m_doc->accessSVGExtensions()->pauseAnimations();
3056 if (d->m_doc && d->m_jscript) {
3057 if (Window* w = Window::retrieveWindow(this))
3058 return w->pauseTimeouts();
3063 void Frame::resumeTimeouts(PausedTimeouts* t)
3066 if (d->m_doc && d->m_doc->svgExtensions())
3067 d->m_doc->accessSVGExtensions()->unpauseAnimations();
3070 if (d->m_doc && d->m_jscript && d->m_bJScriptEnabled) {
3071 if (Window* w = Window::retrieveWindow(this))
3072 w->resumeTimeouts(t);
3076 bool Frame::canCachePage()
3078 // Only save page state if:
3079 // 1. We're not a frame or frameset.
3080 // 2. The page has no unload handler.
3081 // 3. The page has no password fields.
3082 // 4. The URL for the page is not https.
3083 // 5. The page has no applets.
3084 if (tree()->childCount() || d->m_plugins.size() ||
3086 d->m_url.protocol().startsWith("https") ||
3087 (d->m_doc && (d->m_doc->applets()->length() != 0 ||
3088 d->m_doc->hasWindowEventListener(unloadEvent) ||
3089 d->m_doc->hasPasswordField()))) {
3095 void Frame::saveWindowProperties(KJS::SavedProperties *windowProperties)
3097 Window *window = Window::retrieveWindow(this);
3099 window->saveProperties(*windowProperties);
3102 void Frame::saveLocationProperties(SavedProperties *locationProperties)
3104 Window *window = Window::retrieveWindow(this);
3107 Location *location = window->location();
3108 location->saveProperties(*locationProperties);
3112 void Frame::restoreWindowProperties(SavedProperties *windowProperties)
3114 Window *window = Window::retrieveWindow(this);
3116 window->restoreProperties(*windowProperties);
3119 void Frame::restoreLocationProperties(SavedProperties *locationProperties)
3121 Window *window = Window::retrieveWindow(this);
3124 Location *location = window->location();
3125 location->restoreProperties(*locationProperties);
3129 void Frame::saveInterpreterBuiltins(SavedBuiltins& interpreterBuiltins)
3132 jScript()->interpreter()->saveBuiltins(interpreterBuiltins);
3135 void Frame::restoreInterpreterBuiltins(const SavedBuiltins& interpreterBuiltins)
3138 jScript()->interpreter()->restoreBuiltins(interpreterBuiltins);
3141 Frame *Frame::frameForWidget(const Widget *widget)
3143 ASSERT_ARG(widget, widget);
3145 Node *node = nodeForWidget(widget);
3147 return frameForNode(node);
3149 // Assume all widgets are either form controls, or FrameViews.
3150 ASSERT(widget->isFrameView());
3151 return static_cast<const FrameView*>(widget)->frame();
3154 Frame *Frame::frameForNode(Node *node)
3156 ASSERT_ARG(node, node);
3157 return node->document()->frame();
3160 Node* Frame::nodeForWidget(const Widget* widget)
3162 ASSERT_ARG(widget, widget);
3163 WidgetClient* client = widget->client();
3166 return client->element(const_cast<Widget*>(widget));
3169 void Frame::clearDocumentFocus(Widget *widget)
3171 Node *node = nodeForWidget(widget);
3173 node->document()->setFocusNode(0);
3176 void Frame::updatePolicyBaseURL()
3178 if (tree()->parent() && tree()->parent()->document())
3179 setPolicyBaseURL(tree()->parent()->document()->policyBaseURL());
3181 setPolicyBaseURL(d->m_url.url());
3184 void Frame::setPolicyBaseURL(const String& s)
3187 document()->setPolicyBaseURL(s);
3188 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
3189 child->setPolicyBaseURL(s);
3192 void Frame::forceLayout()
3194 FrameView *v = d->m_view.get();
3197 // We cannot unschedule a pending relayout, since the force can be called with
3198 // a tiny rectangle from a drawRect update. By unscheduling we in effect
3199 // "validate" and stop the necessary full repaint from occurring. Basically any basic
3200 // append/remove DHTML is broken by this call. For now, I have removed the optimization
3201 // until we have a better invalidation stategy. -dwh
3202 //v->unscheduleRelayout();
3206 void Frame::forceLayoutWithPageWidthRange(float minPageWidth, float maxPageWidth)
3208 // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
3209 // the state of things before and after the layout
3210 RenderView *root = static_cast<RenderView*>(document()->renderer());
3212 // This magic is basically copied from khtmlview::print
3213 int pageW = (int)ceilf(minPageWidth);
3214 root->setWidth(pageW);
3215 root->setNeedsLayoutAndMinMaxRecalc();
3218 // If we don't fit in the minimum page width, we'll lay out again. If we don't fit in the
3219 // maximum page width, we will lay out to the maximum page width and clip extra content.
3220 // FIXME: We are assuming a shrink-to-fit printing implementation. A cropping
3221 // implementation should not do this!
3222 int rightmostPos = root->rightmostPosition();
3223 if (rightmostPos > minPageWidth) {
3224 pageW = min(rightmostPos, (int)ceilf(maxPageWidth));
3225 root->setWidth(pageW);
3226 root->setNeedsLayoutAndMinMaxRecalc();
3232 void Frame::sendResizeEvent()
3234 if (Document* doc = document())
3235 doc->dispatchWindowEvent(EventNames::resizeEvent, false, false);
3238 void Frame::sendScrollEvent()
3240 FrameView *v = d->m_view.get();
3242 Document *doc = document();
3245 doc->dispatchHTMLEvent(scrollEvent, true, false);
3249 bool Frame::scrollbarsVisible()
3254 if (view()->hScrollbarMode() == ScrollbarAlwaysOff || view()->vScrollbarMode() == ScrollbarAlwaysOff)
3260 void Frame::addMetaData(const String& key, const String& value)
3262 d->m_metaData.set(key, value);
3265 // This does the same kind of work that Frame::openURL does, except it relies on the fact
3266 // that a higher level already checked that the URLs match and the scrolling is the right thing to do.
3267 void Frame::scrollToAnchor(const KURL& URL)
3274 // It's important to model this as a load that starts and immediately finishes.
3275 // Otherwise, the parent frame may think we never finished loading.
3276 d->m_bComplete = false;
3280 bool Frame::closeURL()
3282 saveDocumentState();
3284 clearUndoRedoOperations();
3288 bool Frame::canMouseDownStartSelect(Node* node)
3290 if (!node || !node->renderer())
3293 // Check to see if -webkit-user-select has been set to none
3294 if (!node->renderer()->canSelect())
3297 // Some controls and images can't start a select on a mouse down.
3298 for (RenderObject* curr = node->renderer(); curr; curr = curr->parent()) {
3299 if (curr->style()->userSelect() == SELECT_IGNORE)
3306 void Frame::clearTimers(FrameView *view)
3309 view->unscheduleRelayout();
3310 if (view->frame()) {
3311 Document* document = view->frame()->document();
3312 if (document && document->renderer() && document->renderer()->layer())
3313 document->renderer()->layer()->suspendMarquees();
3318 void Frame::clearTimers()
3320 clearTimers(d->m_view.get());
3323 RenderStyle *Frame::styleForSelectionStart(Node *&nodeToRemove) const
3329 if (selectionController()->isNone())
3332 Position pos = selectionController()->selection().visibleStart().deepEquivalent();
3333 if (!pos.inRenderedContent())
3335 Node *node = pos.node();
3339 if (!d->m_typingStyle)
3340 return node->renderer()->style();
3342 ExceptionCode ec = 0;
3343 RefPtr<Element> styleElement = document()->createElementNS(xhtmlNamespaceURI, "span", ec);
3346 styleElement->setAttribute(styleAttr, d->m_typingStyle->cssText().impl(), ec);
3349 styleElement->appendChild(document()->createEditingTextNode(""), ec);
3352 node->parentNode()->appendChild(styleElement, ec);
3355 nodeToRemove = styleElement.get();
3356 return styleElement->renderer()->style();
3359 void Frame::setMediaType(const String& type)
3362 d->m_view->setMediaType(type);
3365 void Frame::setSelectionFromNone()
3367 // Put a caret inside the body if the entire frame is editable (either the
3368 // entire WebView is editable or designMode is on for this document).
3369 Document *doc = document();
3370 if (!doc || !selectionController()->isNone() || !isContentEditable())
3373 Node* node = doc->documentElement();
3374 while (node && !node->hasTagName(bodyTag))
3375 node = node->traverseNextNode();
3377 selectionController()->setSelection(Selection(Position(node, 0), DOWNSTREAM));
3380 bool Frame::isActive() const
3382 return d->m_isActive;
3385 void Frame::setIsActive(bool flag)
3387 if (d->m_isActive == flag)
3390 d->m_isActive = flag;
3392 // This method does the job of updating the view based on whether the view is "active".
3393 // This involves three kinds of drawing updates:
3395 // 1. The background color used to draw behind selected content (active | inactive color)
3397 d->m_view->updateContents(enclosingIntRect(visibleSelectionRect()));
3399 // 2. Caret blinking (blinks | does not blink)
3401 setSelectionFromNone();
3402 setCaretVisible(flag);
3404 // 3. The drawing of a focus ring around links in web pages.
3405 Document *doc = document();
3407 Node *node = doc->focusNode();
3410 if (node->renderer() && node->renderer()->style()->hasAppearance())
3411 theme()->stateChanged(node->renderer(), FocusState);
3415 // 4. Changing the tint of controls from clear to aqua/graphite and vice versa. We
3416 // do a "fake" paint. When the theme gets a paint call, it can then do an invalidate. This is only
3417 // done if the theme supports control tinting.
3418 if (doc && d->m_view && theme()->supportsControlTints() && renderer()) {
3419 doc->updateLayout(); // Ensure layout is up to date.
3420 IntRect visibleRect(enclosingIntRect(d->m_view->visibleContentRect()));
3421 GraphicsContext context((PlatformGraphicsContext*)0);
3422 context.setUpdatingControlTints(true);
3423 paint(&context, visibleRect);
3426 // 5. Enable or disable secure keyboard entry
3427 if ((flag && !isSecureKeyboardEntry() && doc && doc->focusNode() && doc->focusNode()->hasTagName(inputTag) &&
3428 static_cast<HTMLInputElement*>(doc->focusNode())->inputType() == HTMLInputElement::PASSWORD) ||
3429 (!flag && isSecureKeyboardEntry()))
3430 setSecureKeyboardEntry(flag);
3433 void Frame::setWindowHasFocus(bool flag)
3435 if (d->m_windowHasFocus == flag)
3437 d->m_windowHasFocus = flag;
3439 if (Document *doc = document())
3440 doc->dispatchWindowEvent(flag ? focusEvent : blurEvent, false, false);
3443 bool Frame::inViewSourceMode() const
3445 return d->m_inViewSourceMode;
3448 void Frame::setInViewSourceMode(bool mode) const
3450 d->m_inViewSourceMode = mode;
3453 UChar Frame::backslashAsCurrencySymbol() const
3455 Document *doc = document();
3458 Decoder *decoder = doc->decoder();
3462 return decoder->encoding().backslashAsCurrencySymbol();
3465 bool Frame::markedTextUsesUnderlines() const
3467 return d->m_markedTextUsesUnderlines;
3470 const Vector<MarkedTextUnderline>& Frame::markedTextUnderlines() const
3472 return d->m_markedTextUnderlines;
3475 // Searches from the beginning of the document if nothing is selected.
3476 bool Frame::findString(const String& target, bool forward, bool caseFlag, bool wrapFlag)
3478 if (target.isEmpty())
3481 // Initially search from the start (if forward) or end (if backward) of the selection, and search to edge of document.
3482 RefPtr<Range> searchRange(rangeOfContents(document()));
3483 Selection selection(selectionController()->selection());
3484 if (!selection.isNone()) {
3486 setStart(searchRange.get(), selection.visibleStart());
3488 setEnd(searchRange.get(), selection.visibleEnd());
3490 RefPtr<Range> resultRange(findPlainText(searchRange.get(), target, forward, caseFlag));
3491 // If the found range is already selected, find again.
3492 // Build a selection with the found range to remove collapsed whitespace.
3493 // Compare ranges instead of selection objects to ignore the way that the current selection was made.
3494 if (!selection.isNone() && *Selection(resultRange.get()).toRange() == *selection.toRange()) {
3495 searchRange = rangeOfContents(document());
3497 setStart(searchRange.get(), selection.visibleEnd());
3499 setEnd(searchRange.get(), selection.visibleStart());
3500 resultRange = findPlainText(searchRange.get(), target, forward, caseFlag);
3505 // If we didn't find anything and we're wrapping, search again in the entire document (this will
3506 // redundantly re-search the area already searched in some cases).
3507 if (resultRange->collapsed(exception) && wrapFlag) {
3508 searchRange = rangeOfContents(document());
3509 resultRange = findPlainText(searchRange.get(), target, forward, caseFlag);
3510 // We used to return false here if we ended up with the same range that we started with
3511 // (e.g., the selection was already the only instance of this text). But we decided that
3512 // this should be a success case instead, so we'll just fall through in that case.
3515 if (resultRange->collapsed(exception))
3518 selectionController()->setSelection(Selection(resultRange.get(), DOWNSTREAM));
3523 unsigned Frame::markAllMatchesForText(const String& target, bool caseFlag, unsigned limit)
3525 if (target.isEmpty())
3528 RefPtr<Range> searchRange(rangeOfContents(document()));
3531 unsigned matchCount = 0;
3533 RefPtr<Range> resultRange(findPlainText(searchRange.get(), target, true, caseFlag));
3534 if (resultRange->collapsed(exception))
3537 // A non-collapsed result range can in some funky whitespace cases still not
3538 // advance the range's start position (4509328). Break to avoid infinite loop.
3539 VisiblePosition newStart = endVisiblePosition(resultRange.get(), DOWNSTREAM);
3540 if (newStart == startVisiblePosition(searchRange.get(), DOWNSTREAM))
3545 document()->addMarker(resultRange.get(), DocumentMarker::TextMatch);
3547 // Stop looking if we hit the specified limit. A limit of 0 means no limit.
3548 if (limit > 0 && matchCount >= limit)
3551 setStart(searchRange.get(), newStart);
3554 // Do a "fake" paint in order to execute the code that computes the rendered rect for
3556 Document* doc = document();
3557 if (doc && d->m_view && renderer()) {
3558 doc->updateLayout(); // Ensure layout is up to date.
3559 IntRect visibleRect(enclosingIntRect(d->m_view->visibleContentRect()));
3560 GraphicsContext context((PlatformGraphicsContext*)0);
3561 context.setPaintingDisabled(true);
3562 paint(&context, visibleRect);
3568 bool Frame::markedTextMatchesAreHighlighted() const
3570 return d->m_highlightTextMatches;
3573 void Frame::setMarkedTextMatchesAreHighlighted(bool flag)
3575 if (flag == d->m_highlightTextMatches)
3578 d->m_highlightTextMatches = flag;
3579 document()->repaintMarkers(DocumentMarker::TextMatch);
3582 void Frame::prepareForUserAction()
3584 // Reset the multiple form submission protection code.
3585 // We'll let you submit the same form twice if you do two separate user actions.
3586 d->m_submittedFormURL = KURL();
3589 Node *Frame::mousePressNode()
3591 return d->m_mousePressNode.get();
3594 bool Frame::isComplete() const
3596 return d->m_bComplete;
3599 bool Frame::isLoadingMainResource() const
3601 return d->m_bLoadingMainResource;
3604 FrameTree* Frame::tree() const
3606 return &d->m_treeNode;
3609 DOMWindow* Frame::domWindow() const
3611 if (!d->m_domWindow)
3612 d->m_domWindow = new DOMWindow(const_cast<Frame*>(this));
3614 return d->m_domWindow.get();
3617 KURL Frame::url() const
3622 void Frame::startRedirectionTimer()
3624 d->m_redirectionTimer.startOneShot(d->m_delayRedirect);
3627 void Frame::stopRedirectionTimer()
3629 d->m_redirectionTimer.stop();
3632 void Frame::frameDetached()
3636 void Frame::detachChildren()
3638 // FIXME: is it really necessary to do this in reverse order any more?
3639 while (Frame* child = tree()->lastChild())
3640 tree()->removeChild(child);
3643 void Frame::updateBaseURLForEmptyDocument()
3645 Element* owner = ownerElement();
3646 // FIXME: Should embed be included?
3647 if (owner && (owner->hasTagName(iframeTag) || owner->hasTagName(objectTag) || owner->hasTagName(embedTag)))
3648 d->m_doc->setBaseURL(tree()->parent()->d->m_doc->baseURL());
3651 Page* Frame::page() const
3656 void Frame::pageDestroyed()
3660 // This will stop any JS timers
3661 if (d->m_jscript && d->m_jscript->haveInterpreter())
3662 if (Window* w = Window::retrieveWindow(this))
3663 w->disconnectFrame();
3666 void Frame::completed(bool complete)
3669 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
3670 child->parentCompleted();
3671 if (Frame* parent = tree()->parent())
3672 parent->childCompleted(complete);
3677 void Frame::setStatusBarText(const String&)
3681 void Frame::started()
3683 for (Frame* frame = this; frame; frame = frame->tree()->parent())
3684 frame->d->m_bComplete = false;
3687 void Frame::disconnectOwnerElement()
3689 if (d->m_ownerElement && d->m_page)
3690 d->m_page->decrementFrameCount();
3692 d->m_ownerElement = 0;
3695 String Frame::documentTypeString() const
3697 if (Document *doc = document())
3698 if (DocumentType *doctype = doc->realDocType())
3699 return doctype->toString();
3704 bool Frame::containsPlugins() const
3706 return d->m_plugins.size() != 0;
3709 bool Frame::prohibitsScrolling() const
3711 return d->m_prohibitsScrolling;
3714 void Frame::setProhibitsScrolling(const bool prohibit)
3716 d->m_prohibitsScrolling = prohibit;
3719 } // namespace WebCore