#!/usr/local/bin/perl -w # # HTML::Defaultify-- Pre-fill default values into an existing HTML form. # # The main purpose of this module is the defaultify() routine, which # takes a block of HTML and a hash of default values, and returns that HTML # with all form fields set based on those default values. Default values # (hash elements) may each be given in any of three forms: as a single # scalar, as a list in "\0"-delimited form, or as a reference to an actual # list. If the HTML contains more than one form, you can name which form to # defaultify. Return values are the defaultified block of HTML and a hash of # all unused default values (which may be useful as input to hidden_vars()). # Multiple form fields with the same name are handled correctly. Besides # the main defaultify() routine, this module includes several related # routines which the programmer may find useful. # # This package prefers to have the HTML::Entities module available, but # can improvise without it. # # Copyright (c) 1996, 1997, 2002 James Marshall (james@jmarshall.com). # Adapted from the toolbox htmlutil.pl, which is (c) 1996, 1997 by same. # All rights reserved. # # This program is free software; you can redistribute it and/or modify it # under the same terms as Perl itself. # # Exported by default: # $new_HTML= &defaultify($HTML, \%defaults [, $form_name]) ; # ($new_HTML, $unused_defs)= &defaultify($HTML, \%defaults [, $form_name]) ; # # $my_subset_ref= &subhash(\%hash, @keys_to_include) ; # # Export is allowed: # $hidden_vars= &hidden_vars($unused_defaults_ref) ; # $hidden_vars= &hidden_vars(%unused_defaults) ; # $hidden_tag= &hidden_var($name, $value) ; # # ($tag_name, $attr_ref)= &parse_tag($tag) ; # $new_tag= &build_tag($tag_name, $attr_ref) ; # $new_tag= &build_tag($tag_name, %attr) ; # # # For better documentation, see "perldoc HTML::Defaultify" (or # "perldoc -F this_file_name.pm"). # # For the latest, see http://www.jmarshall.com/tools/defaultify/ . # #---- package-definition-related stuff ------------------------------------- package HTML::Defaultify ; use strict ; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS) ; require Exporter ; @ISA= qw(Exporter) ; $VERSION= '1.01' ; @EXPORT= qw( defaultify subhash ) ; @EXPORT_OK= qw( hidden_vars hidden_var parse_tag build_tag ) ; %EXPORT_TAGS= ( parse => [qw( parse_tag build_tag )], all => [@EXPORT, @EXPORT_OK], ) ; #---- actual package code below -------------------------------------------- use Carp ; use vars qw($HAS_HTML_ENTITIES) ; # Load HTML::Entities if available; set $HAS_HTML_ENTITIES accordingly. eval 'use HTML::Entities' ; $HAS_HTML_ENTITIES= ($@ eq '') ; # defaultify()-- takes a chunk of HTML that includes form input fields, # and sets defaults according to the hash sent. # Returns defaultified HTML block, and a reference to a hash of all defaults # that were not used (possibly for use with hidden_vars()). # In scalar context, only returns defaultified HTML block. # # ($new_HTML, $unused_defs)= &defaultify($HTML, $defaults [, $form_name]) ; # $new_HTML= &defaultify($HTML, $defaults [, $form_name]) ; # # $defaults is a reference to a hash of default values. Each default (hash # element) may be a scalar, a list in the form of a "\0"-delimited scalar, # or a reference to a real list. # As a special case, if $defaults is undefined, this routine clears all default # settings from $HTML, even if they were set with tag attributes, etc. # As another special case, if $defaults is a CGI object (from CGI.pm), this # routine uses its existing parameters as the default set, by calling its # Vars() method. # If you have an existing hash instead of a reference, use e.g. \%my_hash . # If $form_name is given, then only the form(s) with that name in $HTML will # be defaultified. Otherwise, all of $HTML will be defaultified. # Tags inside comments or blocks, # and blocks from $HTML, to avoid matching tags # inside them. Replace these extractions with markers, so they can be # restored after defaultification is complete. Somewhat hacky approach, # but works. # Extractions are stored in @extracts, and the markers consist of: a random # string not otherwise in $HTML, plus each extraction's location in # @extracts, plus "\0". # All four kinds of extractions (two comment formats, scripts, and styles) # are handled simultaneously. This correctly handles cases of when # " # or tags. This is most likely what the HTML author expects # anyway, though it violates the HTML spec. Worse, browsers vary on # whether they'll end a " # inside the script code. Balancing all this, for here it's a reasonable # policy to end those blocks on "" and "". # There's a potential problem with the marker: Even if it's not in # $HTML, certain sequences could cause problems. Consider a marker of # "xy1xy", and a comment preceded by "xy1". After the comment->marker # replacement, the string is "xy1xy1xy" and will match too early. But # since we know \d+\0 will always follow the marker, then excluding # digits and \0 from the marker will prevent a wrong match like this. # I'm pretty sure this solves it, but please tell me if you think of # any combinations that could break this. # Generate a random 5-character string. Exclude digits, \0, and # what the hell, "<" and ">". # srand is automatically called in Perl 5.004 and later. do { $marker= pack("C5", map {rand(193)+63} 1..5) ; # start after ">" } while $HTML=~ /\Q$marker/ ; # Extract comments,