CGI::Application and HTML::Lint

samtregar on 2003-06-13T17:14:18

I just recently learned about HTML::Lint from petdance's journal. I immediately wanted to integrate it into my latest web project. Here's how I did it using CGI::Application's new cgiapp_postrun() method in my CGI::Application super-class:

# check for HTML errors
sub cgiapp_postrun {
  my ($self, $o) = @_;

  # parse the output with HTML::Lint
  my $lint = HTML::Lint->new();
  $lint->parse($$o);
  $lint->eof();

  # if there were errors put them into a
  # new window
  if ($lint->errors) {
      my $err_text = "
    " . join("", map { "
  • $_
  • " } map { s/&/&/g; s//>/g; s/\\/\\\\/g; s/"/\\"/g; $_; } map { $_->as_string } $lint->errors) . "
"; my $js = < var html_lint_window = window.open("", "html_lint_window", "height=300,width=600"); html_lint_window.document.write("HTML Errors Detected

HTML Errors Detected

$err_text"); html_lint_window.document.close(); html_lint_window.focus(); END # insert the js code before the body close, # if one exists if ($$o =~ m!!) { $$o =~ s!!$js\n!; } else { $$o .= $js; } } }

Using this code, when one or more HTML errors are detected a new window pops up with the errors in a list. The window is reused on future requests if not closed.

-sam