#!/usr/bin/env ruby

# load the CGI module
require 'cgi'

# build list of test output types and associated test CGIs
HTML_TYPES = %w{xhtml10 html4 html4Tr xhtml10Tr}

# clear stdin (to prevent CGI offline-mode)
$stdin = File.open('/dev/null', 'r')

# iterate over each test CGI and render the same document
HTML_TYPES.each do |key| 
  # create new CGI with the given output type
  cgi = CGI.new(key)

  # generate document title
  doc_title = "CGI Output Test (#{key})"

  puts "#{key}: " << cgi.html { 
    cgi.head { 
      head = cgi.title { doc_title }

      # style link attributes
      link_attrs = { 
        'type' => 'text/css', 
        'href' => 'style.css', 
        'rel'  => 'stylesheet' 
      }

      # test empty element behavior
      head + cgi.link(link_attrs) 
    } + 
    
    cgi.body { 
      cgi.h1 { doc_title } + 

      # test empty element behavior
      cgi.hr + 

      # test element balancing
      cgi.p { 
        'This is a test ' + 
        cgi.acronym('title' => 'HyperText Markup Language') { 'HTML' } + 
        ' document.' 
      } +

      cgi.form('post', 'foobar.cgi') {
        # test hidden element behavior
        cgi.hidden('hi', 'hidden') +

        # test bare (minimized) attribute behavior
        cgi.popup_menu('bare_attr_test', ['1', 'foo'], ['2', 'bar', true]) +
        cgi.submit("Testing Form Output")
      } +

      cgi.pre { 
        # test id munging
        cgi.code('name' => 'id-map-test') {
          'this is a test of name mapping'
        } 
      }
    } 
  } 
end

