summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/script/bridge/qscriptqobject.cpp51
-rw-r--r--tests/auto/qscriptqobject/tst_qscriptqobject.cpp32
2 files changed, 80 insertions, 3 deletions
diff --git a/src/script/bridge/qscriptqobject.cpp b/src/script/bridge/qscriptqobject.cpp
index e289aad..36c601f 100644
--- a/src/script/bridge/qscriptqobject.cpp
+++ b/src/script/bridge/qscriptqobject.cpp
@@ -27,6 +27,9 @@
#include "JSFunction.h"
#include "JSString.h"
#include "JSValue.h"
+#include "JSArray.h"
+#include "RegExpObject.h"
+#include "RegExpConstructor.h"
QT_BEGIN_NAMESPACE
@@ -1535,7 +1538,53 @@ static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncFindChild(JSC::ExecState *exec
static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncFindChildren(JSC::ExecState *exec, JSC::JSObject*,
JSC::JSValue thisValue, const JSC::ArgList &args)
{
- return throwError(exec, JSC::GeneralError, "QObject.prototype.findChildren not implemented");
+ // extract the QObject
+ if (!thisValue.isObject(&QScriptObject::info))
+ return throwError(exec, JSC::TypeError, "this object is not a QObject");
+ QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(thisValue));
+ QScriptObjectDelegate *delegate = scriptObject->delegate();
+ if (!delegate || (delegate->type() != QScriptObjectDelegate::QtObject))
+ return throwError(exec, JSC::TypeError, "this object is not a QObject");
+ const QObject *const obj = static_cast<QObjectDelegate*>(delegate)->value();
+
+ // find the children
+ QList<QObject *> children;
+ if (args.size() != 0) {
+ const JSC::JSValue arg = args.at(0);
+ if (arg.isObject(&JSC::RegExpObject::info)) {
+ const QObjectList allChildren= obj->children();
+
+ JSC::RegExpObject *const regexp = JSC::asRegExpObject(arg);
+
+ const int allChildrenCount = allChildren.size();
+ for (int i = 0; i < allChildrenCount; ++i) {
+ QObject *const child = allChildren.at(i);
+ const JSC::UString childName = qtStringToJSCUString(child->objectName());
+ JSC::RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
+ int position;
+ int length;
+ regExpConstructor->performMatch(regexp->regExp(), childName, 0, position, length);
+ if (position >= 0)
+ children.append(child);
+ }
+ } else {
+ const QString name = QScript::qtStringFromJSCUString(args.at(0).toString(exec));
+ children = qFindChildren<QObject*>(obj, name);
+ }
+ } else {
+ children = qFindChildren<QObject*>(obj, QString());
+ }
+ // create the result array with the children
+ const int length = children.size();
+ JSC::JSArray *const result = JSC::constructEmptyArray(exec, length);
+
+ QScriptEngine::QObjectWrapOptions opt = QScriptEngine::PreferExistingWrapperObject;
+ QScriptEnginePrivate *engine = static_cast<QScript::GlobalObject*>(exec->lexicalGlobalObject())->engine;
+ for (int i = 0; i < length; ++i) {
+ QObject *const child = children.at(i);
+ result->put(exec, i, engine->newQObject(child, QScriptEngine::QtOwnership, opt));
+ }
+ return JSC::JSValue(result);
}
static JSC::JSValue JSC_HOST_CALL qobjectProtoFuncToString(JSC::ExecState *exec, JSC::JSObject*,
diff --git a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp
index 1f89022..c456f1c 100644
--- a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp
+++ b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp
@@ -2276,7 +2276,7 @@ void tst_QScriptExtQObject::findChild()
QCOMPARE(result.isNull(), true);
}
- {
+ {
QScriptValue result = m_engine->evaluate("myObject.findChild('myChildObject')");
QCOMPARE(result.isQObject(), true);
QCOMPARE(result.toQObject(), child);
@@ -2287,7 +2287,6 @@ void tst_QScriptExtQObject::findChild()
void tst_QScriptExtQObject::findChildren()
{
- QSKIP("Not implemented", SkipAll);
QObject *child = new QObject(m_myObject);
child->setObjectName(QLatin1String("myChildObject"));
@@ -2353,6 +2352,35 @@ void tst_QScriptExtQObject::findChildren()
QVERIFY(count == 0);
}
+ // matchall regexp
+ {
+ QScriptValue result = m_engine->evaluate("myObject.findChildren(/.*/)");
+ QVERIFY(result.isArray());
+ int count = 3;
+ QCOMPARE(result.property("length").toInt32(), count);
+ for (int i = 0; i < 3; ++i) {
+ QObject *o = result.property(i).toQObject();
+ if (o == namelessChild || o == child || o == anotherChild)
+ --count;
+ }
+ QVERIFY(count == 0);
+ }
+
+ // matchall regexp my*
+ {
+ QScriptValue result = m_engine->evaluate("myObject.findChildren(new RegExp(\"^my.*\"))");
+ QCOMPARE(result.isArray(), true);
+ QCOMPARE(result.property(QLatin1String("length")).toNumber(), 2.0);
+ QObject *o1 = result.property(QLatin1String("0")).toQObject();
+ QObject *o2 = result.property(QLatin1String("1")).toQObject();
+ if (o1 != child) {
+ QCOMPARE(o1, anotherChild);
+ QCOMPARE(o2, child);
+ } else {
+ QCOMPARE(o1, child);
+ QCOMPARE(o2, anotherChild);
+ }
+ }
delete anotherChild;
delete namelessChild;
delete child;