File indexing completed on 2024-04-28 15:23:20

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2010 Maksim Orlovich <maksim@kde.org>
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 #include "kjs_binding.h"
0020 #include "xml/dom3_xpathimpl.h"
0021 
0022 namespace KJS
0023 {
0024 
0025 DEFINE_PSEUDO_CONSTRUCTOR(XPathResultPseudoCtor)
0026 
0027 class XPathResult: public DOMWrapperObject<khtml::XPathResultImpl>
0028 {
0029 public:
0030     XPathResult(ExecState *exec, khtml::XPathResultImpl *impl);
0031 
0032     bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot &) override;
0033     using JSObject::getOwnPropertySlot;
0034     JSValue *getValueProperty(ExecState *exec, int token) const;
0035 
0036     const ClassInfo *classInfo() const override
0037     {
0038         return &info;
0039     }
0040     static const ClassInfo info;
0041 
0042     // The various constants are in separate constant table node
0043     enum {
0044         // properties:
0045         ResultType, NumberValue, StringValue, BooleanValue,
0046         SingleNodeValue, InvalidIteratorState, SnapshotLength,
0047         // functions:
0048         IterateNext, SnapshotItem
0049     };
0050 };
0051 
0052 DEFINE_PSEUDO_CONSTRUCTOR(XPathExpressionPseudoCtor)
0053 
0054 class XPathExpression: public DOMWrapperObject<khtml::XPathExpressionImpl>
0055 {
0056 public:
0057     XPathExpression(ExecState *exec, khtml::XPathExpressionImpl *impl);
0058 
0059     const ClassInfo *classInfo() const override
0060     {
0061         return &info;
0062     }
0063     static const ClassInfo info;
0064 
0065     enum {
0066         // functions:
0067         Evaluate
0068     };
0069 
0070     void mark() override;
0071 
0072     void setAssociatedResolver(JSObject *res)
0073     {
0074         jsResolver = res;
0075     }
0076 private:
0077     JSObject *jsResolver; // see notes below.
0078 };
0079 
0080 // For NS resolver, we need to do two things:
0081 // 1) wrap native NS resolvers, such as those returned by
0082 //    Document::createNSResolver
0083 //
0084 // 2) Pass in JS-implemented resolvers to DOM methods, which might retain them.
0085 // This is a bit tricky memory management-wise, as it involves the DOM
0086 // referring to a JS object. The solution we take is to have the wrapper
0087 // for XPathExpression mark the corresponding resolver.
0088 //
0089 // The class XPathNSResolver   does (1)
0090 // The class JSXPathNSResolver does (2).
0091 //
0092 // Further, the method ... to avoid having wrappers-inside-wrappers
0093 DEFINE_PSEUDO_CONSTRUCTOR(XPathNSResolverPseudoCtor)
0094 class XPathNSResolver: public DOMWrapperObject<khtml::XPathNSResolverImpl>
0095 {
0096 public:
0097     XPathNSResolver(ExecState *exec, khtml::XPathNSResolverImpl *impl);
0098 
0099     const ClassInfo *classInfo() const override
0100     {
0101         return &info;
0102     }
0103     static const ClassInfo info;
0104 
0105     enum {
0106         // functions:
0107         LookupNamespaceURI
0108     };
0109 };
0110 
0111 class JSXPathNSResolver: public khtml::XPathNSResolverImpl
0112 {
0113 public:
0114     JSXPathNSResolver(Interpreter *ctx, JSObject *impl);
0115     Type type() override;
0116     DOM::DOMString lookupNamespaceURI(const DOM::DOMString &prefix) override;
0117 
0118     JSObject *resolverObject()
0119     {
0120         return impl;
0121     }
0122 private:
0123     JSObject *impl;
0124     Interpreter *ctx;
0125 };
0126 
0127 // Convert JS -> DOM. Might make a new JSXPathNSResolver. It does not
0128 // protect the JS resolver from collection in any way.
0129 khtml::XPathNSResolverImpl *toResolver(ExecState *exec, JSValue *impl);
0130 
0131 }
0132