| 1 | #!/usr/local/bin/perl | ||
| 2 | # Copyright (c) 2005-2008 W3C | ||
| 3 | |||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use CGI qw(param); | ||
| 9 | require LWP::UserAgent; | ||
| 10 | use URI; | ||
| 11 | use CGI::Carp 'fatalsToBrowser'; | ||
| 12 | use HTML::Template 2.6 qw(); | ||
| 13 | use W3C::XHTML::HTMLCompatChecker; | ||
| 14 | |||
| 15 | # Define global constants | ||
| 16 | use constant TRUE => 1; | ||
| 17 | use constant FALSE => 0; | ||
| 18 | |||
| 19 | |||
| 20 | ## Output routines ######################################################## | ||
| 21 | sub prep_output { | ||
| 22 | my $output_param = shift; | ||
| 23 | my $html_output_template_text = CGI::header(). ' | ||
| 24 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| 25 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| 26 | <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"> | ||
| 27 | <head> | ||
| 28 | <title>W3C HTML Compatibility Checker for HTML</title> | ||
| 29 | <style type="text/css" media="all"> | ||
| 30 | @import "http://validator.w3.org/style/base.css"; | ||
| 31 | p.submit_button { margin-top: 1em; } | ||
| 32 | </style> | ||
| 33 | |||
| 34 | </head> | ||
| 35 | <body> | ||
| 36 | <div id="banner"> | ||
| 37 | <h1 id="title"> | ||
| 38 | <a href="http://www.w3.org/"><img alt="W3C" width="110" height="61" id="logo" src="http://validator.w3.org/images/w3c.png" /></a> | ||
| 39 | <a href="./"><span>HTML Compatibility Checker for HTML</span></a> | ||
| 40 | </h1> | ||
| 41 | <p id="tagline">Check XHTML documents against HTML compatibility guidelines</p> | ||
| 42 | </div> | ||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | <div id="frontforms"> | ||
| 48 | <ul id="tabset_tabs"> | ||
| 49 | <li class="selected"><a href="#validate-by-uri"><span>Check online</span> document</a></li> | ||
| 50 | </ul> | ||
| 51 | <div id="fields"> | ||
| 52 | |||
| 53 | <fieldset id="validate-by-uri" class="tabset_content front"> | ||
| 54 | <form method="get" action=""> | ||
| 55 | <p> | ||
| 56 | <label title="Address of page to Validate" for="uri">Address:</label> | ||
| 57 | <input type="text" name="uri" id="uri" size="60" <TMPL_IF NAME="uri">value="<TMPL_VAR NAME="uri" ESCAPE="HTML">"</TMPL_IF>/> | ||
| 58 | </p> | ||
| 59 | <p class="submit_button"><input title="Submit for validation" type="submit" value="Check" /> | ||
| 60 | </p> | ||
| 61 | |||
| 62 | |||
| 63 | </fieldset> | ||
| 64 | </form> | ||
| 65 | </div> | ||
| 66 | |||
| 67 | <dl> | ||
| 68 | <TMPL_LOOP NAME="message_loop"> | ||
| 69 | <dt><strong><TMPL_VAR NAME="severity" ESCAPE="HTML"></strong> Line <TMPL_VAR NAME="line" ESCAPE="HTML"> column <TMPL_VAR NAME="column" ESCAPE="HTML"></dt> | ||
| 70 | <dd><a href="http://www.w3.org/TR/xhtml1/#C_<TMPL_VAR NAME="cnum" ESCAPE="HTML">"><TMPL_VAR NAME="message_text" ESCAPE="HTML"></a></dd> | ||
| 71 | </TMPL_LOOP> | ||
| 72 | </dl> | ||
| 73 | |||
| 74 | <TMPL_UNLESS NAME="uri"><p>Enter the URI of an XHTML 1.0 document which you would like to check against the | ||
| 75 | <a href="http://www.w3.org/TR/xhtml1/#guidelines">HTML Compatibility Guidelines</a> .</p></TMPL_UNLESS> | ||
| 76 | |||
| 77 | <TMPL_UNLESS NAME="has_messages"><TMPL_IF NAME="uri"><p>No issue found in this document. Congratulations.</p></TMPL_IF></TMPL_UNLESS> | ||
| 78 | |||
| 79 | <TMPL_IF NAME="Abort"> | ||
| 80 | <p>The document was not checked against HTML Compatibility Guidelines. | ||
| 81 | Reason: <em><TMPL_VAR NAME="Abort_Message"></em>. | ||
| 82 | </p> | ||
| 83 | </TMPL_IF> | ||
| 84 | |||
| 85 | </div> | ||
| 86 | |||
| 87 | </body> | ||
| 88 | </html> | ||
| 89 | '; | ||
| 90 | |||
| 91 | my $html_output = HTML::Template->new_scalar_ref(\$html_output_template_text, | ||
| 92 | die_on_bad_params => FALSE, | ||
| 93 | loop_context_vars => TRUE, | ||
| 94 | ); | ||
| 95 | |||
| 96 | |||
| 97 | my $xml_output_template_text = 'Content-Type: text/xml; charset=UTF-8 | ||
| 98 | |||
| 99 | <?xml version="1.0" encoding="UTF-8"?><observationresponse xmlns="http://www.w3.org/unicorn/observationresponse" | ||
| 100 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 101 | xsi:schemaLocation="http://www.w3.org/QA/2006/obs_framework/ns/observation/ http://www.w3.org/QA/2006/obs_framework/response/observer-response.xsd"> | ||
| 102 | <uri><TMPL_VAR NAME="uri" ESCAPE="HTML"></uri> | ||
| 103 | <passed><TMPL_IF NAME="passed">true<TMPL_ELSE>false</TMPL_IF></passed> | ||
| 104 | <result> | ||
| 105 | <errors> | ||
| 106 | <errorcount><TMPL_VAR NAME="error_count" ESCAPE="HTML"></errorcount> | ||
| 107 | <errorlist> | ||
| 108 | <uri><TMPL_VAR NAME="uri" ESCAPE="HTML"></uri> | ||
| 109 | <TMPL_LOOP NAME="error_loop"> | ||
| 110 | <error> | ||
| 111 | <line><TMPL_VAR NAME="line" ESCAPE="HTML"></line> | ||
| 112 | <column><TMPL_VAR NAME="column" ESCAPE="HTML"></column> | ||
| 113 | <message><TMPL_VAR NAME="message_text" ESCAPE="HTML"></message> | ||
| 114 | <longmessage>See HTML Compatibility Guideline <TMPL_VAR NAME="cnum" ESCAPE="HTML">: | ||
| 115 | <a href="http://www.w3.org/TR/xhtml1/#C_<TMPL_VAR NAME="cnum" ESCAPE="HTML">"><TMPL_VAR NAME="guideline_title" ESCAPE="HTML"></a></longmessage> | ||
| 116 | </error> | ||
| 117 | </TMPL_LOOP> | ||
| 118 | </errorlist> | ||
| 119 | </errors> | ||
| 120 | <warnings> | ||
| 121 | <warningcount><TMPL_VAR NAME="warning_count" ESCAPE="HTML"></warningcount> | ||
| 122 | <warninglist> | ||
| 123 | <uri><TMPL_VAR NAME="uri" ESCAPE="HTML"></uri> | ||
| 124 | <TMPL_LOOP NAME="warning_loop"> | ||
| 125 | <warning> | ||
| 126 | <line><TMPL_VAR NAME="line" ESCAPE="HTML"></line> | ||
| 127 | <column><TMPL_VAR NAME="column" ESCAPE="HTML"></column> | ||
| 128 | <message><TMPL_VAR NAME="message_text" ESCAPE="HTML"></message> | ||
| 129 | <longmessage>See HTML Compatibility Guideline <TMPL_VAR NAME="cnum" ESCAPE="HTML">: | ||
| 130 | <a href="http://www.w3.org/TR/xhtml1/#C_<TMPL_VAR NAME="cnum" ESCAPE="HTML">"><TMPL_VAR NAME="guideline_title" ESCAPE="HTML"></a></longmessage> | ||
| 131 | </warning> | ||
| 132 | </TMPL_LOOP> | ||
| 133 | </warninglist> | ||
| 134 | </warnings> | ||
| 135 | <informations> | ||
| 136 | <infocount><TMPL_VAR NAME="info_count" ESCAPE="HTML"></infocount> | ||
| 137 | <infolist> | ||
| 138 | <uri><TMPL_VAR NAME="uri" ESCAPE="HTML"></uri> | ||
| 139 | <TMPL_LOOP NAME="info_loop"> | ||
| 140 | <info> | ||
| 141 | <line><TMPL_VAR NAME="line" ESCAPE="HTML"></line> | ||
| 142 | <column><TMPL_VAR NAME="column" ESCAPE="HTML"></column> | ||
| 143 | <message><TMPL_VAR NAME="message_text" ESCAPE="HTML"></message> | ||
| 144 | <longmessage>See HTML Compatibility Guideline <TMPL_VAR NAME="cnum" ESCAPE="HTML">: | ||
| 145 | <a href="http://www.w3.org/TR/xhtml1/#C_<TMPL_VAR NAME="cnum" ESCAPE="HTML">"><TMPL_VAR NAME="guideline_title" ESCAPE="HTML"></a></longmessage> | ||
| 146 | </info> | ||
| 147 | </TMPL_LOOP> | ||
| 148 | <TMPL_UNLESS NAME="is_wf"> | ||
| 149 | <info> | ||
| 150 | <message>The document appears to not be well-formed XML. Either it is not XHTML (e.g HTML up to version 4.01) or it has XML well-formedness errors. | ||
| 151 | HTML Compatibility guidelines checking does not apply.</message> | ||
| 152 | </info> | ||
| 153 | </TMPL_UNLESS> | ||
| 154 | </infolist> | ||
| 155 | </informations> | ||
| 156 | </result> | ||
| 157 | </observationresponse> | ||
| 158 | '; | ||
| 159 | |||
| 160 | my $xml_output = HTML::Template->new_scalar_ref(\$xml_output_template_text, | ||
| 161 | die_on_bad_params => FALSE, | ||
| 162 | loop_context_vars => TRUE, | ||
| 163 | ); | ||
| 164 | |||
| 165 | if ($output_param eq "html") { | ||
| 166 | return $html_output; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | return $xml_output; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | ## Main ################################################################### | ||
| 174 | |||
| 175 | my $uri = param('uri'); # "http://www.w3.org/TR/xhtml1/"; | ||
| 176 | my $output_param = "html"; | ||
| 177 | if (defined param('output')) | ||
| 178 | { | ||
| 179 | $output_param = param('output'); | ||
| 180 | if ($output_param ne "html" and $output_param ne "xml") { | ||
| 181 | $output_param = "html"; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | my $debug = 0; | ||
| 186 | |||
| 187 | if (defined param('debug')) | ||
| 188 | { | ||
| 189 | $debug = param('debug'); | ||
| 190 | } | ||
| 191 | |||
| 192 | my $output = &prep_output($output_param); | ||
| 193 | ## output defaults | ||
| 194 | $output->param(passed => 1); | ||
| 195 | $output->param(info_count => 0); | ||
| 196 | $output->param(warning_count => 0); | ||
| 197 | $output->param(error_count => 0); | ||
| 198 | $output->param(Abort=>0); | ||
| 199 | $output->param(has_messages=>0); | ||
| 200 | |||
| 201 | |||
| 202 | my $compat_parser = W3C::XHTML::HTMLCompatChecker->new(); | ||
| 203 | if ($uri){ | ||
| 204 | my @checker_messages = $compat_parser->check_uri($uri); | ||
| 205 | if (exists $checker_messages[0]) { | ||
| 206 | $output->param(has_messages=>1); | ||
| 207 | if ($checker_messages[0]{"severity"} eq "Abort"){ | ||
| 208 | if ($checker_messages[0]{"message_text"} ne "Bad URI") { $output->param(uri => $uri);} | ||
| 209 | $output->param(Abort=>1); | ||
| 210 | $output->param(Abort_Message=>$checker_messages[0]{"message_text"}); | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | $output->param(uri => $uri); | ||
| 214 | $output->param(message_loop => \@checker_messages); | ||
| 215 | my (@ERRORS, @WARNINGS, @INFOS); | ||
| 216 | for (my $i=0; $i < scalar @checker_messages; $i++) | ||
| 217 | { | ||
| 218 | if ($checker_messages[$i]{'severity'} eq "Error") { push @ERRORS, $checker_messages[$i];} | ||
| 219 | elsif ($checker_messages[$i]{'severity'} eq "Warning") {push @WARNINGS, $checker_messages[$i];} | ||
| 220 | else {push @INFOS, $checker_messages[$i];} | ||
| 221 | } | ||
| 222 | if (@ERRORS) {$output->param(passed => 0) } else {$output->param(passed => 1)} | ||
| 223 | $output->param(info_loop => \@INFOS); | ||
| 224 | $output->param(info_count => scalar @INFOS); | ||
| 225 | $output->param(warning_loop => \@WARNINGS); | ||
| 226 | $output->param(warning_count => scalar @WARNINGS); | ||
| 227 | $output->param(error_loop => \@ERRORS); | ||
| 228 | $output->param(error_count => scalar @ERRORS); | ||
| 229 | |||
| 230 | } | ||
| 231 | } else { | ||
| 232 | $output->param(uri => $uri); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | print $output->output(); | ||
| 236 | |||
| 237 | |||
| 238 | __END__ |