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