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 friend class QWebPrinter; 174 175 #if defined(WTF_USE_V8) && WTF_USE_V8 176 friend class V8::Bindings::QtWebElementRuntime; 177 #else 178 friend class JSC::Bindings::QtWebElementRuntime; 179 #endif 180 181 QWebElementPrivate* d; 182 WebCore::Element* m_element; 183 }; 184 185 class QWebElementCollectionPrivate; 186 187 class QWEBKIT_EXPORT QWebElementCollection 188 { 189 public: 190 QWebElementCollection(); 191 QWebElementCollection(const QWebElement &contextElement, const QString &query); 192 QWebElementCollection(const QWebElementCollection &); 193 QWebElementCollection &operator=(const QWebElementCollection &); 194 ~QWebElementCollection(); 195 196 QWebElementCollection operator+(const QWebElementCollection &other) const; 197 inline QWebElementCollection &operator+=(const QWebElementCollection &other) 198 { 199 append(other); return *this; 200 } 201 202 void append(const QWebElementCollection &collection); 203 204 int count() const; 205 QWebElement at(int i) const; 206 inline QWebElement operator[](int i) const { return at(i); } 207 first()208 inline QWebElement first() const { return at(0); } last()209 inline QWebElement last() const { return at(count() - 1); } 210 211 QList<QWebElement> toList() const; 212 213 class const_iterator { 214 public: const_iterator(const QWebElementCollection * collection,int index)215 inline const_iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {} const_iterator(const const_iterator & o)216 inline const_iterator(const const_iterator& o) : i(o.i), collection(o.collection) {} 217 218 inline const QWebElement operator*() const { return collection->at(i); } 219 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 || collection != o.collection; } 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 inline bool operator>=(const const_iterator& o) const { return i >= o.i; } 226 227 inline const_iterator& operator++() { ++i; return *this; } 228 inline const_iterator operator++(int) { const_iterator n(collection, i); ++i; return n; } 229 inline const_iterator& operator--() { i--; return *this; } 230 inline const_iterator operator--(int) { const_iterator n(collection, i); i--; return n; } 231 inline const_iterator& operator+=(int j) { i += j; return *this; } 232 inline const_iterator& operator-=(int j) { i -= j; return *this; } 233 inline const_iterator operator+(int j) const { return const_iterator(collection, i + j); } 234 inline const_iterator operator-(int j) const { return const_iterator(collection, i - j); } 235 inline int operator-(const_iterator j) const { return i - j.i; } 236 private: 237 int i; 238 const QWebElementCollection* const collection; 239 }; 240 friend class const_iterator; 241 begin()242 inline const_iterator begin() const { return constBegin(); } end()243 inline const_iterator end() const { return constEnd(); } constBegin()244 inline const_iterator constBegin() const { return const_iterator(this, 0); } constEnd()245 inline const_iterator constEnd() const { return const_iterator(this, count()); }; 246 247 class iterator { 248 public: iterator(const QWebElementCollection * collection,int index)249 inline iterator(const QWebElementCollection* collection, int index) : i(index), collection(collection) {} iterator(const iterator & o)250 inline iterator(const iterator& o) : i(o.i), collection(o.collection) {} 251 252 inline QWebElement operator*() const { return collection->at(i); } 253 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 || collection != o.collection; } 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 inline bool operator>=(const iterator& o) const { return i >= o.i; } 260 261 inline iterator& operator++() { ++i; return *this; } 262 inline iterator operator++(int) { iterator n(collection, i); ++i; return n; } 263 inline iterator& operator--() { i--; return *this; } 264 inline iterator operator--(int) { iterator n(collection, i); i--; return n; } 265 inline iterator& operator+=(int j) { i += j; return *this; } 266 inline iterator& operator-=(int j) { i -= j; return *this; } 267 inline iterator operator+(int j) const { return iterator(collection, i + j); } 268 inline iterator operator-(int j) const { return iterator(collection, i - j); } 269 inline int operator-(iterator j) const { return i - j.i; } 270 private: 271 int i; 272 const QWebElementCollection* const collection; 273 }; 274 friend class iterator; 275 begin()276 inline iterator begin() { return iterator(this, 0); } end()277 inline iterator end() { return iterator(this, count()); } 278 private: 279 QExplicitlySharedDataPointer<QWebElementCollectionPrivate> d; 280 }; 281 282 Q_DECLARE_METATYPE(QWebElement) 283 284 #endif // QWEBELEMENT_H 285