qwebelement.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef QWEBELEMENT_H
00021 #define QWEBELEMENT_H
00022 
00023 #include <QtCore/qstring.h>
00024 #include <QtCore/qstringlist.h>
00025 #include <QtCore/qrect.h>
00026 #include <QtCore/qvariant.h>
00027 #include <QtCore/qshareddata.h>
00028 
00029 #include "qwebkitglobal.h"
00030 namespace WebCore {
00031     class Element;
00032     class Node;
00033 }
00034 
00035 namespace JSC {
00036 namespace Bindings {
00037     class QtWebElementRuntime;
00038 }
00039 }
00040 
00041 QT_BEGIN_NAMESPACE
00042 class QPainter;
00043 QT_END_NAMESPACE
00044 
00045 class QWebFrame;
00046 class QWebElementCollection;
00047 class QWebElementPrivate;
00048 
00049 class QWEBKIT_EXPORT QWebElement {
00050 public:
00051     QWebElement();
00052     QWebElement(const QWebElement&);
00053     QWebElement &operator=(const QWebElement&);
00054     ~QWebElement();
00055 
00056     bool operator==(const QWebElement& o) const;
00057     bool operator!=(const QWebElement& o) const;
00058 
00059     bool isNull() const;
00060 
00061     QWebElementCollection findAll(const QString &selectorQuery) const;
00062     QWebElement findFirst(const QString &selectorQuery) const;
00063 
00064     void setPlainText(const QString& text);
00065     QString toPlainText() const;
00066 
00067     void setOuterXml(const QString& markup);
00068     QString toOuterXml() const;
00069 
00070     void setInnerXml(const QString& markup);
00071     QString toInnerXml() const;
00072 
00073     void setAttribute(const QString& name, const QString& value);
00074     void setAttributeNS(const QString& namespaceUri, const QString& name, const QString& value);
00075     QString attribute(const QString& name, const QString& defaultValue = QString()) const;
00076     QString attributeNS(const QString& namespaceUri, const QString& name, const QString& defaultValue = QString()) const;
00077     bool hasAttribute(const QString& name) const;
00078     bool hasAttributeNS(const QString& namespaceUri, const QString& name) const;
00079     void removeAttribute(const QString& name);
00080     void removeAttributeNS(const QString& namespaceUri, const QString& name);
00081     bool hasAttributes() const;
00082     QStringList attributeNames(const QString& namespaceUri = QString()) const;
00083 
00084     QStringList classes() const;
00085     bool hasClass(const QString& name) const;
00086     void addClass(const QString& name);
00087     void removeClass(const QString& name);
00088     void toggleClass(const QString& name);
00089 
00090     bool hasFocus() const;
00091     void setFocus();
00092 
00093     QRect geometry() const;
00094 
00095     QString tagName() const;
00096     QString prefix() const;
00097     QString localName() const;
00098     QString namespaceUri() const;
00099 
00100     QWebElement parent() const;
00101     QWebElement firstChild() const;
00102     QWebElement lastChild() const;
00103     QWebElement nextSibling() const;
00104     QWebElement previousSibling() const;
00105     QWebElement document() const;
00106     QWebFrame *webFrame() const;
00107 
00108     // TODO: Add QWebElementCollection overloads
00109     // docs need example snippet
00110     void appendInside(const QString& markup);
00111     void appendInside(const QWebElement& element);
00112 
00113     // docs need example snippet
00114     void prependInside(const QString& markup);
00115     void prependInside(const QWebElement& element);
00116 
00117     // docs need example snippet
00118     void appendOutside(const QString& markup);
00119     void appendOutside(const QWebElement& element);
00120 
00121     // docs need example snippet
00122     void prependOutside(const QString& markup);
00123     void prependOutside(const QWebElement& element);
00124 
00125     // docs need example snippet
00126     void encloseContentsWith(const QWebElement& element);
00127     void encloseContentsWith(const QString& markup);
00128     void encloseWith(const QString& markup);
00129     void encloseWith(const QWebElement& element);
00130 
00131     void replace(const QString& markup);
00132     void replace(const QWebElement& element);
00133 
00134     QWebElement clone() const;
00135     QWebElement& takeFromDocument();
00136     void removeFromDocument();
00137     void removeAllChildren();
00138 
00139     QVariant evaluateJavaScript(const QString& scriptSource);
00140 
00141     enum StyleResolveStrategy {
00142          InlineStyle,
00143          CascadedStyle,
00144          ComputedStyle,
00145     };
00146     QString styleProperty(const QString& name, StyleResolveStrategy strategy) const;
00147     void setStyleProperty(const QString& name, const QString& value);
00148 
00149     void render(QPainter* painter);
00150 
00151 private:
00152     explicit QWebElement(WebCore::Element*);
00153     explicit QWebElement(WebCore::Node*);
00154 
00155     static QWebElement enclosingElement(WebCore::Node*);
00156 
00157     friend class QWebFrame;
00158     friend class QWebElementCollection;
00159     friend class QWebHitTestResult;
00160     friend class QWebHitTestResultPrivate;
00161     friend class QWebPage;
00162     friend class JSC::Bindings::QtWebElementRuntime;
00163 
00164     QWebElementPrivate* d;
00165     WebCore::Element* m_element;
00166 };
00167 
00168 class QWebElementCollectionPrivate;
00169 
00170 class QWEBKIT_EXPORT QWebElementCollection
00171 {
00172 public:
00173     QWebElementCollection();
00174     QWebElementCollection(const QWebElement &contextElement, const QString &query);
00175     QWebElementCollection(const QWebElementCollection &);
00176     QWebElementCollection &operator=(const QWebElementCollection &);
00177     ~QWebElementCollection();
00178 
00179     QWebElementCollection operator+(const QWebElementCollection &other) const;
00180     inline QWebElementCollection &operator+=(const QWebElementCollection &other)
00181     {
00182         append(other); return *this;
00183     }
00184 
00185     void append(const QWebElementCollection &collection);
00186 
00187     int count() const;
00188     QWebElement at(int i) const;
00189     inline QWebElement operator[](int i) const { return at(i); }
00190 
00191     inline QWebElement first() const { return at(0); }
00192     inline QWebElement last() const { return at(count() - 1); }
00193 
00194     QList<QWebElement> toList() const;
00195 
00196     class const_iterator {
00197        public:
00198            inline const_iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {}
00199            inline const_iterator(const const_iterator& o) : i(o.i), collection(o.collection) {}
00200 
00201            inline const QWebElement operator*() const { return collection->at(i); }
00202 
00203            inline bool operator==(const const_iterator& o) const { return i == o.i && collection == o.collection; }
00204            inline bool operator!=(const const_iterator& o) const { return i != o.i || collection != o.collection; }
00205            inline bool operator<(const const_iterator& o) const { return i < o.i; }
00206            inline bool operator<=(const const_iterator& o) const { return i <= o.i; }
00207            inline bool operator>(const const_iterator& o) const { return i > o.i; }
00208            inline bool operator>=(const const_iterator& o) const { return i >= o.i; }
00209 
00210            inline const_iterator& operator++() { ++i; return *this; }
00211            inline const_iterator operator++(int) { const_iterator n(collection, i); ++i; return n; }
00212            inline const_iterator& operator--() { i--; return *this; }
00213            inline const_iterator operator--(int) { const_iterator n(collection, i); i--; return n; }
00214            inline const_iterator& operator+=(int j) { i += j; return *this; }
00215            inline const_iterator& operator-=(int j) { i -= j; return *this; }
00216            inline const_iterator operator+(int j) const { return const_iterator(collection, i + j); }
00217            inline const_iterator operator-(int j) const { return const_iterator(collection, i - j); }
00218            inline int operator-(const_iterator j) const { return i - j.i; }
00219        private:
00220             int i;
00221             const QWebElementCollection* const collection;
00222     };
00223     friend class const_iterator;
00224 
00225     inline const_iterator begin() const { return constBegin(); }
00226     inline const_iterator end() const { return constEnd(); }
00227     inline const_iterator constBegin() const { return const_iterator(this, 0); }
00228     inline const_iterator constEnd() const { return const_iterator(this, count()); };
00229 
00230     class iterator {
00231     public:
00232         inline iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {}
00233         inline iterator(const iterator& o) : i(o.i), collection(o.collection) {}
00234 
00235         inline QWebElement operator*() const { return collection->at(i); }
00236 
00237         inline bool operator==(const iterator& o) const { return i == o.i && collection == o.collection; }
00238         inline bool operator!=(const iterator& o) const { return i != o.i || collection != o.collection; }
00239         inline bool operator<(const iterator& o) const { return i < o.i; }
00240         inline bool operator<=(const iterator& o) const { return i <= o.i; }
00241         inline bool operator>(const iterator& o) const { return i > o.i; }
00242         inline bool operator>=(const iterator& o) const { return i >= o.i; }
00243 
00244         inline iterator& operator++() { ++i; return *this; }
00245         inline iterator operator++(int) { iterator n(collection, i); ++i; return n; }
00246         inline iterator& operator--() { i--; return *this; }
00247         inline iterator operator--(int) { iterator n(collection, i); i--; return n; }
00248         inline iterator& operator+=(int j) { i += j; return *this; }
00249         inline iterator& operator-=(int j) { i -= j; return *this; }
00250         inline iterator operator+(int j) const { return iterator(collection, i + j); }
00251         inline iterator operator-(int j) const { return iterator(collection, i - j); }
00252         inline int operator-(iterator j) const { return i - j.i; }
00253     private:
00254         int i;
00255         const QWebElementCollection* const collection;
00256     };
00257     friend class iterator;
00258 
00259     inline iterator begin() { return iterator(this, 0); }
00260     inline iterator end()  { return iterator(this, count()); }
00261 private:
00262     QExplicitlySharedDataPointer<QWebElementCollectionPrivate> d;
00263 };
00264 
00265 Q_DECLARE_METATYPE(QWebElement)
00266 
00267 #endif // QWEBELEMENT_H