Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

samtregar (2699)

samtregar
  (email not shown publicly)
http://sam.tregar.com/

Journal of samtregar (2699)

Friday June 13, 2003
12:14 PM

CGI::Application and HTML::Lint

[ #12793 ]
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 = "<ul>" .
        join("", map { "<li>$_</li>" }
                 map { s/&/&amp;/g;
                       s/</&lt;/g;
                       s/>/&gt;/g;
                       s/\\/\\\\/g;
                       s/"/\\"/g;
                       $_; }
                 map { $_->as_string }
                   $lint->errors) .
                 "</ul>";
    my $js = <<END;
<script language="javascript">
  var html_lint_window = window.open("", "html_lint_window", "height=300,width=600");
html_lint_window.document.write("<html><head><title>HT ML Errors Detected</title></head><body><h1>HTML Errors Detected</h1>$err_text</body></html>");
  html_lint_window.document.close();
  html_lint_window.focus();
</script>
END

    # insert the js code before the body close,
    # if one exists
    if ($$o =~ m!</body>!) {
      $$o =~ s!</body>!$js\n</body>!;
    } 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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.