KHtml

domparser.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2005 Anders Carlsson ([email protected])
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "domparser.h"
21 #include "domparser.lut.h"
22 
23 #include "kjs_window.h"
24 #include "xml/dom_nodeimpl.h"
25 #include "xml/dom_docimpl.h"
26 
27 #include "html/html_documentimpl.h"
28 
29 using DOM::DocumentImpl;
30 
31 ////////////////////// DOMParser Object ////////////////////////
32 
33 /* Source for DOMParserProtoTable.
34 @begin DOMParserProtoTable 1
35  parseFromString DOMParser::ParseFromString DontDelete|Function 2
36 @end
37 */
38 
39 using namespace KJS;
40 
41 namespace KJS
42 {
43 
44 KJS_DEFINE_PROTOTYPE(DOMParserProto)
45 KJS_IMPLEMENT_PROTOFUNC(DOMParserProtoFunc)
46 KJS_IMPLEMENT_PROTOTYPE("DOMParser", DOMParserProto, DOMParserProtoFunc, ObjectPrototype)
47 
48 DOMParserConstructorImp::DOMParserConstructorImp(ExecState *exec, DOM::DocumentImpl *d)
49  : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype()), doc(d)
50 {
51 }
52 
53 bool DOMParserConstructorImp::implementsConstruct() const
54 {
55  return true;
56 }
57 
58 JSObject *DOMParserConstructorImp::construct(ExecState *exec, const List &)
59 {
60  return new DOMParser(exec, doc.get());
61 }
62 
63 const ClassInfo DOMParser::info = { "DOMParser", nullptr, nullptr /* &DOMParserTable*/, nullptr };
64 
65 DOMParser::DOMParser(ExecState *exec, DOM::DocumentImpl *d)
66  : doc(d)
67 {
68  setPrototype(DOMParserProto::self(exec));
69 }
70 
71 JSValue *DOMParserProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
72 {
73  KJS_CHECK_THIS(DOMParser, thisObj);
74 
75  DOMParser *parser = static_cast<DOMParser *>(thisObj);
76 
77  switch (id) {
78  case DOMParser::ParseFromString: {
79  if (args.size() != 2) {
80  return jsUndefined();
81  }
82 
83  QString str = args[0]->toString(exec).qstring();
84  QString contentType = args[1]->toString(exec).qstring().trimmed();
85 
86  if (contentType == "text/xml" || contentType == "application/xml" || contentType == "application/xhtml+xml") {
87  SharedPtr<DocumentImpl> docImpl = parser->doc->implementation()->createDocument();
88 
89  docImpl->open();
90  docImpl->write(str);
91  docImpl->finishParsing();
92  docImpl->close();
93 
94  return getDOMNode(exec, docImpl.get());
95  }
96  }
97  }
98 
99  return jsUndefined();
100 }
101 
102 } // end namespace
103 
This library provides a full-featured HTML parser and widget.
int size() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Oct 15 2021 22:48:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.