diff --git a/src/ecmascript/spidermonkey/document.c b/src/ecmascript/spidermonkey/document.c index 25feaa4f2..77df80834 100644 --- a/src/ecmascript/spidermonkey/document.c +++ b/src/ecmascript/spidermonkey/document.c @@ -463,6 +463,7 @@ static bool document_write(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool document_writeln(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool document_replace(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *rval); +static bool document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool document_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *rval); @@ -471,6 +472,7 @@ const spidermonkeyFunctionSpec document_funcs[] = { { "writeln", document_writeln, 1 }, { "replace", document_replace, 1 }, { "getElementById", document_getElementById, 1 }, + { "getElementsByClassName", document_getElementsByClassName, 1 }, { "getElementsByName", document_getElementsByName, 1 }, { "getElementsByTagName", document_getElementsByTagName, 1 }, { NULL } @@ -770,6 +772,64 @@ document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static bool +document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + JS::CallArgs args = CallArgsFromVp(argc, vp); + + if (argc != 1) { + args.rval().setBoolean(false); + return true; + } + + JSCompartment *comp = js::GetContextCompartment(ctx); + struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp); + struct document_view *doc_view = interpreter->vs->doc_view; + struct document *document = doc_view->document; + + if (!document->dom) { + document->dom = document_parse(document); + } + + if (!document->dom) { + args.rval().setNull(); + return true; + } + + xmlpp::Element* root = (xmlpp::Element *)document->dom; + + struct string idstr; + + init_string(&idstr); + jshandle_value_to_char_string(&idstr, ctx, &args[0]); + std::string id = idstr.source; + + std::string xpath = "//*[@class=\""; + xpath += id; + xpath += "\"]"; + + done_string(&idstr); + + xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + + *elements = root->find(xpath); + + if (elements->size() == 0) { + args.rval().setNull(); + return true; + } + + JSObject *elem = getCollection(ctx, elements); + + if (elem) { + args.rval().setObject(*elem); + } else { + args.rval().setNull(); + } + + return true; +} + static bool document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *vp) { diff --git a/test/ecmascript/getElementsByClassName.html b/test/ecmascript/getElementsByClassName.html new file mode 100644 index 000000000..0f6032ef5 --- /dev/null +++ b/test/ecmascript/getElementsByClassName.html @@ -0,0 +1,22 @@ + + + + +First Name:
+First Name: + +

Click the button to get the tag name of the first element in the document that has a name attribute with the value "fname".

+ + + +

+ + + + +