--- /tmp/wget/srclimpio/html-url.c	2009-09-22 05:00:12.000000000 +0200
+++ html-url.c	2010-03-24 05:05:13.000000000 +0100
@@ -168,7 +168,9 @@ static const char *additional_attributes
   "name",                       /* used by tag_handle_meta  */
   "content",                    /* used by tag_handle_meta  */
   "action",                     /* used by tag_handle_form  */
-  "style"                       /* used by check_style_attr */
+  "style"                       /* used by check_style_attr,
+                                   tag_handle_link, tag_handle_form,
+                                   tag_find_urls  */
 };
 
 static struct hash_table *interesting_tags;
@@ -345,7 +347,22 @@ append_url (const char *link_uri, int po
 
   return newel;
 }
-
+
+/* MUST be called by tag handlers when the tag supports a style attribute.
+   This must be done so the urls found in the style are inserted in the
+   order they are in html (before or after other attributes found in the
+   tag, which have a URL) */
+
+static void
+parse_style_attr (struct taginfo *tag, struct map_context *ctx, int attrind)
+{
+  /* raw pos and raw size include the quotes, hence the +1 -2 */
+  get_urls_css (ctx, ATTR_POS(tag,attrind,ctx)+1, ATTR_SIZE(tag,attrind)-2);
+}
+
+/* Called for all the tags that don't have a handler. The ones with
+   a handler already know the attrind index */
+
 static void
 check_style_attr (struct taginfo *tag, struct map_context *ctx)
 {
@@ -354,12 +371,14 @@ check_style_attr (struct taginfo *tag, s
   if (!style)
     return;
 
-  /* raw pos and raw size include the quotes, hence the +1 -2 */
-  get_urls_css (ctx, ATTR_POS(tag,attrind,ctx)+1, ATTR_SIZE(tag,attrind)-2);
+  parse_style_attr (tag, ctx, attrind);
 }
 
 /* All the tag_* functions are called from collect_tags_mapper, as
-   specified by KNOWN_TAGS.  */
+   specified by KNOWN_TAGS. If the handler is adding URLs to the list
+   referenced by ctx->tail (this is done by append_url()), it MUST
+   check for a style attribute. If you don't, URLs in that style
+   will NOT be parsed (so --convert-links won't work properly) */
 
 /* Default tag handler: collect URLs from attributes specified for
    this tag by tag_url_attributes.  */
@@ -381,6 +400,8 @@ tag_find_urls (int tagid, struct taginfo
       }
   assert (first != -1);
 
+  const size_t size = countof (tag_url_attributes);
+  
   /* Loop over the "interesting" attributes of this tag.  In this
      example, it will loop over "src" and "lowsrc".
 
@@ -394,8 +415,6 @@ tag_find_urls (int tagid, struct taginfo
     {
       /* Find whether TAG/ATTRIND is a combination that contains a
          URL. */
-      char *link = tag->attrs[attrind].value;
-      const size_t size = countof (tag_url_attributes);
 
       /* If you're cringing at the inefficiency of the nested loops,
          remember that they both iterate over a very small number of
@@ -406,7 +425,8 @@ tag_find_urls (int tagid, struct taginfo
           if (0 == strcasecmp (tag->attrs[attrind].name,
                                tag_url_attributes[i].attr_name))
             {
-              struct urlpos *up = append_url (link, ATTR_POS(tag,attrind,ctx),
+              struct urlpos *up = append_url (tag->attrs[attrind].value,
+                                              ATTR_POS(tag,attrind,ctx),
                                               ATTR_SIZE(tag,attrind), ctx);
               if (up)
                 {
@@ -417,6 +437,11 @@ tag_find_urls (int tagid, struct taginfo
                     up->link_expect_html = 1;
                 }
             }
+           else if (0 == strcasecmp (tag->attrs[attrind].name, "style"))
+            {
+              /* Add URLs in the style attribute*/
+              parse_style_attr (tag, ctx, attrind);
+            }                                                                             
         }
     }
 }
@@ -453,14 +478,25 @@ static void
 tag_handle_form (int tagid, struct taginfo *tag, struct map_context *ctx)
 {
   int attrind;
-  char *action = find_attr (tag, "action", &attrind);
 
-  if (action)
+  /* This tag support a style attribute, so the URLs in it must be
+     inserted in the right order (before or after the other attributes
+     with URLs this tag may have) */              
+  for (attrind = 0; attrind < tag->nattrs; attrind++)
     {
-      struct urlpos *up = append_url (action, ATTR_POS(tag,attrind,ctx),
-                                      ATTR_SIZE(tag,attrind), ctx);
-      if (up)
-        up->ignore_when_downloading = 1;
+      if (0 == strcasecmp (tag->attrs[attrind].name, "action"))
+        {
+          struct urlpos *up = append_url (tag->attrs[attrind].value,
+                                          ATTR_POS(tag,attrind,ctx),
+                                          ATTR_SIZE(tag,attrind), ctx);
+          if (up)
+            up->ignore_when_downloading = 1;        
+        }
+      else if (0 == strcasecmp (tag->attrs[attrind].name, "style"))
+        {
+          /* Add URLs in the style attribute*/
+          parse_style_attr (tag, ctx, attrind);                    
+        }
     }
 }
 
@@ -471,37 +507,48 @@ static void
 tag_handle_link (int tagid, struct taginfo *tag, struct map_context *ctx)
 {
   int attrind;
-  char *href = find_attr (tag, "href", &attrind);
 
-  /* All <link href="..."> link references are external, except those
-     known not to be, such as style sheet and shortcut icon:
-
-       <link rel="stylesheet" href="...">
-       <link rel="shortcut icon" href="...">
-  */
-  if (href)
+  /* This tag support a style attribute, so the URLs in it must be
+     inserted in the right order (before or after the other attributes
+     with URLs this tag may have) */
+  for (attrind = 0; attrind < tag->nattrs; attrind++)
     {
-      struct urlpos *up = append_url (href, ATTR_POS(tag,attrind,ctx),
-                                      ATTR_SIZE(tag,attrind), ctx);
-      if (up)
+      /* All <link href="..."> link references are external, except those
+         known not to be, such as style sheet and shortcut icon:
+
+         <link rel="stylesheet" href="...">
+         <link rel="shortcut icon" href="...">
+      */
+      if (0 == strcasecmp (tag->attrs[attrind].name, "href"))
         {
-          char *rel = find_attr (tag, "rel", NULL);
-          if (rel)
+          struct urlpos *up = append_url (tag->attrs[attrind].value,
+                                          ATTR_POS(tag,attrind,ctx),
+                                          ATTR_SIZE(tag,attrind), ctx);
+          if (up)
             {
-              if (0 == strcasecmp (rel, "stylesheet"))
-                {
-                  up->link_inline_p = 1;
-                  up->link_expect_css = 1;
-                }
-              else if (0 == strcasecmp (rel, "shortcut icon"))
+              char *rel = find_attr (tag, "rel", NULL);
+              if (rel)
                 {
-                  up->link_inline_p = 1;
+                  if (0 == strcasecmp (rel, "stylesheet"))
+                    {
+                      up->link_inline_p = 1;
+                      up->link_expect_css = 1;
+                    }
+                  else if (0 == strcasecmp (rel, "shortcut icon"))
+                    {
+                      up->link_inline_p = 1;
+                    }
                 }
-            }
-          else
-            /* The external ones usually point to HTML pages, such as
-               <link rel="next" href="..."> */
-            up->link_expect_html = 1;
+              else
+                /* The external ones usually point to HTML pages, such as
+                   <link rel="next" href="..."> */
+                up->link_expect_html = 1;
+            }        
+        }
+      else if (0 == strcasecmp (tag->attrs[attrind].name, "style"))
+        {
+              /* Add URLs in the style attribute*/
+              parse_style_attr (tag, ctx, attrind);        
         }
     }
 }
@@ -633,9 +680,12 @@ collect_tags_mapper (struct taginfo *tag
   struct known_tag *t = hash_table_get (interesting_tags, tag->name);
 
   if (t != NULL)
-    t->handler (t->tagid, tag, ctx);
-
-  check_style_attr (tag, ctx);
+    t->handler (t->tagid, tag, ctx);  
+    
+  /* Style attribute check only if tag is unknown. For known tags, do
+     it in the handler */
+  if (t == NULL)
+    check_style_attr (tag, ctx);
 
   if (tag->end_tag_p && (0 == strcasecmp (tag->name, "style")) &&
       tag->contents_begin && tag->contents_end)
@@ -645,7 +695,7 @@ collect_tags_mapper (struct taginfo *tag
                   tag->contents_end - tag->contents_begin);
   }
 }
-
+
 /* Analyze HTML tags FILE and construct a list of URLs referenced from
    it.  It merges relative links in FILE with URL.  It is aware of
    <base href=...> and does the right thing.  */
