• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8 
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13 
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19 
20 #ifndef QWEBELEMENT_H
21 #define QWEBELEMENT_H
22 
23 #include <QtCore/qstring.h>
24 #include <QtCore/qstringlist.h>
25 #include <QtCore/qrect.h>
26 #include <QtCore/qvariant.h>
27 #include <QtCore/qshareddata.h>
28 
29 #include "qwebkitglobal.h"
30 namespace WebCore {
31     class Element;
32     class Node;
33 }
34 
35 
36 #if defined(WTF_USE_V8) && WTF_USE_V8
37 namespace V8 {
38     namespace Bindings {
39     class QtWebElementRuntime;
40     }
41 }
42 #else
43 namespace JSC {
44     namespace Bindings {
45     class QtWebElementRuntime;
46     }
47 }
48 #endif
49 
50 QT_BEGIN_NAMESPACE
51 class QPainter;
52 QT_END_NAMESPACE
53 
54 class QWebFrame;
55 class QWebElementCollection;
56 class QWebElementPrivate;
57 
58 class QWEBKIT_EXPORT QWebElement {
59 public:
60     QWebElement();
61     QWebElement(const QWebElement&);
62     QWebElement &operator=(const QWebElement&);
63     ~QWebElement();
64 
65     bool operator==(const QWebElement& o) const;
66     bool operator!=(const QWebElement& o) const;
67 
68     bool isNull() const;
69 
70     QWebElementCollection findAll(const QString &selectorQuery) const;
71     QWebElement findFirst(const QString &selectorQuery) const;
72 
73     void setPlainText(const QString& text);
74     QString toPlainText() const;
75 
76     void setOuterXml(const QString& markup);
77     QString toOuterXml() const;
78 
79     void setInnerXml(const QString& markup);
80     QString toInnerXml() const;
81 
82     void setAttribute(const QString& name, const QString& value);
83     void setAttributeNS(const QString& namespaceUri, const QString& name, const QString& value);
84     QString attribute(const QString& name, const QString& defaultValue = QString()) const;
85     QString attributeNS(const QString& namespaceUri, const QString& name, const QString& defaultValue = QString()) const;
86     bool hasAttribute(const QString& name) const;
87     bool hasAttributeNS(const QString& namespaceUri, const QString& name) const;
88     void removeAttribute(const QString& name);
89     void removeAttributeNS(const QString& namespaceUri, const QString& name);
90     bool hasAttributes() const;
91     QStringList attributeNames(const QString& namespaceUri = QString()) const;
92 
93     QStringList classes() const;
94     bool hasClass(const QString& name) const;
95     void addClass(const QString& name);
96     void removeClass(const QString& name);
97     void toggleClass(const QString& name);
98 
99     bool hasFocus() const;
100     void setFocus();
101 
102     QRect geometry() const;
103 
104     QString tagName() const;
105     QString prefix() const;
106     QString localName() const;
107     QString namespaceUri() const;
108 
109     QWebElement parent() const;
110     QWebElement firstChild() const;
111     QWebElement lastChild() const;
112     QWebElement nextSibling() const;
113     QWebElement previousSibling() const;
114     QWebElement document() const;
115     QWebFrame *webFrame() const;
116 
117     // TODO: Add QWebElementCollection overloads
118     // docs need example snippet
119     void appendInside(const QString& markup);
120     void appendInside(const QWebElement& element);
121 
122     // docs need example snippet
123     void prependInside(const QString& markup);
124     void prependInside(const QWebElement& element);
125 
126     // docs need example snippet
127     void appendOutside(const QString& markup);
128     void appendOutside(const QWebElement& element);
129 
130     // docs need example snippet
131     void prependOutside(const QString& markup);
132     void prependOutside(const QWebElement& element);
133 
134     // docs need example snippet
135     void encloseContentsWith(const QWebElement& element);
136     void encloseContentsWith(const QString& markup);
137     void encloseWith(const QString& markup);
138     void encloseWith(const QWebElement& element);
139 
140     void replace(const QString& markup);
141     void replace(const QWebElement& element);
142 
143     QWebElement clone() const;
144     QWebElement& takeFromDocument();
145     void removeFromDocument();
146     void removeAllChildren();
147 
148     QVariant evaluateJavaScript(const QString& scriptSource);
149 
150     enum StyleResolveStrategy {
151          InlineStyle,
152          CascadedStyle,
153          ComputedStyle,
154     };
155     QString styleProperty(const QString& name, StyleResolveStrategy strategy) const;
156     void setStyleProperty(const QString& name, const QString& value);
157 
158     void render(QPainter* painter);
159     void render(QPainter* painter, const QRect& clipRect);
160 
161 private:
162     explicit QWebElement(WebCore::Element*);
163     explicit QWebElement(WebCore::Node*);
164 
165     static QWebElement enclosingElement(WebCore::Node*);
166 
167     friend class DumpRenderTreeSupportQt;
168     friend class QWebFrame;
169     friend class QWebElementCollection;
170     friend class QWebHitTestResult;
171     friend class QWebHitTestResultPrivate;
172     friend class QWebPage;
173 
174 #if defined(WTF_USE_V8) && WTF_USE_V8
175     friend class V8::Bindings::QtWebElementRuntime;
176 #else
177     friend class JSC::Bindings::QtWebElementRuntime;
178 #endif
179 
180     QWebElementPrivate* d;
181     WebCore::Element* m_element;
182 };
183 
184 class QWebElementCollectionPrivate;
185 
186 class QWEBKIT_EXPORT QWebElementCollection
187 {
188 public:
189     QWebElementCollection();
190     QWebElementCollection(const QWebElement &contextElement, const QString &query);
191     QWebElementCollection(const QWebElementCollection &);
192     QWebElementCollection &operator=(const QWebElementCollection &);
193     ~QWebElementCollection();
194 
195     QWebElementCollection operator+(const QWebElementCollection &other) const;
196     inline QWebElementCollection &operator+=(const QWebElementCollection &other)
197     {
198         append(other); return *this;
199     }
200 
201     void append(const QWebElementCollection &collection);
202 
203     int count() const;
204     QWebElement at(int i) const;
205     inline QWebElement operator[](int i) const { return at(i); }
206 
first()207     inline QWebElement first() const { return at(0); }
last()208     inline QWebElement last() const { return at(count() - 1); }
209 
210     QList<QWebElement> toList() const;
211 
212     class const_iterator {
213        public:
const_iterator(const QWebElementCollection * collection,int index)214            inline const_iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {}
const_iterator(const const_iterator & o)215            inline const_iterator(const const_iterator& o) : i(o.i), collection(o.collection) {}
216 
217            inline const QWebElement operator*() const { return collection->at(i); }
218 
219            inline bool operator==(const const_iterator& o) const { return i == o.i && collection == o.collection; }
220            inline bool operator!=(const const_iterator& o) const { return i != o.i || collection != o.collection; }
221            inline bool operator<(const const_iterator& o) const { return i < o.i; }
222            inline bool operator<=(const const_iterator& o) const { return i <= o.i; }
223            inline bool operator>(const const_iterator& o) const { return i > o.i; }
224            inline bool operator>=(const const_iterator& o) const { return i >= o.i; }
225 
226            inline const_iterator& operator++() { ++i; return *this; }
227            inline const_iterator operator++(int) { const_iterator n(collection, i); ++i; return n; }
228            inline const_iterator& operator--() { i--; return *this; }
229            inline const_iterator operator--(int) { const_iterator n(collection, i); i--; return n; }
230            inline const_iterator& operator+=(int j) { i += j; return *this; }
231            inline const_iterator& operator-=(int j) { i -= j; return *this; }
232            inline const_iterator operator+(int j) const { return const_iterator(collection, i + j); }
233            inline const_iterator operator-(int j) const { return const_iterator(collection, i - j); }
234            inline int operator-(const_iterator j) const { return i - j.i; }
235        private:
236             int i;
237             const QWebElementCollection* const collection;
238     };
239     friend class const_iterator;
240 
begin()241     inline const_iterator begin() const { return constBegin(); }
end()242     inline const_iterator end() const { return constEnd(); }
constBegin()243     inline const_iterator constBegin() const { return const_iterator(this, 0); }
constEnd()244     inline const_iterator constEnd() const { return const_iterator(this, count()); };
245 
246     class iterator {
247     public:
iterator(const QWebElementCollection * collection,int index)248         inline iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {}
iterator(const iterator & o)249         inline iterator(const iterator& o) : i(o.i), collection(o.collection) {}
250 
251         inline QWebElement operator*() const { return collection->at(i); }
252 
253         inline bool operator==(const iterator& o) const { return i == o.i && collection == o.collection; }
254         inline bool operator!=(const iterator& o) const { return i != o.i || collection != o.collection; }
255         inline bool operator<(const iterator& o) const { return i < o.i; }
256         inline bool operator<=(const iterator& o) const { return i <= o.i; }
257         inline bool operator>(const iterator& o) const { return i > o.i; }
258         inline bool operator>=(const iterator& o) const { return i >= o.i; }
259 
260         inline iterator& operator++() { ++i; return *this; }
261         inline iterator operator++(int) { iterator n(collection, i); ++i; return n; }
262         inline iterator& operator--() { i--; return *this; }
263         inline iterator operator--(int) { iterator n(collection, i); i--; return n; }
264         inline iterator& operator+=(int j) { i += j; return *this; }
265         inline iterator& operator-=(int j) { i -= j; return *this; }
266         inline iterator operator+(int j) const { return iterator(collection, i + j); }
267         inline iterator operator-(int j) const { return iterator(collection, i - j); }
268         inline int operator-(iterator j) const { return i - j.i; }
269     private:
270         int i;
271         const QWebElementCollection* const collection;
272     };
273     friend class iterator;
274 
begin()275     inline iterator begin() { return iterator(this, 0); }
end()276     inline iterator end()  { return iterator(this, count()); }
277 private:
278     QExplicitlySharedDataPointer<QWebElementCollectionPrivate> d;
279 };
280 
281 Q_DECLARE_METATYPE(QWebElement)
282 
283 #endif // QWEBELEMENT_H
284