Commit b7297d10 authored by ariya@webkit.org's avatar ariya@webkit.org

2009-04-29 Ariya Hidayat <ariya.hidayat@nokia.com>

        Reviewed by Simon Hausmann.

        Implement QWebElement::evaluateScript.

        * Api/qwebelement.cpp:
        (setupScriptContext):
        (QWebElement::evaluateScript):
        * Api/qwebelement.h:
        * tests/qwebelement/tst_qwebelement.cpp:
        (tst_QWebElement::evaluateScript):

git-svn-id: svn://svn.chromium.org/blink/trunk@42995 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 2bb5409c
......@@ -538,6 +538,35 @@ QWebFrame *QWebElement::webFrame() const
return QWebFramePrivate::kit(frame);
}
static bool setupScriptContext(WebCore::Element* element, JSC::JSValuePtr& thisValue, ScriptState*& state, ScriptController*& scriptController)
{
if (!element)
return false;
Document* document = element->document();
if (!document)
return false;
Frame* frame = document->frame();
if (!frame)
return false;
scriptController = frame->script();
if (!scriptController)
return false;
state = scriptController->globalObject()->globalExec();
if (!state)
return false;
thisValue = toJS(state, element);
if (!thisValue)
return false;
return true;
}
static bool setupScriptObject(WebCore::Element* element, ScriptObject& object, ScriptState*& state, ScriptController*& scriptController)
{
if (!element)
......@@ -567,6 +596,37 @@ static bool setupScriptObject(WebCore::Element* element, ScriptObject& object, S
return true;
}
/*!
Executes the \a scriptSource with this element as the `this' object.
\sa callFunction()
*/
QVariant QWebElement::evaluateScript(const QString& scriptSource)
{
if (scriptSource.isEmpty())
return QVariant();
ScriptState* state = 0;
JSC::JSValuePtr thisValue;
ScriptController* scriptController = 0;
if (!setupScriptContext(m_element, thisValue, state, scriptController))
return QVariant();
JSC::ScopeChain& scopeChain = state->dynamicGlobalObject()->globalScopeChain();
JSC::UString script((const ushort*)scriptSource.data(), scriptSource.length());
JSC::Completion completion = JSC::evaluate(state, scopeChain, JSC::makeSource(script), thisValue);
if ((completion.complType() != JSC::ReturnValue) && (completion.complType() != JSC::Normal))
return QVariant();
JSC::JSValuePtr result = completion.value();
if (!result)
return QVariant();
int distance = 0;
return JSC::Bindings::convertValueToQVariant(state, result, QMetaType::Void, &distance);
}
/*!
Calls the function with the given \a name and \a arguments.
......
......@@ -118,6 +118,8 @@ public:
void removeFromDocument();
void removeChildren();
QVariant evaluateScript(const QString& scriptSource);
QVariant callFunction(const QString &functionName, const QVariantList &arguments = QVariantList());
QStringList functions() const;
......
2009-04-29 Ariya Hidayat <ariya.hidayat@nokia.com>
Reviewed by Simon Hausmann.
Implement QWebElement::evaluateScript.
* Api/qwebelement.cpp:
(setupScriptContext):
(QWebElement::evaluateScript):
* Api/qwebelement.h:
* tests/qwebelement/tst_qwebelement.cpp:
(tst_QWebElement::evaluateScript):
2009-04-29 Simon Hausmann <simon.hausmann@nokia.com>
Reviewed by Ariya Hidayat.
......
......@@ -71,6 +71,7 @@ private slots:
void classes();
void namespaceURI();
void foreachManipulation();
void evaluateScript();
void callFunction();
void callFunctionSubmitForm();
void functionNames();
......@@ -280,6 +281,32 @@ void tst_QWebElement::foreachManipulation()
QCOMPARE(body.findAll("div").count(), 4);
}
void tst_QWebElement::evaluateScript()
{
QVariant result;
m_mainFrame->setHtml("<body><p>test");
QWebElement para = m_mainFrame->findFirstElement("p");
result = para.evaluateScript("this.tagName");
QVERIFY(result.isValid());
QVERIFY(result.type() == QVariant::String);
QCOMPARE(result.toString(), QLatin1String("P"));
QVERIFY(para.functions().contains("hasAttributes"));
result = para.evaluateScript("this.hasAttributes()");
QVERIFY(result.isValid());
QVERIFY(result.type() == QVariant::Bool);
QVERIFY(!result.toBool());
para.evaluateScript("this.setAttribute('align', 'left');");
QCOMPARE(para.attribute("align"), QLatin1String("left"));
result = para.evaluateScript("this.hasAttributes()");
QVERIFY(result.isValid());
QVERIFY(result.type() == QVariant::Bool);
QVERIFY(result.toBool());
}
void tst_QWebElement::callFunction()
{
m_mainFrame->setHtml("<body><p>test");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment