changeset 598:4ff90b6242f9

fix id, class, style attribute handling
author Johannes Hofmann <Johannes.Hofmann@gmx.de>
date Fri, 14 Nov 2008 20:18:35 +0100
parents 63c57aa7f872
children 72772fdef974
files src/html.cc src/styleengine.cc src/styleengine.hh
diffstat 3 files changed, 51 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/html.cc	Fri Nov 14 19:36:18 2008 +0100
+++ b/src/html.cc	Fri Nov 14 20:18:35 2008 +0100
@@ -2451,11 +2451,11 @@
 
       if (a_Capi_get_flags(url) & CAPI_IsCached) {
          html->InVisitedLink = true;
-         html->styleEngine->setPseudoClass ("visited");
+         html->styleEngine->setPseudo ("visited");
          if (html->visited_color != -1)
             props.set (CssProperty::CSS_PROPERTY_COLOR, html->visited_color);
       } else {
-         html->styleEngine->setPseudoClass ("link");
+         html->styleEngine->setPseudo ("link");
          if (html->link_color != -1)
             props.set (CssProperty::CSS_PROPERTY_COLOR, html->link_color);
       }
@@ -3399,7 +3399,6 @@
 {
    int ci, ni;           /* current and new tag indexes */
    const char *attrbuf;
-   char *id = NULL, *klass = NULL, *style = NULL;
    char *start = tag + 1; /* discard the '<' */
    int IsCloseTag = (*start == '/');
 
@@ -3437,6 +3436,7 @@
       /* Push the tag into the stack */
       Html_push_tag(html, ni);
 
+      html->styleEngine->startElement (ni);
 
       /* Now parse attributes that can appear on any tag */
       if (tagsize >= 8 &&        /* length of "<t id=i>" */
@@ -3449,13 +3449,14 @@
           * So we don't do it and hope for better specs in the future ...
           */
          if (attrbuf)
-            id = strdup (attrbuf);
-         Html_check_name_val(html, id, "id");
+            html->styleEngine->setId (attrbuf);
+
+         Html_check_name_val(html, attrbuf, "id");
          /* We compare the "id" value with the url-decoded "name" value */
-         if (!html->NameVal || strcmp(html->NameVal, id)) {
+         if (!html->NameVal || strcmp(html->NameVal, attrbuf)) {
             if (html->NameVal)
                BUG_MSG("'id' and 'name' attribute of <a> tag differ\n");
-            Html_add_anchor(html, id);
+            Html_add_anchor(html, attrbuf);
          }
       }
 
@@ -3469,26 +3470,16 @@
           attrbuf = Html_get_attr2(html, tag, tagsize, "class",
                                    HTML_LeftTrim | HTML_RightTrim);
           if (attrbuf)
-            klass = strdup (attrbuf);
+            html->styleEngine->setClass (attrbuf);
       }
 
       if (tagsize >= 11) {       /* length of "<t style=i>" */
           attrbuf = Html_get_attr2(html, tag, tagsize, "style",
                                    HTML_LeftTrim | HTML_RightTrim);
           if (attrbuf)
-            style = strdup (attrbuf);
+            html->styleEngine->setStyle (attrbuf);
       }
 
-
-      html->styleEngine->startElement (ni, id, klass, style);
-
-      if (id)
-         free (id);
-      if (klass)
-         free (klass);
-      if (style)
-         free (style);
-
       /* Call the open function for this tag */
       Tags[ni].open (html, tag, tagsize);
       if (html->stop_parser)
--- a/src/styleengine.cc	Fri Nov 14 19:36:18 2008 +0100
+++ b/src/styleengine.cc	Fri Nov 14 20:18:35 2008 +0100
@@ -11,6 +11,7 @@
 
 #include <stdio.h>
 #include <math.h>
+#include "../dlib/dlib.h"
 #include "styleengine.hh"
 
 using namespace dw::core::style;
@@ -47,9 +48,8 @@
 /**
  * \brief tell the styleEngine that a new html element has started.
  */
-void StyleEngine::startElement (int element, const char *id, const char *klass,
-   const char *style) {
-//   fprintf(stderr, "===> START %d %s %s %s\n", element, id, klass, style);
+void StyleEngine::startElement (int element) {
+//   fprintf(stderr, "===> START %d\n", element);
 
    if (stack->getRef (stack->size () - 1)->style == NULL)
       style0 ();
@@ -59,12 +59,30 @@
    n->style = NULL;
    n->depth = stack->size ();
    n->element = element;
-   n->id = id;
-   n->klass = klass;
+   n->id = NULL;
+   n->klass = NULL;
    n->pseudo = NULL;
-   n->styleAttribute = style;
+   n->styleAttribute = NULL;
 }
 
+void StyleEngine::setId (const char *id) {
+   Node *n =  stack->getRef (stack->size () - 1);
+   assert (n->id == NULL);
+   n->id = dStrdup (id);
+};
+
+void StyleEngine::setClass (const char *klass) {
+   Node *n =  stack->getRef (stack->size () - 1);
+   assert (n->klass == NULL);
+   n->klass = dStrdup (klass);
+};
+
+void StyleEngine::setStyle (const char *style) {
+   Node *n =  stack->getRef (stack->size () - 1);
+   assert (n->styleAttribute == NULL);
+   n->styleAttribute = dStrdup (style);
+};
+
 /**
  * \brief set properties that were definded using (mostly deprecated) HTML
  *    attributes (e.g. bgColor).
@@ -78,8 +96,10 @@
 /**
  * \brief set the CSS pseudo class (e.g. "link", "visited").
  */
-void StyleEngine::setPseudoClass (const char *pseudo) {
-   stack->getRef (stack->size () - 1)->pseudo = pseudo;
+void StyleEngine::setPseudo (const char *pseudo) {
+   Node *n =  stack->getRef (stack->size () - 1);
+   assert (n->pseudo == NULL);
+   n->pseudo = dStrdup (pseudo);
 }
 
 /**
@@ -91,9 +111,16 @@
    assert (element == stack->getRef (stack->size () - 1)->element);
 
    Node *n =  stack->getRef (stack->size () - 1);
+
    if (n->style)
       n->style->unref ();
-   
+   if (n->id)
+      dFree ((void*) n->id); 
+   if (n->klass)
+      dFree ((void*) n->klass); 
+   if (n->styleAttribute)
+      dFree ((void*) n->styleAttribute); 
+
    stack->setSize (stack->size () - 1);
 }
 
--- a/src/styleengine.hh	Fri Nov 14 19:36:18 2008 +0100
+++ b/src/styleengine.hh	Fri Nov 14 20:18:35 2008 +0100
@@ -37,11 +37,13 @@
             return NULL;
       };
 
-      void startElement (int tag, const char *id = NULL, const char *klass = NULL,
-         const char *style = NULL);
+      void startElement (int tag);
+      void setId (const char *id);
+      void setClass (const char *klass);
+      void setStyle (const char *style);
       void endElement (int tag);
+      void setPseudo (const char *pseudo);
       void setNonCssHints (CssPropertyList *nonCssHints);
-      void setPseudoClass (const char *pseudoClass);
 
       inline dw::core::style::Style *style () {
          dw::core::style::Style *s = stack->getRef (stack->size () - 1)->style;