changeset 262:bb0694ce4e6b

- html.cc cleanup (in progress). Source split.
author jcid
date Thu, 05 Jun 2008 15:12:04 +0200
parents b4a68758b032
children af0161ff58ec
files ChangeLog src/Makefile.am src/form.cc src/form.hh src/html.cc src/html_common.hh
diffstat 6 files changed, 268 insertions(+), 205 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Jun 04 00:39:11 2008 +0200
+++ b/ChangeLog	Thu Jun 05 15:12:04 2008 +0200
@@ -156,7 +156,7 @@
  - Implemented the SELECT element in FORMS!
  - Implemented MULTIPLE SELECT in FORMS.
  - Fixed a memory leak in nav.c
-!- html.cc cleanup (in progress).
+!- html.cc cleanup (in progress). New classes, form API, source split.
    Patches: Jeremy Henty
 +- Added int32_t, EAI_NODATA and iconv tests for FreeBSD.
    Patch: Thomas-Martin Seck
--- a/src/Makefile.am	Wed Jun 04 00:39:11 2008 +0200
+++ b/src/Makefile.am	Thu Jun 05 15:12:04 2008 +0200
@@ -69,6 +69,8 @@
 	plain.cc \
 	html.cc \
 	html.hh \
+	form.cc \
+	form.hh \
 	bookmark.c \
 	bookmark.h \
 	dns.c \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/form.cc	Thu Jun 05 15:12:04 2008 +0200
@@ -0,0 +1,13 @@
+/*
+ * File: form.cc
+ *
+ * Copyright 2008 Jorge Arellano Cid <jcid@dillo.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ */
+
+#include "form.hh"
+#include "html_common.hh"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/form.hh	Thu Jun 05 15:12:04 2008 +0200
@@ -0,0 +1,25 @@
+#ifndef __FORM_HH__
+#define __FORM_HH__
+
+/*
+ * Typedefs 
+ */
+
+typedef enum {
+   DILLO_HTML_METHOD_UNKNOWN,
+   DILLO_HTML_METHOD_GET,
+   DILLO_HTML_METHOD_POST
+} DilloHtmlMethod;
+
+typedef enum {
+   DILLO_HTML_ENC_URLENCODING,
+   DILLO_HTML_ENC_MULTIPART
+} DilloHtmlEnc;
+
+/*
+ * Classes 
+ */
+
+class DilloHtmlForm;
+
+#endif /* __FORM_HH__ */
--- a/src/html.cc	Wed Jun 04 00:39:11 2008 +0200
+++ b/src/html.cc	Thu Jun 05 15:12:04 2008 +0200
@@ -43,6 +43,7 @@
 #include "prefs.h"
 #include "capi.h"
 #include "html.hh"
+#include "html_common.hh"
 
 #include "dw/textblock.hh"
 #include "dw/bullet.hh"
@@ -65,21 +66,6 @@
 #define TAB_SIZE 8
 #define dillo_dbg_rendering 0
 
-// Dw to Textblock
-#define DW2TB(dw)  ((Textblock*)dw)
-// "html struct" to "Layout"
-#define HT2LT(html)  ((Layout*)html->bw->render_layout)
-// "Image" to "Dw Widget"
-#define IM2DW(Image)  ((Widget*)Image->dw)
-// Top of the parsing stack
-#define S_TOP(html)  (html->stack->getRef(html->stack->size()-1))
-
-// Add a bug-meter message.
-#define BUG_MSG(...)                               \
-   D_STMT_START {                                  \
-         html->bugMessage(__VA_ARGS__);            \
-   } D_STMT_END
-
 /*-----------------------------------------------------------------------------
  * Name spaces
  *---------------------------------------------------------------------------*/
@@ -94,31 +80,13 @@
 class DilloHtml;
 typedef void (*TagOpenFunct) (DilloHtml *Html, const char *Tag, int Tagsize);
 typedef void (*TagCloseFunct) (DilloHtml *Html, int TagIdx);
-typedef struct _DilloLinkImage   DilloLinkImage;
 typedef struct _DilloHtmlClass   DilloHtmlClass;
-typedef struct _DilloHtmlState   DilloHtmlState;
-class DilloHtmlForm;
 class DilloHtmlReceiver;
 class DilloHtmlInput;
 typedef struct _DilloHtmlSelect  DilloHtmlSelect;
 typedef struct _DilloHtmlOption  DilloHtmlOption;
 
 typedef enum {
-   DT_NONE,           
-   DT_HTML,           
-   DT_XHTML
-} DilloHtmlDocumentType;
-
-typedef enum {
-   DILLO_HTML_PARSE_MODE_INIT = 0,
-   DILLO_HTML_PARSE_MODE_STASH,
-   DILLO_HTML_PARSE_MODE_STASH_AND_BODY,
-   DILLO_HTML_PARSE_MODE_VERBATIM,
-   DILLO_HTML_PARSE_MODE_BODY,
-   DILLO_HTML_PARSE_MODE_PRE
-} DilloHtmlParseMode;
-
-typedef enum {
    SEEK_ATTR_START,
    MATCH_ATTR_NAME,
    SEEK_TOKEN_START,
@@ -135,30 +103,6 @@
 } DilloHtmlTagParsingFlags;
 
 typedef enum {
-   DILLO_HTML_TABLE_MODE_NONE,  /* no table at all */
-   DILLO_HTML_TABLE_MODE_TOP,   /* outside of <tr> */
-   DILLO_HTML_TABLE_MODE_TR,    /* inside of <tr>, outside of <td> */
-   DILLO_HTML_TABLE_MODE_TD     /* inside of <td> */
-} DilloHtmlTableMode;
-
-typedef enum {
-   HTML_LIST_NONE,
-   HTML_LIST_UNORDERED,
-   HTML_LIST_ORDERED
-} DilloHtmlListMode;
-
-typedef enum {
-   DILLO_HTML_METHOD_UNKNOWN,
-   DILLO_HTML_METHOD_GET,
-   DILLO_HTML_METHOD_POST
-} DilloHtmlMethod;
-
-typedef enum {
-   DILLO_HTML_ENC_URLENCODING,
-   DILLO_HTML_ENC_MULTIPART
-} DilloHtmlEnc;
-
-typedef enum {
    DILLO_HTML_INPUT_UNKNOWN,
    DILLO_HTML_INPUT_TEXT,
    DILLO_HTML_INPUT_PASSWORD,
@@ -178,50 +122,9 @@
    DILLO_HTML_INPUT_INDEX
 } DilloHtmlInputType;
 
-typedef enum {
-   IN_NONE        = 0,
-   IN_HTML        = 1 << 0,
-   IN_HEAD        = 1 << 1,
-   IN_BODY        = 1 << 2,
-   IN_FORM        = 1 << 3,
-   IN_SELECT      = 1 << 4,
-   IN_OPTION      = 1 << 5,
-   IN_TEXTAREA    = 1 << 6,
-   IN_MAP         = 1 << 7,
-   IN_PRE         = 1 << 8,
-   IN_BUTTON      = 1 << 9,
-   IN_LI          = 1 << 10,
-} DilloHtmlProcessingState;
-
 /*-----------------------------------------------------------------------------
  * Data Structures
  *---------------------------------------------------------------------------*/
-struct _DilloLinkImage {
-   DilloUrl *url;
-   DilloImage *image;
-};
-
-struct _DilloHtmlState {
-   dw::core::style::Style *style, *table_cell_style;
-   DilloHtmlParseMode parse_mode;
-   DilloHtmlTableMode table_mode;
-   bool_t cell_text_align_set;
-   DilloHtmlListMode list_type;
-   int list_number;
-
-   /* TagInfo index for the tag that's being processed */
-   int tag_idx;
-
-   dw::core::Widget *textblock, *table;
-
-   /* This is used to align list items (especially in enumerated lists) */
-   dw::core::Widget *ref_list_item;
-
-   /* This is used for list items etc; if it is set to TRUE, breaks
-      have to be "handed over" (see Html_add_indented and
-      Html_eventually_pop_dw). */
-   bool_t hand_over_break;
-};
 
 class DilloHtmlForm {
    friend class DilloHtmlReceiver;
@@ -317,95 +220,6 @@
 /*-----------------------------------------------------------------------------
  * Classes
  *---------------------------------------------------------------------------*/
-class DilloHtml {
-private:
-   class HtmlLinkReceiver: public dw::core::Widget::LinkReceiver {
-   public:
-      DilloHtml *html;
-
-      bool enter (dw::core::Widget *widget, int link, int img, int x, int y);
-      bool press (dw::core::Widget *widget, int link, int img, int x, int y,
-                  dw::core::EventButton *event);
-      bool click (dw::core::Widget *widget, int link, int img, int x, int y,
-                  dw::core::EventButton *event);
-   };
-   HtmlLinkReceiver linkReceiver;
-
-public:  //BUG: for now everything is public
-
-   BrowserWindow *bw;
-   DilloUrl *page_url, *base_url;
-   dw::core::Widget *dw;    /* this is duplicated in the stack */
-
-   /* -------------------------------------------------------------------*/
-   /* Variables required at parsing time                                 */
-   /* -------------------------------------------------------------------*/
-   size_t Buf_Consumed; /* amount of source from cache consumed */
-   char *Start_Buf;
-   int Start_Ofs;
-   char *content_type, *charset;
-   bool stop_parser;
-
-   size_t CurrTagOfs;
-   size_t OldTagOfs, OldTagLine;
-
-   DilloHtmlDocumentType DocType; /* as given by DOCTYPE tag */
-   float DocTypeVersion;          /* HTML or XHTML version number */
-
-   misc::SimpleVector<DilloHtmlState> *stack;
-
-   int InFlags; /* tracks which elements we are in */
-
-   Dstr *Stash;
-   bool_t StashSpace;
-
-   int pre_column;        /* current column, used in PRE tags with tabs */
-   bool_t PreFirstChar;   /* used to skip the first CR or CRLF in PRE tags */
-   bool_t PrevWasCR;      /* Flag to help parsing of "\r\n" in PRE tags */
-   bool_t PrevWasOpenTag; /* Flag to help deferred parsing of white space */
-   bool_t SPCPending;     /* Flag to help deferred parsing of white space */
-   bool_t PrevWasSPC;     /* Flag to help handling collapsing white space */
-   bool_t InVisitedLink;  /* used to 'contrast_visited_colors' */
-   bool_t ReqTagClose;    /* Flag to help handling bad-formed HTML */
-   bool_t CloseOneTag;    /* Flag to help Html_tag_cleanup_at_close() */
-   bool_t WordAfterLI;    /* Flag to help ignoring the 1st <P> after <LI> */
-   bool_t TagSoup;        /* Flag to enable the parser's cleanup functions */
-   char *NameVal;         /* used for validation of "NAME" and "ID" in <A> */
-
-   /* element counters: used for validation purposes */
-   uchar_t Num_HTML, Num_HEAD, Num_BODY, Num_TITLE;
-
-   Dstr *attr_data;       /* Buffer for attribute value */
-
-   /* -------------------------------------------------------------------*/
-   /* Variables required after parsing (for page functionality)          */
-   /* -------------------------------------------------------------------*/
-   misc::SimpleVector<DilloHtmlForm*> *forms;
-   misc::SimpleVector<DilloUrl*> *links;
-   misc::SimpleVector<DilloLinkImage*> *images;
-   ImageMapsList maps;
-
-   int32_t link_color;
-   int32_t visited_color;
-
-private:
-   bool_t parse_finished;
-   void freeParseData();
-   void initDw();  /* Used by the constructor */
-
-public:
-   DilloHtml(BrowserWindow *bw, const DilloUrl *url, const char *content_type);
-   ~DilloHtml();
-   void bugMessage(const char *format, ... );
-   void connectSignals(dw::core::Widget *dw);
-   void write(char *Buf, int BufSize, int Eof);
-   int getCurTagLineNumber();
-   void finishParsing(int ClientKey);
-   int formNew(DilloHtmlMethod method, const DilloUrl *action,
-               DilloHtmlEnc enc, const char *charset);
-   inline DilloHtmlForm *getCurrentForm ();
-   void loadImages (const DilloUrl *pattern);
-};
 
 
 /*
@@ -596,23 +410,6 @@
 }
 
 
-/*
- * Change one toplevel attribute. var should be an identifier. val is
- * only evaluated once, so you can safely use a function call for it.
- */
-#define HTML_SET_TOP_ATTR(html, var, val) \
-   do { \
-      StyleAttrs style_attrs; \
-      Style *old_style; \
-       \
-      old_style = S_TOP(html)->style; \
-      style_attrs = *old_style; \
-      style_attrs.var = (val); \
-      S_TOP(html)->style = \
-         Style::create (HT2LT(html), &style_attrs); \
-      old_style->unref (); \
-   } while (FALSE)
-
 
 
 /*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/html_common.hh	Thu Jun 05 15:12:04 2008 +0200
@@ -0,0 +1,226 @@
+#ifndef __HTML_COMMON_HH__
+#define __HTML_COMMON_HH__
+
+#include "url.h"
+#include "bw.h"
+
+#include "lout/misc.hh"
+#include "dw/core.hh"
+#include "dw/image.hh"
+
+#include "image.hh"
+
+#include "form.hh"
+
+/*
+ * Macros 
+ */
+
+// Dw to Textblock
+#define DW2TB(dw)  ((Textblock*)dw)
+// "html struct" to "Layout"
+#define HT2LT(html)  ((Layout*)html->bw->render_layout)
+// "Image" to "Dw Widget"
+#define IM2DW(Image)  ((Widget*)Image->dw)
+// Top of the parsing stack
+#define S_TOP(html)  (html->stack->getRef(html->stack->size()-1))
+
+// Add a bug-meter message.
+#define BUG_MSG(...)                               \
+   D_STMT_START {                                  \
+         html->bugMessage(__VA_ARGS__);            \
+   } D_STMT_END
+
+/*
+ * Change one toplevel attribute. var should be an identifier. val is
+ * only evaluated once, so you can safely use a function call for it.
+ */
+#define HTML_SET_TOP_ATTR(html, var, val) \
+   do { \
+      StyleAttrs style_attrs; \
+      Style *old_style; \
+       \
+      old_style = S_TOP(html)->style; \
+      style_attrs = *old_style; \
+      style_attrs.var = (val); \
+      S_TOP(html)->style = \
+         Style::create (HT2LT(html), &style_attrs); \
+      old_style->unref (); \
+   } while (FALSE)
+
+/*
+ * Typedefs 
+ */
+
+typedef struct _DilloLinkImage   DilloLinkImage;
+typedef struct _DilloHtmlState   DilloHtmlState;
+
+typedef enum {
+   DT_NONE,           
+   DT_HTML,           
+   DT_XHTML
+} DilloHtmlDocumentType;
+
+typedef enum {
+   DILLO_HTML_PARSE_MODE_INIT = 0,
+   DILLO_HTML_PARSE_MODE_STASH,
+   DILLO_HTML_PARSE_MODE_STASH_AND_BODY,
+   DILLO_HTML_PARSE_MODE_VERBATIM,
+   DILLO_HTML_PARSE_MODE_BODY,
+   DILLO_HTML_PARSE_MODE_PRE
+} DilloHtmlParseMode;
+
+typedef enum {
+   DILLO_HTML_TABLE_MODE_NONE,  /* no table at all */
+   DILLO_HTML_TABLE_MODE_TOP,   /* outside of <tr> */
+   DILLO_HTML_TABLE_MODE_TR,    /* inside of <tr>, outside of <td> */
+   DILLO_HTML_TABLE_MODE_TD     /* inside of <td> */
+} DilloHtmlTableMode;
+
+typedef enum {
+   HTML_LIST_NONE,
+   HTML_LIST_UNORDERED,
+   HTML_LIST_ORDERED
+} DilloHtmlListMode;
+
+typedef enum {
+   IN_NONE        = 0,
+   IN_HTML        = 1 << 0,
+   IN_HEAD        = 1 << 1,
+   IN_BODY        = 1 << 2,
+   IN_FORM        = 1 << 3,
+   IN_SELECT      = 1 << 4,
+   IN_OPTION      = 1 << 5,
+   IN_TEXTAREA    = 1 << 6,
+   IN_MAP         = 1 << 7,
+   IN_PRE         = 1 << 8,
+   IN_BUTTON      = 1 << 9,
+   IN_LI          = 1 << 10,
+} DilloHtmlProcessingState;
+
+/*
+ * Data Structures 
+ */
+
+struct _DilloLinkImage {
+   DilloUrl *url;
+   DilloImage *image;
+};
+
+struct _DilloHtmlState {
+   dw::core::style::Style *style, *table_cell_style;
+   DilloHtmlParseMode parse_mode;
+   DilloHtmlTableMode table_mode;
+   bool_t cell_text_align_set;
+   DilloHtmlListMode list_type;
+   int list_number;
+
+   /* TagInfo index for the tag that's being processed */
+   int tag_idx;
+
+   dw::core::Widget *textblock, *table;
+
+   /* This is used to align list items (especially in enumerated lists) */
+   dw::core::Widget *ref_list_item;
+
+   /* This is used for list items etc; if it is set to TRUE, breaks
+      have to be "handed over" (see Html_add_indented and
+      Html_eventually_pop_dw). */
+   bool_t hand_over_break;
+};
+
+/*
+ * Classes 
+ */
+
+class DilloHtml {
+private:
+   class HtmlLinkReceiver: public dw::core::Widget::LinkReceiver {
+   public:
+      DilloHtml *html;
+
+      bool enter (dw::core::Widget *widget, int link, int img, int x, int y);
+      bool press (dw::core::Widget *widget, int link, int img, int x, int y,
+                  dw::core::EventButton *event);
+      bool click (dw::core::Widget *widget, int link, int img, int x, int y,
+                  dw::core::EventButton *event);
+   };
+   HtmlLinkReceiver linkReceiver;
+
+public:  //BUG: for now everything is public
+
+   BrowserWindow *bw;
+   DilloUrl *page_url, *base_url;
+   dw::core::Widget *dw;    /* this is duplicated in the stack */
+
+   /* -------------------------------------------------------------------*/
+   /* Variables required at parsing time                                 */
+   /* -------------------------------------------------------------------*/
+   size_t Buf_Consumed; /* amount of source from cache consumed */
+   char *Start_Buf;
+   int Start_Ofs;
+   char *content_type, *charset;
+   bool stop_parser;
+
+   size_t CurrTagOfs;
+   size_t OldTagOfs, OldTagLine;
+
+   DilloHtmlDocumentType DocType; /* as given by DOCTYPE tag */
+   float DocTypeVersion;          /* HTML or XHTML version number */
+
+   lout::misc::SimpleVector<DilloHtmlState> *stack;
+
+   int InFlags; /* tracks which elements we are in */
+
+   Dstr *Stash;
+   bool_t StashSpace;
+
+   int pre_column;        /* current column, used in PRE tags with tabs */
+   bool_t PreFirstChar;   /* used to skip the first CR or CRLF in PRE tags */
+   bool_t PrevWasCR;      /* Flag to help parsing of "\r\n" in PRE tags */
+   bool_t PrevWasOpenTag; /* Flag to help deferred parsing of white space */
+   bool_t SPCPending;     /* Flag to help deferred parsing of white space */
+   bool_t PrevWasSPC;     /* Flag to help handling collapsing white space */
+   bool_t InVisitedLink;  /* used to 'contrast_visited_colors' */
+   bool_t ReqTagClose;    /* Flag to help handling bad-formed HTML */
+   bool_t CloseOneTag;    /* Flag to help Html_tag_cleanup_at_close() */
+   bool_t WordAfterLI;    /* Flag to help ignoring the 1st <P> after <LI> */
+   bool_t TagSoup;        /* Flag to enable the parser's cleanup functions */
+   char *NameVal;         /* used for validation of "NAME" and "ID" in <A> */
+
+   /* element counters: used for validation purposes */
+   uchar_t Num_HTML, Num_HEAD, Num_BODY, Num_TITLE;
+
+   Dstr *attr_data;       /* Buffer for attribute value */
+
+   /* -------------------------------------------------------------------*/
+   /* Variables required after parsing (for page functionality)          */
+   /* -------------------------------------------------------------------*/
+   lout::misc::SimpleVector<DilloHtmlForm*> *forms;
+   lout::misc::SimpleVector<DilloUrl*> *links;
+   lout::misc::SimpleVector<DilloLinkImage*> *images;
+   dw::ImageMapsList maps;
+
+   int32_t link_color;
+   int32_t visited_color;
+
+private:
+   bool_t parse_finished;
+   void freeParseData();
+   void initDw();  /* Used by the constructor */
+
+public:
+   DilloHtml(BrowserWindow *bw, const DilloUrl *url, const char *content_type);
+   ~DilloHtml();
+   void bugMessage(const char *format, ... );
+   void connectSignals(dw::core::Widget *dw);
+   void write(char *Buf, int BufSize, int Eof);
+   int getCurTagLineNumber();
+   void finishParsing(int ClientKey);
+   int formNew(DilloHtmlMethod method, const DilloUrl *action,
+               DilloHtmlEnc enc, const char *charset);
+   inline DilloHtmlForm *getCurrentForm ();
+   void loadImages (const DilloUrl *pattern);
+};
+
+#endif /* __HTML_COMMON_HH__ */