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 "RenderPart.h"
68 #include "RenderTextControl.h"
69 #include "RenderTheme.h"
70 #include "RenderView.h"
71 #include "SegmentedString.h"
72 #include "TextDocument.h"
73 #include "TextIterator.h"
74 #include "TypingCommand.h"
75 #include "XMLTokenizer.h"
76 #include "cssstyleselector.h"
77 #include "htmlediting.h"
78 #include "kjs_window.h"
80 #include "visible_units.h"
81 #include "xmlhttprequest.h"
83 #include <sys/types.h>
84 #include <wtf/Platform.h>
96 #include "XLinkNames.h"
98 #include "SVGDocument.h"
99 #include "SVGDocumentExtensions.h"
107 using KJS::PausedTimeouts;
108 using KJS::SavedProperties;
109 using KJS::SavedBuiltins;
115 using namespace EventNames;
116 using namespace HTMLNames;
118 const double caretBlinkFrequency = 0.5;
119 const double autoscrollInterval = 0.1;
121 class UserStyleSheetLoader : public CachedResourceClient {
123 UserStyleSheetLoader(Frame* frame, const String& url, DocLoader* dl)
125 , m_cachedSheet(Cache::requestStyleSheet(dl, url, false, 0, ""))
127 m_cachedSheet->ref(this);
129 ~UserStyleSheetLoader()
131 m_cachedSheet->deref(this);
134 virtual void setStyleSheet(const String& /*URL*/, const String& sheet)
136 m_frame->setUserStyleSheet(sheet);
139 CachedCSSStyleSheet* m_cachedSheet;
143 struct FrameCounter {
145 ~FrameCounter() { if (count != 0) fprintf(stderr, "LEAK: %d Frame\n", count); }
147 int FrameCounter::count = 0;
148 static FrameCounter frameCounter;
151 static inline Frame* parentFromOwnerElement(Element* ownerElement)
155 return ownerElement->document()->frame();
158 Frame::Frame(Page* page, Element* ownerElement)
159 : d(new FramePrivate(page, parentFromOwnerElement(ownerElement), this, ownerElement))
161 AtomicString::init();
165 QualifiedName::init();
166 MediaFeatureNames::init();
174 if (d->m_ownerElement)
175 d->m_page->incrementFrameCount();
177 // FIXME: Frames were originally created with a refcount of 1, leave this
178 // ref call here until we can straighten that out.
181 ++FrameCounter::count;
187 ASSERT(!d->m_lifeSupportTimer.isActive());
190 --FrameCounter::count;
195 if (d->m_jscript && d->m_jscript->haveInterpreter())
196 if (Window* w = Window::retrieveWindow(this)) {
197 w->disconnectFrame();
198 // Must clear the window pointer, otherwise we will not
199 // garbage-collect collect the window (inside the call to
204 disconnectOwnerElement();
207 d->m_domWindow->disconnectFrame();
210 HashSet<Frame*> openedBy = d->m_openedFrames;
211 HashSet<Frame*>::iterator end = openedBy.end();
212 for (HashSet<Frame*>::iterator it = openedBy.begin(); it != end; ++it)
217 d->m_view->m_frame = 0;
220 ASSERT(!d->m_lifeSupportTimer.isActive());
222 delete d->m_userStyleSheetLoader;
227 KURL Frame::iconURL()
229 // If this isn't a top level frame, return nothing
230 if (tree() && tree()->parent())
233 // If we have an iconURL from a Link element, return that
234 if (document() && !document()->iconURL().isEmpty())
235 return KURL(document()->iconURL().deprecatedString());
237 // Don't return a favicon iconURL unless we're http or https
238 if (d->m_url.protocol() != "http" && d->m_url.protocol() != "https")
242 url.setProtocol(d->m_url.protocol());
243 url.setHost(d->m_url.host());
244 url.setPath("/favicon.ico");
248 bool Frame::didOpenURL(const KURL& url)
250 if (d->m_scheduledRedirection == locationChangeScheduledDuringLoad) {
251 // A redirect was shceduled before the document was created. This can happen
252 // when one frame changes another frame's location.
258 // clear last edit command
259 d->m_lastEditCommand = 0;
263 d->m_bComplete = false;
264 d->m_bLoadingMainResource = true;
265 d->m_bLoadEventEmitted = false;
267 d->m_kjsStatusBarText = String();
268 d->m_kjsDefaultStatusBarText = String();
270 d->m_bJScriptEnabled = d->m_settings->isJavaScriptEnabled();
271 d->m_bJavaEnabled = d->m_settings->isJavaEnabled();
272 d->m_bPluginsEnabled = d->m_settings->isPluginsEnabled();
274 // 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
275 // data arrives) (Simon)
277 if (d->m_url.protocol().startsWith("http") && !d->m_url.host().isEmpty() && d->m_url.path().isEmpty())
278 d->m_url.setPath("/");
279 d->m_workingURL = d->m_url;
286 void Frame::didExplicitOpen()
288 d->m_bComplete = false;
289 d->m_bLoadEventEmitted = false;
291 // Prevent window.open(url) -- eg window.open("about:blank") -- from blowing away results
292 // from a subsequent window.document.open / window.document.write call.
293 // Cancelling redirection here works for all cases because document.open
294 // implicitly precedes document.write.
296 d->m_url = d->m_doc->URL();
299 void Frame::stopLoading(bool sendUnload)
301 if (d->m_doc && d->m_doc->tokenizer())
302 d->m_doc->tokenizer()->stopParsing();
304 d->m_metaData.clear();
308 if (d->m_bLoadEventEmitted && !d->m_bUnloadEventEmitted) {
309 Node* currentFocusNode = d->m_doc->focusNode();
310 if (currentFocusNode)
311 currentFocusNode->aboutToUnload();
312 d->m_doc->dispatchWindowEvent(unloadEvent, false, false);
314 d->m_doc->updateRendering();
315 d->m_bUnloadEventEmitted = true;
319 if (d->m_doc && !d->m_doc->inPageCache())
320 d->m_doc->removeAllEventListenersFromAllNodes();
323 d->m_bComplete = true; // to avoid calling completed() in finishedParsing() (David)
324 d->m_bLoadingMainResource = false;
325 d->m_bLoadEventEmitted = true; // don't want that one either
326 d->m_cachePolicy = CachePolicyVerify; // Why here?
328 if (d->m_doc && d->m_doc->parsing()) {
330 d->m_doc->setParsing(false);
333 d->m_workingURL = KURL();
335 if (Document *doc = d->m_doc.get()) {
336 if (DocLoader *docLoader = doc->docLoader())
337 Cache::loader()->cancelRequests(docLoader);
338 XMLHttpRequest::cancelRequests(doc);
341 // tell all subframes to stop as well
342 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
343 child->stopLoading(sendUnload);
345 d->m_bPendingChildRedirection = false;
350 BrowserExtension *Frame::browserExtension() const
352 return d->m_extension;
355 FrameView* Frame::view() const
357 return d->m_view.get();
360 void Frame::setView(FrameView* view)
365 bool Frame::jScriptEnabled() const
367 return d->m_bJScriptEnabled;
370 KJSProxy *Frame::jScript()
372 if (!d->m_bJScriptEnabled)
376 d->m_jscript = new KJSProxy(this);
381 static bool getString(JSValue* result, String& string)
387 if (!result->getString(ustring))
393 void Frame::replaceContentsWithScriptResult(const KURL& url)
395 JSValue* ret = executeScript(0, KURL::decode_string(url.url().mid(strlen("javascript:"))));
397 if (getString(ret, scriptResult)) {
404 JSValue* Frame::executeScript(Node* n, const String& script, bool forceUserGesture)
406 KJSProxy *proxy = jScript();
411 d->m_runningScripts++;
412 // If forceUserGesture is true, then make the script interpreter
413 // treat it as if triggered by a user gesture even if there is no
414 // current DOM event being processed.
415 JSValue* ret = proxy->evaluate(forceUserGesture ? DeprecatedString::null : d->m_url.url(), 0, script, n);
416 d->m_runningScripts--;
418 if (!d->m_runningScripts)
421 Document::updateDocumentsRendering();
426 bool Frame::javaEnabled() const
428 return d->m_settings->isJavaEnabled();
431 bool Frame::pluginsEnabled() const
433 return d->m_bPluginsEnabled;
436 void Frame::setAutoloadImages(bool enable)
438 if (d->m_doc && d->m_doc->docLoader()->autoloadImages() == enable)
442 d->m_doc->docLoader()->setAutoloadImages(enable);
445 bool Frame::autoloadImages() const
448 return d->m_doc->docLoader()->autoloadImages();
453 void Frame::cancelAndClear()
463 void Frame::clear(bool clearWindowProperties)
467 d->m_bCleared = true;
468 d->m_mousePressNode = 0;
471 // FIXME: This is a temporary hack to work around a mismatch between WebCore and WebKit
472 // regarding frame lifetime. The proper solution is to move all frame management
473 // into WebCore, so frames can work the same way on all platforms.
474 for (Frame* descendant = tree()->firstChild(); descendant; descendant = descendant->tree()->traverseNext())
475 descendant->disconnectOwnerElement();
479 d->m_doc->cancelParsing();
483 // Moving past doc so that onUnload works.
484 if (clearWindowProperties && d->m_jscript)
485 d->m_jscript->clear();
490 // do not drop the document before the jscript and view are cleared, as some destructors
491 // might still try to access the document.
495 d->m_plugins.clear();
496 cleanupPluginObjects();
498 d->m_scheduledRedirection = noRedirectionScheduled;
499 d->m_delayRedirect = 0;
500 d->m_redirectURL = DeprecatedString::null;
501 d->m_redirectReferrer = String();
502 d->m_redirectLockHistory = true;
503 d->m_redirectUserGesture = false;
504 d->m_bHTTPRefresh = false;
505 d->m_bFirstData = true;
507 d->m_bMousePressed = false;
509 if (!d->m_haveEncoding)
510 d->m_encoding = String();
513 Document *Frame::document() const
516 return d->m_doc.get();
520 void Frame::setDocument(Document* newDoc)
531 void Frame::receivedFirstData()
533 begin(d->m_workingURL);
535 d->m_doc->docLoader()->setCachePolicy(d->m_cachePolicy);
536 d->m_workingURL = KURL();
538 // When the first data arrives, the metadata has just been made available
539 DeprecatedString qData;
541 // Support for http-refresh
542 qData = d->m_metaData.get("http-refresh").deprecatedString();
543 if (!qData.isEmpty()) {
545 int pos = qData.find(';');
547 pos = qData.find(',');
550 delay = qData.stripWhiteSpace().toDouble();
551 // We want a new history item if the refresh timeout > 1 second
552 scheduleRedirection(delay, d->m_url.url(), delay <= 1);
554 int end_pos = qData.length();
555 delay = qData.left(pos).stripWhiteSpace().toDouble();
556 while (qData[++pos] == ' ');
557 if (qData.find("url", pos, false) == pos) {
559 while (qData[pos] == ' ' || qData[pos] == '=')
561 if (qData[pos] == '"') {
563 int index = end_pos-1;
564 while (index > pos) {
565 if (qData[index] == '"')
573 // We want a new history item if the refresh timeout > 1 second
574 scheduleRedirection(delay, d->m_doc->completeURL(qData.mid(pos, end_pos)), delay <= 1);
576 d->m_bHTTPRefresh = true;
579 // Support for http last-modified
580 d->m_lastModified = d->m_metaData.get("modified");
583 void Frame::childBegin()
585 // We need to do this when the child is created so as to avoid the parent thining the child
586 // is complete before it has even started loading.
587 // FIXME: do we really still need this?
588 d->m_bComplete = false;
591 void Frame::setResourceRequest(const ResourceRequest& request)
593 d->m_request = request;
596 const ResourceRequest& Frame::resourceRequest() const
601 void Frame::begin(const KURL& url)
603 if (d->m_workingURL.isEmpty())
604 createEmptyDocument(); // Creates an empty document if we don't have one already
607 partClearedInBegin();
609 d->m_bCleared = false;
610 d->m_bComplete = false;
611 d->m_bLoadEventEmitted = false;
612 d->m_bLoadingMainResource = true;
615 ref.setUser(DeprecatedString());
616 ref.setPass(DeprecatedString());
617 ref.setRef(DeprecatedString());
618 d->m_referrer = ref.url();
622 // We don't need KDE chained URI handling or window caption setting
623 if (!d->m_url.isEmpty())
627 if (d->m_request.m_responseMIMEType == "image/svg+xml")
628 d->m_doc = DOMImplementation::instance()->createSVGDocument(d->m_view.get());
631 if (DOMImplementation::isXMLMIMEType(d->m_request.m_responseMIMEType))
632 d->m_doc = DOMImplementation::instance()->createDocument(d->m_view.get());
633 else if (DOMImplementation::isTextMIMEType(d->m_request.m_responseMIMEType))
634 d->m_doc = new TextDocument(DOMImplementation::instance(), d->m_view.get());
635 else if ((d->m_request.m_responseMIMEType == "application/pdf" || d->m_request.m_responseMIMEType == "text/pdf") && PlugInInfoStore::supportsMIMEType(d->m_request.m_responseMIMEType))
636 d->m_doc = new PluginDocument(DOMImplementation::instance(), d->m_view.get());
637 else if (Image::supportsType(d->m_request.m_responseMIMEType))
638 d->m_doc = new ImageDocument(DOMImplementation::instance(), d->m_view.get());
639 else if (PlugInInfoStore::supportsMIMEType(d->m_request.m_responseMIMEType))
640 d->m_doc = new PluginDocument(DOMImplementation::instance(), d->m_view.get());
641 else if (inViewSourceMode())
642 d->m_doc = new HTMLViewSourceDocument(DOMImplementation::instance(), d->m_view.get());
644 d->m_doc = DOMImplementation::instance()->createHTMLDocument(d->m_view.get());
646 if (!d->m_doc->attached())
648 d->m_doc->setURL(d->m_url.url());
649 // We prefer m_baseURL over d->m_url because d->m_url changes when we are
650 // about to load a new page.
651 d->m_doc->setBaseURL(baseurl.url());
653 d->m_doc->setDecoder(d->m_decoder.get());
655 updatePolicyBaseURL();
657 setAutoloadImages(d->m_settings->autoLoadImages());
658 const KURL& userStyleSheet = d->m_settings->userStyleSheetLocation();
660 if (!userStyleSheet.isEmpty())
661 setUserStyleSheetLocation(KURL(userStyleSheet));
663 restoreDocumentState();
665 d->m_doc->implicitOpen();
668 d->m_view->resizeContents(0, 0);
671 void Frame::write(const char* str, int len)
679 if (Tokenizer* t = d->m_doc->tokenizer()) {
680 if (t->wantsRawData()) {
681 t->writeRawData(str, len);
687 d->m_decoder = new Decoder(d->m_request.m_responseMIMEType, settings()->encoding());
688 if (!d->m_encoding.isNull())
689 d->m_decoder->setEncoding(d->m_encoding,
690 d->m_haveEncoding ? Decoder::UserChosenEncoding : Decoder::EncodingFromHTTPHeader);
692 d->m_doc->setDecoder(d->m_decoder.get());
695 String decoded = d->m_decoder->decode(str, len);
697 if (decoded.isEmpty())
700 if (d->m_bFirstData) {
701 d->m_bFirstData = false;
702 d->m_doc->determineParseMode(decoded);
703 if (d->m_decoder->encoding().usesVisualOrdering())
704 d->m_doc->setVisuallyOrdered();
705 d->m_doc->recalcStyle(Node::Force);
708 if (Tokenizer* t = d->m_doc->tokenizer()) {
709 ASSERT(!t->wantsRawData());
710 t->write(decoded, true);
714 void Frame::write(const String& str)
719 if (d->m_bFirstData) {
720 // determine the parse mode
721 d->m_doc->setParseMode(Document::Strict);
722 d->m_bFirstData = false;
724 Tokenizer* t = d->m_doc->tokenizer();
731 d->m_bLoadingMainResource = false;
735 void Frame::endIfNotLoading()
737 // http://bugzilla.opendarwin.org/show_bug.cgi?id=10854
738 // The frame's last ref may be remove and it be deleted by checkCompleted(),
739 // so we'll add a protective refcount
740 RefPtr<Frame> protector(this);
742 if (d->m_bLoadingMainResource)
745 // make sure nothing's left in there...
748 String decoded = d->m_decoder->flush();
749 if (d->m_bFirstData) {
750 d->m_bFirstData = false;
751 d->m_doc->determineParseMode(decoded);
755 d->m_doc->finishParsing();
757 // WebKit partially uses WebCore when loading non-HTML docs. In these cases doc==nil, but
758 // WebCore is enough involved that we need to checkCompleted() in order for m_bComplete to
759 // become true. An example is when a subframe is a pure text doc, and that subframe is the
760 // last one to complete.
763 // FIXME - Right now, we kick off the icon loader when the frame is done receiving all its main resource.
764 // We could kick off the icon loader after we're done parsing the HEAD element, if that becomes convinient to find
767 // Don't load an icon if -
768 // 1) This is not the main frame
769 // 2) The database is disabled
770 // 3) We have no valid icon URL
771 // 4) We already have an unexpired icon
773 if (tree()->parent())
777 if (IconDatabase* sharedIconDatabase = IconDatabase::sharedIconDatabase()) {
778 if (!sharedIconDatabase->enabled())
781 String url(iconURL().url());
785 // If we already have an unexpired icon, we won't kick off a load but we *will* map the appropriate URLs to it
786 if (sharedIconDatabase->hasEntryForIconURL(url) && !isLoadTypeReload() && !sharedIconDatabase->isIconExpiredForIconURL(url)) {
787 commitIconURLToIconDatabase();
791 if (!d->m_iconLoader)
792 d->m_iconLoader = IconLoader::createForFrame(this);
793 d->m_iconLoader->startLoading();
797 void Frame::commitIconURLToIconDatabase()
799 KURL icon = iconURL();
801 IconDatabase* iconDatabase = IconDatabase::sharedIconDatabase();
802 ASSERT(iconDatabase);
803 iconDatabase->setIconURLForPageURL(icon.url(), this->url().url());
804 iconDatabase->setIconURLForPageURL(icon.url(), originalRequestURL().url());
809 // http://bugzilla.opendarwin.org/show_bug.cgi?id=10854
810 // The frame's last ref may be remove and it be deleted by checkCompleted(),
811 // so we'll add a protective refcount
812 RefPtr<Frame> protector(this);
815 if (d->m_doc->tokenizer())
816 d->m_doc->tokenizer()->stopParsing();
817 d->m_doc->finishParsing();
819 // WebKit partially uses WebCore when loading non-HTML docs. In these cases doc==nil, but
820 // WebCore is enough involved that we need to checkCompleted() in order for m_bComplete to
821 // become true. An example is when a subframe is a pure text doc, and that subframe is the
822 // last one to complete.
825 d->m_iconLoader->stopLoading();
828 void Frame::gotoAnchor()
830 // If our URL has no ref, then we have no place we need to jump to.
831 if (!d->m_url.hasRef())
834 DeprecatedString ref = d->m_url.encodedHtmlRef();
835 if (!gotoAnchor(ref)) {
836 // Can't use htmlRef() here because it doesn't know which encoding to use to decode.
837 // Decoding here has to match encoding in completeURL, which means it has to use the
838 // page's encoding rather than UTF-8.
840 gotoAnchor(KURL::decode_string(ref, d->m_decoder->encoding()));
844 void Frame::finishedParsing()
846 RefPtr<Frame> protector(this);
850 return; // We are being destroyed by something checkCompleted called.
852 // check if the scrollbars are really needed for the content
853 // if not, remove them, relayout, and repaint
855 d->m_view->restoreScrollBar();
859 void Frame::loadDone()
865 void Frame::checkCompleted()
867 // Any frame that hasn't completed yet ?
868 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
869 if (!child->d->m_bComplete)
872 // Have we completed before?
876 // Are we still parsing?
877 if (d->m_doc && d->m_doc->parsing())
880 // Still waiting for images/scripts from the loader ?
882 if (d->m_doc && d->m_doc->docLoader())
883 requests = Cache::loader()->numRequests(d->m_doc->docLoader());
889 // Now do what should be done when we are really completed.
890 d->m_bComplete = true;
892 checkEmitLoadEvent(); // if we didn't do it before
894 if (d->m_scheduledRedirection != noRedirectionScheduled) {
895 // Do not start redirection for frames here! That action is
896 // deferred until the parent emits a completed signal.
897 if (!tree()->parent())
898 startRedirectionTimer();
902 completed(d->m_bPendingChildRedirection);
906 void Frame::checkEmitLoadEvent()
908 if (d->m_bLoadEventEmitted || !d->m_doc || d->m_doc->parsing())
911 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
912 if (!child->d->m_bComplete) // still got a frame running -> too early
915 // All frames completed -> set their domain to the frameset's domain
916 // This must only be done when loading the frameset initially (#22039),
917 // not when following a link in a frame (#44162).
919 String domain = d->m_doc->domain();
920 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
922 child->d->m_doc->setDomain(domain);
925 d->m_bLoadEventEmitted = true;
926 d->m_bUnloadEventEmitted = false;
928 d->m_doc->implicitClose();
931 const Settings *Frame::settings() const
933 return d->m_settings;
936 KURL Frame::baseURL() const
940 return d->m_doc->baseURL();
943 String Frame::baseTarget() const
947 return d->m_doc->baseTarget();
950 KURL Frame::completeURL(const DeprecatedString& url)
955 return KURL(d->m_doc->completeURL(url));
958 void Frame::scheduleRedirection(double delay, const DeprecatedString& url, bool doLockHistory)
960 if (delay < 0 || delay > INT_MAX / 1000)
962 if (d->m_scheduledRedirection == noRedirectionScheduled || delay <= d->m_delayRedirect)
964 d->m_scheduledRedirection = redirectionScheduled;
965 d->m_delayRedirect = delay;
966 d->m_redirectURL = url;
967 d->m_redirectReferrer = String();
968 d->m_redirectLockHistory = doLockHistory;
969 d->m_redirectUserGesture = false;
971 stopRedirectionTimer();
973 startRedirectionTimer();
977 void Frame::scheduleLocationChange(const DeprecatedString& url, const String& referrer, bool lockHistory, bool userGesture)
981 // If the URL we're going to navigate to is the same as the current one, except for the
982 // fragment part, we don't need to schedule the location change.
983 if (u.hasRef() && equalIgnoringRef(d->m_url, u)) {
984 changeLocation(url, referrer, lockHistory, userGesture);
988 // Handle a location change of a page with no document as a special case.
989 // This may happen when a frame changes the location of another frame.
990 d->m_scheduledRedirection = d->m_doc ? locationChangeScheduled : locationChangeScheduledDuringLoad;
992 // If a redirect was scheduled during a load, then stop the current load.
993 // Otherwise when the current load transitions from a provisional to a
994 // committed state, pending redirects may be cancelled.
995 if (d->m_scheduledRedirection == locationChangeScheduledDuringLoad) {
999 d->m_delayRedirect = 0;
1000 d->m_redirectURL = url;
1001 d->m_redirectReferrer = referrer;
1002 d->m_redirectLockHistory = lockHistory;
1003 d->m_redirectUserGesture = userGesture;
1004 stopRedirectionTimer();
1006 startRedirectionTimer();
1009 void Frame::scheduleRefresh(bool userGesture)
1011 // Handle a location change of a page with no document as a special case.
1012 // This may happen when a frame requests a refresh of another frame.
1013 d->m_scheduledRedirection = d->m_doc ? locationChangeScheduled : locationChangeScheduledDuringLoad;
1015 // If a refresh was scheduled during a load, then stop the current load.
1016 // Otherwise when the current load transitions from a provisional to a
1017 // committed state, pending redirects may be cancelled.
1018 if (d->m_scheduledRedirection == locationChangeScheduledDuringLoad)
1021 d->m_delayRedirect = 0;
1022 d->m_redirectURL = url().url();
1023 d->m_redirectReferrer = referrer();
1024 d->m_redirectLockHistory = true;
1025 d->m_redirectUserGesture = userGesture;
1026 d->m_cachePolicy = CachePolicyRefresh;
1027 stopRedirectionTimer();
1029 startRedirectionTimer();
1032 bool Frame::isScheduledLocationChangePending() const
1034 switch (d->m_scheduledRedirection) {
1035 case noRedirectionScheduled:
1036 case redirectionScheduled:
1038 case historyNavigationScheduled:
1039 case locationChangeScheduled:
1040 case locationChangeScheduledDuringLoad:
1046 void Frame::scheduleHistoryNavigation(int steps)
1048 // navigation will always be allowed in the 0 steps case, which is OK because
1049 // that's supposed to force a reload.
1050 if (!canGoBackOrForward(steps)) {
1051 cancelRedirection();
1055 // If the URL we're going to navigate to is the same as the current one, except for the
1056 // fragment part, we don't need to schedule the navigation.
1057 if (d->m_extension) {
1058 KURL u = d->m_extension->historyURL(steps);
1060 if (equalIgnoringRef(d->m_url, u)) {
1061 d->m_extension->goBackOrForward(steps);
1066 d->m_scheduledRedirection = historyNavigationScheduled;
1067 d->m_delayRedirect = 0;
1068 d->m_redirectURL = DeprecatedString::null;
1069 d->m_redirectReferrer = String();
1070 d->m_scheduledHistoryNavigationSteps = steps;
1071 stopRedirectionTimer();
1073 startRedirectionTimer();
1076 void Frame::cancelRedirection(bool cancelWithLoadInProgress)
1079 d->m_cancelWithLoadInProgress = cancelWithLoadInProgress;
1080 d->m_scheduledRedirection = noRedirectionScheduled;
1081 stopRedirectionTimer();
1085 void Frame::changeLocation(const DeprecatedString& URL, const String& referrer, bool lockHistory, bool userGesture)
1087 if (URL.find("javascript:", 0, false) == 0) {
1088 String script = KURL::decode_string(URL.mid(11));
1089 JSValue* result = executeScript(0, script, userGesture);
1090 String scriptResult;
1091 if (getString(result, scriptResult)) {
1093 write(scriptResult);
1099 ResourceRequest request(completeURL(URL));
1100 request.setLockHistory(lockHistory);
1101 if (!referrer.isEmpty())
1102 request.setReferrer(referrer);
1104 request.reload = (d->m_cachePolicy == CachePolicyReload) || (d->m_cachePolicy == CachePolicyRefresh);
1106 urlSelected(request, "_self");
1109 void Frame::redirectionTimerFired(Timer<Frame>*)
1111 if (d->m_scheduledRedirection == historyNavigationScheduled) {
1112 d->m_scheduledRedirection = noRedirectionScheduled;
1114 // Special case for go(0) from a frame -> reload only the frame
1115 // go(i!=0) from a frame navigates into the history of the frame only,
1116 // in both IE and NS (but not in Mozilla).... we can't easily do that
1118 if (d->m_scheduledHistoryNavigationSteps == 0) // add && parent() to get only frames, but doesn't matter
1119 openURL(url()); /// ## need args.reload=true?
1121 if (d->m_extension) {
1122 d->m_extension->goBackOrForward(d->m_scheduledHistoryNavigationSteps);
1128 DeprecatedString URL = d->m_redirectURL;
1129 String referrer = d->m_redirectReferrer;
1130 bool lockHistory = d->m_redirectLockHistory;
1131 bool userGesture = d->m_redirectUserGesture;
1133 d->m_scheduledRedirection = noRedirectionScheduled;
1134 d->m_delayRedirect = 0;
1135 d->m_redirectURL = DeprecatedString::null;
1136 d->m_redirectReferrer = String();
1138 changeLocation(URL, referrer, lockHistory, userGesture);
1141 String Frame::encoding() const
1143 if (d->m_haveEncoding && !d->m_encoding.isEmpty())
1144 return d->m_encoding;
1145 if (d->m_decoder && d->m_decoder->encoding().isValid())
1146 return d->m_decoder->encoding().name();
1147 return settings()->encoding();
1150 void Frame::setUserStyleSheetLocation(const KURL& url)
1152 delete d->m_userStyleSheetLoader;
1153 d->m_userStyleSheetLoader = 0;
1154 if (d->m_doc && d->m_doc->docLoader())
1155 d->m_userStyleSheetLoader = new UserStyleSheetLoader(this, url.url(), d->m_doc->docLoader());
1158 void Frame::setUserStyleSheet(const String& styleSheet)
1160 delete d->m_userStyleSheetLoader;
1161 d->m_userStyleSheetLoader = 0;
1163 d->m_doc->setUserStyleSheet(styleSheet);
1166 bool Frame::gotoAnchor(const String& name)
1171 Node *n = d->m_doc->getElementById(AtomicString(name));
1173 n = d->m_doc->anchors()->namedItem(name, !d->m_doc->inCompatMode());
1175 d->m_doc->setCSSTarget(n); // Setting to null will clear the current target.
1177 // Implement the rule that "" and "top" both mean top of page as in other browsers.
1178 if (!n && !(name.isEmpty() || name.lower() == "top"))
1181 // We need to update the layout before scrolling, otherwise we could
1182 // really mess things up if an anchor scroll comes at a bad moment.
1184 d->m_doc->updateRendering();
1185 // Only do a layout if changes have occurred that make it necessary.
1186 if (d->m_view && d->m_doc->renderer() && d->m_doc->renderer()->needsLayout()) {
1187 d->m_view->layout();
1191 // Scroll nested layers and frames to reveal the anchor.
1192 RenderObject *renderer;
1195 renderer = n->renderer();
1196 rect = n->getRect();
1198 // If there's no node, we should scroll to the top of the document.
1199 renderer = d->m_doc->renderer();
1204 // Align to the top and to the closest side (this matches other browsers).
1205 renderer->enclosingLayer()->scrollRectToVisible(rect, RenderLayer::gAlignToEdgeIfNeeded, RenderLayer::gAlignTopAlways);
1211 void Frame::setStandardFont(const String& name)
1213 d->m_settings->setStdFontName(AtomicString(name));
1216 void Frame::setFixedFont(const String& name)
1218 d->m_settings->setFixedFontName(AtomicString(name));
1221 String Frame::selectedText() const
1223 return plainText(selectionController()->toRange().get());
1226 bool Frame::hasSelection() const
1228 return selectionController()->isCaretOrRange();
1231 SelectionController* Frame::selectionController() const
1233 return &(d->m_selectionController);
1236 CommandByName* Frame::command() const
1238 return &(d->m_command);
1241 TextGranularity Frame::selectionGranularity() const
1243 return d->m_selectionGranularity;
1246 void Frame::setSelectionGranularity(TextGranularity granularity) const
1248 d->m_selectionGranularity = granularity;
1251 SelectionController* Frame::dragCaretController() const
1253 return d->m_page->dragCaretController();
1256 const Selection& Frame::mark() const
1261 void Frame::setMark(const Selection& s)
1263 ASSERT(!s.base().node() || s.base().node()->document() == document());
1264 ASSERT(!s.extent().node() || s.extent().node()->document() == document());
1265 ASSERT(!s.start().node() || s.start().node()->document() == document());
1266 ASSERT(!s.end().node() || s.end().node()->document() == document());
1271 void Frame::notifyRendererOfSelectionChange(bool userTriggered)
1273 RenderObject* renderer = 0;
1274 if (selectionController()->rootEditableElement())
1275 renderer = selectionController()->rootEditableElement()->shadowAncestorNode()->renderer();
1277 // If the current selection is in a textfield or textarea, notify the renderer that the selection has changed
1278 if (renderer && (renderer->isTextArea() || renderer->isTextField()))
1279 static_cast<RenderTextControl*>(renderer)->selectionChanged(userTriggered);
1282 void Frame::invalidateSelection()
1284 clearCaretRectIfNeeded();
1285 selectionController()->setNeedsLayout();
1286 selectionLayoutChanged();
1289 void Frame::setCaretVisible(bool flag)
1291 if (d->m_caretVisible == flag)
1293 clearCaretRectIfNeeded();
1295 setFocusNodeIfNeeded();
1296 d->m_caretVisible = flag;
1297 selectionLayoutChanged();
1301 void Frame::clearCaretRectIfNeeded()
1303 if (d->m_caretPaint) {
1304 d->m_caretPaint = false;
1305 selectionController()->needsCaretRepaint();
1309 // Helper function that tells whether a particular node is an element that has an entire
1310 // Frame and FrameView, a <frame>, <iframe>, or <object>.
1311 static bool isFrameElement(const Node *n)
1315 RenderObject *renderer = n->renderer();
1316 if (!renderer || !renderer->isWidget())
1318 Widget* widget = static_cast<RenderWidget*>(renderer)->widget();
1319 return widget && widget->isFrameView();
1322 void Frame::setFocusNodeIfNeeded()
1324 if (!document() || selectionController()->isNone() || !d->m_isActive)
1327 Node* target = selectionController()->rootEditableElement();
1329 RenderObject* renderer = target->renderer();
1331 // Walk up the render tree to search for a node to focus.
1332 // Walking up the DOM tree wouldn't work for shadow trees, like those behind the engine-based text fields.
1334 // We don't want to set focus on a subframe when selecting in a parent frame,
1335 // so add the !isFrameElement check here. There's probably a better way to make this
1336 // work in the long term, but this is the safest fix at this time.
1337 if (target && target->isMouseFocusable() && !isFrameElement(target)) {
1338 document()->setFocusNode(target);
1341 renderer = renderer->parent();
1343 target = renderer->element();
1345 document()->setFocusNode(0);
1349 void Frame::selectionLayoutChanged()
1351 // kill any caret blink timer now running
1352 d->m_caretBlinkTimer.stop();
1354 // see if a new caret blink timer needs to be started
1355 if (d->m_caretVisible && d->m_caretBlinks &&
1356 selectionController()->isCaret() && selectionController()->start().node()->isContentEditable()) {
1357 d->m_caretBlinkTimer.startRepeating(caretBlinkFrequency);
1358 d->m_caretPaint = true;
1359 selectionController()->needsCaretRepaint();
1363 d->m_doc->updateSelection();
1366 void Frame::setXPosForVerticalArrowNavigation(int x)
1368 d->m_xPosForVerticalArrowNavigation = x;
1371 int Frame::xPosForVerticalArrowNavigation() const
1373 return d->m_xPosForVerticalArrowNavigation;
1376 void Frame::caretBlinkTimerFired(Timer<Frame>*)
1378 // Might be better to turn the timer off during some of these circumstances
1379 // and assert rather then letting the timer fire and do nothing here.
1380 // Could do that in selectionLayoutChanged.
1382 if (!d->m_caretVisible)
1384 if (!d->m_caretBlinks)
1386 if (!selectionController()->isCaret())
1388 bool caretPaint = d->m_caretPaint;
1389 if (d->m_bMousePressed && caretPaint)
1391 d->m_caretPaint = !caretPaint;
1392 selectionController()->needsCaretRepaint();
1395 void Frame::paintCaret(GraphicsContext* p, const IntRect& rect) const
1397 if (d->m_caretPaint)
1398 selectionController()->paintCaret(p, rect);
1401 void Frame::paintDragCaret(GraphicsContext* p, const IntRect& rect) const
1403 SelectionController* dragCaretController = d->m_page->dragCaretController();
1404 assert(dragCaretController->selection().isCaret());
1405 if (dragCaretController->selection().start().node()->document()->frame() == this)
1406 dragCaretController->paintCaret(p, rect);
1409 void Frame::urlSelected(const DeprecatedString& url, const String& target)
1411 urlSelected(ResourceRequest(completeURL(url)), target);
1414 void Frame::urlSelected(const ResourceRequest& request, const String& _target)
1416 String target = _target;
1417 if (target.isEmpty() && d->m_doc)
1418 target = d->m_doc->baseTarget();
1420 const KURL& url = request.url();
1422 if (url.url().startsWith("javascript:", false)) {
1423 executeScript(0, KURL::decode_string(url.url().mid(11)), true);
1428 // ### ERROR HANDLING
1431 ResourceRequest requestCopy = request;
1432 requestCopy.frameName = target;
1434 if (d->m_bHTTPRefresh)
1435 d->m_bHTTPRefresh = false;
1437 if (!d->m_referrer.isEmpty())
1438 requestCopy.setReferrer(d->m_referrer);
1440 urlSelected(requestCopy);
1443 bool Frame::requestFrame(Element* ownerElement, const String& urlParam, const AtomicString& frameName)
1445 DeprecatedString _url = urlParam.deprecatedString();
1446 // Support for <frame src="javascript:string">
1449 if (_url.startsWith("javascript:", false)) {
1451 url = "about:blank";
1453 url = completeURL(_url);
1455 Frame* frame = tree()->child(frameName);
1457 ResourceRequest request(url);
1458 request.setReferrer(d->m_referrer);
1459 request.reload = (d->m_cachePolicy == CachePolicyReload) || (d->m_cachePolicy == CachePolicyRefresh);
1460 frame->openURLRequest(request);
1462 frame = loadSubframe(ownerElement, url, frameName, d->m_referrer);
1467 if (!scriptURL.isEmpty())
1468 frame->replaceContentsWithScriptResult(scriptURL);
1473 bool Frame::requestObject(RenderPart* renderer, const String& url, const AtomicString& frameName,
1474 const String& mimeType, const Vector<String>& paramNames, const Vector<String>& paramValues)
1478 completedURL = completeURL(url.deprecatedString());
1480 if (url.isEmpty() && mimeType.isEmpty())
1484 if (shouldUsePlugin(renderer->element(), completedURL, mimeType, renderer->hasFallbackContent(), useFallback))
1485 return loadPlugin(renderer, completedURL, mimeType, paramNames, paramValues, useFallback);
1487 ASSERT(renderer->node()->hasTagName(objectTag) || renderer->node()->hasTagName(embedTag));
1488 AtomicString uniqueFrameName = tree()->uniqueChildName(frameName);
1489 static_cast<HTMLPlugInElement*>(renderer->node())->setFrameName(uniqueFrameName);
1491 // FIXME: ok to always make a new one? when does the old frame get removed?
1492 return loadSubframe(static_cast<Element*>(renderer->node()), completedURL, uniqueFrameName, d->m_referrer);
1495 bool Frame::shouldUsePlugin(Node* element, const KURL& url, const String& mimeType, bool hasFallback, bool& useFallback)
1497 useFallback = false;
1498 ObjectContentType objectType = objectContentType(url, mimeType);
1500 // if an object's content can't be handled and it has no fallback, let
1501 // it be handled as a plugin to show the broken plugin icon
1502 if (objectType == ObjectContentNone && hasFallback)
1505 return objectType == ObjectContentNone || objectType == ObjectContentPlugin;
1509 bool Frame::loadPlugin(RenderPart *renderer, const KURL& url, const String& mimeType,
1510 const Vector<String>& paramNames, const Vector<String>& paramValues, bool useFallback)
1513 checkEmitLoadEvent();
1517 Element *pluginElement;
1518 if (renderer && renderer->node() && renderer->node()->isElementNode())
1519 pluginElement = static_cast<Element*>(renderer->node());
1523 Plugin* plugin = createPlugin(pluginElement, url, paramNames, paramValues, mimeType);
1525 checkEmitLoadEvent();
1528 d->m_plugins.append(plugin);
1530 if (renderer && plugin->view())
1531 renderer->setWidget(plugin->view());
1533 checkEmitLoadEvent();
1538 Frame* Frame::loadSubframe(Element* ownerElement, const KURL& url, const String& name, const String& referrer)
1540 Frame* frame = createFrame(url, name, ownerElement, referrer);
1542 checkEmitLoadEvent();
1546 frame->childBegin();
1548 if (ownerElement->renderer() && frame->view())
1549 static_cast<RenderWidget*>(ownerElement->renderer())->setWidget(frame->view());
1551 checkEmitLoadEvent();
1553 // In these cases, the synchronous load would have finished
1554 // before we could connect the signals, so make sure to send the
1555 // completed() signal for the child by hand
1556 // FIXME: In this case the Frame will have finished loading before
1557 // it's being added to the child list. It would be a good idea to
1558 // create the child first, then invoke the loader separately.
1559 if (url.isEmpty() || url == "about:blank") {
1560 frame->completed(false);
1561 frame->checkCompleted();
1567 void Frame::clearRecordedFormValues()
1569 d->m_formAboutToBeSubmitted = 0;
1570 d->m_formValuesAboutToBeSubmitted.clear();
1573 void Frame::recordFormValue(const String& name, const String& value, PassRefPtr<HTMLFormElement> element)
1575 d->m_formAboutToBeSubmitted = element;
1576 d->m_formValuesAboutToBeSubmitted.set(name, value);
1579 void Frame::submitFormAgain()
1581 FramePrivate::SubmitForm* form = d->m_submitForm;
1582 d->m_submitForm = 0;
1583 if (d->m_doc && !d->m_doc->parsing() && form)
1584 submitForm(form->submitAction, form->submitUrl, form->submitFormData,
1585 form->target, form->submitContentType, form->submitBoundary);
1589 void Frame::submitForm(const char *action, const String& url, const FormData& formData, const String& _target, const String& contentType, const String& boundary)
1591 KURL u = completeURL(url.deprecatedString());
1594 // ### ERROR HANDLING!
1597 DeprecatedString urlstring = u.url();
1598 if (urlstring.startsWith("javascript:", false)) {
1599 urlstring = KURL::decode_string(urlstring);
1600 d->m_executingJavaScriptFormAction = true;
1601 executeScript(0, urlstring.mid(11));
1602 d->m_executingJavaScriptFormAction = false;
1606 ResourceRequest request;
1608 if (!d->m_referrer.isEmpty())
1609 request.setReferrer(d->m_referrer);
1611 request.frameName = _target.isEmpty() ? d->m_doc->baseTarget() : _target ;
1613 // Handle mailto: forms
1614 if (u.protocol() == "mailto") {
1615 // 1) Check for attach= and strip it
1616 DeprecatedString q = u.query().mid(1);
1617 DeprecatedStringList nvps = DeprecatedStringList::split("&", q);
1618 bool triedToAttach = false;
1620 for (DeprecatedStringList::Iterator nvp = nvps.begin(); nvp != nvps.end(); ++nvp) {
1621 DeprecatedStringList pair = DeprecatedStringList::split("=", *nvp);
1622 if (pair.count() >= 2) {
1623 if (pair.first().lower() == "attach") {
1624 nvp = nvps.remove(nvp);
1625 triedToAttach = true;
1632 DeprecatedString bodyEnc;
1633 if (contentType.lower() == "multipart/form-data")
1634 // FIXME: is this correct? I suspect not
1635 bodyEnc = KURL::encode_string(formData.flattenToString().deprecatedString());
1636 else if (contentType.lower() == "text/plain") {
1637 // Convention seems to be to decode, and s/&/\n/
1638 DeprecatedString tmpbody = formData.flattenToString().deprecatedString();
1639 tmpbody.replace('&', '\n');
1640 tmpbody.replace('+', ' ');
1641 tmpbody = KURL::decode_string(tmpbody); // Decode the rest of it
1642 bodyEnc = KURL::encode_string(tmpbody); // Recode for the URL
1644 bodyEnc = KURL::encode_string(formData.flattenToString().deprecatedString());
1646 nvps.append(String::sprintf("body=%s", bodyEnc.latin1()).deprecatedString());
1651 if (strcmp(action, "get") == 0) {
1652 if (u.protocol() != "mailto")
1653 u.setQuery(formData.flattenToString().deprecatedString());
1654 request.setDoPost(false);
1656 request.postData = formData;
1657 request.setDoPost(true);
1659 // construct some user headers if necessary
1660 if (contentType.isNull() || contentType == "application/x-www-form-urlencoded")
1661 request.setContentType("Content-Type: application/x-www-form-urlencoded");
1662 else // contentType must be "multipart/form-data"
1663 request.setContentType("Content-Type: " + contentType + "; boundary=" + boundary);
1666 if (d->m_doc->parsing() || d->m_runningScripts > 0) {
1667 if (d->m_submitForm)
1669 d->m_submitForm = new FramePrivate::SubmitForm;
1670 d->m_submitForm->submitAction = action;
1671 d->m_submitForm->submitUrl = url;
1672 d->m_submitForm->submitFormData = formData;
1673 d->m_submitForm->target = _target;
1674 d->m_submitForm->submitContentType = contentType;
1675 d->m_submitForm->submitBoundary = boundary;
1678 submitForm(request);
1682 void Frame::parentCompleted()
1684 if (d->m_scheduledRedirection != noRedirectionScheduled && !d->m_redirectionTimer.isActive())
1685 startRedirectionTimer();
1688 void Frame::childCompleted(bool complete)
1690 if (complete && !tree()->parent())
1691 d->m_bPendingChildRedirection = true;
1695 int Frame::zoomFactor() const
1697 return d->m_zoomFactor;
1700 void Frame::setZoomFactor(int percent)
1702 if (d->m_zoomFactor == percent)
1705 d->m_zoomFactor = percent;
1708 d->m_doc->recalcStyle(Node::Force);
1710 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
1711 child->setZoomFactor(d->m_zoomFactor);
1713 if (d->m_doc && d->m_doc->renderer() && d->m_doc->renderer()->needsLayout())
1717 void Frame::setJSStatusBarText(const String& text)
1719 d->m_kjsStatusBarText = text;
1720 setStatusBarText(d->m_kjsStatusBarText);
1723 void Frame::setJSDefaultStatusBarText(const String& text)
1725 d->m_kjsDefaultStatusBarText = text;
1726 setStatusBarText(d->m_kjsDefaultStatusBarText);
1729 String Frame::jsStatusBarText() const
1731 return d->m_kjsStatusBarText;
1734 String Frame::jsDefaultStatusBarText() const
1736 return d->m_kjsDefaultStatusBarText;
1739 String Frame::referrer() const
1741 return d->m_referrer;
1744 String Frame::lastModified() const
1746 return d->m_lastModified;
1749 void Frame::reparseConfiguration()
1751 setAutoloadImages(d->m_settings->autoLoadImages());
1753 d->m_bJScriptEnabled = d->m_settings->isJavaScriptEnabled();
1754 d->m_bJavaEnabled = d->m_settings->isJavaEnabled();
1755 d->m_bPluginsEnabled = d->m_settings->isPluginsEnabled();
1757 const KURL& userStyleSheetLocation = d->m_settings->userStyleSheetLocation();
1758 if (!userStyleSheetLocation.isEmpty())
1759 setUserStyleSheetLocation(userStyleSheetLocation);
1761 setUserStyleSheet(String());
1763 // FIXME: It's not entirely clear why the following is needed.
1764 // The document automatically does this as required when you set the style sheet.
1765 // But we had problems when this code was removed. Details are in
1766 // <http://bugzilla.opendarwin.org/show_bug.cgi?id=8079>.
1768 d->m_doc->updateStyleSelector();
1771 bool Frame::shouldDragAutoNode(Node *node, const IntPoint& point) const
1777 bool Frame::isPointInsideSelection(const IntPoint& point)
1779 // Treat a collapsed selection like no selection.
1780 if (!selectionController()->isRange())
1782 if (!document()->renderer())
1785 RenderObject::NodeInfo nodeInfo(true, true);
1786 document()->renderer()->layer()->hitTest(nodeInfo, point);
1787 Node *innerNode = nodeInfo.innerNode();
1788 if (!innerNode || !innerNode->renderer())
1791 Position pos(innerNode->renderer()->positionForPoint(point).deepEquivalent());
1795 Node *n = selectionController()->start().node();
1797 if (n == pos.node()) {
1798 if ((n == selectionController()->start().node() && pos.offset() < selectionController()->start().offset()) ||
1799 (n == selectionController()->end().node() && pos.offset() > selectionController()->end().offset())) {
1804 if (n == selectionController()->end().node())
1806 n = n->traverseNextNode();
1812 void Frame::selectClosestWordFromMouseEvent(const PlatformMouseEvent& mouse, Node *innerNode)
1814 Selection newSelection;
1816 if (innerNode && innerNode->renderer() && mouseDownMayStartSelect() && innerNode->renderer()->shouldSelect()) {
1817 IntPoint vPoint = view()->viewportToContents(mouse.pos());
1818 VisiblePosition pos(innerNode->renderer()->positionForPoint(vPoint));
1819 if (pos.isNotNull()) {
1820 newSelection = Selection(pos);
1821 newSelection.expandUsingGranularity(WordGranularity);
1825 if (newSelection.isRange()) {
1826 d->m_selectionGranularity = WordGranularity;
1827 d->m_beganSelectingText = true;
1830 if (shouldChangeSelection(newSelection))
1831 selectionController()->setSelection(newSelection);
1834 void Frame::handleMousePressEventDoubleClick(const MouseEventWithHitTestResults& event)
1836 if (event.event().button() == LeftButton) {
1837 if (selectionController()->isRange())
1838 // A double-click when range is already selected
1839 // should not change the selection. So, do not call
1840 // selectClosestWordFromMouseEvent, but do set
1841 // m_beganSelectingText to prevent handleMouseReleaseEvent
1842 // from setting caret selection.
1843 d->m_beganSelectingText = true;
1845 selectClosestWordFromMouseEvent(event.event(), event.targetNode());
1849 void Frame::handleMousePressEventTripleClick(const MouseEventWithHitTestResults& event)
1851 Node *innerNode = event.targetNode();
1853 if (event.event().button() == LeftButton && innerNode && innerNode->renderer() &&
1854 mouseDownMayStartSelect() && innerNode->renderer()->shouldSelect()) {
1855 Selection newSelection;
1856 IntPoint vPoint = view()->viewportToContents(event.event().pos());
1857 VisiblePosition pos(innerNode->renderer()->positionForPoint(vPoint));
1858 if (pos.isNotNull()) {
1859 newSelection = Selection(pos);
1860 newSelection.expandUsingGranularity(ParagraphGranularity);
1862 if (newSelection.isRange()) {
1863 d->m_selectionGranularity = ParagraphGranularity;
1864 d->m_beganSelectingText = true;
1867 if (shouldChangeSelection(newSelection))
1868 selectionController()->setSelection(newSelection);
1872 void Frame::handleMousePressEventSingleClick(const MouseEventWithHitTestResults& event)
1874 Node *innerNode = event.targetNode();
1876 if (event.event().button() == LeftButton) {
1877 if (innerNode && innerNode->renderer() &&
1878 mouseDownMayStartSelect() && innerNode->renderer()->shouldSelect()) {
1880 // Extend the selection if the Shift key is down, unless the click is in a link.
1881 bool extendSelection = event.event().shiftKey() && !event.isOverLink();
1883 // Don't restart the selection when the mouse is pressed on an
1884 // existing selection so we can allow for text dragging.
1885 IntPoint vPoint = view()->viewportToContents(event.event().pos());
1886 if (!extendSelection && isPointInsideSelection(vPoint))
1889 VisiblePosition visiblePos(innerNode->renderer()->positionForPoint(vPoint));
1890 if (visiblePos.isNull())
1891 visiblePos = VisiblePosition(innerNode, innerNode->caretMinOffset(), DOWNSTREAM);
1892 Position pos = visiblePos.deepEquivalent();
1894 Selection newSelection = selectionController()->selection();
1895 if (extendSelection && newSelection.isCaretOrRange()) {
1896 selectionController()->clearModifyBias();
1898 // See <rdar://problem/3668157> REGRESSION (Mail): shift-click deselects when selection
1899 // was created right-to-left
1900 Position start = newSelection.start();
1901 Position end = newSelection.end();
1902 short before = Range::compareBoundaryPoints(pos.node(), pos.offset(), start.node(), start.offset());
1904 newSelection = Selection(pos, end);
1906 newSelection = Selection(start, pos);
1908 if (d->m_selectionGranularity != CharacterGranularity)
1909 newSelection.expandUsingGranularity(d->m_selectionGranularity);
1910 d->m_beganSelectingText = true;
1912 newSelection = Selection(visiblePos);
1913 d->m_selectionGranularity = CharacterGranularity;
1916 if (shouldChangeSelection(newSelection))
1917 selectionController()->setSelection(newSelection);
1922 void Frame::handleMousePressEvent(const MouseEventWithHitTestResults& event)
1924 Node *innerNode = event.targetNode();
1926 d->m_mousePressNode = innerNode;
1927 d->m_dragStartPos = event.event().pos();
1929 if (event.event().button() == LeftButton || event.event().button() == MiddleButton) {
1930 d->m_bMousePressed = true;
1931 d->m_beganSelectingText = false;
1933 if (event.event().clickCount() == 2) {
1934 handleMousePressEventDoubleClick(event);
1937 if (event.event().clickCount() >= 3) {
1938 handleMousePressEventTripleClick(event);
1941 handleMousePressEventSingleClick(event);
1945 void Frame::handleMouseMoveEvent(const MouseEventWithHitTestResults& event)
1947 // Mouse not pressed. Do nothing.
1948 if (!d->m_bMousePressed)
1951 Node *innerNode = event.targetNode();
1953 if (event.event().button() != 0 || !innerNode || !innerNode->renderer() || !mouseDownMayStartSelect() || !innerNode->renderer()->shouldSelect())
1956 // handle making selection
1957 IntPoint vPoint = view()->viewportToContents(event.event().pos());
1958 VisiblePosition pos(innerNode->renderer()->positionForPoint(vPoint));
1960 // Don't modify the selection if we're not on a node.
1964 // Restart the selection if this is the first mouse move. This work is usually
1965 // done in handleMousePressEvent, but not if the mouse press was on an existing selection.
1966 Selection newSelection = selectionController()->selection();
1967 selectionController()->clearModifyBias();
1969 if (!d->m_beganSelectingText) {
1970 d->m_beganSelectingText = true;
1971 newSelection = Selection(pos);
1974 newSelection.setExtent(pos);
1975 if (d->m_selectionGranularity != CharacterGranularity)
1976 newSelection.expandUsingGranularity(d->m_selectionGranularity);
1978 if (shouldChangeSelection(newSelection))
1979 selectionController()->setSelection(newSelection);
1982 void Frame::handleMouseReleaseEvent(const MouseEventWithHitTestResults& event)
1984 stopAutoscrollTimer();
1986 // Used to prevent mouseMoveEvent from initiating a drag before
1987 // the mouse is pressed again.
1988 d->m_bMousePressed = false;
1990 // Clear the selection if the mouse didn't move after the last mouse press.
1991 // We do this so when clicking on the selection, the selection goes away.
1992 // However, if we are editing, place the caret.
1993 if (mouseDownMayStartSelect() && !d->m_beganSelectingText
1994 && d->m_dragStartPos == event.event().pos()
1995 && selectionController()->isRange()) {
1996 Selection newSelection;
1997 Node *node = event.targetNode();
1998 if (node && node->isContentEditable() && node->renderer()) {
1999 IntPoint vPoint = view()->viewportToContents(event.event().pos());
2000 VisiblePosition pos = node->renderer()->positionForPoint(vPoint);
2001 newSelection = Selection(pos);
2003 if (shouldChangeSelection(newSelection))
2004 selectionController()->setSelection(newSelection);
2007 notifyRendererOfSelectionChange(true);
2009 selectFrameElementInParentIfFullySelected();
2012 void Frame::selectAll()
2017 Node* root = selectionController()->isContentEditable() ? selectionController()->rootEditableElement() : d->m_doc->documentElement();
2018 selectContentsOfNode(root);
2019 selectFrameElementInParentIfFullySelected();
2020 notifyRendererOfSelectionChange(true);
2023 bool Frame::selectContentsOfNode(Node* node)
2025 Selection newSelection(Selection::selectionFromContentsOfNode(node));
2026 if (shouldChangeSelection(newSelection)) {
2027 selectionController()->setSelection(newSelection);
2033 bool Frame::shouldChangeSelection(const Selection& newSelection) const
2035 return shouldChangeSelection(selectionController()->selection(), newSelection, newSelection.affinity(), false);
2038 bool Frame::shouldDeleteSelection(const Selection& newSelection) const
2043 bool Frame::shouldBeginEditing(const Range *range) const
2048 bool Frame::shouldEndEditing(const Range *range) const
2053 bool Frame::isContentEditable() const
2057 return d->m_doc->inDesignMode();
2060 void Frame::textFieldDidBeginEditing(Element* input)
2064 void Frame::textFieldDidEndEditing(Element* input)
2068 void Frame::textDidChangeInTextField(Element* input)
2072 bool Frame::doTextFieldCommandFromEvent(Element* input, const PlatformKeyboardEvent* evt)
2077 void Frame::textWillBeDeletedInTextField(Element* input)
2081 void Frame::textDidChangeInTextArea(Element* input)
2085 bool Frame::isSelectionInPasswordField()
2087 Node* startNode = selectionController()->start().node();
2089 startNode = startNode->shadowAncestorNode();
2090 return startNode && startNode->hasTagName(inputTag) && static_cast<HTMLInputElement*>(startNode)->inputType() == HTMLInputElement::PASSWORD;
2093 EditCommand* Frame::lastEditCommand()
2095 return d->m_lastEditCommand.get();
2098 static void dispatchEditableContentChangedEvents(const EditCommand& command)
2100 Element* startRoot = command.startingRootEditableElement();
2101 Element* endRoot = command.endingRootEditableElement();
2104 startRoot->dispatchEvent(new Event(khtmlEditableContentChangedEvent, false, false), ec, true);
2105 if (endRoot && endRoot != startRoot)
2106 endRoot->dispatchEvent(new Event(khtmlEditableContentChangedEvent, false, false), ec, true);
2109 void Frame::appliedEditing(PassRefPtr<EditCommand> cmd)
2111 dispatchEditableContentChangedEvents(*cmd);
2113 Selection newSelection(cmd->endingSelection());
2114 if (shouldChangeSelection(newSelection))
2115 selectionController()->setSelection(newSelection, false);
2117 // Now set the typing style from the command. Clear it when done.
2118 // This helps make the case work where you completely delete a piece
2119 // of styled text and then type a character immediately after.
2120 // That new character needs to take on the style of the just-deleted text.
2121 // FIXME: Improve typing style.
2122 // See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
2123 if (cmd->typingStyle()) {
2124 setTypingStyle(cmd->typingStyle());
2125 cmd->setTypingStyle(0);
2128 // Command will be equal to last edit command only in the case of typing
2129 if (d->m_lastEditCommand == cmd)
2130 assert(cmd->isTypingCommand());
2132 // Only register a new undo command if the command passed in is
2133 // different from the last command
2134 d->m_lastEditCommand = cmd.get();
2135 registerCommandForUndo(cmd);
2137 respondToChangedContents(newSelection);
2140 void Frame::unappliedEditing(PassRefPtr<EditCommand> cmd)
2142 dispatchEditableContentChangedEvents(*cmd);
2144 Selection newSelection(cmd->startingSelection());
2145 if (shouldChangeSelection(newSelection))
2146 selectionController()->setSelection(newSelection, true);
2148 d->m_lastEditCommand = 0;
2149 registerCommandForRedo(cmd);
2150 respondToChangedContents(newSelection);
2153 void Frame::reappliedEditing(PassRefPtr<EditCommand> cmd)
2155 dispatchEditableContentChangedEvents(*cmd);
2157 Selection newSelection(cmd->endingSelection());
2158 if (shouldChangeSelection(newSelection))
2159 selectionController()->setSelection(newSelection, true);
2161 d->m_lastEditCommand = 0;
2162 registerCommandForUndo(cmd);
2163 respondToChangedContents(newSelection);
2166 CSSMutableStyleDeclaration *Frame::typingStyle() const
2168 return d->m_typingStyle.get();
2171 void Frame::setTypingStyle(CSSMutableStyleDeclaration *style)
2173 d->m_typingStyle = style;
2176 void Frame::clearTypingStyle()
2178 d->m_typingStyle = 0;
2181 JSValue* Frame::executeScript(const String& filename, int baseLine, Node* n, const String& script)
2183 // FIXME: This is missing stuff that the other executeScript has.
2184 // --> d->m_runningScripts and submitFormAgain.
2186 KJSProxy* proxy = jScript();
2189 JSValue* ret = proxy->evaluate(filename, baseLine, script, n);
2190 Document::updateDocumentsRendering();
2194 Frame *Frame::opener()
2199 void Frame::setOpener(Frame* opener)
2202 d->m_opener->d->m_openedFrames.remove(this);
2204 opener->d->m_openedFrames.add(this);
2205 d->m_opener = opener;
2208 bool Frame::openedByJS()
2210 return d->m_openedByJS;
2213 void Frame::setOpenedByJS(bool _openedByJS)
2215 d->m_openedByJS = _openedByJS;
2218 bool Frame::tabsToLinks() const
2223 bool Frame::tabsToAllControls() const
2228 void Frame::copyToPasteboard()
2233 void Frame::cutToPasteboard()
2238 void Frame::pasteFromPasteboard()
2240 issuePasteCommand();
2243 void Frame::pasteAndMatchStyle()
2245 issuePasteAndMatchStyleCommand();
2248 bool Frame::mayCopy()
2250 return !isSelectionInPasswordField();
2253 void Frame::transpose()
2255 issueTransposeCommand();
2269 void Frame::computeAndSetTypingStyle(CSSStyleDeclaration *style, EditAction editingAction)
2271 if (!style || style->length() == 0) {
2276 // Calculate the current typing style.
2277 RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
2278 if (typingStyle()) {
2279 typingStyle()->merge(mutableStyle.get());
2280 mutableStyle = typingStyle();
2283 Node *node = selectionController()->selection().visibleStart().deepEquivalent().node();
2284 CSSComputedStyleDeclaration computedStyle(node);
2285 computedStyle.diff(mutableStyle.get());
2287 // Handle block styles, substracting these from the typing style.
2288 RefPtr<CSSMutableStyleDeclaration> blockStyle = mutableStyle->copyBlockProperties();
2289 blockStyle->diff(mutableStyle.get());
2290 if (document() && blockStyle->length() > 0)
2291 applyCommand(new ApplyStyleCommand(document(), blockStyle.get(), editingAction));
2293 // Set the remaining style as the typing style.
2294 d->m_typingStyle = mutableStyle.release();
2297 void Frame::applyStyle(CSSStyleDeclaration *style, EditAction editingAction)
2299 switch (selectionController()->state()) {
2300 case Selection::NONE:
2303 case Selection::CARET: {
2304 computeAndSetTypingStyle(style, editingAction);
2307 case Selection::RANGE:
2308 if (document() && style)
2309 applyCommand(new ApplyStyleCommand(document(), style, editingAction));
2314 void Frame::applyParagraphStyle(CSSStyleDeclaration *style, EditAction editingAction)
2316 switch (selectionController()->state()) {
2317 case Selection::NONE:
2320 case Selection::CARET:
2321 case Selection::RANGE:
2322 if (document() && style)
2323 applyCommand(new ApplyStyleCommand(document(), style, editingAction, ApplyStyleCommand::ForceBlockProperties));
2328 static void updateState(CSSMutableStyleDeclaration *desiredStyle, CSSComputedStyleDeclaration *computedStyle, bool& atStart, Frame::TriState& state)
2330 DeprecatedValueListConstIterator<CSSProperty> end;
2331 for (DeprecatedValueListConstIterator<CSSProperty> it = desiredStyle->valuesIterator(); it != end; ++it) {
2332 int propertyID = (*it).id();
2333 String desiredProperty = desiredStyle->getPropertyValue(propertyID);
2334 String computedProperty = computedStyle->getPropertyValue(propertyID);
2335 Frame::TriState propertyState = equalIgnoringCase(desiredProperty, computedProperty)
2336 ? Frame::trueTriState : Frame::falseTriState;
2338 state = propertyState;
2340 } else if (state != propertyState) {
2341 state = Frame::mixedTriState;
2347 Frame::TriState Frame::selectionListState() const
2349 TriState state = falseTriState;
2351 if (!selectionController()->isRange()) {
2352 Node* selectionNode = selectionController()->selection().start().node();
2353 if (enclosingList(selectionNode))
2354 return trueTriState;
2356 //FIXME: Support ranges
2362 Frame::TriState Frame::selectionHasStyle(CSSStyleDeclaration *style) const
2364 bool atStart = true;
2365 TriState state = falseTriState;
2367 RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
2369 if (!selectionController()->isRange()) {
2371 RefPtr<CSSComputedStyleDeclaration> selectionStyle = selectionComputedStyle(nodeToRemove);
2372 if (!selectionStyle)
2373 return falseTriState;
2374 updateState(mutableStyle.get(), selectionStyle.get(), atStart, state);
2376 ExceptionCode ec = 0;
2377 nodeToRemove->remove(ec);
2381 for (Node* node = selectionController()->start().node(); node; node = node->traverseNextNode()) {
2382 RefPtr<CSSComputedStyleDeclaration> computedStyle = new CSSComputedStyleDeclaration(node);
2384 updateState(mutableStyle.get(), computedStyle.get(), atStart, state);
2385 if (state == mixedTriState)
2387 if (node == selectionController()->end().node())
2395 bool Frame::selectionStartHasStyle(CSSStyleDeclaration *style) const
2398 RefPtr<CSSStyleDeclaration> selectionStyle = selectionComputedStyle(nodeToRemove);
2399 if (!selectionStyle)
2402 RefPtr<CSSMutableStyleDeclaration> mutableStyle = style->makeMutable();
2405 DeprecatedValueListConstIterator<CSSProperty> end;
2406 for (DeprecatedValueListConstIterator<CSSProperty> it = mutableStyle->valuesIterator(); it != end; ++it) {
2407 int propertyID = (*it).id();
2408 if (!equalIgnoringCase(mutableStyle->getPropertyValue(propertyID), selectionStyle->getPropertyValue(propertyID))) {
2415 ExceptionCode ec = 0;
2416 nodeToRemove->remove(ec);
2423 String Frame::selectionStartStylePropertyValue(int stylePropertyID) const
2426 RefPtr<CSSStyleDeclaration> selectionStyle = selectionComputedStyle(nodeToRemove);
2427 if (!selectionStyle)
2430 String value = selectionStyle->getPropertyValue(stylePropertyID);
2433 ExceptionCode ec = 0;
2434 nodeToRemove->remove(ec);
2441 CSSComputedStyleDeclaration *Frame::selectionComputedStyle(Node *&nodeToRemove) const
2448 if (selectionController()->isNone())
2451 RefPtr<Range> range(selectionController()->toRange());
2452 Position pos = range->editingStartPosition();
2454 Element *elem = pos.element();
2458 RefPtr<Element> styleElement = elem;
2459 ExceptionCode ec = 0;
2461 if (d->m_typingStyle) {
2462 styleElement = document()->createElementNS(xhtmlNamespaceURI, "span", ec);
2465 styleElement->setAttribute(styleAttr, d->m_typingStyle->cssText().impl(), ec);
2468 styleElement->appendChild(document()->createEditingTextNode(""), ec);
2471 if (elem->renderer() && elem->renderer()->canHaveChildren()) {
2472 elem->appendChild(styleElement, ec);
2474 Node *parent = elem->parent();
2475 Node *next = elem->nextSibling();
2478 parent->insertBefore(styleElement, next, ec);
2480 parent->appendChild(styleElement, ec);
2485 nodeToRemove = styleElement.get();
2488 return new CSSComputedStyleDeclaration(styleElement);
2491 void Frame::applyEditingStyleToBodyElement() const
2496 RefPtr<NodeList> list = d->m_doc->getElementsByTagName("body");
2497 unsigned len = list->length();
2498 for (unsigned i = 0; i < len; i++) {
2499 applyEditingStyleToElement(static_cast<Element*>(list->item(i)));
2503 void Frame::removeEditingStyleFromBodyElement() const
2508 RefPtr<NodeList> list = d->m_doc->getElementsByTagName("body");
2509 unsigned len = list->length();
2510 for (unsigned i = 0; i < len; i++) {
2511 removeEditingStyleFromElement(static_cast<Element*>(list->item(i)));
2515 void Frame::applyEditingStyleToElement(Element* element) const
2520 CSSStyleDeclaration* style = element->style();
2523 ExceptionCode ec = 0;
2524 style->setProperty(CSS_PROP_WORD_WRAP, "break-word", false, ec);
2526 style->setProperty(CSS_PROP__WEBKIT_NBSP_MODE, "space", false, ec);
2528 style->setProperty(CSS_PROP__WEBKIT_LINE_BREAK, "after-white-space", false, ec);
2532 void Frame::removeEditingStyleFromElement(Element*) const
2536 bool Frame::isCharacterSmartReplaceExempt(UChar, bool)
2543 static HashSet<Frame*> lifeSupportSet;
2546 void Frame::endAllLifeSupport()
2549 HashSet<Frame*> lifeSupportCopy = lifeSupportSet;
2550 HashSet<Frame*>::iterator end = lifeSupportCopy.end();
2551 for (HashSet<Frame*>::iterator it = lifeSupportCopy.begin(); it != end; ++it)
2552 (*it)->endLifeSupport();
2556 void Frame::keepAlive()
2558 if (d->m_lifeSupportTimer.isActive())
2562 lifeSupportSet.add(this);
2564 d->m_lifeSupportTimer.startOneShot(0);
2567 void Frame::endLifeSupport()
2569 if (!d->m_lifeSupportTimer.isActive())
2571 d->m_lifeSupportTimer.stop();
2573 lifeSupportSet.remove(this);
2578 void Frame::lifeSupportTimerFired(Timer<Frame>*)
2581 lifeSupportSet.remove(this);
2586 // Workaround for the fact that it's hard to delete a frame.
2587 // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
2588 // Can't do this implicitly as part of every setSelection call because in some contexts it might not be good
2589 // for the focus to move to another frame. So instead we call it from places where we are selecting with the
2590 // mouse or the keyboard after setting the selection.
2591 void Frame::selectFrameElementInParentIfFullySelected()
2593 // Find the parent frame; if there is none, then we have nothing to do.
2594 Frame *parent = tree()->parent();
2597 FrameView *parentView = parent->view();
2601 // Check if the selection contains the entire frame contents; if not, then there is nothing to do.
2602 if (!selectionController()->isRange())
2604 if (!isStartOfDocument(selectionController()->selection().visibleStart()))
2606 if (!isEndOfDocument(selectionController()->selection().visibleEnd()))
2609 // Get to the <iframe> or <frame> (or even <object>) element in the parent frame.
2610 Document *doc = document();
2613 Element *ownerElement = doc->ownerElement();
2616 Node *ownerElementParent = ownerElement->parentNode();
2617 if (!ownerElementParent)
2620 // 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.
2621 if (!ownerElementParent->isContentEditable())
2624 // Create compute positions before and after the element.
2625 unsigned ownerElementNodeIndex = ownerElement->nodeIndex();
2626 VisiblePosition beforeOwnerElement(VisiblePosition(ownerElementParent, ownerElementNodeIndex, SEL_DEFAULT_AFFINITY));
2627 VisiblePosition afterOwnerElement(VisiblePosition(ownerElementParent, ownerElementNodeIndex + 1, VP_UPSTREAM_IF_POSSIBLE));
2629 // Focus on the parent frame, and then select from before this element to after.
2630 Selection newSelection(beforeOwnerElement, afterOwnerElement);
2631 if (parent->shouldChangeSelection(newSelection)) {
2632 parentView->setFocus();
2633 parent->selectionController()->setSelection(newSelection);
2637 void Frame::handleFallbackContent()
2639 Element* owner = ownerElement();
2640 if (!owner || !owner->hasTagName(objectTag))
2642 static_cast<HTMLObjectElement*>(owner)->renderFallbackContent();
2645 void Frame::setSettings(Settings *settings)
2647 d->m_settings = settings;
2650 void Frame::provisionalLoadStarted()
2652 // we don't want to wait until we get an actual http response back
2653 // to cancel pending redirects, otherwise they might fire before
2655 cancelRedirection(true);
2658 bool Frame::userGestureHint()
2660 Frame *rootFrame = this;
2661 while (rootFrame->tree()->parent())
2662 rootFrame = rootFrame->tree()->parent();
2664 if (rootFrame->jScript())
2665 return rootFrame->jScript()->interpreter()->wasRunByUserGesture();
2667 return true; // If JavaScript is disabled, a user gesture must have initiated the navigation
2670 RenderObject *Frame::renderer() const
2672 Document *doc = document();
2673 return doc ? doc->renderer() : 0;
2676 Element* Frame::ownerElement()
2678 return d->m_ownerElement;
2681 RenderPart* Frame::ownerRenderer()
2683 Element* ownerElement = d->m_ownerElement;
2686 return static_cast<RenderPart*>(ownerElement->renderer());
2689 IntRect Frame::selectionRect() const
2691 RenderView *root = static_cast<RenderView*>(renderer());
2695 return root->selectionRect();
2698 // returns FloatRect because going through IntRect would truncate any floats
2699 FloatRect Frame::visibleSelectionRect() const
2704 return intersection(selectionRect(), d->m_view->visibleContentRect());
2707 bool Frame::isFrameSet() const
2709 Document* document = d->m_doc.get();
2710 if (!document || !document->isHTMLDocument())
2712 Node *body = static_cast<HTMLDocument*>(document)->body();
2713 return body && body->renderer() && body->hasTagName(framesetTag);
2716 bool Frame::openURL(const KURL& URL)
2718 ASSERT_NOT_REACHED();
2722 void Frame::didNotOpenURL(const KURL& URL)
2724 if (d->m_submittedFormURL == URL)
2725 d->m_submittedFormURL = KURL();
2728 // Scans logically forward from "start", including any child frames
2729 static HTMLFormElement *scanForForm(Node *start)
2732 for (n = start; n; n = n->traverseNextNode()) {
2733 if (n->hasTagName(formTag))
2734 return static_cast<HTMLFormElement*>(n);
2735 else if (n->isHTMLElement() && static_cast<HTMLElement*>(n)->isGenericFormElement())
2736 return static_cast<HTMLGenericFormElement*>(n)->form();
2737 else if (n->hasTagName(frameTag) || n->hasTagName(iframeTag)) {
2738 Node *childDoc = static_cast<HTMLFrameElement*>(n)->contentDocument();
2739 if (HTMLFormElement *frameResult = scanForForm(childDoc))
2746 // We look for either the form containing the current focus, or for one immediately after it
2747 HTMLFormElement *Frame::currentForm() const
2749 // start looking either at the active (first responder) node, or where the selection is
2750 Node *start = d->m_doc ? d->m_doc->focusNode() : 0;
2752 start = selectionController()->start().node();
2754 // try walking up the node tree to find a form element
2756 for (n = start; n; n = n->parentNode()) {
2757 if (n->hasTagName(formTag))
2758 return static_cast<HTMLFormElement*>(n);
2759 else if (n->isHTMLElement()
2760 && static_cast<HTMLElement*>(n)->isGenericFormElement())
2761 return static_cast<HTMLGenericFormElement*>(n)->form();
2764 // try walking forward in the node tree to find a form element
2765 return start ? scanForForm(start) : 0;
2768 void Frame::setEncoding(const String& name, bool userChosen)
2770 if (!d->m_workingURL.isEmpty())
2771 receivedFirstData();
2772 d->m_encoding = name;
2773 d->m_haveEncoding = userChosen;
2776 void Frame::addData(const char *bytes, int length)
2778 ASSERT(d->m_workingURL.isEmpty());
2780 ASSERT(d->m_doc->parsing());
2781 write(bytes, length);
2784 // FIXME: should this go in SelectionController?
2785 void Frame::revealSelection(const RenderLayer::ScrollAlignment& alignment) const
2789 switch (selectionController()->state()) {
2790 case Selection::NONE:
2793 case Selection::CARET:
2794 rect = selectionController()->caretRect();
2797 case Selection::RANGE:
2798 rect = selectionRect();
2802 Position start = selectionController()->start();
2803 Position end = selectionController()->end();
2805 ASSERT(start.node());
2806 if (start.node() && start.node()->renderer()) {
2807 RenderLayer *layer = start.node()->renderer()->enclosingLayer();
2809 ASSERT(!end.node() || !end.node()->renderer()
2810 || (end.node()->renderer()->enclosingLayer() == layer));
2811 layer->scrollRectToVisible(rect, alignment, alignment);
2816 void Frame::revealCaret(const RenderLayer::ScrollAlignment& alignment) const
2818 if (!hasSelection())
2821 Position extent = selectionController()->extent();
2822 if (extent.node() && extent.node()->renderer()) {
2823 IntRect extentRect = VisiblePosition(extent).caretRect();
2824 RenderLayer* layer = extent.node()->renderer()->enclosingLayer();
2826 layer->scrollRectToVisible(extentRect, alignment, alignment);
2830 // FIXME: should this be here?
2831 bool Frame::scrollOverflow(ScrollDirection direction, ScrollGranularity granularity)
2837 Node *node = document()->focusNode();
2839 node = d->m_mousePressNode.get();
2843 RenderObject *r = node->renderer();
2845 return r->scroll(direction, granularity);
2852 void Frame::handleAutoscroll(RenderLayer* layer)
2854 if (d->m_autoscrollTimer.isActive())
2856 d->m_autoscrollLayer = layer;
2857 startAutoscrollTimer();
2860 void Frame::autoscrollTimerFired(Timer<Frame>*)
2862 if (!d->m_bMousePressed){
2863 stopAutoscrollTimer();
2866 if (d->m_autoscrollLayer) {
2867 d->m_autoscrollLayer->autoscroll();
2871 RenderObject::NodeInfo Frame::nodeInfoAtPoint(const IntPoint& point, bool allowShadowContent)
2873 RenderObject::NodeInfo nodeInfo(true, true);
2874 renderer()->layer()->hitTest(nodeInfo, point);
2878 IntPoint widgetPoint(point);
2881 n = nodeInfo.innerNode();
2882 if (!n || !n->renderer() || !n->renderer()->isWidget())
2884 widget = static_cast<RenderWidget*>(n->renderer())->widget();
2885 if (!widget || !widget->isFrameView())
2887 Frame* frame = static_cast<HTMLFrameElement*>(n)->contentFrame();
2888 if (!frame || !frame->renderer())
2891 n->renderer()->absolutePosition(absX, absY, true);
2892 FrameView *view = static_cast<FrameView*>(widget);
2893 widgetPoint.setX(widgetPoint.x() - absX + view->contentsX());
2894 widgetPoint.setY(widgetPoint.y() - absY + view->contentsY());
2896 RenderObject::NodeInfo widgetNodeInfo(true, true);
2897 frame->renderer()->layer()->hitTest(widgetNodeInfo, widgetPoint);
2898 nodeInfo = widgetNodeInfo;
2901 if (!allowShadowContent) {
2902 Node* node = nodeInfo.innerNode();
2904 node = node->shadowAncestorNode();
2905 nodeInfo.setInnerNode(node);
2906 node = nodeInfo.innerNonSharedNode();
2908 node = node->shadowAncestorNode();
2909 nodeInfo.setInnerNonSharedNode(node);
2914 bool Frame::hasSelection()
2916 if (selectionController()->isNone())
2919 // If a part has a selection, it should also have a document.
2925 void Frame::startAutoscrollTimer()
2927 d->m_autoscrollTimer.startRepeating(autoscrollInterval);
2930 void Frame::stopAutoscrollTimer()
2932 d->m_autoscrollLayer = 0;
2933 d->m_autoscrollTimer.stop();
2936 // FIXME: why is this here instead of on the FrameView?
2937 void Frame::paint(GraphicsContext* p, const IntRect& rect)
2941 if (!document() || document()->printing())
2942 fillWithRed = false; // Printing, don't fill with red (can't remember why).
2943 else if (document()->ownerElement())
2944 fillWithRed = false; // Subframe, don't fill with red.
2945 else if (view() && view()->isTransparent())
2946 fillWithRed = false; // Transparent, don't fill with red.
2947 else if (d->m_paintRestriction == PaintRestrictionSelectionOnly || d->m_paintRestriction == PaintRestrictionSelectionOnlyWhiteText)
2948 fillWithRed = false; // Selections are transparent, don't fill with red.
2949 else if (d->m_elementToDraw)
2950 fillWithRed = false; // Element images are transparent, don't fill with red.
2955 p->fillRect(rect, Color(0xFF, 0, 0));
2959 // d->m_elementToDraw is used to draw only one element
2960 RenderObject *eltRenderer = d->m_elementToDraw ? d->m_elementToDraw->renderer() : 0;
2961 if (d->m_paintRestriction == PaintRestrictionNone)
2962 renderer()->document()->invalidateRenderedRectsForMarkersInRect(rect);
2963 renderer()->layer()->paint(p, rect, d->m_paintRestriction, eltRenderer);
2966 // Regions may have changed as a result of the visibility/z-index of element changing.
2967 if (renderer()->document()->dashboardRegionsDirty())
2968 renderer()->view()->frameView()->updateDashboardRegions();
2971 LOG_ERROR("called Frame::paint with nil renderer");
2976 void Frame::adjustPageHeight(float *newBottom, float oldTop, float oldBottom, float bottomLimit)
2978 RenderView *root = static_cast<RenderView*>(document()->renderer());
2980 // Use a context with painting disabled.
2981 GraphicsContext context(0);
2982 root->setTruncatedAt((int)floorf(oldBottom));
2983 IntRect dirtyRect(0, (int)floorf(oldTop), root->docWidth(), (int)ceilf(oldBottom - oldTop));
2984 root->layer()->paint(&context, dirtyRect);
2985 *newBottom = root->bestTruncatedAt();
2986 if (*newBottom == 0)
2987 *newBottom = oldBottom;
2989 *newBottom = oldBottom;
2994 PausedTimeouts *Frame::pauseTimeouts()
2997 if (d->m_doc && d->m_doc->svgExtensions())
2998 d->m_doc->accessSVGExtensions()->pauseAnimations();
3001 if (d->m_doc && d->m_jscript) {
3002 if (Window* w = Window::retrieveWindow(this))
3003 return w->pauseTimeouts();
3008 void Frame::resumeTimeouts(PausedTimeouts* t)
3011 if (d->m_doc && d->m_doc->svgExtensions())
3012 d->m_doc->accessSVGExtensions()->unpauseAnimations();
3015 if (d->m_doc && d->m_jscript && d->m_bJScriptEnabled) {
3016 if (Window* w = Window::retrieveWindow(this))
3017 w->resumeTimeouts(t);
3021 bool Frame::canCachePage()
3023 // Only save page state if:
3024 // 1. We're not a frame or frameset.
3025 // 2. The page has no unload handler.
3026 // 3. The page has no password fields.
3027 // 4. The URL for the page is not https.
3028 // 5. The page has no applets.
3029 if (tree()->childCount() || d->m_plugins.size() ||
3031 d->m_url.protocol().startsWith("https") ||
3032 (d->m_doc && (d->m_doc->applets()->length() != 0 ||
3033 d->m_doc->hasWindowEventListener(unloadEvent) ||
3034 d->m_doc->hasPasswordField()))) {
3040 void Frame::saveWindowProperties(KJS::SavedProperties *windowProperties)
3042 Window *window = Window::retrieveWindow(this);
3044 window->saveProperties(*windowProperties);
3047 void Frame::saveLocationProperties(SavedProperties *locationProperties)
3049 Window *window = Window::retrieveWindow(this);
3052 Location *location = window->location();
3053 location->saveProperties(*locationProperties);
3057 void Frame::restoreWindowProperties(SavedProperties *windowProperties)
3059 Window *window = Window::retrieveWindow(this);
3061 window->restoreProperties(*windowProperties);
3064 void Frame::restoreLocationProperties(SavedProperties *locationProperties)
3066 Window *window = Window::retrieveWindow(this);
3069 Location *location = window->location();
3070 location->restoreProperties(*locationProperties);
3074 void Frame::saveInterpreterBuiltins(SavedBuiltins& interpreterBuiltins)
3077 jScript()->interpreter()->saveBuiltins(interpreterBuiltins);
3080 void Frame::restoreInterpreterBuiltins(const SavedBuiltins& interpreterBuiltins)
3083 jScript()->interpreter()->restoreBuiltins(interpreterBuiltins);
3086 Frame *Frame::frameForWidget(const Widget *widget)
3088 ASSERT_ARG(widget, widget);
3090 Node *node = nodeForWidget(widget);
3092 return frameForNode(node);
3094 // Assume all widgets are either form controls, or FrameViews.
3095 ASSERT(widget->isFrameView());
3096 return static_cast<const FrameView*>(widget)->frame();
3099 Frame *Frame::frameForNode(Node *node)
3101 ASSERT_ARG(node, node);
3102 return node->document()->frame();
3105 Node* Frame::nodeForWidget(const Widget* widget)
3107 ASSERT_ARG(widget, widget);
3108 WidgetClient* client = widget->client();
3111 return client->element(const_cast<Widget*>(widget));
3114 void Frame::clearDocumentFocus(Widget *widget)
3116 Node *node = nodeForWidget(widget);
3118 node->document()->setFocusNode(0);
3121 void Frame::updatePolicyBaseURL()
3123 if (tree()->parent() && tree()->parent()->document())
3124 setPolicyBaseURL(tree()->parent()->document()->policyBaseURL());
3126 setPolicyBaseURL(d->m_url.url());
3129 void Frame::setPolicyBaseURL(const String& s)
3132 document()->setPolicyBaseURL(s);
3133 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
3134 child->setPolicyBaseURL(s);
3137 void Frame::forceLayout()
3139 FrameView *v = d->m_view.get();
3142 // We cannot unschedule a pending relayout, since the force can be called with
3143 // a tiny rectangle from a drawRect update. By unscheduling we in effect
3144 // "validate" and stop the necessary full repaint from occurring. Basically any basic
3145 // append/remove DHTML is broken by this call. For now, I have removed the optimization
3146 // until we have a better invalidation stategy. -dwh
3147 //v->unscheduleRelayout();
3151 void Frame::forceLayoutWithPageWidthRange(float minPageWidth, float maxPageWidth)
3153 // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
3154 // the state of things before and after the layout
3155 RenderView *root = static_cast<RenderView*>(document()->renderer());
3157 // This magic is basically copied from khtmlview::print
3158 int pageW = (int)ceilf(minPageWidth);
3159 root->setWidth(pageW);
3160 root->setNeedsLayoutAndMinMaxRecalc();
3163 // If we don't fit in the minimum page width, we'll lay out again. If we don't fit in the
3164 // maximum page width, we will lay out to the maximum page width and clip extra content.
3165 // FIXME: We are assuming a shrink-to-fit printing implementation. A cropping
3166 // implementation should not do this!
3167 int rightmostPos = root->rightmostPosition();
3168 if (rightmostPos > minPageWidth) {
3169 pageW = min(rightmostPos, (int)ceilf(maxPageWidth));
3170 root->setWidth(pageW);
3171 root->setNeedsLayoutAndMinMaxRecalc();
3177 void Frame::sendResizeEvent()
3179 if (Document* doc = document())
3180 doc->dispatchWindowEvent(EventNames::resizeEvent, false, false);
3183 void Frame::sendScrollEvent()
3185 FrameView *v = d->m_view.get();
3187 Document *doc = document();
3190 doc->dispatchHTMLEvent(scrollEvent, true, false);
3194 bool Frame::scrollbarsVisible()
3199 if (view()->hScrollBarMode() == ScrollBarAlwaysOff || view()->vScrollBarMode() == ScrollBarAlwaysOff)
3205 void Frame::addMetaData(const String& key, const String& value)
3207 d->m_metaData.set(key, value);
3210 // This does the same kind of work that Frame::openURL does, except it relies on the fact
3211 // that a higher level already checked that the URLs match and the scrolling is the right thing to do.
3212 void Frame::scrollToAnchor(const KURL& URL)
3219 // It's important to model this as a load that starts and immediately finishes.
3220 // Otherwise, the parent frame may think we never finished loading.
3221 d->m_bComplete = false;
3225 bool Frame::closeURL()
3227 saveDocumentState();
3229 clearUndoRedoOperations();
3233 bool Frame::canMouseDownStartSelect(Node* node)
3235 if (!node || !node->renderer())
3238 // Check to see if -webkit-user-select has been set to none
3239 if (!node->renderer()->canSelect())
3242 // Some controls and images can't start a select on a mouse down.
3243 for (RenderObject* curr = node->renderer(); curr; curr = curr->parent()) {
3244 if (curr->style()->userSelect() == SELECT_IGNORE)
3251 void Frame::handleMouseReleaseDoubleClickEvent(const MouseEventWithHitTestResults& event)
3253 passWidgetMouseDownEventToWidget(event, true);
3256 bool Frame::passWidgetMouseDownEventToWidget(const MouseEventWithHitTestResults& event, bool isDoubleClick)
3258 // Figure out which view to send the event to.
3259 RenderObject *target = event.targetNode() ? event.targetNode()->renderer() : 0;
3263 Widget* widget = RenderLayer::gScrollBar;
3265 if (!target->isWidget())
3267 widget = static_cast<RenderWidget*>(target)->widget();
3270 // Doubleclick events don't exist in Cocoa. Since passWidgetMouseDownEventToWidget will
3271 // just pass _currentEvent down to the widget, we don't want to call it for events that
3272 // don't correspond to Cocoa events. The mousedown/ups will have already been passed on as
3273 // part of the pressed/released handling.
3275 return passMouseDownEventToWidget(widget);
3279 bool Frame::passWidgetMouseDownEventToWidget(RenderWidget *renderWidget)
3281 return passMouseDownEventToWidget(renderWidget->widget());
3284 void Frame::clearTimers(FrameView *view)
3287 view->unscheduleRelayout();
3288 if (view->frame()) {
3289 Document* document = view->frame()->document();
3290 if (document && document->renderer() && document->renderer()->layer())
3291 document->renderer()->layer()->suspendMarquees();
3296 void Frame::clearTimers()
3298 clearTimers(d->m_view.get());
3301 RenderStyle *Frame::styleForSelectionStart(Node *&nodeToRemove) const
3307 if (selectionController()->isNone())
3310 Position pos = selectionController()->selection().visibleStart().deepEquivalent();
3311 if (!pos.inRenderedContent())
3313 Node *node = pos.node();
3317 if (!d->m_typingStyle)
3318 return node->renderer()->style();
3320 ExceptionCode ec = 0;
3321 RefPtr<Element> styleElement = document()->createElementNS(xhtmlNamespaceURI, "span", ec);
3324 styleElement->setAttribute(styleAttr, d->m_typingStyle->cssText().impl(), ec);
3327 styleElement->appendChild(document()->createEditingTextNode(""), ec);
3330 node->parentNode()->appendChild(styleElement, ec);
3333 nodeToRemove = styleElement.get();
3334 return styleElement->renderer()->style();
3337 void Frame::setMediaType(const String& type)
3340 d->m_view->setMediaType(type);
3343 void Frame::setSelectionFromNone()
3345 // Put a caret inside the body if the entire frame is editable (either the
3346 // entire WebView is editable or designMode is on for this document).
3347 Document *doc = document();
3348 if (!doc || !selectionController()->isNone() || !isContentEditable())
3351 Node* node = doc->documentElement();
3352 while (node && !node->hasTagName(bodyTag))
3353 node = node->traverseNextNode();
3355 selectionController()->setSelection(Selection(Position(node, 0), DOWNSTREAM));
3358 bool Frame::isActive() const
3360 return d->m_isActive;
3363 void Frame::setIsActive(bool flag)
3365 if (d->m_isActive == flag)
3368 d->m_isActive = flag;
3370 // This method does the job of updating the view based on whether the view is "active".
3371 // This involves three kinds of drawing updates:
3373 // 1. The background color used to draw behind selected content (active | inactive color)
3375 d->m_view->updateContents(enclosingIntRect(visibleSelectionRect()));
3377 // 2. Caret blinking (blinks | does not blink)
3379 setSelectionFromNone();
3380 setCaretVisible(flag);
3382 // 3. The drawing of a focus ring around links in web pages.
3383 Document *doc = document();
3385 Node *node = doc->focusNode();
3388 if (node->renderer() && node->renderer()->style()->hasAppearance())
3389 theme()->stateChanged(node->renderer(), FocusState);
3393 // 4. Changing the tint of controls from clear to aqua/graphite and vice versa. We
3394 // do a "fake" paint. When the theme gets a paint call, it can then do an invalidate. This is only
3395 // done if the theme supports control tinting.
3396 if (doc && d->m_view && theme()->supportsControlTints() && renderer()) {
3397 doc->updateLayout(); // Ensure layout is up to date.
3398 IntRect visibleRect(enclosingIntRect(d->m_view->visibleContentRect()));
3399 GraphicsContext context((PlatformGraphicsContext*)0);
3400 context.setUpdatingControlTints(true);
3401 paint(&context, visibleRect);
3404 // 5. Enable or disable secure keyboard entry
3405 if ((flag && !isSecureKeyboardEntry() && doc && doc->focusNode() && doc->focusNode()->hasTagName(inputTag) &&
3406 static_cast<HTMLInputElement*>(doc->focusNode())->inputType() == HTMLInputElement::PASSWORD) ||
3407 (!flag && isSecureKeyboardEntry()))
3408 setSecureKeyboardEntry(flag);
3411 void Frame::setWindowHasFocus(bool flag)
3413 if (d->m_windowHasFocus == flag)
3415 d->m_windowHasFocus = flag;
3417 if (Document *doc = document())
3418 doc->dispatchWindowEvent(flag ? focusEvent : blurEvent, false, false);
3421 bool Frame::inViewSourceMode() const
3423 return d->m_inViewSourceMode;
3426 void Frame::setInViewSourceMode(bool mode) const
3428 d->m_inViewSourceMode = mode;
3431 UChar Frame::backslashAsCurrencySymbol() const
3433 Document *doc = document();
3436 Decoder *decoder = doc->decoder();
3440 return decoder->encoding().backslashAsCurrencySymbol();
3443 bool Frame::markedTextUsesUnderlines() const
3445 return d->m_markedTextUsesUnderlines;
3448 const Vector<MarkedTextUnderline>& Frame::markedTextUnderlines() const
3450 return d->m_markedTextUnderlines;
3453 // Searches from the beginning of the document if nothing is selected.
3454 bool Frame::findString(const String& target, bool forward, bool caseFlag, bool wrapFlag)
3456 if (target.isEmpty())
3459 // Initially search from the start (if forward) or end (if backward) of the selection, and search to edge of document.
3460 RefPtr<Range> searchRange(rangeOfContents(document()));
3461 Selection selection(selectionController()->selection());
3462 if (!selection.isNone()) {
3464 setStart(searchRange.get(), selection.visibleStart());
3466 setEnd(searchRange.get(), selection.visibleEnd());
3468 RefPtr<Range> resultRange(findPlainText(searchRange.get(), target, forward, caseFlag));
3469 // If the found range is already selected, find again.
3470 // Build a selection with the found range to remove collapsed whitespace.
3471 // Compare ranges instead of selection objects to ignore the way that the current selection was made.
3472 if (!selection.isNone() && *Selection(resultRange.get()).toRange() == *selection.toRange()) {
3473 searchRange = rangeOfContents(document());
3475 setStart(searchRange.get(), selection.visibleEnd());
3477 setEnd(searchRange.get(), selection.visibleStart());
3478 resultRange = findPlainText(searchRange.get(), target, forward, caseFlag);
3483 // If we didn't find anything and we're wrapping, search again in the entire document (this will
3484 // redundantly re-search the area already searched in some cases).
3485 if (resultRange->collapsed(exception) && wrapFlag) {
3486 searchRange = rangeOfContents(document());
3487 resultRange = findPlainText(searchRange.get(), target, forward, caseFlag);
3488 // We used to return false here if we ended up with the same range that we started with
3489 // (e.g., the selection was already the only instance of this text). But we decided that
3490 // this should be a success case instead, so we'll just fall through in that case.
3493 if (resultRange->collapsed(exception))
3496 selectionController()->setSelection(Selection(resultRange.get(), DOWNSTREAM));
3501 unsigned Frame::markAllMatchesForText(const String& target, bool caseFlag, unsigned limit)
3503 if (target.isEmpty())
3506 RefPtr<Range> searchRange(rangeOfContents(document()));
3509 unsigned matchCount = 0;
3511 RefPtr<Range> resultRange(findPlainText(searchRange.get(), target, true, caseFlag));
3512 if (resultRange->collapsed(exception))
3515 // A non-collapsed result range can in some funky whitespace cases still not
3516 // advance the range's start position (4509328). Break to avoid infinite loop.
3517 VisiblePosition newStart = endVisiblePosition(resultRange.get(), DOWNSTREAM);
3518 if (newStart == startVisiblePosition(searchRange.get(), DOWNSTREAM))
3523 document()->addMarker(resultRange.get(), DocumentMarker::TextMatch);
3525 // Stop looking if we hit the specified limit. A limit of 0 means no limit.
3526 if (limit > 0 && matchCount >= limit)
3529 setStart(searchRange.get(), newStart);
3532 // Do a "fake" paint in order to execute the code that computes the rendered rect for
3534 Document* doc = document();
3535 if (doc && d->m_view && renderer()) {
3536 doc->updateLayout(); // Ensure layout is up to date.
3537 IntRect visibleRect(enclosingIntRect(d->m_view->visibleContentRect()));
3538 GraphicsContext context((PlatformGraphicsContext*)0);
3539 context.setPaintingDisabled(true);
3540 paint(&context, visibleRect);
3546 bool Frame::markedTextMatchesAreHighlighted() const
3548 return d->m_highlightTextMatches;
3551 void Frame::setMarkedTextMatchesAreHighlighted(bool flag)
3553 if (flag == d->m_highlightTextMatches)
3556 d->m_highlightTextMatches = flag;
3557 document()->repaintMarkers(DocumentMarker::TextMatch);
3560 void Frame::prepareForUserAction()
3562 // Reset the multiple form submission protection code.
3563 // We'll let you submit the same form twice if you do two separate user actions.
3564 d->m_submittedFormURL = KURL();
3567 Node *Frame::mousePressNode()
3569 return d->m_mousePressNode.get();
3572 bool Frame::isComplete() const
3574 return d->m_bComplete;
3577 bool Frame::isLoadingMainResource() const
3579 return d->m_bLoadingMainResource;
3582 FrameTree* Frame::tree() const
3584 return &d->m_treeNode;
3587 DOMWindow* Frame::domWindow() const
3589 if (!d->m_domWindow)
3590 d->m_domWindow = new DOMWindow(const_cast<Frame*>(this));
3592 return d->m_domWindow.get();
3595 KURL Frame::url() const
3600 void Frame::startRedirectionTimer()
3602 d->m_redirectionTimer.startOneShot(d->m_delayRedirect);
3605 void Frame::stopRedirectionTimer()
3607 d->m_redirectionTimer.stop();
3610 void Frame::frameDetached()
3614 void Frame::updateBaseURLForEmptyDocument()
3616 Element* owner = ownerElement();
3617 // FIXME: Should embed be included?
3618 if (owner && (owner->hasTagName(iframeTag) || owner->hasTagName(objectTag) || owner->hasTagName(embedTag)))
3619 d->m_doc->setBaseURL(tree()->parent()->d->m_doc->baseURL());
3622 Page* Frame::page() const
3627 void Frame::pageDestroyed()
3631 // This will stop any JS timers
3632 if (d->m_jscript && d->m_jscript->haveInterpreter())
3633 if (Window* w = Window::retrieveWindow(this))
3634 w->disconnectFrame();
3637 void Frame::completed(bool complete)
3640 for (Frame* child = tree()->firstChild(); child; child = child->tree()->nextSibling())
3641 child->parentCompleted();
3642 if (Frame* parent = tree()->parent())
3643 parent->childCompleted(complete);
3648 void Frame::setStatusBarText(const String&)
3652 void Frame::started()
3654 for (Frame* frame = this; frame; frame = frame->tree()->parent())
3655 frame->d->m_bComplete = false;
3658 void Frame::disconnectOwnerElement()
3660 if (d->m_ownerElement && d->m_page)
3661 d->m_page->decrementFrameCount();
3663 d->m_ownerElement = 0;
3666 String Frame::documentTypeString() const
3668 if (Document *doc = document())
3669 if (DocumentType *doctype = doc->realDocType())
3670 return doctype->toString();
3675 bool Frame::containsPlugins() const
3677 return d->m_plugins.size() != 0;
3680 bool Frame::prohibitsScrolling() const
3682 return d->m_prohibitsScrolling;
3685 void Frame::setProhibitsScrolling(const bool prohibit)
3687 d->m_prohibitsScrolling = prohibit;
3690 } // namespace WebCore