#include "domparser.h"
#include "domparser.lut.h"
#include "html/html_documentimpl.h"
#include "misc/loader.h"
using DOM::DocumentImpl;
using namespace KJS;
DEFINE_PROTOTYPE("DOMParser",DOMParserProto)
IMPLEMENT_PROTOFUNC(DOMParserProtoFunc)
IMPLEMENT_PROTOTYPE(DOMParserProto,DOMParserProtoFunc)
namespace KJS {
DOMParserConstructorImp::DOMParserConstructorImp(ExecState *, DOM::DocumentImpl *d)
: doc(d)
{
}
bool DOMParserConstructorImp::implementsConstruct() const
{
return true;
}
Object DOMParserConstructorImp::construct(ExecState *exec, const List &)
{
return Object(new DOMParser(exec, doc.get()));
}
const ClassInfo DOMParser::info = { "DOMParser", 0, &DOMParserTable, 0 };
DOMParser::DOMParser(ExecState *exec, DOM::DocumentImpl *d)
: doc(d)
{
setPrototype(DOMParserProto::self(exec));
}
Value DOMParserProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
{
if (!thisObj.inherits(&DOMParser::info)) {
Object err = Error::create(exec,TypeError);
exec->setException(err);
return err;
}
DOMParser *parser = static_cast<DOMParser *>(thisObj.imp());
switch (id) {
case DOMParser::ParseFromString:
{
if (args.size() != 2) {
return Undefined();
}
QString str = args[0].toString(exec).qstring();
QString contentType = args[1].toString(exec).qstring().stripWhiteSpace();
if (khtml::isXMLMIMEType(contentType)) {
DocumentImpl *docImpl = parser->doc->implementation()->createDocument();
docImpl->open();
docImpl->write(str);
docImpl->finishParsing();
docImpl->close();
return getDOMNode(exec, docImpl);
}
}
}
return Undefined();
}
}