]> sjero.net Git - wget/blobdiff - src/html-parse.c
[svn] Use hash table for tag lookup in html-url.c and html-parse.c.
[wget] / src / html-parse.c
index a5fab446edf7a0ef04c6d399369175a94286f6a6..88aec75341f7d0553af4b83843eff698817a39ea 100644 (file)
@@ -130,8 +130,17 @@ so, delete this exception statement from your version.  */
 # define TOLOWER(x) tolower (x)
 # define TOUPPER(x) toupper (x)
 
-static struct options opt;
-#endif /* STANDALONE */
+struct hash_table {
+  int dummy;
+};
+static void *
+hash_table_get (const struct hash_table *ht, void *ptr)
+{
+  return ptr;
+}
+#else  /* not STANDALONE */
+# include "hash.h"
+#endif
 
 /* Pool support.  A pool is a resizable chunk of memory.  It is first
    allocated on the stack, and moved to the heap if it needs to be
@@ -392,24 +401,6 @@ convert_and_copy (struct pool *pool, const char *beg, const char *end, int flags
     }
 }
 \f
-/* Check whether the contents of [POS, POS+LENGTH) match any of the
-   strings in the ARRAY.  */
-static int
-array_allowed (const char **array, const char *beg, const char *end)
-{
-  int length = end - beg;
-  if (array)
-    {
-      for (; *array; array++)
-       if (length >= strlen (*array)
-           && !strncasecmp (*array, beg, length))
-         break;
-      if (!*array)
-       return 0;
-    }
-  return 1;
-}
-\f
 /* Originally we used to adhere to rfc 1866 here, and allowed only
    letters, digits, periods, and hyphens as names (of tags or
    attributes).  However, this broke too many pages which used
@@ -661,6 +652,19 @@ find_comment_end (const char *beg, const char *end)
   return NULL;
 }
 \f
+/* Return non-zero of the string inside [b, e) are present in hash
+   table HT.  */
+
+static int
+name_allowed (const struct hash_table *ht, const char *b, const char *e)
+{
+  char *copy;
+  if (!ht)
+    return 1;
+  BOUNDED_TO_ALLOCA (b, e, copy);
+  return hash_table_get (ht, copy) != NULL;
+}
+
 /* Advance P (a char pointer), with the explicit intent of being able
    to read the next character.  If this is not possible, go to finish.  */
 
@@ -692,7 +696,7 @@ static int tag_backout_count;
 
 /* Map MAPFUN over HTML tags in TEXT, which is SIZE characters long.
    MAPFUN will be called with two arguments: pointer to an initialized
-   struct taginfo, and CLOSURE.
+   struct taginfo, and MAPARG.
 
    ALLOWED_TAG_NAMES should be a NULL-terminated array of tag names to
    be processed by this function.  If it is NULL, all the tags are
@@ -708,10 +712,10 @@ static int tag_backout_count;
 
 void
 map_html_tags (const char *text, int size,
-              const char **allowed_tag_names,
-              const char **allowed_attribute_names,
-              void (*mapfun) (struct taginfo *, void *),
-              void *closure)
+              void (*mapfun) (struct taginfo *, void *), void *maparg,
+              int flags,
+              const struct hash_table *allowed_tags,
+              const struct hash_table *allowed_attributes)
 {
   /* storage for strings passed to MAPFUN callback; if 256 bytes is
      too little, POOL_APPEND allocates more with malloc. */
@@ -756,7 +760,7 @@ map_html_tags (const char *text, int size,
        declaration).  */
     if (*p == '!')
       {
-       if (!opt.strict_comments
+       if (!(flags & MHT_STRICT_COMMENTS)
            && p < end + 3 && p[1] == '-' && p[2] == '-')
          {
            /* If strict comments are not enforced and if we know
@@ -795,7 +799,7 @@ map_html_tags (const char *text, int size,
     if (end_tag && *p != '>')
       goto backout_tag;
 
-    if (!array_allowed (allowed_tag_names, tag_name_begin, tag_name_end))
+    if (!name_allowed (allowed_tags, tag_name_begin, tag_name_end))
       /* We can't just say "goto look_for_tag" here because we need
          the loop below to properly advance over the tag's attributes.  */
       uninteresting_tag = 1;
@@ -893,11 +897,9 @@ map_html_tags (const char *text, int size,
                  goto look_for_tag;
                attr_raw_value_end = p; /* <foo bar="baz"> */
                                        /*               ^ */
-               /* The AP_TRIM_BLANKS is there for buggy HTML
-                  generators that generate <a href=" foo"> instead of
-                  <a href="foo"> (Netscape ignores spaces as well.)
-                  If you really mean space, use &32; or %20.  */
-               operation = AP_PROCESS_ENTITIES | AP_TRIM_BLANKS;
+               operation = AP_PROCESS_ENTITIES;
+               if (flags & MHT_TRIM_VALUES)
+                 operation |= AP_TRIM_BLANKS;
              }
            else
              {
@@ -938,9 +940,7 @@ map_html_tags (const char *text, int size,
        /* If we aren't interested in the attribute, skip it.  We
            cannot do this test any sooner, because our text pointer
            needs to correctly advance over the attribute.  */
-       if (allowed_attribute_names
-           && !array_allowed (allowed_attribute_names, attr_name_begin,
-                              attr_name_end))
+       if (!name_allowed (allowed_attributes, attr_name_begin, attr_name_end))
          continue;
 
        GROW_ARRAY (pairs, attr_pair_size, nattrs + 1, attr_pair_resized,
@@ -985,7 +985,7 @@ map_html_tags (const char *text, int size,
       taginfo.start_position = tag_start_position;
       taginfo.end_position   = p + 1;
       /* Ta-dam! */
-      (*mapfun) (&taginfo, closure);
+      (*mapfun) (&taginfo, maparg);
       ADVANCE (p);
     }
     goto look_for_tag;
@@ -1038,7 +1038,7 @@ int main ()
       x = (char *)xrealloc (x, size);
     }
 
-  map_html_tags (x, length, NULL, NULL, test_mapper, &tag_counter);
+  map_html_tags (x, length, test_mapper, &tag_counter, 0, NULL, NULL);
   printf ("TAGS: %d\n", tag_counter);
   printf ("Tag backouts:     %d\n", tag_backout_count);
   printf ("Comment backouts: %d\n", comment_backout_count);