7 HTML::Tagset - data tables useful in parsing HTML
15 use vars qw( $VERSION );
22 # Then use any of the items in the HTML::Tagset package
27 This module contains several data tables useful in various kinds of
28 HTML parsing operations.
30 Note that all tag names used are lowercase.
32 In the following documentation, a "hashset" is a hash being used as a
33 set -- the hash conveys that its keys are there, and the actual values
34 associated with the keys are not significant. (But what values are
35 there, are always true.)
41 %emptyElement %optionalEndTag %linkElements %boolean_attr
42 %isHeadElement %isBodyElement %isPhraseMarkup
43 %is_Possible_Strict_P_Content
45 %isList %isTableElement %isFormElement
53 Note that none of these variables are exported.
55 =head2 hashset %HTML::Tagset::emptyElement
57 This hashset has as values the tag-names (GIs) of elements that cannot
58 have content. (For example, "base", "br", "hr".) So
59 C<$HTML::Tagset::emptyElement{'hr'}> exists and is true.
60 C<$HTML::Tagset::emptyElement{'dl'}> does not exist, and so is not true.
64 %emptyElement = map {; $_ => 1 } qw(base link meta isindex
72 # The "~"-initial names are for pseudo-elements used by HTML::Entities
75 =head2 hashset %HTML::Tagset::optionalEndTag
77 This hashset lists tag-names for elements that can have content, but whose
78 end-tags are generally, "safely", omissible. Example:
79 C<$HTML::Tagset::emptyElement{'li'}> exists and is true.
83 %optionalEndTag = map {; $_ => 1 } qw(p li dt dd); # option th tr td);
85 =head2 hash %HTML::Tagset::linkElements
87 Values in this hash are tagnames for elements that might contain
88 links, and the value for each is a reference to an array of the names
89 of attributes whose values can be links.
96 'applet' => ['archive', 'codebase', 'code'],
100 'blockquote' => ['cite'],
101 'body' => ['background'],
103 'embed' => ['pluginspage', 'src'],
104 'form' => ['action'],
105 'frame' => ['src', 'longdesc'],
106 'iframe' => ['src', 'longdesc'],
107 'ilayer' => ['background'],
108 'img' => ['src', 'lowsrc', 'longdesc', 'usemap'],
109 'input' => ['src', 'usemap'],
111 'isindex' => ['action'],
112 'head' => ['profile'],
113 'layer' => ['background', 'src'],
115 'object' => ['classid', 'codebase', 'data', 'archive', 'usemap'],
117 'script' => ['src', 'for'],
118 'table' => ['background'],
119 'td' => ['background'],
120 'th' => ['background'],
121 'tr' => ['background'],
125 =head2 hash %HTML::Tagset::boolean_attr
127 This hash (not hashset) lists what attributes of what elements can be
128 printed without showing the value (for example, the "noshade" attribute
129 of "hr" elements). For elements with only one such attribute, its value
130 is simply that attribute name. For elements with many such attributes,
131 the value is a reference to a hashset containing all such attributes.
136 # TODO: make these all hashes
142 'input' => { 'checked' => 1, 'readonly' => 1, 'disabled' => 1 },
145 'option' => 'selected',
146 'select' => 'multiple',
152 #==========================================================================
153 # List of all elements from Extensible HTML version 1.0 Transitional DTD:
155 # a abbr acronym address applet area b base basefont bdo big
156 # blockquote body br button caption center cite code col colgroup
157 # dd del dfn dir div dl dt em fieldset font form h1 h2 h3 h4 h5 h6
158 # head hr html i iframe img input ins isindex kbd label legend li
159 # link map menu meta noframes noscript object ol optgroup option p
160 # param pre q s samp script select small span strike strong style
161 # sub sup table tbody td textarea tfoot th thead title tr tt u ul
164 # Varia from Mozilla source internal table of tags:
166 # xmp listing wbr nobr frame frameset noframes ilayer
167 # layer nolayer spacer embed multicol
168 # But these are unimplemented:
169 # sound?? keygen?? server??
170 # Also seen here and there:
171 # marquee?? app?? (both unimplemented)
172 #==========================================================================
174 =head2 hashset %HTML::Tagset::isPhraseMarkup
176 This hashset contains all phrasal-level elements.
180 %isPhraseMarkup = map {; $_ => 1 } qw(
181 span abbr acronym q sub sup
182 cite code em kbd samp strong var dfn strike
188 ); # had: center, hr, table
191 =head2 hashset %HTML::Tagset::is_Possible_Strict_P_Content
193 This hashset contains all phrasal-level elements that be content of a
194 P element, for a strict model of HTML.
198 %is_Possible_Strict_P_Content = (
201 map {; $_ => 1} qw( object script map )
202 # I've no idea why there's these latter exceptions.
203 # I'm just following the HTML4.01 DTD.
207 #<!ENTITY % fontstyle "TT | I | B | BIG | SMALL">
209 #<!ENTITY % phrase "EM | STRONG | DFN | CODE |
210 # SAMP | KBD | VAR | CITE | ABBR | ACRONYM" >
213 # "A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">
215 #<!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
217 #<!-- %inline; covers inline or "text-level" elements -->
218 #<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
220 =head2 hashset %HTML::Tagset::isHeadElement
222 This hashset contains all elements that elements that should be
223 present only in the 'head' element of an HTML document.
227 %isHeadElement = map {; $_ => 1 }
228 qw(title base link meta isindex script style object bgsound);
230 =head2 hashset %HTML::Tagset::isList
232 This hashset contains all elements that can contain "li" elements.
236 %isList = map {; $_ => 1 } qw(ul ol dir menu);
238 =head2 hashset %HTML::Tagset::isTableElement
240 This hashset contains all elements that are to be found only in/under
245 %isTableElement = map {; $_ => 1 }
246 qw(tr td th thead tbody tfoot caption col colgroup);
248 =head2 hashset %HTML::Tagset::isFormElement
250 This hashset contains all elements that are to be found only in/under
255 %isFormElement = map {; $_ => 1 }
256 qw(input select option optgroup textarea button label);
258 =head2 hashset %HTML::Tagset::isBodyMarkup
260 This hashset contains all elements that are to be found only in/under
261 the "body" element of an HTML document.
265 %isBodyElement = map {; $_ => 1 } qw(
267 p div pre plaintext address blockquote
272 iframe ilayer nolayer
284 isindex script noscript
290 keys %isPhraseMarkup, # And everything phrasal
291 keys %isTableElement,
295 =head2 hashset %HTML::Tagset::isHeadOrBodyElement
297 This hashset includes all elements that I notice can fall either in
298 the head or in the body.
302 %isHeadOrBodyElement = map {; $_ => 1 }
303 qw(script isindex style object map area param noscript bgsound);
304 # i.e., if we find 'script' in the 'body' or the 'head', don't freak out.
307 =head2 hashset %HTML::Tagset::isKnown
309 This hashset lists all known HTML elements.
313 %isKnown = (%isHeadElement, %isBodyElement,
316 frame frameset noframes
317 ~comment ~pi ~directive ~literal
319 # that should be all known tags ever ever
322 =head2 hashset %HTML::Tagset::canTighten
324 This hashset lists elements that might have ignorable whitespace as
325 children or siblings.
329 %canTighten = %isKnown;
331 keys(%isPhraseMarkup), 'input', 'select',
332 'xmp', 'listing', 'plaintext', 'pre',
334 # xmp, listing, plaintext, and pre are untightenable, and
335 # in a really special way.
336 @canTighten{'hr','br'} = (1,1);
337 # exceptional 'phrasal' things that ARE subject to tightening.
339 # The one case where I can think of my tightening rules failing is:
340 # <p>foo bar<center> <em>baz quux</em> ...
341 # ^-- that would get deleted.
342 # But that's pretty gruesome code anyhow. You gets what you pays for.
344 #==========================================================================
346 =head2 array @HTML::Tagset::p_closure_barriers
348 This array has a meaning that I have only seen a need for in
349 C<HTML::TreeBuilder>, but I include it here on the off chance that someone
350 might find it of use:
352 When we see a "E<lt>pE<gt>" token, we go lookup up the lineage for a p
353 element we might have to minimize. At first sight, we might say that
354 if there's a p anywhere in the lineage of this new p, it should be
355 closed. But that's wrong. Consider this document:
375 The second p is quite legally inside a much higher p.
377 My formalization of the reason why this is legal, but this:
381 isn't, is that something about the table constitutes a "barrier" to
382 the application of the rule about what p must minimize.
384 So C<@HTML::Tagset::p_closure_barriers> is the list of all such
389 @p_closure_barriers = qw(
393 td th tr table caption
397 # In an ideal world (i.e., XHTML) we wouldn't have to bother with any of this
398 # monkey business of barriers to minimization!
400 =head2 hashset %isCDATA_Parent
402 This hashset includes all elements whose content is CDATA.
406 %isCDATA_Parent = map {; $_ => 1 }
407 qw(script style xmp listing plaintext);
409 # TODO: there's nothing else that takes CDATA children, right?
411 # As the HTML3 DTD (Raggett 1995-04-24) noted:
412 # The XMP, LISTING and PLAINTEXT tags are incompatible with SGML
413 # and derive from very early versions of HTML. They require non-
414 # standard parsers and will cause problems for processing
415 # documents with standard SGML tools.
420 You may find it useful to alter the behavior of modules (like
421 C<HTML::Element> or C<HTML::TreeBuilder>) that use C<HTML::Tagset>'s
422 data tables by altering the data tables themselves. You are welcome
423 to try, but be careful; and be aware that different modules may or may
424 react differently to the data tables being changed.
426 Note that it may be inappropriate to use these tables for I<producing>
427 HTML -- for example, C<%isHeadOrBodyElement> lists the tagnames
428 for all elements that can appear either in the head or in the body,
429 such as "script". That doesn't mean that I am saying your code that
430 produces HTML should feel free to put script elements in either place!
431 If you are producing programs that spit out HTML, you should be
432 I<intimately> familiar with the DTDs for HTML or XHTML (available at
433 C<http://www.w3.org/>), and you should slavishly obey them, not
434 the data tables in this document.
438 L<HTML::Element>, L<HTML::TreeBuilder>, L<HTML::LinkExtor>
440 =head1 COPYRIGHT & LICENSE
442 Copyright 1995-2000 Gisle Aas.
444 Copyright 2000-2005 Sean M. Burke.
446 Copyright 2005-2008 Andy Lester.
448 This program is free software; you can redistribute it and/or modify it
449 under the same terms as Perl itself.
451 =head1 ACKNOWLEDGEMENTS
453 Most of the code/data in this module was adapted from code written
454 by Gisle Aas for C<HTML::Element>, C<HTML::TreeBuilder>, and
455 C<HTML::LinkExtor>. Then it was maintained by Sean M. Burke.
459 Current maintainer: Andy Lester, C<< <andy at petdance.com> >>
463 Please report any bugs or feature requests to
464 C<bug-html-tagset at rt.cpan.org>, or through the web interface at
465 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-Tagset>. I will
466 be notified, and then you'll automatically be notified of progress on
467 your bug as I make changes.