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
31namespace WebCore {
32class ChromeClientQt;
33class Element;
34class Node;
35}
36
37QT_BEGIN_NAMESPACE
38class QPainter;
39QT_END_NAMESPACE
40
41class QWebFrame;
42class QWebFullScreenRequest;
43class QWebElementCollection;
44class QWebElementPrivate;
45
46class QWEBKIT_EXPORT QWebElement {
47public:
48 QWebElement();
49 QWebElement(const QWebElement&);
50 QWebElement &operator=(const QWebElement&);
51 ~QWebElement();
52
53 bool operator==(const QWebElement& o) const;
54 bool operator!=(const QWebElement& o) const;
55
56 bool isNull() const;
57
58 QWebElementCollection findAll(const QString &selectorQuery) const;
59 QWebElement findFirst(const QString &selectorQuery) const;
60
61 void setPlainText(const QString& text);
62 QString toPlainText() const;
63
64 void setOuterXml(const QString& markup);
65 QString toOuterXml() const;
66
67 void setInnerXml(const QString& markup);
68 QString toInnerXml() const;
69
70 void setAttribute(const QString& name, const QString& value);
71 void setAttributeNS(const QString& namespaceUri, const QString& name, const QString& value);
72 QString attribute(const QString& name, const QString& defaultValue = QString()) const;
73 QString attributeNS(const QString& namespaceUri, const QString& name, const QString& defaultValue = QString()) const;
74 bool hasAttribute(const QString& name) const;
75 bool hasAttributeNS(const QString& namespaceUri, const QString& name) const;
76 void removeAttribute(const QString& name);
77 void removeAttributeNS(const QString& namespaceUri, const QString& name);
78 bool hasAttributes() const;
79 QStringList attributeNames(const QString& namespaceUri = QString()) const;
80
81 QStringList classes() const;
82 bool hasClass(const QString& name) const;
83 void addClass(const QString& name);
84 void removeClass(const QString& name);
85 void toggleClass(const QString& name);
86
87 bool hasFocus() const;
88 void setFocus();
89
90 QRect geometry() const;
91
92 QString tagName() const;
93 QString prefix() const;
94 QString localName() const;
95 QString namespaceUri() const;
96
97 QWebElement parent() const;
98 QWebElement firstChild() const;
99 QWebElement lastChild() const;
100 QWebElement nextSibling() const;
101 QWebElement previousSibling() const;
102 QWebElement document() const;
103 QWebFrame *webFrame() const;
104
105 // TODO: Add QWebElementCollection overloads
106 // docs need example snippet
107 void appendInside(const QString& markup);
108 void appendInside(const QWebElement& element);
109
110 // docs need example snippet
111 void prependInside(const QString& markup);
112 void prependInside(const QWebElement& element);
113
114 // docs need example snippet
115 void appendOutside(const QString& markup);
116 void appendOutside(const QWebElement& element);
117
118 // docs need example snippet
119 void prependOutside(const QString& markup);
120 void prependOutside(const QWebElement& element);
121
122 // docs need example snippet
123 void encloseContentsWith(const QWebElement& element);
124 void encloseContentsWith(const QString& markup);
125 void encloseWith(const QString& markup);
126 void encloseWith(const QWebElement& element);
127
128 void replace(const QString& markup);
129 void replace(const QWebElement& element);
130
131 QWebElement clone() const;
132 QWebElement& takeFromDocument();
133 void removeFromDocument();
134 void removeAllChildren();
135
136 QVariant evaluateJavaScript(const QString& scriptSource);
137
138 enum StyleResolveStrategy {
139 InlineStyle,
140 CascadedStyle,
141 ComputedStyle
142 };
143 QString styleProperty(const QString& name, StyleResolveStrategy strategy) const;
144 void setStyleProperty(const QString& name, const QString& value);
145
146 void render(QPainter* painter);
147 void render(QPainter* painter, const QRect& clipRect);
148
149private:
150 explicit QWebElement(WebCore::Element*);
151
152 void beginEnterFullScreen();
153 void endEnterFullScreen();
154 void beginExitFullScreen();
155 void endExitFullScreen();
156
157 friend class WebCore::ChromeClientQt;
158 friend class DumpRenderTreeSupportQt;
159 friend class QWebElementCollection;
160 friend class QWebFrameAdapter;
161 friend class QWebFullScreenRequest;
162 friend class QWebHitTestResult;
163 friend class QWebHitTestResultPrivate;
164 friend class QWebPage;
165 friend class QWebPageAdapter;
166 friend class QWebPagePrivate;
167 friend class QtWebElementRuntime;
168
169 QWebElementPrivate* d;
170 WebCore::Element* m_element;
171};
172
173class QWebElementCollectionPrivate;
174
175class QWEBKIT_EXPORT QWebElementCollection
176{
177public:
178 QWebElementCollection();
179 QWebElementCollection(const QWebElement &contextElement, const QString &query);
180 QWebElementCollection(const QWebElementCollection &);
181 QWebElementCollection &operator=(const QWebElementCollection &);
182 ~QWebElementCollection();
183
184 QWebElementCollection operator+(const QWebElementCollection &other) const;
185 inline QWebElementCollection &operator+=(const QWebElementCollection &other)
186 {
187 append(other); return *this;
188 }
189
190 void append(const QWebElementCollection &collection);
191
192 int count() const;
193 QWebElement at(int i) const;
194 inline QWebElement operator[](int i) const { return at(i); }
195
196 inline QWebElement first() const { return at(0); }
197 inline QWebElement last() const { return at(count() - 1); }
198
199 QList<QWebElement> toList() const;
200
201 class const_iterator {
202 public:
203 inline const_iterator(const QWebElementCollection* collection_, int index) : i(index), collection(collection_) {}
204 inline const_iterator(const const_iterator& o) : i(o.i), collection(o.collection) {}
205
206 inline const QWebElement operator*() const { return collection->at(i); }
207
208 inline bool operator==(const const_iterator& o) const { return i == o.i && collection == o.collection; }
209 inline bool operator!=(const const_iterator& o) const { return i != o.i || collection != o.collection; }
210 inline bool operator<(const const_iterator& o) const { return i < o.i; }
211 inline bool operator<=(const const_iterator& o) const { return i <= o.i; }
212 inline bool operator>(const const_iterator& o) const { return i > o.i; }
213 inline bool operator>=(const const_iterator& o) const { return i >= o.i; }
214
215 inline const_iterator& operator++() { ++i; return *this; }
216 inline const_iterator operator++(int) { const_iterator n(collection, i); ++i; return n; }
217 inline const_iterator& operator--() { i--; return *this; }
218 inline const_iterator operator--(int) { const_iterator n(collection, i); i--; return n; }
219 inline const_iterator& operator+=(int j) { i += j; return *this; }
220 inline const_iterator& operator-=(int j) { i -= j; return *this; }
221 inline const_iterator operator+(int j) const { return const_iterator(collection, i + j); }
222 inline const_iterator operator-(int j) const { return const_iterator(collection, i - j); }
223 inline int operator-(const_iterator j) const { return i - j.i; }
224 private:
225 int i;
226 const QWebElementCollection* const collection;
227 };
228 friend class const_iterator;
229
230 inline const_iterator begin() const { return constBegin(); }
231 inline const_iterator end() const { return constEnd(); }
232 inline const_iterator constBegin() const { return const_iterator(this, 0); }
233 inline const_iterator constEnd() const { return const_iterator(this, count()); };
234
235 class iterator {
236 public:
237 inline iterator(const QWebElementCollection* collection_, int index) : i(index), collection(collection_) {}
238 inline iterator(const iterator& o) : i(o.i), collection(o.collection) {}
239
240 inline QWebElement operator*() const { return collection->at(i); }
241
242 inline bool operator==(const iterator& o) const { return i == o.i && collection == o.collection; }
243 inline bool operator!=(const iterator& o) const { return i != o.i || collection != o.collection; }
244 inline bool operator<(const iterator& o) const { return i < o.i; }
245 inline bool operator<=(const iterator& o) const { return i <= o.i; }
246 inline bool operator>(const iterator& o) const { return i > o.i; }
247 inline bool operator>=(const iterator& o) const { return i >= o.i; }
248
249 inline iterator& operator++() { ++i; return *this; }
250 inline iterator operator++(int) { iterator n(collection, i); ++i; return n; }
251 inline iterator& operator--() { i--; return *this; }
252 inline iterator operator--(int) { iterator n(collection, i); i--; return n; }
253 inline iterator& operator+=(int j) { i += j; return *this; }
254 inline iterator& operator-=(int j) { i -= j; return *this; }
255 inline iterator operator+(int j) const { return iterator(collection, i + j); }
256 inline iterator operator-(int j) const { return iterator(collection, i - j); }
257 inline int operator-(iterator j) const { return i - j.i; }
258 private:
259 int i;
260 const QWebElementCollection* const collection;
261 };
262 friend class iterator;
263
264 inline iterator begin() { return iterator(this, 0); }
265 inline iterator end() { return iterator(this, count()); }
266private:
267 QExplicitlySharedDataPointer<QWebElementCollectionPrivate> d;
268};
269
270Q_DECLARE_METATYPE(QWebElement)
271
272#endif // QWEBELEMENT_H
273