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
This commit is contained in:
Steve Lamerton 2011-07-15 12:38:47 +00:00
parent 41933aa5a0
commit c9ccc09c64
9 changed files with 45 additions and 34 deletions

View File

@ -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;

View File

@ -106,6 +106,7 @@ public:
virtual wxString GetSelectedSource();
virtual void ClearSelection();
virtual void RunScript(const wxString& javascript);
// ---- IE-specific methods

View File

@ -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);

View File

@ -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?

View File

@ -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.

View File

@ -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))

View File

@ -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();

View File

@ -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)

View File

@ -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