From 75f9a954480942d56202ac08274bf1b23141078d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jul 2013 14:10:17 +0000 Subject: [PATCH] Use [DOMRange markupString] to get selection source. This DOMRange method, previously used in GetSelectedText(), seems to provide exactly what we need so there doesn't seem to be any reason to use JS to get the selection text, especially as it didn't even work under OS X 10.8 and returned an empty string in the unit test. The unit test still needs adjustment to pass because we don't get back exactly the same HTML as we used originally, but with more relaxed matching it does pass now. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74541 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/osx/webview_webkit.mm | 12 +++++------- tests/controls/webtest.cpp | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index 17f273603c..59617c81cf 100644 --- a/src/osx/webview_webkit.mm +++ b/src/osx/webview_webkit.mm @@ -906,13 +906,11 @@ void wxWebViewWebKit::SelectAll() wxString wxWebViewWebKit::GetSelectedSource() const { - wxString script = ("var range = window.getSelection().getRangeAt(0);" - "var element = document.createElement('div');" - "element.appendChild(range.cloneContents());" - "return element.innerHTML;"); - NSString *result = [m_webView stringByEvaluatingJavaScriptFromString: - wxNSStringWithWxString(script)]; - return wxStringWithNSString(result); + DOMRange* dr = [m_webView selectedDOMRange]; + if ( !dr ) + return wxString(); + + return wxStringWithNSString([dr markupString]); } wxString wxWebViewWebKit::GetPageText() const diff --git a/tests/controls/webtest.cpp b/tests/controls/webtest.cpp index 6e7bd6da4c..fbb1bcd2ca 100644 --- a/tests/controls/webtest.cpp +++ b/tests/controls/webtest.cpp @@ -220,9 +220,18 @@ void WebTestCase::Selection() CPPUNIT_ASSERT(m_browser->HasSelection()); CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText()); - //We lower case the result as ie returns tags in uppercase - CPPUNIT_ASSERT_EQUAL("some strong text", - m_browser->GetSelectedSource().Lower()); + + // The web engine doesn't necessarily represent the HTML in the same way as + // we used above, e.g. IE uses upper case for all the tags while WebKit + // under OS X inserts plenty of its own tags, so don't test for + // equality and just check that the source contains things we'd expect it + // to. + const wxString selSource = m_browser->GetSelectedSource(); + WX_ASSERT_MESSAGE + ( + ("Unexpected selection source: \"%s\"", selSource), + selSource.Lower().Matches("*some**text*") + ); m_browser->ClearSelection(); CPPUNIT_ASSERT(!m_browser->HasSelection()); -- 2.45.2