summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/webkit/WebKit/qt/Api/qwebelement.h
blob: 3833070b5f50c25444727c02029c664a418d97f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef QWEBELEMENT_H
#define QWEBELEMENT_H

#include <QString>
#include <QStringList>
#include <QRect>
#include <QVariant>
#include <QExplicitlySharedDataPointer>

#include "qwebkitglobal.h"
namespace WebCore {
    class Element;
    class Node;
}

QT_BEGIN_NAMESPACE
class QPainter;
QT_END_NAMESPACE

class QWebFrame;
class QWebElementCollection;
class QWebElementPrivate;

class QWEBKIT_EXPORT QWebElement {
public:
    QWebElement();
    QWebElement(const QWebElement&);
    QWebElement &operator=(const QWebElement&);
    ~QWebElement();

    bool operator==(const QWebElement& o) const;
    bool operator!=(const QWebElement& o) const;

    bool isNull() const;

    QWebElementCollection findAll(const QString &selectorQuery) const;
    QWebElement findFirst(const QString &selectorQuery) const;

    void setPlainText(const QString& text);
    QString toPlainText() const;

    void setOuterXml(const QString& markup);
    QString toOuterXml() const;

    void setInnerXml(const QString& markup);
    QString toInnerXml() const;

    void setAttribute(const QString& name, const QString& value);
    void setAttributeNS(const QString& namespaceUri, const QString& name, const QString& value);
    QString attribute(const QString& name, const QString& defaultValue = QString()) const;
    QString attributeNS(const QString& namespaceUri, const QString& name, const QString& defaultValue = QString()) const;
    bool hasAttribute(const QString& name) const;
    bool hasAttributeNS(const QString& namespaceUri, const QString& name) const;
    void removeAttribute(const QString& name);
    void removeAttributeNS(const QString& namespaceUri, const QString& name);
    bool hasAttributes() const;
    QStringList attributeNames(const QString& namespaceUri = QString()) const;

    QStringList classes() const;
    bool hasClass(const QString& name) const;
    void addClass(const QString& name);
    void removeClass(const QString& name);
    void toggleClass(const QString& name);

    bool hasFocus() const;
    void setFocus();

    QRect geometry() const;

    QString tagName() const;
    QString prefix() const;
    QString localName() const;
    QString namespaceUri() const;

    QWebElement parent() const;
    QWebElement firstChild() const;
    QWebElement lastChild() const;
    QWebElement nextSibling() const;
    QWebElement previousSibling() const;
    QWebElement document() const;
    QWebFrame *webFrame() const;

    // TODO: Add QWebElementCollection overloads
    // docs need example snippet
    void appendInside(const QString& markup);
    void appendInside(const QWebElement& element);

    // docs need example snippet
    void prependInside(const QString& markup);
    void prependInside(const QWebElement& element);

    // docs need example snippet
    void appendOutside(const QString& markup);
    void appendOutside(const QWebElement& element);

    // docs need example snippet
    void prependOutside(const QString& markup);
    void prependOutside(const QWebElement& element);

    // docs need example snippet
    void encloseContentsWith(const QWebElement& element);
    void encloseContentsWith(const QString& markup);
    void encloseWith(const QString& markup);
    void encloseWith(const QWebElement& element);

    void replace(const QString& markup);
    void replace(const QWebElement& element);

    QWebElement clone() const;
    QWebElement& takeFromDocument();
    void removeFromDocument();
    void removeAllChildren();

    QVariant evaluateJavaScript(const QString& scriptSource);

    enum StyleResolveStrategy {
         InlineStyle,
         CascadedStyle,
         ComputedStyle,
    };
    QString styleProperty(const QString& name, StyleResolveStrategy strategy) const;
    void setStyleProperty(const QString& name, const QString& value);

    void render(QPainter* painter);

private:
    explicit QWebElement(WebCore::Element*);
    explicit QWebElement(WebCore::Node*);

    static QWebElement enclosingElement(WebCore::Node*);

    friend class QWebFrame;
    friend class QWebElementCollection;
    friend class QWebHitTestResult;
    friend class QWebHitTestResultPrivate;
    friend class QWebPage;

    QWebElementPrivate* d;
    WebCore::Element* m_element;
};

class QWebElementCollectionPrivate;

class QWEBKIT_EXPORT QWebElementCollection
{
public:
    QWebElementCollection();
    QWebElementCollection(const QWebElement &contextElement, const QString &query);
    QWebElementCollection(const QWebElementCollection &);
    QWebElementCollection &operator=(const QWebElementCollection &);
    ~QWebElementCollection();

    QWebElementCollection operator+(const QWebElementCollection &other) const;
    inline QWebElementCollection &operator+=(const QWebElementCollection &other)
    {
        append(other); return *this;
    }

    void append(const QWebElementCollection &collection);

    int count() const;
    QWebElement at(int i) const;
    inline QWebElement operator[](int i) const { return at(i); }

    inline QWebElement first() const { return at(0); }
    inline QWebElement last() const { return at(count() - 1); }

    QList<QWebElement> toList() const;

    class const_iterator {
       public:
           inline const_iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {}
           inline const_iterator(const const_iterator& o) : i(o.i), collection(o.collection) {}

           inline const QWebElement operator*() const { return collection->at(i); }

           inline bool operator==(const const_iterator& o) const { return i == o.i && collection == o.collection; }
           inline bool operator!=(const const_iterator& o) const { return i != o.i || collection != o.collection; }
           inline bool operator<(const const_iterator& o) const { return i < o.i; }
           inline bool operator<=(const const_iterator& o) const { return i <= o.i; }
           inline bool operator>(const const_iterator& o) const { return i > o.i; }
           inline bool operator>=(const const_iterator& o) const { return i >= o.i; }

           inline const_iterator& operator++() { ++i; return *this; }
           inline const_iterator operator++(int) { const_iterator n(collection, i); ++i; return n; }
           inline const_iterator& operator--() { i--; return *this; }
           inline const_iterator operator--(int) { const_iterator n(collection, i); i--; return n; }
           inline const_iterator& operator+=(int j) { i += j; return *this; }
           inline const_iterator& operator-=(int j) { i -= j; return *this; }
           inline const_iterator operator+(int j) const { return const_iterator(collection, i + j); }
           inline const_iterator operator-(int j) const { return const_iterator(collection, i - j); }
           inline int operator-(const_iterator j) const { return i - j.i; }
       private:
            int i;
            const QWebElementCollection* const collection;
    };
    friend class const_iterator;

    inline const_iterator begin() const { return constBegin(); }
    inline const_iterator end() const { return constEnd(); }
    inline const_iterator constBegin() const { return const_iterator(this, 0); }
    inline const_iterator constEnd() const { return const_iterator(this, count()); };

    class iterator {
    public:
        inline iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {}
        inline iterator(const iterator& o) : i(o.i), collection(o.collection) {}

        inline QWebElement operator*() const { return collection->at(i); }

        inline bool operator==(const iterator& o) const { return i == o.i && collection == o.collection; }
        inline bool operator!=(const iterator& o) const { return i != o.i || collection != o.collection; }
        inline bool operator<(const iterator& o) const { return i < o.i; }
        inline bool operator<=(const iterator& o) const { return i <= o.i; }
        inline bool operator>(const iterator& o) const { return i > o.i; }
        inline bool operator>=(const iterator& o) const { return i >= o.i; }

        inline iterator& operator++() { ++i; return *this; }
        inline iterator operator++(int) { iterator n(collection, i); ++i; return n; }
        inline iterator& operator--() { i--; return *this; }
        inline iterator operator--(int) { iterator n(collection, i); i--; return n; }
        inline iterator& operator+=(int j) { i += j; return *this; }
        inline iterator& operator-=(int j) { i -= j; return *this; }
        inline iterator operator+(int j) const { return iterator(collection, i + j); }
        inline iterator operator-(int j) const { return iterator(collection, i - j); }
        inline int operator-(iterator j) const { return i - j.i; }
    private:
        int i;
        const QWebElementCollection* const collection;
    };
    friend class iterator;

    inline iterator begin() { return iterator(this, 0); }
    inline iterator end()  { return iterator(this, count()); }
private:
    QExplicitlySharedDataPointer<QWebElementCollectionPrivate> d;
};

#endif // QWEBELEMENT_H