[PATCH] Web shortcuts creation improvement

View: New views
1 Messages — Rating Filter:   Alert me  

[PATCH] Web shortcuts creation improvement

by Bugzilla from fid@gpul.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
I've written a patch which I think it improves the creation of web shortcuts
in the context menu of text inputs.
This patch handles select and textarea elements, shows a warning if the form
uses POST method and does not allow to create the web shortcut if the input is
not inside a form.

Regards,
José

[render_form.diff]

Index: khtml/rendering/render_form.cpp
===================================================================
--- khtml/rendering/render_form.cpp (revision 948881)
+++ khtml/rendering/render_form.cpp (working copy)
@@ -655,6 +655,7 @@
       * @short Creates a Web Shourtcut without using kdebase SearchProvider class.
       *        It is used by LineEditWidget.
       */
+    static bool createWebShortcut(KUrl baseUrl, HTMLFormElementImpl *form, QString queryName);
     static bool createWebShortcut(QString query);
 
 private:
@@ -662,6 +663,69 @@
     static void createFile(QString query, QString name, QString keys);
 };
 
+bool WebShortcutCreator::createWebShortcut( KUrl baseUrl, HTMLFormElementImpl *form,
+    QString queryName )
+{
+    KUrl url( form->action().string() );
+    if ( !url.hasPath() ) {
+        url.setPath( baseUrl.path() );
+    }
+    if ( !url.hasHost() ) {
+        url.setProtocol( baseUrl.protocol() );
+        url.setHost( baseUrl.host() );
+    }
+    NodeImpl *node;
+    HTMLGenericFormElementImpl *formNode;
+    QString method = form->getAttribute("method").string();
+    if ( !( method.isEmpty() ) &&
+         ( QString::compare( method, "get", Qt::CaseInsensitive) ) ) {
+        //It's a POST form
+        KMessageBox::sorry( 0,
+          i18n("This form is not completely compatible with Web Shortcuts, "
+               "an equivalent Web Shortcut will be created, but it might not "
+               "work correctly"),
+          i18n("Incompatible form") );
+    }
+    HTMLInputElementImpl *inputNode;
+    HTMLSelectElementImpl *selectNode;
+    HTMLTextAreaElementImpl *textAreaNode;
+    for( unsigned long i = 0; ( node = form->elements()->item( i ) ); i++ ) {
+        formNode = dynamic_cast<HTMLGenericFormElementImpl*>( node );
+        if ( ( !formNode ) || !( formNode->name().string().size() ) ||
+            ( formNode->name().string() == queryName ) ) {
+            continue;
+        }
+        if ( inputNode = dynamic_cast<HTMLInputElementImpl*>( node ) ) {
+            switch ( inputNode->inputType() ) {
+                case HTMLInputElementImpl::CHECKBOX:
+                case HTMLInputElementImpl::RADIO:
+                    if ( !inputNode->checked() ) {
+                        break;
+                    }
+                case HTMLInputElementImpl::TEXT:
+                case HTMLInputElementImpl::PASSWORD:
+                case HTMLInputElementImpl::HIDDEN:
+                    url.addQueryItem( inputNode->name().string(), inputNode->value().string() );
+                default:
+                    break;
+            }
+            continue;
+        }
+        else if ( selectNode = dynamic_cast<HTMLSelectElementImpl*>( node ) ) {
+            url.addQueryItem( selectNode->name().string(), selectNode->value().string() );
+        }
+        else if ( textAreaNode = dynamic_cast<HTMLTextAreaElementImpl*>( node ) ) {
+            url.addQueryItem( textAreaNode->name().string(), textAreaNode->value().string() );
+        }
+    }
+    QString query( url.url() );
+    if ( !query.contains( "?" ) ) {
+        query += '?'; //This input is the only one of the form
+    }
+    query += '&' + queryName + "=\\{@}";
+    return createWebShortcut( query );
+}
+
 bool WebShortcutCreator::createWebShortcut(QString query)
 {
     QString name = i18n( "New Web Shortcut" );
@@ -757,46 +821,8 @@
 {
     QString queryName( m_input->name().string() );
     HTMLFormElementImpl *form = m_input->form();
-    KUrl url( form->action().string() );
     KUrl baseUrl( m_view->part()->baseURL().url() + '?' );
-    if ( !url.hasPath() ) {
-        url.setPath( baseUrl.path() );
-    }
-    if ( !url.hasHost() ) {
-        url.setProtocol( baseUrl.protocol() );
-        url.setHost( baseUrl.host() );
-    }
-    NodeImpl *node;
-    HTMLInputElementImpl *inputNode;
-    for( unsigned long i = 0; ( node = form->elements()->item( i ) ); i++ ) {
-        inputNode = dynamic_cast<HTMLInputElementImpl*>( node );
-        if ( inputNode ) {
-            if ( ( !inputNode->name().string().size() ) ||
-                 (inputNode->name().string() == queryName) ) {
-                continue;
-            } else {
-                switch ( inputNode->inputType() ) {
-                    case HTMLInputElementImpl::CHECKBOX:
-                    case HTMLInputElementImpl::RADIO:
-                        if ( !inputNode->checked() ) {
-                            break;
-                        }
-                    case HTMLInputElementImpl::TEXT:
-                    case HTMLInputElementImpl::PASSWORD:
-                    case HTMLInputElementImpl::HIDDEN:
-                        url.addQueryItem( inputNode->name().string(), inputNode->value().string() );
-                    default:
-                        break;
-                }
-            }
-        }
-    }
-    QString query( url.url() );
-    if ( !query.contains( "?" ) ) {
-        query += '?'; //This input is the only one of the form
-    }
-    query += '&' + queryName + "=\\{@}";
-    WebShortcutCreator::createWebShortcut( query );
+    WebShortcutCreator::createWebShortcut( baseUrl, form, queryName );
 }
 
 void LineEditWidget::contextMenuEvent(QContextMenuEvent *e)
@@ -821,7 +847,8 @@
         popup->addAction( m_spellAction );
         m_spellAction->setEnabled( !text().isEmpty() );
     }
-    if ( !m_view->part()->onlyLocalReferences() ) {
+    if ( ( !m_view->part()->onlyLocalReferences() ) &&
+         ( m_input->form() ) ) {
         popup->addSeparator();
         QAction *act = popup->addAction( i18n("Create Web Shortcut") );
         connect(act, SIGNAL(triggered()),