]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/nxhtml/nxhtml/html-chklnk/PerlLib/HTML/ParserTagEnd.pm
remove toolbar and menubar
[.emacs.d.git] / emacs / nxhtml / nxhtml / html-chklnk / PerlLib / HTML / ParserTagEnd.pm
1 package HTML::ParserTagEnd;\r
2 \r
3 # Author address: <gisle@aas.no>\r
4 ### Modified for <tag />, Lennart\r
5 \r
6 use strict;\r
7 use HTML::Entities ();\r
8 \r
9 use vars qw($VERSION);\r
10 $VERSION = "2.23";  # $Date: 1999/06/09 10:27:16 $\r
11 \r
12 \r
13 sub new\r
14 {\r
15     my $class = shift;\r
16     my $self = bless { '_buf'            => '',\r
17                        '_strict_comment' => 0,\r
18                      }, $class;\r
19     $self;\r
20 }\r
21 \r
22 \r
23 # A little note about the observed Netscape behaviour:\r
24 #\r
25 # It parse <xmp> in the depreceated 'literal' mode, i.e. no tags are\r
26 # recognized until a </xmp> is found.\r
27\r
28 # <listing> is parsed like <pre>, i.e. tags are recognized.  <listing>\r
29 # are presentend in smaller font than <pre>\r
30 #\r
31 # Netscape does not parse this comment correctly (it terminates the comment\r
32 # too early):\r
33 #\r
34 #    <! -- comment -- --> more comment -->\r
35 #\r
36 # Netscape ignores '<!--' and '-->' within the <SCRIPT> and <STYLE> tag.\r
37 # This is used as a trick to make non-script-aware browsers ignore\r
38 # the scripts.\r
39 \r
40 \r
41 sub parse\r
42 {\r
43     my $self = shift;\r
44     my $buf = \ $self->{'_buf'};\r
45     unless (defined $_[0]) {\r
46         # signals EOF (assume rest is plain text)\r
47         $self->text($$buf) if length $$buf;\r
48         $$buf = '';\r
49         return $self;\r
50     }\r
51     $$buf .= $_[0];\r
52     my $netscape_comment = !$self->{'_strict_comment'};\r
53 \r
54     # Parse html text in $$buf.  The strategy is to remove complete\r
55     # tokens from the beginning of $$buf until we can't deside whether\r
56     # it is a token or not, or the $$buf is empty.\r
57 \r
58   TOKEN:\r
59     while (1) {\r
60 \r
61         # First we try to pull off any plain text (anything before a "<" char)\r
62         if ($$buf =~ s|^([^<]+)||) {\r
63             if (length $$buf) {\r
64                 $self->text($1);\r
65             } else {\r
66                 my $text = $1;\r
67                 # At the end of the buffer, we should not parse white space\r
68                 # but leave it for parsing on the next round.\r
69                 if ($text =~ s|(\s+)$||) {\r
70                     $$buf = $1;\r
71                 # Same treatment for chopped up entites and words.\r
72                 # We must wait until we have it all.\r
73                 } elsif ($text =~ s|(\s*\S+)$||) {\r
74                     $$buf = $1;\r
75                 };\r
76                 $self->text($text) if length $text;\r
77                 last TOKEN;\r
78             }\r
79 \r
80         # Netscapes buggy comments are easy to handle\r
81         } elsif ($netscape_comment && $$buf =~ m|^<!\s*--|) {\r
82             if ($$buf =~ s|^<!\s*--(.*?)--\s*>||s) {\r
83                 $self->comment($1);\r
84             } else {\r
85                 last TOKEN;  # must wait until we see the end of it\r
86             }\r
87 \r
88         # Then, markup declarations (usually either <!DOCTYPE...> or a comment)\r
89         } elsif ($$buf =~ s|^(<!)||) {\r
90             my $eaten = $1;\r
91             my $text = '';\r
92             my @com = ();  # keeps comments until we have seen the end\r
93             # Eat text and beginning of comment\r
94             while ($$buf =~ s|^(([^>]*?)--)||) {\r
95                 $eaten .= $1;\r
96                 $text .= $2;\r
97                 # Look for end of comment\r
98                 if ($$buf =~ s|^((.*?)--)||s) {\r
99                     $eaten .= $1;\r
100                     push(@com, $2);\r
101                 } else {\r
102                     # Need more data to get all comment text.\r
103                     $$buf = $eaten . $$buf;\r
104                     last TOKEN;\r
105                 }\r
106             }\r
107             # Can we finish the tag\r
108             if ($$buf =~ s|^([^>]*)>||) {\r
109                 $text .= $1;\r
110                 $self->declaration($text) if $text =~ /\S/;\r
111                 # then tell about all the comments we found\r
112                 for (@com) { $self->comment($_); }\r
113             } else {\r
114                 $$buf = $eaten . $$buf;  # must start with it all next time\r
115                 last TOKEN;\r
116             }\r
117 \r
118         # Should we look for 'processing instructions' <? ...> ??\r
119         #} elsif ($$buf =~ s|<\?||) {\r
120             # ...\r
121 \r
122         # Then, look for a end tag\r
123         } elsif ($$buf =~ s|^</||) {\r
124             # end tag\r
125             if ($$buf =~ s|^([a-zA-Z][a-zA-Z0-9\.\-]*)(\s*>)||) {\r
126                 $self->end(lc($1), "</$1$2");\r
127             } elsif ($$buf =~ m|^[a-zA-Z]*[a-zA-Z0-9\.\-]*\s*$|) {\r
128                 $$buf = "</" . $$buf;  # need more data to be sure\r
129                 last TOKEN;\r
130             } else {\r
131                 # it is plain text after all\r
132                 $self->text("</");\r
133             }\r
134 \r
135         # Then, finally we look for a start tag\r
136         } elsif ($$buf =~ s|^(<([a-zA-Z]+)>)||) {\r
137             # special case plain start tags for slight speed-up (2.5%)\r
138             ### mod Lennart\r
139             $self->start(lc($2), {}, 0, [], $1);\r
140 \r
141         } elsif ($$buf =~ s|^<||) {\r
142             # start tag\r
143             my $eaten = '<';\r
144 \r
145             # This first thing we must find is a tag name.  RFC1866 says:\r
146             #   A name consists of a letter followed by letters,\r
147             #   digits, periods, or hyphens. The length of a name is\r
148             #   limited to 72 characters by the `NAMELEN' parameter in\r
149             #   the SGML declaration for HTML, 9.5, "SGML Declaration\r
150             #   for HTML".  In a start-tag, the element name must\r
151             #   immediately follow the tag open delimiter `<'.\r
152             if ($$buf =~ s|^(([a-zA-Z][a-zA-Z0-9\.\-]*)\s*)||) {\r
153                 $eaten .= $1;\r
154                 my $tag = lc $2;\r
155                 my %attr;\r
156                 my @attrseq;\r
157 \r
158                 # Then we would like to find some attributes\r
159                 #\r
160                 # Arrgh!! Since stupid Netscape violates RCF1866 by\r
161                 # using "_" in attribute names (like "ADD_DATE") of\r
162                 # their bookmarks.html, we allow this too.\r
163                 while ($$buf =~ s|^(([a-zA-Z][a-zA-Z0-9\.\-_]*)\s*)||) {\r
164                     $eaten .= $1;\r
165                     my $attr = lc $2;\r
166                     my $val;\r
167                     # The attribute might take an optional value (first we\r
168                     # check for an unquoted value)\r
169                     if ($$buf =~ s|(^=\s*([^\"\'>\s][^>\s]*)\s*)||) {\r
170                         $eaten .= $1;\r
171                         $val = $2;\r
172                         HTML::Entities::decode($val);\r
173                     # or quoted by " or '\r
174                     } elsif ($$buf =~ s|(^=\s*([\"\'])(.*?)\2\s*)||s) {\r
175                         $eaten .= $1;\r
176                         $val = $3;\r
177                         HTML::Entities::decode($val);\r
178                     # truncated just after the '=' or inside the attribute\r
179                     } elsif ($$buf =~ m|^(=\s*)$| or\r
180                              $$buf =~ m|^(=\s*[\"\'].*)|s) {\r
181                         $$buf = "$eaten$1";\r
182                         last TOKEN;\r
183                     } else {\r
184                         # assume attribute with implicit value\r
185                         $val = $attr;\r
186                     }\r
187                     $attr{$attr} = $val;\r
188                     push(@attrseq, $attr);\r
189                 }\r
190 \r
191                 # At the end there should be a closing ">"\r
192 ### Modified for <tag />, Lennart\r
193                 if ($$buf =~ s|^/>||) {\r
194                     $self->start($tag, \%attr, 1, \@attrseq, "$eaten>");\r
195                 } elsif ($$buf =~ s|^>||) {\r
196                 #if ($$buf =~ s|^>||) {\r
197                     $self->start($tag, \%attr, 0, \@attrseq, "$eaten>");\r
198                 } elsif (length $$buf) {\r
199                     # Not a conforming start tag, regard it as normal text\r
200                     $self->text($eaten);\r
201                 } else {\r
202                     $$buf = $eaten;  # need more data to know\r
203                     last TOKEN;\r
204                 }\r
205 \r
206             } elsif (length $$buf) {\r
207                 $self->text($eaten);\r
208             } else {\r
209                 $$buf = $eaten . $$buf;  # need more data to parse\r
210                 last TOKEN;\r
211             }\r
212 \r
213         } else {\r
214             #die if length($$buf);  # This should never happen\r
215             last TOKEN;             # The buffer should be empty now\r
216         }\r
217     }\r
218 \r
219     $self;\r
220 }\r
221 \r
222 \r
223 sub eof\r
224 {\r
225     shift->parse(undef);\r
226 }\r
227 \r
228 \r
229 sub parse_file\r
230 {\r
231     my($self, $file) = @_;\r
232     no strict 'refs';  # so that a symbol ref as $file works\r
233     local(*F);\r
234     unless (ref($file) || $file =~ /^\*[\w:]+$/) {\r
235         # Assume $file is a filename\r
236         open(F, $file) || die "Can't open $file: $!";\r
237         $file = \*F;\r
238     }\r
239     my $chunk = '';\r
240     while(read($file, $chunk, 512)) {\r
241         $self->parse($chunk);\r
242     }\r
243     close($file);\r
244     $self->eof;\r
245 }\r
246 \r
247 \r
248 sub strict_comment\r
249 {\r
250     my $self = shift;\r
251     my $old = $self->{'_strict_comment'};\r
252     $self->{'_strict_comment'} = shift if @_;\r
253     return $old;\r
254 }\r
255 \r
256 \r
257 sub netscape_buggy_comment  # legacy\r
258 {\r
259     my $self = shift;\r
260     my $old = !$self->strict_comment;\r
261     $self->strict_comment(!shift) if @_;\r
262     return $old;\r
263 }\r
264 \r
265 \r
266 sub text\r
267 {\r
268     # my($self, $text) = @_;\r
269 }\r
270 \r
271 sub declaration\r
272 {\r
273     # my($self, $decl) = @_;\r
274 }\r
275 \r
276 sub comment\r
277 {\r
278     # my($self, $comment) = @_;\r
279 }\r
280 \r
281 sub start\r
282 {\r
283 die "hie";\r
284     # my($self, $tag, $attr, $attrseq, $origtext) = @_;\r
285     # $attr is reference to a HASH, $attrseq is reference to an ARRAY\r
286 }\r
287 \r
288 sub end\r
289 {\r
290     # my($self, $tag, $origtext) = @_;\r
291 }\r
292 \r
293 1;\r
294 \r
295 \r
296 __END__\r
297 \r
298 \r
299 =head1 NAME\r
300 \r
301 HTML::Parser - SGML parser class\r
302 \r
303 =head1 SYNOPSIS\r
304 \r
305  require HTML::Parser;\r
306  $p = HTML::Parser->new;  # should really a be subclass\r
307  $p->parse($chunk1);\r
308  $p->parse($chunk2);\r
309  #...\r
310  $p->eof;                 # signal end of document\r
311 \r
312  # Parse directly from file\r
313  $p->parse_file("foo.html");\r
314  # or\r
315  open(F, "foo.html") || die;\r
316  $p->parse_file(\*F);\r
317 \r
318 =head1 DESCRIPTION\r
319 \r
320 The C<HTML::Parser> will tokenize an HTML document when the parse()\r
321 method is called by invoking various callback methods.  The document to\r
322 be parsed can be supplied in arbitrary chunks.\r
323 \r
324 The external interface the an I<HTML::Parser> is:\r
325 \r
326 =over 4\r
327 \r
328 =item $p = HTML::Parser->new\r
329 \r
330 The object constructor takes no arguments.\r
331 \r
332 =item $p->parse( $string );\r
333 \r
334 Parse the $string as an HTML document.  Can be called multiple times.\r
335 The return value is a reference to the parser object.\r
336 \r
337 =item $p->eof\r
338 \r
339 Signals end of document.  Call eof() to flush any remaining buffered\r
340 text.  The return value is a reference to the parser object.\r
341 \r
342 =item $p->parse_file( $file );\r
343 \r
344 This method can be called to parse text from a file.  The argument can\r
345 be a filename or an already opened file handle. The return value from\r
346 parse_file() is a reference to the parser object.\r
347 \r
348 =item $p->strict_comment( [$bool] )\r
349 \r
350 By default we parse comments similar to how the popular browsers (like\r
351 Netscape and MSIE) do it.  This means that comments will always be\r
352 terminated by the first occurrence of "-->".  This is not correct\r
353 according to the "official" HTML standards.  The official behaviour\r
354 can be enabled by calling the strict_comment() method with a TRUE\r
355 argument.\r
356 \r
357 The return value from strict_comment() is the old attribute value.\r
358 \r
359 =back\r
360 \r
361 \r
362 \r
363 In order to make the parser do anything interesting, you must make a\r
364 subclass where you override one or more of the following methods as\r
365 appropriate:\r
366 \r
367 =over 4\r
368 \r
369 =item $self->declaration($decl)\r
370 \r
371 This method is called when a I<markup declaration> has been\r
372 recognized.  For typical HTML documents, the only declaration you are\r
373 likely to find is <!DOCTYPE ...>.  The initial "<!" and ending ">" is\r
374 not part of the string passed as argument.  Comments are removed and\r
375 entities will B<not> be expanded.\r
376 \r
377 =item $self->start($tag, $attr, $attrseq, $origtext)\r
378 \r
379 This method is called when a complete start tag has been recognized.\r
380 The first argument is the tag name (in lower case) and the second\r
381 argument is a reference to a hash that contain all attributes found\r
382 within the start tag.  The attribute keys are converted to lower case.\r
383 Entities found in the attribute values are already expanded.  The\r
384 third argument is a reference to an array with the lower case\r
385 attribute keys in the original order.  The fourth argument is the\r
386 original HTML text.\r
387 \r
388 \r
389 =item $self->end($tag, $origtext)\r
390 \r
391 This method is called when an end tag has been recognized.  The\r
392 first argument is the lower case tag name, the second the original\r
393 HTML text of the tag.\r
394 \r
395 =item $self->text($text)\r
396 \r
397 This method is called when plain text in the document is recognized.\r
398 The text is passed on unmodified and might contain multiple lines.\r
399 Note that for efficiency reasons entities in the text are B<not>\r
400 expanded.  You should call HTML::Entities::decode($text) before you\r
401 process the text any further.\r
402 \r
403 A sequence of text in the HTML document can be broken between several\r
404 invocations of $self->text.  The parser will make sure that it does\r
405 not break a word or a sequence of spaces between two invocations of\r
406 $self->text().\r
407 \r
408 =item $self->comment($comment)\r
409 \r
410 This method is called as comments are recognized.  The leading and\r
411 trailing "--" sequences have been stripped off the comment text.\r
412 \r
413 =back\r
414 \r
415 The default implementation of these methods do nothing, i.e., the\r
416 tokens are just ignored.\r
417 \r
418 There is really nothing in the basic parser that is HTML specific, so\r
419 it is likely that the parser can parse other kinds of SGML documents.\r
420 SGML has many obscure features (not implemented by this module) that\r
421 prevent us from renaming this module as C<SGML::Parser>.\r
422 \r
423 =head1 EFFICIENCY\r
424 \r
425 The parser is fairly inefficient if the chunks passed to $p->parse()\r
426 are too big.  The reason is probably that perl ends up with a lot of\r
427 character copying when tokens are removed from the beginning of the\r
428 strings.  A chunk size of about 256-512 bytes was optimal in a test I\r
429 made with some real world HTML documents.  (The parser was about 3\r
430 times slower with a chunk size of 20K).\r
431 \r
432 =head1 SEE ALSO\r
433 \r
434 L<HTML::Entities>, L<HTML::TokeParser>, L<HTML::Filter>,\r
435 L<HTML::HeadParser>, L<HTML::LinkExtor>\r
436 \r
437 L<HTML::TreeBuilder> (part of the I<HTML-Tree> distribution)\r
438 \r
439 =head1 COPYRIGHT\r
440 \r
441 Copyright 1996-1999 Gisle Aas. All rights reserved.\r
442 \r
443 This library is free software; you can redistribute it and/or\r
444 modify it under the same terms as Perl itself.\r
445 \r
446 =cut\r
447 \r
448 \r