File indexing completed on 2024-04-21 11:29:45

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2005 Anders Carlsson (andersca@mac.com)
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Lesser General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Lesser General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Lesser General Public
0016  *  License along with this library; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include "domparser.h"
0021 #include "domparser.lut.h"
0022 
0023 #include "kjs_window.h"
0024 #include "xml/dom_nodeimpl.h"
0025 #include "xml/dom_docimpl.h"
0026 
0027 #include "html/html_documentimpl.h"
0028 
0029 using DOM::DocumentImpl;
0030 
0031 ////////////////////// DOMParser Object ////////////////////////
0032 
0033 /* Source for DOMParserProtoTable.
0034 @begin DOMParserProtoTable 1
0035   parseFromString DOMParser::ParseFromString DontDelete|Function 2
0036 @end
0037 */
0038 
0039 using namespace KJS;
0040 
0041 namespace KJS
0042 {
0043 
0044 KJS_DEFINE_PROTOTYPE(DOMParserProto)
0045 KJS_IMPLEMENT_PROTOFUNC(DOMParserProtoFunc)
0046 KJS_IMPLEMENT_PROTOTYPE("DOMParser", DOMParserProto, DOMParserProtoFunc, ObjectPrototype)
0047 
0048 DOMParserConstructorImp::DOMParserConstructorImp(ExecState *exec, DOM::DocumentImpl *d)
0049     : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype()), doc(d)
0050 {
0051 }
0052 
0053 bool DOMParserConstructorImp::implementsConstruct() const
0054 {
0055     return true;
0056 }
0057 
0058 JSObject *DOMParserConstructorImp::construct(ExecState *exec, const List &)
0059 {
0060     return new DOMParser(exec, doc.get());
0061 }
0062 
0063 const ClassInfo DOMParser::info = { "DOMParser", nullptr, nullptr /* &DOMParserTable*/, nullptr };
0064 
0065 DOMParser::DOMParser(ExecState *exec, DOM::DocumentImpl *d)
0066     : doc(d)
0067 {
0068     setPrototype(DOMParserProto::self(exec));
0069 }
0070 
0071 JSValue *DOMParserProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
0072 {
0073     KJS_CHECK_THIS(DOMParser, thisObj);
0074 
0075     DOMParser *parser = static_cast<DOMParser *>(thisObj);
0076 
0077     switch (id) {
0078     case DOMParser::ParseFromString: {
0079         if (args.size() != 2) {
0080             return jsUndefined();
0081         }
0082 
0083         QString str = args[0]->toString(exec).qstring();
0084         QString contentType = args[1]->toString(exec).qstring().trimmed();
0085 
0086         if (contentType == "text/xml" || contentType == "application/xml" || contentType == "application/xhtml+xml") {
0087             SharedPtr<DocumentImpl> docImpl = parser->doc->implementation()->createDocument();
0088 
0089             docImpl->open();
0090             docImpl->write(str);
0091             docImpl->finishParsing();
0092             docImpl->close();
0093 
0094             return getDOMNode(exec, docImpl.get());
0095         }
0096     }
0097     }
0098 
0099     return jsUndefined();
0100 }
0101 
0102 } // end namespace
0103