Hello everybody

I took a look at how some of the other platforms solved similar problems with 
form-elements, and chromium turned out to have two specialized WebElement 
classes called WebFormElement and WebFormControlElement. This seemed like a 
viable solution so I have implemented a similar API for QWebElement, and 
attached the result below.

The API is a bit special though, because it avoids virtual methods and adding 
data to QWebElement. So the way to use the extra method is create a new 
QFormElement based on the QWebElement.

if (element.isFormElement()) {
  QWebFormElement formElemnt(element);

}

This makes it possible to make this extension without changing neither API nor 
ABI of the existing classes.

Any comments?

Best regards
`Allan
diff --git a/Source/WebKit/WebKit1.pro b/Source/WebKit/WebKit1.pro
index 75d4d79..61a6861 100644
--- a/Source/WebKit/WebKit1.pro
+++ b/Source/WebKit/WebKit1.pro
@@ -20,6 +20,8 @@ SOURCES += \
     $$PWD/qt/Api/qwebpage.cpp \
     $$PWD/qt/Api/qwebview.cpp \
     $$PWD/qt/Api/qwebelement.cpp \
+    $$PWD/qt/Api/qwebformelement.cpp \
+    $$PWD/qt/Api/qwebformcontrolelement.cpp \
     $$PWD/qt/Api/qwebhistory.cpp \
     $$PWD/qt/Api/qwebsettings.cpp \
     $$PWD/qt/Api/qwebhistoryinterface.cpp \
@@ -69,6 +71,8 @@ HEADERS += \
     $$PWD/qt/Api/qwebsecurityorigin.h \
     $$PWD/qt/Api/qwebelement.h \
     $$PWD/qt/Api/qwebelement_p.h \
+    $$PWD/qt/Api/qwebformelement.h \
+    $$PWD/qt/Api/qwebformcontrolelement.h \
     $$PWD/qt/Api/qwebpluginfactory.h \
     $$PWD/qt/Api/qwebhistory.h \
     $$PWD/qt/Api/qwebinspector.h \
diff --git a/Source/WebKit/qt/Api/qwebelement.cpp b/Source/WebKit/qt/Api/qwebelement.cpp
index 298f6cd..915ae3f 100644
--- a/Source/WebKit/qt/Api/qwebelement.cpp
+++ b/Source/WebKit/qt/Api/qwebelement.cpp
@@ -32,6 +32,7 @@
 #include "FrameView.h"
 #include "GraphicsContext.h"
 #include "HTMLElement.h"
+#include "HTMLNames.h"
 #include "StylePropertySet.h"
 #include "StyleRule.h"
 #include "Completion.h"
@@ -220,6 +221,22 @@ bool QWebElement::isNull() const
 }
 
 /*!
+    Returns true if the element is a form element; otherwise returns false.
+*/
+bool QWebElement::isFormElement() const
+{
+    return m_element && m_element->hasTagName(HTMLNames::formTag);
+}
+
+/*!
+    Returns true if the element is a form control element; otherwise returns false.
+*/
+bool QWebElement::isFormControlElement() const
+{
+    return m_element && m_element->isFormControlElement();
+}
+
+/*!
     Returns a new list of child elements matching the given CSS selector
     \a selectorQuery. If there are no matching elements, an empty list is
     returned.
diff --git a/Source/WebKit/qt/Api/qwebelement.h b/Source/WebKit/qt/Api/qwebelement.h
index efec5eb..06ce1cb 100644
--- a/Source/WebKit/qt/Api/qwebelement.h
+++ b/Source/WebKit/qt/Api/qwebelement.h
@@ -51,6 +51,8 @@ public:
     bool operator!=(const QWebElement& o) const;
 
     bool isNull() const;
+    bool isFormElement() const;
+    bool isFormControlElement() const;
 
     QWebElementCollection findAll(const QString &selectorQuery) const;
     QWebElement findFirst(const QString &selectorQuery) const;
@@ -152,6 +154,8 @@ private:
     friend class DumpRenderTreeSupportQt;
     friend class QWebFrame;
     friend class QWebElementCollection;
+    friend class QWebFormElement;
+    friend class QWebFormControlElement;
     friend class QWebHitTestResult;
     friend class QWebHitTestResultPrivate;
     friend class QWebPage;
diff --git a/Source/WebKit/qt/Api/qwebformcontrolelement.cpp b/Source/WebKit/qt/Api/qwebformcontrolelement.cpp
new file mode 100644
index 0000000..de07508
--- /dev/null
+++ b/Source/WebKit/qt/Api/qwebformcontrolelement.cpp
@@ -0,0 +1,163 @@
+/*
+    Copyright (C) 2012 Digia Plc. 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.
+*/
+
+#include "config.h"
+#include "qwebformcontrolelement.h"
+
+#include "HTMLFormControlElement.h"
+#include "HTMLFormElement.h"
+#include "HTMLInputElement.h"
+#include "HTMLNames.h"
+#include <wtf/Vector.h>
+
+#include "qwebformelement.h"
+
+using namespace WebCore;
+
+/*!
+    Constructs a null web form control element.
+*/
+QWebFormControlElement::QWebFormControlElement()
+    : QWebElement()
+{
+}
+
+/*!
+    Constructs a web form control element from a web element. If \a element is not a form control element it constructs a null web form control element.
+*/
+QWebFormControlElement::QWebFormControlElement(QWebElement& element)
+    : QWebElement(element.isFormControlElement() ? element.m_element : 0)
+{
+}
+
+/*!
+    \internal
+*/
+QWebFormControlElement::QWebFormControlElement(HTMLFormControlElement* formControlElement)
+    : QWebElement(formControlElement)
+{
+}
+
+/*!
+    Constructs a copy of \a other.
+*/
+QWebFormControlElement::QWebFormControlElement(const QWebFormControlElement &other)
+    : QWebElement(other)
+{
+}
+
+/*!
+    Assigns \a other to this element and returns a reference to this element.
+*/
+QWebFormControlElement& QWebFormControlElement::operator=(const QWebFormControlElement &other)
+{
+    QWebElement::operator=(other);
+    return *this;
+}
+
+/*!
+    Returns true if the form control element should be auto completed.
+*/
+bool QWebFormControlElement::shouldAutoComplete() const
+{
+    if (!formControlElement())
+        return false;
+
+    if (!formControlElement()->hasTagName(HTMLNames::inputTag))
+        return form().shouldAutoComplete();
+
+    return static_cast<const HTMLInputElement*>(formControlElement())->shouldAutocomplete();
+}
+
+/*!
+    Returns true if the form control element is enabled.
+*/
+bool QWebFormControlElement::isEnabled() const
+{
+    if (!formControlElement())
+        return false;
+    return formControlElement()->isEnabledFormControl();
+}
+
+/*!
+    Returns true if the form control element is read-only.
+*/
+bool QWebFormControlElement::isReadOnly() const
+{
+    if (!formControlElement())
+        return true;
+    return formControlElement()->readOnly();
+}
+
+/*!
+    Returns true if the form control element is required.
+*/
+bool QWebFormControlElement::isRequired() const
+{
+    if (!formControlElement())
+        return false;
+    return formControlElement()->isRequiredFormControl();
+}
+
+/*!
+    Returns the type of form control.
+*/
+QString QWebFormControlElement::formControlType() const
+{
+    if (!formControlElement())
+        return QString();
+    return formControlElement()->type();
+}
+
+/*!
+    Returns the name that should be used when storing autofill data. This is either the field name or its id,
+    an empty string if it has no name and no id.
+*/
+QString QWebFormControlElement::nameForAutofill() const
+{
+    if (!formControlElement())
+        return QString();
+    QString name = formControlElement()->name();
+    QString trimmedName = name.trimmed();
+    if (!trimmedName.isEmpty())
+        return trimmedName;
+    name = formControlElement()->getIdAttribute();
+    trimmedName = name.trimmed();
+    if (!trimmedName.isEmpty())
+        return trimmedName;
+    return QString();
+}
+
+/*!
+    Returns the form element this form control is associated with.
+*/
+QWebFormElement QWebFormControlElement::form() const
+{
+    if (!formControlElement())
+        return QWebFormElement();
+    return QWebFormElement(formControlElement()->form());
+}
+
+HTMLFormControlElement* QWebFormControlElement::formControlElement() {
+    return static_cast<HTMLFormControlElement*>(m_element);
+}
+
+const HTMLFormControlElement* QWebFormControlElement::formControlElement() const {
+    return static_cast<const HTMLFormControlElement*>(m_element);
+}
diff --git a/Source/WebKit/qt/Api/qwebformcontrolelement.h b/Source/WebKit/qt/Api/qwebformcontrolelement.h
new file mode 100644
index 0000000..6d78477
--- /dev/null
+++ b/Source/WebKit/qt/Api/qwebformcontrolelement.h
@@ -0,0 +1,60 @@
+/*
+    Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies)
+              (C) 2010 Google Inc. All rights reserved.
+
+    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 QWEBFORMCONTROLELEMENT_H
+#define QWEBFORMCONTROLELEMENT_H
+
+#include "qwebelement.h"
+
+#include "qwebkitglobal.h"
+
+namespace WebCore {
+    class HTMLFormControlElement;
+}
+
+class QWebFormElement;
+
+class QWEBKIT_EXPORT QWebFormControlElement : public QWebElement {
+public:
+    QWebFormControlElement();
+    QWebFormControlElement(QWebElement&);
+    QWebFormControlElement(const QWebFormControlElement&);
+    QWebFormControlElement &operator=(const QWebFormControlElement&);
+
+    QWEBKIT_EXPORT bool shouldAutoComplete() const;
+    QWEBKIT_EXPORT bool isEnabled() const;
+    QWEBKIT_EXPORT bool isReadOnly() const;
+    QWEBKIT_EXPORT bool isRequired() const;
+    QWEBKIT_EXPORT QString formControlType() const;
+
+    QWEBKIT_EXPORT QString nameForAutofill() const;
+
+    QWEBKIT_EXPORT QWebFormElement form() const;
+
+private:
+    explicit QWebFormControlElement(WebCore::HTMLFormControlElement*);
+
+    friend class QWebFormElement;
+
+    WebCore::HTMLFormControlElement* formControlElement();
+    const WebCore::HTMLFormControlElement* formControlElement() const;
+};
+
+#endif // QWEBFORMCONTROLELEMENT_H
diff --git a/Source/WebKit/qt/Api/qwebformelement.cpp b/Source/WebKit/qt/Api/qwebformelement.cpp
new file mode 100644
index 0000000..cf97f38
--- /dev/null
+++ b/Source/WebKit/qt/Api/qwebformelement.cpp
@@ -0,0 +1,134 @@
+/*
+    Copyright (C) 2012 Digia Plc. 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.
+*/
+
+#include "config.h"
+#include "qwebformelement.h"
+
+#include "HTMLFormControlElement.h"
+#include "HTMLFormElement.h"
+#include <wtf/Vector.h>
+
+#include "qwebformcontrolelement.h"
+
+using namespace WebCore;
+
+/*!
+    Constructs a null web form element.
+*/
+QWebFormElement::QWebFormElement()
+    : QWebElement()
+{
+}
+
+/*!
+    Constructs a web form element from a web element. If \a element is not a form element it constructs a null web form element.
+*/
+QWebFormElement::QWebFormElement(QWebElement& element)
+    : QWebElement(element.isFormElement() ? element.m_element : 0)
+{
+}
+
+/*!
+    \internal
+*/
+QWebFormElement::QWebFormElement(HTMLFormElement* formElement)
+    : QWebElement(formElement)
+{
+}
+
+/*!
+    Constructs a copy of \a other.
+*/
+QWebFormElement::QWebFormElement(const QWebFormElement &other)
+    : QWebElement(other)
+{
+}
+
+/*!
+    Assigns \a other to this element and returns a reference to this element.
+*/
+QWebFormElement& QWebFormElement::operator=(const QWebFormElement &other)
+{
+    QWebElement::operator=(other);
+    return *this;
+}
+
+
+/*!
+    Returns true if the form element should be auto completed.
+*/
+bool QWebFormElement::shouldAutoComplete() const
+{
+    if (!formElement())
+        return false;
+    return formElement()->shouldAutocomplete();
+}
+
+/*!
+    Returns true if the form element was submitted due to a user actions, and returns false otherwise.
+*/
+bool QWebFormElement::wasUserSubmitted() const
+{
+    if (!formElement())
+        return false;
+    return formElement()->wasUserSubmitted();
+}
+
+/*!
+    Submits the form.
+*/
+void QWebFormElement::submit()
+{
+    if (!formElement())
+        return;
+    formElement()->submit();
+}
+
+/*!
+    Returns a collections of the form control elements associated with the form.
+*/
+QList<QWebFormControlElement> QWebFormElement::formControlElements() const
+{
+    if (!formElement())
+        return QList<QWebFormControlElement>();
+
+    QList<QWebFormControlElement> out;
+
+    const WTF::Vector<FormAssociatedElement*>& associatedElements = formElement()->associatedElements();
+
+    WTF::Vector<FormAssociatedElement*>::const_iterator it = associatedElements.begin();
+    const WTF::Vector<FormAssociatedElement*>::const_iterator itend = associatedElements.end();
+
+    for (; it != itend; ++it) {
+        if (!(*it)->isFormControlElement())
+            continue;
+        HTMLFormControlElement* formControlElement = static_cast<HTMLFormControlElement*>(*it);
+        out.append(QWebFormControlElement(formControlElement));
+    }
+
+    return out;
+}
+
+HTMLFormElement* QWebFormElement::formElement() {
+    return static_cast<HTMLFormElement*>(m_element);
+}
+
+const HTMLFormElement* QWebFormElement::formElement() const {
+    return static_cast<const HTMLFormElement*>(m_element);
+}
diff --git a/Source/WebKit/qt/Api/qwebformelement.h b/Source/WebKit/qt/Api/qwebformelement.h
new file mode 100644
index 0000000..1776ca0
--- /dev/null
+++ b/Source/WebKit/qt/Api/qwebformelement.h
@@ -0,0 +1,54 @@
+/*
+    Copyright (C) 2012 Digia Plc. 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 QWEBFORMELEMENT_H
+#define QWEBFORMELEMENT_H
+
+#include "qwebelement.h"
+
+#include "qwebkitglobal.h"
+
+namespace WebCore {
+    class HTMLFormElement;
+}
+
+class QWebFormControlElement;
+
+class QWEBKIT_EXPORT QWebFormElement : public QWebElement {
+public:
+    QWebFormElement();
+    QWebFormElement(QWebElement&);
+    QWebFormElement(const QWebFormElement&);
+    QWebFormElement& operator=(const QWebFormElement&);
+
+    QWEBKIT_EXPORT bool shouldAutoComplete() const;
+    QWEBKIT_EXPORT bool wasUserSubmitted() const;
+    QWEBKIT_EXPORT void submit();
+    QWEBKIT_EXPORT QList<QWebFormControlElement> formControlElements() const;
+
+private:
+    explicit QWebFormElement(WebCore::HTMLFormElement*);
+
+    friend class QWebFormControlElement;
+
+    WebCore::HTMLFormElement* formElement();
+    const WebCore::HTMLFormElement* formElement() const;
+};
+
+#endif // QWEBFORMELEMENT_H
_______________________________________________
webkit-qt mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-qt

Reply via email to