From: Steve Lamerton Date: Fri, 15 Jul 2011 12:38:47 +0000 (+0000) Subject: Add RunScript and implement on all backends. Document and add a very simple unit... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/c9ccc09c64f4c29d4667796bef7b507d9e8d25ed Add RunScript and implement on all backends. Document and add a very simple unit test. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68275 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/gtk/webview_webkit.h b/include/wx/gtk/webview_webkit.h index e675d5d98a..3dc9acc282 100644 --- a/include/wx/gtk/webview_webkit.h +++ b/include/wx/gtk/webview_webkit.h @@ -149,6 +149,8 @@ public: virtual wxString GetSelectedSource(); virtual void ClearSelection(); + virtual void RunScript(const wxString& javascript); + /** FIXME: hack to work around signals being received too early */ bool m_ready; diff --git a/include/wx/msw/webview_ie.h b/include/wx/msw/webview_ie.h index 5d854abd9d..ebf325cc44 100644 --- a/include/wx/msw/webview_ie.h +++ b/include/wx/msw/webview_ie.h @@ -106,6 +106,7 @@ public: virtual wxString GetSelectedSource(); virtual void ClearSelection(); + virtual void RunScript(const wxString& javascript); // ---- IE-specific methods diff --git a/include/wx/osx/webview_webkit.h b/include/wx/osx/webview_webkit.h index a30f95cccb..975bee5ec7 100644 --- a/include/wx/osx/webview_webkit.h +++ b/include/wx/osx/webview_webkit.h @@ -110,10 +110,10 @@ public: virtual wxString GetSelectedText(); virtual wxString GetSelectedSource() { return ""; } virtual void ClearSelection() {} + + void RunScript(const wxString& javascript); // ---- methods not from the parent (common) interface - wxString RunScript(const wxString& javascript); - bool CanGetPageSource(); void SetScrollPos(int pos); diff --git a/include/wx/webview.h b/include/wx/webview.h index 22d9a2cbbc..2a114f96c5 100644 --- a/include/wx/webview.h +++ b/include/wx/webview.h @@ -292,9 +292,11 @@ public: virtual wxString GetSelectedSource() = 0; virtual void ClearSelection() = 0; + virtual void RunScript(const wxString& javascript) = 0; + // TODO: // void EnableJavascript(bool enabled); // maybe? - // wxString RunScript(const wxString& javascript); // maybe? + // // maybe? // void SetScrollPos(int pos); // maybe? // int GetScrollPos(); // maybe? diff --git a/interface/wx/webview.h b/interface/wx/webview.h index 2ee3844688..da6f95214c 100644 --- a/interface/wx/webview.h +++ b/interface/wx/webview.h @@ -262,6 +262,11 @@ public: */ virtual void Reload(wxWebViewReloadFlags flags = wxWEB_VIEW_RELOAD_DEFAULT) = 0; + /** + Runs the given javascript code. + */ + virtual void RunScript(const wxString& javascript) = 0; + /** Set the editable property of the web control. Enabling allows the user to edit the page even if the @c contenteditable attribute is not set. diff --git a/src/gtk/webview_webkit.cpp b/src/gtk/webview_webkit.cpp index 11304653d6..d1aed673fa 100644 --- a/src/gtk/webview_webkit.cpp +++ b/src/gtk/webview_webkit.cpp @@ -792,6 +792,12 @@ wxString wxWebViewWebKit::GetPageText() wxConvUTF8); } +void wxWebViewWebKit::RunScript(const wxString& javascript) +{ + webkit_web_view_execute_script(WEBKIT_WEB_VIEW(web_view), + javascript.mb_str(wxConvUTF8)); +} + // static wxVisualAttributes wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 01d0061146..dbb35fdc3a 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -686,6 +686,22 @@ wxString wxWebViewIE::GetPageText() return text; } +void wxWebViewIE::RunScript(const wxString& javascript) +{ + IHTMLDocument2* document = GetDocument(); + IHTMLWindow2* window; + wxString language = "javascript"; + HRESULT hr = document->get_parentWindow(&window); + if(SUCCEEDED(hr)) + { + VARIANT level; + VariantInit(&level); + V_VT(&level) = VT_EMPTY; + window->execScript(SysAllocString(javascript), SysAllocString(language), &level); + } + document->Release(); +} + bool wxWebViewIE::CanExecCommand(wxString command) { IHTMLDocument2* document = GetDocument(); diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index 9ba00d6c63..59ede2dbb4 100644 --- a/src/osx/webview_webkit.mm +++ b/src/osx/webview_webkit.mm @@ -732,42 +732,13 @@ wxString wxWebViewWebKit::GetSelectedText() return wxStringWithNSString(selection); } -wxString wxWebViewWebKit::RunScript(const wxString& javascript) +void wxWebViewWebKit::RunScript(const wxString& javascript) { if ( !m_webView ) return wxEmptyString; - id result = [[m_webView windowScriptObject] evaluateWebScript: + [[m_webView windowScriptObject] evaluateWebScript: (NSString*)wxNSStringWithWxString( javascript )]; - - NSString* resultAsString; - NSString* className = NSStringFromClass([result class]); - - if ([className isEqualToString:@"NSCFNumber"]) - { - resultAsString = [NSString stringWithFormat:@"%@", result]; - } - else if ([className isEqualToString:@"NSCFString"]) - { - resultAsString = result; - } - else if ([className isEqualToString:@"NSCFBoolean"]) - { - if ([result boolValue]) - resultAsString = @"true"; - else - resultAsString = @"false"; - } - else if ([className isEqualToString:@"WebScriptObject"]) - { - resultAsString = [result stringRepresentation]; - } - else - { - return wxString(); - } - - return wxStringWithNSString( resultAsString ); } void wxWebViewWebKit::OnSize(wxSizeEvent &event) diff --git a/tests/controls/webtest.cpp b/tests/controls/webtest.cpp index 8154a9032b..ae4cfe2245 100644 --- a/tests/controls/webtest.cpp +++ b/tests/controls/webtest.cpp @@ -43,6 +43,7 @@ private: CPPUNIT_TEST( Editable ); CPPUNIT_TEST( Selection ); CPPUNIT_TEST( Zoom ); + CPPUNIT_TEST( RunScript ); CPPUNIT_TEST_SUITE_END(); void Title(); @@ -54,6 +55,7 @@ private: void Editable(); void Selection(); void Zoom(); + void RunScript(); void LoadUrl(int times = 1); wxWebView* m_browser; @@ -236,4 +238,10 @@ void WebTestCase::Zoom() } } +void WebTestCase::RunScript() +{ + m_browser->RunScript("document.write(\"Hello World!\");"); + CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText()); +} + #endif //wxUSE_WEB