File indexing completed on 2024-04-28 15:29:24
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2010 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KPARTS_SELECTORINTERFACE_H 0009 #define KPARTS_SELECTORINTERFACE_H 0010 0011 #include <kparts/kparts_export.h> 0012 0013 #include <QObject> 0014 #include <QSharedDataPointer> 0015 #include <QStringList> 0016 0017 class QString; 0018 template<typename T> 0019 class QList; 0020 0021 namespace KParts 0022 { 0023 /** 0024 * @class SelectorInterface selectorinterface.h <KParts/SelectorInterface> 0025 * 0026 * @short Optional base class for HtmlExtension-derived classes 0027 * Provides DOM Selector like API: querySelector and querySelectorAll, 0028 * in order to find specific elements in an HTML document. 0029 * 0030 * Example: 0031 * @code 0032 * const QList<SelectorInterface::Element> elements = selectorInterface->querySelectorAll("head > link[rel=\"alternate\"]"); 0033 * @endcode 0034 */ 0035 class KPARTS_EXPORT SelectorInterface 0036 { 0037 public: 0038 class ElementPrivate; 0039 class Element; 0040 0041 /** 0042 * Query methods. 0043 * @see QueryMethods 0044 */ 0045 enum QueryMethod { 0046 None = 0x00, /*!< Querying is not possible. */ 0047 EntireContent = 0x01, /*!< Query or can query the entire content. */ 0048 SelectedContent = 0x02, /*!< Query or can query only the user selected content, if any. */ 0049 }; 0050 /** 0051 * Stores a combination of #QueryMethod values. 0052 */ 0053 Q_DECLARE_FLAGS(QueryMethods, QueryMethod) 0054 0055 /** 0056 * Destructor 0057 */ 0058 virtual ~SelectorInterface() 0059 { 0060 } 0061 0062 /** 0063 * Returns the supported query methods. 0064 * 0065 * By default this function returns None. 0066 * 0067 * @see QueryMethod 0068 */ 0069 virtual QueryMethods supportedQueryMethods() const; 0070 0071 /** 0072 * Returns the first (in document order) element in this fragment matching 0073 * the given CSS selector @p query and querying method @p method. 0074 * 0075 * Note that since the returned item is static snapshot, i.e. not live, it 0076 * will not be updated when the document changes. 0077 * 0078 * If the querying method specified by @p method is not supported or cannot be 0079 * handled, then a null element is returned. 0080 * 0081 * @see supportedQueryMethods 0082 * @see QueryMethod 0083 */ 0084 virtual Element querySelector(const QString &query, QueryMethod method) const = 0; 0085 0086 /** 0087 * Returns all (in document order) elements in this fragment matching the 0088 * given CSS selector @p query and querying method @p method. 0089 * 0090 * Note that since the returned list is static snapshot, i.e. not live, it 0091 * will not be updated when the document changes. 0092 * 0093 * If the querying method specified by @p method is not supported or cannot be 0094 * handled, then an empty list is returned. 0095 * 0096 * @see supportedQueryMethods 0097 * @see QueryMethod 0098 */ 0099 virtual QList<Element> querySelectorAll(const QString &query, QueryMethod method) const = 0; 0100 0101 class KPARTS_EXPORT Element 0102 { 0103 public: 0104 /** 0105 * Constructor 0106 */ 0107 Element(); 0108 0109 /** 0110 * Copy constructor 0111 */ 0112 Element(const Element &other); 0113 0114 /** 0115 * Destructor 0116 */ 0117 ~Element(); 0118 0119 /** 0120 * Returns true if the element is null ; otherwise returns false. 0121 */ 0122 bool isNull() const; 0123 0124 /** 0125 * Sets the tag name of this element. 0126 */ 0127 void setTagName(const QString &tag); 0128 0129 /** 0130 * Returns the tag name of this element. 0131 */ 0132 QString tagName() const; 0133 0134 /** 0135 * Adds an attribute with the given name and value. 0136 * If an attribute with the same name exists, its value is replaced by value. 0137 */ 0138 void setAttribute(const QString &name, const QString &value); 0139 0140 /** 0141 * Returns the list of attributes in this element. 0142 */ 0143 QStringList attributeNames() const; 0144 0145 /** 0146 * Returns the attribute with the given name. If the attribute does not exist, defaultValue is returned. 0147 */ 0148 QString attribute(const QString &name, const QString &defaultValue = QString()) const; 0149 0150 /** 0151 * Returns true if the attribute with the given @p name exists. 0152 */ 0153 bool hasAttribute(const QString &name) const; 0154 0155 // No namespace support yet, could be added with attributeNS, setAttributeNS 0156 0157 /** 0158 * Swaps the contents of @p other with the contents of this. 0159 */ 0160 void swap(Element &other) 0161 { 0162 d.swap(other.d); 0163 } 0164 0165 /** 0166 * Assignment operator 0167 */ 0168 Element &operator=(const Element &other) 0169 { 0170 if (this != &other) { 0171 Element copy(other); 0172 swap(copy); 0173 } 0174 return *this; 0175 } 0176 0177 private: 0178 QSharedDataPointer<ElementPrivate> d; 0179 }; 0180 }; 0181 0182 Q_DECLARE_OPERATORS_FOR_FLAGS(SelectorInterface::QueryMethods) 0183 0184 } // namespace KParts 0185 0186 inline void qSwap(KParts::SelectorInterface::Element &lhs, KParts::SelectorInterface::Element &rhs) 0187 { 0188 lhs.swap(rhs); 0189 } 0190 0191 Q_DECLARE_TYPEINFO(KParts::SelectorInterface::Element, Q_MOVABLE_TYPE); 0192 0193 Q_DECLARE_INTERFACE(KParts::SelectorInterface, "org.kde.KParts.SelectorInterface") 0194 0195 #endif /* KPARTS_SELECTORINTERFACE_H */